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

This commit is contained in:
Marko
2024-01-11 07:55:50 +00:00
committed by GitHub
parent 8c6df364b7
commit 6db53cafcb
9 changed files with 73 additions and 42 deletions
+5 -4
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 {
+5 -14
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)
}
+19
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)
}
+8 -4
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",
},
{
+5 -4
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))