diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 73bb03a04292..0f1de773cbda 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -14,6 +14,8 @@ import ( "testing" "time" + "github.com/cosmos/cosmos-sdk/codec" + abci "github.com/cometbft/cometbft/abci/types" cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" @@ -760,6 +762,16 @@ func TestABCI_FinalizeBlock_MultiMsg(t *testing.T) { require.Equal(t, int64(2), msgCounter2) } +func anyMessage(t *testing.T, cdc codec.Codec, msg *baseapptestutil.MsgSend) *any.Any { + t.Helper() + b, err := cdc.Marshal(msg) + require.NoError(t, err) + return &any.Any{ + TypeUrl: sdk.MsgTypeURL(msg), + Value: b, + } +} + func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { gasConsumed := uint64(5) anteOpt := func(bapp *baseapp.BaseApp) { @@ -781,60 +793,92 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { _, _, addr := testdata.KeyTestPubAddr() _, _, toAddr := testdata.KeyTestPubAddr() tests := []struct { - name string - nestedMsgs []*baseapptestutil.MsgSend - wantErr bool + name string + message sdk.Msg + wantErr bool }{ { name: "ok nested message", - nestedMsgs: []*baseapptestutil.MsgSend{ - { - From: addr.String(), - To: toAddr.String(), - Amount: "10000stake", - }, + message: &baseapptestutil.MsgSend{ + From: addr.String(), + To: toAddr.String(), + Amount: "10000stake", }, }, { name: "different signers", - nestedMsgs: []*baseapptestutil.MsgSend{ - { - From: toAddr.String(), - To: addr.String(), - Amount: "10000stake", - }, + message: &baseapptestutil.MsgSend{ + From: toAddr.String(), + To: addr.String(), + Amount: "10000stake", }, wantErr: true, }, { name: "empty from", - nestedMsgs: []*baseapptestutil.MsgSend{ - { - From: "", - To: toAddr.String(), - Amount: "10000stake", - }, + message: &baseapptestutil.MsgSend{ + From: "", + To: toAddr.String(), + Amount: "10000stake", }, wantErr: true, }, { name: "empty to", - nestedMsgs: []*baseapptestutil.MsgSend{ - { - From: addr.String(), - To: "", - Amount: "10000stake", - }, + message: &baseapptestutil.MsgSend{ + From: addr.String(), + To: "", + Amount: "10000stake", }, wantErr: true, }, { name: "negative amount", - nestedMsgs: []*baseapptestutil.MsgSend{ - { - From: addr.String(), - To: toAddr.String(), - Amount: "-10000stake", + message: &baseapptestutil.MsgSend{ + From: addr.String(), + To: toAddr.String(), + Amount: "-10000stake", + }, + wantErr: true, + }, + { + name: "with nested messages", + message: &baseapptestutil.MsgNestedMessages{ + Signer: addr.String(), + Messages: []*any.Any{ + anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ + From: addr.String(), + To: toAddr.String(), + Amount: "10000stake", + }), + }, + }, + wantErr: false, + }, + { + name: "with invalid nested messages", + message: &baseapptestutil.MsgNestedMessages{ + Signer: addr.String(), + Messages: []*any.Any{ + anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ + From: "", + To: toAddr.String(), + Amount: "10000stake", + }), + }, + }, + wantErr: true, + }, + { + name: "with different signer ", + message: &baseapptestutil.MsgNestedMessages{ + Signer: addr.String(), + Messages: []*any.Any{ + anyMessage(t, suite.cdc, &baseapptestutil.MsgSend{ + From: toAddr.String(), + To: addr.String(), + Amount: "10000stake", + }), }, }, wantErr: true, @@ -842,15 +886,14 @@ func TestABCI_Query_SimulateNestedMessagesTx(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - nestedMessages := make([]*any.Any, len(tt.nestedMsgs)) - for i, msg := range tt.nestedMsgs { - b, err := suite.cdc.Marshal(msg) - require.NoError(t, err) - nestedMessages[i] = &any.Any{ - TypeUrl: sdk.MsgTypeURL(msg), - Value: b, - } + nestedMessages := make([]*any.Any, 1) + b, err := suite.cdc.Marshal(tt.message) + require.NoError(t, err) + nestedMessages[0] = &any.Any{ + TypeUrl: sdk.MsgTypeURL(tt.message), + Value: b, } + msg := &baseapptestutil.MsgNestedMessages{ Messages: nestedMessages, Signer: addr.String(), diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index a98fe4ca2faf..91e4a97da5e7 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -13,10 +13,11 @@ import ( cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" "github.com/cometbft/cometbft/crypto/tmhash" dbm "github.com/cosmos/cosmos-db" + "github.com/cosmos/cosmos-proto/anyutil" "github.com/cosmos/gogoproto/proto" "golang.org/x/exp/maps" protov2 "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/protoadapt" + "google.golang.org/protobuf/types/known/anypb" "cosmossdk.io/core/header" errorsmod "cosmossdk.io/errors" @@ -962,10 +963,6 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res if mode == execModeSimulate { nestedMsgsContext, _ := app.cacheTxContext(ctx, txBytes) for _, msg := range msgs { - msg, ok := msg.(HasNestedMsgs) - if !ok { - continue - } nestedErr := app.simulateNestedMessages(nestedMsgsContext, msg) if nestedErr != nil { return gInfo, nil, anteEvents, nestedErr @@ -1080,12 +1077,24 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, msgsV2 []protov2.Me } // simulateNestedMessages simulates a message nested messages. -func (app *BaseApp) simulateNestedMessages(ctx sdk.Context, msg HasNestedMsgs) error { - msgs, err := msg.GetMsgs() +func (app *BaseApp) simulateNestedMessages(ctx sdk.Context, msg sdk.Msg) error { + nestedMsgs, ok := msg.(HasNestedMsgs) + if !ok { + return nil + } + + msgs, err := nestedMsgs.GetMsgs() if err != nil { return err } + for _, msg := range msgs { + err = app.simulateNestedMessages(ctx, msg) + if err != nil { + return err + } + } + if err := validateBasicTxMsgs(app.msgServiceRouter, msgs); err != nil { return err } @@ -1103,7 +1112,19 @@ func (app *BaseApp) simulateNestedMessages(ctx sdk.Context, msg HasNestedMsgs) e func (app *BaseApp) msgsV1ToMsgsV2(msgs []sdk.Msg) ([]protov2.Message, error) { msgsV2 := make([]protov2.Message, len(msgs)) for i, msg := range msgs { - msgsV2[i] = protoadapt.MessageV2Of(msg) + gogoAny, err := codectypes.NewAnyWithValue(msg) + if err != nil { + return nil, err + } + anyMsg := &anypb.Any{ + TypeUrl: gogoAny.TypeUrl, + Value: gogoAny.Value, + } + msgV2, err := anyutil.Unpack(anyMsg, app.cdc.InterfaceRegistry().SigningContext().FileResolver(), app.cdc.InterfaceRegistry().SigningContext().TypeResolver()) + if err != nil { + return nil, err + } + msgsV2[i] = msgV2 } return msgsV2, nil