refactor(x/bank): rm cyclic deps with gov, staking, distr (#16578)
Co-authored-by: Marko <marbar3778@yahoo.com> Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
This commit is contained in:
co-authored by
Marko
atheeshp
parent
f51992d0c4
commit
c2a07cea27
@@ -0,0 +1,447 @@
|
||||
package bank_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/depinject"
|
||||
"cosmossdk.io/log"
|
||||
sdkmath "cosmossdk.io/math"
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/configurator"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/auth"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/consensus"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/distribution"
|
||||
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/gov"
|
||||
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/params"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/staking"
|
||||
)
|
||||
|
||||
type (
|
||||
expectedBalance struct {
|
||||
addr sdk.AccAddress
|
||||
coins sdk.Coins
|
||||
}
|
||||
|
||||
appTestCase struct {
|
||||
desc string
|
||||
expSimPass bool
|
||||
expPass bool
|
||||
msgs []sdk.Msg
|
||||
accNums []uint64
|
||||
accSeqs []uint64
|
||||
privKeys []cryptotypes.PrivKey
|
||||
expectedBalances []expectedBalance
|
||||
expInError []string
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
priv1 = secp256k1.GenPrivKey()
|
||||
addr1 = sdk.AccAddress(priv1.PubKey().Address())
|
||||
priv2 = secp256k1.GenPrivKey()
|
||||
addr2 = sdk.AccAddress(priv2.PubKey().Address())
|
||||
addr3 = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
|
||||
|
||||
coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}
|
||||
halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}
|
||||
|
||||
sendMsg1 = types.NewMsgSend(addr1, addr2, coins)
|
||||
|
||||
multiSendMsg1 = &types.MsgMultiSend{
|
||||
Inputs: []types.Input{types.NewInput(addr1, coins)},
|
||||
Outputs: []types.Output{types.NewOutput(addr2, coins)},
|
||||
}
|
||||
multiSendMsg2 = &types.MsgMultiSend{
|
||||
Inputs: []types.Input{types.NewInput(addr1, coins)},
|
||||
Outputs: []types.Output{
|
||||
types.NewOutput(addr2, halfCoins),
|
||||
types.NewOutput(addr3, halfCoins),
|
||||
},
|
||||
}
|
||||
multiSendMsg3 = &types.MsgMultiSend{
|
||||
Inputs: []types.Input{types.NewInput(addr2, coins)},
|
||||
Outputs: []types.Output{
|
||||
types.NewOutput(addr1, coins),
|
||||
},
|
||||
}
|
||||
multiSendMsg4 = &types.MsgMultiSend{
|
||||
Inputs: []types.Input{types.NewInput(addr1, coins)},
|
||||
Outputs: []types.Output{
|
||||
types.NewOutput(moduleAccAddr, coins),
|
||||
},
|
||||
}
|
||||
invalidMultiSendMsg = &types.MsgMultiSend{
|
||||
Inputs: []types.Input{types.NewInput(addr1, coins), types.NewInput(addr2, coins)},
|
||||
Outputs: []types.Output{},
|
||||
}
|
||||
)
|
||||
|
||||
type suite struct {
|
||||
BankKeeper bankkeeper.Keeper
|
||||
AccountKeeper types.AccountKeeper
|
||||
DistributionKeeper distrkeeper.Keeper
|
||||
App *runtime.App
|
||||
}
|
||||
|
||||
func createTestSuite(t *testing.T, genesisAccounts []authtypes.GenesisAccount) suite {
|
||||
res := suite{}
|
||||
|
||||
var genAccounts []simtestutil.GenesisAccount
|
||||
for _, acc := range genesisAccounts {
|
||||
genAccounts = append(genAccounts, simtestutil.GenesisAccount{GenesisAccount: acc})
|
||||
}
|
||||
|
||||
startupCfg := simtestutil.DefaultStartUpConfig()
|
||||
startupCfg.GenesisAccounts = genAccounts
|
||||
|
||||
app, err := simtestutil.SetupWithConfiguration(
|
||||
depinject.Configs(
|
||||
configurator.NewAppConfig(
|
||||
configurator.ParamsModule(),
|
||||
configurator.AuthModule(),
|
||||
configurator.StakingModule(),
|
||||
configurator.TxModule(),
|
||||
configurator.ConsensusModule(),
|
||||
configurator.BankModule(),
|
||||
configurator.GovModule(),
|
||||
configurator.DistributionModule(),
|
||||
),
|
||||
depinject.Supply(log.NewNopLogger()),
|
||||
),
|
||||
startupCfg, &res.BankKeeper, &res.AccountKeeper, &res.DistributionKeeper)
|
||||
|
||||
res.App = app
|
||||
|
||||
require.NoError(t, err)
|
||||
return res
|
||||
}
|
||||
|
||||
// CheckBalance checks the balance of an account.
|
||||
func checkBalance(t *testing.T, baseApp *baseapp.BaseApp, addr sdk.AccAddress, balances sdk.Coins, keeper bankkeeper.Keeper) {
|
||||
ctxCheck := baseApp.NewContext(true)
|
||||
keeperBalances := keeper.GetAllBalances(ctxCheck, addr)
|
||||
require.True(t, balances.Equal(keeperBalances))
|
||||
}
|
||||
|
||||
func TestSendNotEnoughBalance(t *testing.T) {
|
||||
acc := &authtypes.BaseAccount{
|
||||
Address: addr1.String(),
|
||||
}
|
||||
|
||||
genAccs := []authtypes.GenesisAccount{acc}
|
||||
s := createTestSuite(t, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))
|
||||
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
|
||||
require.NoError(t, err)
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(t, err)
|
||||
|
||||
res1 := s.AccountKeeper.GetAccount(ctx, addr1)
|
||||
require.NotNil(t, res1)
|
||||
require.Equal(t, acc, res1.(*authtypes.BaseAccount))
|
||||
|
||||
origAccNum := res1.GetAccountNumber()
|
||||
origSeq := res1.GetSequence()
|
||||
|
||||
sendMsg := types.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)})
|
||||
header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1}
|
||||
txConfig := moduletestutil.MakeTestTxConfig()
|
||||
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, []sdk.Msg{sendMsg}, "", []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1)
|
||||
require.Error(t, err)
|
||||
|
||||
checkBalance(t, baseApp, addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 67)}, s.BankKeeper)
|
||||
|
||||
ctx2 := baseApp.NewContext(true)
|
||||
res2 := s.AccountKeeper.GetAccount(ctx2, addr1)
|
||||
require.NotNil(t, res2)
|
||||
|
||||
require.Equal(t, origAccNum, res2.GetAccountNumber())
|
||||
require.Equal(t, origSeq+1, res2.GetSequence())
|
||||
}
|
||||
|
||||
func TestMsgMultiSendWithAccounts(t *testing.T) {
|
||||
acc := &authtypes.BaseAccount{
|
||||
Address: addr1.String(),
|
||||
}
|
||||
|
||||
genAccs := []authtypes.GenesisAccount{acc}
|
||||
s := createTestSuite(t, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))
|
||||
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
|
||||
require.NoError(t, err)
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(t, err)
|
||||
|
||||
res1 := s.AccountKeeper.GetAccount(ctx, addr1)
|
||||
require.NotNil(t, res1)
|
||||
require.Equal(t, acc, res1.(*authtypes.BaseAccount))
|
||||
|
||||
testCases := []appTestCase{
|
||||
{
|
||||
desc: "make a valid tx",
|
||||
msgs: []sdk.Msg{multiSendMsg1},
|
||||
accNums: []uint64{0},
|
||||
accSeqs: []uint64{0},
|
||||
expSimPass: true,
|
||||
expPass: true,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
expectedBalances: []expectedBalance{
|
||||
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 57)}},
|
||||
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "wrong accNum should pass Simulate, but not Deliver",
|
||||
msgs: []sdk.Msg{multiSendMsg1, multiSendMsg2},
|
||||
accNums: []uint64{1}, // wrong account number
|
||||
accSeqs: []uint64{1},
|
||||
expSimPass: true, // doesn't check signature
|
||||
expPass: false,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
},
|
||||
{
|
||||
desc: "wrong accSeq should not pass Simulate",
|
||||
msgs: []sdk.Msg{multiSendMsg4},
|
||||
accNums: []uint64{0},
|
||||
accSeqs: []uint64{0}, // wrong account sequence
|
||||
expSimPass: false,
|
||||
expPass: false,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
},
|
||||
{
|
||||
desc: "multiple inputs not allowed",
|
||||
msgs: []sdk.Msg{invalidMultiSendMsg},
|
||||
accNums: []uint64{0},
|
||||
accSeqs: []uint64{0},
|
||||
expSimPass: false,
|
||||
expPass: false,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Logf("testing %s", tc.desc)
|
||||
header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1}
|
||||
txConfig := moduletestutil.MakeTestTxConfig()
|
||||
_, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
||||
if tc.expPass {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
for _, eb := range tc.expectedBalances {
|
||||
checkBalance(t, baseApp, eb.addr, eb.coins, s.BankKeeper)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsgMultiSendMultipleOut(t *testing.T) {
|
||||
acc1 := &authtypes.BaseAccount{
|
||||
Address: addr1.String(),
|
||||
}
|
||||
acc2 := &authtypes.BaseAccount{
|
||||
Address: addr2.String(),
|
||||
}
|
||||
|
||||
genAccs := []authtypes.GenesisAccount{acc1, acc2}
|
||||
s := createTestSuite(t, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
|
||||
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
|
||||
require.NoError(t, err)
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(t, err)
|
||||
|
||||
testCases := []appTestCase{
|
||||
{
|
||||
msgs: []sdk.Msg{multiSendMsg2},
|
||||
accNums: []uint64{0},
|
||||
accSeqs: []uint64{0},
|
||||
expSimPass: true,
|
||||
expPass: true,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
expectedBalances: []expectedBalance{
|
||||
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
||||
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 47)}},
|
||||
{addr3, sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1}
|
||||
txConfig := moduletestutil.MakeTestTxConfig()
|
||||
_, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, eb := range tc.expectedBalances {
|
||||
checkBalance(t, baseApp, eb.addr, eb.coins, s.BankKeeper)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsgMultiSendDependent(t *testing.T) {
|
||||
acc1 := authtypes.NewBaseAccountWithAddress(addr1)
|
||||
acc2 := authtypes.NewBaseAccountWithAddress(addr2)
|
||||
err := acc2.SetAccountNumber(1)
|
||||
require.NoError(t, err)
|
||||
|
||||
genAccs := []authtypes.GenesisAccount{acc1, acc2}
|
||||
s := createTestSuite(t, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
|
||||
_, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
|
||||
require.NoError(t, err)
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(t, err)
|
||||
|
||||
testCases := []appTestCase{
|
||||
{
|
||||
msgs: []sdk.Msg{multiSendMsg1},
|
||||
accNums: []uint64{0},
|
||||
accSeqs: []uint64{0},
|
||||
expSimPass: true,
|
||||
expPass: true,
|
||||
privKeys: []cryptotypes.PrivKey{priv1},
|
||||
expectedBalances: []expectedBalance{
|
||||
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
||||
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}},
|
||||
},
|
||||
},
|
||||
{
|
||||
msgs: []sdk.Msg{multiSendMsg3},
|
||||
accNums: []uint64{1},
|
||||
accSeqs: []uint64{0},
|
||||
expSimPass: true,
|
||||
expPass: true,
|
||||
privKeys: []cryptotypes.PrivKey{priv2},
|
||||
expectedBalances: []expectedBalance{
|
||||
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1}
|
||||
txConfig := moduletestutil.MakeTestTxConfig()
|
||||
_, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, eb := range tc.expectedBalances {
|
||||
checkBalance(t, baseApp, eb.addr, eb.coins, s.BankKeeper)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsgSetSendEnabled(t *testing.T) {
|
||||
acc1 := authtypes.NewBaseAccountWithAddress(addr1)
|
||||
|
||||
genAccs := []authtypes.GenesisAccount{acc1}
|
||||
s := createTestSuite(t, genAccs)
|
||||
|
||||
ctx := s.App.BaseApp.NewContext(false)
|
||||
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 101))))
|
||||
addr1Str := addr1.String()
|
||||
govAddr := s.BankKeeper.GetAuthority()
|
||||
goodGovProp, err := govv1.NewMsgSubmitProposal(
|
||||
[]sdk.Msg{
|
||||
types.NewMsgSetSendEnabled(govAddr, nil, nil),
|
||||
},
|
||||
sdk.Coins{{Denom: "foocoin", Amount: sdkmath.NewInt(5)}},
|
||||
addr1Str,
|
||||
"set default send enabled to true",
|
||||
"Change send enabled",
|
||||
"Modify send enabled and set to true",
|
||||
false,
|
||||
)
|
||||
require.NoError(t, err, "making goodGovProp")
|
||||
|
||||
testCases := []appTestCase{
|
||||
{
|
||||
desc: "wrong authority",
|
||||
expSimPass: false,
|
||||
expPass: false,
|
||||
msgs: []sdk.Msg{
|
||||
types.NewMsgSetSendEnabled(addr1Str, nil, nil),
|
||||
},
|
||||
accSeqs: []uint64{0},
|
||||
expInError: []string{
|
||||
"invalid authority",
|
||||
"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
|
||||
addr1Str,
|
||||
"invalid signer",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "right authority wrong signer",
|
||||
expSimPass: false,
|
||||
expPass: false,
|
||||
msgs: []sdk.Msg{
|
||||
types.NewMsgSetSendEnabled(govAddr, nil, nil),
|
||||
},
|
||||
accSeqs: []uint64{1}, // wrong signer, so this sequence doesn't actually get used.
|
||||
expInError: []string{
|
||||
"pubKey does not match signer address",
|
||||
govAddr,
|
||||
"with signer index: 0",
|
||||
"invalid pubkey",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "submitted good as gov prop",
|
||||
expSimPass: true,
|
||||
expPass: true,
|
||||
msgs: []sdk.Msg{
|
||||
goodGovProp,
|
||||
},
|
||||
accSeqs: []uint64{1},
|
||||
expInError: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(tt *testing.T) {
|
||||
header := cmtproto.Header{Height: s.App.LastBlockHeight() + 1}
|
||||
txGen := moduletestutil.MakeTestTxConfig()
|
||||
_, _, err = simtestutil.SignCheckDeliver(tt, txGen, s.App.BaseApp, header, tc.msgs, "", []uint64{0}, tc.accSeqs, tc.expSimPass, tc.expPass, priv1)
|
||||
if len(tc.expInError) > 0 {
|
||||
require.Error(tt, err)
|
||||
for _, exp := range tc.expInError {
|
||||
assert.ErrorContains(tt, err, exp)
|
||||
}
|
||||
} else {
|
||||
require.NoError(tt, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package bank_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
_ "github.com/cosmos/cosmos-sdk/x/bank"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
var moduleAccAddr = authtypes.NewModuleAddress(stakingtypes.BondedPoolName)
|
||||
|
||||
// GenSequenceOfTxs generates a set of signed transactions of messages, such
|
||||
// that they differ only by having the sequence numbers incremented between
|
||||
// every transaction.
|
||||
func genSequenceOfTxs(txGen client.TxConfig,
|
||||
msgs []sdk.Msg,
|
||||
accNums []uint64,
|
||||
initSeqNums []uint64,
|
||||
numToGenerate int,
|
||||
priv ...cryptotypes.PrivKey,
|
||||
) ([]sdk.Tx, error) {
|
||||
var err error
|
||||
|
||||
txs := make([]sdk.Tx, numToGenerate)
|
||||
for i := 0; i < numToGenerate; i++ {
|
||||
txs[i], err = simtestutil.GenSignedMockTx(
|
||||
rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
txGen,
|
||||
msgs,
|
||||
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
|
||||
simtestutil.DefaultGenTxGas,
|
||||
"",
|
||||
accNums,
|
||||
initSeqNums,
|
||||
priv...,
|
||||
)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
for i := 0; i < len(initSeqNums); i++ {
|
||||
initSeqNums[i]++
|
||||
}
|
||||
}
|
||||
|
||||
return txs, err
|
||||
}
|
||||
|
||||
func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
|
||||
// b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023")
|
||||
b.ReportAllocs()
|
||||
|
||||
acc := authtypes.BaseAccount{
|
||||
Address: addr1.String(),
|
||||
}
|
||||
|
||||
// construct genesis state
|
||||
genAccs := []authtypes.GenesisAccount{&acc}
|
||||
s := createTestSuite(&testing.T{}, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
|
||||
require.NoError(b, err)
|
||||
|
||||
require.NoError(b, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
|
||||
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(b, err)
|
||||
|
||||
txGen := moduletestutil.MakeTestTxConfig()
|
||||
txEncoder := txGen.TxEncoder()
|
||||
|
||||
// pre-compute all txs
|
||||
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
|
||||
require.NoError(b, err)
|
||||
b.ResetTimer()
|
||||
|
||||
height := int64(2)
|
||||
|
||||
// Run this with a profiler, so its easy to distinguish what time comes from
|
||||
// Committing, and what time comes from Check/Deliver Tx.
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, err := baseApp.SimCheck(txEncoder, txs[i])
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to simulate tx: %w", err))
|
||||
}
|
||||
|
||||
bz, err := txEncoder(txs[i])
|
||||
require.NoError(b, err)
|
||||
|
||||
_, err = baseApp.FinalizeBlock(
|
||||
&abci.RequestFinalizeBlock{
|
||||
Height: height,
|
||||
Txs: [][]byte{bz},
|
||||
},
|
||||
)
|
||||
require.NoError(b, err)
|
||||
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(b, err)
|
||||
|
||||
height++
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
|
||||
// b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023")
|
||||
b.ReportAllocs()
|
||||
|
||||
acc := authtypes.BaseAccount{
|
||||
Address: addr1.String(),
|
||||
}
|
||||
|
||||
// construct genesis state
|
||||
genAccs := []authtypes.GenesisAccount{&acc}
|
||||
s := createTestSuite(&testing.T{}, genAccs)
|
||||
baseApp := s.App.BaseApp
|
||||
ctx := baseApp.NewContext(false)
|
||||
|
||||
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
|
||||
require.NoError(b, err)
|
||||
|
||||
require.NoError(b, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
|
||||
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(b, err)
|
||||
|
||||
txGen := moduletestutil.MakeTestTxConfig()
|
||||
txEncoder := txGen.TxEncoder()
|
||||
|
||||
// pre-compute all txs
|
||||
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
|
||||
require.NoError(b, err)
|
||||
b.ResetTimer()
|
||||
|
||||
height := int64(2)
|
||||
|
||||
// Run this with a profiler, so its easy to distinguish what time comes from
|
||||
// Committing, and what time comes from Check/Deliver Tx.
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, err := baseApp.SimCheck(txEncoder, txs[i])
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to simulate tx: %w", err))
|
||||
}
|
||||
|
||||
bz, err := txEncoder(txs[i])
|
||||
require.NoError(b, err)
|
||||
|
||||
_, err = baseApp.FinalizeBlock(
|
||||
&abci.RequestFinalizeBlock{
|
||||
Height: height,
|
||||
Txs: [][]byte{bz},
|
||||
},
|
||||
)
|
||||
require.NoError(b, err)
|
||||
|
||||
_, err = baseApp.Commit()
|
||||
require.NoError(b, err)
|
||||
|
||||
height++
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user