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(x/tx): API improvements #15871

Merged
merged 6 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 11 additions & 2 deletions x/tx/signing/handler_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import (
// based on sign mode.
type HandlerMap struct {
signModeHandlers map[signingv1beta1.SignMode]SignModeHandler
defaultMode signingv1beta1.SignMode
modes []signingv1beta1.SignMode
}

// NewHandlerMap constructs a new sign mode handler map.
// NewHandlerMap constructs a new sign mode handler map. The first handler is used as the default.
func NewHandlerMap(handlers ...SignModeHandler) *HandlerMap {
res := &HandlerMap{
signModeHandlers: map[signingv1beta1.SignMode]SignModeHandler{},
}

for _, handler := range handlers {
for i, handler := range handlers {
mode := handler.Mode()
if i == 0 {
res.defaultMode = mode
Copy link
Member

Choose a reason for hiding this comment

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

A unit test for this would be good, even though this is likely tested indirectly in many places.

It would also be good to have test coverage defining what should happen if NewHandlerMap is called without any handlers. I'm guessing a panic would be appropriate.

}
res.signModeHandlers[mode] = handler
res.modes = append(res.modes, mode)
}
Expand All @@ -34,6 +38,11 @@ func (h *HandlerMap) SupportedModes() []signingv1beta1.SignMode {
return h.modes
}

// DefaultMode returns the default mode for this handler map.
func (h *HandlerMap) DefaultMode() signingv1beta1.SignMode {
return h.defaultMode
}

// GetSignBytes returns the sign bytes for the transaction for the requested mode.
func (h *HandlerMap) GetSignBytes(ctx context.Context, signMode signingv1beta1.SignMode, signerData SignerData, txData TxData) ([]byte, error) {
handler, ok := h.signModeHandlers[signMode]
Expand Down
10 changes: 6 additions & 4 deletions x/tx/signing/textual/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (
"fmt"
"reflect"

signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"

cosmos_proto "github.com/cosmos/cosmos-proto"

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
cosmos_proto "github.com/cosmos/cosmos-proto"

"cosmossdk.io/x/tx/signing"
"cosmossdk.io/x/tx/signing/textual/internal/textualpb"
Expand All @@ -41,7 +43,7 @@ type SignModeOptions struct {

// FileResolver are the protobuf files to use for resolving message
// descriptors. If it is nil, the global protobuf registry will be used.
FileResolver *protoregistry.Files
FileResolver signing.ProtoFileResolver

// TypeResolver are the protobuf type resolvers to use for resolving message
// types. If it is nil, then a dynamicpb will be used on top of FileResolver.
Expand All @@ -51,7 +53,7 @@ type SignModeOptions struct {
// SignModeHandler holds the configuration for dispatching
// to specific value renderers for SIGN_MODE_TEXTUAL.
type SignModeHandler struct {
fileResolver *protoregistry.Files
fileResolver signing.ProtoFileResolver
typeResolver protoregistry.MessageTypeResolver
coinMetadataQuerier CoinMetadataQueryFn
// scalars defines a registry for Cosmos scalars.
Expand Down