From f89848fce72ec64471d6584b2ccd3cfc473f0fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Mon, 2 Dec 2024 16:39:01 +0700 Subject: [PATCH] updates new type MsgInit --- x/accounts/cli/cli.go | 17 +---------------- x/accounts/genesis_test.go | 4 +--- x/accounts/keeper.go | 4 +++- x/accounts/msg_server_test.go | 8 ++++---- x/accounts/query_server_test.go | 8 ++------ x/accounts/v1/schema.go | 4 ++-- 6 files changed, 13 insertions(+), 32 deletions(-) diff --git a/x/accounts/cli/cli.go b/x/accounts/cli/cli.go index 4f58baf9cb4e..57fc5ec14bb2 100644 --- a/x/accounts/cli/cli.go +++ b/x/accounts/cli/cli.go @@ -43,25 +43,10 @@ func GetTxInitCmd() *cobra.Command { return err } - // we need to convert the message from json to a protobuf message - // to know which message to use, we need to know the account type - // init message schema. - accClient := v1.NewQueryClient(clientCtx) - schema, err := accClient.Schema(cmd.Context(), &v1.SchemaRequest{ - AccountType: args[0], - }) - if err != nil { - return err - } - - msgBytes, err := encodeJSONToProto(schema.InitSchema.Request, args[1]) - if err != nil { - return err - } msg := v1.MsgInit{ Sender: sender, AccountType: args[0], - Message: msgBytes, + JsonMessage: args[1], } isGenesis, err := cmd.Flags().GetBool("genesis") diff --git a/x/accounts/genesis_test.go b/x/accounts/genesis_test.go index 14c2470d84bd..530d3d697a6d 100644 --- a/x/accounts/genesis_test.go +++ b/x/accounts/genesis_test.go @@ -40,12 +40,10 @@ func TestGenesis(t *testing.T) { return testAccountType, acc, err }) // add to state a genesis account init msg. - initMsg, err := implementation.PackAny(&types.Empty{}) - require.NoError(t, err) state.InitAccountMsgs = append(state.InitAccountMsgs, &v1.MsgInit{ Sender: "sender-2", AccountType: testAccountType, - Message: initMsg, + JsonMessage: `{}`, Funds: nil, }) err = k.ImportState(ctx, state) diff --git a/x/accounts/keeper.go b/x/accounts/keeper.go index cf306dab0e01..939c0e1c7040 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -173,8 +173,10 @@ func (k Keeper) initFromMsg(ctx context.Context, initMsg *v1.MsgInit) (transacti return nil, nil, err } + schema := v1.MakeAccountSchema(k.accounts[initMsg.AccountType]) + // decode message bytes into the concrete boxed message type - msg, err := implementation.UnpackAnyRaw(initMsg.Message) + msg, err := implementation.EncodeMsgJSONToProto(schema.InitSchema.Request, initMsg.JsonMessage) if err != nil { return nil, nil, err } diff --git a/x/accounts/msg_server_test.go b/x/accounts/msg_server_test.go index 4a5c6fb87d2c..1ce94739dbd5 100644 --- a/x/accounts/msg_server_test.go +++ b/x/accounts/msg_server_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/stretchr/testify/require" - "google.golang.org/protobuf/types/known/emptypb" + // "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/wrapperspb" "cosmossdk.io/x/accounts/accountstd" @@ -17,13 +17,13 @@ func TestMsgServer(t *testing.T) { s := NewMsgServer(k) // create - initMsg, err := implementation.PackAny(&emptypb.Empty{}) - require.NoError(t, err) + // initMsg, err := implementation.PackAny(&emptypb.Empty{}) + // require.NoError(t, err) initResp, err := s.Init(ctx, &v1.MsgInit{ Sender: "sender", AccountType: "test", - Message: initMsg, + JsonMessage: `{}`, }) require.NoError(t, err) require.NotNil(t, initResp) diff --git a/x/accounts/query_server_test.go b/x/accounts/query_server_test.go index 80341bdf9c01..38dfece18ed0 100644 --- a/x/accounts/query_server_test.go +++ b/x/accounts/query_server_test.go @@ -5,7 +5,7 @@ import ( "github.com/cosmos/gogoproto/types" "github.com/stretchr/testify/require" - "google.golang.org/protobuf/types/known/emptypb" + // "google.golang.org/protobuf/types/known/emptypb" "cosmossdk.io/x/accounts/accountstd" "cosmossdk.io/x/accounts/internal/implementation" @@ -18,14 +18,10 @@ func TestQueryServer(t *testing.T) { ms := NewMsgServer(k) qs := NewQueryServer(k) - // create account - initMsg, err := implementation.PackAny(&emptypb.Empty{}) - require.NoError(t, err) - initResp, err := ms.Init(ctx, &v1.MsgInit{ Sender: "sender", AccountType: "test", - Message: initMsg, + JsonMessage: `{}`, }) require.NoError(t, err) diff --git a/x/accounts/v1/schema.go b/x/accounts/v1/schema.go index 6cebadb306f1..9c2ed14444a7 100644 --- a/x/accounts/v1/schema.go +++ b/x/accounts/v1/schema.go @@ -12,12 +12,12 @@ import ( func MakeAccountsSchemas(impls map[string]implementation.Implementation) map[string]*SchemaResponse { m := make(map[string]*SchemaResponse, len(impls)) for name, impl := range impls { - m[name] = makeAccountSchema(impl) + m[name] = MakeAccountSchema(impl) } return m } -func makeAccountSchema(impl implementation.Implementation) *SchemaResponse { +func MakeAccountSchema(impl implementation.Implementation) *SchemaResponse { return &SchemaResponse{ InitSchema: &SchemaResponse_Handler{ Request: impl.InitHandlerSchema.RequestSchema.Name,