chore(sdk): remove modules as a dependency (1/n) (#19003)

This commit is contained in:
Marko 2024-01-11 08:55:50 +01:00 committed by GitHub
parent 8c6df364b7
commit 6db53cafcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 42 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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,

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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",
},
{

View File

@ -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))

View File

@ -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 {