From 4290946f56347de22e10c961c47b14d81cff1afc Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 30 Jan 2020 13:57:26 -0500 Subject: [PATCH] Merge PR #5580: ADR 004 Implementation II (Unit Tests & Misc.) --- simapp/genesis_account_test.go | 17 +- simapp/test_helpers.go | 3 +- store/prefix/store_test.go | 4 +- x/auth/ante/ante_test.go | 61 +- x/auth/ante/fee_test.go | 4 +- x/auth/exported/exported_test.go | 2 +- x/auth/keeper/keeper_bench_test.go | 52 -- x/auth/legacy/v0_38/migrate.go | 4 +- x/auth/legacy/v0_38/types.go | 12 +- x/auth/legacy/v0_39/migrate.go | 23 + x/auth/legacy/v0_39/migrate_test.go | 87 +++ x/auth/legacy/v0_39/types.go | 8 + x/auth/types/account_test.go | 21 +- x/auth/types/genesis_test.go | 15 - x/auth/vesting/types/genesis_test.go | 12 +- x/auth/vesting/types/vesting_account_test.go | 353 ++++------ x/bank/alias.go | 2 +- x/bank/app_test.go | 66 +- x/bank/bench_test.go | 37 +- x/bank/genesis.go | 11 +- x/bank/internal/keeper/integration_test.go | 19 - x/bank/internal/keeper/keeper.go | 18 +- x/bank/internal/keeper/keeper_test.go | 650 ++++++++++-------- x/bank/internal/keeper/querier_test.go | 96 ++- x/bank/internal/types/key.go | 16 +- x/bank/internal/types/key_test.go | 7 +- x/bank/legacy/v0_38/types.go | 14 + x/bank/legacy/v0_39/migrate.go | 22 + x/bank/legacy/v0_39/migrate_test.go | 67 ++ x/bank/legacy/v0_39/types.go | 43 ++ x/bank/simulation/genesis.go | 3 - x/bank/simulation/operations.go | 38 +- x/crisis/handler_test.go | 2 +- x/distribution/keeper/allocation_test.go | 10 +- x/distribution/keeper/delegation_test.go | 27 +- x/distribution/keeper/keeper_test.go | 16 +- x/distribution/keeper/querier_test.go | 2 +- x/distribution/keeper/test_common.go | 31 +- x/distribution/proposal_handler_test.go | 34 +- x/distribution/simulation/operations.go | 16 +- x/distribution/types/expected_keepers.go | 1 + x/evidence/internal/keeper/infraction_test.go | 14 +- x/evidence/internal/keeper/keeper_test.go | 6 + x/genutil/client/cli/migrate.go | 2 + x/genutil/legacy/v0_39/migrate.go | 55 ++ x/gov/abci_test.go | 7 +- x/gov/genesis_test.go | 6 +- x/gov/keeper/deposit_test.go | 16 +- x/gov/keeper/keeper_test.go | 4 +- x/gov/keeper/proposal_test.go | 8 +- x/gov/keeper/querier_test.go | 4 +- x/gov/keeper/tally_test.go | 30 +- x/gov/keeper/test_common.go | 23 +- x/gov/keeper/vote_test.go | 2 +- x/gov/simulation/operations.go | 17 +- x/gov/test_common.go | 38 +- x/gov/types/expected_keepers.go | 1 + x/simulation/account_test.go | 2 + x/simulation/rand_util_test.go | 2 + x/slashing/abci_test.go | 4 +- x/slashing/app_test.go | 32 +- x/slashing/handler_test.go | 19 +- x/slashing/internal/keeper/keeper_test.go | 6 +- x/slashing/internal/keeper/test_common.go | 16 +- x/slashing/internal/types/expected_keepers.go | 1 + x/slashing/simulation/operations.go | 6 +- x/staking/app_test.go | 48 +- x/staking/genesis_test.go | 8 +- x/staking/handler_test.go | 66 +- x/staking/keeper/delegation_test.go | 137 ++-- x/staking/keeper/historical_info_test.go | 4 +- x/staking/keeper/keeper_test.go | 2 +- x/staking/keeper/querier_test.go | 21 +- x/staking/keeper/slash_test.go | 162 +++-- x/staking/keeper/test_common.go | 21 +- x/staking/keeper/validator_test.go | 74 +- x/staking/simulation/operations.go | 33 +- x/staking/types/expected_keepers.go | 1 + x/supply/internal/keeper/bank_test.go | 62 +- x/supply/internal/types/account_test.go | 7 +- x/supply/internal/types/expected_keepers.go | 3 + 81 files changed, 1692 insertions(+), 1204 deletions(-) create mode 100644 x/auth/legacy/v0_39/migrate.go create mode 100644 x/auth/legacy/v0_39/migrate_test.go create mode 100644 x/auth/legacy/v0_39/types.go delete mode 100644 x/bank/internal/keeper/integration_test.go create mode 100644 x/bank/legacy/v0_38/types.go create mode 100644 x/bank/legacy/v0_39/migrate.go create mode 100644 x/bank/legacy/v0_39/migrate_test.go create mode 100644 x/bank/legacy/v0_39/types.go create mode 100644 x/genutil/legacy/v0_39/migrate.go diff --git a/simapp/genesis_account_test.go b/simapp/genesis_account_test.go index 6c0c6d6775c5..385bb7b217b6 100644 --- a/simapp/genesis_account_test.go +++ b/simapp/genesis_account_test.go @@ -20,8 +20,7 @@ func TestSimGenesisAccountValidate(t *testing.T) { vestingStart := time.Now().UTC() coins := sdk.NewCoins(sdk.NewInt64Coin("test", 1000)) - baseAcc := authtypes.NewBaseAccount(addr, nil, pubkey, 0, 0) - require.NoError(t, baseAcc.SetCoins(coins)) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 0, 0) testCases := []struct { name string @@ -38,14 +37,14 @@ func TestSimGenesisAccountValidate(t *testing.T) { { "invalid basic account with mismatching address/pubkey", simapp.SimGenesisAccount{ - BaseAccount: authtypes.NewBaseAccount(addr, nil, secp256k1.GenPrivKey().PubKey(), 0, 0), + BaseAccount: authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0), }, true, }, { "valid basic account with module name", simapp.SimGenesisAccount{ - BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(crypto.AddressHash([]byte("testmod"))), nil, nil, 0, 0), + BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(crypto.AddressHash([]byte("testmod"))), nil, 0, 0), ModuleName: "testmod", }, false, @@ -78,16 +77,6 @@ func TestSimGenesisAccountValidate(t *testing.T) { }, true, }, - { - "valid basic account with invalid original vesting coins", - simapp.SimGenesisAccount{ - BaseAccount: baseAcc, - OriginalVesting: coins.Add(coins...), - StartTime: vestingStart.Unix(), - EndTime: vestingStart.Add(1 * time.Hour).Unix(), - }, - true, - }, } for _, tc := range testCases { diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index edadd7d749ee..3f2967e58f16 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -1,7 +1,6 @@ package simapp import ( - "os" "testing" "github.com/stretchr/testify/require" @@ -48,7 +47,7 @@ func Setup(isCheckTx bool) *SimApp { // genesis accounts. func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount) *SimApp { db := dbm.NewMemDB() - app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0) + app := NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 0) // initialize the chain with the passed in genesis accounts genesisState := NewDefaultGenesisState() diff --git a/store/prefix/store_test.go b/store/prefix/store_test.go index 8431459748d0..b5e99329055b 100644 --- a/store/prefix/store_test.go +++ b/store/prefix/store_test.go @@ -18,9 +18,7 @@ import ( // copied from iavl/store_test.go var ( - cacheSize = 100 - numRecent int64 = 5 - storeEvery int64 = 3 + cacheSize = 100 ) func bz(s string) []byte { return []byte(s) } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 1d55b4d320c3..752895456970 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -47,17 +47,17 @@ func TestSimulateGasCost(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, acc1.GetAddress(), types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, acc2.GetAddress(), types.NewTestCoins()) acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) - acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) app.AccountKeeper.SetAccount(ctx, acc3) + app.BankKeeper.SetBalances(ctx, acc3.GetAddress(), types.NewTestCoins()) // set up msgs and fee var tx sdk.Tx @@ -128,8 +128,8 @@ func TestAnteHandlerSigErrors(t *testing.T) { // save the first account, but second is still unrecognized acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(fee.Amount) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, fee.Amount) checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnknownAddress) } @@ -146,13 +146,13 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) // msg and signatures var tx sdk.Tx @@ -203,12 +203,12 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { // set the accounts, we don't need the acc numbers as it is in the genesis block acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) // msg and signatures var tx sdk.Tx @@ -260,17 +260,17 @@ func TestAnteHandlerSequences(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) - acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) app.AccountKeeper.SetAccount(ctx, acc3) + app.BankKeeper.SetBalances(ctx, addr3, types.NewTestCoins()) // msg and signatures var tx sdk.Tx @@ -347,19 +347,21 @@ func TestAnteHandlerFees(t *testing.T) { tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds) - acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds) - require.True(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) - require.True(sdk.IntEq(t, app.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149))) + modAcc := app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName) + + require.True(t, app.BankKeeper.GetAllBalances(ctx, modAcc.GetAddress()).Empty()) + require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, addr1).AmountOf("atom"), sdk.NewInt(149))) - acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) checkValidTx(t, anteHandler, ctx, tx, false) - require.True(sdk.IntEq(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150))) - require.True(sdk.IntEq(t, app.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(0))) + require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150))) + require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, addr1).AmountOf("atom"), sdk.NewInt(0))) } // Test logic around memo gas consumption. @@ -416,17 +418,17 @@ func TestAnteHandlerMultiSigner(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) - acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) app.AccountKeeper.SetAccount(ctx, acc3) + app.BankKeeper.SetBalances(ctx, addr3, types.NewTestCoins()) // set up msgs and fee var tx sdk.Tx @@ -465,13 +467,13 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -542,13 +544,13 @@ func TestAnteHandlerSetPubKey(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) + app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins()) var tx sdk.Tx @@ -669,9 +671,9 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) { // set the accounts for i, addr := range addrs { acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - acc.SetCoins(types.NewTestCoins()) acc.SetAccountNumber(uint64(i)) app.AccountKeeper.SetAccount(ctx, acc) + app.BankKeeper.SetBalances(ctx, addr, types.NewTestCoins()) } var tx sdk.Tx @@ -705,8 +707,8 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { // verify that an secp256k1 account gets rejected priv1, _, addr1 := types.KeyTestPubAddr() acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - _ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -721,7 +723,8 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { pub2 := priv2.PubKey() addr2 := sdk.AccAddress(pub2.Address()) acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))) + + require.NoError(t, app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))) require.NoError(t, acc2.SetAccountNumber(1)) app.AccountKeeper.SetAccount(ctx, acc2) msg = types.NewTestMsg(addr2) @@ -745,9 +748,9 @@ func TestAnteHandlerReCheck(t *testing.T) { // set the accounts acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins()) antehandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) @@ -806,8 +809,8 @@ func TestAnteHandlerReCheck(t *testing.T) { ctx = ctx.WithMinGasPrices(sdk.DecCoins{}) // remove funds for account so antehandler fails on recheck - acc1.SetCoins(sdk.Coins{}) app.AccountKeeper.SetAccount(ctx, acc1) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins()) _, err = antehandler(ctx, tx, false) require.NotNil(t, err, "antehandler on recheck did not fail once feePayer no longer has sufficient funds") diff --git a/x/auth/ante/fee_test.go b/x/auth/ante/fee_test.go index b088c1961951..e489b19ba8fb 100644 --- a/x/auth/ante/fee_test.go +++ b/x/auth/ante/fee_test.go @@ -78,8 +78,8 @@ func TestDeductFees(t *testing.T) { // Set account with insufficient funds acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) app.AccountKeeper.SetAccount(ctx, acc) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))) dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.SupplyKeeper) antehandler := sdk.ChainAnteDecorators(dfd) @@ -89,8 +89,8 @@ func TestDeductFees(t *testing.T) { require.NotNil(t, err, "Tx did not error when fee payer had insufficient funds") // Set account with sufficient funds - acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(200))}) app.AccountKeeper.SetAccount(ctx, acc) + app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) _, err = antehandler(ctx, tx, false) diff --git a/x/auth/exported/exported_test.go b/x/auth/exported/exported_test.go index 8eaab048a8ae..23de4f777e72 100644 --- a/x/auth/exported/exported_test.go +++ b/x/auth/exported/exported_test.go @@ -14,7 +14,7 @@ import ( func TestGenesisAccountsContains(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) - acc := authtypes.NewBaseAccount(addr, nil, secp256k1.GenPrivKey().PubKey(), 0, 0) + acc := authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0) genAccounts := exported.GenesisAccounts{} require.False(t, genAccounts.Contains(acc.GetAddress())) diff --git a/x/auth/keeper/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go index 818c36a7a949..a217eddacae3 100644 --- a/x/auth/keeper/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -24,34 +24,6 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) { } } -func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) { - app, ctx := createTestApp(false) - - coins := sdk.Coins{ - sdk.NewCoin("ltc", sdk.NewInt(1000)), - sdk.NewCoin("btc", sdk.NewInt(1000)), - sdk.NewCoin("eth", sdk.NewInt(1000)), - sdk.NewCoin("xrp", sdk.NewInt(1000)), - sdk.NewCoin("bch", sdk.NewInt(1000)), - sdk.NewCoin("eos", sdk.NewInt(1000)), - } - - // assumes b.N < 2**24 - for i := 0; i < b.N; i++ { - arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - addr := sdk.AccAddress(arr) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - acc.SetCoins(coins) - app.AccountKeeper.SetAccount(ctx, acc) - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - app.AccountKeeper.GetAccount(ctx, sdk.AccAddress(arr)) - } -} - func BenchmarkAccountMapperSetAccount(b *testing.B) { app, ctx := createTestApp(false) @@ -65,27 +37,3 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { app.AccountKeeper.SetAccount(ctx, acc) } } - -func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) { - app, ctx := createTestApp(false) - - coins := sdk.Coins{ - sdk.NewCoin("ltc", sdk.NewInt(1000)), - sdk.NewCoin("btc", sdk.NewInt(1000)), - sdk.NewCoin("eth", sdk.NewInt(1000)), - sdk.NewCoin("xrp", sdk.NewInt(1000)), - sdk.NewCoin("bch", sdk.NewInt(1000)), - sdk.NewCoin("eos", sdk.NewInt(1000)), - } - - b.ResetTimer() - - // assumes b.N < 2**24 - for i := 0; i < b.N; i++ { - arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - addr := sdk.AccAddress(arr) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - acc.SetCoins(coins) - app.AccountKeeper.SetAccount(ctx, acc) - } -} diff --git a/x/auth/legacy/v0_38/migrate.go b/x/auth/legacy/v0_38/migrate.go index e76937777678..f4a32b4454c3 100644 --- a/x/auth/legacy/v0_38/migrate.go +++ b/x/auth/legacy/v0_38/migrate.go @@ -42,9 +42,9 @@ func Migrate(authGenState v036auth.GenesisState, genAccountsGenState v036genacco accounts[i] = genAccount } - accounts = sanitizeGenesisAccounts(accounts) + accounts = SanitizeGenesisAccounts(accounts) - if err := validateGenAccounts(accounts); err != nil { + if err := ValidateGenAccounts(accounts); err != nil { panic(err) } diff --git a/x/auth/legacy/v0_38/types.go b/x/auth/legacy/v0_38/types.go index 15896a39121f..1502fb27dbf0 100644 --- a/x/auth/legacy/v0_38/types.go +++ b/x/auth/legacy/v0_38/types.go @@ -46,7 +46,7 @@ type ( BaseAccount struct { Address sdk.AccAddress `json:"address" yaml:"address"` - Coins sdk.Coins `json:"coins" yaml:"coins"` + Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"` PubKey crypto.PubKey `json:"public_key" yaml:"public_key"` AccountNumber uint64 `json:"account_number" yaml:"account_number"` Sequence uint64 `json:"sequence" yaml:"sequence"` @@ -54,7 +54,7 @@ type ( baseAccountPretty struct { Address sdk.AccAddress `json:"address" yaml:"address"` - Coins sdk.Coins `json:"coins" yaml:"coins"` + Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"` PubKey string `json:"public_key" yaml:"public_key"` AccountNumber uint64 `json:"account_number" yaml:"account_number"` Sequence uint64 `json:"sequence" yaml:"sequence"` @@ -72,7 +72,7 @@ type ( vestingAccountPretty struct { Address sdk.AccAddress `json:"address" yaml:"address"` - Coins sdk.Coins `json:"coins" yaml:"coins"` + Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"` PubKey string `json:"public_key" yaml:"public_key"` AccountNumber uint64 `json:"account_number" yaml:"account_number"` Sequence uint64 `json:"sequence" yaml:"sequence"` @@ -104,7 +104,7 @@ type ( moduleAccountPretty struct { Address sdk.AccAddress `json:"address" yaml:"address"` - Coins sdk.Coins `json:"coins" yaml:"coins"` + Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"` PubKey string `json:"public_key" yaml:"public_key"` AccountNumber uint64 `json:"account_number" yaml:"account_number"` Sequence uint64 `json:"sequence" yaml:"sequence"` @@ -486,7 +486,7 @@ func validatePermissions(permissions ...string) error { return nil } -func sanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts { +func SanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts { sort.Slice(genAccounts, func(i, j int) bool { return genAccounts[i].GetAccountNumber() < genAccounts[j].GetAccountNumber() }) @@ -500,7 +500,7 @@ func sanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts { return genAccounts } -func validateGenAccounts(genAccounts GenesisAccounts) error { +func ValidateGenAccounts(genAccounts GenesisAccounts) error { addrMap := make(map[string]bool, len(genAccounts)) for _, acc := range genAccounts { diff --git a/x/auth/legacy/v0_39/migrate.go b/x/auth/legacy/v0_39/migrate.go new file mode 100644 index 000000000000..82b1390869f8 --- /dev/null +++ b/x/auth/legacy/v0_39/migrate.go @@ -0,0 +1,23 @@ +package v039 + +import ( + "fmt" + + v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38" +) + +// Migrate accepts exported x/auth genesis state from v0.38 and migrates it to +// v0.39 x/auth genesis state. The migration includes: +// +// - Removing coins from account encoding. +func Migrate(authGenState v038auth.GenesisState) v038auth.GenesisState { + for _, account := range authGenState.Accounts { + // set coins to nil and allow the JSON encoding to omit coins + if err := account.SetCoins(nil); err != nil { + panic(fmt.Sprintf("failed to set account coins to nil: %s", err)) + } + } + + authGenState.Accounts = v038auth.SanitizeGenesisAccounts(authGenState.Accounts) + return authGenState +} diff --git a/x/auth/legacy/v0_39/migrate_test.go b/x/auth/legacy/v0_39/migrate_test.go new file mode 100644 index 000000000000..5f9cee0d257c --- /dev/null +++ b/x/auth/legacy/v0_39/migrate_test.go @@ -0,0 +1,87 @@ +package v039_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34" + v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38" + v039 "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39" + + "github.com/stretchr/testify/require" +) + +func TestMigrate(t *testing.T) { + v039Codec := codec.New() + codec.RegisterCrypto(v039Codec) + v038auth.RegisterCodec(v039Codec) + + coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) + addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u") + acc1 := v038auth.NewBaseAccount(addr1, coins, nil, 1, 0) + + addr2, _ := sdk.AccAddressFromBech32("cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74") + vaac := v038auth.NewContinuousVestingAccountRaw( + v038auth.NewBaseVestingAccount( + v038auth.NewBaseAccount(addr2, coins, nil, 1, 0), coins, nil, nil, 3160620846, + ), + 1580309972, + ) + + gs := v038auth.GenesisState{ + Params: v0_34.Params{ + MaxMemoCharacters: 10, + TxSigLimit: 10, + TxSizeCostPerByte: 10, + SigVerifyCostED25519: 10, + SigVerifyCostSecp256k1: 10, + }, + Accounts: v038auth.GenesisAccounts{acc1, vaac}, + } + + migrated := v039.Migrate(gs) + expected := `{ + "params": { + "max_memo_characters": "10", + "tx_sig_limit": "10", + "tx_size_cost_per_byte": "10", + "sig_verify_cost_ed25519": "10", + "sig_verify_cost_secp256k1": "10" + }, + "accounts": [ + { + "type": "cosmos-sdk/Account", + "value": { + "address": "cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u", + "public_key": "", + "account_number": 1, + "sequence": 0 + } + }, + { + "type": "cosmos-sdk/ContinuousVestingAccount", + "value": { + "address": "cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74", + "public_key": "", + "account_number": 1, + "sequence": 0, + "original_vesting": [ + { + "denom": "stake", + "amount": "50" + } + ], + "delegated_free": [], + "delegated_vesting": [], + "end_time": 3160620846, + "start_time": 1580309972 + } + } + ] +}` + + bz, err := v039Codec.MarshalJSONIndent(migrated, "", " ") + require.NoError(t, err) + require.Equal(t, expected, string(bz)) +} diff --git a/x/auth/legacy/v0_39/types.go b/x/auth/legacy/v0_39/types.go new file mode 100644 index 000000000000..419ffe39020d --- /dev/null +++ b/x/auth/legacy/v0_39/types.go @@ -0,0 +1,8 @@ +package v039 + +// DONTCOVER +// nolint + +const ( + ModuleName = "auth" +) diff --git a/x/auth/types/account_test.go b/x/auth/types/account_test.go index 7be6a25e5623..90a4dacb0c9f 100644 --- a/x/auth/types/account_test.go +++ b/x/auth/types/account_test.go @@ -46,17 +46,6 @@ func TestBaseAddressPubKey(t *testing.T) { require.EqualValues(t, addr2, acc2.GetAddress()) } -func TestBaseAccountCoins(t *testing.T) { - _, _, addr := KeyTestPubAddr() - acc := NewBaseAccountWithAddress(addr) - - someCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 246)} - - err := acc.SetCoins(someCoins) - require.Nil(t, err) - require.Equal(t, someCoins, acc.GetCoins()) -} - func TestBaseAccountSequence(t *testing.T) { _, _, addr := KeyTestPubAddr() acc := NewBaseAccountWithAddress(addr) @@ -72,7 +61,6 @@ func TestBaseAccountMarshal(t *testing.T) { _, pub, addr := KeyTestPubAddr() acc := NewBaseAccountWithAddress(addr) - someCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 246)} seq := uint64(7) // set everything on the account @@ -80,8 +68,6 @@ func TestBaseAccountMarshal(t *testing.T) { require.Nil(t, err) err = acc.SetSequence(seq) require.Nil(t, err) - err = acc.SetCoins(someCoins) - require.Nil(t, err) // need a codec for marshaling cdc := codec.New() @@ -104,7 +90,7 @@ func TestBaseAccountMarshal(t *testing.T) { func TestGenesisAccountValidate(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) - baseAcc := NewBaseAccount(addr, nil, pubkey, 0, 0) + baseAcc := NewBaseAccount(addr, pubkey, 0, 0) tests := []struct { name string acc exported.GenesisAccount @@ -117,7 +103,7 @@ func TestGenesisAccountValidate(t *testing.T) { }, { "invalid base valid account", - NewBaseAccount(addr, sdk.NewCoins(), secp256k1.GenPrivKey().PubKey(), 0, 0), + NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0), errors.New("pubkey and address pair is invalid"), }, } @@ -133,8 +119,7 @@ func TestGenesisAccountValidate(t *testing.T) { func TestBaseAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) - coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := NewBaseAccount(addr, coins, pubkey, 10, 50) + baseAcc := NewBaseAccount(addr, pubkey, 10, 50) bz, err := json.Marshal(baseAcc) require.NoError(t, err) diff --git a/x/auth/types/genesis_test.go b/x/auth/types/genesis_test.go index f28b67b21a2d..3b892f48038d 100644 --- a/x/auth/types/genesis_test.go +++ b/x/auth/types/genesis_test.go @@ -14,31 +14,19 @@ import ( func TestSanitize(t *testing.T) { addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) authAcc1 := NewBaseAccountWithAddress(addr1) - authAcc1.SetCoins(sdk.Coins{ - sdk.NewInt64Coin("bcoin", 150), - sdk.NewInt64Coin("acoin", 150), - }) authAcc1.SetAccountNumber(1) addr2 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) authAcc2 := NewBaseAccountWithAddress(addr2) - authAcc2.SetCoins(sdk.Coins{ - sdk.NewInt64Coin("acoin", 150), - sdk.NewInt64Coin("bcoin", 150), - }) genAccs := exported.GenesisAccounts{&authAcc1, &authAcc2} require.True(t, genAccs[0].GetAccountNumber() > genAccs[1].GetAccountNumber()) - require.Equal(t, genAccs[0].GetCoins()[0].Denom, "bcoin") - require.Equal(t, genAccs[0].GetCoins()[1].Denom, "acoin") require.Equal(t, genAccs[1].GetAddress(), addr2) genAccs = SanitizeGenesisAccounts(genAccs) require.False(t, genAccs[0].GetAccountNumber() > genAccs[1].GetAccountNumber()) require.Equal(t, genAccs[1].GetAddress(), addr1) - require.Equal(t, genAccs[1].GetCoins()[0].Denom, "acoin") - require.Equal(t, genAccs[1].GetCoins()[1].Denom, "bcoin") } var ( @@ -51,7 +39,6 @@ var ( // require duplicate accounts fails validation func TestValidateGenesisDuplicateAccounts(t *testing.T) { acc1 := NewBaseAccountWithAddress(sdk.AccAddress(addr1)) - acc1.Coins = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) genAccs := make(exported.GenesisAccounts, 2) genAccs[0] = &acc1 @@ -62,10 +49,8 @@ func TestValidateGenesisDuplicateAccounts(t *testing.T) { func TestGenesisAccountIterator(t *testing.T) { acc1 := NewBaseAccountWithAddress(sdk.AccAddress(addr1)) - acc1.Coins = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) acc2 := NewBaseAccountWithAddress(sdk.AccAddress(addr2)) - acc2.Coins = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) genAccounts := exported.GenesisAccounts{&acc1, &acc2} diff --git a/x/auth/vesting/types/genesis_test.go b/x/auth/vesting/types/genesis_test.go index aee8d1fa091a..7a9781eb7040 100644 --- a/x/auth/vesting/types/genesis_test.go +++ b/x/auth/vesting/types/genesis_test.go @@ -21,21 +21,21 @@ var ( // require invalid vesting account fails validation func TestValidateGenesisInvalidAccounts(t *testing.T) { acc1 := authtypes.NewBaseAccountWithAddress(sdk.AccAddress(addr1)) - acc1.Coins = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) - baseVestingAcc, err := NewBaseVestingAccount(&acc1, acc1.Coins, 1548775410) - require.NoError(t, err) + acc1Balance := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) + baseVestingAcc := NewBaseVestingAccount(&acc1, acc1Balance, 1548775410) + // invalid delegated vesting - baseVestingAcc.DelegatedVesting = acc1.Coins.Add(acc1.Coins...) + baseVestingAcc.DelegatedVesting = acc1Balance.Add(acc1Balance...) acc2 := authtypes.NewBaseAccountWithAddress(sdk.AccAddress(addr2)) - acc2.Coins = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) + // acc2Balance := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) genAccs := make([]exported.GenesisAccount, 2) genAccs[0] = baseVestingAcc genAccs[1] = &acc2 require.Error(t, authtypes.ValidateGenAccounts(genAccs)) - baseVestingAcc.DelegatedVesting = acc1.Coins + baseVestingAcc.DelegatedVesting = acc1Balance genAccs[0] = baseVestingAcc require.NoError(t, authtypes.ValidateGenAccounts(genAccs)) // invalid start time diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index 651416338c06..6d3082bb8fad 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -27,8 +27,7 @@ func TestGetVestedCoinsContVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - cva := NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) + cva := NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) // require no coins vested in the very beginning of the vesting schedule vestedCoins := cva.GetVestedCoins(now) @@ -54,8 +53,7 @@ func TestGetVestingCoinsContVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - cva := NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) + cva := NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) // require all coins vesting in the beginning of the vesting schedule vestingCoins := cva.GetVestingCoins(now) @@ -77,37 +75,31 @@ func TestSpendableCoinsContVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - cva := NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - // require that there exist no spendable coins in the beginning of the - // vesting schedule - spendableCoins := cva.SpendableCoins(now) - require.Nil(t, spendableCoins) + currBalance := origCoins - // require that all original coins are spendable at the end of the vesting + cva := NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + + // require that all original coins are locked at the end of the vesting // schedule - spendableCoins = cva.SpendableCoins(endTime) - require.Equal(t, origCoins, spendableCoins) + lockedCoins := cva.LockedCoins(currBalance, now) + require.Equal(t, origCoins, lockedCoins) + + // require that there exist no locked coins in the beginning of the + lockedCoins = cva.LockedCoins(currBalance, endTime) + require.Nil(t, lockedCoins) // require that all vested coins (50%) are spendable - spendableCoins = cva.SpendableCoins(now.Add(12 * time.Hour)) - require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, spendableCoins) + lockedCoins = cva.LockedCoins(currBalance, now.Add(12*time.Hour)) + require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, lockedCoins) // receive some coins recvAmt := sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)} - cva.SetCoins(cva.GetCoins().Add(recvAmt...)) + currBalance = currBalance.Add(recvAmt...) // require that all vested coins (50%) are spendable plus any received - spendableCoins = cva.SpendableCoins(now.Add(12 * time.Hour)) - require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 100)}, spendableCoins) - - // spend all spendable coins - cva.SetCoins(cva.GetCoins().Sub(spendableCoins)) - - // require that no more coins are spendable - spendableCoins = cva.SpendableCoins(now.Add(12 * time.Hour)) - require.Nil(t, spendableCoins) + lockedCoins = cva.LockedCoins(currBalance, now.Add(12*time.Hour)) + require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, lockedCoins) } func TestTrackDelegationContVestingAcc(t *testing.T) { @@ -117,37 +109,33 @@ func TestTrackDelegationContVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to delegate all vesting coins - cva := NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - cva.TrackDelegation(now, origCoins) + cva := NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + cva.TrackDelegation(now, origCoins, origCoins) require.Equal(t, origCoins, cva.DelegatedVesting) require.Nil(t, cva.DelegatedFree) // require the ability to delegate all vested coins - bacc.SetCoins(origCoins) - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - cva.TrackDelegation(endTime, origCoins) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + cva.TrackDelegation(endTime, origCoins, origCoins) require.Nil(t, cva.DelegatedVesting) require.Equal(t, origCoins, cva.DelegatedFree) // require the ability to delegate all vesting coins (50%) and all vested coins (50%) - bacc.SetCoins(origCoins) - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - cva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + cva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, cva.DelegatedVesting) require.Nil(t, cva.DelegatedFree) - cva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + cva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, cva.DelegatedVesting) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, cva.DelegatedFree) // require no modifications when delegation amount is zero or not enough funds - bacc.SetCoins(origCoins) - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) require.Panics(t, func() { - cva.TrackDelegation(endTime, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) + cva.TrackDelegation(endTime, origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) }) require.Nil(t, cva.DelegatedVesting) require.Nil(t, cva.DelegatedFree) @@ -160,27 +148,24 @@ func TestTrackUndelegationContVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to undelegate all vesting coins - cva := NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - cva.TrackDelegation(now, origCoins) + cva := NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + cva.TrackDelegation(now, origCoins, origCoins) cva.TrackUndelegation(origCoins) require.Nil(t, cva.DelegatedFree) require.Nil(t, cva.DelegatedVesting) // require the ability to undelegate all vested coins - bacc.SetCoins(origCoins) - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) - cva.TrackDelegation(endTime, origCoins) + cva.TrackDelegation(endTime, origCoins, origCoins) cva.TrackUndelegation(origCoins) require.Nil(t, cva.DelegatedFree) require.Nil(t, cva.DelegatedVesting) // require no modifications when the undelegation amount is zero - bacc.SetCoins(origCoins) - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) require.Panics(t, func() { cva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 0)}) @@ -189,9 +174,9 @@ func TestTrackUndelegationContVestingAcc(t *testing.T) { require.Nil(t, cva.DelegatedVesting) // vest 50% and delegate to two validators - cva = NewContinuousVestingAccount(&bacc, now.Unix(), endTime.Unix()) - cva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) - cva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + cva = NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + cva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + cva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) // undelegate from one validator that got slashed 50% cva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)}) @@ -211,10 +196,9 @@ func TestGetVestedCoinsDelVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require no coins are vested until schedule maturation - dva := NewDelayedVestingAccount(&bacc, endTime.Unix()) + dva := NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) vestedCoins := dva.GetVestedCoins(now) require.Nil(t, vestedCoins) @@ -230,10 +214,9 @@ func TestGetVestingCoinsDelVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require all coins vesting at the beginning of the schedule - dva := NewDelayedVestingAccount(&bacc, endTime.Unix()) + dva := NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) vestingCoins := dva.GetVestingCoins(now) require.Equal(t, origCoins, vestingCoins) @@ -249,38 +232,34 @@ func TestSpendableCoinsDelVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - // require that no coins are spendable in the beginning of the vesting + // require that all coins are locked in the beginning of the vesting // schedule - dva := NewDelayedVestingAccount(&bacc, endTime.Unix()) - spendableCoins := dva.SpendableCoins(now) - require.Nil(t, spendableCoins) + dva := NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + lockedCoins := dva.LockedCoins(origCoins, now) + require.True(t, lockedCoins.IsEqual(origCoins)) // require that all coins are spendable after the maturation of the vesting // schedule - spendableCoins = dva.SpendableCoins(endTime) - require.Equal(t, origCoins, spendableCoins) + lockedCoins = dva.LockedCoins(origCoins, endTime) + require.Nil(t, lockedCoins) // require that all coins are still vesting after some time - spendableCoins = dva.SpendableCoins(now.Add(12 * time.Hour)) - require.Nil(t, spendableCoins) + lockedCoins = dva.LockedCoins(origCoins, now.Add(12*time.Hour)) + require.True(t, lockedCoins.IsEqual(origCoins)) // receive some coins - recvAmt := sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)} - dva.SetCoins(dva.GetCoins().Add(recvAmt...)) - // require that only received coins are spendable since the account is still // vesting - spendableCoins = dva.SpendableCoins(now.Add(12 * time.Hour)) - require.Equal(t, recvAmt, spendableCoins) - - // spend all spendable coins - dva.SetCoins(dva.GetCoins().Sub(spendableCoins)) - - // require that no more coins are spendable - spendableCoins = dva.SpendableCoins(now.Add(12 * time.Hour)) - require.Nil(t, spendableCoins) + lockedCoins = dva.LockedCoins(origCoins.Add(sdk.NewInt64Coin(stakeDenom, 50)), now.Add(12*time.Hour)) + require.True(t, lockedCoins.IsEqual(origCoins)) + + // delegate some locked coins + // require that locked is reduced + delegatedAmount := sdk.NewCoins(sdk.NewInt64Coin(stakeDenom, 50)) + dva.TrackDelegation(now.Add(12*time.Hour), origCoins, delegatedAmount) + lockedCoins = dva.LockedCoins(origCoins.Sub(delegatedAmount), now.Add(12*time.Hour)) + require.True(t, lockedCoins.IsEqual(origCoins.Sub(delegatedAmount))) } func TestTrackDelegationDelVestingAcc(t *testing.T) { @@ -290,36 +269,31 @@ func TestTrackDelegationDelVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to delegate all vesting coins - bacc.SetCoins(origCoins) - dva := NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(now, origCoins) + dva := NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(now, origCoins, origCoins) require.Equal(t, origCoins, dva.DelegatedVesting) require.Nil(t, dva.DelegatedFree) // require the ability to delegate all vested coins - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(endTime, origCoins) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(endTime, origCoins, origCoins) require.Nil(t, dva.DelegatedVesting) require.Equal(t, origCoins, dva.DelegatedFree) // require the ability to delegate all coins half way through the vesting // schedule - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(now.Add(12*time.Hour), origCoins) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(now.Add(12*time.Hour), origCoins, origCoins) require.Equal(t, origCoins, dva.DelegatedVesting) require.Nil(t, dva.DelegatedFree) // require no modifications when delegation amount is zero or not enough funds - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) require.Panics(t, func() { - dva.TrackDelegation(endTime, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) + dva.TrackDelegation(endTime, origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) }) require.Nil(t, dva.DelegatedVesting) require.Nil(t, dva.DelegatedFree) @@ -332,27 +306,23 @@ func TestTrackUndelegationDelVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to undelegate all vesting coins - bacc.SetCoins(origCoins) - dva := NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(now, origCoins) + dva := NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(now, origCoins, origCoins) dva.TrackUndelegation(origCoins) require.Nil(t, dva.DelegatedFree) require.Nil(t, dva.DelegatedVesting) // require the ability to undelegate all vested coins - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(endTime, origCoins) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(endTime, origCoins, origCoins) dva.TrackUndelegation(origCoins) require.Nil(t, dva.DelegatedFree) require.Nil(t, dva.DelegatedVesting) // require no modifications when the undelegation amount is zero - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) require.Panics(t, func() { dva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 0)}) @@ -361,10 +331,9 @@ func TestTrackUndelegationDelVestingAcc(t *testing.T) { require.Nil(t, dva.DelegatedVesting) // vest 50% and delegate to two validators - bacc.SetCoins(origCoins) - dva = NewDelayedVestingAccount(&bacc, endTime.Unix()) - dva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) - dva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + dva = NewDelayedVestingAccount(&bacc, origCoins, endTime.Unix()) + dva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + dva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) // undelegate from one validator that got slashed 50% dva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)}) @@ -390,8 +359,7 @@ func TestGetVestedCoinsPeriodicVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - pva := NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva := NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) // require no coins vested at the beginning of the vesting schedule vestedCoins := pva.GetVestedCoins(now) @@ -437,8 +405,7 @@ func TestGetVestingCoinsPeriodicVestingAcc(t *testing.T) { origCoins := sdk.Coins{ sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - pva := NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva := NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) // require all coins vesting at the beginning of the vesting schedule vestingCoins := pva.GetVestingCoins(now) @@ -478,37 +445,27 @@ func TestSpendableCoinsPeriodicVestingAcc(t *testing.T) { origCoins := sdk.Coins{ sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) - pva := NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva := NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) // require that there exist no spendable coins at the beginning of the // vesting schedule - spendableCoins := pva.SpendableCoins(now) - require.Nil(t, spendableCoins) + lockedCoins := pva.LockedCoins(origCoins, now) + require.Equal(t, origCoins, lockedCoins) // require that all original coins are spendable at the end of the vesting // schedule - spendableCoins = pva.SpendableCoins(endTime) - require.Equal(t, origCoins, spendableCoins) + lockedCoins = pva.LockedCoins(origCoins, endTime) + require.Nil(t, lockedCoins) - // require that all vested coins (50%) are spendable - spendableCoins = pva.SpendableCoins(now.Add(12 * time.Hour)) - require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, spendableCoins) + // require that all still vesting coins (50%) are locked + lockedCoins = pva.LockedCoins(origCoins, now.Add(12*time.Hour)) + require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, lockedCoins) // receive some coins + // require that all still vesting coins (50% of original) are locked plus any received recvAmt := sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)} - pva.SetCoins(pva.GetCoins().Add(recvAmt...)) - - // require that all vested coins (50%) are spendable plus any received - spendableCoins = pva.SpendableCoins(now.Add(12 * time.Hour)) - require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 100)}, spendableCoins) - - // spend all spendable coins - pva.SetCoins(pva.GetCoins().Sub(spendableCoins)) - - // require that no more coins are spendable - spendableCoins = pva.SpendableCoins(now.Add(12 * time.Hour)) - require.Nil(t, spendableCoins) + lockedCoins = pva.LockedCoins(origCoins.Add(recvAmt...), now.Add(12*time.Hour)) + require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, lockedCoins) } func TestTrackDelegationPeriodicVestingAcc(t *testing.T) { @@ -523,53 +480,47 @@ func TestTrackDelegationPeriodicVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to delegate all vesting coins - pva := NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now, origCoins) + pva := NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now, origCoins, origCoins) require.Equal(t, origCoins, pva.DelegatedVesting) require.Nil(t, pva.DelegatedFree) // require the ability to delegate all vested coins - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(endTime, origCoins) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(endTime, origCoins, origCoins) require.Nil(t, pva.DelegatedVesting) require.Equal(t, origCoins, pva.DelegatedFree) // delegate half of vesting coins - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now, periods[0].Amount) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now, origCoins, periods[0].Amount) // require that all delegated coins are delegated vesting require.Equal(t, pva.DelegatedVesting, periods[0].Amount) require.Nil(t, pva.DelegatedFree) // delegate 75% of coins, split between vested and vesting - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now.Add(12*time.Hour), periods[0].Amount.Add(periods[1].Amount...)) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now.Add(12*time.Hour), origCoins, periods[0].Amount.Add(periods[1].Amount...)) // require that the maximum possible amount of vesting coins are chosen for delegation. require.Equal(t, pva.DelegatedFree, periods[1].Amount) require.Equal(t, pva.DelegatedVesting, periods[0].Amount) // require the ability to delegate all vesting coins (50%) and all vested coins (50%) - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, pva.DelegatedVesting) require.Nil(t, pva.DelegatedFree) - pva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + pva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, pva.DelegatedVesting) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}, pva.DelegatedFree) // require no modifications when delegation amount is zero or not enough funds - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) require.Panics(t, func() { - pva.TrackDelegation(endTime, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) + pva.TrackDelegation(endTime, origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)}) }) require.Nil(t, pva.DelegatedVesting) require.Nil(t, pva.DelegatedFree) @@ -587,35 +538,31 @@ func TestTrackUndelegationPeriodicVestingAcc(t *testing.T) { _, _, addr := KeyTestPubAddr() origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} bacc := authtypes.NewBaseAccountWithAddress(addr) - bacc.SetCoins(origCoins) // require the ability to undelegate all vesting coins at the beginning of vesting - pva := NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now, origCoins) + pva := NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now, origCoins, origCoins) pva.TrackUndelegation(origCoins) require.Nil(t, pva.DelegatedFree) require.Nil(t, pva.DelegatedVesting) // require the ability to undelegate all vested coins at the end of vesting - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) - pva.TrackDelegation(endTime, origCoins) + pva.TrackDelegation(endTime, origCoins, origCoins) pva.TrackUndelegation(origCoins) require.Nil(t, pva.DelegatedFree) require.Nil(t, pva.DelegatedVesting) // require the ability to undelegate half of coins - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(endTime, periods[0].Amount) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(endTime, origCoins, periods[0].Amount) pva.TrackUndelegation(periods[0].Amount) require.Nil(t, pva.DelegatedFree) require.Nil(t, pva.DelegatedVesting) // require no modifications when the undelegation amount is zero - bacc.SetCoins(origCoins) - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) require.Panics(t, func() { pva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 0)}) @@ -624,9 +571,9 @@ func TestTrackUndelegationPeriodicVestingAcc(t *testing.T) { require.Nil(t, pva.DelegatedVesting) // vest 50% and delegate to two validators - pva = NewPeriodicVestingAccount(&bacc, now.Unix(), periods) - pva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) - pva.TrackDelegation(now.Add(12*time.Hour), sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + pva = NewPeriodicVestingAccount(&bacc, origCoins, now.Unix(), periods) + pva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) + pva.TrackDelegation(now.Add(12*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)}) // undelegate from one validator that got slashed 50% pva.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)}) @@ -639,42 +586,42 @@ func TestTrackUndelegationPeriodicVestingAcc(t *testing.T) { require.Equal(t, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)}, pva.DelegatedVesting) } -func TestNewBaseVestingAccount(t *testing.T) { - pubkey := secp256k1.GenPrivKey().PubKey() - addr := sdk.AccAddress(pubkey.Address()) - _, err := NewBaseVestingAccount( - authtypes.NewBaseAccount(addr, sdk.NewCoins(), pubkey, 0, 0), - sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, - ) - require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) - - _, err = NewBaseVestingAccount( - authtypes.NewBaseAccount(addr, sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)), pubkey, 0, 0), - sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, - ) - require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) - - _, err = NewBaseVestingAccount( - authtypes.NewBaseAccount(addr, sdk.NewCoins(sdk.NewInt64Coin("uatom", 50), sdk.NewInt64Coin("eth", 50)), pubkey, 0, 0), - sdk.NewCoins(sdk.NewInt64Coin("uatom", 100), sdk.NewInt64Coin("eth", 20)), 100, - ) - require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) - - _, err = NewBaseVestingAccount( - authtypes.NewBaseAccount(addr, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, pubkey, 0, 0), - sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, - ) - require.NoError(t, err) - -} +// TODO: Move test to bank +// func TestNewBaseVestingAccount(t *testing.T) { +// pubkey := secp256k1.GenPrivKey().PubKey() +// addr := sdk.AccAddress(pubkey.Address()) +// _, err := NewBaseVestingAccount( +// authtypes.NewBaseAccount(addr, sdk.NewCoins(), pubkey, 0, 0), +// sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, +// ) +// require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) + +// _, err = NewBaseVestingAccount( +// authtypes.NewBaseAccount(addr, sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)), pubkey, 0, 0), +// sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, +// ) +// require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) + +// _, err = NewBaseVestingAccount( +// authtypes.NewBaseAccount(addr, sdk.NewCoins(sdk.NewInt64Coin("uatom", 50), sdk.NewInt64Coin("eth", 50)), pubkey, 0, 0), +// sdk.NewCoins(sdk.NewInt64Coin("uatom", 100), sdk.NewInt64Coin("eth", 20)), 100, +// ) +// require.Equal(t, errors.New("vesting amount cannot be greater than total amount"), err) + +// _, err = NewBaseVestingAccount( +// authtypes.NewBaseAccount(addr, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, pubkey, 0, 0), +// sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, 100, +// ) +// require.NoError(t, err) + +// } func TestGenesisAccountValidate(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) - baseAcc := authtypes.NewBaseAccount(addr, nil, pubkey, 0, 0) - baseAccWithCoins := authtypes.NewBaseAccount(addr, nil, pubkey, 0, 0) - baseAccWithCoins.SetCoins(sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}) - baseVestingWithCoins, _ := NewBaseVestingAccount(baseAccWithCoins, baseAccWithCoins.Coins, 100) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 0, 0) + initialVesting := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)) + baseVestingWithCoins := NewBaseVestingAccount(baseAcc, initialVesting, 100) tests := []struct { name string acc authexported.GenesisAccount @@ -687,7 +634,7 @@ func TestGenesisAccountValidate(t *testing.T) { }, { "invalid base valid account", - authtypes.NewBaseAccount(addr, sdk.NewCoins(), secp256k1.GenPrivKey().PubKey(), 0, 0), + authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0), errors.New("pubkey and address pair is invalid"), }, { @@ -697,17 +644,17 @@ func TestGenesisAccountValidate(t *testing.T) { }, { "valid continuous vesting account", - NewContinuousVestingAccount(baseAcc, 100, 200), + NewContinuousVestingAccount(baseAcc, initialVesting, 100, 200), nil, }, { "invalid vesting times", - NewContinuousVestingAccount(baseAcc, 1654668078, 1554668078), + NewContinuousVestingAccount(baseAcc, initialVesting, 1654668078, 1554668078), errors.New("vesting start-time cannot be before end-time"), }, { "valid periodic vesting account", - NewPeriodicVestingAccount(baseAccWithCoins, 0, Periods{Period{Length: int64(100), Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}}}), + NewPeriodicVestingAccount(baseAcc, initialVesting, 0, Periods{Period{Length: int64(100), Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}}}), nil, }, { @@ -738,10 +685,9 @@ func TestBaseVestingAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 10, 50) - acc, err := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix()) - require.NoError(t, err) + acc := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix()) bz, err := json.Marshal(acc) require.NoError(t, err) @@ -759,11 +705,10 @@ func TestContinuousVestingAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 10, 50) - baseVesting, err := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix()) + baseVesting := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix()) acc := NewContinuousVestingAccountRaw(baseVesting, baseVesting.EndTime) - require.NoError(t, err) bz, err := json.Marshal(acc) require.NoError(t, err) @@ -781,9 +726,9 @@ func TestPeriodicVestingAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 10, 50) - acc := NewPeriodicVestingAccount(baseAcc, time.Now().Unix(), Periods{Period{3600, coins}}) + acc := NewPeriodicVestingAccount(baseAcc, coins, time.Now().Unix(), Periods{Period{3600, coins}}) bz, err := json.Marshal(acc) require.NoError(t, err) @@ -801,9 +746,9 @@ func TestDelayedVestingAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50) + baseAcc := authtypes.NewBaseAccount(addr, pubkey, 10, 50) - acc := NewDelayedVestingAccount(baseAcc, time.Now().Unix()) + acc := NewDelayedVestingAccount(baseAcc, coins, time.Now().Unix()) bz, err := json.Marshal(acc) require.NoError(t, err) diff --git a/x/bank/alias.go b/x/bank/alias.go index 302a5ffdb762..2fdfa6602b82 100644 --- a/x/bank/alias.go +++ b/x/bank/alias.go @@ -50,7 +50,7 @@ var ( ModuleCdc = types.ModuleCdc ParamStoreKeySendEnabled = types.ParamStoreKeySendEnabled BalancesPrefix = types.BalancesPrefix - AddressFromBalancesKey = types.AddressFromBalancesKey + AddressFromBalancesStore = types.AddressFromBalancesStore ) type ( diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 7a9894aa8c3b..e124baaeef7f 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -49,7 +49,6 @@ var ( halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)} sendMsg1 = types.NewMsgSend(addr1, addr2, coins) - sendMsg2 = types.NewMsgSend(addr1, moduleAccAddr, coins) multiSendMsg1 = types.MsgMultiSend{ Inputs: []types.Input{types.NewInput(addr1, coins)}, @@ -93,15 +92,18 @@ var ( func TestSendNotEnoughBalance(t *testing.T) { acc := &auth.BaseAccount{ Address: addr1, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 67)}, } genAccs := []authexported.GenesisAccount{acc} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) - ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) + err := app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))) + require.NoError(t, err) + + app.Commit() - res1 := app.AccountKeeper.GetAccount(ctxCheck, addr1) + res1 := app.AccountKeeper.GetAccount(ctx, addr1) require.NotNil(t, res1) require.Equal(t, acc, res1.(*auth.BaseAccount)) @@ -157,15 +159,18 @@ func TestSendToModuleAcc(t *testing.T) { t.Run(test.name, func(t *testing.T) { acc := &auth.BaseAccount{ Address: test.msg.FromAddress, - Coins: test.fromBalance, } genAccs := []authexported.GenesisAccount{acc} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + + err := app.BankKeeper.SetBalances(ctx, test.msg.FromAddress, test.fromBalance) + require.NoError(t, err) - ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) + app.Commit() - res1 := app.AccountKeeper.GetAccount(ctxCheck, test.msg.FromAddress) + res1 := app.AccountKeeper.GetAccount(ctx, test.msg.FromAddress) require.NotNil(t, res1) require.Equal(t, acc, res1.(*auth.BaseAccount)) @@ -190,15 +195,18 @@ func TestSendToModuleAcc(t *testing.T) { func TestMsgMultiSendWithAccounts(t *testing.T) { acc := &auth.BaseAccount{ Address: addr1, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 67)}, } genAccs := []authexported.GenesisAccount{acc} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) - ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) + err := app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))) + require.NoError(t, err) + + app.Commit() - res1 := app.AccountKeeper.GetAccount(ctxCheck, addr1) + res1 := app.AccountKeeper.GetAccount(ctx, addr1) require.NotNil(t, res1) require.Equal(t, acc, res1.(*auth.BaseAccount)) @@ -244,18 +252,24 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { } func TestMsgMultiSendMultipleOut(t *testing.T) { - acc1 := &auth.BaseAccount{ Address: addr1, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } acc2 := &auth.BaseAccount{ Address: addr2, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } genAccs := []authexported.GenesisAccount{acc1, acc2} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + + err := app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + err = app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + app.Commit() testCases := []appTestCase{ { @@ -284,22 +298,30 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { } func TestMsgMultiSendMultipleInOut(t *testing.T) { - acc1 := &auth.BaseAccount{ Address: addr1, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } acc2 := &auth.BaseAccount{ Address: addr2, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } acc4 := &auth.BaseAccount{ Address: addr4, - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } genAccs := []authexported.GenesisAccount{acc1, acc2, acc4} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + + err := app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + err = app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + err = app.BankKeeper.SetBalances(ctx, addr4, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + app.Commit() testCases := []appTestCase{ { @@ -331,13 +353,17 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) { func TestMsgMultiSendDependent(t *testing.T) { acc1 := auth.NewBaseAccountWithAddress(addr1) acc2 := auth.NewBaseAccountWithAddress(addr2) - err := acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) - require.NoError(t, err) - err = acc2.SetAccountNumber(1) + err := acc2.SetAccountNumber(1) require.NoError(t, err) genAccs := []authexported.GenesisAccount{&acc1, &acc2} app := simapp.SetupWithGenesisAccounts(genAccs) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + + err = app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))) + require.NoError(t, err) + + app.Commit() testCases := []appTestCase{ { diff --git a/x/bank/bench_test.go b/x/bank/bench_test.go index 9161a88c31ba..d9ccdc5702a0 100644 --- a/x/bank/bench_test.go +++ b/x/bank/bench_test.go @@ -3,6 +3,7 @@ package bank_test import ( "testing" + "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/simapp" @@ -19,29 +20,38 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // Add an account at genesis acc := auth.BaseAccount{ Address: addr1, - // Some value conceivably higher than the benchmarks would ever go - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)}, } - // Construct genesis state + // construct genesis state genAccs := []authexported.GenesisAccount{&acc} benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs) + ctx := benchmarkApp.BaseApp.NewContext(false, abci.Header{}) + + // some value conceivably higher than the benchmarks would ever go + err := benchmarkApp.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))) + require.NoError(b, err) + + benchmarkApp.Commit() // Precompute all txs txs := simapp.GenSequenceOfTxs([]sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1) b.ResetTimer() + + height := int64(3) + // Run this with a profiler, so its easy to distinguish what time comes from // Committing, and what time comes from Check/Deliver Tx. for i := 0; i < b.N; i++ { - benchmarkApp.BeginBlock(abci.RequestBeginBlock{}) + benchmarkApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: height}}) _, _, err := benchmarkApp.Check(txs[i]) if err != nil { panic("something is broken in checking transaction") } benchmarkApp.Deliver(txs[i]) - benchmarkApp.EndBlock(abci.RequestEndBlock{}) + benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height}) benchmarkApp.Commit() + height++ } } @@ -49,28 +59,37 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) { // Add an account at genesis acc := auth.BaseAccount{ Address: addr1, - // Some value conceivably higher than the benchmarks would ever go - Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)}, } // Construct genesis state genAccs := []authexported.GenesisAccount{&acc} benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs) + ctx := benchmarkApp.BaseApp.NewContext(false, abci.Header{}) + + // some value conceivably higher than the benchmarks would ever go + err := benchmarkApp.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))) + require.NoError(b, err) + + benchmarkApp.Commit() // Precompute all txs txs := simapp.GenSequenceOfTxs([]sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1) b.ResetTimer() + + height := int64(3) + // Run this with a profiler, so its easy to distinguish what time comes from // Committing, and what time comes from Check/Deliver Tx. for i := 0; i < b.N; i++ { - benchmarkApp.BeginBlock(abci.RequestBeginBlock{}) + benchmarkApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: height}}) _, _, err := benchmarkApp.Check(txs[i]) if err != nil { panic("something is broken in checking transaction") } benchmarkApp.Deliver(txs[i]) - benchmarkApp.EndBlock(abci.RequestEndBlock{}) + benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height}) benchmarkApp.Commit() + height++ } } diff --git a/x/bank/genesis.go b/x/bank/genesis.go index c93196e2553e..7f68e94cff8e 100644 --- a/x/bank/genesis.go +++ b/x/bank/genesis.go @@ -1,6 +1,8 @@ package bank import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -29,9 +31,14 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState { balances := []Balance{} - for addr, coins := range balancesSet { + for addrStr, coins := range balancesSet { + addr, err := sdk.AccAddressFromBech32(addrStr) + if err != nil { + panic(fmt.Errorf("failed to convert address from string: %w", err)) + } + balances = append(balances, Balance{ - Address: sdk.AccAddress([]byte(addr)), + Address: addr, Coins: coins, }) } diff --git a/x/bank/internal/keeper/integration_test.go b/x/bank/internal/keeper/integration_test.go deleted file mode 100644 index f008a5558839..000000000000 --- a/x/bank/internal/keeper/integration_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package keeper_test - -import ( - abci "github.com/tendermint/tendermint/abci/types" - - "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" -) - -func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(isCheckTx) - ctx := app.BaseApp.NewContext(isCheckTx, abci.Header{}) - - app.AccountKeeper.SetParams(ctx, auth.DefaultParams()) - app.BankKeeper.SetSendEnabled(ctx, true) - - return app, ctx -} diff --git a/x/bank/internal/keeper/keeper.go b/x/bank/internal/keeper/keeper.go index 7565513f3bf6..380507178635 100644 --- a/x/bank/internal/keeper/keeper.go +++ b/x/bank/internal/keeper/keeper.go @@ -368,6 +368,7 @@ type ViewKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins IterateAccountBalances(ctx sdk.Context, addr sdk.AccAddress, cb func(coin sdk.Coin) (stop bool)) IterateAllBalances(ctx sdk.Context, cb func(address sdk.AccAddress, coin sdk.Coin) (stop bool)) @@ -460,7 +461,7 @@ func (k BaseViewKeeper) IterateAllBalances(ctx sdk.Context, cb func(sdk.AccAddre defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - address := types.AddressFromBalancesKey(iterator.Key()) + address := types.AddressFromBalancesStore(iterator.Key()) var balance sdk.Coin k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &balance) @@ -487,6 +488,21 @@ func (k BaseViewKeeper) LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Co return sdk.NewCoins() } +// SpendableCoins returns the total balances of spendable coins for an account +// by address. If the account has no spendable coins, an empty Coins slice is +// returned. +func (k BaseViewKeeper) SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins { + balances := k.GetAllBalances(ctx, addr) + locked := k.LockedCoins(ctx, addr) + + spendable, hasNeg := balances.SafeSub(locked) + if hasNeg { + return sdk.NewCoins() + } + + return spendable +} + // ValidateBalance validates all balances for a given account address returning // an error if any balance is invalid. It will check for vesting account types // and validate the balances against the original vesting balances. diff --git a/x/bank/internal/keeper/keeper_test.go b/x/bank/internal/keeper/keeper_test.go index 6b6f3cd4048f..9af2e1cbbd77 100644 --- a/x/bank/internal/keeper/keeper_test.go +++ b/x/bank/internal/keeper/keeper_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" abci "github.com/tendermint/tendermint/abci/types" tmkv "github.com/tendermint/tendermint/libs/kv" tmtime "github.com/tendermint/tendermint/types/time" @@ -13,189 +13,248 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/vesting" - keep "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" "github.com/cosmos/cosmos-sdk/x/bank/internal/types" - "github.com/cosmos/cosmos-sdk/x/supply" ) -func TestKeeper(t *testing.T) { - app, ctx := createTestApp(false) - - addr := sdk.AccAddress([]byte("addr1")) - addr2 := sdk.AccAddress([]byte("addr2")) - addr3 := sdk.AccAddress([]byte("addr3")) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - - // Test GetCoins/SetCoins - app.AccountKeeper.SetAccount(ctx, acc) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins())) +const ( + fooDenom = "foo" + barDenom = "bar" +) - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) +func newFooCoin(amt int64) sdk.Coin { + return sdk.NewInt64Coin(fooDenom, amt) +} - // Test HasCoins - require.True(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) - require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))) - require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5)))) +func newBarCoin(amt int64) sdk.Coin { + return sdk.NewInt64Coin(barDenom, amt) +} - // Test AddCoins - app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 25)))) +type IntegrationTestSuite struct { + suite.Suite - app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 15))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 15), sdk.NewInt64Coin("foocoin", 25)))) + app *simapp.SimApp + ctx sdk.Context +} - // Test SubtractCoins - app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))) - app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 15)))) +func (suite *IntegrationTestSuite) SetupTest() { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, abci.Header{}) - app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 11))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 15)))) + app.AccountKeeper.SetParams(ctx, auth.DefaultParams()) + app.BankKeeper.SetSendEnabled(ctx, true) - app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))) - require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1)))) + suite.app = app + suite.ctx = ctx +} - // Test SendCoins - app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) +func (suite *IntegrationTestSuite) TestInputOutputCoins() { + app, ctx := suite.app, suite.ctx + balances := sdk.NewCoins(newFooCoin(90), newBarCoin(30)) - app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) + addr1 := sdk.AccAddress([]byte("addr1")) + acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) + app.AccountKeeper.SetAccount(ctx, acc1) - app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 30))) - app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 5))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 5)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 10)))) + addr2 := sdk.AccAddress([]byte("addr2")) + acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + app.AccountKeeper.SetAccount(ctx, acc2) - // Test InputOutputCoins - input1 := types.NewInput(addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 2))) - output1 := types.NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 2))) - app.BankKeeper.InputOutputCoins(ctx, []types.Input{input1}, []types.Output{output1}) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 7)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 8)))) + addr3 := sdk.AccAddress([]byte("addr3")) + acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) + app.AccountKeeper.SetAccount(ctx, acc3) inputs := []types.Input{ - types.NewInput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 3))), - types.NewInput(addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 3), sdk.NewInt64Coin("foocoin", 2))), + {Address: addr1, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))}, + {Address: addr1, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))}, } - outputs := []types.Output{ - types.NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1))), - types.NewOutput(addr3, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5))), + {Address: addr2, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))}, + {Address: addr3, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))}, } - app.BankKeeper.InputOutputCoins(ctx, inputs, outputs) - require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 21), sdk.NewInt64Coin("foocoin", 4)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 7), sdk.NewInt64Coin("foocoin", 6)))) - require.True(t, app.BankKeeper.GetCoins(ctx, addr3).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5)))) - - // Test retrieving black listed accounts - for acc := range simapp.GetMaccPerms() { - addr := supply.NewModuleAddress(acc) - require.Equal(t, app.BlacklistedAccAddrs()[addr.String()], app.BankKeeper.BlacklistedAddr(addr)) + + suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, []types.Output{})) + suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)) + + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances)) + + insufficientInputs := []types.Input{ + {Address: addr1, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))}, + {Address: addr1, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))}, } + insufficientOutputs := []types.Output{ + {Address: addr2, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))}, + {Address: addr3, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))}, + } + suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, insufficientInputs, insufficientOutputs)) + suite.Require().NoError(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)) + + acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1) + expected := sdk.NewCoins(newFooCoin(30), newBarCoin(10)) + suite.Require().Equal(expected, acc1Balances) + + acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2) + suite.Require().Equal(expected, acc2Balances) + + acc3Balances := app.BankKeeper.GetAllBalances(ctx, addr3) + suite.Require().Equal(expected, acc3Balances) } -func TestSendKeeper(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestSendCoins() { + app, ctx := suite.app, suite.ctx + balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50)) - blacklistedAddrs := make(map[string]bool) + addr1 := sdk.AccAddress([]byte("addr1")) + acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) + app.AccountKeeper.SetAccount(ctx, acc1) - paramSpace := app.ParamsKeeper.Subspace("newspace") - sendKeeper := keep.NewBaseSendKeeper(app.AccountKeeper, paramSpace, blacklistedAddrs) - app.BankKeeper.SetSendEnabled(ctx, true) + addr2 := sdk.AccAddress([]byte("addr2")) + acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + app.AccountKeeper.SetAccount(ctx, acc2) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, balances)) - addr := sdk.AccAddress([]byte("addr1")) + sendAmt := sdk.NewCoins(newFooCoin(50), newBarCoin(25)) + suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendAmt)) + + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances)) + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendAmt)) + + acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1) + expected := sdk.NewCoins(newFooCoin(50), newBarCoin(25)) + suite.Require().Equal(expected, acc1Balances) + + acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2) + expected = sdk.NewCoins(newFooCoin(150), newBarCoin(75)) + suite.Require().Equal(expected, acc2Balances) +} + +func (suite *IntegrationTestSuite) TestValidateBalance() { + app, ctx := suite.app, suite.ctx + now := tmtime.Now() + ctx = ctx.WithBlockHeader(abci.Header{Time: now}) + endTime := now.Add(24 * time.Hour) + + addr1 := sdk.AccAddress([]byte("addr1")) addr2 := sdk.AccAddress([]byte("addr2")) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - // Test GetCoins/SetCoins + suite.Require().Error(app.BankKeeper.ValidateBalance(ctx, addr1)) + + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) app.AccountKeeper.SetAccount(ctx, acc) - require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins())) - - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))) - require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - - // Test HasCoins - require.True(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) - require.False(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))) - require.False(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5)))) - - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))) - - // Test SendCoins - sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))) - require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) - - sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))) - require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) - - app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 30))) - sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 5))) - require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 5)))) - require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 10)))) - - // validate coins with invalid denoms or negative values cannot be sent - // NOTE: We must use the Coin literal as the constructor does not allow - // negative values. - err := sendKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{sdk.Coin{Denom: "FOOCOIN", Amount: sdk.NewInt(-5)}}) - require.Error(t, err) + + balances := sdk.NewCoins(newFooCoin(100)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances)) + suite.Require().NoError(app.BankKeeper.ValidateBalance(ctx, addr1)) + + bacc := auth.NewBaseAccountWithAddress(addr2) + vacc := vesting.NewContinuousVestingAccount(&bacc, balances.Add(balances...), now.Unix(), endTime.Unix()) + + app.AccountKeeper.SetAccount(ctx, vacc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, balances)) + suite.Require().Error(app.BankKeeper.ValidateBalance(ctx, addr2)) } -func TestMsgSendEvents(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestBalance() { + app, ctx := suite.app, suite.ctx + addr := sdk.AccAddress([]byte("addr1")) - app.BankKeeper.SetSendEnabled(ctx, true) + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) + app.AccountKeeper.SetAccount(ctx, acc) + + suite.Require().Equal(sdk.NewCoin(fooDenom, sdk.ZeroInt()), app.BankKeeper.GetBalance(ctx, addr, fooDenom)) + balances := sdk.NewCoins(newFooCoin(100)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr, balances)) + + suite.Require().Equal(balances.AmountOf(fooDenom), app.BankKeeper.GetBalance(ctx, addr, fooDenom).Amount) + suite.Require().Equal(balances, app.BankKeeper.GetAllBalances(ctx, addr)) + + newFooBalance := newFooCoin(99) + suite.Require().NoError(app.BankKeeper.SetBalance(ctx, addr, newFooBalance)) + suite.Require().Equal(newFooBalance, app.BankKeeper.GetBalance(ctx, addr, fooDenom)) + + balances = sdk.NewCoins(newBarCoin(500)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr, balances)) + suite.Require().Equal(sdk.NewCoin(fooDenom, sdk.ZeroInt()), app.BankKeeper.GetBalance(ctx, addr, fooDenom)) + suite.Require().Equal(balances.AmountOf(barDenom), app.BankKeeper.GetBalance(ctx, addr, barDenom).Amount) + suite.Require().Equal(balances, app.BankKeeper.GetAllBalances(ctx, addr)) + invalidBalance := sdk.Coin{Denom: "fooDenom", Amount: sdk.NewInt(-50)} + suite.Require().Error(app.BankKeeper.SetBalance(ctx, addr, invalidBalance)) +} + +func (suite *IntegrationTestSuite) TestSendEnabled() { + app, ctx := suite.app, suite.ctx + enabled := false + app.BankKeeper.SetSendEnabled(ctx, enabled) + suite.Require().Equal(enabled, app.BankKeeper.GetSendEnabled(ctx)) +} + +func (suite *IntegrationTestSuite) TestHasBalance() { + app, ctx := suite.app, suite.ctx + addr := sdk.AccAddress([]byte("addr1")) + + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) + app.AccountKeeper.SetAccount(ctx, acc) + + balances := sdk.NewCoins(newFooCoin(100)) + suite.Require().False(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(99))) + + app.BankKeeper.SetBalances(ctx, addr, balances) + suite.Require().False(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(101))) + suite.Require().True(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(100))) + suite.Require().True(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(1))) +} + +func (suite *IntegrationTestSuite) TestMsgSendEvents() { + app, ctx := suite.app, suite.ctx addr := sdk.AccAddress([]byte("addr1")) addr2 := sdk.AccAddress([]byte("addr2")) acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) app.AccountKeeper.SetAccount(ctx, acc) - newCoins := sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)) - err := app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins) - require.Error(t, err) + newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) + + suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)) + events := ctx.EventManager().Events() - require.Equal(t, 2, len(events)) + suite.Require().Equal(2, len(events)) + event1 := sdk.Event{ Type: types.EventTypeTransfer, Attributes: []tmkv.Pair{}, } event1.Attributes = append( event1.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr2.String())}) + tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr2.String())}, + ) event1.Attributes = append( event1.Attributes, - tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())}) + tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())}, + ) event2 := sdk.Event{ Type: sdk.EventTypeMessage, Attributes: []tmkv.Pair{}, } event2.Attributes = append( event2.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())}) - require.Equal(t, event1, events[0]) - require.Equal(t, event2, events[1]) - - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))) - newCoins = sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)) - err = app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins) - require.NoError(t, err) + tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())}, + ) + + suite.Require().Equal(event1, events[0]) + suite.Require().Equal(event2, events[1]) + + app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))) + newCoins = sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) + + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)) + events = ctx.EventManager().Events() - require.Equal(t, 4, len(events)) - require.Equal(t, event1, events[2]) - require.Equal(t, event2, events[3]) + suite.Require().Equal(4, len(events)) + suite.Require().Equal(event1, events[2]) + suite.Require().Equal(event2, events[3]) } -func TestMsgMultiSendEvents(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestMsgMultiSendEvents() { + app, ctx := suite.app, suite.ctx app.BankKeeper.SetSendEnabled(ctx, true) @@ -208,8 +267,9 @@ func TestMsgMultiSendEvents(t *testing.T) { app.AccountKeeper.SetAccount(ctx, acc) app.AccountKeeper.SetAccount(ctx, acc2) - newCoins := sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)) - newCoins2 := sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100)) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) + newCoins2 := sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100)) inputs := []types.Input{ {Address: addr, Coins: newCoins}, {Address: addr2, Coins: newCoins2}, @@ -218,51 +278,58 @@ func TestMsgMultiSendEvents(t *testing.T) { {Address: addr3, Coins: newCoins}, {Address: addr4, Coins: newCoins2}, } - err := app.BankKeeper.InputOutputCoins(ctx, inputs, outputs) - require.Error(t, err) + + suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)) + events := ctx.EventManager().Events() - require.Equal(t, 0, len(events)) + suite.Require().Equal(0, len(events)) // Set addr's coins but not addr2's coins - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))) + app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))) + + suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)) - err = app.BankKeeper.InputOutputCoins(ctx, inputs, outputs) - require.Error(t, err) events = ctx.EventManager().Events() - require.Equal(t, 1, len(events)) + suite.Require().Equal(1, len(events)) + event1 := sdk.Event{ Type: sdk.EventTypeMessage, Attributes: []tmkv.Pair{}, } event1.Attributes = append( event1.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())}) - require.Equal(t, event1, events[0]) + tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())}, + ) + suite.Require().Equal(event1, events[0]) // Set addr's coins and addr2's coins - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))) - newCoins = sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)) - app.BankKeeper.SetCoins(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100))) - newCoins2 = sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100)) + app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))) + newCoins = sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) + + app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100))) + newCoins2 = sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100)) + + suite.Require().NoError(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)) - err = app.BankKeeper.InputOutputCoins(ctx, inputs, outputs) - require.NoError(t, err) events = ctx.EventManager().Events() - require.Equal(t, 5, len(events)) + suite.Require().Equal(5, len(events)) + event2 := sdk.Event{ Type: sdk.EventTypeMessage, Attributes: []tmkv.Pair{}, } event2.Attributes = append( event2.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr2.String())}) + tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr2.String())}, + ) event3 := sdk.Event{ Type: types.EventTypeTransfer, Attributes: []tmkv.Pair{}, } event3.Attributes = append( event3.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr3.String())}) + tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr3.String())}, + ) event3.Attributes = append( event3.Attributes, tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())}) @@ -272,41 +339,52 @@ func TestMsgMultiSendEvents(t *testing.T) { } event4.Attributes = append( event4.Attributes, - tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr4.String())}) + tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr4.String())}, + ) event4.Attributes = append( event4.Attributes, - tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins2.String())}) - require.Equal(t, event1, events[1]) - require.Equal(t, event2, events[2]) - require.Equal(t, event3, events[3]) - require.Equal(t, event4, events[4]) + tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins2.String())}, + ) + + suite.Require().Equal(event1, events[1]) + suite.Require().Equal(event2, events[2]) + suite.Require().Equal(event3, events[3]) + suite.Require().Equal(event4, events[4]) } -func TestViewKeeper(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestSpendableCoins() { + app, ctx := suite.app, suite.ctx + now := tmtime.Now() + ctx = ctx.WithBlockHeader(abci.Header{Time: now}) + endTime := now.Add(24 * time.Hour) - //paramSpace := app.ParamsKeeper.Subspace(types.DefaultParamspace) - viewKeeper := keep.NewBaseViewKeeper(app.AccountKeeper) + origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) + delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) - addr := sdk.AccAddress([]byte("addr1")) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) + addr1 := sdk.AccAddress([]byte("addr1")) + addr2 := sdk.AccAddress([]byte("addr2")) + addrModule := sdk.AccAddress([]byte("moduleAcc")) + + macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) + bacc := auth.NewBaseAccountWithAddress(addr1) + vacc := vesting.NewContinuousVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix()) + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) - // Test GetCoins/SetCoins + app.AccountKeeper.SetAccount(ctx, macc) + app.AccountKeeper.SetAccount(ctx, vacc) app.AccountKeeper.SetAccount(ctx, acc) - require.True(t, viewKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins())) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins)) - app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))) - require.True(t, viewKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) + suite.Require().Equal(origCoins, app.BankKeeper.SpendableCoins(ctx, addr2)) - // Test HasCoins - require.True(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))) - require.True(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))) - require.False(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))) - require.False(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5)))) + ctx = ctx.WithBlockTime(now.Add(12 * time.Hour)) + suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins)) + suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.SpendableCoins(ctx, addr1)) } -func TestVestingAccountSend(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestVestingAccountSend() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) endTime := now.Add(24 * time.Hour) @@ -316,29 +394,27 @@ func TestVestingAccountSend(t *testing.T) { addr1 := sdk.AccAddress([]byte("addr1")) addr2 := sdk.AccAddress([]byte("addr2")) + bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) - vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix()) + vacc := vesting.NewContinuousVestingAccount(&bacc, origCoins, now.Unix(), endTime.Unix()) + app.AccountKeeper.SetAccount(ctx, vacc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) // require that no coins be sendable at the beginning of the vesting schedule - err := app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins) - require.Error(t, err) + suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)) // receive some coins - vacc.SetCoins(origCoins.Add(sendCoins...)) - app.AccountKeeper.SetAccount(ctx, vacc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins.Add(sendCoins...))) // require that all vested coins are spendable plus any received ctx = ctx.WithBlockTime(now.Add(12 * time.Hour)) - err = app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins) - vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount) - require.NoError(t, err) - require.Equal(t, origCoins, vacc.GetCoins()) + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)) + suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1)) } -func TestPeriodicVestingAccountSend(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestPeriodicVestingAccountSend() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) @@ -346,34 +422,32 @@ func TestPeriodicVestingAccountSend(t *testing.T) { addr1 := sdk.AccAddress([]byte("addr1")) addr2 := sdk.AccAddress([]byte("addr2")) - bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) periods := vesting.Periods{ vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}}, vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}}, vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}}, } - vacc := vesting.NewPeriodicVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), periods) + + bacc := auth.NewBaseAccountWithAddress(addr1) + vacc := vesting.NewPeriodicVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), periods) + app.AccountKeeper.SetAccount(ctx, vacc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) // require that no coins be sendable at the beginning of the vesting schedule - err := app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins) - require.Error(t, err) + suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)) // receive some coins - vacc.SetCoins(origCoins.Add(sendCoins...)) - app.AccountKeeper.SetAccount(ctx, vacc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins.Add(sendCoins...))) // require that all vested coins are spendable plus any received ctx = ctx.WithBlockTime(now.Add(12 * time.Hour)) - err = app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins) - vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.PeriodicVestingAccount) - require.NoError(t, err) - require.Equal(t, origCoins, vacc.GetCoins()) + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)) + suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1)) } -func TestVestingAccountReceive(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestVestingAccountReceive() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) endTime := now.Add(24 * time.Hour) @@ -385,27 +459,29 @@ func TestVestingAccountReceive(t *testing.T) { addr2 := sdk.AccAddress([]byte("addr2")) bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) - vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix()) + vacc := vesting.NewContinuousVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix()) acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + app.AccountKeeper.SetAccount(ctx, vacc) app.AccountKeeper.SetAccount(ctx, acc) - app.BankKeeper.SetCoins(ctx, addr2, origCoins) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins)) // send some coins to the vesting account - app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins) + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins)) // require the coins are spendable vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount) - require.Equal(t, origCoins.Add(sendCoins...), vacc.GetCoins()) - require.Equal(t, vacc.SpendableCoins(now), sendCoins) + balances := app.BankKeeper.GetAllBalances(ctx, addr1) + suite.Require().Equal(origCoins.Add(sendCoins...), balances) + suite.Require().Equal(balances.Sub(vacc.LockedCoins(balances, now)), sendCoins) // require coins are spendable plus any that have vested - require.Equal(t, vacc.SpendableCoins(now.Add(12*time.Hour)), origCoins) + suite.Require().Equal(balances.Sub(vacc.LockedCoins(balances, now.Add(12*time.Hour))), origCoins) } -func TestPeriodicVestingAccountReceive(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestPeriodicVestingAccountReceive() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) @@ -416,36 +492,38 @@ func TestPeriodicVestingAccountReceive(t *testing.T) { addr2 := sdk.AccAddress([]byte("addr2")) bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) periods := vesting.Periods{ vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}}, vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}}, vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}}, } - vacc := vesting.NewPeriodicVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), periods) + + vacc := vesting.NewPeriodicVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), periods) acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + app.AccountKeeper.SetAccount(ctx, vacc) app.AccountKeeper.SetAccount(ctx, acc) - app.BankKeeper.SetCoins(ctx, addr2, origCoins) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins)) // send some coins to the vesting account - app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins) + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins)) // require the coins are spendable vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.PeriodicVestingAccount) - require.Equal(t, origCoins.Add(sendCoins...), vacc.GetCoins()) - require.Equal(t, vacc.SpendableCoins(now), sendCoins) + balances := app.BankKeeper.GetAllBalances(ctx, addr1) + suite.Require().Equal(origCoins.Add(sendCoins...), balances) + suite.Require().Equal(balances.Sub(vacc.LockedCoins(balances, now)), sendCoins) // require coins are spendable plus any that have vested - require.Equal(t, vacc.SpendableCoins(now.Add(12*time.Hour)), origCoins) + suite.Require().Equal(balances.Sub(vacc.LockedCoins(balances, now.Add(12*time.Hour))), origCoins) } -func TestDelegateCoins(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestDelegateCoins() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) endTime := now.Add(24 * time.Hour) - ak := app.AccountKeeper origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) @@ -454,39 +532,58 @@ func TestDelegateCoins(t *testing.T) { addr2 := sdk.AccAddress([]byte("addr2")) addrModule := sdk.AccAddress([]byte("moduleAcc")) + macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) - macc := ak.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing - vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix()) - acc := ak.NewAccountWithAddress(ctx, addr2) - ak.SetAccount(ctx, vacc) - ak.SetAccount(ctx, acc) - ak.SetAccount(ctx, macc) - app.BankKeeper.SetCoins(ctx, addr2, origCoins) + vacc := vesting.NewContinuousVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix()) + + app.AccountKeeper.SetAccount(ctx, vacc) + app.AccountKeeper.SetAccount(ctx, acc) + app.AccountKeeper.SetAccount(ctx, macc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins)) ctx = ctx.WithBlockTime(now.Add(12 * time.Hour)) // require the ability for a non-vesting account to delegate - err := app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins) - acc = ak.GetAccount(ctx, addr2) - macc = ak.GetAccount(ctx, addrModule) - require.NoError(t, err) - require.Equal(t, origCoins.Sub(delCoins), acc.GetCoins()) - require.Equal(t, delCoins, macc.GetCoins()) + suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins)) + suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr2)) + suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule)) // require the ability for a vesting account to delegate - err = app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins) - vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount) - require.NoError(t, err) - require.Equal(t, delCoins, vacc.GetCoins()) + suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)) + suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addr1)) +} + +func (suite *IntegrationTestSuite) TestDelegateCoins_Invalid() { + app, ctx := suite.app, suite.ctx + + origCoins := sdk.NewCoins(newFooCoin(100)) + delCoins := sdk.NewCoins(newFooCoin(50)) + + addr1 := sdk.AccAddress([]byte("addr1")) + addrModule := sdk.AccAddress([]byte("moduleAcc")) + macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) + + suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)) + invalidCoins := sdk.Coins{sdk.Coin{Denom: "fooDenom", Amount: sdk.NewInt(-50)}} + suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, invalidCoins)) + + app.AccountKeeper.SetAccount(ctx, macc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + + suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)) + app.AccountKeeper.SetAccount(ctx, acc) + + suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, origCoins.Add(origCoins...))) } -func TestUndelegateCoins(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestUndelegateCoins() { + app, ctx := suite.app, suite.ctx now := tmtime.Now() ctx = ctx.WithBlockHeader(abci.Header{Time: now}) endTime := now.Add(24 * time.Hour) - ak := app.AccountKeeper origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) @@ -496,50 +593,67 @@ func TestUndelegateCoins(t *testing.T) { addrModule := sdk.AccAddress([]byte("moduleAcc")) bacc := auth.NewBaseAccountWithAddress(addr1) - bacc.SetCoins(origCoins) - macc := ak.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing - vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix()) - acc := ak.NewAccountWithAddress(ctx, addr2) - ak.SetAccount(ctx, vacc) - ak.SetAccount(ctx, acc) - ak.SetAccount(ctx, macc) - app.BankKeeper.SetCoins(ctx, addr2, origCoins) + macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing + + vacc := vesting.NewContinuousVestingAccount(&bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix()) + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + + app.AccountKeeper.SetAccount(ctx, vacc) + app.AccountKeeper.SetAccount(ctx, acc) + app.AccountKeeper.SetAccount(ctx, macc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins)) ctx = ctx.WithBlockTime(now.Add(12 * time.Hour)) // require the ability for a non-vesting account to delegate err := app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins) - require.NoError(t, err) + suite.Require().NoError(err) - acc = ak.GetAccount(ctx, addr2) - macc = ak.GetAccount(ctx, addrModule) - require.Equal(t, origCoins.Sub(delCoins), acc.GetCoins()) - require.Equal(t, delCoins, macc.GetCoins()) + suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr2)) + suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule)) // require the ability for a non-vesting account to undelegate - err = app.BankKeeper.UndelegateCoins(ctx, addrModule, addr2, delCoins) - require.NoError(t, err) + suite.Require().NoError(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr2, delCoins)) - acc = ak.GetAccount(ctx, addr2) - macc = ak.GetAccount(ctx, addrModule) - require.Equal(t, origCoins, acc.GetCoins()) - require.True(t, macc.GetCoins().Empty()) + suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr2)) + suite.Require().True(app.BankKeeper.GetAllBalances(ctx, addrModule).Empty()) // require the ability for a vesting account to delegate - err = app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins) - require.NoError(t, err) + suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)) - vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount) - macc = ak.GetAccount(ctx, addrModule) - require.Equal(t, origCoins.Sub(delCoins), vacc.GetCoins()) - require.Equal(t, delCoins, macc.GetCoins()) + suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr1)) + suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule)) // require the ability for a vesting account to undelegate - err = app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins) - require.NoError(t, err) + suite.Require().NoError(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins)) + + suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1)) + suite.Require().True(app.BankKeeper.GetAllBalances(ctx, addrModule).Empty()) +} + +func (suite *IntegrationTestSuite) TestUndelegateCoins_Invalid() { + app, ctx := suite.app, suite.ctx + + origCoins := sdk.NewCoins(newFooCoin(100)) + delCoins := sdk.NewCoins(newFooCoin(50)) + + addr1 := sdk.AccAddress([]byte("addr1")) + addrModule := sdk.AccAddress([]byte("moduleAcc")) + macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) + + suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins)) + + app.AccountKeeper.SetAccount(ctx, macc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins)) + + suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins)) + app.AccountKeeper.SetAccount(ctx, acc) + + suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins)) +} - vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount) - macc = ak.GetAccount(ctx, addrModule) - require.Equal(t, origCoins, vacc.GetCoins()) - require.True(t, macc.GetCoins().Empty()) +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) } diff --git a/x/bank/internal/keeper/querier_test.go b/x/bank/internal/keeper/querier_test.go index 11f7504043a4..643f7b1798cf 100644 --- a/x/bank/internal/keeper/querier_test.go +++ b/x/bank/internal/keeper/querier_test.go @@ -2,59 +2,95 @@ package keeper_test import ( "fmt" - "testing" - - "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - keep "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" + "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" "github.com/cosmos/cosmos-sdk/x/bank/internal/types" ) -func TestBalances(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() { + app, ctx := suite.app, suite.ctx + _, _, addr := authtypes.KeyTestPubAddr() req := abci.RequestQuery{ - Path: fmt.Sprintf("custom/bank/%s", keep.QueryBalance), + Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryBalance), Data: []byte{}, } - querier := keep.NewQuerier(app.BankKeeper) + querier := keeper.NewQuerier(app.BankKeeper) + + res, err := querier(ctx, []string{types.QueryBalance}, req) + suite.Require().NotNil(err) + suite.Require().Nil(res) + + req.Data = app.Codec().MustMarshalJSON(types.NewQueryBalanceParams(addr, fooDenom)) + res, err = querier(ctx, []string{types.QueryBalance}, req) + suite.Require().NoError(err) + suite.Require().NotNil(res) + + var balance sdk.Coin + suite.Require().NoError(app.Codec().UnmarshalJSON(res, &balance)) + suite.True(balance.IsZero()) + + origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30)) + acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) + + app.AccountKeeper.SetAccount(ctx, acc) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, acc.GetAddress(), origCoins)) - res, err := querier(ctx, []string{"balances"}, req) - require.NotNil(t, err) - require.Nil(t, res) + res, err = querier(ctx, []string{types.QueryBalance}, req) + suite.Require().NoError(err) + suite.Require().NotNil(res) + suite.Require().NoError(app.Codec().UnmarshalJSON(res, &balance)) + suite.True(balance.IsEqual(newFooCoin(50))) +} +func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() { + app, ctx := suite.app, suite.ctx _, _, addr := authtypes.KeyTestPubAddr() - req.Data = app.Codec().MustMarshalJSON(types.NewQueryBalanceParams(addr)) - res, err = querier(ctx, []string{"balances"}, req) - require.Nil(t, err) // the account does not exist, no error returned anyway - require.NotNil(t, res) + req := abci.RequestQuery{ + Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryAllBalances), + Data: []byte{}, + } + + querier := keeper.NewQuerier(app.BankKeeper) - var coins sdk.Coins - require.NoError(t, app.Codec().UnmarshalJSON(res, &coins)) - require.True(t, coins.IsZero()) + res, err := querier(ctx, []string{types.QueryAllBalances}, req) + suite.Require().NotNil(err) + suite.Require().Nil(res) + req.Data = app.Codec().MustMarshalJSON(types.NewQueryAllBalancesParams(addr)) + res, err = querier(ctx, []string{types.QueryAllBalances}, req) + suite.Require().NoError(err) + suite.Require().NotNil(res) + + var balances sdk.Coins + suite.Require().NoError(app.Codec().UnmarshalJSON(res, &balances)) + suite.True(balances.IsZero()) + + origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30)) acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - acc.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("foo", 10))) + app.AccountKeeper.SetAccount(ctx, acc) - res, err = querier(ctx, []string{"balances"}, req) - require.Nil(t, err) - require.NotNil(t, res) - require.NoError(t, app.Codec().UnmarshalJSON(res, &coins)) - require.True(t, coins.AmountOf("foo").Equal(sdk.NewInt(10))) + suite.Require().NoError(app.BankKeeper.SetBalances(ctx, acc.GetAddress(), origCoins)) + + res, err = querier(ctx, []string{types.QueryAllBalances}, req) + suite.Require().NoError(err) + suite.Require().NotNil(res) + suite.Require().NoError(app.Codec().UnmarshalJSON(res, &balances)) + suite.True(balances.IsEqual(origCoins)) } -func TestQuerierRouteNotFound(t *testing.T) { - app, ctx := createTestApp(false) +func (suite *IntegrationTestSuite) TestQuerierRouteNotFound() { + app, ctx := suite.app, suite.ctx req := abci.RequestQuery{ - Path: "custom/bank/notfound", + Path: fmt.Sprintf("custom/%s/invalid", types.ModuleName), Data: []byte{}, } - querier := keep.NewQuerier(app.BankKeeper) - _, err := querier(ctx, []string{"notfound"}, req) - require.Error(t, err) + querier := keeper.NewQuerier(app.BankKeeper) + _, err := querier(ctx, []string{"invalid"}, req) + suite.Error(err) } diff --git a/x/bank/internal/types/key.go b/x/bank/internal/types/key.go index 0a9e716f73a1..dcb106553478 100644 --- a/x/bank/internal/types/key.go +++ b/x/bank/internal/types/key.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -23,8 +25,14 @@ var ( BalancesPrefix = []byte("balances") ) -// AddressFromBalancesKey returns an account address from a balances key which -// is used as an index to store balances per account. -func AddressFromBalancesKey(key []byte) sdk.AccAddress { - return sdk.AccAddress(key[len(BalancesPrefix) : len(BalancesPrefix)+sdk.AddrLen]) +// AddressFromBalancesStore returns an account address from a balances prefix +// store. The key must not contain the perfix BalancesPrefix as the prefix store +// iterator discards the actual prefix. +func AddressFromBalancesStore(key []byte) sdk.AccAddress { + addr := key[:sdk.AddrLen] + if len(addr) != sdk.AddrLen { + panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), sdk.AddrLen)) + } + + return sdk.AccAddress(addr) } diff --git a/x/bank/internal/types/key_test.go b/x/bank/internal/types/key_test.go index 5065794588eb..a16a22381d6e 100644 --- a/x/bank/internal/types/key_test.go +++ b/x/bank/internal/types/key_test.go @@ -15,12 +15,11 @@ func cloneAppend(bz []byte, tail []byte) (res []byte) { return } -func TestAddressFromBalancesKey(t *testing.T) { +func TestAddressFromBalancesStore(t *testing.T) { addr, err := sdk.AccAddressFromBech32("cosmos1n88uc38xhjgxzw9nwre4ep2c8ga4fjxcar6mn7") require.NoError(t, err) - key := cloneAppend(types.BalancesPrefix, addr.Bytes()) - - res := types.AddressFromBalancesKey(key) + key := cloneAppend(addr.Bytes(), []byte("stake")) + res := types.AddressFromBalancesStore(key) require.Equal(t, res, addr) } diff --git a/x/bank/legacy/v0_38/types.go b/x/bank/legacy/v0_38/types.go new file mode 100644 index 000000000000..e67640bf2b12 --- /dev/null +++ b/x/bank/legacy/v0_38/types.go @@ -0,0 +1,14 @@ +package v038 + +// DONTCOVER +// nolint + +const ( + ModuleName = "bank" +) + +type ( + GenesisState struct { + SendEnabled bool `json:"send_enabled" yaml:"send_enabled"` + } +) diff --git a/x/bank/legacy/v0_39/migrate.go b/x/bank/legacy/v0_39/migrate.go new file mode 100644 index 000000000000..222992357d9f --- /dev/null +++ b/x/bank/legacy/v0_39/migrate.go @@ -0,0 +1,22 @@ +package v039 + +import ( + v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38" + v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38" +) + +// Migrate accepts exported x/auth and x/bank genesis state from v0.38 and migrates +// it to v0.39 x/bank genesis state. The migration includes: +// +// - Moving balances from x/auth to x/bank genesis state. +func Migrate(bankGenState v038bank.GenesisState, authGenState v038auth.GenesisState) GenesisState { + balances := make([]Balance, len(authGenState.Accounts)) + for i, acc := range authGenState.Accounts { + balances[i] = Balance{ + Address: acc.GetAddress(), + Coins: acc.GetCoins(), + } + } + + return NewGenesisState(bankGenState.SendEnabled, balances) +} diff --git a/x/bank/legacy/v0_39/migrate_test.go b/x/bank/legacy/v0_39/migrate_test.go new file mode 100644 index 000000000000..ae51134d47c1 --- /dev/null +++ b/x/bank/legacy/v0_39/migrate_test.go @@ -0,0 +1,67 @@ +package v039_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38" + v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38" + v039bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_39" + + "github.com/stretchr/testify/require" +) + +func TestMigrate(t *testing.T) { + v039Codec := codec.New() + codec.RegisterCrypto(v039Codec) + v038auth.RegisterCodec(v039Codec) + + coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) + addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u") + acc1 := v038auth.NewBaseAccount(addr1, coins, nil, 1, 0) + + addr2, _ := sdk.AccAddressFromBech32("cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74") + vaac := v038auth.NewContinuousVestingAccountRaw( + v038auth.NewBaseVestingAccount( + v038auth.NewBaseAccount(addr2, coins, nil, 1, 0), coins, nil, nil, 3160620846, + ), + 1580309972, + ) + + bankGenState := v038bank.GenesisState{ + SendEnabled: true, + } + authGenState := v038auth.GenesisState{ + Accounts: v038auth.GenesisAccounts{acc1, vaac}, + } + + migrated := v039bank.Migrate(bankGenState, authGenState) + expected := `{ + "send_enabled": true, + "balances": [ + { + "address": "cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u", + "coins": [ + { + "denom": "stake", + "amount": "50" + } + ] + }, + { + "address": "cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74", + "coins": [ + { + "denom": "stake", + "amount": "50" + } + ] + } + ] +}` + + bz, err := v039Codec.MarshalJSONIndent(migrated, "", " ") + require.NoError(t, err) + require.Equal(t, expected, string(bz)) +} diff --git a/x/bank/legacy/v0_39/types.go b/x/bank/legacy/v0_39/types.go new file mode 100644 index 000000000000..d569ac4f7573 --- /dev/null +++ b/x/bank/legacy/v0_39/types.go @@ -0,0 +1,43 @@ +package v039 + +// DONTCOVER +// nolint + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +const ( + ModuleName = "bank" +) + +var _ GenesisBalance = (*Balance)(nil) + +type ( + GenesisBalance interface { + GetAddress() sdk.AccAddress + GetCoins() sdk.Coins + } + + GenesisState struct { + SendEnabled bool `json:"send_enabled" yaml:"send_enabled"` + Balances []Balance `json:"balances" yaml:"balances"` + } + + Balance struct { + Address sdk.AccAddress `json:"address" yaml:"address"` + Coins sdk.Coins `json:"coins" yaml:"coins"` + } +) + +func NewGenesisState(sendEnabled bool, balances []Balance) GenesisState { + return GenesisState{SendEnabled: sendEnabled, Balances: balances} +} + +func (b Balance) GetAddress() sdk.AccAddress { + return b.Address +} + +func (b Balance) GetCoins() sdk.Coins { + return b.Coins +} diff --git a/x/bank/simulation/genesis.go b/x/bank/simulation/genesis.go index 38172b208768..b593fa05b070 100644 --- a/x/bank/simulation/genesis.go +++ b/x/bank/simulation/genesis.go @@ -3,10 +3,8 @@ package simulation // DONTCOVER import ( - "fmt" "math/rand" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/bank/internal/types" @@ -47,6 +45,5 @@ func RandomizedGenState(simState *module.SimulationState) { bankGenesis := types.NewGenesisState(sendEnabled, RandomGenesisBalances(simState)) - fmt.Printf("Selected randomly generated bank parameters:\n%s\n", codec.MustMarshalJSONIndent(simState.Cdc, bankGenesis)) simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(bankGenesis) } diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index 90c97522e68c..0c6d8d242b8d 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -22,8 +22,9 @@ const ( ) // WeightedOperations returns all the operations from the module with their respective weights -func WeightedOperations(appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper, - bk keeper.Keeper) simulation.WeightedOperations { +func WeightedOperations( + appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper, bk keeper.Keeper, +) simulation.WeightedOperations { var weightMsgSend, weightMsgMultiSend int appParams.GetOrGenerate(cdc, OpWeightMsgSend, &weightMsgSend, nil, @@ -84,20 +85,22 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simulation.Operat } // sendMsgSend sends a transaction with a MsgSend from a provided random account. +// nolint: interfacer func sendMsgSend( r *rand.Rand, app *baseapp.BaseApp, bk keeper.Keeper, ak types.AccountKeeper, msg types.MsgSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey, ) error { - account := ak.GetAccount(ctx, msg.FromAddress) - var ( fees sdk.Coins err error ) - cacheCtx, _ := ctx.CacheContext() - coins, err := bk.SubtractCoins(cacheCtx, msg.FromAddress, msg.Amount) - if err == nil { + + account := ak.GetAccount(ctx, msg.FromAddress) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) + + coins, hasNeg := spendable.SafeSub(msg.Amount) + if !hasNeg { fees, err = simulation.RandomFees(r, ctx, coins) if err != nil { return err @@ -218,6 +221,7 @@ func SimulateMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simulation.O // sendMsgMultiSend sends a transaction with a MsgMultiSend from a provided random // account. +// nolint: interfacer func sendMsgMultiSend( r *rand.Rand, app *baseapp.BaseApp, bk keeper.Keeper, ak types.AccountKeeper, msg types.MsgMultiSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey, @@ -232,16 +236,17 @@ func sendMsgMultiSend( sequenceNumbers[i] = acc.GetSequence() } - // feePayer is the first signer, i.e. first input address - feePayer := ak.GetAccount(ctx, msg.Inputs[0].Address) - var ( fees sdk.Coins err error ) - cacheCtx, _ := ctx.CacheContext() - coins, err := bk.SubtractCoins(cacheCtx, feePayer.GetAddress(), msg.Inputs[0].Coins) - if err == nil { + + // feePayer is the first signer, i.e. first input address + feePayer := ak.GetAccount(ctx, msg.Inputs[0].Address) + spendable := bk.SpendableCoins(ctx, feePayer.GetAddress()) + + coins, hasNeg := spendable.SafeSub(msg.Inputs[0].Coins) + if !hasNeg { fees, err = simulation.RandomFees(r, ctx, coins) if err != nil { return err @@ -268,6 +273,7 @@ func sendMsgMultiSend( // randomSendFields returns the sender and recipient simulation accounts as well // as the transferred amount. +// nolint: interfacer func randomSendFields( r *rand.Rand, ctx sdk.Context, accs []simulation.Account, bk keeper.Keeper, ak types.AccountKeeper, ) (simulation.Account, simulation.Account, sdk.Coins, bool, error) { @@ -285,11 +291,9 @@ func randomSendFields( return simAccount, toSimAcc, nil, true, nil // skip error } - balances := bk.GetAllBalances(ctx, acc.GetAddress()) - locked := bk.LockedCoins(ctx, acc.GetAddress()) - balances = balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, acc.GetAddress()) - sendCoins := simulation.RandSubsetCoins(r, balances) + sendCoins := simulation.RandSubsetCoins(r, spendable) if sendCoins.Empty() { return simAccount, toSimAcc, nil, true, nil // skip error } diff --git a/x/crisis/handler_test.go b/x/crisis/handler_test.go index 912bd863f7c5..2db4012d2f70 100644 --- a/x/crisis/handler_test.go +++ b/x/crisis/handler_test.go @@ -90,7 +90,7 @@ func TestHandleMsgVerifyInvariant(t *testing.T) { func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) { app, ctx, addrs := createTestApp() sender := addrs[0] - coin := app.AccountKeeper.GetAccount(ctx, sender).GetCoins()[0] + coin := app.BankKeeper.GetAllBalances(ctx, sender)[0] excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1)) app.CrisisKeeper.SetConstantFee(ctx, excessCoins) diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 83821ea3820d..61513a02dca3 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -11,7 +11,7 @@ import ( ) func TestAllocateTokensToValidatorWithCommission(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -44,7 +44,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { } func TestAllocateTokensToManyValidators(t *testing.T) { - ctx, ak, k, sk, supplyKeeper := CreateTestInputDefault(t, false, 1000) + ctx, ak, bk, k, sk, supplyKeeper := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -88,7 +88,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { feeCollector := supplyKeeper.GetModuleAccount(ctx, k.feeCollectorName) require.NotNil(t, feeCollector) - err = feeCollector.SetCoins(fees) + err = bk.SetBalances(ctx, feeCollector.GetAddress(), fees) require.NoError(t, err) ak.SetAccount(ctx, feeCollector) @@ -121,7 +121,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { func TestAllocateTokensTruncation(t *testing.T) { communityTax := sdk.NewDec(0) - ctx, ak, _, k, sk, _, supplyKeeper := CreateTestInputAdvanced(t, false, 1000000, communityTax) + ctx, ak, bk, k, sk, _, supplyKeeper := CreateTestInputAdvanced(t, false, 1000000, communityTax) sh := staking.NewHandler(sk) // create validator with 10% commission @@ -177,7 +177,7 @@ func TestAllocateTokensTruncation(t *testing.T) { feeCollector := supplyKeeper.GetModuleAccount(ctx, k.feeCollectorName) require.NotNil(t, feeCollector) - err = feeCollector.SetCoins(fees) + err = bk.SetBalances(ctx, feeCollector.GetAddress(), fees) require.NoError(t, err) ak.SetAccount(ctx, feeCollector) diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index 6d23ad958991..7f593fd5ecd6 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -10,7 +10,7 @@ import ( ) func TestCalculateRewardsBasic(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -67,7 +67,7 @@ func TestCalculateRewardsBasic(t *testing.T) { } func TestCalculateRewardsAfterSlash(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -132,7 +132,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { } func TestCalculateRewardsAfterManySlashes(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -209,7 +209,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { } func TestCalculateRewardsMultiDelegator(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -279,12 +279,12 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { func TestWithdrawDelegationRewardsBasic(t *testing.T) { balancePower := int64(1000) balanceTokens := sdk.TokensFromConsensusPower(balancePower) - ctx, ak, k, sk, _ := CreateTestInputDefault(t, false, balancePower) + ctx, _, bk, k, sk, _ := CreateTestInputDefault(t, false, balancePower) sh := staking.NewHandler(sk) // set module account coins distrAcc := k.GetDistributionAccount(ctx) - distrAcc.SetCoins(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, balanceTokens))) + require.NoError(t, bk.SetBalances(ctx, distrAcc.GetAddress(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, balanceTokens)))) k.supplyKeeper.SetModuleAccount(ctx, distrAcc) // create validator with 50% commission @@ -305,7 +305,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { expTokens := balanceTokens.Sub(valTokens) require.Equal(t, sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, expTokens)}, - ak.GetAccount(ctx, sdk.AccAddress(valOpAddr1)).GetCoins(), + bk.GetAllBalances(ctx, sdk.AccAddress(valOpAddr1)), ) // end block to bond validator @@ -337,7 +337,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { exp := balanceTokens.Sub(valTokens).Add(initial.QuoRaw(2)) require.Equal(t, sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, exp)}, - ak.GetAccount(ctx, sdk.AccAddress(valOpAddr1)).GetCoins(), + bk.GetAllBalances(ctx, sdk.AccAddress(valOpAddr1)), ) // withdraw commission @@ -348,12 +348,12 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { exp = balanceTokens.Sub(valTokens).Add(initial) require.Equal(t, sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, exp)}, - ak.GetAccount(ctx, sdk.AccAddress(valOpAddr1)).GetCoins(), + bk.GetAllBalances(ctx, sdk.AccAddress(valOpAddr1)), ) } func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -423,7 +423,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { } func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) // create validator with 50% commission @@ -505,13 +505,14 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { } func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { - ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, bk, k, sk, _ := CreateTestInputDefault(t, false, 1000) sh := staking.NewHandler(sk) initial := int64(20) // set module account coins distrAcc := k.GetDistributionAccount(ctx) - distrAcc.SetCoins(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))) + err := bk.SetBalances(ctx, distrAcc.GetAddress(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))) + require.NoError(t, err) k.supplyKeeper.SetModuleAccount(ctx, distrAcc) tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDec(initial))} diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 5b2d989f7f82..ce0721ce6658 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -11,7 +11,7 @@ import ( ) func TestSetWithdrawAddr(t *testing.T) { - ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000) // nolint: dogseld params := keeper.GetParams(ctx) params.WithdrawAddrEnabled = false @@ -31,7 +31,7 @@ func TestSetWithdrawAddr(t *testing.T) { } func TestWithdrawValidatorCommission(t *testing.T) { - ctx, ak, keeper, _, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, bk, keeper, _, _ := CreateTestInputDefault(t, false, 1000) valCommission := sdk.DecCoins{ sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(5).Quo(sdk.NewDec(4))), @@ -40,14 +40,14 @@ func TestWithdrawValidatorCommission(t *testing.T) { // set module account coins distrAcc := keeper.GetDistributionAccount(ctx) - distrAcc.SetCoins(sdk.NewCoins( + bk.SetBalances(ctx, distrAcc.GetAddress(), sdk.NewCoins( sdk.NewCoin("mytoken", sdk.NewInt(2)), sdk.NewCoin("stake", sdk.NewInt(2)), )) keeper.supplyKeeper.SetModuleAccount(ctx, distrAcc) // check initial balance - balance := ak.GetAccount(ctx, sdk.AccAddress(valOpAddr3)).GetCoins() + balance := bk.GetAllBalances(ctx, sdk.AccAddress(valOpAddr3)) expTokens := sdk.TokensFromConsensusPower(1000) expCoins := sdk.NewCoins(sdk.NewCoin("stake", expTokens)) require.Equal(t, expCoins, balance) @@ -62,7 +62,7 @@ func TestWithdrawValidatorCommission(t *testing.T) { keeper.WithdrawValidatorCommission(ctx, valOpAddr3) // check balance increase - balance = ak.GetAccount(ctx, sdk.AccAddress(valOpAddr3)).GetCoins() + balance = bk.GetAllBalances(ctx, sdk.AccAddress(valOpAddr3)) require.Equal(t, sdk.NewCoins( sdk.NewCoin("mytoken", sdk.NewInt(1)), sdk.NewCoin("stake", expTokens.AddRaw(1)), @@ -79,7 +79,7 @@ func TestWithdrawValidatorCommission(t *testing.T) { } func TestGetTotalRewards(t *testing.T) { - ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000) + ctx, _, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000) // nolint: dogseld valCommission := sdk.DecCoins{ sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(5).Quo(sdk.NewDec(4))), @@ -100,7 +100,7 @@ func TestFundCommunityPool(t *testing.T) { ctx, _, bk, keeper, _, _, _ := CreateTestInputAdvanced(t, false, 1000, sdk.NewDecWithPrec(2, 2)) amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) - _ = bk.SetCoins(ctx, delAddr1, amount) + require.NoError(t, bk.SetBalances(ctx, delAddr1, amount)) initPool := keeper.GetFeePool(ctx) assert.Empty(t, initPool.CommunityPool) @@ -109,5 +109,5 @@ func TestFundCommunityPool(t *testing.T) { assert.Nil(t, err) assert.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), keeper.GetFeePool(ctx).CommunityPool) - assert.Empty(t, bk.GetCoins(ctx, delAddr1)) + assert.Empty(t, bk.GetAllBalances(ctx, delAddr1)) } diff --git a/x/distribution/keeper/querier_test.go b/x/distribution/keeper/querier_test.go index b5efd15a3a62..ed87f42edd78 100644 --- a/x/distribution/keeper/querier_test.go +++ b/x/distribution/keeper/querier_test.go @@ -109,7 +109,7 @@ func TestQueries(t *testing.T) { cdc := codec.New() types.RegisterCodec(cdc) supply.RegisterCodec(cdc) - ctx, _, keeper, sk, _ := CreateTestInputDefault(t, false, 100) + ctx, _, _, keeper, sk, _ := CreateTestInputDefault(t, false, 100) querier := NewQuerier(keeper) // test param queries diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index 78425e47c11d..e049c3bbc57a 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" @@ -53,6 +54,9 @@ var ( delAddr1, delAddr2, delAddr3, valAccAddr1, valAccAddr2, valAccAddr3, } + pubkeys = []crypto.PubKey{ + delPk1, delPk2, delPk3, valOpPk1, valOpPk2, valOpPk3, + } distrAcc = supply.NewEmptyModuleAccount(types.ModuleName) ) @@ -73,21 +77,23 @@ func MakeTestCodec() *codec.Codec { // test input with default values func CreateTestInputDefault(t *testing.T, isCheckTx bool, initPower int64) ( - sdk.Context, auth.AccountKeeper, Keeper, staking.Keeper, types.SupplyKeeper) { + sdk.Context, auth.AccountKeeper, bank.Keeper, Keeper, staking.Keeper, types.SupplyKeeper) { communityTax := sdk.NewDecWithPrec(2, 2) - ctx, ak, _, dk, sk, _, supplyKeeper := CreateTestInputAdvanced(t, isCheckTx, initPower, communityTax) - return ctx, ak, dk, sk, supplyKeeper + ctx, ak, bk, dk, sk, _, supplyKeeper := CreateTestInputAdvanced(t, isCheckTx, initPower, communityTax) + return ctx, ak, bk, dk, sk, supplyKeeper } // hogpodge of all sorts of input required for testing -func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initPower int64, - communityTax sdk.Dec) (sdk.Context, auth.AccountKeeper, bank.Keeper, - Keeper, staking.Keeper, params.Keeper, types.SupplyKeeper) { +func CreateTestInputAdvanced( + t *testing.T, isCheckTx bool, initPower int64, communityTax sdk.Dec, +) (sdk.Context, auth.AccountKeeper, bank.Keeper, Keeper, staking.Keeper, params.Keeper, types.SupplyKeeper, +) { initTokens := sdk.TokensFromConsensusPower(initPower) + keyBank := sdk.NewKVStoreKey(bank.StoreKey) keyDistr := sdk.NewKVStoreKey(types.StoreKey) keyStaking := sdk.NewKVStoreKey(staking.StoreKey) keyAcc := sdk.NewKVStoreKey(auth.StoreKey) @@ -98,6 +104,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initPower int64, db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyBank, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyDistr, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db) @@ -123,7 +130,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initPower int64, ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger()) accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, pk.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) - bankKeeper := bank.NewBaseKeeper(accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs) + bankKeeper := bank.NewBaseKeeper(cdc, keyBank, accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs) maccPerms := map[string][]string{ auth.FeeCollectorName: nil, types.ModuleName: nil, @@ -132,19 +139,19 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initPower int64, } supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bankKeeper, maccPerms) - sk := staking.NewKeeper(cdc, keyStaking, supplyKeeper, pk.Subspace(staking.DefaultParamspace)) + sk := staking.NewKeeper(cdc, keyStaking, bankKeeper, supplyKeeper, pk.Subspace(staking.DefaultParamspace)) sk.SetParams(ctx, staking.DefaultParams()) - keeper := NewKeeper(cdc, keyDistr, pk.Subspace(types.DefaultParamspace), sk, supplyKeeper, auth.FeeCollectorName, blacklistedAddrs) + keeper := NewKeeper(cdc, keyDistr, pk.Subspace(types.DefaultParamspace), bankKeeper, sk, supplyKeeper, auth.FeeCollectorName, blacklistedAddrs) initCoins := sdk.NewCoins(sdk.NewCoin(sk.BondDenom(ctx), initTokens)) totalSupply := sdk.NewCoins(sdk.NewCoin(sk.BondDenom(ctx), initTokens.MulRaw(int64(len(TestAddrs))))) supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply)) // fill all the addresses with some coins, set the loose pool tokens simultaneously - for _, addr := range TestAddrs { - _, err := bankKeeper.AddCoins(ctx, addr, initCoins) - require.Nil(t, err) + for i, addr := range TestAddrs { + accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, pubkeys[i], uint64(i), 0)) + require.NoError(t, bankKeeper.SetBalances(ctx, addr, initCoins)) } // set module accounts diff --git a/x/distribution/proposal_handler_test.go b/x/distribution/proposal_handler_test.go index 1aeee1f2880a..1f13f15d5240 100644 --- a/x/distribution/proposal_handler_test.go +++ b/x/distribution/proposal_handler_test.go @@ -19,28 +19,24 @@ var ( ) func testProposal(recipient sdk.AccAddress, amount sdk.Coins) types.CommunityPoolSpendProposal { - return types.NewCommunityPoolSpendProposal( - "Test", - "description", - recipient, - amount, - ) + return types.NewCommunityPoolSpendProposal("Test", "description", recipient, amount) } func TestProposalHandlerPassed(t *testing.T) { - ctx, accountKeeper, keeper, _, supplyKeeper := CreateTestInputDefault(t, false, 10) + ctx, ak, bk, keeper, _, supplyKeeper := CreateTestInputDefault(t, false, 10) recipient := delAddr1 // add coins to the module account macc := keeper.GetDistributionAccount(ctx) - err := macc.SetCoins(macc.GetCoins().Add(amount...)) + balances := bk.GetAllBalances(ctx, macc.GetAddress()) + err := bk.SetBalances(ctx, macc.GetAddress(), balances.Add(amount...)) require.NoError(t, err) supplyKeeper.SetModuleAccount(ctx, macc) - account := accountKeeper.NewAccountWithAddress(ctx, recipient) - require.True(t, account.GetCoins().IsZero()) - accountKeeper.SetAccount(ctx, account) + account := ak.NewAccountWithAddress(ctx, recipient) + ak.SetAccount(ctx, account) + require.True(t, bk.GetAllBalances(ctx, account.GetAddress()).IsZero()) feePool := keeper.GetFeePool(ctx) feePool.CommunityPool = sdk.NewDecCoinsFromCoins(amount...) @@ -49,19 +45,23 @@ func TestProposalHandlerPassed(t *testing.T) { tp := testProposal(recipient, amount) hdlr := NewCommunityPoolSpendProposalHandler(keeper) require.NoError(t, hdlr(ctx, tp)) - require.Equal(t, accountKeeper.GetAccount(ctx, recipient).GetCoins(), amount) + + balances = bk.GetAllBalances(ctx, recipient) + require.Equal(t, balances, amount) } func TestProposalHandlerFailed(t *testing.T) { - ctx, accountKeeper, keeper, _, _ := CreateTestInputDefault(t, false, 10) + ctx, ak, bk, keeper, _, _ := CreateTestInputDefault(t, false, 10) recipient := delAddr1 - account := accountKeeper.NewAccountWithAddress(ctx, recipient) - require.True(t, account.GetCoins().IsZero()) - accountKeeper.SetAccount(ctx, account) + account := ak.NewAccountWithAddress(ctx, recipient) + ak.SetAccount(ctx, account) + require.True(t, bk.GetAllBalances(ctx, account.GetAddress()).IsZero()) tp := testProposal(recipient, amount) hdlr := NewCommunityPoolSpendProposalHandler(keeper) require.Error(t, hdlr(ctx, tp)) - require.True(t, accountKeeper.GetAccount(ctx, recipient).GetCoins().IsZero()) + + balances := bk.GetAllBalances(ctx, recipient) + require.True(t, balances.IsZero()) } diff --git a/x/distribution/simulation/operations.go b/x/distribution/simulation/operations.go index 1c81a047a72f..30bc1a61ec2e 100644 --- a/x/distribution/simulation/operations.go +++ b/x/distribution/simulation/operations.go @@ -91,9 +91,7 @@ func SimulateMsgSetWithdrawAddress(ak types.AccountKeeper, bk types.BankKeeper, simToAccount, _ := simulation.RandomAcc(r, accs) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -141,9 +139,7 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee } account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -194,9 +190,7 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban } account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -234,9 +228,7 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k funder, _ := simulation.RandomAcc(r, accs) account := ak.GetAccount(ctx, funder.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fundAmount := simulation.RandSubsetCoins(r, spendable) if fundAmount.Empty() { diff --git a/x/distribution/types/expected_keepers.go b/x/distribution/types/expected_keepers.go index 5937e1b986fb..1f9d0a51a348 100644 --- a/x/distribution/types/expected_keepers.go +++ b/x/distribution/types/expected_keepers.go @@ -19,6 +19,7 @@ type BankKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SetBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } // StakingKeeper expected staking keeper (noalias) diff --git a/x/evidence/internal/keeper/infraction_test.go b/x/evidence/internal/keeper/infraction_test.go index cbef53de1921..d0ae5d74239d 100644 --- a/x/evidence/internal/keeper/infraction_test.go +++ b/x/evidence/internal/keeper/infraction_test.go @@ -24,24 +24,24 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() { power := int64(100) stakingParams := suite.app.StakingKeeper.GetParams(ctx) - amt := sdk.TokensFromConsensusPower(power) + selfDelegation := sdk.TokensFromConsensusPower(power) operatorAddr, val := valAddresses[0], pubkeys[0] // create validator - res, err := staking.NewHandler(suite.app.StakingKeeper)(ctx, newTestMsgCreateValidator(operatorAddr, val, amt)) + res, err := staking.NewHandler(suite.app.StakingKeeper)(ctx, newTestMsgCreateValidator(operatorAddr, val, selfDelegation)) suite.NoError(err) suite.NotNil(res) // execute end-blocker and verify validator attributes staking.EndBlocker(ctx, suite.app.StakingKeeper) suite.Equal( - suite.app.BankKeeper.GetCoins(ctx, sdk.AccAddress(operatorAddr)), - sdk.NewCoins(sdk.NewCoin(stakingParams.BondDenom, initAmt.Sub(amt))), + suite.app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(operatorAddr)).String(), + sdk.NewCoins(sdk.NewCoin(stakingParams.BondDenom, initAmt.Sub(selfDelegation))).String(), ) - suite.Equal(amt, suite.app.StakingKeeper.Validator(ctx, operatorAddr).GetBondedTokens()) + suite.Equal(selfDelegation, suite.app.StakingKeeper.Validator(ctx, operatorAddr).GetBondedTokens()) // handle a signature to set signing info - suite.app.SlashingKeeper.HandleValidatorSignature(ctx, val.Address(), amt.Int64(), true) + suite.app.SlashingKeeper.HandleValidatorSignature(ctx, val.Address(), selfDelegation.Int64(), true) // double sign less than max age oldTokens := suite.app.StakingKeeper.Validator(ctx, operatorAddr).GetTokens() @@ -101,7 +101,7 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() { // execute end-blocker and verify validator attributes staking.EndBlocker(ctx, suite.app.StakingKeeper) suite.Equal( - suite.app.BankKeeper.GetCoins(ctx, sdk.AccAddress(operatorAddr)), + suite.app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(operatorAddr)), sdk.NewCoins(sdk.NewCoin(stakingParams.BondDenom, initAmt.Sub(amt))), ) suite.Equal(amt, suite.app.StakingKeeper.Validator(ctx, operatorAddr).GetBondedTokens()) diff --git a/x/evidence/internal/keeper/keeper_test.go b/x/evidence/internal/keeper/keeper_test.go index 45347ab96be0..d6e35a4956a9 100644 --- a/x/evidence/internal/keeper/keeper_test.go +++ b/x/evidence/internal/keeper/keeper_test.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/evidence" "github.com/cosmos/cosmos-sdk/x/evidence/exported" "github.com/cosmos/cosmos-sdk/x/evidence/internal/keeper" @@ -76,6 +77,11 @@ func (suite *KeeperTestSuite) SetupTest() { suite.querier = keeper.NewQuerier(*evidenceKeeper) suite.keeper = *evidenceKeeper suite.app = app + + for i, addr := range valAddresses { + addr := sdk.AccAddress(addr) + app.AccountKeeper.SetAccount(suite.ctx, auth.NewBaseAccount(addr, pubkeys[i], uint64(i), 0)) + } } func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence { diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 3ae77c506d61..eeba89356a95 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -16,6 +16,7 @@ import ( extypes "github.com/cosmos/cosmos-sdk/x/genutil" v036 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_36" v038 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_38" + v039 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_39" ) const ( @@ -29,6 +30,7 @@ const ( var migrationMap = extypes.MigrationMap{ "v0.36": v036.Migrate, "v0.38": v038.Migrate, // NOTE: v0.37 and v0.38 are genesis compatible + "v0.39": v039.Migrate, } // GetMigrationCallback returns a MigrationCallback for a given version. diff --git a/x/genutil/legacy/v0_39/migrate.go b/x/genutil/legacy/v0_39/migrate.go new file mode 100644 index 000000000000..aea5ccd3f91e --- /dev/null +++ b/x/genutil/legacy/v0_39/migrate.go @@ -0,0 +1,55 @@ +package v039 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38" + v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39" + v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38" + v039bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_39" + "github.com/cosmos/cosmos-sdk/x/genutil" +) + +func Migrate(appState genutil.AppMap) genutil.AppMap { + v038Codec := codec.New() + codec.RegisterCrypto(v038Codec) + v038auth.RegisterCodec(v038Codec) + + v039Codec := codec.New() + codec.RegisterCrypto(v039Codec) + v038auth.RegisterCodec(v039Codec) + + // remove balances from existing accounts + if appState[v038auth.ModuleName] != nil { + // unmarshal relative source genesis application state + var authGenState v038auth.GenesisState + v038Codec.MustUnmarshalJSON(appState[v038auth.ModuleName], &authGenState) + + // delete deprecated x/auth genesis state + delete(appState, v038auth.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v039auth.ModuleName] = v039Codec.MustMarshalJSON(v039auth.Migrate(authGenState)) + } + + if appState[v038bank.ModuleName] != nil { + // unmarshal relative source genesis application state + var bankGenState v038bank.GenesisState + v038Codec.MustUnmarshalJSON(appState[v038bank.ModuleName], &bankGenState) + + // unmarshal x/auth genesis state to retrieve all account balances + var authGenState v038auth.GenesisState + v038Codec.MustUnmarshalJSON(appState[v038auth.ModuleName], &authGenState) + + // delete deprecated x/bank genesis state + delete(appState, v038bank.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v039bank.ModuleName] = v039Codec.MustMarshalJSON( + v039bank.Migrate(bankGenState, authGenState), + ) + } + + return appState +} diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index db5509f45e18..355abed3e84e 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -5,7 +5,6 @@ import ( "time" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -263,7 +262,7 @@ func TestProposalPassedEndblocker(t *testing.T) { macc := input.keeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - initialModuleAccCoins := macc.GetCoins() + initialModuleAccCoins := input.bk.GetAllBalances(ctx, macc.GetAddress()) proposal, err := input.keeper.SubmitProposal(ctx, keep.TestProposal) require.NoError(t, err) @@ -277,7 +276,7 @@ func TestProposalPassedEndblocker(t *testing.T) { macc = input.keeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - moduleAccCoins := macc.GetCoins() + moduleAccCoins := input.bk.GetAllBalances(ctx, macc.GetAddress()) deposits := initialModuleAccCoins.Add(proposal.TotalDeposit...).Add(proposalCoins...) require.True(t, moduleAccCoins.IsEqual(deposits)) @@ -293,7 +292,7 @@ func TestProposalPassedEndblocker(t *testing.T) { macc = input.keeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - require.True(t, macc.GetCoins().IsEqual(initialModuleAccCoins)) + require.True(t, input.bk.GetAllBalances(ctx, macc.GetAddress()).IsEqual(initialModuleAccCoins)) } func TestEndBlockerProposalHandlerFailed(t *testing.T) { diff --git a/x/gov/genesis_test.go b/x/gov/genesis_test.go index 02b6eed9b42c..0add3343c406 100644 --- a/x/gov/genesis_test.go +++ b/x/gov/genesis_test.go @@ -3,9 +3,8 @@ package gov import ( "testing" - "github.com/stretchr/testify/require" - keep "github.com/cosmos/cosmos-sdk/x/gov/keeper" + "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" ) @@ -63,7 +62,8 @@ func TestImportExportQueues(t *testing.T) { require.True(t, proposal1.Status == StatusDepositPeriod) require.True(t, proposal2.Status == StatusVotingPeriod) - require.Equal(t, input2.keeper.GetDepositParams(ctx2).MinDeposit, input2.keeper.GetGovernanceAccount(ctx2).GetCoins()) + macc := input2.keeper.GetGovernanceAccount(ctx2) + require.Equal(t, input2.keeper.GetDepositParams(ctx2).MinDeposit, input2.bk.GetAllBalances(ctx2, macc.GetAddress())) // Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod. EndBlocker(ctx2, input2.keeper) diff --git a/x/gov/keeper/deposit_test.go b/x/gov/keeper/deposit_test.go index edf452464094..ed17d4481551 100644 --- a/x/gov/keeper/deposit_test.go +++ b/x/gov/keeper/deposit_test.go @@ -10,7 +10,7 @@ import ( ) func TestDeposits(t *testing.T) { - ctx, ak, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, bk, keeper, _, _ := createTestInput(t, false, 100) tp := TestProposal proposal, err := keeper.SubmitProposal(ctx, tp) @@ -20,8 +20,8 @@ func TestDeposits(t *testing.T) { fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4))) fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5))) - addr0Initial := ak.GetAccount(ctx, TestAddrs[0]).GetCoins() - addr1Initial := ak.GetAccount(ctx, TestAddrs[1]).GetCoins() + addr0Initial := bk.GetAllBalances(ctx, TestAddrs[0]) + addr1Initial := bk.GetAllBalances(ctx, TestAddrs[1]) require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins())) @@ -43,7 +43,7 @@ func TestDeposits(t *testing.T) { proposal, ok = keeper.GetProposal(ctx, proposalID) require.True(t, ok) require.Equal(t, fourStake, proposal.TotalDeposit) - require.Equal(t, addr0Initial.Sub(fourStake), ak.GetAccount(ctx, TestAddrs[0]).GetCoins()) + require.Equal(t, addr0Initial.Sub(fourStake), bk.GetAllBalances(ctx, TestAddrs[0])) // Check a second deposit from same address votingStarted, err = keeper.AddDeposit(ctx, proposalID, TestAddrs[0], fiveStake) @@ -56,7 +56,7 @@ func TestDeposits(t *testing.T) { proposal, ok = keeper.GetProposal(ctx, proposalID) require.True(t, ok) require.Equal(t, fourStake.Add(fiveStake...), proposal.TotalDeposit) - require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), ak.GetAccount(ctx, TestAddrs[0]).GetCoins()) + require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), bk.GetAllBalances(ctx, TestAddrs[0])) // Check third deposit from a new address votingStarted, err = keeper.AddDeposit(ctx, proposalID, TestAddrs[1], fourStake) @@ -69,7 +69,7 @@ func TestDeposits(t *testing.T) { proposal, ok = keeper.GetProposal(ctx, proposalID) require.True(t, ok) require.Equal(t, fourStake.Add(fiveStake...).Add(fourStake...), proposal.TotalDeposit) - require.Equal(t, addr1Initial.Sub(fourStake), ak.GetAccount(ctx, TestAddrs[1]).GetCoins()) + require.Equal(t, addr1Initial.Sub(fourStake), bk.GetAllBalances(ctx, TestAddrs[1])) // Check that proposal moved to voting period proposal, ok = keeper.GetProposal(ctx, proposalID) @@ -93,6 +93,6 @@ func TestDeposits(t *testing.T) { keeper.RefundDeposits(ctx, proposalID) deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[1]) require.False(t, found) - require.Equal(t, addr0Initial, ak.GetAccount(ctx, TestAddrs[0]).GetCoins()) - require.Equal(t, addr1Initial, ak.GetAccount(ctx, TestAddrs[1]).GetCoins()) + require.Equal(t, addr0Initial, bk.GetAllBalances(ctx, TestAddrs[0])) + require.Equal(t, addr1Initial, bk.GetAllBalances(ctx, TestAddrs[1])) } diff --git a/x/gov/keeper/keeper_test.go b/x/gov/keeper/keeper_test.go index 5549049e3a35..9cd2a0559e65 100644 --- a/x/gov/keeper/keeper_test.go +++ b/x/gov/keeper/keeper_test.go @@ -9,7 +9,7 @@ import ( ) func TestIncrementProposalNumber(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled tp := TestProposal keeper.SubmitProposal(ctx, tp) @@ -24,7 +24,7 @@ func TestIncrementProposalNumber(t *testing.T) { } func TestProposalQueues(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled // create test proposals tp := TestProposal diff --git a/x/gov/keeper/proposal_test.go b/x/gov/keeper/proposal_test.go index b8f37cbd3649..5848e0d753e9 100644 --- a/x/gov/keeper/proposal_test.go +++ b/x/gov/keeper/proposal_test.go @@ -14,7 +14,7 @@ import ( ) func TestGetSetProposal(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled tp := TestProposal proposal, err := keeper.SubmitProposal(ctx, tp) @@ -28,7 +28,7 @@ func TestGetSetProposal(t *testing.T) { } func TestActivateVotingPeriod(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled tp := TestProposal proposal, err := keeper.SubmitProposal(ctx, tp) @@ -97,7 +97,7 @@ func registerTestCodec(cdc *codec.Codec) { } func TestSubmitProposal(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled registerTestCodec(keeper.cdc) @@ -125,7 +125,7 @@ func TestSubmitProposal(t *testing.T) { func TestGetProposalsFiltered(t *testing.T) { proposalID := uint64(1) - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled status := []types.ProposalStatus{types.StatusDepositPeriod, types.StatusVotingPeriod} addr1 := sdk.AccAddress("foo") diff --git a/x/gov/keeper/querier_test.go b/x/gov/keeper/querier_test.go index f79057f03e96..54aeae6fe7a5 100644 --- a/x/gov/keeper/querier_test.go +++ b/x/gov/keeper/querier_test.go @@ -142,7 +142,7 @@ func getQueriedVotes(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sd } func TestQueries(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 1000) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 1000) // nolint: dogsled querier := NewQuerier(keeper) oneCoins := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)) @@ -285,7 +285,7 @@ func TestQueries(t *testing.T) { } func TestPaginatedVotesQuery(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 1000) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 1000) // nolint: dogsled proposal := types.Proposal{ ProposalID: 100, diff --git a/x/gov/keeper/tally_test.go b/x/gov/keeper/tally_test.go index ceaed4eb4437..52812793c951 100644 --- a/x/gov/keeper/tally_test.go +++ b/x/gov/keeper/tally_test.go @@ -11,7 +11,7 @@ import ( ) func TestTallyNoOneVotes(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 5, 5}) tp := TestProposal @@ -31,7 +31,7 @@ func TestTallyNoOneVotes(t *testing.T) { } func TestTallyNoQuorum(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{2, 5, 0}) tp := TestProposal @@ -52,7 +52,7 @@ func TestTallyNoQuorum(t *testing.T) { } func TestTallyOnlyValidatorsAllYes(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 5, 5}) tp := TestProposal @@ -76,7 +76,7 @@ func TestTallyOnlyValidatorsAllYes(t *testing.T) { } func TestTallyOnlyValidators51No(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 0}) tp := TestProposal @@ -98,7 +98,7 @@ func TestTallyOnlyValidators51No(t *testing.T) { } func TestTallyOnlyValidators51Yes(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 0}) tp := TestProposal @@ -121,7 +121,7 @@ func TestTallyOnlyValidators51Yes(t *testing.T) { } func TestTallyOnlyValidatorsVetoed(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{6, 6, 7}) tp := TestProposal @@ -146,7 +146,7 @@ func TestTallyOnlyValidatorsVetoed(t *testing.T) { } func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{6, 6, 7}) tp := TestProposal @@ -170,7 +170,7 @@ func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) { } func TestTallyOnlyValidatorsAbstainFails(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{6, 6, 7}) tp := TestProposal @@ -194,7 +194,7 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) { } func TestTallyOnlyValidatorsNonVoter(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 7}) tp := TestProposal @@ -217,7 +217,7 @@ func TestTallyOnlyValidatorsNonVoter(t *testing.T) { } func TestTallyDelgatorOverride(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 7}) delTokens := sdk.TokensFromConsensusPower(30) @@ -251,7 +251,7 @@ func TestTallyDelgatorOverride(t *testing.T) { } func TestTallyDelgatorInherit(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 7}) delTokens := sdk.TokensFromConsensusPower(30) @@ -284,7 +284,7 @@ func TestTallyDelgatorInherit(t *testing.T) { } func TestTallyDelgatorMultipleOverride(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{5, 6, 7}) delTokens := sdk.TokensFromConsensusPower(10) @@ -322,7 +322,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) { } func TestTallyDelgatorMultipleInherit(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{25, 6, 7}) delTokens := sdk.TokensFromConsensusPower(10) @@ -359,7 +359,7 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) { } func TestTallyJailedValidator(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{25, 6, 7}) delTokens := sdk.TokensFromConsensusPower(10) @@ -398,7 +398,7 @@ func TestTallyJailedValidator(t *testing.T) { } func TestTallyValidatorMultipleDelegations(t *testing.T) { - ctx, _, keeper, sk, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100) createValidators(ctx, sk, []int64{10, 10, 10}) delTokens := sdk.TokensFromConsensusPower(10) diff --git a/x/gov/keeper/test_common.go b/x/gov/keeper/test_common.go index 777d71a8c887..897ad8b05da5 100644 --- a/x/gov/keeper/test_common.go +++ b/x/gov/keeper/test_common.go @@ -52,6 +52,9 @@ var ( delAddr1, delAddr2, delAddr3, valAccAddr1, valAccAddr2, valAccAddr3, } + pubkeys = []crypto.PubKey{ + delPk1, delPk2, delPk3, valOpPk1, valOpPk2, valOpPk3, + } emptyDelAddr sdk.AccAddress emptyValAddr sdk.ValAddress @@ -88,11 +91,14 @@ func makeTestCodec() *codec.Codec { return cdc } -func createTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, Keeper, staking.Keeper, types.SupplyKeeper) { +func createTestInput( + t *testing.T, isCheckTx bool, initPower int64, +) (sdk.Context, auth.AccountKeeper, bank.Keeper, Keeper, staking.Keeper, types.SupplyKeeper, +) { initTokens := sdk.TokensFromConsensusPower(initPower) - keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyBank := sdk.NewKVStoreKey(bank.StoreKey) keyGov := sdk.NewKVStoreKey(types.StoreKey) keyStaking := sdk.NewKVStoreKey(staking.StoreKey) keySupply := sdk.NewKVStoreKey(supply.StoreKey) @@ -104,6 +110,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyBank, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyGov, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) @@ -141,10 +148,10 @@ func createTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context pk := params.NewKeeper(cdc, keyParams, tkeyParams) accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, pk.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) - bankKeeper := bank.NewBaseKeeper(accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs) + bankKeeper := bank.NewBaseKeeper(cdc, keyBank, accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs) supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bankKeeper, maccPerms) - sk := staking.NewKeeper(cdc, keyStaking, supplyKeeper, pk.Subspace(staking.DefaultParamspace)) + sk := staking.NewKeeper(cdc, keyStaking, bankKeeper, supplyKeeper, pk.Subspace(staking.DefaultParamspace)) sk.SetParams(ctx, staking.DefaultParams()) rtr := types.NewRouter(). @@ -163,9 +170,9 @@ func createTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context totalSupply := sdk.NewCoins(sdk.NewCoin(sk.BondDenom(ctx), initTokens.MulRaw(int64(len(TestAddrs))))) supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply)) - for _, addr := range TestAddrs { - _, err := bankKeeper.AddCoins(ctx, addr, initCoins) - require.Nil(t, err) + for i, addr := range TestAddrs { + accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, pubkeys[i], uint64(i), 0)) + require.NoError(t, bankKeeper.SetBalances(ctx, addr, initCoins)) } keeper.supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc) @@ -173,7 +180,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context keeper.supplyKeeper.SetModuleAccount(ctx, bondPool) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) - return ctx, accountKeeper, keeper, sk, supplyKeeper + return ctx, accountKeeper, bankKeeper, keeper, sk, supplyKeeper } // ProposalEqual checks if two proposals are equal (note: slow, for tests only) diff --git a/x/gov/keeper/vote_test.go b/x/gov/keeper/vote_test.go index 36c02d091374..ae25c7fcc230 100644 --- a/x/gov/keeper/vote_test.go +++ b/x/gov/keeper/vote_test.go @@ -9,7 +9,7 @@ import ( ) func TestVotes(t *testing.T) { - ctx, _, keeper, _, _ := createTestInput(t, false, 100) + ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled tp := TestProposal proposal, err := keeper.SubmitProposal(ctx, tp) diff --git a/x/gov/simulation/operations.go b/x/gov/simulation/operations.go index 3ebc2617cfd0..91659bd423a1 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -128,9 +128,7 @@ func SimulateSubmitProposal( msg := types.NewMsgSubmitProposal(content, deposit, simAccount.Address) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) var fees sdk.Coins coins, hasNeg := spendable.SafeSub(deposit) @@ -213,9 +211,7 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke msg := types.NewMsgDeposit(simAccount.Address, proposalID, deposit) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) var fees sdk.Coins coins, hasNeg := spendable.SafeSub(deposit) @@ -275,13 +271,10 @@ func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k kee } option := randomVotingOption(r) - msg := types.NewMsgVote(simAccount.Address, proposalID, option) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -315,9 +308,7 @@ func randomDeposit(r *rand.Rand, ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, addr sdk.AccAddress, ) (deposit sdk.Coins, skip bool, err error) { account := ak.GetAccount(ctx, addr) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) if spendable.Empty() { return nil, true, nil // skip diff --git a/x/gov/test_common.go b/x/gov/test_common.go index 7ecd4bd50f68..c6fe5d7fcbba 100644 --- a/x/gov/test_common.go +++ b/x/gov/test_common.go @@ -19,6 +19,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/bank" + bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" keep "github.com/cosmos/cosmos-sdk/x/gov/keeper" "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/cosmos/cosmos-sdk/x/mock" @@ -38,6 +39,7 @@ type testInput struct { mApp *mock.App keeper keep.Keeper router types.Router + bk bank.Keeper sk staking.Keeper addrs []sdk.AccAddress pubKeys []crypto.PubKey @@ -68,21 +70,18 @@ func getMockApp( blacklistedAddrs[notBondedPool.GetAddress().String()] = true blacklistedAddrs[bondPool.GetAddress().String()] = true - pk := mApp.ParamsKeeper - - rtr := types.NewRouter(). - AddRoute(types.RouterKey, handler) - - bk := bank.NewBaseKeeper(mApp.AccountKeeper, mApp.ParamsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) - + rtr := types.NewRouter().AddRoute(types.RouterKey, handler) maccPerms := map[string][]string{ types.ModuleName: {supply.Burner}, staking.NotBondedPoolName: {supply.Burner, supply.Staking}, staking.BondedPoolName: {supply.Burner, supply.Staking}, } + + pk := mApp.ParamsKeeper + bk := mApp.BankKeeper supplyKeeper := supply.NewKeeper(mApp.Cdc, keySupply, mApp.AccountKeeper, bk, maccPerms) sk := staking.NewKeeper( - mApp.Cdc, keyStaking, supplyKeeper, pk.Subspace(staking.DefaultParamspace), + mApp.Cdc, keyStaking, bk, supplyKeeper, pk.Subspace(staking.DefaultParamspace), ) keeper := keep.NewKeeper( @@ -93,24 +92,25 @@ func getMockApp( mApp.QueryRouter().AddRoute(types.QuerierRoute, keep.NewQuerier(keeper)) mApp.SetEndBlocker(getEndBlocker(keeper)) - mApp.SetInitChainer(getInitChainer(mApp, keeper, sk, supplyKeeper, genAccs, genState, + mApp.SetInitChainer(getInitChainer(mApp, bk, keeper, sk, supplyKeeper, genAccs, genState, []supplyexported.ModuleAccountI{govAcc, notBondedPool, bondPool})) require.NoError(t, mApp.CompleteSetup(keyStaking, keyGov, keySupply)) var ( - addrs []sdk.AccAddress - pubKeys []crypto.PubKey - privKeys []crypto.PrivKey + genBalances []bankexported.GenesisBalance + addrs []sdk.AccAddress + pubKeys []crypto.PubKey + privKeys []crypto.PrivKey ) if genAccs == nil || len(genAccs) == 0 { - genAccs, addrs, pubKeys, privKeys = mock.CreateGenAccounts(numGenAccs, valCoins) + genAccs, genBalances, addrs, pubKeys, privKeys = mock.CreateGenAccounts(numGenAccs, valCoins) } - mock.SetGenesis(mApp, genAccs) + mock.SetGenesis(mApp, genAccs, genBalances) - return testInput{mApp, keeper, rtr, sk, addrs, pubKeys, privKeys} + return testInput{mApp, keeper, rtr, bk, sk, addrs, pubKeys, privKeys} } // gov and staking endblocker @@ -122,7 +122,7 @@ func getEndBlocker(keeper Keeper) sdk.EndBlocker { } // gov and staking initchainer -func getInitChainer(mapp *mock.App, keeper Keeper, stakingKeeper staking.Keeper, supplyKeeper supply.Keeper, accs []authexported.Account, genState GenesisState, +func getInitChainer(mapp *mock.App, bk types.BankKeeper, keeper Keeper, stakingKeeper staking.Keeper, supplyKeeper supply.Keeper, accs []authexported.Account, genState GenesisState, blacklistedAddrs []supplyexported.ModuleAccountI) sdk.InitChainer { return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { mapp.InitChainer(ctx, req) @@ -137,11 +137,11 @@ func getInitChainer(mapp *mock.App, keeper Keeper, stakingKeeper staking.Keeper, supplyKeeper.SetModuleAccount(ctx, macc) } - validators := staking.InitGenesis(ctx, stakingKeeper, mapp.AccountKeeper, supplyKeeper, stakingGenesis) + validators := staking.InitGenesis(ctx, stakingKeeper, mapp.AccountKeeper, bk, supplyKeeper, stakingGenesis) if genState.IsEmpty() { - InitGenesis(ctx, keeper, supplyKeeper, types.DefaultGenesisState()) + InitGenesis(ctx, bk, supplyKeeper, keeper, types.DefaultGenesisState()) } else { - InitGenesis(ctx, keeper, supplyKeeper, genState) + InitGenesis(ctx, bk, supplyKeeper, keeper, genState) } return abci.ResponseInitChain{ Validators: validators, diff --git a/x/gov/types/expected_keepers.go b/x/gov/types/expected_keepers.go index f2b699dadbd9..e2b7572016e2 100644 --- a/x/gov/types/expected_keepers.go +++ b/x/gov/types/expected_keepers.go @@ -51,4 +51,5 @@ type BankKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SetBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } diff --git a/x/simulation/account_test.go b/x/simulation/account_test.go index adb28dcf984f..b0f6494fac30 100644 --- a/x/simulation/account_test.go +++ b/x/simulation/account_test.go @@ -65,6 +65,8 @@ func TestRandomFees(t *testing.T) { {"0 coins", sdk.NewCoins(sdk.NewInt64Coin("aaa", 10), sdk.NewInt64Coin("bbb", 5)), false, false}, } for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { got, err := simulation.RandomFees(r, sdk.Context{}, tt.spendableCoins) if (err != nil) != tt.wantErr { diff --git a/x/simulation/rand_util_test.go b/x/simulation/rand_util_test.go index 2128de4050aa..9d46164e59f6 100644 --- a/x/simulation/rand_util_test.go +++ b/x/simulation/rand_util_test.go @@ -46,6 +46,8 @@ func TestRandStringOfLength(t *testing.T) { {"10-size", 1_000_000_000, 1_000_000_000}, } for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { got := simulation.RandStringOfLength(r, tt.n) require.Equal(t, tt.want, len(got)) diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index bcd1ee7ce8a2..a2138479f2c6 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -14,7 +14,7 @@ import ( ) func TestBeginBlocker(t *testing.T) { - ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) + ctx, bk, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) power := int64(100) amt := sdk.TokensFromConsensusPower(power) addr, pk := slashingkeeper.Addrs[2], slashingkeeper.Pks[2] @@ -26,7 +26,7 @@ func TestBeginBlocker(t *testing.T) { staking.EndBlocker(ctx, sk) require.Equal( - t, ck.GetCoins(ctx, sdk.AccAddress(addr)), + t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)), sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))), ) require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens()) diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 23dd43e590bf..88ae3bb0b75a 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/bank" + bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" "github.com/cosmos/cosmos-sdk/x/mock" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -48,21 +49,24 @@ func getMockApp(t *testing.T) (*mock.App, staking.Keeper, Keeper) { blacklistedAddrs[notBondedPool.GetAddress().String()] = true blacklistedAddrs[bondPool.GetAddress().String()] = true - bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper, mapp.ParamsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) maccPerms := map[string][]string{ auth.FeeCollectorName: nil, staking.NotBondedPoolName: {supply.Burner, supply.Staking}, staking.BondedPoolName: {supply.Burner, supply.Staking}, } - supplyKeeper := supply.NewKeeper(mapp.Cdc, keySupply, mapp.AccountKeeper, bankKeeper, maccPerms) - stakingKeeper := staking.NewKeeper(mapp.Cdc, keyStaking, supplyKeeper, mapp.ParamsKeeper.Subspace(staking.DefaultParamspace)) + supplyKeeper := supply.NewKeeper(mapp.Cdc, keySupply, mapp.AccountKeeper, mapp.BankKeeper, maccPerms) + stakingKeeper := staking.NewKeeper(mapp.Cdc, keyStaking, mapp.BankKeeper, supplyKeeper, mapp.ParamsKeeper.Subspace(staking.DefaultParamspace)) keeper := NewKeeper(mapp.Cdc, keySlashing, stakingKeeper, mapp.ParamsKeeper.Subspace(DefaultParamspace)) mapp.Router().AddRoute(staking.RouterKey, staking.NewHandler(stakingKeeper)) mapp.Router().AddRoute(RouterKey, NewHandler(keeper)) mapp.SetEndBlocker(getEndBlocker(stakingKeeper)) - mapp.SetInitChainer(getInitChainer(mapp, stakingKeeper, mapp.AccountKeeper, supplyKeeper, - []supplyexported.ModuleAccountI{feeCollector, notBondedPool, bondPool})) + mapp.SetInitChainer( + getInitChainer( + mapp, stakingKeeper, mapp.AccountKeeper, mapp.BankKeeper, supplyKeeper, + []supplyexported.ModuleAccountI{feeCollector, notBondedPool, bondPool}, + ), + ) require.NoError(t, mapp.CompleteSetup(keyStaking, keySupply, keySlashing)) @@ -80,8 +84,11 @@ func getEndBlocker(keeper staking.Keeper) sdk.EndBlocker { } // overwrite the mock init chainer -func getInitChainer(mapp *mock.App, keeper staking.Keeper, accountKeeper types.AccountKeeper, supplyKeeper types.SupplyKeeper, - blacklistedAddrs []supplyexported.ModuleAccountI) sdk.InitChainer { +func getInitChainer( + mapp *mock.App, keeper staking.Keeper, accountKeeper types.AccountKeeper, bk types.BankKeeper, + supplyKeeper types.SupplyKeeper, blacklistedAddrs []supplyexported.ModuleAccountI, +) sdk.InitChainer { + return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { // set module accounts for _, macc := range blacklistedAddrs { @@ -90,7 +97,7 @@ func getInitChainer(mapp *mock.App, keeper staking.Keeper, accountKeeper types.A mapp.InitChainer(ctx, req) stakingGenesis := staking.DefaultGenesisState() - validators := staking.InitGenesis(ctx, keeper, accountKeeper, supplyKeeper, stakingGenesis) + validators := staking.InitGenesis(ctx, keeper, accountKeeper, bk, supplyKeeper, stakingGenesis) return abci.ResponseInitChain{ Validators: validators, } @@ -123,10 +130,15 @@ func TestSlashingMsgs(t *testing.T) { acc1 := &auth.BaseAccount{ Address: addr1, - Coins: sdk.Coins{genCoin}, } accs := []authexported.Account{acc1} - mock.SetGenesis(mapp, accs) + balances := []bankexported.GenesisBalance{ + bank.Balance{ + Address: addr1, + Coins: sdk.Coins{genCoin}, + }, + } + mock.SetGenesis(mapp, accs, balances) description := staking.NewDescription("foo_moniker", "", "", "", "") commission := staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index a137ff8d0d08..b3d181c806e9 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -17,7 +17,7 @@ import ( func TestCannotUnjailUnlessJailed(t *testing.T) { // initial setup - ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) + ctx, bk, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) slh := NewHandler(keeper) amt := sdk.TokensFromConsensusPower(100) addr, val := slashingkeeper.Addrs[0], slashingkeeper.Pks[0] @@ -30,7 +30,7 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { staking.EndBlocker(ctx, sk) require.Equal( - t, ck.GetCoins(ctx, sdk.AccAddress(addr)), + t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)), sdk.Coins{sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))}, ) require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens()) @@ -44,7 +44,7 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { // initial setup - ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) + ctx, bk, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) slh := NewHandler(keeper) amtInt := int64(100) addr, val, amt := slashingkeeper.Addrs[0], slashingkeeper.Pks[0], sdk.TokensFromConsensusPower(amtInt) @@ -58,7 +58,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { staking.EndBlocker(ctx, sk) require.Equal( - t, ck.GetCoins(ctx, sdk.AccAddress(addr)), + t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)), sdk.Coins{sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))}, ) @@ -153,9 +153,8 @@ func TestInvalidMsg(t *testing.T) { // Test a validator through uptime, downtime, revocation, // unrevocation, starting height reset, and revocation again func TestHandleAbsentValidator(t *testing.T) { - // initial setup - ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, slashingkeeper.TestParams()) + ctx, bk, sk, _, keeper := slashingkeeper.CreateTestInput(t, slashingkeeper.TestParams()) power := int64(100) amt := sdk.TokensFromConsensusPower(power) addr, val := slashingkeeper.Addrs[0], slashingkeeper.Pks[0] @@ -169,7 +168,7 @@ func TestHandleAbsentValidator(t *testing.T) { staking.EndBlocker(ctx, sk) require.Equal( - t, ck.GetCoins(ctx, sdk.AccAddress(addr)), + t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)), sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))), ) require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens()) @@ -206,8 +205,9 @@ func TestHandleAbsentValidator(t *testing.T) { // validator should be bonded still validator, _ := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val)) require.Equal(t, sdk.Bonded, validator.GetStatus()) + bondPool := sk.GetBondedPool(ctx) - require.True(sdk.IntEq(t, amt, bondPool.GetCoins().AmountOf(sk.BondDenom(ctx)))) + require.True(sdk.IntEq(t, amt, bk.GetBalance(ctx, bondPool.GetAddress(), sk.BondDenom(ctx)).Amount)) // 501st block missed ctx = ctx.WithBlockHeight(height) @@ -265,8 +265,7 @@ func TestHandleAbsentValidator(t *testing.T) { require.Equal(t, sdk.Bonded, validator.GetStatus()) // validator should have been slashed - bondPool = sk.GetBondedPool(ctx) - require.Equal(t, amt.Int64()-slashAmt, bondPool.GetCoins().AmountOf(sk.BondDenom(ctx)).Int64()) + require.Equal(t, amt.Int64()-slashAmt, bk.GetBalance(ctx, bondPool.GetAddress(), sk.BondDenom(ctx)).Amount.Int64()) // Validator start height should not have been changed info, found = keeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(val.Address())) diff --git a/x/slashing/internal/keeper/keeper_test.go b/x/slashing/internal/keeper/keeper_test.go index 52eb6b59b77a..fdf50a261e27 100644 --- a/x/slashing/internal/keeper/keeper_test.go +++ b/x/slashing/internal/keeper/keeper_test.go @@ -16,7 +16,7 @@ import ( // and that they are not immediately jailed func TestHandleNewValidator(t *testing.T) { // initial setup - ctx, ck, sk, _, keeper := CreateTestInput(t, TestParams()) + ctx, bk, sk, _, keeper := CreateTestInput(t, TestParams()) addr, val := Addrs[0], Pks[0] amt := sdk.TokensFromConsensusPower(100) sh := staking.NewHandler(sk) @@ -32,7 +32,7 @@ func TestHandleNewValidator(t *testing.T) { staking.EndBlocker(ctx, sk) require.Equal( - t, ck.GetCoins(ctx, sdk.AccAddress(addr)), + t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)), sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, InitTokens.Sub(amt))), ) require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens()) @@ -54,7 +54,7 @@ func TestHandleNewValidator(t *testing.T) { require.Equal(t, sdk.Bonded, validator.GetStatus()) bondPool := sk.GetBondedPool(ctx) expTokens := sdk.TokensFromConsensusPower(100) - require.Equal(t, expTokens.Int64(), bondPool.GetCoins().AmountOf(sk.BondDenom(ctx)).Int64()) + require.Equal(t, expTokens.Int64(), bk.GetBalance(ctx, bondPool.GetAddress(), sk.BondDenom(ctx)).Amount.Int64()) } // Test a jailed validator being "down" twice diff --git a/x/slashing/internal/keeper/test_common.go b/x/slashing/internal/keeper/test_common.go index eb8870c1d199..79a436b2e1df 100644 --- a/x/slashing/internal/keeper/test_common.go +++ b/x/slashing/internal/keeper/test_common.go @@ -57,6 +57,7 @@ func createTestCodec() *codec.Codec { func CreateTestInput(t *testing.T, defaults types.Params) (sdk.Context, bank.Keeper, staking.Keeper, params.Subspace, Keeper) { keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyBank := sdk.NewKVStoreKey(bank.StoreKey) keyStaking := sdk.NewKVStoreKey(staking.StoreKey) keySlashing := sdk.NewKVStoreKey(types.StoreKey) keySupply := sdk.NewKVStoreKey(supply.StoreKey) @@ -67,6 +68,7 @@ func CreateTestInput(t *testing.T, defaults types.Params) (sdk.Context, bank.Kee ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyBank, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keySlashing, sdk.StoreTypeIAVL, db) @@ -91,7 +93,7 @@ func CreateTestInput(t *testing.T, defaults types.Params) (sdk.Context, bank.Kee paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams) accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) - bk := bank.NewBaseKeeper(accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) + bk := bank.NewBaseKeeper(cdc, keyBank, accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) maccPerms := map[string][]string{ auth.FeeCollectorName: nil, staking.NotBondedPoolName: {supply.Burner, supply.Staking}, @@ -102,7 +104,7 @@ func CreateTestInput(t *testing.T, defaults types.Params) (sdk.Context, bank.Kee totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, InitTokens.MulRaw(int64(len(Addrs))))) supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply)) - sk := staking.NewKeeper(cdc, keyStaking, supplyKeeper, paramsKeeper.Subspace(staking.DefaultParamspace)) + sk := staking.NewKeeper(cdc, keyStaking, bk, supplyKeeper, paramsKeeper.Subspace(staking.DefaultParamspace)) genesis := staking.DefaultGenesisState() // set module accounts @@ -110,12 +112,14 @@ func CreateTestInput(t *testing.T, defaults types.Params) (sdk.Context, bank.Kee supplyKeeper.SetModuleAccount(ctx, bondPool) supplyKeeper.SetModuleAccount(ctx, notBondedPool) - _ = staking.InitGenesis(ctx, sk, accountKeeper, supplyKeeper, genesis) + _ = staking.InitGenesis(ctx, sk, accountKeeper, bk, supplyKeeper, genesis) - for _, addr := range Addrs { - _, err = bk.AddCoins(ctx, sdk.AccAddress(addr), initCoins) + for i, addr := range Addrs { + addr := sdk.AccAddress(addr) + accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, Pks[i], uint64(i), 0)) + require.NoError(t, bk.SetBalances(ctx, addr, initCoins)) } - require.Nil(t, err) + paramstore := paramsKeeper.Subspace(types.DefaultParamspace) keeper := NewKeeper(cdc, keySlashing, &sk, paramstore) diff --git a/x/slashing/internal/types/expected_keepers.go b/x/slashing/internal/types/expected_keepers.go index e59c7d2877c2..bf09731a03e6 100644 --- a/x/slashing/internal/types/expected_keepers.go +++ b/x/slashing/internal/types/expected_keepers.go @@ -21,6 +21,7 @@ type BankKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SetBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } // ParamSubspace defines the expected Subspace interfacace diff --git a/x/slashing/simulation/operations.go b/x/slashing/simulation/operations.go index ccd06ef5625a..c7e1b0bfb6c4 100644 --- a/x/slashing/simulation/operations.go +++ b/x/slashing/simulation/operations.go @@ -42,7 +42,7 @@ func WeightedOperations( } // SimulateMsgUnjail generates a MsgUnjail with random values -// nolint: funlen +// nolint: funlen interfacer func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, sk stakingkeeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, @@ -76,9 +76,7 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee } account := ak.GetAccount(ctx, sdk.AccAddress(validator.GetOperator())) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { diff --git a/x/staking/app_test.go b/x/staking/app_test.go index a56923113380..586b5d95d757 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -10,6 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/bank" + bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" "github.com/cosmos/cosmos-sdk/x/mock" "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/cosmos-sdk/x/supply" @@ -35,19 +36,22 @@ func getMockApp(t *testing.T) (*mock.App, Keeper) { blacklistedAddrs[notBondedPool.GetAddress().String()] = true blacklistedAddrs[bondPool.GetAddress().String()] = true - bankKeeper := bank.NewBaseKeeper(mApp.AccountKeeper, mApp.ParamsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) maccPerms := map[string][]string{ auth.FeeCollectorName: nil, types.NotBondedPoolName: {supply.Burner, supply.Staking}, types.BondedPoolName: {supply.Burner, supply.Staking}, } - supplyKeeper := supply.NewKeeper(mApp.Cdc, keySupply, mApp.AccountKeeper, bankKeeper, maccPerms) - keeper := NewKeeper(mApp.Cdc, keyStaking, supplyKeeper, mApp.ParamsKeeper.Subspace(DefaultParamspace)) + supplyKeeper := supply.NewKeeper(mApp.Cdc, keySupply, mApp.AccountKeeper, mApp.BankKeeper, maccPerms) + keeper := NewKeeper(mApp.Cdc, keyStaking, mApp.BankKeeper, supplyKeeper, mApp.ParamsKeeper.Subspace(DefaultParamspace)) mApp.Router().AddRoute(RouterKey, NewHandler(keeper)) mApp.SetEndBlocker(getEndBlocker(keeper)) - mApp.SetInitChainer(getInitChainer(mApp, keeper, mApp.AccountKeeper, supplyKeeper, - []supplyexported.ModuleAccountI{feeCollector, notBondedPool, bondPool})) + mApp.SetInitChainer( + getInitChainer( + mApp, keeper, mApp.AccountKeeper, mApp.BankKeeper, supplyKeeper, + []supplyexported.ModuleAccountI{feeCollector, notBondedPool, bondPool}, + ), + ) require.NoError(t, mApp.CompleteSetup(keyStaking, keySupply)) return mApp, keeper @@ -66,18 +70,22 @@ func getEndBlocker(keeper Keeper) sdk.EndBlocker { // getInitChainer initializes the chainer of the mock app and sets the genesis // state. It returns an empty ResponseInitChain. -func getInitChainer(mapp *mock.App, keeper Keeper, accountKeeper types.AccountKeeper, supplyKeeper types.SupplyKeeper, - blacklistedAddrs []supplyexported.ModuleAccountI) sdk.InitChainer { +func getInitChainer( + mapp *mock.App, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper, + sk types.SupplyKeeper, blacklistedAddrs []supplyexported.ModuleAccountI, +) sdk.InitChainer { + return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { mapp.InitChainer(ctx, req) // set module accounts for _, macc := range blacklistedAddrs { - supplyKeeper.SetModuleAccount(ctx, macc) + sk.SetModuleAccount(ctx, macc) } stakingGenesis := DefaultGenesisState() - validators := InitGenesis(ctx, keeper, accountKeeper, supplyKeeper, stakingGenesis) + validators := InitGenesis(ctx, keeper, ak, bk, sk, stakingGenesis) + return abci.ResponseInitChain{ Validators: validators, } @@ -121,17 +129,21 @@ func TestStakingMsgs(t *testing.T) { genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) - acc1 := &auth.BaseAccount{ - Address: addr1, - Coins: sdk.Coins{genCoin}, - } - acc2 := &auth.BaseAccount{ - Address: addr2, - Coins: sdk.Coins{genCoin}, - } + acc1 := &auth.BaseAccount{Address: addr1} + acc2 := &auth.BaseAccount{Address: addr2} accs := []authexported.Account{acc1, acc2} + balances := []bankexported.GenesisBalance{ + bank.Balance{ + Address: addr1, + Coins: sdk.Coins{genCoin}, + }, + bank.Balance{ + Address: addr2, + Coins: sdk.Coins{genCoin}, + }, + } - mock.SetGenesis(mApp, accs) + mock.SetGenesis(mApp, accs, balances) mock.CheckBalance(t, mApp, addr1, sdk.Coins{genCoin}) mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin}) diff --git a/x/staking/genesis_test.go b/x/staking/genesis_test.go index 4a56b3ac9c9c..4f3a7f1d05bf 100644 --- a/x/staking/genesis_test.go +++ b/x/staking/genesis_test.go @@ -17,7 +17,7 @@ import ( ) func TestInitGenesis(t *testing.T) { - ctx, accKeeper, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000) + ctx, accKeeper, bk, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000) valTokens := sdk.TokensFromConsensusPower(1) @@ -40,7 +40,7 @@ func TestInitGenesis(t *testing.T) { validators[1].DelegatorShares = valTokens.ToDec() genesisState := types.NewGenesisState(params, validators, delegations) - vals := InitGenesis(ctx, keeper, accKeeper, supplyKeeper, genesisState) + vals := InitGenesis(ctx, keeper, accKeeper, bk, supplyKeeper, genesisState) actualGenesis := ExportGenesis(ctx, keeper) require.Equal(t, genesisState.Params, actualGenesis.Params) @@ -68,7 +68,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) { size := 200 require.True(t, size > 100) - ctx, accKeeper, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000) + ctx, accKeeper, bk, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000) params := keeper.GetParams(ctx) delegations := []Delegation{} @@ -89,7 +89,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) { } genesisState := types.NewGenesisState(params, validators, delegations) - vals := InitGenesis(ctx, keeper, accKeeper, supplyKeeper, genesisState) + vals := InitGenesis(ctx, keeper, accKeeper, bk, supplyKeeper, genesisState) abcivals := make([]abci.ValidatorUpdate, 100) for i, val := range validators[:100] { diff --git a/x/staking/handler_test.go b/x/staking/handler_test.go index 92147eabb097..75e9242e07c7 100644 --- a/x/staking/handler_test.go +++ b/x/staking/handler_test.go @@ -22,7 +22,7 @@ func TestValidatorByPowerIndex(t *testing.T) { initPower := int64(1000000) initBond := sdk.TokensFromConsensusPower(initPower) - ctx, _, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, initPower) // create validator msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond) @@ -104,7 +104,7 @@ func TestValidatorByPowerIndex(t *testing.T) { } func TestDuplicatesMsgCreateValidator(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) addr1, addr2 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]) pk1, pk2 := keep.PKs[0], keep.PKs[1] @@ -160,7 +160,7 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) { } func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) addr := sdk.ValAddress(keep.Addrs[0]) invalidPk := secp256k1.GenPrivKey().PubKey() @@ -181,7 +181,7 @@ func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) { } func TestLegacyValidatorDelegations(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, int64(1000)) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, int64(1000)) bondAmount := sdk.TokensFromConsensusPower(10) valAddr := sdk.ValAddress(keep.Addrs[0]) @@ -279,7 +279,7 @@ func TestLegacyValidatorDelegations(t *testing.T) { func TestIncrementsMsgDelegate(t *testing.T) { initPower := int64(1000) initBond := sdk.TokensFromConsensusPower(initPower) - ctx, accMapper, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, bk, keeper, _ := keep.CreateTestInput(t, false, initPower) params := keeper.GetParams(ctx) bondAmount := sdk.TokensFromConsensusPower(10) @@ -332,7 +332,7 @@ func TestIncrementsMsgDelegate(t *testing.T) { gotBond := bond.Shares.RoundInt() gotDelegatorShares := validator.DelegatorShares.RoundInt() - gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom) + gotDelegatorAcc := bk.GetBalance(ctx, delegatorAddr, params.BondDenom).Amount require.Equal(t, expBond, gotBond, "i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n", @@ -351,7 +351,7 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) { initPower := int64(100) initBond := sdk.TokensFromConsensusPower(100) - ctx, _, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, initPower) // create validator msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond) @@ -384,7 +384,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { initPower := int64(100) initBond := sdk.TokensFromConsensusPower(100) - ctx, _, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, initPower) // create validator msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond) @@ -415,7 +415,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { func TestIncrementsMsgUnbond(t *testing.T) { initPower := int64(1000) initBond := sdk.TokensFromConsensusPower(initPower) - ctx, accMapper, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, bk, keeper, _ := keep.CreateTestInput(t, false, initPower) params := keeper.GetParams(ctx) denom := params.BondDenom @@ -429,7 +429,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { require.NotNil(t, res) // initial balance - amt1 := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(denom) + amt1 := bk.GetBalance(ctx, delegatorAddr, denom).Amount msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, initBond) res, err = handleMsgDelegate(ctx, msgDelegate, keeper) @@ -437,7 +437,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { require.NotNil(t, res) // balance should have been subtracted after delegation - amt2 := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(denom) + amt2 := bk.GetBalance(ctx, delegatorAddr, denom).Amount require.True(sdk.IntEq(t, amt1.Sub(initBond), amt2)) // apply TM updates @@ -477,7 +477,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { gotBond := bond.Shares.RoundInt() gotDelegatorShares := validator.DelegatorShares.RoundInt() - gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom) + gotDelegatorAcc := bk.GetBalance(ctx, delegatorAddr, params.BondDenom).Amount require.Equal(t, expBond.Int64(), gotBond.Int64(), "i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n", @@ -520,7 +520,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { func TestMultipleMsgCreateValidator(t *testing.T) { initPower := int64(1000) initTokens := sdk.TokensFromConsensusPower(initPower) - ctx, accMapper, keeper, _ := keep.CreateTestInput(t, false, initPower) + ctx, _, bk, keeper, _ := keep.CreateTestInput(t, false, initPower) params := keeper.GetParams(ctx) blockTime := time.Now().UTC() @@ -552,7 +552,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) { val := validators[i] balanceExpd := initTokens.Sub(valTokens) - balanceGot := accMapper.GetAccount(ctx, delegatorAddrs[i]).GetCoins().AmountOf(params.BondDenom) + balanceGot := bk.GetBalance(ctx, delegatorAddrs[i], params.BondDenom).Amount require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators) require.Equal(t, valTokens, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", 10, val.DelegatorShares) @@ -589,13 +589,13 @@ func TestMultipleMsgCreateValidator(t *testing.T) { _, found = keeper.GetValidator(ctx, validatorAddr) require.False(t, found) - gotBalance := accMapper.GetAccount(ctx, delegatorAddrs[i]).GetCoins().AmountOf(params.BondDenom) + gotBalance := bk.GetBalance(ctx, delegatorAddrs[i], params.BondDenom).Amount require.Equal(t, initTokens, gotBalance, "expected account to have %d, got %d", initTokens, gotBalance) } } func TestMultipleMsgDelegate(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr, delegatorAddrs := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1:] // first make a validator @@ -639,7 +639,7 @@ func TestMultipleMsgDelegate(t *testing.T) { } func TestJailValidator(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1] // create the validator @@ -689,7 +689,7 @@ func TestJailValidator(t *testing.T) { } func TestValidatorQueue(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1] // set the unbonding time @@ -750,7 +750,7 @@ func TestValidatorQueue(t *testing.T) { } func TestUnbondingPeriod(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr := sdk.ValAddress(keep.Addrs[0]) // set the unbonding time @@ -798,7 +798,7 @@ func TestUnbondingPeriod(t *testing.T) { } func TestUnbondingFromUnbondingValidator(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1] // create the validator @@ -843,7 +843,7 @@ func TestUnbondingFromUnbondingValidator(t *testing.T) { } func TestRedelegationPeriod(t *testing.T) { - ctx, AccMapper, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, bk, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr, validatorAddr2 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]) denom := keeper.GetParams(ctx).BondDenom @@ -856,14 +856,14 @@ func TestRedelegationPeriod(t *testing.T) { msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10)) // initial balance - amt1 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins().AmountOf(denom) + amt1 := bk.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount res, err := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.NoError(t, err) require.NotNil(t, res) // balance should have been subtracted after creation - amt2 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins().AmountOf(denom) + amt2 := bk.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount require.Equal(t, amt1.Sub(sdk.NewInt(10)).Int64(), amt2.Int64(), "expected coins to be subtracted") msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, keep.PKs[1], sdk.NewInt(10)) @@ -871,7 +871,7 @@ func TestRedelegationPeriod(t *testing.T) { require.NoError(t, err) require.NotNil(t, res) - bal1 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins() + bal1 := bk.GetAllBalances(ctx, sdk.AccAddress(validatorAddr)) // begin redelegate redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) @@ -881,7 +881,7 @@ func TestRedelegationPeriod(t *testing.T) { require.NotNil(t, res) // origin account should not lose tokens as with a regular delegation - bal2 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins() + bal2 := bk.GetAllBalances(ctx, sdk.AccAddress(validatorAddr)) require.Equal(t, bal1, bal2) origHeader := ctx.BlockHeader() @@ -905,7 +905,7 @@ func TestRedelegationPeriod(t *testing.T) { } func TestTransitiveRedelegation(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr := sdk.ValAddress(keep.Addrs[0]) validatorAddr2 := sdk.ValAddress(keep.Addrs[1]) validatorAddr3 := sdk.ValAddress(keep.Addrs[2]) @@ -955,7 +955,7 @@ func TestTransitiveRedelegation(t *testing.T) { } func TestMultipleRedelegationAtSameTime(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valAddr := sdk.ValAddress(keep.Addrs[0]) valAddr2 := sdk.ValAddress(keep.Addrs[1]) @@ -1011,7 +1011,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { } func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valAddr := sdk.ValAddress(keep.Addrs[0]) valAddr2 := sdk.ValAddress(keep.Addrs[1]) @@ -1069,7 +1069,7 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { } func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valAddr := sdk.ValAddress(keep.Addrs[0]) // set the unbonding time @@ -1119,7 +1119,7 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { } func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valAddr := sdk.ValAddress(keep.Addrs[0]) // set the unbonding time @@ -1176,7 +1176,7 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { } func TestUnbondingWhenExcessValidators(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) validatorAddr1 := sdk.ValAddress(keep.Addrs[0]) validatorAddr2 := sdk.ValAddress(keep.Addrs[1]) validatorAddr3 := sdk.ValAddress(keep.Addrs[2]) @@ -1238,7 +1238,7 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { } func TestBondUnbondRedelegateSlashTwice(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valA, valB, del := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]), keep.Addrs[2] consAddr0 := sdk.ConsAddress(keep.PKs[0].Address()) @@ -1353,7 +1353,7 @@ func TestInvalidMsg(t *testing.T) { } func TestInvalidCoinDenom(t *testing.T) { - ctx, _, keeper, _ := keep.CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := keep.CreateTestInput(t, false, 1000) valA, valB, delAddr := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]), keep.Addrs[2] valTokens := sdk.TokensFromConsensusPower(100) diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index e3e619697679..f2832dd498d2 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -13,7 +13,7 @@ import ( // tests GetDelegation, GetDelegatorDelegations, SetDelegation, RemoveDelegation, GetDelegatorDelegations func TestDelegation(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 10) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10) //construct the validators amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)} @@ -128,7 +128,7 @@ func TestDelegation(t *testing.T) { // tests Get/Set/Remove UnbondingDelegation func TestUnbondingDelegation(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 0) ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 0, time.Unix(0, 0), sdk.NewInt(5)) @@ -167,13 +167,12 @@ func TestUnbondingDelegation(t *testing.T) { } func TestUnbondDelegation(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) startTokens := sdk.TokensFromConsensusPower(10) - notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens))) - require.NoError(t, err) + + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens)))) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) // create a validator and a delegator to that validator @@ -204,12 +203,13 @@ func TestUnbondDelegation(t *testing.T) { } func TestUnbondingDelegationsMaxEntries(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1) startTokens := sdk.TokensFromConsensusPower(10) - bondDenom := keeper.BondDenom(ctx) + bondDenom := keeper.BondDenom(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(bondDenom, startTokens))) + + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(bondDenom, startTokens))) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -228,8 +228,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) { maxEntries := keeper.MaxEntries(ctx) - oldBonded := keeper.GetBondedPool(ctx).GetCoins().AmountOf(bondDenom) - oldNotBonded := keeper.GetNotBondedPool(ctx).GetCoins().AmountOf(bondDenom) + oldBonded := bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + oldNotBonded := bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount // should all pass var completionTime time.Time @@ -239,51 +239,50 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) { require.NoError(t, err) } - bondedPool := keeper.GetBondedPool(ctx) - notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, bondedPool.GetCoins().AmountOf(bondDenom), oldBonded.SubRaw(int64(maxEntries)))) - require.True(sdk.IntEq(t, notBondedPool.GetCoins().AmountOf(bondDenom), oldNotBonded.AddRaw(int64(maxEntries)))) + newBonded := bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + newNotBonded := bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, newBonded, oldBonded.SubRaw(int64(maxEntries)))) + require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.AddRaw(int64(maxEntries)))) - oldBonded = bondedPool.GetCoins().AmountOf(bondDenom) - oldNotBonded = notBondedPool.GetCoins().AmountOf(bondDenom) + oldBonded = bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + oldNotBonded = bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount // an additional unbond should fail due to max entries _, err = keeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1)) require.Error(t, err) - bondedPool = keeper.GetBondedPool(ctx) - notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, bondedPool.GetCoins().AmountOf(bondDenom), oldBonded)) - require.True(sdk.IntEq(t, notBondedPool.GetCoins().AmountOf(bondDenom), oldNotBonded)) + newBonded = bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + newNotBonded = bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount + + require.True(sdk.IntEq(t, newBonded, oldBonded)) + require.True(sdk.IntEq(t, newNotBonded, oldNotBonded)) // mature unbonding delegations ctx = ctx.WithBlockTime(completionTime) err = keeper.CompleteUnbonding(ctx, addrDels[0], addrVals[0]) require.NoError(t, err) - bondedPool = keeper.GetBondedPool(ctx) - notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, bondedPool.GetCoins().AmountOf(bondDenom), oldBonded)) - require.True(sdk.IntEq(t, notBondedPool.GetCoins().AmountOf(bondDenom), oldNotBonded.SubRaw(int64(maxEntries)))) + newBonded = bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + newNotBonded = bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, newBonded, oldBonded)) + require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.SubRaw(int64(maxEntries)))) - oldNotBonded = notBondedPool.GetCoins().AmountOf(bondDenom) + oldNotBonded = bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount // unbonding should work again _, err = keeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1)) require.NoError(t, err) - bondedPool = keeper.GetBondedPool(ctx) - - notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, bondedPool.GetCoins().AmountOf(bondDenom), oldBonded.SubRaw(1))) - require.True(sdk.IntEq(t, notBondedPool.GetCoins().AmountOf(bondDenom), oldNotBonded.AddRaw(1))) + newBonded = bk.GetBalance(ctx, keeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount + newNotBonded = bk.GetBalance(ctx, keeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, newBonded, oldBonded.SubRaw(1))) + require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1))) } // test undelegating self delegation from a validator pushing it below MinSelfDelegation // shift it from the bonded to unbonding state and jailed func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { - - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) delTokens := sdk.TokensFromConsensusPower(10) delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) @@ -296,7 +295,8 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(delCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -308,7 +308,8 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { // add bonded tokens to pool for delegations bondedPool := keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -319,8 +320,8 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { require.Equal(t, delTokens, issuedShares.RoundInt()) // add bonded tokens to pool for delegations - bondedPool = keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded = bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -344,7 +345,7 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { } func TestUndelegateFromUnbondingValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) delTokens := sdk.TokensFromConsensusPower(10) delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) @@ -356,7 +357,8 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(delCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -367,7 +369,8 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { keeper.SetDelegation(ctx, selfDelegation) bondedPool := keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -377,8 +380,8 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { validator, issuedShares = validator.AddTokensFromDel(delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) - bondedPool = keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded = bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -386,8 +389,8 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares) keeper.SetDelegation(ctx, delegation) - bondedPool = keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded = bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -432,13 +435,14 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { } func TestUndelegateFromUnbondedValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1) delTokens := sdk.TokensFromConsensusPower(10) delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(delCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -456,7 +460,8 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { keeper.SetDelegation(ctx, selfDelegation) bondedPool := keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -511,13 +516,14 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { } func TestUnbondingAllDelegationFromValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) delTokens := sdk.TokensFromConsensusPower(10) delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(delCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -541,7 +547,8 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) { require.Equal(t, delTokens, issuedShares.RoundInt()) bondedPool := keeper.GetBondedPool(ctx) - err = bondedPool.SetCoins(bondedPool.GetCoins().Add(delCoins...)) + oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) + err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -582,7 +589,7 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) { // Make sure that that the retrieving the delegations doesn't affect the state func TestGetRedelegationsFromSrcValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 0) rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0, time.Unix(0, 0), sdk.NewInt(5), @@ -606,7 +613,7 @@ func TestGetRedelegationsFromSrcValidator(t *testing.T) { // tests Get/Set/Remove/Has UnbondingDelegation func TestRedelegation(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 0) rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0, time.Unix(0, 0), sdk.NewInt(5), @@ -666,13 +673,14 @@ func TestRedelegation(t *testing.T) { } func TestRedelegateToSameValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) valTokens := sdk.TokensFromConsensusPower(10) startCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), valTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(startCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(startCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -689,17 +697,17 @@ func TestRedelegateToSameValidator(t *testing.T) { _, err = keeper.BeginRedelegation(ctx, val0AccAddr, addrVals[0], addrVals[0], sdk.NewDec(5)) require.Error(t, err) - } func TestRedelegationMaxEntries(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) startTokens := sdk.TokensFromConsensusPower(20) startCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(startCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(startCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -746,13 +754,14 @@ func TestRedelegationMaxEntries(t *testing.T) { } func TestRedelegateSelfDelegation(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) startTokens := sdk.TokensFromConsensusPower(30) startCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(startCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(startCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -798,13 +807,14 @@ func TestRedelegateSelfDelegation(t *testing.T) { } func TestRedelegateFromUnbondingValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) startTokens := sdk.TokensFromConsensusPower(30) startCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(startCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(startCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -877,13 +887,14 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) { } func TestRedelegateFromUnbondedValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) startTokens := sdk.TokensFromConsensusPower(30) startCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), startTokens)) // add bonded tokens to pool for delegations notBondedPool := keeper.GetNotBondedPool(ctx) - err := notBondedPool.SetCoins(notBondedPool.GetCoins().Add(startCoins...)) + oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(startCoins...)) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index 204dd3ac512d..574d0c9dd9af 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -13,7 +13,7 @@ import ( ) func TestHistoricalInfo(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 10) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10) validators := make([]types.Validator, len(addrVals)) for i, valAddr := range addrVals { @@ -37,7 +37,7 @@ func TestHistoricalInfo(t *testing.T) { } func TestTrackHistoricalInfo(t *testing.T) { - ctx, _, k, _ := CreateTestInput(t, false, 10) + ctx, _, _, k, _ := CreateTestInput(t, false, 10) // set historical entries in params to 5 params := types.DefaultParams() diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index eb9d16459e49..dbde12e7b1a9 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -9,7 +9,7 @@ import ( ) func TestParams(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 0) expParams := types.DefaultParams() //check that the empty keeper loads the default diff --git a/x/staking/keeper/querier_test.go b/x/staking/keeper/querier_test.go index 320266ca1a28..2bbd468ae075 100644 --- a/x/staking/keeper/querier_test.go +++ b/x/staking/keeper/querier_test.go @@ -20,7 +20,7 @@ var ( func TestNewQuerier(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) // Create Validators amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8)} var validators [2]types.Validator @@ -107,7 +107,7 @@ func TestNewQuerier(t *testing.T) { func TestQueryParametersPool(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) bondDenom := sdk.DefaultBondDenom res, err := queryParameters(ctx, keeper) @@ -124,15 +124,14 @@ func TestQueryParametersPool(t *testing.T) { var pool types.Pool bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) - errRes = cdc.UnmarshalJSON(res, &pool) - require.NoError(t, errRes) - require.Equal(t, bondedPool.GetCoins().AmountOf(bondDenom), pool.BondedTokens) - require.Equal(t, notBondedPool.GetCoins().AmountOf(bondDenom), pool.NotBondedTokens) + require.NoError(t, cdc.UnmarshalJSON(res, &pool)) + require.Equal(t, keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount, pool.NotBondedTokens) + require.Equal(t, keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount, pool.BondedTokens) } func TestQueryValidators(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 10000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000) params := keeper.GetParams(ctx) // Create Validators @@ -195,7 +194,7 @@ func TestQueryValidators(t *testing.T) { func TestQueryDelegation(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 10000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000) params := keeper.GetParams(ctx) // Create Validators and Delegation @@ -415,7 +414,7 @@ func TestQueryDelegation(t *testing.T) { func TestQueryRedelegations(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 10000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000) // Create Validators and Delegation val1 := types.NewValidator(addrVal1, pk1, types.Description{}) @@ -480,7 +479,7 @@ func TestQueryRedelegations(t *testing.T) { func TestQueryUnbondingDelegation(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 10000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000) // Create Validators and Delegation val1 := types.NewValidator(addrVal1, pk1, types.Description{}) @@ -571,7 +570,7 @@ func TestQueryUnbondingDelegation(t *testing.T) { func TestQueryHistoricalInfo(t *testing.T) { cdc := codec.New() - ctx, _, keeper, _ := CreateTestInput(t, false, 10000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000) // Create Validators and Delegation val1 := types.NewValidator(addrVal1, pk1, types.Description{}) diff --git a/x/staking/keeper/slash_test.go b/x/staking/keeper/slash_test.go index cc4d4651731a..2127cf51e196 100644 --- a/x/staking/keeper/slash_test.go +++ b/x/staking/keeper/slash_test.go @@ -15,17 +15,15 @@ import ( // TODO integrate with test_common.go helper (CreateTestInput) // setup helper function - creates two validators func setupHelper(t *testing.T, power int64) (sdk.Context, Keeper, types.Params) { - // setup - ctx, _, keeper, _ := CreateTestInput(t, false, power) + ctx, _, _, keeper, _ := CreateTestInput(t, false, power) params := keeper.GetParams(ctx) numVals := int64(3) amt := sdk.TokensFromConsensusPower(power) bondedCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), amt.MulRaw(numVals))) bondedPool := keeper.GetBondedPool(ctx) - err := bondedPool.SetCoins(bondedCoins) - require.NoError(t, err) + require.NoError(t, keeper.bankKeeper.SetBalances(ctx, bondedPool.GetAddress(), bondedCoins)) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) // add numVals validators @@ -43,7 +41,6 @@ func setupHelper(t *testing.T, power int64) (sdk.Context, Keeper, types.Params) // tests Jail, Unjail func TestRevocation(t *testing.T) { - // setup ctx, keeper, _ := setupHelper(t, 10) addr := addrVals[0] @@ -90,7 +87,8 @@ func TestSlashUnbondingDelegation(t *testing.T) { require.Equal(t, int64(0), slashAmount.Int64()) // test valid slash, before expiration timestamp and to which stake contributed - oldUnbondedPool := keeper.GetNotBondedPool(ctx) + notBondedPool := keeper.GetNotBondedPool(ctx) + oldUnbondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress()) ctx = ctx.WithBlockHeader(abci.Header{Time: time.Unix(0, 0)}) keeper.SetUnbondingDelegation(ctx, ubd) slashAmount = keeper.slashUnbondingDelegation(ctx, ubd, 0, fraction) @@ -104,9 +102,9 @@ func TestSlashUnbondingDelegation(t *testing.T) { // balance decreased require.Equal(t, sdk.NewInt(5), ubd.Entries[0].Balance) - newUnbondedPool := keeper.GetNotBondedPool(ctx) - diffTokens := oldUnbondedPool.GetCoins().Sub(newUnbondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) - require.Equal(t, int64(5), diffTokens.Int64()) + newUnbondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress()) + diffTokens := oldUnbondedPoolBalances.Sub(newUnbondedPoolBalances) + require.Equal(t, int64(5), diffTokens.AmountOf(keeper.BondDenom(ctx)).Int64()) } // tests slashRedelegation @@ -117,8 +115,9 @@ func TestSlashRedelegation(t *testing.T) { // add bonded tokens to pool for (re)delegations startCoins := sdk.NewCoins(sdk.NewInt64Coin(keeper.BondDenom(ctx), 15)) bondedPool := keeper.GetBondedPool(ctx) - err := bondedPool.SetCoins(bondedPool.GetCoins().Add(startCoins...)) - require.NoError(t, err) + balances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + + require.NoError(t, keeper.bankKeeper.SetBalances(ctx, bondedPool.GetAddress(), balances.Add(startCoins...))) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) // set a redelegation with an expiration timestamp beyond which the @@ -146,6 +145,8 @@ func TestSlashRedelegation(t *testing.T) { slashAmount = keeper.slashRedelegation(ctx, validator, rd, 0, fraction) require.Equal(t, int64(0), slashAmount.Int64()) + balances = keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + // test valid slash, before expiration timestamp and to which stake contributed ctx = ctx.WithBlockHeader(abci.Header{Time: time.Unix(0, 0)}) keeper.SetRedelegation(ctx, rd) @@ -171,8 +172,7 @@ func TestSlashRedelegation(t *testing.T) { // pool bonded tokens should decrease burnedCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), slashAmount)) - newBondedPool := keeper.GetBondedPool(ctx) - require.Equal(t, bondedPool.GetCoins().Sub(burnedCoins), newBondedPool.GetCoins()) + require.Equal(t, balances.Sub(burnedCoins), keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())) } // tests Slash at a future height (must panic) @@ -190,7 +190,9 @@ func TestSlashAtNegativeHeight(t *testing.T) { consAddr := sdk.ConsAddress(PKs[0].Address()) fraction := sdk.NewDecWithPrec(5, 1) - oldBondedPool := keeper.GetBondedPool(ctx) + bondedPool := keeper.GetBondedPool(ctx) + oldBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + validator, found := keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) keeper.Slash(ctx, consAddr, -2, 10, fraction) @@ -198,7 +200,6 @@ func TestSlashAtNegativeHeight(t *testing.T) { // read updated state validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) - newBondedPool := keeper.GetBondedPool(ctx) // end block updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -207,9 +208,11 @@ func TestSlashAtNegativeHeight(t *testing.T) { validator = keeper.mustGetValidator(ctx, validator.OperatorAddress) // power decreased require.Equal(t, int64(5), validator.GetConsensusPower()) + // pool bonded shares decreased - diffTokens := oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) - require.Equal(t, sdk.TokensFromConsensusPower(5), diffTokens) + newBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens := oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) + require.Equal(t, sdk.TokensFromConsensusPower(5).String(), diffTokens.String()) } // tests Slash at the current height @@ -218,7 +221,9 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { consAddr := sdk.ConsAddress(PKs[0].Address()) fraction := sdk.NewDecWithPrec(5, 1) - oldBondedPool := keeper.GetBondedPool(ctx) + bondedPool := keeper.GetBondedPool(ctx) + oldBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + validator, found := keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) keeper.Slash(ctx, consAddr, ctx.BlockHeight(), 10, fraction) @@ -226,7 +231,6 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { // read updated state validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) - newBondedPool := keeper.GetBondedPool(ctx) // end block updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -235,9 +239,11 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { validator = keeper.mustGetValidator(ctx, validator.OperatorAddress) // power decreased require.Equal(t, int64(5), validator.GetConsensusPower()) + // pool bonded shares decreased - diffTokens := oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) - require.Equal(t, sdk.TokensFromConsensusPower(5), diffTokens) + newBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens := oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) + require.Equal(t, sdk.TokensFromConsensusPower(5).String(), diffTokens.String()) } // tests Slash at a previous height with an unbonding delegation @@ -255,7 +261,9 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // slash validator for the first time ctx = ctx.WithBlockHeight(12) - oldBondedPool := keeper.GetBondedPool(ctx) + bondedPool := keeper.GetBondedPool(ctx) + oldBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + validator, found := keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) keeper.Slash(ctx, consAddr, 10, 10, fraction) @@ -268,16 +276,19 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { ubd, found = keeper.GetUnbondingDelegation(ctx, addrDels[0], addrVals[0]) require.True(t, found) require.Len(t, ubd.Entries, 1) + // balance decreased require.Equal(t, sdk.TokensFromConsensusPower(2), ubd.Entries[0].Balance) - // read updated pool - newBondedPool := keeper.GetBondedPool(ctx) + // bonded tokens burned - diffTokens := oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) + newBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens := oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) require.Equal(t, sdk.TokensFromConsensusPower(3), diffTokens) + // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) + // power decreased by 3 - 6 stake originally bonded at the time of infraction // was still bonded at the time of discovery and was slashed by half, 4 stake // bonded at the time of discovery hadn't been bonded at the time of infraction @@ -287,19 +298,23 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // slash validator again ctx = ctx.WithBlockHeight(13) keeper.Slash(ctx, consAddr, 9, 10, fraction) + ubd, found = keeper.GetUnbondingDelegation(ctx, addrDels[0], addrVals[0]) require.True(t, found) require.Len(t, ubd.Entries, 1) + // balance decreased again require.Equal(t, sdk.NewInt(0), ubd.Entries[0].Balance) - // read updated pool - newBondedPool = keeper.GetBondedPool(ctx) + // bonded tokens burned again - diffTokens = oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) + newBondedPoolBalances = keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens = oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) require.Equal(t, sdk.TokensFromConsensusPower(6), diffTokens) + // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) + // power decreased by 3 again require.Equal(t, int64(4), validator.GetConsensusPower()) @@ -309,19 +324,23 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // this may not be the desirable behaviour, ref https://github.com/cosmos/cosmos-sdk/issues/1440 ctx = ctx.WithBlockHeight(13) keeper.Slash(ctx, consAddr, 9, 10, fraction) + ubd, found = keeper.GetUnbondingDelegation(ctx, addrDels[0], addrVals[0]) require.True(t, found) require.Len(t, ubd.Entries, 1) + // balance unchanged require.Equal(t, sdk.NewInt(0), ubd.Entries[0].Balance) - // read updated pool - newBondedPool = keeper.GetBondedPool(ctx) + // bonded tokens burned again - diffTokens = oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) + newBondedPoolBalances = keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens = oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) require.Equal(t, sdk.TokensFromConsensusPower(9), diffTokens) + // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) + // power decreased by 3 again require.Equal(t, int64(1), validator.GetConsensusPower()) @@ -331,18 +350,22 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // this may not be the desirable behaviour, ref https://github.com/cosmos/cosmos-sdk/issues/1440 ctx = ctx.WithBlockHeight(13) keeper.Slash(ctx, consAddr, 9, 10, fraction) + ubd, found = keeper.GetUnbondingDelegation(ctx, addrDels[0], addrVals[0]) require.True(t, found) require.Len(t, ubd.Entries, 1) + // balance unchanged require.Equal(t, sdk.NewInt(0), ubd.Entries[0].Balance) - // read updated pool - newBondedPool = keeper.GetBondedPool(ctx) + // just 1 bonded token burned again since that's all the validator now has - diffTokens = oldBondedPool.GetCoins().Sub(newBondedPool.GetCoins()).AmountOf(keeper.BondDenom(ctx)) + newBondedPoolBalances = keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + diffTokens = oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx)) require.Equal(t, sdk.TokensFromConsensusPower(10), diffTokens) + // apply TM updates keeper.ApplyAndReturnValidatorSetUpdates(ctx) + // read updated validator // power decreased by 1 again, validator is out of stake // validator should be in unbonding period @@ -371,12 +394,15 @@ func TestSlashWithRedelegation(t *testing.T) { bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) rdCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, rdTokens.MulRaw(2))) - err := bondedPool.SetCoins(bondedPool.GetCoins().Add(rdCoins...)) + + balances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + err := keeper.bankKeeper.SetBalances(ctx, bondedPool.GetAddress(), balances.Add(rdCoins...)) require.NoError(t, err) + keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) - oldBonded := bondedPool.GetCoins().AmountOf(bondDenom) - oldNotBonded := notBondedPool.GetCoins().AmountOf(bondDenom) + oldBonded := keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + oldNotBonded := keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount // slash validator ctx = ctx.WithBlockHeight(12) @@ -388,10 +414,14 @@ func TestSlashWithRedelegation(t *testing.T) { bondedPool = keeper.GetBondedPool(ctx) notBondedPool = keeper.GetNotBondedPool(ctx) + // burn bonded tokens from only from delegations - require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPool.GetCoins().AmountOf(bondDenom))) - require.True(sdk.IntEq(t, oldNotBonded, notBondedPool.GetCoins().AmountOf(bondDenom))) - oldBonded = bondedPool.GetCoins().AmountOf(bondDenom) + bondedPoolBalance := keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance)) + + notBondedPoolBalance := keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance)) + oldBonded = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount // read updating redelegation rd, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) @@ -416,10 +446,18 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool bondedPool = keeper.GetBondedPool(ctx) notBondedPool = keeper.GetNotBondedPool(ctx) + // seven bonded tokens burned - require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPool.GetCoins().AmountOf(bondDenom))) - require.True(sdk.IntEq(t, oldNotBonded, notBondedPool.GetCoins().AmountOf(bondDenom))) - oldBonded = bondedPool.GetCoins().AmountOf(bondDenom) + bondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance)) + require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance)) + + bondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance)) + + notBondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance)) + oldBonded = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount // read updating redelegation rd, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) @@ -444,9 +482,12 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool bondedPool = keeper.GetBondedPool(ctx) notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPool.GetCoins().AmountOf(bondDenom))) - require.True(sdk.IntEq(t, oldNotBonded, notBondedPool.GetCoins().AmountOf(bondDenom))) - oldBonded = bondedPool.GetCoins().AmountOf(bondDenom) + + bondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance)) + notBondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance)) + oldBonded = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount // read updating redelegation rd, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) @@ -471,8 +512,11 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool bondedPool = keeper.GetBondedPool(ctx) notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, oldBonded, bondedPool.GetCoins().AmountOf(bondDenom))) - require.True(sdk.IntEq(t, oldNotBonded, notBondedPool.GetCoins().AmountOf(bondDenom))) + + bondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded, bondedPoolBalance)) + notBondedPoolBalance = keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance)) // read updating redelegation rd, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) @@ -515,14 +559,18 @@ func TestSlashBoth(t *testing.T) { // update bonded tokens bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) - require.NoError(t, bondedPool.SetCoins(bondedPool.GetCoins().Add(bondedCoins...))) - require.NoError(t, bondedPool.SetCoins(notBondedPool.GetCoins().Add(notBondedCoins...))) + + bondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + require.NoError(t, keeper.bankKeeper.SetBalances(ctx, bondedPool.GetAddress(), bondedPoolBalances.Add(bondedCoins...))) + + notBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress()) + require.NoError(t, keeper.bankKeeper.SetBalances(ctx, notBondedPool.GetAddress(), notBondedPoolBalances.Add(notBondedCoins...))) + keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) - oldBonded := bondedPool.GetCoins().AmountOf(bondDenom) - oldNotBonded := notBondedPool.GetCoins().AmountOf(bondDenom) - + oldBonded := keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + oldNotBonded := keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount // slash validator ctx = ctx.WithBlockHeight(12) validator, found := keeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(PKs[0])) @@ -537,8 +585,12 @@ func TestSlashBoth(t *testing.T) { // read updated pool bondedPool = keeper.GetBondedPool(ctx) notBondedPool = keeper.GetNotBondedPool(ctx) - require.True(sdk.IntEq(t, oldBonded.Sub(burnedBondAmount), bondedPool.GetCoins().AmountOf(bondDenom))) - require.True(sdk.IntEq(t, oldNotBonded.Sub(burnedNotBondedAmount), notBondedPool.GetCoins().AmountOf(bondDenom))) + + bondedPoolBalance := keeper.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldBonded.Sub(burnedBondAmount), bondedPoolBalance)) + + notBondedPoolBalance := keeper.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount + require.True(sdk.IntEq(t, oldNotBonded.Sub(burnedNotBondedAmount), notBondedPoolBalance)) // read updating redelegation rdA, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 0d730b48b3ca..65efabe4e25c 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -79,9 +79,10 @@ func MakeTestCodec() *codec.Codec { // Hogpodge of all sorts of input required for testing. // `initPower` is converted to an amount of tokens. // If `initPower` is 0, no addrs get created. -func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, Keeper, types.SupplyKeeper) { +func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, types.BankKeeper, Keeper, types.SupplyKeeper) { keyStaking := sdk.NewKVStoreKey(types.StoreKey) keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + bankKey := sdk.NewKVStoreKey(bank.StoreKey) keyParams := sdk.NewKVStoreKey(params.StoreKey) tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) keySupply := sdk.NewKVStoreKey(supply.StoreKey) @@ -90,6 +91,7 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(bankKey, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db) @@ -125,6 +127,8 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context ) bk := bank.NewBaseKeeper( + cdc, + bankKey, accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs, @@ -143,26 +147,23 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply)) - keeper := NewKeeper(cdc, keyStaking, supplyKeeper, pk.Subspace(DefaultParamspace)) + keeper := NewKeeper(cdc, keyStaking, bk, supplyKeeper, pk.Subspace(DefaultParamspace)) keeper.SetParams(ctx, types.DefaultParams()) // set module accounts - err = notBondedPool.SetCoins(totalSupply) - require.NoError(t, err) + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), totalSupply)) supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc) supplyKeeper.SetModuleAccount(ctx, bondPool) supplyKeeper.SetModuleAccount(ctx, notBondedPool) // fill all the addresses with some coins, set the loose pool tokens simultaneously - for _, addr := range Addrs { - _, err := bk.AddCoins(ctx, addr, initCoins) - if err != nil { - panic(err) - } + for i, addr := range Addrs { + accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, PKs[i], uint64(i), 0)) + require.NoError(t, bk.SetBalances(ctx, addr, initCoins)) } - return ctx, accountKeeper, keeper, supplyKeeper + return ctx, accountKeeper, bk, keeper, supplyKeeper } func NewPubKey(pk string) (res crypto.PubKey) { diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index c01fabbca2da..ae2552fe8373 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -17,7 +17,7 @@ import ( //_______________________________________________________ func TestSetValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 10) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 10) valPubKey := PKs[0] valAddr := sdk.ValAddress(valPubKey.Address().Bytes()) @@ -70,12 +70,12 @@ func TestSetValidator(t *testing.T) { } func TestUpdateValidatorByPowerIndex(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) - bondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(1234)))) - notBondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(10000)))) + bk.SetBalances(ctx, bondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(1234)))) + bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(10000)))) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -111,7 +111,7 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { maxVals := 5 // create context, keeper, and pool for tests - ctx, _, keeper, _ := CreateTestInput(t, false, 0) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) @@ -121,8 +121,8 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { keeper.SetParams(ctx, params) // create a random pool - bondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(1234)))) - notBondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(10000)))) + bk.SetBalances(ctx, bondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(1234)))) + bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(10000)))) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) @@ -165,14 +165,14 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { func TestSlashToZeroPowerRemoved(t *testing.T) { // initialize setup - ctx, _, keeper, _ := CreateTestInput(t, false, 100) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 100) // add a validator validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) valTokens := sdk.TokensFromConsensusPower(100) bondedPool := keeper.GetBondedPool(ctx) - err := bondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), valTokens))) + err := bk.SetBalances(ctx, bondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), valTokens))) require.NoError(t, err) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -195,7 +195,7 @@ func TestSlashToZeroPowerRemoved(t *testing.T) { // This function tests UpdateValidator, GetValidator, GetLastValidators, RemoveValidator func TestValidatorBasics(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) //construct the validators var validators [3]types.Validator @@ -294,7 +294,7 @@ func TestValidatorBasics(t *testing.T) { // test how the validators are sorted, tests GetBondedValidatorsByPower func TestGetValidatorSortingUnmixed(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) // initialize some validators into the state amts := []int64{ @@ -374,11 +374,12 @@ func TestGetValidatorSortingUnmixed(t *testing.T) { } func TestGetValidatorSortingMixed(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1000) bondedPool := keeper.GetBondedPool(ctx) notBondedPool := keeper.GetNotBondedPool(ctx) - bondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(501)))) - notBondedPool.SetCoins(sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(0)))) + + bk.SetBalances(ctx, bondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(501)))) + bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(0)))) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) @@ -432,7 +433,7 @@ func TestGetValidatorSortingMixed(t *testing.T) { // TODO separate out into multiple tests func TestGetValidatorsEdgeCases(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1000) // set max validators to 2 params := keeper.GetParams(ctx) @@ -446,10 +447,14 @@ func TestGetValidatorsEdgeCases(t *testing.T) { for i, power := range powers { moniker := fmt.Sprintf("val#%d", int64(i)) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{Moniker: moniker}) + tokens := sdk.TokensFromConsensusPower(power) validators[i], _ = validators[i].AddTokensFromDel(tokens) + notBondedPool := keeper.GetNotBondedPool(ctx) - require.NoError(t, notBondedPool.SetCoins(notBondedPool.GetCoins().Add(sdk.NewCoin(params.BondDenom, tokens)))) + balances := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, tokens)))) + keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) validators[i] = TestingUpdateValidator(keeper, ctx, validators[i], true) } @@ -465,8 +470,10 @@ func TestGetValidatorsEdgeCases(t *testing.T) { delTokens := sdk.TokensFromConsensusPower(500) validators[0], _ = validators[0].AddTokensFromDel(delTokens) notBondedPool := keeper.GetNotBondedPool(ctx) + newTokens := sdk.NewCoins() - require.NoError(t, notBondedPool.SetCoins(notBondedPool.GetCoins().Add(newTokens...))) + balances := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(newTokens...))) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) // test that the two largest validators are @@ -496,7 +503,8 @@ func TestGetValidatorsEdgeCases(t *testing.T) { notBondedPool = keeper.GetNotBondedPool(ctx) newTokens = sdk.NewCoins(sdk.NewCoin(params.BondDenom, sdk.TokensFromConsensusPower(1))) - require.NoError(t, notBondedPool.SetCoins(notBondedPool.GetCoins().Add(newTokens...))) + balances = bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(newTokens...))) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true) @@ -511,7 +519,8 @@ func TestGetValidatorsEdgeCases(t *testing.T) { validators[3], _ = validators[3].RemoveDelShares(sdk.NewDec(201)) bondedPool := keeper.GetBondedPool(ctx) - require.NoError(t, bondedPool.SetCoins(bondedPool.GetCoins().Add(sdk.NewCoin(params.BondDenom, rmTokens)))) + balances = bk.GetAllBalances(ctx, bondedPool.GetAddress()) + require.NoError(t, bk.SetBalances(ctx, bondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, rmTokens)))) keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true) @@ -525,7 +534,8 @@ func TestGetValidatorsEdgeCases(t *testing.T) { validators[3], _ = validators[3].AddTokensFromDel(sdk.NewInt(200)) notBondedPool = keeper.GetNotBondedPool(ctx) - require.NoError(t, notBondedPool.SetCoins(notBondedPool.GetCoins().Add(sdk.NewCoin(params.BondDenom, sdk.NewInt(200))))) + balances = bk.GetAllBalances(ctx, notBondedPool.GetAddress()) + require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, sdk.NewInt(200))))) keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true) @@ -538,7 +548,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { } func TestValidatorBondHeight(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) // now 2 max resValidators params := keeper.GetParams(ctx) @@ -585,7 +595,7 @@ func TestValidatorBondHeight(t *testing.T) { } func TestFullValidatorSetPowerChange(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) params := keeper.GetParams(ctx) max := 2 params.MaxValidators = uint16(2) @@ -627,7 +637,7 @@ func TestFullValidatorSetPowerChange(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{10, 20} var validators [2]types.Validator @@ -657,7 +667,7 @@ func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesIdentical(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{10, 20} var validators [2]types.Validator @@ -680,7 +690,7 @@ func TestApplyAndReturnValidatorSetUpdatesIdentical(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{10, 20} var validators [2]types.Validator @@ -709,7 +719,7 @@ func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{10, 20} var validators [2]types.Validator @@ -741,7 +751,7 @@ func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{10, 20, 5, 15, 25} var validators [5]types.Validator @@ -787,7 +797,7 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) params := types.DefaultParams() params.MaxValidators = 2 keeper.SetParams(ctx, params) @@ -828,7 +838,7 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) powers := []int64{100, 100} var validators [2]types.Validator @@ -869,7 +879,7 @@ func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) params := keeper.GetParams(ctx) params.MaxValidators = uint16(3) @@ -950,7 +960,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { } func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) params := keeper.GetParams(ctx) params.MaxValidators = uint16(2) @@ -1027,7 +1037,7 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { } func TestUpdateValidatorCommission(t *testing.T) { - ctx, _, keeper, _ := CreateTestInput(t, false, 1000) + ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000) ctx = ctx.WithBlockHeader(abci.Header{Time: time.Now().UTC()}) commission1 := types.NewCommissionWithTime( diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index 8744496e07b6..58451676a739 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -92,7 +92,7 @@ func WeightedOperations( } // SimulateMsgCreateValidator generates a MsgCreateValidator with random values -// nolint: funlen +// nolint: funlen interfacer func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, @@ -122,9 +122,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k selfDelegation := sdk.NewCoin(denom, amount) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) var fees sdk.Coins coins, hasNeg := spendable.SafeSub(sdk.Coins{selfDelegation}) @@ -173,7 +171,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k } // SimulateMsgEditValidator generates a MsgEditValidator with random values -// nolint: funlen +// nolint: funlen interfacer func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, @@ -203,9 +201,7 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k kee } account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -242,7 +238,7 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k kee } // SimulateMsgDelegate generates a MsgDelegate with random values -// nolint: funlen +// nolint: funlen interfacer func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, @@ -276,9 +272,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K bondAmt := sdk.NewCoin(denom, amount) account := ak.GetAccount(ctx, simAccount.Address) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) var fees sdk.Coins coins, hasNeg := spendable.SafeSub(sdk.Coins{bondAmt}) @@ -311,7 +305,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K } // SimulateMsgUndelegate generates a MsgUndelegate with random values -// nolint: funlen +// nolint: funlen,interfacer func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, @@ -366,9 +360,7 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper } account := ak.GetAccount(ctx, delAddr) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { @@ -395,7 +387,7 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper } // SimulateMsgBeginRedelegate generates a MsgBeginRedelegate with random values -// nolint: funlen +// nolint: funlen,interfacer func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, @@ -406,8 +398,8 @@ func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k k if !ok { return simulation.NoOpMsg(types.ModuleName), nil, nil } - srcAddr := srcVal.GetOperator() + srcAddr := srcVal.GetOperator() delegations := k.GetValidatorDelegations(ctx, srcAddr) // get random delegator from src validator @@ -464,15 +456,14 @@ func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k k break } } + // if simaccount.PrivKey == nil, delegation address does not exist in accs. Return error if simAccount.PrivKey == nil { return simulation.NoOpMsg(types.ModuleName), nil, fmt.Errorf("delegation addr: %s does not exist in simulation accounts", delAddr) } account := ak.GetAccount(ctx, delAddr) - balances := bk.GetAllBalances(ctx, account.GetAddress()) - locked := bk.LockedCoins(ctx, account.GetAddress()) - spendable := balances.Sub(locked) + spendable := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simulation.RandomFees(r, ctx, spendable) if err != nil { diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index c0e407ac7c20..c9e69b9018bb 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -25,6 +25,7 @@ type BankKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SetBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } // SupplyKeeper defines the expected supply Keeper (noalias) diff --git a/x/supply/internal/keeper/bank_test.go b/x/supply/internal/keeper/bank_test.go index 6a817a164f8d..5388dc804d49 100644 --- a/x/supply/internal/keeper/bank_test.go +++ b/x/supply/internal/keeper/bank_test.go @@ -24,24 +24,26 @@ var ( initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)) ) -func getCoinsByName(ctx sdk.Context, sk keep.Keeper, ak types.AccountKeeper, moduleName string) sdk.Coins { +// nolint +func getCoinsByName(ctx sdk.Context, sk keep.Keeper, ak types.AccountKeeper, bk types.BankKeeper, moduleName string) sdk.Coins { moduleAddress := sk.GetModuleAddress(moduleName) macc := ak.GetAccount(ctx, moduleAddress) if macc == nil { return sdk.Coins(nil) } - return macc.GetCoins() + + return bk.GetAllBalances(ctx, macc.GetAddress()) } func TestSendCoins(t *testing.T) { app, ctx := createTestApp(false) keeper := app.SupplyKeeper ak := app.AccountKeeper + bk := app.BankKeeper baseAcc := ak.NewAccountWithAddress(ctx, types.NewModuleAddress("baseAcc")) - err := holderAcc.SetCoins(initCoins) - require.NoError(t, err) + require.NoError(t, bk.SetBalances(ctx, holderAcc.GetAddress(), initCoins)) keeper.SetSupply(ctx, types.NewSupply(initCoins)) keeper.SetModuleAccount(ctx, holderAcc) @@ -60,30 +62,33 @@ func TestSendCoins(t *testing.T) { keeper.SendCoinsFromModuleToAccount(ctx, "", baseAcc.GetAddress(), initCoins) }) - err = keeper.SendCoinsFromModuleToAccount(ctx, holderAcc.GetName(), baseAcc.GetAddress(), initCoins.Add(initCoins...)) - require.Error(t, err) - - err = keeper.SendCoinsFromModuleToModule(ctx, holderAcc.GetName(), types.Burner, initCoins) - require.NoError(t, err) - require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, holderAcc.GetName())) - require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, types.Burner)) - - err = keeper.SendCoinsFromModuleToAccount(ctx, types.Burner, baseAcc.GetAddress(), initCoins) - require.NoError(t, err) - require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, types.Burner)) - - require.Equal(t, initCoins, ak.GetAccount(ctx, baseAcc.GetAddress()).GetCoins()) - - err = keeper.SendCoinsFromAccountToModule(ctx, baseAcc.GetAddress(), types.Burner, initCoins) - require.NoError(t, err) - require.Equal(t, sdk.Coins(nil), ak.GetAccount(ctx, baseAcc.GetAddress()).GetCoins()) - require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, types.Burner)) + require.Error( + t, + keeper.SendCoinsFromModuleToAccount(ctx, holderAcc.GetName(), baseAcc.GetAddress(), initCoins.Add(initCoins...)), + ) + + require.NoError( + t, keeper.SendCoinsFromModuleToModule(ctx, holderAcc.GetName(), types.Burner, initCoins), + ) + require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, bk, holderAcc.GetName())) + require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, bk, types.Burner)) + + require.NoError( + t, keeper.SendCoinsFromModuleToAccount(ctx, types.Burner, baseAcc.GetAddress(), initCoins), + ) + require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, bk, types.Burner)) + require.Equal(t, initCoins, bk.GetAllBalances(ctx, baseAcc.GetAddress())) + + require.NoError(t, keeper.SendCoinsFromAccountToModule(ctx, baseAcc.GetAddress(), types.Burner, initCoins)) + require.Equal(t, sdk.Coins(nil), bk.GetAllBalances(ctx, baseAcc.GetAddress())) + require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, bk, types.Burner)) } func TestMintCoins(t *testing.T) { app, ctx := createTestApp(false) keeper := app.SupplyKeeper ak := app.AccountKeeper + bk := app.BankKeeper keeper.SetModuleAccount(ctx, burnerAcc) keeper.SetModuleAccount(ctx, minterAcc) @@ -101,7 +106,7 @@ func TestMintCoins(t *testing.T) { err = keeper.MintCoins(ctx, types.Minter, initCoins) require.NoError(t, err) - require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, types.Minter)) + require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, bk, types.Minter)) require.Equal(t, initialSupply.GetTotal().Add(initCoins...), keeper.GetSupply(ctx).GetTotal()) // test same functionality on module account with multiple permissions @@ -109,7 +114,7 @@ func TestMintCoins(t *testing.T) { err = keeper.MintCoins(ctx, multiPermAcc.GetName(), initCoins) require.NoError(t, err) - require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, multiPermAcc.GetName())) + require.Equal(t, initCoins, getCoinsByName(ctx, keeper, ak, bk, multiPermAcc.GetName())) require.Equal(t, initialSupply.GetTotal().Add(initCoins...), keeper.GetSupply(ctx).GetTotal()) require.Panics(t, func() { keeper.MintCoins(ctx, types.Burner, initCoins) }) @@ -119,8 +124,9 @@ func TestBurnCoins(t *testing.T) { app, ctx := createTestApp(false) keeper := app.SupplyKeeper ak := app.AccountKeeper + bk := app.BankKeeper - require.NoError(t, burnerAcc.SetCoins(initCoins)) + require.NoError(t, bk.SetBalances(ctx, burnerAcc.GetAddress(), initCoins)) keeper.SetSupply(ctx, types.NewSupply(initCoins)) keeper.SetModuleAccount(ctx, burnerAcc) @@ -136,7 +142,7 @@ func TestBurnCoins(t *testing.T) { err = keeper.BurnCoins(ctx, types.Burner, initCoins) require.NoError(t, err) - require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, types.Burner)) + require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, bk, types.Burner)) require.Equal(t, initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal()) // test same functionality on module account with multiple permissions @@ -144,11 +150,11 @@ func TestBurnCoins(t *testing.T) { initialSupply = initialSupply.Inflate(initCoins) keeper.SetSupply(ctx, initialSupply) - require.NoError(t, multiPermAcc.SetCoins(initCoins)) + require.NoError(t, bk.SetBalances(ctx, multiPermAcc.GetAddress(), initCoins)) keeper.SetModuleAccount(ctx, multiPermAcc) err = keeper.BurnCoins(ctx, multiPermAcc.GetName(), initCoins) require.NoError(t, err) - require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, multiPermAcc.GetName())) + require.Equal(t, sdk.Coins(nil), getCoinsByName(ctx, keeper, ak, bk, multiPermAcc.GetName())) require.Equal(t, initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal()) } diff --git a/x/supply/internal/types/account_test.go b/x/supply/internal/types/account_test.go index 396f1a90be14..a3ee01bf1afa 100644 --- a/x/supply/internal/types/account_test.go +++ b/x/supply/internal/types/account_test.go @@ -23,7 +23,7 @@ func TestModuleAccountMarshalYAML(t *testing.T) { bs, err := yaml.Marshal(moduleAcc) require.NoError(t, err) - want := "|\n address: cosmos1n7rdpqvgf37ktx30a2sv2kkszk3m7ncmg5drhe\n coins: []\n public_key: \"\"\n account_number: 0\n sequence: 0\n name: test\n permissions:\n - minter\n - burner\n - staking\n" + want := "|\n address: cosmos1n7rdpqvgf37ktx30a2sv2kkszk3m7ncmg5drhe\n public_key: \"\"\n account_number: 0\n sequence: 0\n name: test\n permissions:\n - minter\n - burner\n - staking\n" require.Equal(t, want, string(bs)) } @@ -52,7 +52,7 @@ func TestHasPermissions(t *testing.T) { func TestValidate(t *testing.T) { addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - baseAcc := authtypes.NewBaseAccount(addr, sdk.Coins{}, nil, 0, 0) + baseAcc := authtypes.NewBaseAccount(addr, nil, 0, 0) tests := []struct { name string acc authexported.GenesisAccount @@ -86,8 +86,7 @@ func TestValidate(t *testing.T) { func TestModuleAccountJSON(t *testing.T) { pubkey := secp256k1.GenPrivKey().PubKey() addr := sdk.AccAddress(pubkey.Address()) - coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5)) - baseAcc := authtypes.NewBaseAccount(addr, coins, nil, 10, 50) + baseAcc := authtypes.NewBaseAccount(addr, nil, 10, 50) acc := NewModuleAccount(baseAcc, "test", "burner") bz, err := json.Marshal(acc) diff --git a/x/supply/internal/types/expected_keepers.go b/x/supply/internal/types/expected_keepers.go index 8ef551e7e63d..de933b75145c 100644 --- a/x/supply/internal/types/expected_keepers.go +++ b/x/supply/internal/types/expected_keepers.go @@ -19,6 +19,9 @@ type BankKeeper interface { DelegateCoins(ctx sdk.Context, fromAdd, toAddr sdk.AccAddress, amt sdk.Coins) error UndelegateCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error + GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin + SubtractCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, error) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, error)