From d26fe653c755aa8dce6ab59638741893e98947df Mon Sep 17 00:00:00 2001 From: testinginprod <98415576+testinginprod@users.noreply.github.com> Date: Tue, 6 Feb 2024 17:53:45 +0100 Subject: [PATCH] feat(accounts): Add methods to check if an account accepts certain messages or queries (interface assertion) (#19361) --- .../internal/implementation/implementation.go | 17 ++++++++++++++++ .../implementation/implementation_test.go | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/x/accounts/internal/implementation/implementation.go b/x/accounts/internal/implementation/implementation.go index 8916deae57..57bf0f144b 100644 --- a/x/accounts/internal/implementation/implementation.go +++ b/x/accounts/internal/implementation/implementation.go @@ -126,6 +126,23 @@ type Implementation struct { ExecuteHandlersSchema map[string]HandlerSchema } +// HasExec returns true if the account can execute the given msg. +func (i Implementation) HasExec(m ProtoMsg) bool { + _, ok := i.ExecuteHandlersSchema[MessageName(m)] + return ok +} + +// HasQuery returns true if the account can execute the given request. +func (i Implementation) HasQuery(m ProtoMsg) bool { + _, ok := i.QueryHandlersSchema[MessageName(m)] + return ok +} + +// HasInit returns true if the account uses the provided init message. +func (i Implementation) HasInit(m ProtoMsg) bool { + return i.InitHandlerSchema.RequestSchema.Name == MessageName(m) +} + // MessageSchema defines the schema of a message. // A message can also define a state schema. type MessageSchema struct { diff --git a/x/accounts/internal/implementation/implementation_test.go b/x/accounts/internal/implementation/implementation_test.go index 696420f009..fc98429934 100644 --- a/x/accounts/internal/implementation/implementation_test.go +++ b/x/accounts/internal/implementation/implementation_test.go @@ -56,4 +56,24 @@ func TestImplementation(t *testing.T) { _, err := impl.Query(ctx, &types.Int32Value{Value: 1}) require.ErrorIs(t, err, errInvalidMessage) }) + + 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{}) + require.True(t, ok) + + ok = impl.HasQuery(&types.Duration{}) + require.False(t, ok) + + ok = impl.HasInit(&types.StringValue{}) + require.True(t, ok) + + ok = impl.HasInit(&types.Duration{}) + require.False(t, ok) + }) }