Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accounts): Add methods to check if an account accepts certain messages or queries (interface assertion) #19361

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions x/accounts/internal/implementation/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions x/accounts/internal/implementation/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Comment on lines +60 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases for HasExec, HasQuery, and HasInit methods are correctly implemented, ensuring that these methods behave as expected for different message types. However, consider adding negative test cases or edge cases, if any, to cover more scenarios.

}
Loading