Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk.Int in sdk.Coin #1218

Merged
merged 7 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- checkout
- restore_cache:
keys:
- v1-pkg-cache
- v2-pkg-cache
- run:
name: tools
command: |
Expand All @@ -39,11 +39,11 @@ jobs:
- bin
- profiles
- save_cache:
key: v1-pkg-cache
key: v2-pkg-cache
paths:
- /go/pkg
- save_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
paths:
- /go/src/github.com/cosmos/cosmos-sdk

Expand All @@ -54,9 +54,9 @@ jobs:
- attach_workspace:
at: /tmp/workspace
- restore_cache:
key: v1-pkg-cache
key: v2-pkg-cache
- restore_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Get metalinter
command: |
Expand All @@ -76,9 +76,9 @@ jobs:
- attach_workspace:
at: /tmp/workspace
- restore_cache:
key: v1-pkg-cache
key: v2-pkg-cache
- restore_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Test unit
command: |
Expand All @@ -92,9 +92,9 @@ jobs:
- attach_workspace:
at: /tmp/workspace
- restore_cache:
key: v1-pkg-cache
key: v2-pkg-cache
- restore_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Test cli
command: |
Expand All @@ -108,9 +108,9 @@ jobs:
- attach_workspace:
at: /tmp/workspace
- restore_cache:
key: v1-pkg-cache
key: v2-pkg-cache
- restore_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Run tests
command: |
Expand All @@ -132,7 +132,7 @@ jobs:
- attach_workspace:
at: /tmp/workspace
- restore_cache:
key: v1-tree-{{ .Environment.CIRCLE_SHA1 }}
key: v2-tree-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: gather
command: |
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ BREAKING CHANGES
BREAKING CHANGES
* msg.GetSignBytes() now returns bech32-encoded addresses in all cases
* [lcd] REST end-points now include gas
* sdk.Coin now uses sdk.Int, a big.Int wrapper with 256bit range cap

FEATURES
* [x/auth] Added AccountNumbers to BaseAccount and StdTxs to allow for replay protection with account pruning
Expand Down
26 changes: 15 additions & 11 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,17 @@ func TestCoinSend(t *testing.T) {
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
mycoins := coins[0]

assert.Equal(t, "steak", mycoins.Denom)
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
assert.Equal(t, initialBalance[0].Amount.SubRaw(1), mycoins.Amount)

// query receiver
acc = getAccount(t, port, receiveAddr)
coins = acc.GetCoins()
mycoins = coins[0]

assert.Equal(t, "steak", mycoins.Denom)
assert.Equal(t, int64(1), mycoins.Amount)
assert.Equal(t, int64(1), mycoins.Amount.Int64())
}

