From a48a75fe918783fb4ae71f26c657254f021ec6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Toledano?= Date: Sat, 7 Sep 2024 09:44:42 +0200 Subject: [PATCH] refactor(baseapp): remove AccAddress.String() (#21521) --- baseapp/abci_test.go | 172 ++++++++++++++++------------- baseapp/abci_utils.go | 7 +- baseapp/abci_utils_test.go | 42 ++++--- baseapp/baseapp_test.go | 30 +++-- baseapp/msg_service_router_test.go | 4 +- baseapp/streaming_test.go | 2 +- baseapp/utils_test.go | 24 ++-- 7 files changed, 160 insertions(+), 121 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 7a5fde3ba17d..68dc99bcc8c0 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -39,6 +39,7 @@ import ( baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil" "github.com/cosmos/cosmos-sdk/baseapp/testutil/mock" "github.com/cosmos/cosmos-sdk/codec" + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -600,7 +601,7 @@ func TestABCI_CheckTx(t *testing.T) { require.NoError(t, err) for i := int64(0); i < nTxs; i++ { - tx := newTxCounter(t, suite.txConfig, i, 0) // no messages + tx := newTxCounter(t, suite.txConfig, suite.ac, i, 0) // no messages txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -655,7 +656,7 @@ func TestABCI_FinalizeBlock_DeliverTx(t *testing.T) { txs := [][]byte{} for i := 0; i < txPerHeight; i++ { counter := int64(blockN*txPerHeight + i) - tx := newTxCounter(t, suite.txConfig, counter, counter) + tx := newTxCounter(t, suite.txConfig, suite.ac, counter, counter) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -703,7 +704,7 @@ func TestABCI_FinalizeBlock_MultiMsg(t *testing.T) { // run a multi-msg tx // with all msgs the same route - tx := newTxCounter(t, suite.txConfig, 0, 0, 1, 2) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0, 1, 2) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -724,13 +725,15 @@ func TestABCI_FinalizeBlock_MultiMsg(t *testing.T) { require.Equal(t, int64(3), msgCounter) // replace the second message with a Counter2 - tx = newTxCounter(t, suite.txConfig, 1, 3) + tx = newTxCounter(t, suite.txConfig, suite.ac, 1, 3) builder := suite.txConfig.NewTxBuilder() msgs := tx.GetMsgs() _, _, addr := testdata.KeyTestPubAddr() - msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 0, Signer: addr.String()}) - msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 1, Signer: addr.String()}) + addrStr, err := suite.ac.BytesToString(addr) + require.NoError(t, err) + msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 0, Signer: addrStr}) + msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 1, Signer: addrStr}) err = builder.SetMsgs(msgs...) require.NoError(t, err) @@ -789,8 +792,13 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { baseapptestutil.RegisterNestedMessagesServer(suite.baseApp.MsgServiceRouter(), NestedMessgesServerImpl{}) baseapptestutil.RegisterSendServer(suite.baseApp.MsgServiceRouter(), SendServerImpl{}) + ac := codectestutil.CodecOptions{}.GetAddressCodec() _, _, addr := testdata.KeyTestPubAddr() _, _, toAddr := testdata.KeyTestPubAddr() + addrStr, err := ac.BytesToString(addr) + require.NoError(t, err) + toAddrStr, err := ac.BytesToString(toAddr) + require.NoError(t, err) tests := []struct { name string message sdk.Msg @@ -799,16 +807,16 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "ok nested message", message: &baseapptestutil.MsgSend{ - From: addr.String(), - To: toAddr.String(), + From: addrStr, + To: toAddrStr, Amount: "10000stake", }, }, { name: "different signers", message: &baseapptestutil.MsgSend{ - From: toAddr.String(), - To: addr.String(), + From: toAddrStr, + To: addrStr, Amount: "10000stake", }, wantErr: true, @@ -817,7 +825,7 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { name: "empty from", message: &baseapptestutil.MsgSend{ From: "", - To: toAddr.String(), + To: toAddrStr, Amount: "10000stake", }, wantErr: true, @@ -825,7 +833,7 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "empty to", message: &baseapptestutil.MsgSend{ - From: addr.String(), + From: addrStr, To: "", Amount: "10000stake", }, @@ -834,8 +842,8 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "negative amount", message: &baseapptestutil.MsgSend{ - From: addr.String(), - To: toAddr.String(), + From: addrStr, + To: toAddrStr, Amount: "-10000stake", }, wantErr: true, @@ -843,11 +851,11 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "with nested messages", message: &baseapptestutil.MsgNestedMessages{ - Signer: addr.String(), + Signer: addrStr, Messages: []*any.Any{ anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ - From: addr.String(), - To: toAddr.String(), + From: addrStr, + To: toAddrStr, Amount: "10000stake", }), }, @@ -856,11 +864,11 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "with invalid nested messages", message: &baseapptestutil.MsgNestedMessages{ - Signer: addr.String(), + Signer: addrStr, Messages: []*any.Any{ anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ From: "", - To: toAddr.String(), + To: toAddrStr, Amount: "10000stake", }), }, @@ -870,11 +878,11 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { { name: "with different signer ", message: &baseapptestutil.MsgNestedMessages{ - Signer: addr.String(), + Signer: addrStr, Messages: []*any.Any{ anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ - From: toAddr.String(), - To: addr.String(), + From: toAddrStr, + To: addrStr, Amount: "10000stake", }), }, @@ -894,7 +902,7 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { msg := &baseapptestutil.MsgNestedMessages{ Messages: nestedMessages, - Signer: addr.String(), + Signer: addrStr, } builder := suite.txConfig.NewTxBuilder() @@ -926,9 +934,13 @@ func TestABCI_Query_SimulateNestedMessagesGas(t *testing.T) { }) } + ac := codectestutil.CodecOptions{}.GetAddressCodec() _, _, addr := testdata.KeyTestPubAddr() _, _, toAddr := testdata.KeyTestPubAddr() - + addrStr, err := ac.BytesToString(addr) + require.NoError(t, err) + toAddrStr, err := ac.BytesToString(toAddr) + require.NoError(t, err) tests := []struct { name string suite *BaseAppSuite @@ -939,8 +951,8 @@ func TestABCI_Query_SimulateNestedMessagesGas(t *testing.T) { name: "don't add gas", suite: NewBaseAppSuite(t, anteOpt), message: &baseapptestutil.MsgSend{ - From: addr.String(), - To: toAddr.String(), + From: addrStr, + To: toAddrStr, Amount: "10000stake", }, consumedGas: 5, @@ -949,8 +961,8 @@ func TestABCI_Query_SimulateNestedMessagesGas(t *testing.T) { name: "add gas", suite: NewBaseAppSuite(t, anteOpt, baseapp.SetIncludeNestedMsgsGas([]sdk.Msg{&baseapptestutil.MsgNestedMessages{}})), message: &baseapptestutil.MsgSend{ - From: addr.String(), - To: toAddr.String(), + From: addrStr, + To: toAddrStr, Amount: "10000stake", }, consumedGas: 10, @@ -976,7 +988,7 @@ func TestABCI_Query_SimulateNestedMessagesGas(t *testing.T) { msg := &baseapptestutil.MsgNestedMessages{ Messages: nestedMessages, - Signer: addr.String(), + Signer: addrStr, } builder := tt.suite.txConfig.NewTxBuilder() @@ -1017,7 +1029,7 @@ func TestABCI_Query_SimulateTx(t *testing.T) { for blockN := 0; blockN < nBlocks; blockN++ { count := int64(blockN + 1) - tx := newTxCounter(t, suite.txConfig, count, count) + tx := newTxCounter(t, suite.txConfig, suite.ac, count, count) txBytes, err := suite.txConfig.TxEncoder()(tx) require.Nil(t, err) @@ -1108,14 +1120,14 @@ func TestABCI_InvalidTransaction(t *testing.T) { tx signing.Tx fail bool }{ - {newTxCounter(t, suite.txConfig, 0, 0), false}, - {newTxCounter(t, suite.txConfig, -1, 0), false}, - {newTxCounter(t, suite.txConfig, 100, 100), false}, - {newTxCounter(t, suite.txConfig, 100, 5, 4, 3, 2, 1), false}, - - {newTxCounter(t, suite.txConfig, 0, -1), true}, - {newTxCounter(t, suite.txConfig, 0, 1, -2), true}, - {newTxCounter(t, suite.txConfig, 0, 1, 2, -10, 5), true}, + {newTxCounter(t, suite.txConfig, suite.ac, 0, 0), false}, + {newTxCounter(t, suite.txConfig, suite.ac, -1, 0), false}, + {newTxCounter(t, suite.txConfig, suite.ac, 100, 100), false}, + {newTxCounter(t, suite.txConfig, suite.ac, 100, 5, 4, 3, 2, 1), false}, + + {newTxCounter(t, suite.txConfig, suite.ac, 0, -1), true}, + {newTxCounter(t, suite.txConfig, suite.ac, 0, 1, -2), true}, + {newTxCounter(t, suite.txConfig, suite.ac, 0, 1, 2, -10, 5), true}, } for _, testCase := range testCases { @@ -1138,7 +1150,9 @@ func TestABCI_InvalidTransaction(t *testing.T) { { txBuilder := suite.txConfig.NewTxBuilder() _, _, addr := testdata.KeyTestPubAddr() - err = txBuilder.SetMsgs(&baseapptestutil.MsgCounter2{Signer: addr.String()}) + addrStr, err := suite.ac.BytesToString(addr) + require.NoError(t, err) + err = txBuilder.SetMsgs(&baseapptestutil.MsgCounter2{Signer: addrStr}) require.NoError(t, err) setTxSignature(t, txBuilder, 0) unknownRouteTx := txBuilder.GetTx() @@ -1153,8 +1167,8 @@ func TestABCI_InvalidTransaction(t *testing.T) { txBuilder = suite.txConfig.NewTxBuilder() err = txBuilder.SetMsgs( - &baseapptestutil.MsgCounter{Signer: addr.String()}, - &baseapptestutil.MsgCounter2{Signer: addr.String()}, + &baseapptestutil.MsgCounter{Signer: addrStr}, + &baseapptestutil.MsgCounter2{Signer: addrStr}, ) require.NoError(t, err) setTxSignature(t, txBuilder, 0) @@ -1219,19 +1233,19 @@ func TestABCI_TxGasLimits(t *testing.T) { gasUsed int64 fail bool }{ - {newTxCounter(t, suite.txConfig, 0, 0), 0, false}, - {newTxCounter(t, suite.txConfig, 1, 1), 2, false}, - {newTxCounter(t, suite.txConfig, 9, 1), 10, false}, - {newTxCounter(t, suite.txConfig, 1, 9), 10, false}, - {newTxCounter(t, suite.txConfig, 10, 0), 10, false}, + {newTxCounter(t, suite.txConfig, suite.ac, 0, 0), 0, false}, + {newTxCounter(t, suite.txConfig, suite.ac, 1, 1), 2, false}, + {newTxCounter(t, suite.txConfig, suite.ac, 9, 1), 10, false}, + {newTxCounter(t, suite.txConfig, suite.ac, 1, 9), 10, false}, + {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 10, false}, - {newTxCounter(t, suite.txConfig, 9, 2), 11, true}, - {newTxCounter(t, suite.txConfig, 2, 9), 11, true}, - // {newTxCounter(t, suite.txConfig, 9, 1, 1), 11, true}, - // {newTxCounter(t, suite.txConfig, 1, 8, 1, 1), 11, true}, - // {newTxCounter(t, suite.txConfig, 11, 0), 11, true}, - // {newTxCounter(t, suite.txConfig, 0, 11), 11, true}, - // {newTxCounter(t, suite.txConfig, 0, 5, 11), 16, true}, + {newTxCounter(t, suite.txConfig, suite.ac, 9, 2), 11, true}, + {newTxCounter(t, suite.txConfig, suite.ac, 2, 9), 11, true}, + // {newTxCounter(t, suite.txConfig, suite.ac, 9, 1, 1), 11, true}, + // {newTxCounter(t, suite.txConfig, suite.ac, 1, 8, 1, 1), 11, true}, + // {newTxCounter(t, suite.txConfig, suite.ac, 11, 0), 11, true}, + // {newTxCounter(t, suite.txConfig, suite.ac, 0, 11), 11, true}, + // {newTxCounter(t, suite.txConfig, suite.ac, 0, 5, 11), 16, true}, } txs := [][]byte{} @@ -1312,16 +1326,16 @@ func TestABCI_MaxBlockGasLimits(t *testing.T) { fail bool failAfterDeliver int }{ - {newTxCounter(t, suite.txConfig, 0, 0), 0, 0, false, 0}, - {newTxCounter(t, suite.txConfig, 9, 1), 2, 10, false, 0}, - {newTxCounter(t, suite.txConfig, 10, 0), 3, 10, false, 0}, - {newTxCounter(t, suite.txConfig, 10, 0), 10, 10, false, 0}, - {newTxCounter(t, suite.txConfig, 2, 7), 11, 9, false, 0}, - // {newTxCounter(t, suite.txConfig, 10, 0), 10, 10, false, 0}, // hit the limit but pass + {newTxCounter(t, suite.txConfig, suite.ac, 0, 0), 0, 0, false, 0}, + {newTxCounter(t, suite.txConfig, suite.ac, 9, 1), 2, 10, false, 0}, + {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 3, 10, false, 0}, + {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 10, 10, false, 0}, + {newTxCounter(t, suite.txConfig, suite.ac, 2, 7), 11, 9, false, 0}, + // {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 10, 10, false, 0}, // hit the limit but pass - // {newTxCounter(t, suite.txConfig, 10, 0), 11, 10, true, 10}, - // {newTxCounter(t, suite.txConfig, 10, 0), 15, 10, true, 10}, - // {newTxCounter(t, suite.txConfig, 9, 0), 12, 9, true, 11}, // fly past the limit + // {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 11, 10, true, 10}, + // {newTxCounter(t, suite.txConfig, suite.ac, 10, 0), 15, 10, true, 10}, + // {newTxCounter(t, suite.txConfig, suite.ac, 9, 0), 12, 9, true, 11}, // fly past the limit } for i, tc := range testCases { @@ -1403,13 +1417,13 @@ func TestABCI_GasConsumptionBadTx(t *testing.T) { }) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 5, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 5, 0) tx = setFailOnAnte(t, suite.txConfig, tx, true) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) // require next tx to fail due to black gas limit - tx = newTxCounter(t, suite.txConfig, 5, 0) + tx = newTxCounter(t, suite.txConfig, suite.ac, 5, 0) txBytes2, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -1445,7 +1459,7 @@ func TestABCI_Query(t *testing.T) { Path: "/store/key1/key", Data: key, } - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) // query is empty before we do anything res, err := suite.baseApp.Query(context.TODO(), &query) @@ -1656,7 +1670,7 @@ func TestABCI_Proposal_HappyPath(t *testing.T) { }) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 0, 1) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 1) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -1667,7 +1681,7 @@ func TestABCI_Proposal_HappyPath(t *testing.T) { _, err = suite.baseApp.CheckTx(&reqCheckTx) require.NoError(t, err) - tx2 := newTxCounter(t, suite.txConfig, 1, 1) + tx2 := newTxCounter(t, suite.txConfig, suite.ac, 1, 1) tx2Bytes, err := suite.txConfig.TxEncoder()(tx2) require.NoError(t, err) @@ -1837,7 +1851,7 @@ func TestABCI_PrepareProposal_ReachedMaxBytes(t *testing.T) { var expectedTxBytes int64 for i := 0; i < 100; i++ { - tx2 := newTxCounter(t, suite.txConfig, int64(i), int64(i)) + tx2 := newTxCounter(t, suite.txConfig, suite.ac, int64(i), int64(i)) err := pool.Insert(sdk.Context{}, tx2) require.NoError(t, err) @@ -1873,7 +1887,7 @@ func TestABCI_PrepareProposal_BadEncoding(t *testing.T) { }) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) err = pool.Insert(sdk.Context{}, tx) require.NoError(t, err) @@ -1901,8 +1915,10 @@ func TestABCI_PrepareProposal_OverGasUnderBytes(t *testing.T) { require.NoError(t, err) // insert 100 txs, each with a gas limit of 10 _, _, addr := testdata.KeyTestPubAddr() + addrStr, err := suite.ac.BytesToString(addr) + require.NoError(t, err) for i := int64(0); i < 100; i++ { - msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addr.String()} + msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addrStr} msgs := []sdk.Msg{msg} builder := suite.txConfig.NewTxBuilder() @@ -1941,8 +1957,10 @@ func TestABCI_PrepareProposal_MaxGas(t *testing.T) { require.NoError(t, err) // insert 100 txs, each with a gas limit of 10 _, _, addr := testdata.KeyTestPubAddr() + addrStr, err := suite.ac.BytesToString(addr) + require.NoError(t, err) for i := int64(0); i < 100; i++ { - msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addr.String()} + msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addrStr} msgs := []sdk.Msg{msg} builder := suite.txConfig.NewTxBuilder() @@ -1980,7 +1998,7 @@ func TestABCI_PrepareProposal_Failures(t *testing.T) { }) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -1992,7 +2010,7 @@ func TestABCI_PrepareProposal_Failures(t *testing.T) { require.NoError(t, err) require.True(t, checkTxRes.IsOK()) - failTx := newTxCounter(t, suite.txConfig, 1, 1) + failTx := newTxCounter(t, suite.txConfig, suite.ac, 1, 1) failTx = setFailOnAnte(t, suite.txConfig, failTx, true) err = pool.Insert(sdk.Context{}, failTx) @@ -2049,7 +2067,7 @@ func TestABCI_PrepareProposal_VoteExtensions(t *testing.T) { pk, err := cryptocodec.FromCmtProtoPublicKey(tmPk) require.NoError(t, err) - consAddr := sdk.ConsAddress(addr.String()) + consAddr := sdk.ConsAddress(addr) valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), consAddr.Bytes()).Return(pk, nil) // set up baseapp @@ -2631,7 +2649,7 @@ func TestOptimisticExecution(t *testing.T) { // run 50 blocks for i := 0; i < 50; i++ { - tx := newTxCounter(t, suite.txConfig, 0, 1) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 1) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -2689,7 +2707,7 @@ func TestABCI_Proposal_FailReCheckTx(t *testing.T) { }) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 0, 1) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 1) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -2700,7 +2718,7 @@ func TestABCI_Proposal_FailReCheckTx(t *testing.T) { _, err = suite.baseApp.CheckTx(&reqCheckTx) require.NoError(t, err) - tx2 := newTxCounter(t, suite.txConfig, 1, 1) + tx2 := newTxCounter(t, suite.txConfig, suite.ac, 1, 1) tx2Bytes, err := suite.txConfig.TxEncoder()(tx2) require.NoError(t, err) diff --git a/baseapp/abci_utils.go b/baseapp/abci_utils.go index 4fa068b3c9a4..f379415e7849 100644 --- a/baseapp/abci_utils.go +++ b/baseapp/abci_utils.go @@ -304,9 +304,10 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan shouldAdd := true txSignersSeqs := make(map[string]uint64) for _, signer := range signerData { - seq, ok := selectedTxsSignersSeqs[signer.Signer.String()] + signerKey := string(signer.Signer) + seq, ok := selectedTxsSignersSeqs[signerKey] if !ok { - txSignersSeqs[signer.Signer.String()] = signer.Sequence + txSignersSeqs[signerKey] = signer.Sequence continue } @@ -317,7 +318,7 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan shouldAdd = false break } - txSignersSeqs[signer.Signer.String()] = signer.Sequence + txSignersSeqs[signerKey] = signer.Sequence } if !shouldAdd { return true diff --git a/baseapp/abci_utils_test.go b/baseapp/abci_utils_test.go index 65e4e0ec1a78..8dd272f57c46 100644 --- a/baseapp/abci_utils_test.go +++ b/baseapp/abci_utils_test.go @@ -18,6 +18,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "cosmossdk.io/core/address" "cosmossdk.io/core/comet" "cosmossdk.io/core/header" "cosmossdk.io/log" @@ -485,9 +486,11 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_NoOpMempoolTxSelection() // build a tx _, _, addr := testdata.KeyTestPubAddr() + addrStr, err := signingCtx.AddressCodec().BytesToString(addr) + require.NoError(s.T(), err) builder := txConfig.NewTxBuilder() s.Require().NoError(builder.SetMsgs( - &baseapptestutil.MsgCounter{Counter: 0, FailOnHandler: false, Signer: addr.String()}, + &baseapptestutil.MsgCounter{Counter: 0, FailOnHandler: false, Signer: addrStr}, )) builder.SetGasLimit(100) setTxSignature(s.T(), builder, 0) @@ -592,24 +595,24 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_PriorityNonceMempoolTxSe testTxs := []testTx{ // test 1 - {tx: buildMsg(s.T(), txConfig, []byte(`0`), [][]byte{secret1}, []uint64{1}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`12345678910`), [][]byte{secret1}, []uint64{2}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`22`), [][]byte{secret1}, []uint64{3}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`32`), [][]byte{secret2}, []uint64{1}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`0`), [][]byte{secret1}, []uint64{1}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`12345678910`), [][]byte{secret1}, []uint64{2}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`22`), [][]byte{secret1}, []uint64{3}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`32`), [][]byte{secret2}, []uint64{1}), priority: 8}, // test 2 - {tx: buildMsg(s.T(), txConfig, []byte(`4`), [][]byte{secret1, secret2}, []uint64{3, 3}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`52345678910`), [][]byte{secret1, secret3}, []uint64{4, 3}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`62`), [][]byte{secret1, secret4}, []uint64{5, 3}), priority: 8}, - {tx: buildMsg(s.T(), txConfig, []byte(`72`), [][]byte{secret3, secret5}, []uint64{4, 3}), priority: 8}, - {tx: buildMsg(s.T(), txConfig, []byte(`82`), [][]byte{secret2, secret6}, []uint64{4, 3}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`4`), [][]byte{secret1, secret2}, []uint64{3, 3}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`52345678910`), [][]byte{secret1, secret3}, []uint64{4, 3}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`62`), [][]byte{secret1, secret4}, []uint64{5, 3}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`72`), [][]byte{secret3, secret5}, []uint64{4, 3}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`82`), [][]byte{secret2, secret6}, []uint64{4, 3}), priority: 8}, // test 3 - {tx: buildMsg(s.T(), txConfig, []byte(`9`), [][]byte{secret3, secret4}, []uint64{3, 3}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`1052345678910`), [][]byte{secret1, secret2}, []uint64{4, 4}), priority: 8}, - {tx: buildMsg(s.T(), txConfig, []byte(`11`), [][]byte{secret1, secret2}, []uint64{5, 5}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`9`), [][]byte{secret3, secret4}, []uint64{3, 3}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`1052345678910`), [][]byte{secret1, secret2}, []uint64{4, 4}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`11`), [][]byte{secret1, secret2}, []uint64{5, 5}), priority: 8}, // test 4 - {tx: buildMsg(s.T(), txConfig, []byte(`1252345678910`), [][]byte{secret1}, []uint64{3}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`13`), [][]byte{secret1}, []uint64{5}), priority: 10}, - {tx: buildMsg(s.T(), txConfig, []byte(`14`), [][]byte{secret1}, []uint64{6}), priority: 8}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`1252345678910`), [][]byte{secret1}, []uint64{3}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`13`), [][]byte{secret1}, []uint64{5}), priority: 10}, + {tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`14`), [][]byte{secret1}, []uint64{6}), priority: 8}, } for i := range testTxs { @@ -718,7 +721,7 @@ func marshalDelimitedFn(msg proto.Message) ([]byte, error) { return buf.Bytes(), nil } -func buildMsg(t *testing.T, txConfig client.TxConfig, value []byte, secrets [][]byte, nonces []uint64) sdk.Tx { +func buildMsg(t *testing.T, txConfig client.TxConfig, ac address.Codec, value []byte, secrets [][]byte, nonces []uint64) sdk.Tx { t.Helper() builder := txConfig.NewTxBuilder() @@ -735,9 +738,12 @@ func buildMsg(t *testing.T, txConfig client.TxConfig, value []byte, secrets [][] }) } + addr, err := ac.BytesToString(signatures[0].PubKey.Bytes()) + require.NoError(t, err) + _ = builder.SetMsgs( &baseapptestutil.MsgKeyValue{ - Signer: sdk.AccAddress(signatures[0].PubKey.Bytes()).String(), + Signer: addr, Value: value, }, ) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 22b046cf8a63..fe29d981feb7 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -14,6 +14,7 @@ import ( dbm "github.com/cosmos/cosmos-db" "github.com/stretchr/testify/require" + "cosmossdk.io/core/address" corestore "cosmossdk.io/core/store" errorsmod "cosmossdk.io/errors" "cosmossdk.io/log" @@ -50,6 +51,7 @@ type ( cdc *codec.ProtoCodec txConfig client.TxConfig logBuffer *bytes.Buffer + ac address.Codec } SnapshotsConfig struct { @@ -91,6 +93,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite cdc: cdc, txConfig: txConfig, logBuffer: logBuffer, + ac: cdc.InterfaceRegistry().SigningContext().AddressCodec(), } } @@ -152,7 +155,10 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun _, err := r.Read(value) require.NoError(t, err) - msgs = append(msgs, &baseapptestutil.MsgKeyValue{Key: key, Value: value, Signer: addr.String()}) + addrStr, err := suite.ac.BytesToString(addr) + require.NoError(t, err) + + msgs = append(msgs, &baseapptestutil.MsgKeyValue{Key: key, Value: value, Signer: addrStr}) keyCounter++ } @@ -226,7 +232,7 @@ func TestAnteHandlerGasMeter(t *testing.T) { deliverKey := []byte("deliver-key") baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey}) - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) _, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}}) @@ -508,7 +514,7 @@ func TestTxDecoder(t *testing.T) { // patch in TxConfig instead of using an output from x/auth/tx txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes) - tx := newTxCounter(t, txConfig, 1, 0) + tx := newTxCounter(t, txConfig, signingCtx.AddressCodec(), 1, 0) txBytes, err := txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -552,7 +558,7 @@ func TestCustomRunTxPanicHandler(t *testing.T) { // transaction should panic with custom handler above { - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) require.PanicsWithValue(t, customPanicMsg, func() { bz, err := suite.txConfig.TxEncoder()(tx) @@ -582,7 +588,7 @@ func TestBaseAppAnteHandler(t *testing.T) { // // NOTE: State should not be mutated here. This will be implicitly checked by // the next txs ante handler execution (anteHandlerTxTest). - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) tx = setFailOnAnte(t, suite.txConfig, tx, true) txBytes, err := suite.txConfig.TxEncoder()(tx) @@ -599,8 +605,8 @@ func TestBaseAppAnteHandler(t *testing.T) { // execute at tx that will pass the ante handler (the checkTx state should // mutate) but will fail the message handler - tx = newTxCounter(t, suite.txConfig, 0, 0) - tx = setFailOnHandler(t, suite.txConfig, tx, true) + tx = newTxCounter(t, suite.txConfig, suite.ac, 0, 0) + tx = setFailOnHandler(t, suite.txConfig, suite.ac, tx, true) txBytes, err = suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -617,7 +623,7 @@ func TestBaseAppAnteHandler(t *testing.T) { // Execute a successful ante handler and message execution where state is // implicitly checked by previous tx executions. - tx = newTxCounter(t, suite.txConfig, 1, 0) + tx = newTxCounter(t, suite.txConfig, suite.ac, 1, 0) txBytes, err = suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -657,7 +663,7 @@ func TestBaseAppPostHandler(t *testing.T) { // // NOTE: State should not be mutated here. This will be implicitly checked by // the next txs ante handler execution (anteHandlerTxTest). - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) @@ -671,7 +677,7 @@ func TestBaseAppPostHandler(t *testing.T) { // It should also run on failed message execution postHandlerRun = false - tx = setFailOnHandler(t, suite.txConfig, tx, true) + tx = setFailOnHandler(t, suite.txConfig, suite.ac, tx, true) txBytes, err = suite.txConfig.TxEncoder()(tx) require.NoError(t, err) res, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}}) @@ -682,7 +688,7 @@ func TestBaseAppPostHandler(t *testing.T) { require.True(t, postHandlerRun) // regression test, should not panic when runMsgs fails - tx = wonkyMsg(t, suite.txConfig, tx) + tx = wonkyMsg(t, suite.txConfig, suite.ac, tx) txBytes, err = suite.txConfig.TxEncoder()(tx) require.NoError(t, err) _, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}}) @@ -903,7 +909,7 @@ func TestABCI_FinalizeWithInvalidTX(t *testing.T) { _, err := suite.baseApp.InitChain(&abci.InitChainRequest{ConsensusParams: &cmtproto.ConsensusParams{}}) require.NoError(t, err) - tx := newTxCounter(t, suite.txConfig, 0, 0) + tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0) bz, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) diff --git a/baseapp/msg_service_router_test.go b/baseapp/msg_service_router_test.go index 84862c76f89c..d61a54b36268 100644 --- a/baseapp/msg_service_router_test.go +++ b/baseapp/msg_service_router_test.go @@ -155,9 +155,11 @@ func TestMsgService(t *testing.T) { require.NoError(t, err) _, _, addr := testdata.KeyTestPubAddr() + addrStr, err := signingCtx.AddressCodec().BytesToString(addr) + require.NoError(t, err) msg := testdata.MsgCreateDog{ Dog: &testdata.Dog{Name: "Spot"}, - Owner: addr.String(), + Owner: addrStr, } txBuilder := txConfig.NewTxBuilder() diff --git a/baseapp/streaming_test.go b/baseapp/streaming_test.go index 6276b9988c9d..b0779c6b91ca 100644 --- a/baseapp/streaming_test.go +++ b/baseapp/streaming_test.go @@ -74,7 +74,7 @@ func TestABCI_MultiListener_StateChanges(t *testing.T) { for i := 0; i < txPerHeight; i++ { counter := int64(blockN*txPerHeight + i) - tx := newTxCounter(t, suite.txConfig, counter, counter) + tx := newTxCounter(t, suite.txConfig, suite.ac, counter, counter) txBytes, err := suite.txConfig.TxEncoder()(tx) require.NoError(t, err) diff --git a/baseapp/utils_test.go b/baseapp/utils_test.go index 3d1a867100f1..6794a4ae1617 100644 --- a/baseapp/utils_test.go +++ b/baseapp/utils_test.go @@ -295,17 +295,19 @@ func parseTxMemo(t *testing.T, tx sdk.Tx) (counter int64, failOnAnte bool) { return counter, failOnAnte } -func newTxCounter(t *testing.T, cfg client.TxConfig, counter int64, msgCounters ...int64) signing.Tx { +func newTxCounter(t *testing.T, cfg client.TxConfig, ac address.Codec, counter int64, msgCounters ...int64) signing.Tx { t.Helper() _, _, addr := testdata.KeyTestPubAddr() + addrStr, err := ac.BytesToString(addr) + require.NoError(t, err) msgs := make([]sdk.Msg, 0, len(msgCounters)) for _, c := range msgCounters { - msg := &baseapptestutil.MsgCounter{Counter: c, FailOnHandler: false, Signer: addr.String()} + msg := &baseapptestutil.MsgCounter{Counter: c, FailOnHandler: false, Signer: addrStr} msgs = append(msgs, msg) } builder := cfg.NewTxBuilder() - err := builder.SetMsgs(msgs...) + err = builder.SetMsgs(msgs...) require.NoError(t, err) builder.SetMemo("counter=" + strconv.FormatInt(counter, 10) + "&failOnAnte=false") setTxSignature(t, builder, uint64(counter)) @@ -343,37 +345,41 @@ func setFailOnAnte(t *testing.T, cfg client.TxConfig, tx signing.Tx, failOnAnte return builder.GetTx() } -func setFailOnHandler(t *testing.T, cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx { +func setFailOnHandler(t *testing.T, cfg client.TxConfig, ac address.Codec, tx signing.Tx, fail bool) signing.Tx { t.Helper() builder := cfg.NewTxBuilder() builder.SetMemo(tx.GetMemo()) msgs := tx.GetMsgs() + addr, err := ac.BytesToString(sdk.AccAddress("addr")) + require.NoError(t, err) for i, msg := range msgs { msgs[i] = &baseapptestutil.MsgCounter{ Counter: msg.(*baseapptestutil.MsgCounter).Counter, FailOnHandler: fail, - Signer: sdk.AccAddress("addr").String(), + Signer: addr, } } - err := builder.SetMsgs(msgs...) + err = builder.SetMsgs(msgs...) require.NoError(t, err) return builder.GetTx() } // wonkyMsg is to be used to run a MsgCounter2 message when the MsgCounter2 handler is not registered. -func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx { +func wonkyMsg(t *testing.T, cfg client.TxConfig, ac address.Codec, tx signing.Tx) signing.Tx { t.Helper() builder := cfg.NewTxBuilder() builder.SetMemo(tx.GetMemo()) msgs := tx.GetMsgs() + addr, err := ac.BytesToString(sdk.AccAddress("wonky")) + require.NoError(t, err) msgs = append(msgs, &baseapptestutil.MsgCounter2{ - Signer: sdk.AccAddress("wonky").String(), + Signer: addr, }) - err := builder.SetMsgs(msgs...) + err = builder.SetMsgs(msgs...) require.NoError(t, err) return builder.GetTx() }