feat: decouple x/nft from simapp (#12233)

This commit is contained in:
Julien Robert
2022-06-14 08:32:12 +02:00
committed by GitHub
parent b786d5dea8
commit df41b6563c
17 changed files with 469 additions and 256 deletions
+5 -2
View File
@@ -6,13 +6,15 @@ import (
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/depinject"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/keeper"
"github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
)
var (
@@ -21,7 +23,8 @@ var (
)
func TestDecodeStore(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
var cdc codec.Codec
depinject.Inject(testutil.AppConfig, &cdc)
dec := simulation.NewDecodeStore(cdc)
class := nft.Class{
+6 -3
View File
@@ -8,22 +8,25 @@ import (
"github.com/stretchr/testify/require"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/depinject"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
)
func TestRandomizedGenState(t *testing.T) {
app := simapp.Setup(t, false)
var cdc codec.Codec
depinject.Inject(testutil.AppConfig, &cdc)
s := rand.NewSource(1)
r := rand.New(s)
simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: app.AppCodec(),
Cdc: cdc,
Rand: r,
NumBonded: 3,
Accounts: simtypes.RandomAccounts(r, 3),
+2 -2
View File
@@ -7,9 +7,9 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/keeper"
@@ -96,7 +96,7 @@ func SimulateMsgSend(
Receiver: receiver.Address.String(),
}
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes)
tx, err := helpers.GenSignedMockTx(
txCfg,
[]sdk.Msg{msg},
+42 -18
View File
@@ -11,35 +11,59 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/nft"
nftkeeper "github.com/cosmos/cosmos-sdk/x/nft/keeper"
"github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)
type SimTestSuite struct {
suite.Suite
ctx sdk.Context
app *simapp.SimApp
app *runtime.App
codec codec.Codec
interfaceRegistry codectypes.InterfaceRegistry
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
nftKeeper nftkeeper.Keeper
}
func (suite *SimTestSuite) SetupTest() {
checkTx := false
app := simapp.Setup(suite.T(), checkTx)
app, err := simtestutil.Setup(
testutil.AppConfig,
&suite.codec,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.stakingKeeper,
&suite.nftKeeper,
)
suite.Require().NoError(err)
suite.app = app
suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{})
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
}
func (suite *SimTestSuite) TestWeightedOperations() {
weightedOps := simulation.WeightedOperations(
suite.app.InterfaceRegistry(),
suite.interfaceRegistry,
make(simtypes.AppParams),
suite.app.AppCodec(),
suite.app.AccountKeeper,
suite.app.BankKeeper, suite.app.NFTKeeper,
suite.codec,
suite.accountKeeper,
suite.bankKeeper,
suite.nftKeeper,
)
// setup 3 accounts
@@ -61,7 +85,7 @@ func (suite *SimTestSuite) TestWeightedOperations() {
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
// will fail
suite.Require().Equal(expected[i].weight, w.Weight(), "weight should be the same")
suite.Require().Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
suite.Require().Contains(expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
suite.Require().Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
}
}
@@ -69,14 +93,14 @@ func (suite *SimTestSuite) TestWeightedOperations() {
func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account {
accounts := simtypes.RandomAccounts(r, n)
initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000)
initAmt := suite.stakingKeeper.TokensFromConsensusPower(suite.ctx, 200000)
initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt))
// add coins to the accounts
for _, account := range accounts {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, account.Address)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
suite.Require().NoError(testutil.FundAccount(suite.app.BankKeeper, suite.ctx, account.Address, initCoins))
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, account.Address)
suite.accountKeeper.SetAccount(suite.ctx, acc)
suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, suite.ctx, account.Address, initCoins))
}
return accounts
@@ -98,13 +122,13 @@ func (suite *SimTestSuite) TestSimulateMsgSend() {
})
// execute operation
registry := suite.app.InterfaceRegistry()
op := simulation.SimulateMsgSend(codec.NewProtoCodec(registry), suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.NFTKeeper)
registry := suite.interfaceRegistry
op := simulation.SimulateMsgSend(codec.NewProtoCodec(registry), suite.accountKeeper, suite.bankKeeper, suite.nftKeeper)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "")
suite.Require().NoError(err)
var msg nft.MsgSend
suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg)
suite.codec.UnmarshalJSON(operationMsg.Msg, &msg)
suite.Require().True(operationMsg.OK)
suite.Require().Len(futureOperations, 0)
}