func TestIBCTransfer(t *testing.T) {
Expand All @@ -274,8 +276,9 @@ func TestIBCTransfer(t *testing.T) {
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
mycoins := coins[0]

assert.Equal(t, "steak", mycoins.Denom)
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
assert.Equal(t, initialBalance[0].Amount.SubRaw(1), mycoins.Amount)

// TODO: query ibc egress packet state
}
Expand Down Expand Up @@ -383,7 +386,8 @@ func TestBonding(t *testing.T) {
// query sender
acc := getAccount(t, port, addr)
coins := acc.GetCoins()
assert.Equal(t, int64(40), coins.AmountOf(denom))

assert.Equal(t, int64(40), coins.AmountOf(denom).Int64())

// query validator
bond := getDelegation(t, port, addr, validator1Owner)
Expand Down Expand Up @@ -438,19 +442,19 @@ func doSend(t *testing.T, port, seed, name, password string, addr sdk.Address) (
sequence := acc.GetSequence()

// send
coinbz, err := json.Marshal(sdk.NewCoin("steak", 1))
if err != nil {
panic(err)
}

jsonStr := []byte(fmt.Sprintf(`{
"name":"%s",
"password":"%s",
"account_number":%d,
"sequence":%d,
"gas": 10000,
"amount":[
{
"denom": "%s",
"amount": 1
}
]
}`, name, password, accnum, sequence, "steak"))
"amount":[%s]
}`, name, password, accnum, sequence, coinbz))
res, body := Request(t, port, "POST", "/accounts/"+receiveAddrBech+"/send", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)

Expand Down
2 changes: 1 addition & 1 deletion client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.Address) (
// add some tokens to init accounts
for _, addr := range initAddrs {
accAuth := auth.NewBaseAccountWithAddress(addr)
accAuth.Coins = sdk.Coins{{"steak", 100}}
accAuth.Coins = sdk.Coins{sdk.NewCoin("steak", 100)}
acc := gapp.NewGenesisAccount(&accAuth)
genesisState.Accounts = append(genesisState.Accounts, acc)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

var (
// bonded tokens given to genesis validators/accounts
freeTokenVal = int64(100)
freeTokensAcc = int64(50)
freeFermionVal = sdk.NewInt(100)
freeFermionsAcc = sdk.NewInt(50)
)

// State to Unmarshal
Expand Down Expand Up @@ -123,7 +123,7 @@ func GaiaAppGenTxNF(cdc *wire.Codec, pk crypto.PubKey, addr sdk.Address, name st

validator = tmtypes.GenesisValidator{
PubKey: pk,
Power: freeTokenVal,
Power: freeFermionVal.Int64(),
}
return
}
Expand Down Expand Up @@ -153,23 +153,23 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState
// create the genesis account, give'm few steaks and a buncha token with there name
accAuth := auth.NewBaseAccountWithAddress(genTx.Address)
accAuth.Coins = sdk.Coins{
{genTx.Name + "Token", 1000},
{"steak", freeTokensAcc},
{genTx.Name + "Token", sdk.NewInt(1000)},
{"steak", freeFermionsAcc},
}
acc := NewGenesisAccount(&accAuth)
genaccs[i] = acc
stakeData.Pool.LooseUnbondedTokens += freeTokensAcc // increase the supply
stakeData.Pool.LooseUnbondedTokens = stakeData.Pool.LooseUnbondedTokens.Add(freeFermionsAcc) // increase the supply

// add the validator
if len(genTx.Name) > 0 {
desc := stake.NewDescription(genTx.Name, "", "", "")
validator := stake.NewValidator(genTx.Address, genTx.PubKey, desc)
validator.PoolShares = stake.NewBondedShares(sdk.NewRat(freeTokenVal))
validator.PoolShares = stake.NewBondedShares(sdk.NewRatFromInt(freeFermionVal))
stakeData.Validators = append(stakeData.Validators, validator)

// pool logic
stakeData.Pool.BondedTokens += freeTokenVal
stakeData.Pool.BondedShares = sdk.NewRat(stakeData.Pool.BondedTokens)
stakeData.Pool.BondedTokens = stakeData.Pool.BondedTokens.Add(freeFermionVal)
stakeData.Pool.BondedShares = sdk.NewRatFromInt(stakeData.Pool.BondedTokens)
}
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ func TestGaiaCLISend(t *testing.T) {
require.NoError(t, err)

fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooCech, flags))
assert.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak").Int64())

executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass)
tests.WaitForNextHeightTM(port)

barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak").Int64())
fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooCech, flags))
assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak").Int64())

// test autosequencing
executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass)
tests.WaitForNextHeightTM(port)

barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
assert.Equal(t, int64(20), barAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(20), barAcc.GetCoins().AmountOf("steak").Int64())
fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooCech, flags))
assert.Equal(t, int64(30), fooAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(30), fooAcc.GetCoins().AmountOf("steak").Int64())
}

func TestGaiaCLICreateValidator(t *testing.T) {
Expand Down Expand Up @@ -98,9 +98,9 @@ func TestGaiaCLICreateValidator(t *testing.T) {
tests.WaitForNextHeightTM(port)

barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak").Int64())
fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooCech, flags))
assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak"))
assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak").Int64())

// create validator
cvStr := fmt.Sprintf("gaiacli stake create-validator %v", flags)
Expand All @@ -114,7 +114,7 @@ func TestGaiaCLICreateValidator(t *testing.T) {
tests.WaitForNextHeightTM(port)

barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
require.Equal(t, int64(8), barAcc.GetCoins().AmountOf("steak"), "%v", barAcc)
require.Equal(t, int64(8), barAcc.GetCoins().AmountOf("steak").Int64(), "%v", barAcc)

validator := executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %v --output=json %v", barCech, flags))
assert.Equal(t, validator.Owner, barAddr)
Expand All @@ -133,7 +133,7 @@ func TestGaiaCLICreateValidator(t *testing.T) {
tests.WaitForNextHeightTM(port)

barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
require.Equal(t, int64(9), barAcc.GetCoins().AmountOf("steak"), "%v", barAcc)
require.Equal(t, int64(9), barAcc.GetCoins().AmountOf("steak").Int64(), "%v", barAcc)
validator = executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %v --output=json %v", barCech, flags))
assert.Equal(t, "1/1", validator.PoolShares.Amount.String())
}
Expand Down
10 changes: 5 additions & 5 deletions examples/democoin/x/cool/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func TestMsgQuiz(t *testing.T) {
// Set the trend, submit a really cool quiz and check for reward
mock.SignCheckDeliver(t, mapp.BaseApp, setTrendMsg1, []int64{0}, []int64{0}, true, priv1)
mock.SignCheckDeliver(t, mapp.BaseApp, quizMsg1, []int64{0}, []int64{1}, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", 69}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", 69)})
mock.SignCheckDeliver(t, mapp.BaseApp, quizMsg2, []int64{0}, []int64{2}, false, priv1) // result without reward
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", 69}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", 69)})
mock.SignCheckDeliver(t, mapp.BaseApp, quizMsg1, []int64{0}, []int64{3}, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", 138}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", 138)})
mock.SignCheckDeliver(t, mapp.BaseApp, setTrendMsg2, []int64{0}, []int64{4}, true, priv1) // reset the trend
mock.SignCheckDeliver(t, mapp.BaseApp, quizMsg1, []int64{0}, []int64{5}, false, priv1) // the same answer will nolonger do!
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", 138}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", 138)})
mock.SignCheckDeliver(t, mapp.BaseApp, quizMsg2, []int64{0}, []int64{6}, true, priv1) // earlier answer now relavent again
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"badvibesonly", 69}, {"icecold", 138}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("badvibesonly", 69), sdk.NewCoin("icecold", 138)})
mock.SignCheckDeliver(t, mapp.BaseApp, setTrendMsg3, []int64{0}, []int64{7}, false, priv1) // expect to fail to set the trend to something which is not cool
}
2 changes: 1 addition & 1 deletion examples/democoin/x/cool/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func handleMsgQuiz(ctx sdk.Context, k Keeper, msg MsgQuiz) sdk.Result {
return sdk.Result{} // TODO
}

bonusCoins := sdk.Coins{{msg.CoolAnswer, 69}}
bonusCoins := sdk.Coins{sdk.NewCoin(msg.CoolAnswer, 69)}

_, _, err := k.ck.AddCoins(ctx, msg.Sender, bonusCoins)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions examples/democoin/x/pow/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func TestMsgMine(t *testing.T) {
// Mine and check for reward
mineMsg1 := GenerateMsgMine(addr1, 1, 2)
mock.SignCheckDeliver(t, mapp.BaseApp, mineMsg1, []int64{0}, []int64{0}, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", 1}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", 1)})
// Mine again and check for reward
mineMsg2 := GenerateMsgMine(addr1, 2, 3)
mock.SignCheckDeliver(t, mapp.BaseApp, mineMsg2, []int64{0}, []int64{1}, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", 2}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", 2)})
// Mine again - should be invalid
mock.SignCheckDeliver(t, mapp.BaseApp, mineMsg2, []int64{0}, []int64{1}, false, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", 2}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", 2)})
}
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (k Keeper) CheckValid(ctx sdk.Context, difficulty uint64, count uint64) (ui

// Add some coins for a POW well done
func (k Keeper) ApplyValid(ctx sdk.Context, sender sdk.Address, newDifficulty uint64, newCount uint64) sdk.Error {
_, _, ckErr := k.ck.AddCoins(ctx, sender, []sdk.Coin{sdk.Coin{k.config.Denomination, k.config.Reward}})
_, _, ckErr := k.ck.AddCoins(ctx, sender, []sdk.Coin{sdk.NewCoin(k.config.Denomination, k.config.Reward)})
if ckErr != nil {
return ckErr
}
Expand Down
6 changes: 3 additions & 3 deletions examples/democoin/x/simplestake/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (k Keeper) Bond(ctx sdk.Context, addr sdk.Address, pubKey crypto.PubKey, st
}
}

bi.Power = bi.Power + stake.Amount
bi.Power = bi.Power + stake.Amount.Int64()

k.setBondInfo(ctx, addr, bi)
return bi.Power, nil
Expand All @@ -93,7 +93,7 @@ func (k Keeper) Unbond(ctx sdk.Context, addr sdk.Address) (crypto.PubKey, int64,
}
k.deleteBondInfo(ctx, addr)

returnedBond := sdk.Coin{stakingToken, bi.Power}
returnedBond := sdk.NewCoin(stakingToken, bi.Power)

_, _, err := k.ck.AddCoins(ctx, addr, []sdk.Coin{returnedBond})
if err != nil {
Expand All @@ -118,7 +118,7 @@ func (k Keeper) bondWithoutCoins(ctx sdk.Context, addr sdk.Address, pubKey crypt
}
}

bi.Power = bi.Power + stake.Amount
bi.Power = bi.Power + stake.Amount.Int64()

k.setBondInfo(ctx, addr, bi)
return bi.Power, nil
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/simplestake/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestBonding(t *testing.T) {
_, _, err := stakeKeeper.unbondWithoutCoins(ctx, addr)
assert.Equal(t, err, ErrInvalidUnbond(DefaultCodespace))

_, err = stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.Coin{"steak", 10})
_, err = stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.NewCoin("steak", 10))
assert.Nil(t, err)

power, err := stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.Coin{"steak", 10})
power, err := stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.NewCoin("steak", 10))
assert.Equal(t, int64(20), power)

pk, _, err := stakeKeeper.unbondWithoutCoins(ctx, addr)
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/simplestake/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestBondMsgValidation(t *testing.T) {
valid bool
msgBond MsgBond
}{
{true, NewMsgBond(sdk.Address{}, sdk.Coin{"mycoin", 5}, privKey.PubKey())},
{false, NewMsgBond(sdk.Address{}, sdk.Coin{"mycoin", 0}, privKey.PubKey())},
{true, NewMsgBond(sdk.Address{}, sdk.NewCoin("mycoin", 5), privKey.PubKey())},
{false, NewMsgBond(sdk.Address{}, sdk.NewCoin("mycoin", 0), privKey.PubKey())},
}

for i, tc := range cases {
Expand Down
Loading