From 6db53cafcb54df89354c8640a96f87b6d468cc7a Mon Sep 17 00:00:00 2001 From: Marko Date: Thu, 11 Jan 2024 08:55:50 +0100 Subject: [PATCH] chore(sdk): remove modules as a dependency (1/n) (#19003) --- baseapp/block_gas_test.go | 12 +++++------- baseapp/expected_keepers.go | 17 +++++++++++++++++ client/rpc/rpc_test.go | 5 +---- testutil/network/util.go | 9 +++++---- testutil/sims/address_helpers.go | 19 +++++-------------- testutil/sims/expected_keepers.go | 19 +++++++++++++++++++ testutil/sims/simulation_helpers_test.go | 12 ++++++++---- testutil/sims/state_helpers.go | 9 +++++---- types/query/fuzz_test.go | 13 ++++++++----- 9 files changed, 73 insertions(+), 42 deletions(-) create mode 100644 baseapp/expected_keepers.go create mode 100644 testutil/sims/expected_keepers.go diff --git a/baseapp/block_gas_test.go b/baseapp/block_gas_test.go index bef8ef4d75..d18b4807dd 100644 --- a/baseapp/block_gas_test.go +++ b/baseapp/block_gas_test.go @@ -14,11 +14,9 @@ import ( "cosmossdk.io/log" sdkmath "cosmossdk.io/math" store "cosmossdk.io/store/types" - authkeeper "cosmossdk.io/x/auth/keeper" xauthsigning "cosmossdk.io/x/auth/signing" - bankkeeper "cosmossdk.io/x/bank/keeper" - banktypes "cosmossdk.io/x/bank/types" + "github.com/cosmos/cosmos-sdk/baseapp" baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/tx" @@ -71,8 +69,8 @@ func TestBaseApp_BlockGas(t *testing.T) { for _, tc := range testcases { var ( - bankKeeper bankkeeper.Keeper - accountKeeper authkeeper.AccountKeeper + bankKeeper baseapp.BankKeeper + accountKeeper baseapp.AuthKeeper appBuilder *runtime.AppBuilder txConfig client.TxConfig cdc codec.Codec @@ -108,7 +106,7 @@ func TestBaseApp_BlockGas(t *testing.T) { baseapptestutil.RegisterKeyValueServer(bapp.MsgServiceRouter(), BlockGasImpl{ panicTx: tc.panicTx, gasToConsume: tc.gasToConsume, - key: bapp.UnsafeFindStoreKey(banktypes.ModuleName), + key: bapp.UnsafeFindStoreKey(testutil.BankModuleName), }) genState := GenesisStateWithSingleValidator(t, cdc, appBuilder) @@ -160,7 +158,7 @@ func TestBaseApp_BlockGas(t *testing.T) { // check result ctx = bapp.GetContextForFinalizeBlock(txBytes) - okValue := ctx.KVStore(bapp.UnsafeFindStoreKey(banktypes.ModuleName)).Get([]byte("ok")) + okValue := ctx.KVStore(bapp.UnsafeFindStoreKey(testutil.BankModuleName)).Get([]byte("ok")) if tc.expErr { if tc.panicTx { diff --git a/baseapp/expected_keepers.go b/baseapp/expected_keepers.go new file mode 100644 index 0000000000..26147f935e --- /dev/null +++ b/baseapp/expected_keepers.go @@ -0,0 +1,17 @@ +package baseapp + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type BankKeeper interface { + MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error + SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin +} + +type AuthKeeper interface { + GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI +} diff --git a/client/rpc/rpc_test.go b/client/rpc/rpc_test.go index e8b4635eee..7606602127 100644 --- a/client/rpc/rpc_test.go +++ b/client/rpc/rpc_test.go @@ -2,7 +2,6 @@ package rpc_test import ( "context" - "fmt" "strconv" "testing" @@ -11,8 +10,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/metadata" - banktypes "cosmossdk.io/x/bank/types" - "github.com/cosmos/cosmos-sdk/testutil/network" "github.com/cosmos/cosmos-sdk/testutil/testdata" "github.com/cosmos/cosmos-sdk/types/address" @@ -97,7 +94,7 @@ func (s *IntegrationTestSuite) TestQueryABCIHeight() { clientCtx = clientCtx.WithHeight(tc.ctxHeight) req := abci.RequestQuery{ - Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey), + Path: "store/bank/key", Height: tc.reqHeight, Data: address.MustLengthPrefix(val.GetAddress()), Prove: true, diff --git a/testutil/network/util.go b/testutil/network/util.go index 3b75cc3d6e..702c195aa4 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -26,6 +26,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/api" servergrpc "github.com/cosmos/cosmos-sdk/server/grpc" servercmtlog "github.com/cosmos/cosmos-sdk/server/log" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/x/genutil" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) @@ -166,7 +167,7 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error { func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalances []banktypes.Balance, genFiles []string) error { // set the accounts in the genesis state var authGenState authtypes.GenesisState - cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[authtypes.ModuleName], &authGenState) + cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[testutil.AuthModuleName], &authGenState) accounts, err := authtypes.PackAccounts(genAccounts) if err != nil { @@ -174,14 +175,14 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance } authGenState.Accounts = append(authGenState.Accounts, accounts...) - cfg.GenesisState[authtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&authGenState) + cfg.GenesisState[testutil.AuthModuleName] = cfg.Codec.MustMarshalJSON(&authGenState) // set the balances in the genesis state var bankGenState banktypes.GenesisState - cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[banktypes.ModuleName], &bankGenState) + cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[testutil.BankModuleName], &bankGenState) bankGenState.Balances = append(bankGenState.Balances, genBalances...) - cfg.GenesisState[banktypes.ModuleName] = cfg.Codec.MustMarshalJSON(&bankGenState) + cfg.GenesisState[testutil.BankModuleName] = cfg.Codec.MustMarshalJSON(&bankGenState) appGenStateJSON, err := json.MarshalIndent(cfg.GenesisState, "", " ") if err != nil { diff --git a/testutil/sims/address_helpers.go b/testutil/sims/address_helpers.go index 7425f1e8a8..0f6d85e275 100644 --- a/testutil/sims/address_helpers.go +++ b/testutil/sims/address_helpers.go @@ -2,14 +2,12 @@ package sims import ( "bytes" - "context" "encoding/hex" "fmt" "strconv" errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" - bankkeeper "cosmossdk.io/x/bank/keeper" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -21,15 +19,8 @@ const mintModuleName = "mint" type GenerateAccountStrategy func(int) []sdk.AccAddress -// BondDenomProvider is a subset of the staking keeper's public interface that -// provides the staking bond denom. It is used in arguments in this package's -// functions so that a mock staking keeper can be passed instead of the real one. -type BondDenomProvider interface { - BondDenom(ctx context.Context) (string, error) -} - // AddTestAddrsFromPubKeys adds the addresses into the SimApp providing only the public keys. -func AddTestAddrsFromPubKeys(bankKeeper bankkeeper.Keeper, stakingKeeper BondDenomProvider, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) { +func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) { bondDenom, err := stakingKeeper.BondDenom(ctx) if err != nil { panic(err) @@ -43,16 +34,16 @@ func AddTestAddrsFromPubKeys(bankKeeper bankkeeper.Keeper, stakingKeeper BondDen // AddTestAddrs constructs and returns accNum amount of accounts with an // initial balance of accAmt in random order -func AddTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper BondDenomProvider, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress { +func AddTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress { return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateRandomAccounts) } // AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order -func AddTestAddrsIncremental(bankKeeper bankkeeper.Keeper, stakingKeeper BondDenomProvider, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress { +func AddTestAddrsIncremental(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress { return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts) } -func addTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper BondDenomProvider, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress { +func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress { testAddrs := strategy(accNum) bondDenom, err := stakingKeeper.BondDenom(ctx) if err != nil { @@ -67,7 +58,7 @@ func addTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper BondDenomProvider, return testAddrs } -func initAccountWithCoins(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) { +func initAccountWithCoins(bankKeeper BankKeeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) { if err := bankKeeper.MintCoins(ctx, mintModuleName, coins); err != nil { panic(err) } diff --git a/testutil/sims/expected_keepers.go b/testutil/sims/expected_keepers.go new file mode 100644 index 0000000000..5a6d9c9f0f --- /dev/null +++ b/testutil/sims/expected_keepers.go @@ -0,0 +1,19 @@ +package sims + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type BankKeeper interface { + SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error +} + +// StakingKeeper is a subset of the staking keeper's public interface that +// provides the staking bond denom. It is used in arguments in this package's +// functions so that a mock staking keeper can be passed instead of the real one. +type StakingKeeper interface { + BondDenom(ctx context.Context) (string, error) +} diff --git a/testutil/sims/simulation_helpers_test.go b/testutil/sims/simulation_helpers_test.go index dd30429745..b22c86e40c 100644 --- a/testutil/sims/simulation_helpers_test.go +++ b/testutil/sims/simulation_helpers_test.go @@ -12,17 +12,21 @@ import ( "cosmossdk.io/store/metrics" "cosmossdk.io/store/rootmulti" storetypes "cosmossdk.io/store/types" - authtypes "cosmossdk.io/x/auth/types" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/types/simulation" ) +const ( + authStoreKey = "acc" + GlobalAccountNumberKey = 0x1 +) + func TestGetSimulationLog(t *testing.T) { legacyAmino := codec.NewLegacyAmino() decoders := make(simulation.StoreDecoderRegistry) - decoders[authtypes.StoreKey] = func(kvAs, kvBs kv.Pair) string { return "10" } + decoders[authStoreKey] = func(kvAs, kvBs kv.Pair) string { return "10" } tests := []struct { store string @@ -35,8 +39,8 @@ func TestGetSimulationLog(t *testing.T) { "", }, { - authtypes.StoreKey, - []kv.Pair{{Key: authtypes.GlobalAccountNumberKey, Value: legacyAmino.MustMarshal(uint64(10))}}, + authStoreKey, + []kv.Pair{{Key: []byte{GlobalAccountNumberKey}, Value: legacyAmino.MustMarshal(uint64(10))}}, "10", }, { diff --git a/testutil/sims/state_helpers.go b/testutil/sims/state_helpers.go index 3c1575a2e4..031a6125ca 100644 --- a/testutil/sims/state_helpers.go +++ b/testutil/sims/state_helpers.go @@ -20,6 +20,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" @@ -134,7 +135,7 @@ func AppStateFnWithExtendedCbs( } notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens) // edit bank state to make it have the not bonded pool tokens - bankStateBz, ok := rawState[banktypes.ModuleName] + bankStateBz, ok := rawState[testutil.BankModuleName] // TODO(fdymylja/jonathan): should we panic in this case if !ok { panic("bank genesis state is missing") @@ -162,7 +163,7 @@ func AppStateFnWithExtendedCbs( // change appState back for name, state := range map[string]proto.Message{ stakingtypes.ModuleName: stakingState, - banktypes.ModuleName: bankState, + testutil.BankModuleName: bankState, } { if moduleStateCb != nil { moduleStateCb(name, state) @@ -269,8 +270,8 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str } var authGenesis authtypes.GenesisState - if appState[authtypes.ModuleName] != nil { - cdc.MustUnmarshalJSON(appState[authtypes.ModuleName], &authGenesis) + if appState[testutil.AuthModuleName] != nil { + cdc.MustUnmarshalJSON(appState[testutil.AuthModuleName], &authGenesis) } newAccs := make([]simtypes.Account, len(authGenesis.Accounts)) diff --git a/types/query/fuzz_test.go b/types/query/fuzz_test.go index 8ce7f4715d..e3f47d38f8 100644 --- a/types/query/fuzz_test.go +++ b/types/query/fuzz_test.go @@ -9,13 +9,17 @@ import ( "cosmossdk.io/math" "cosmossdk.io/store/prefix" "cosmossdk.io/x/bank/testutil" - "cosmossdk.io/x/bank/types" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" "github.com/cosmos/cosmos-sdk/types/query" ) +const ( + balancesPrefix = 0x2 +) + type fuzzTestSuite struct { paginationTestSuite } @@ -80,12 +84,11 @@ func FuzzPagination(f *testing.F) { } // Now try to paginate it. - req := types.NewQueryAllBalancesRequest(addr1, qr, false) balResult := sdk.NewCoins() - authStore := suite.ctx.KVStore(suite.app.UnsafeFindStoreKey(types.StoreKey)) - balancesStore := prefix.NewStore(authStore, types.BalancesPrefix) + authStore := suite.ctx.KVStore(suite.app.UnsafeFindStoreKey(sdktestutil.BankModuleName)) + balancesStore := prefix.NewStore(authStore, []byte{balancesPrefix}) accountStore := prefix.NewStore(balancesStore, address.MustLengthPrefix(addr1)) - _, _ = query.Paginate(accountStore, req.Pagination, func(key, value []byte) error { + _, _ = query.Paginate(accountStore, qr, func(key, value []byte) error { var amount math.Int err := amount.Unmarshal(value) if err != nil {