Skip to content

Commit

Permalink
all works apparently
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown unknown committed Oct 25, 2023
1 parent 2dd30a7 commit c4282a9
Show file tree
Hide file tree
Showing 8 changed files with 1,698 additions and 235 deletions.
1,285 changes: 1,120 additions & 165 deletions api/cosmos/accounts/v1/query.pulsar.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions api/cosmos/accounts/v1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/cosmos/accounts/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ service Query {
rpc AccountQuery(AccountQueryRequest) returns (AccountQueryResponse) {};
// Schema returns an x/account schema. Unstable.
rpc Schema(SchemaRequest) returns (SchemaResponse) {};
// AccountType returns the account type for an address.
rpc AccountType(AccountTypeRequest) returns (AccountTypeResponse) {};
}

// AccountQueryRequest is the request type for the Query/AccountQuery RPC
Expand Down Expand Up @@ -50,3 +52,15 @@ message SchemaResponse {
// query_handlers defines the schema descriptor for the Query account method.
repeated Handler query_handlers = 3;
}

// AccountTypeRequest is the request type for the Query/AccountType RPC method.
message QueryAccountTypeRequest {
// address defines the address to query the account type for.
string address = 1;
}

// AccountTypeResponse is the response type for the Query/AccountType RPC method.
message AccountTypeResponse {
// account_type defines the account type for the address.
string account_type = 1;
}
130 changes: 88 additions & 42 deletions x/accounts/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ func GetExecuteCmd() *cobra.Command {
}
sender := clientCtx.GetFromAddress()

// 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
// execute message schema.
accClient := v1.NewQueryClient(clientCtx)
schema, err := accClient.Schema(cmd.Context(), &v1.SchemaRequest{
AccountType: args[0],
})
schema, err := getSchemaForAccount(clientCtx, args[0])
if err != nil {
return err
}
Expand All @@ -115,6 +109,58 @@ func GetExecuteCmd() *cobra.Command {
return cmd
}

func GetQueryAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query [account-address] [query-request-type-url] [json-message]",
Short: "Query account state",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

schema, err := getSchemaForAccount(clientCtx, args[0])
if err != nil {
return err
}
msgBytes, err := handlerMsgBytes(schema.QueryHandlers, args[1], args[2])
if err != nil {
return err
}
queryClient := v1.NewQueryClient(clientCtx)
res, err := queryClient.AccountQuery(cmd.Context(), &v1.AccountQueryRequest{
Target: args[0],
Request: msgBytes,
})
if err != nil {
return err
}
jsonResp, err := handlerResponseJSONBytes(schema.QueryHandlers, args[1], res.Response)
if err != nil {
return err
}

return clientCtx.PrintString(jsonResp)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func getSchemaForAccount(clientCtx client.Context, addr string) (*v1.SchemaResponse, error) {
queryClient := v1.NewQueryClient(clientCtx)
accType, err := queryClient.AccountType(clientCtx.CmdContext, &v1.AccountTypeRequest{
Address: addr,
})
if err != nil {
return nil, err
}
return queryClient.Schema(clientCtx.CmdContext, &v1.SchemaRequest{
AccountType: accType.AccountType,
})
}

func handlerMsgBytes(handlersSchema []*v1.SchemaResponse_Handler, msgTypeURL string, msgString string) ([]byte, error) {
var msgSchema *v1.SchemaResponse_Handler
for _, handler := range handlersSchema {
Expand All @@ -136,6 +182,25 @@ func handlerMsgBytes(handlersSchema []*v1.SchemaResponse_Handler, msgTypeURL str
})
}

