Skip to content

Commit

Permalink
update: check for nested messages in nested messages
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed May 9, 2024
1 parent e9569ed commit 6d0e987
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 48 deletions.
123 changes: 83 additions & 40 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -781,76 +793,107 @@ 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,
},
}
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(),
Expand Down
37 changes: 29 additions & 8 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 6d0e987

Please sign in to comment.