test(wip): add machine module simulation test

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2024-11-21 10:46:07 +01:00
parent 8e87cd77db
commit a74ac48421
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
3 changed files with 281 additions and 5 deletions

View File

@ -0,0 +1,119 @@
package simulation_test
import (
"math/rand"
"testing"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
machinekeeper "github.com/planetmint/planetmint-go/x/machine/keeper"
"github.com/planetmint/planetmint-go/x/machine/simulation"
"github.com/planetmint/planetmint-go/x/machine/testutil"
"github.com/planetmint/planetmint-go/x/machine/types"
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
)
type SimTestSuite struct {
suite.Suite
r *rand.Rand
accounts []simtypes.Account
ctx sdk.Context
app *runtime.App
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
stakingKeeper *stakingkeeper.Keeper
machineKeeper *machinekeeper.Keeper
encCfg moduletestutil.TestEncodingConfig
}
func (s *SimTestSuite) SetupTest() {
s.r = rand.New(rand.NewSource(1))
accounts := simtypes.RandomAccounts(s.r, 4)
// create genesis accounts
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
accs := []simtestutil.GenesisAccount{
{GenesisAccount: acc, Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000)))},
}
// create validator set with single validator
account := accounts[0]
tmPk, err := cryptocodec.ToTmPubKeyInterface(account.PubKey)
require.NoError(s.T(), err)
validator := cmttypes.NewValidator(tmPk, 1)
startupCfg := simtestutil.DefaultStartUpConfig()
startupCfg.GenesisAccounts = accs
startupCfg.ValidatorSet = func() (*cmttypes.ValidatorSet, error) {
return cmttypes.NewValidatorSet([]*cmttypes.Validator{validator}), nil
}
var (
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
machineKeeper *machinekeeper.Keeper
)
app, err := simtestutil.SetupWithConfiguration(testutil.AppConfig, startupCfg, &bankKeeper, &accountKeeper, &stakingKeeper, &machineKeeper)
require.NoError(s.T(), err)
ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
initAmt := stakingKeeper.TokensFromConsensusPower(ctx, 200)
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
s.accounts = accounts
// remove genesis validator account
// add coins to the accounts
for _, account := range accounts[1:] {
acc := accountKeeper.NewAccountWithAddress(ctx, account.Address)
accountKeeper.SetAccount(ctx, acc)
s.Require().NoError(banktestutil.FundAccount(bankKeeper, ctx, account.Address, initCoins))
}
s.accountKeeper = accountKeeper
s.bankKeeper = bankKeeper
s.machineKeeper = machineKeeper
s.ctx = ctx
s.app = app
}
func (s *SimTestSuite) TestSimulateMsgRegisterTrustAnchor() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash}})
// execute operation
op := simulation.SimulateMsgRegisterTrustAnchor(s.accountKeeper, s.bankKeeper, *s.machineKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, s.ctx, s.accounts[1:], "")
s.Require().NoError(err)
var msg types.MsgRegisterTrustAnchor
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
s.Require().True(operationMsg.OK)
s.Require().Equal(types.TypeMsgRegisterTrustAnchor, msg.Type())
s.Require().Len(futureOperations, 0)
}
func TestSimTestSuite(t *testing.T) {
suite.Run(t, new(SimTestSuite))
}

View File

@ -1,29 +1,54 @@
package simulation
import (
"math"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/planetmint/planetmint-go/testutil/sample"
"github.com/planetmint/planetmint-go/x/machine/keeper"
"github.com/planetmint/planetmint-go/x/machine/types"
)
func SimulateMsgRegisterTrustAnchor(
_ types.AccountKeeper,
_ types.BankKeeper,
ak types.AccountKeeper,
bk types.BankKeeper,
_ keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
_, pubKey := sample.KeyPair(simtypes.RandIntBetween(r, math.MinInt, math.MaxInt))
msg := &types.MsgRegisterTrustAnchor{
Creator: simAccount.Address.String(),
Creator: simAccount.Address.String(),
TrustAnchor: &types.TrustAnchor{Pubkey: pubKey},
}
// TODO: Handling the RegisterTrustAnchor simulation
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "RegisterTrustAnchor simulation not implemented"), nil, nil
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}
fees := sdk.NewCoins()
return simulation.GenAndDeliverTx(txCtx, fees)
}
}

View File

@ -0,0 +1,132 @@
package testutil
import (
_ "github.com/cosmos/cosmos-sdk/x/auth"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
_ "github.com/cosmos/cosmos-sdk/x/bank"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
_ "github.com/cosmos/cosmos-sdk/x/genutil"
_ "github.com/cosmos/cosmos-sdk/x/mint"
_ "github.com/cosmos/cosmos-sdk/x/params"
_ "github.com/cosmos/cosmos-sdk/x/slashing"
_ "github.com/cosmos/cosmos-sdk/x/staking"
"cosmossdk.io/core/appconfig"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
bankmodulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
consensusmodulev1 "cosmossdk.io/api/cosmos/consensus/module/v1"
distrmodulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1"
mintmodulev1 "cosmossdk.io/api/cosmos/mint/module/v1"
paramsmodulev1 "cosmossdk.io/api/cosmos/params/module/v1"
slashingmodulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
)
var AppConfig = appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
{
Name: "runtime",
Config: appconfig.WrapAny(&runtimev1alpha1.Module{
AppName: "MachineApp",
BeginBlockers: []string{
minttypes.ModuleName,
distrtypes.ModuleName,
stakingtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
genutiltypes.ModuleName,
slashingtypes.ModuleName,
paramstypes.ModuleName,
consensustypes.ModuleName,
},
EndBlockers: []string{
stakingtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
genutiltypes.ModuleName,
distrtypes.ModuleName,
minttypes.ModuleName,
slashingtypes.ModuleName,
paramstypes.ModuleName,
consensustypes.ModuleName,
},
InitGenesis: []string{
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
stakingtypes.ModuleName,
minttypes.ModuleName,
slashingtypes.ModuleName,
genutiltypes.ModuleName,
paramstypes.ModuleName,
consensustypes.ModuleName,
},
}),
},
{
Name: authtypes.ModuleName,
Config: appconfig.WrapAny(&authmodulev1.Module{
Bech32Prefix: "cosmos",
ModuleAccountPermissions: []*authmodulev1.ModuleAccountPermission{
{Account: authtypes.FeeCollectorName},
{Account: distrtypes.ModuleName},
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
{Account: stakingtypes.NotBondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
},
}),
},
{
Name: banktypes.ModuleName,
Config: appconfig.WrapAny(&bankmodulev1.Module{}),
},
{
Name: stakingtypes.ModuleName,
Config: appconfig.WrapAny(&stakingmodulev1.Module{}),
},
{
Name: slashingtypes.ModuleName,
Config: appconfig.WrapAny(&slashingmodulev1.Module{}),
},
{
Name: paramstypes.ModuleName,
Config: appconfig.WrapAny(&paramsmodulev1.Module{}),
},
{
Name: consensustypes.ModuleName,
Config: appconfig.WrapAny(&consensusmodulev1.Module{}),
},
{
Name: "tx",
Config: appconfig.WrapAny(&txconfigv1.Config{}),
},
{
Name: genutiltypes.ModuleName,
Config: appconfig.WrapAny(&genutilmodulev1.Module{}),
},
{
Name: minttypes.ModuleName,
Config: appconfig.WrapAny(&mintmodulev1.Module{}),
},
{
Name: distrtypes.ModuleName,
Config: appconfig.WrapAny(&distrmodulev1.Module{}),
},
},
})