func handlerResponseJSONBytes(handlerSchema []*v1.SchemaResponse_Handler, msgTypeURL string, protoBytes []byte) (string, error) {
var msgSchema *v1.SchemaResponse_Handler
for _, handler := range handlerSchema {
if handler.Request == msgTypeURL {
msgSchema = handler
break
}
}
if msgSchema == nil {
return "", fmt.Errorf("handler for message type %s not found", msgTypeURL)
}
anyMsg := new(anypb.Any)
err := proto.Unmarshal(protoBytes, anyMsg)
if err != nil {
return "", err
}
return decodeProtoToJSON(msgSchema.Response, anyMsg.Value)
}

func encodeJSONToProto(name string, jsonMsg string) ([]byte, error) {
jsonBytes := []byte(jsonMsg)
impl, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(name))
Expand All @@ -150,40 +215,21 @@ func encodeJSONToProto(name string, jsonMsg string) ([]byte, error) {
return proto.Marshal(msg)
}

func GetQueryAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query [account-address] [query-request-type-url] [json-message]",
Short: "Query account state",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

accClient := v1.NewQueryClient(clientCtx)

schema, err := accClient.Schema(cmd.Context(), &v1.SchemaRequest{
AccountType: args[0],
})
if err != nil {
return err
}
msgBytes, err := handlerMsgBytes(schema.QueryHandlers, args[1], args[2])
if err != nil {
return err
}
res, err := accClient.AccountQuery(cmd.Context(), &v1.AccountQueryRequest{
Target: args[0],
Request: msgBytes,
})
if err != nil {
return err
}
// TODO: decode response into JSON
return clientCtx.PrintProto(res)
},
func decodeProtoToJSON(name string, protoBytes []byte) (string, error) {
impl, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(name))
if err != nil {
return "", err
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
msg := impl.New().Interface()
err = proto.UnmarshalOptions{}.Unmarshal(protoBytes, msg)
if err != nil {
return "", fmt.Errorf(
"%w: unable to unmarshal protobytes in message '%s', message name: %s",
err, protoBytes, name)
}
jsonBytes, err := protojson.Marshal(msg)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
12 changes: 9 additions & 3 deletions x/accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/x/accounts/cli"
v1 "cosmossdk.io/x/accounts/v1"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -21,9 +22,10 @@ const (
)

var (
_ module.HasName = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasServices = AppModule{}
_ appmodule.AppModule = AppModule{}
_ module.HasName = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasServices = AppModule{}
)

func NewAppModule(k Keeper) AppModule {
Expand All @@ -34,6 +36,10 @@ type AppModule struct {
k Keeper
}

func (m AppModule) IsOnePerModuleType() {}

func (m AppModule) IsAppModule() {}

func (m AppModule) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {}

func (m AppModule) RegisterInterfaces(registry types.InterfaceRegistry) {
Expand Down
4 changes: 4 additions & 0 deletions x/accounts/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

v1 "cosmossdk.io/x/accounts/v1"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ v1.MsgServer = msgServer{}
Expand Down Expand Up @@ -51,6 +52,9 @@ func (m msgServer) Init(ctx context.Context, request *v1.MsgInit) (*v1.MsgInitRe
return nil, err
}

sdk.UnwrapSDKContext(ctx).EventManager().EmitEvent(
sdk.NewEvent("account_creation", sdk.NewAttribute("address", accAddrString)),
)
return &v1.MsgInitResponse{
AccountAddress: accAddrString,
Response: respBytes,
Expand Down
14 changes: 14 additions & 0 deletions x/accounts/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ func (q queryServer) Schema(_ context.Context, request *v1.SchemaRequest) (*v1.S
}
return schema, nil
}

func (q queryServer) AccountType(ctx context.Context, request *v1.AccountTypeRequest) (*v1.AccountTypeResponse, error) {
addr, err := q.k.addressCodec.StringToBytes(request.Address)
if err != nil {
return nil, err
}
accType, err := q.k.AccountsByType.Get(ctx, addr)
if err != nil {
return nil, err
}
return &v1.AccountTypeResponse{
AccountType: accType,
}, nil
}
Loading

0 comments on commit c4282a9

Please sign in to comment.