Skip to content

Commit

Permalink
feat(x/tx): Support gogo registry in Textual (cosmos#15302)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Mar 20, 2023
1 parent bf85bfb commit 124aed4
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 107 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
cosmossdk.io/log v0.1.0
cosmossdk.io/math v1.0.0-rc.0
cosmossdk.io/store v0.1.0-alpha.1
cosmossdk.io/x/tx v0.3.0
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f
github.com/99designs/keyring v1.2.1
github.com/armon/go-metrics v0.4.1
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ cosmossdk.io/math v1.0.0-rc.0 h1:ml46ukocrAAoBpYKMidF0R2tQJ1Uxfns0yH8wqgMAFc=
cosmossdk.io/math v1.0.0-rc.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/store v0.1.0-alpha.1 h1:NGomhLUXzAxvK4OF8+yP6eNUG5i4LwzOzx+S494pTCg=
cosmossdk.io/store v0.1.0-alpha.1/go.mod h1:kmCMbhrleCZ6rDZPY/EGNldNvPebFNyVPFYp+pv05/k=
cosmossdk.io/x/tx v0.3.0 h1:AgVYy6bxL3XqEV7RLyxFh1uT+wywsrbgVMmYnL3FgWM=
cosmossdk.io/x/tx v0.3.0/go.mod h1:ELY0bP2SmOqyffJFp00g979xsI1zBdmc55A6JCi1Qe8=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f h1:yXEE3D6L0Ykwlp4FuS1SoHgT9vZ8brBJ/dkHezXBU9o=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f/go.mod h1:V/7DjCSReJ7LBBYrNtVFUec7t63hVNyFh0vBXOBK2Yg=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
cloud.google.com/go/storage v1.29.0 // indirect
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/x/tx v0.3.0 // indirect
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ cosmossdk.io/math v1.0.0-rc.0 h1:ml46ukocrAAoBpYKMidF0R2tQJ1Uxfns0yH8wqgMAFc=
cosmossdk.io/math v1.0.0-rc.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/store v0.1.0-alpha.1 h1:NGomhLUXzAxvK4OF8+yP6eNUG5i4LwzOzx+S494pTCg=
cosmossdk.io/store v0.1.0-alpha.1/go.mod h1:kmCMbhrleCZ6rDZPY/EGNldNvPebFNyVPFYp+pv05/k=
cosmossdk.io/x/tx v0.3.0 h1:AgVYy6bxL3XqEV7RLyxFh1uT+wywsrbgVMmYnL3FgWM=
cosmossdk.io/x/tx v0.3.0/go.mod h1:ELY0bP2SmOqyffJFp00g979xsI1zBdmc55A6JCi1Qe8=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f h1:yXEE3D6L0Ykwlp4FuS1SoHgT9vZ8brBJ/dkHezXBU9o=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f/go.mod h1:V/7DjCSReJ7LBBYrNtVFUec7t63hVNyFh0vBXOBK2Yg=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
6 changes: 5 additions & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ func NewRootCmd() *cobra.Command {
// TODO Currently, the TxConfig below doesn't include Textual, so
// an error will arise when using the --textual flag.
// ref: https://github.com/cosmos/cosmos-sdk/issues/11970
txt, err := txmodule.NewTextualWithGRPCConn(initClientCtx)
if err != nil {
return err
}
txConfigWithTextual := tx.NewTxConfigWithTextual(
codec.NewProtoCodec(encodingConfig.InterfaceRegistry),
encodingConfig.TxConfig.SignModeHandler().Modes(),
txmodule.NewTextualWithGRPCConn(initClientCtx),
txt,
)
initClientCtx = initClientCtx.WithTxConfig(txConfigWithTextual)

Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.0.0-20230117113717-50e7c4a4ceff
cosmossdk.io/x/nft v0.0.0-20230113085233-fae3332d62fc
cosmossdk.io/x/tx v0.3.0
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f
cosmossdk.io/x/upgrade v0.0.0-20230127052425-54c8e1568335
github.com/cometbft/cometbft v0.37.0
github.com/cosmos/cosmos-db v1.0.0-rc.1
Expand Down
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ cosmossdk.io/math v1.0.0-rc.0 h1:ml46ukocrAAoBpYKMidF0R2tQJ1Uxfns0yH8wqgMAFc=
cosmossdk.io/math v1.0.0-rc.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/store v0.1.0-alpha.1 h1:NGomhLUXzAxvK4OF8+yP6eNUG5i4LwzOzx+S494pTCg=
cosmossdk.io/store v0.1.0-alpha.1/go.mod h1:kmCMbhrleCZ6rDZPY/EGNldNvPebFNyVPFYp+pv05/k=
cosmossdk.io/x/tx v0.3.0 h1:AgVYy6bxL3XqEV7RLyxFh1uT+wywsrbgVMmYnL3FgWM=
cosmossdk.io/x/tx v0.3.0/go.mod h1:ELY0bP2SmOqyffJFp00g979xsI1zBdmc55A6JCi1Qe8=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f h1:yXEE3D6L0Ykwlp4FuS1SoHgT9vZ8brBJ/dkHezXBU9o=
cosmossdk.io/x/tx v0.3.1-0.20230320072322-5fceb7c0495f/go.mod h1:V/7DjCSReJ7LBBYrNtVFUec7t63hVNyFh0vBXOBK2Yg=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
8 changes: 6 additions & 2 deletions x/auth/ante/sigverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ func TestSigVerification(t *testing.T) {
enabledSignModes := []signing.SignMode{signing.SignMode_SIGN_MODE_DIRECT, signing.SignMode_SIGN_MODE_TEXTUAL, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}
// Since TEXTUAL is not enabled by default, we create a custom TxConfig
// here which includes it.
txt, err := txmodule.NewTextualWithGRPCConn(suite.clientCtx)
require.NoError(t, err)
suite.clientCtx.TxConfig = authtx.NewTxConfigWithTextual(
codec.NewProtoCodec(suite.encCfg.InterfaceRegistry),
enabledSignModes,
txmodule.NewTextualWithGRPCConn(suite.clientCtx),
txt,
)
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()

Expand All @@ -161,10 +163,12 @@ func TestSigVerification(t *testing.T) {
gasLimit := testdata.NewTestGasLimit()

spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper)
txt, err = txmodule.NewTextualWithBankKeeper(suite.txBankKeeper)
require.NoError(t, err)
anteTxConfig := authtx.NewTxConfigWithTextual(
codec.NewProtoCodec(suite.encCfg.InterfaceRegistry),
enabledSignModes,
txmodule.NewTextualWithBankKeeper(suite.txBankKeeper),
txt,
)
svd := ante.NewSigVerificationDecorator(suite.accountKeeper, anteTxConfig.SignModeHandler())
antehandler := sdk.ChainAnteDecorators(spkd, svd)
Expand Down
5 changes: 4 additions & 1 deletion x/auth/tx/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ type TxOutputs struct {
}

func ProvideModule(in TxInputs) TxOutputs {
textual := NewTextualWithBankKeeper(in.TxBankKeeper)
textual, err := NewTextualWithBankKeeper(in.TxBankKeeper)
if err != nil {
panic(err)
}
var txConfig client.TxConfig
if in.CustomSignModeHandlers == nil {
txConfig = tx.NewTxConfigWithTextual(in.ProtoCodecMarshaler, tx.DefaultSignModes, textual)
Expand Down
89 changes: 52 additions & 37 deletions x/auth/tx/config/textual.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tx
import (
"context"

gogoproto "github.com/cosmos/gogoproto/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
Expand All @@ -21,17 +22,25 @@ import (
//
// clientCtx := client.GetClientContextFromCmd(cmd)
// txt := tx.NewTextualWithGRPCConn(clientCtxx)
func NewTextualWithGRPCConn(grpcConn grpc.ClientConnInterface) *textual.SignModeHandler {
return textual.NewSignModeHandler(func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
bankQueryClient := bankv1beta1.NewQueryClient(grpcConn)
res, err := bankQueryClient.DenomMetadata(ctx, &bankv1beta1.QueryDenomMetadataRequest{
Denom: denom,
})
if err != nil {
return nil, metadataExists(err)
}
func NewTextualWithGRPCConn(grpcConn grpc.ClientConnInterface) (*textual.SignModeHandler, error) {
protoFiles, err := gogoproto.MergedRegistry()
if err != nil {
return nil, err
}

return textual.NewSignModeHandler(textual.SignModeOptions{
CoinMetadataQuerier: func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
bankQueryClient := bankv1beta1.NewQueryClient(grpcConn)
res, err := bankQueryClient.DenomMetadata(ctx, &bankv1beta1.QueryDenomMetadataRequest{
Denom: denom,
})
if err != nil {
return nil, metadataExists(err)
}

return res.Metadata, nil
return res.Metadata, nil
},
FileResolver: protoFiles,
})
}

Expand All @@ -41,37 +50,43 @@ func NewTextualWithGRPCConn(grpcConn grpc.ClientConnInterface) *textual.SignMode
// Note: Once we switch to ADR-033, and keepers become ADR-033 clients to each
// other, this function could probably be deprecated in favor of
// `NewTextualWithGRPCConn`.
func NewTextualWithBankKeeper(bk BankKeeper) *textual.SignModeHandler {
textual := textual.NewSignModeHandler(func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
res, err := bk.DenomMetadata(ctx, &types.QueryDenomMetadataRequest{Denom: denom})
if err != nil {
return nil, metadataExists(err)
}
func NewTextualWithBankKeeper(bk BankKeeper) (*textual.SignModeHandler, error) {
protoFiles, err := gogoproto.MergedRegistry()
if err != nil {
return nil, err
}

m := &bankv1beta1.Metadata{
Base: res.Metadata.Base,
Display: res.Metadata.Display,
// fields below are not strictly needed by Textual
// but added here for completeness.
Description: res.Metadata.Description,
Name: res.Metadata.Name,
Symbol: res.Metadata.Symbol,
Uri: res.Metadata.URI,
UriHash: res.Metadata.URIHash,
}
m.DenomUnits = make([]*bankv1beta1.DenomUnit, len(res.Metadata.DenomUnits))
for i, d := range res.Metadata.DenomUnits {
m.DenomUnits[i] = &bankv1beta1.DenomUnit{
Denom: d.Denom,
Exponent: d.Exponent,
Aliases: d.Aliases,
return textual.NewSignModeHandler(textual.SignModeOptions{
CoinMetadataQuerier: func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
res, err := bk.DenomMetadata(ctx, &types.QueryDenomMetadataRequest{Denom: denom})
if err != nil {
return nil, metadataExists(err)
}
}

return m, nil
})
m := &bankv1beta1.Metadata{
Base: res.Metadata.Base,
Display: res.Metadata.Display,
// fields below are not strictly needed by Textual
// but added here for completeness.
Description: res.Metadata.Description,
Name: res.Metadata.Name,
Symbol: res.Metadata.Symbol,
Uri: res.Metadata.URI,
UriHash: res.Metadata.URIHash,
}
m.DenomUnits = make([]*bankv1beta1.DenomUnit, len(res.Metadata.DenomUnits))
for i, d := range res.Metadata.DenomUnits {
m.DenomUnits[i] = &bankv1beta1.DenomUnit{
Denom: d.Denom,
Exponent: d.Exponent,
Aliases: d.Aliases,
}
}

return textual
return m, nil
},
FileResolver: protoFiles,
})
}

// metadataExists parses the error, and only propagates the error if it's
Expand Down
6 changes: 6 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
## API Breaking

* [#15278](https://github.com/cosmos/cosmos-sdk/pull/15278) Move `x/tx/{textual,aminojson}` into `x/tx/signing`.
* [#15302](https://github.com/cosmos/cosmos-sdk/pull/15302) `textual.NewSignModeHandler` now takes an options struct instead of a simple coin querier argument. It also returns an error.

## Improvements

* [#15302](https://github.com/cosmos/cosmos-sdk/pull/15302) Add support for a custom registry (e.g. gogo's MergedRegistry) to be plugged into SIGN_MODE_TEXTUAL.

13 changes: 6 additions & 7 deletions x/tx/signing/std/handler_map.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package std

import (
"fmt"

"cosmossdk.io/x/tx/signing"
"cosmossdk.io/x/tx/signing/direct"
"cosmossdk.io/x/tx/signing/textual"
)

// SignModeOptions are options for configuring the standard sign mode handler map.
type SignModeOptions struct {
// CoinMetadataQueryFn is the CoinMetadataQueryFn required for SIGN_MODE_TEXTUAL.
CoinMetadataQueryFn textual.CoinMetadataQueryFn
// Textual are options for SIGN_MODE_TEXTUAL
Textual textual.SignModeOptions
}

// HandlerMap returns a sign mode handler map that Cosmos SDK apps can use out
// of the box to support all "standard" sign modes.
func (s SignModeOptions) HandlerMap() (*signing.HandlerMap, error) {
if s.CoinMetadataQueryFn == nil {
return nil, fmt.Errorf("missing %T needed for SIGN_MODE_TEXTUAL", s.CoinMetadataQueryFn)
txt, err := textual.NewSignModeHandler(s.Textual)
if err != nil {
return nil, err
}

return signing.NewHandlerMap(
direct.SignModeHandler{},
textual.NewSignModeHandler(s.CoinMetadataQueryFn),
txt,
), nil
}
35 changes: 26 additions & 9 deletions x/tx/signing/textual/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package textual
import (
"context"
"fmt"
"strings"

"github.com/cosmos/cosmos-proto/anyutil"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/anypb"
)

Expand All @@ -26,17 +28,17 @@ func NewAnyValueRenderer(t *SignModeHandler) ValueRenderer {
// Format implements the ValueRenderer interface.
func (ar anyValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]Screen, error) {
msg := v.Message().Interface()
omitHeader := 0

anymsg, ok := msg.(*anypb.Any)
if !ok {
return nil, fmt.Errorf("expected Any, got %T", msg)
anymsg := &anypb.Any{}
err := coerceToMessage(msg, anymsg)
if err != nil {
return nil, err
}

internalMsg, err := anymsg.UnmarshalNew()
internalMsg, err := anyutil.Unpack(anymsg, ar.tr.fileResolver, ar.tr.typeResolver)
if err != nil {
return nil, fmt.Errorf("error unmarshalling any %s: %w", anymsg.TypeUrl, err)
return nil, err
}

vr, err := ar.tr.GetMessageValueRenderer(internalMsg.ProtoReflect().Descriptor())
if err != nil {
return nil, err
Expand All @@ -48,6 +50,7 @@ func (ar anyValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]
}

// The Any value renderer suppresses emission of the object header
omitHeader := 0
_, isMsgRenderer := vr.(*messageValueRenderer)
if isMsgRenderer && subscreens[0].Content == fmt.Sprintf("%s object", internalMsg.ProtoReflect().Descriptor().Name()) {
omitHeader = 1
Expand All @@ -72,8 +75,22 @@ func (ar anyValueRenderer) Parse(ctx context.Context, screens []Screen) (protore
return nilValue, fmt.Errorf("bad indentation: want 0, got %d", screens[0].Indent)
}

msgType, err := protoregistry.GlobalTypes.FindMessageByURL(screens[0].Content)
if err != nil {
typeURL := screens[0].Content
msgType, err := ar.tr.typeResolver.FindMessageByURL(typeURL)
if err == protoregistry.NotFound {
// If the proto v2 registry doesn't have this message, then we use
// protoFiles (which can e.g. be initialized to gogo's MergedRegistry)
// to retrieve the message descriptor, and then use dynamicpb on that
// message descriptor to create a proto.Message
typeURL := strings.TrimPrefix(typeURL, "/")

msgDesc, err := ar.tr.fileResolver.FindDescriptorByName(protoreflect.FullName(typeURL))
if err != nil {
return nilValue, fmt.Errorf("textual protoFiles does not have descriptor %s: %w", typeURL, err)
}

msgType = dynamicpb.NewMessageType(msgDesc.(protoreflect.MessageDescriptor))
} else if err != nil {
return nilValue, err
}
vr, err := ar.tr.GetMessageValueRenderer(msgType.Descriptor())
Expand Down
Loading

0 comments on commit 124aed4

Please sign in to comment.