From 72d98da3d1e3fa6d86cb8af6c709b8d61a9f1c09 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 18 Apr 2023 13:25:02 -0400 Subject: [PATCH] refactor: update x/bank to comet v0.38 (#15872) --- x/bank/app_test.go | 15 ++++------ x/bank/bench_test.go | 65 ++++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index a47e060a96f5..b0643f1c7a7a 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -1,9 +1,11 @@ package bank_test import ( + "context" "testing" 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" @@ -144,8 +146,7 @@ func TestSendNotEnoughBalance(t *testing.T) { ctx := baseApp.NewContext(false, cmtproto.Header{}) require.NoError(t, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) - - baseApp.Commit() + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) res1 := s.AccountKeeper.GetAccount(ctx, addr1) require.NotNil(t, res1) @@ -181,8 +182,7 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { ctx := baseApp.NewContext(false, cmtproto.Header{}) require.NoError(t, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) - - baseApp.Commit() + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) res1 := s.AccountKeeper.GetAccount(ctx, addr1) require.NotNil(t, res1) @@ -261,10 +261,8 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { ctx := baseApp.NewContext(false, cmtproto.Header{}) require.NoError(t, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - require.NoError(t, testutil.FundAccount(s.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - - baseApp.Commit() + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) testCases := []appTestCase{ { @@ -306,8 +304,7 @@ func TestMsgMultiSendDependent(t *testing.T) { ctx := baseApp.NewContext(false, cmtproto.Header{}) require.NoError(t, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - - baseApp.Commit() + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) testCases := []appTestCase{ { diff --git a/x/bank/bench_test.go b/x/bank/bench_test.go index bbfed71d6f2e..1689b74420c5 100644 --- a/x/bank/bench_test.go +++ b/x/bank/bench_test.go @@ -1,6 +1,8 @@ package bank_test import ( + "context" + "fmt" "math/rand" "testing" "time" @@ -31,8 +33,9 @@ func genSequenceOfTxs(txGen client.TxConfig, numToGenerate int, priv ...cryptotypes.PrivKey, ) ([]sdk.Tx, error) { - txs := make([]sdk.Tx, numToGenerate) 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())), @@ -59,9 +62,8 @@ func genSequenceOfTxs(txGen client.TxConfig, func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023") - b.ReportAllocs() - // Add an account at genesis + acc := authtypes.BaseAccount{ Address: addr1.String(), } @@ -72,13 +74,13 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { baseApp := s.App.BaseApp ctx := baseApp.NewContext(false, cmtproto.Header{}) - // some value conceivably higher than the benchmarks would ever go require.NoError(b, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000)))) + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) - baseApp.Commit() txGen := moduletestutil.MakeTestTxConfig() + txEncoder := txGen.TxEncoder() - // Precompute all txs + // 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() @@ -88,42 +90,49 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // 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++ { - baseApp.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: height}}) - _, _, err := baseApp.SimCheck(txGen.TxEncoder(), txs[i]) + _, _, err := baseApp.SimCheck(txEncoder, txs[i]) if err != nil { - panic("something is broken in checking transaction") + panic(fmt.Errorf("failed to simulate tx: %w", err)) } - _, _, err = baseApp.SimDeliver(txGen.TxEncoder(), txs[i]) + bz, err := txEncoder(txs[i]) require.NoError(b, err) - baseApp.EndBlock(abci.RequestEndBlock{Height: height}) - baseApp.Commit() + + baseApp.FinalizeBlock( + context.TODO(), + &abci.RequestFinalizeBlock{ + Height: height, + Txs: [][]byte{bz}, + }, + ) + + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) + 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() - // Add an account at genesis + acc := authtypes.BaseAccount{ Address: addr1.String(), } - // Construct genesis state + // construct genesis state genAccs := []authtypes.GenesisAccount{&acc} s := createTestSuite(&testing.T{}, genAccs) baseApp := s.App.BaseApp ctx := baseApp.NewContext(false, cmtproto.Header{}) - // some value conceivably higher than the benchmarks would ever go require.NoError(b, testutil.FundAccount(s.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000)))) + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) - baseApp.Commit() txGen := moduletestutil.MakeTestTxConfig() + txEncoder := txGen.TxEncoder() - // Precompute all txs + // 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() @@ -133,16 +142,24 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) { // 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++ { - baseApp.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: height}}) - _, _, err := baseApp.SimCheck(txGen.TxEncoder(), txs[i]) + _, _, err := baseApp.SimCheck(txEncoder, txs[i]) if err != nil { - panic("something is broken in checking transaction") + panic(fmt.Errorf("failed to simulate tx: %w", err)) } - _, _, err = baseApp.SimDeliver(txGen.TxEncoder(), txs[i]) + bz, err := txEncoder(txs[i]) require.NoError(b, err) - baseApp.EndBlock(abci.RequestEndBlock{Height: height}) - baseApp.Commit() + + baseApp.FinalizeBlock( + context.TODO(), + &abci.RequestFinalizeBlock{ + Height: height, + Txs: [][]byte{bz}, + }, + ) + + baseApp.Commit(context.TODO(), &abci.RequestCommit{}) + height++ } }