From 8a4e0b6be4333ce1419278acc132134540cd2d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Tue, 3 Dec 2024 11:26:37 +0700 Subject: [PATCH] updates with new MsgExecute --- x/accounts/cli/cli.go | 16 ++------ x/accounts/defaults/multisig/account_test.go | 41 +++++++++++++++++--- x/accounts/msg_server.go | 2 +- x/accounts/msg_server_test.go | 21 +++------- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/x/accounts/cli/cli.go b/x/accounts/cli/cli.go index 99b3153476f4..f1d32a57b57c 100644 --- a/x/accounts/cli/cli.go +++ b/x/accounts/cli/cli.go @@ -43,19 +43,11 @@ func GetExecuteCmd() *cobra.Command { return err } - schema, err := getSchemaForAccount(clientCtx, args[0]) - if err != nil { - return err - } - - msgBytes, err := handlerMsgBytes(schema.ExecuteHandlers, args[1], args[2]) - if err != nil { - return err - } msg := v1.MsgExecute{ - Sender: sender, - Target: args[0], - Message: msgBytes, + Sender: sender, + Target: args[0], + ExecuteMsgTypeUrl: args[1], + JsonMessage: args[2], } return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) diff --git a/x/accounts/defaults/multisig/account_test.go b/x/accounts/defaults/multisig/account_test.go index a53b57f6884f..3ea08b90b7c0 100644 --- a/x/accounts/defaults/multisig/account_test.go +++ b/x/accounts/defaults/multisig/account_test.go @@ -2,7 +2,9 @@ package multisig import ( "context" + "encoding/json" "math" + "strings" "testing" "time" @@ -14,6 +16,14 @@ import ( "cosmossdk.io/x/accounts/accountstd" v1 "cosmossdk.io/x/accounts/defaults/multisig/v1" accountsv1 "cosmossdk.io/x/accounts/v1" + + "bytes" + "fmt" + "reflect" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/gogoproto/jsonpb" + gogoproto "github.com/cosmos/gogoproto/proto" ) func setup(t *testing.T, _ context.Context, ss store.KVStoreService, timefn func() time.Time) *Account { @@ -567,7 +577,8 @@ func TestProposalPassing(t *testing.T) { 0, []byte("multisig_acc"), []byte("addr1"), TestFunds, func(ictx context.Context, sender []byte, msg transaction.Msg) (transaction.Msg, error) { if execmsg, ok := msg.(*accountsv1.MsgExecute); ok { - updateCfg, err := accountstd.UnpackAny[v1.MsgUpdateConfig](execmsg.GetMessage()) + anyExecmsg, err := encodeJSONToProto(execmsg.ExecuteMsgTypeUrl, execmsg.JsonMessage) + updateCfg, err := accountstd.UnpackAny[v1.MsgUpdateConfig](anyExecmsg) if err != nil { return nil, err } @@ -600,11 +611,18 @@ func TestProposalPassing(t *testing.T) { anymsg, err := accountstd.PackAny(msg) require.NoError(t, err) + split := strings.Split(anymsg.TypeUrl, "/") + nameTypeUrl := split[len(split)-1] + + jsonReq, err := json.Marshal(msg) + require.NoError(t, err) + execMsg := &accountsv1.MsgExecute{ - Sender: "multisig_acc", - Target: "multisig_acc", - Message: anymsg, - Funds: nil, + Sender: "multisig_acc", + Target: "multisig_acc", + ExecuteMsgTypeUrl: nameTypeUrl, + JsonMessage: string(jsonReq), + Funds: nil, } execMsgAny, err := accountstd.PackAny(execMsg) require.NoError(t, err) @@ -710,3 +728,16 @@ func TestWeightOverflow(t *testing.T) { _, err = acc.Init(ctx, startAcc) require.ErrorContains(t, err, "overflow") } + +func encodeJSONToProto(name, jsonMsg string) (*codectypes.Any, error) { + impl := gogoproto.MessageType(name) + if impl == nil { + return nil, fmt.Errorf("message type %s not found", name) + } + msg := reflect.New(impl.Elem()).Interface().(gogoproto.Message) + err := jsonpb.Unmarshal(bytes.NewBufferString(jsonMsg), msg) + if err != nil { + return nil, fmt.Errorf("provided message is not valid %s: %w", jsonMsg, err) + } + return codectypes.NewAnyWithValue(msg) +} diff --git a/x/accounts/msg_server.go b/x/accounts/msg_server.go index 678cef2a0f51..cc635dd0053c 100644 --- a/x/accounts/msg_server.go +++ b/x/accounts/msg_server.go @@ -66,7 +66,7 @@ func (m msgServer) Execute(ctx context.Context, execute *v1.MsgExecute) (*v1.Msg } // decode message bytes into the concrete boxed message type - req, err := implementation.EncodeMsgJSONToProto(execute.JsonMessage) + req, err := implementation.EncodeMsgJSONToProto(execute.ExecuteMsgTypeUrl, execute.JsonMessage) if err != nil { return nil, err } diff --git a/x/accounts/msg_server_test.go b/x/accounts/msg_server_test.go index 1ce94739dbd5..c68b99a1eecf 100644 --- a/x/accounts/msg_server_test.go +++ b/x/accounts/msg_server_test.go @@ -3,12 +3,9 @@ package accounts import ( "testing" + "cosmossdk.io/x/accounts/accountstd" "github.com/stretchr/testify/require" - // "google.golang.org/protobuf/types/known/emptypb" - "google.golang.org/protobuf/types/known/wrapperspb" - "cosmossdk.io/x/accounts/accountstd" - "cosmossdk.io/x/accounts/internal/implementation" v1 "cosmossdk.io/x/accounts/v1" ) @@ -17,9 +14,6 @@ func TestMsgServer(t *testing.T) { s := NewMsgServer(k) // create - // initMsg, err := implementation.PackAny(&emptypb.Empty{}) - // require.NoError(t, err) - initResp, err := s.Init(ctx, &v1.MsgInit{ Sender: "sender", AccountType: "test", @@ -29,16 +23,11 @@ func TestMsgServer(t *testing.T) { require.NotNil(t, initResp) // execute - executeMsg := &wrapperspb.StringValue{ - Value: "10", - } - executeMsgAny, err := implementation.PackAny(executeMsg) - require.NoError(t, err) - execResp, err := s.Execute(ctx, &v1.MsgExecute{ - Sender: "sender", - Target: initResp.AccountAddress, - Message: executeMsgAny, + Sender: "sender", + Target: initResp.AccountAddress, + ExecuteMsgTypeUrl: "google.protobuf.UInt64Value", + JsonMessage: `10`, }) require.NoError(t, err) require.NotNil(t, execResp)