Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
testinginprod committed Oct 23, 2024
1 parent 3e65a61 commit be5d265
Show file tree
Hide file tree
Showing 14 changed files with 2,457 additions and 1,005 deletions.
2,425 changes: 1,697 additions & 728 deletions api/cosmos/accounts/v1/query.pulsar.go

Large diffs are not rendered by default.

40 changes: 40 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: 4 additions & 10 deletions x/accounts/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetTxInitCmd() *cobra.Command {
// 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{
schema, err := accClient.InitSchema(cmd.Context(), &v1.InitSchemaRequest{
AccountType: args[0],
})
if err != nil {
Expand Down Expand Up @@ -166,19 +166,13 @@ func GetQueryAccountCmd() *cobra.Command {

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,
Address: addr,
})
}

func handlerMsgBytes(handlersSchema []*v1.SchemaResponse_Handler, msgTypeURL, msgString string) (*codectypes.Any, error) {
var msgSchema *v1.SchemaResponse_Handler
func handlerMsgBytes(handlersSchema []*v1.Handler, msgTypeURL, msgString string) (*codectypes.Any, error) {
var msgSchema *v1.Handler
for _, handler := range handlersSchema {
if handler.Request == msgTypeURL {
msgSchema = handler
Expand Down
2 changes: 1 addition & 1 deletion x/accounts/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *CLITestSuite) TestTxInitCmd() {

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&v1.SchemaResponse{
InitSchema: &v1.SchemaResponse_Handler{
InitSchema: &v1.Handler{
Request: sdk.MsgTypeURL(&types.Empty{})[1:],
Response: sdk.MsgTypeURL(&types.Empty{})[1:],
},
Expand Down
8 changes: 0 additions & 8 deletions x/accounts/internal/implementation/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ type ProtoMsgG[T any] interface {

type Any = codectypes.Any

func FindMessageByName(name string) (transaction.Msg, error) {
typ := proto.MessageType(name)
if typ == nil {
return nil, fmt.Errorf("no message type found for %s", name)
}
return reflect.New(typ.Elem()).Interface().(transaction.Msg), nil
}

func MessageName(msg transaction.Msg) string {
return proto.MessageName(msg)
}
Expand Down
103 changes: 77 additions & 26 deletions x/accounts/internal/implementation/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,50 +94,69 @@ func newImplementation(schemaBuilder *collections.SchemaBuilder, account Account
return Implementation{}, err
}
return Implementation{
Init: initHandler,
Execute: executeHandler,
Query: queryHandler,
CollectionsSchema: schema,
InitHandlerSchema: ir.schema,
QueryHandlersSchema: qr.er.handlersSchema,
ExecuteHandlersSchema: er.handlersSchema,
init: initHandler,
execute: executeHandler,
query: queryHandler,
collectionsSchema: schema,
initHandlerSchema: ir.schema,
queryHandlersSchema: qr.er.handlersSchema,
executeHandlersSchema: er.handlersSchema,
}, nil
}

// Implementation wraps an Account implementer in order to provide a concrete
// and non-generic implementation usable by the x/accounts module.
type Implementation struct {
// Init defines the initialisation handler for the smart account.
Init func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
// init defines the initialisation handler for the smart account.
init func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
// Execute defines the execution handler for the smart account.
Execute func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
execute func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
// Query defines the query handler for the smart account.
Query func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
// CollectionsSchema represents the state schema.
CollectionsSchema collections.Schema
// InitHandlerSchema represents the init handler schema.
InitHandlerSchema HandlerSchema
// QueryHandlersSchema is the schema of the query handlers.
QueryHandlersSchema map[string]HandlerSchema
// ExecuteHandlersSchema is the schema of the execute handlers.
ExecuteHandlersSchema map[string]HandlerSchema
query func(ctx context.Context, msg transaction.Msg) (resp transaction.Msg, err error)
// collectionsSchema represents the state schema.
collectionsSchema collections.Schema
// initHandlerSchema represents the init handler schema.
initHandlerSchema HandlerSchema
// queryHandlersSchema is the schema of the query handlers.
queryHandlersSchema map[string]HandlerSchema
// executeHandlersSchema is the schema of the execute handlers.
executeHandlersSchema map[string]HandlerSchema
}

func (i Implementation) Init(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
return i.init(ctx, msg)
}

func (i Implementation) Execute(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
return i.execute(ctx, msg)
}

func (i Implementation) Query(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
return i.query(ctx, msg)
}

// HasExec returns true if the account can execute the given msg.
func (i Implementation) HasExec(m transaction.Msg) bool {
_, ok := i.ExecuteHandlersSchema[MessageName(m)]
func (i Implementation) HasExec(_ context.Context, m transaction.Msg) bool {
_, ok := i.executeHandlersSchema[MessageName(m)]
return ok
}

// HasQuery returns true if the account can execute the given request.
func (i Implementation) HasQuery(m transaction.Msg) bool {
_, ok := i.QueryHandlersSchema[MessageName(m)]
func (i Implementation) HasQuery(_ context.Context, m transaction.Msg) bool {
_, ok := i.queryHandlersSchema[MessageName(m)]
return ok
}

// HasInit returns true if the account uses the provided init message.
func (i Implementation) HasInit(m transaction.Msg) bool {
return i.InitHandlerSchema.RequestSchema.Name == MessageName(m)
func (i Implementation) GetInitHandlerSchema(_ context.Context) (HandlerSchema, error) {
return i.initHandlerSchema, nil
}

func (i Implementation) GetQueryHandlersSchema(_ context.Context) (map[string]HandlerSchema, error) {
return i.queryHandlersSchema, nil
}

func (i Implementation) GetExecuteHandlersSchema(_ context.Context) (map[string]HandlerSchema, error) {
return i.executeHandlersSchema, nil
}

// MessageSchema defines the schema of a message.
Expand All @@ -156,3 +175,35 @@ type HandlerSchema struct {
// ResponseSchema defines the schema of the response.
ResponseSchema MessageSchema
}

const msgInterfaceName = "cosmos.accounts.v1.MsgInterface"

// creates a new interface type which is an alias of the proto message interface to avoid conflicts with sdk.Msg
type msgInterface transaction.Msg

var msgInterfaceType = (*msgInterface)(nil)

// registerToInterfaceRegistry registers all the interfaces of the accounts to the
// global interface registry. This is required for the SDK to correctly decode
// the google.Protobuf.Any used in x/accounts.
func registerToInterfaceRegistry(ir InterfaceRegistry, accMap map[string]Implementation) {
ir.RegisterInterface(msgInterfaceName, msgInterfaceType)

for _, acc := range accMap {
// register init
ir.RegisterImplementations(msgInterfaceType, acc.initHandlerSchema.RequestSchema.New(), acc.initHandlerSchema.ResponseSchema.New())
// register exec
for _, exec := range acc.executeHandlersSchema {
ir.RegisterImplementations(msgInterfaceType, exec.RequestSchema.New(), exec.ResponseSchema.New())
}
// register query
for _, query := range acc.queryHandlersSchema {
ir.RegisterImplementations(msgInterfaceType, query.RequestSchema.New(), query.ResponseSchema.New())
}
}
}

type InterfaceRegistry interface {
RegisterInterface(name string, iface any, impls ...gogoproto.Message)
RegisterImplementations(iface any, impls ...gogoproto.Message)
}
13 changes: 4 additions & 9 deletions x/accounts/internal/implementation/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,17 @@ func TestImplementation(t *testing.T) {
})

t.Run("Has* methods", func(t *testing.T) {
ok := impl.HasExec(&types.StringValue{})
require.True(t, ok)

ok = impl.HasExec(&types.Duration{})
require.False(t, ok)

ok = impl.HasQuery(&types.StringValue{})
ok := impl.HasExec(ctx, &types.StringValue{})
require.True(t, ok)

ok = impl.HasQuery(&types.Duration{})
ok = impl.HasExec(ctx, &types.Duration{})
require.False(t, ok)

ok = impl.HasInit(&types.StringValue{})
ok = impl.HasQuery(ctx, &types.StringValue{})
require.True(t, ok)

ok = impl.HasInit(&types.Duration{})
ok = impl.HasQuery(ctx, &types.Duration{})
require.False(t, ok)
})
}
35 changes: 1 addition & 34 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"errors"
"fmt"

gogoproto "github.com/cosmos/gogoproto/proto"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -37,10 +35,7 @@ var (
AccountByNumber = collections.NewPrefix(2)
)

type InterfaceRegistry interface {
RegisterInterface(name string, iface any, impls ...gogoproto.Message)
RegisterImplementations(iface any, impls ...gogoproto.Message)
}
type InterfaceRegistry = implementation.InterfaceRegistry

func NewKeeper(
cdc codec.Codec,
Expand Down Expand Up @@ -71,7 +66,6 @@ func NewKeeper(
if err != nil {
return Keeper{}, err
}
registerToInterfaceRegistry(ir, keeper.accounts)
return keeper, nil
}

Expand Down Expand Up @@ -428,30 +422,3 @@ func (k Keeper) maybeSendFunds(ctx context.Context, from, to []byte, amt sdk.Coi

return nil
}

const msgInterfaceName = "cosmos.accounts.v1.MsgInterface"

// creates a new interface type which is an alias of the proto message interface to avoid conflicts with sdk.Msg
type msgInterface transaction.Msg

var msgInterfaceType = (*msgInterface)(nil)

// registerToInterfaceRegistry registers all the interfaces of the accounts to the
// global interface registry. This is required for the SDK to correctly decode
// the google.Protobuf.Any used in x/accounts.
func registerToInterfaceRegistry(ir InterfaceRegistry, accMap map[string]implementation.Implementation) {
ir.RegisterInterface(msgInterfaceName, msgInterfaceType)

for _, acc := range accMap {
// register init
ir.RegisterImplementations(msgInterfaceType, acc.InitHandlerSchema.RequestSchema.New(), acc.InitHandlerSchema.ResponseSchema.New())
// register exec
for _, exec := range acc.ExecuteHandlersSchema {
ir.RegisterImplementations(msgInterfaceType, exec.RequestSchema.New(), exec.ResponseSchema.New())
}
// register query
for _, query := range acc.QueryHandlersSchema {
ir.RegisterImplementations(msgInterfaceType, query.RequestSchema.New(), query.ResponseSchema.New())
}
}
}
2 changes: 1 addition & 1 deletion x/accounts/keeper_account_abstraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k Keeper) IsAbstractedAccount(ctx context.Context, addr []byte) (bool, err
if !ok {
return false, fmt.Errorf("%w: %s", errAccountTypeNotFound, accType)
}
return impl.HasExec(&aa_interface_v1.MsgAuthenticate{}), nil
return impl.HasExec(ctx, &aa_interface_v1.MsgAuthenticate{}), nil
}

func (k Keeper) AuthenticateAccount(ctx context.Context, signer []byte, bundler string, rawTx *tx.TxRaw, protoTx *tx.Tx, signIndex uint32) error {
Expand Down
Loading

0 comments on commit be5d265

Please sign in to comment.