From 9befc6ced86398204381264901590ba7bf2e924f Mon Sep 17 00:00:00 2001 From: Jack Zampolin Date: Tue, 27 Oct 2020 06:53:54 -0700 Subject: [PATCH] Wrap ProtoCodec in interface (#7637) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * WIP encoding change * Add test that describes issue * WIP debugging * remove extra code * Update codec/proto_codec.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- codec/proto_codec.go | 8 ++++++++ x/auth/tx/config.go | 4 ++-- x/auth/tx/decoder.go | 4 ++-- x/auth/tx/encoder.go | 2 +- x/ibc/core/03-connection/types/codec.go | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/codec/proto_codec.go b/codec/proto_codec.go index c9123f5d754c..e77409fe4829 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -11,6 +11,13 @@ import ( "github.com/gogo/protobuf/proto" ) +// ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both +// binary and JSON encoding. +type ProtoCodecMarshaler interface { + Marshaler + InterfaceRegistry() types.InterfaceRegistry +} + // ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON // encoding. type ProtoCodec struct { @@ -18,6 +25,7 @@ type ProtoCodec struct { } var _ Marshaler = &ProtoCodec{} +var _ ProtoCodecMarshaler = &ProtoCodec{} // NewProtoCodec returns a reference to a new ProtoCodec func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec { diff --git a/x/auth/tx/config.go b/x/auth/tx/config.go index b7ca179a84f3..8402423dbf77 100644 --- a/x/auth/tx/config.go +++ b/x/auth/tx/config.go @@ -18,12 +18,12 @@ type config struct { encoder sdk.TxEncoder jsonDecoder sdk.TxDecoder jsonEncoder sdk.TxEncoder - protoCodec *codec.ProtoCodec + protoCodec codec.ProtoCodecMarshaler } // NewTxConfig returns a new protobuf TxConfig using the provided ProtoCodec and sign modes. The // first enabled sign mode will become the default sign mode. -func NewTxConfig(protoCodec *codec.ProtoCodec, enabledSignModes []signingtypes.SignMode) client.TxConfig { +func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode) client.TxConfig { return &config{ handler: makeSignModeHandler(enabledSignModes), decoder: DefaultTxDecoder(protoCodec), diff --git a/x/auth/tx/decoder.go b/x/auth/tx/decoder.go index 59ba8467ebde..5f48ddd3aae6 100644 --- a/x/auth/tx/decoder.go +++ b/x/auth/tx/decoder.go @@ -9,7 +9,7 @@ import ( ) // DefaultTxDecoder returns a default protobuf TxDecoder using the provided Marshaler. -func DefaultTxDecoder(cdc *codec.ProtoCodec) sdk.TxDecoder { +func DefaultTxDecoder(cdc codec.ProtoCodecMarshaler) sdk.TxDecoder { return func(txBytes []byte) (sdk.Tx, error) { var raw tx.TxRaw @@ -66,7 +66,7 @@ func DefaultTxDecoder(cdc *codec.ProtoCodec) sdk.TxDecoder { } // DefaultJSONTxDecoder returns a default protobuf JSON TxDecoder using the provided Marshaler. -func DefaultJSONTxDecoder(cdc *codec.ProtoCodec) sdk.TxDecoder { +func DefaultJSONTxDecoder(cdc codec.ProtoCodecMarshaler) sdk.TxDecoder { return func(txBytes []byte) (sdk.Tx, error) { var theTx tx.Tx err := cdc.UnmarshalJSON(txBytes, &theTx) diff --git a/x/auth/tx/encoder.go b/x/auth/tx/encoder.go index 5655df7686d7..35cecac556eb 100644 --- a/x/auth/tx/encoder.go +++ b/x/auth/tx/encoder.go @@ -29,7 +29,7 @@ func DefaultTxEncoder() sdk.TxEncoder { } // DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler. -func DefaultJSONTxEncoder(cdc *codec.ProtoCodec) sdk.TxEncoder { +func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) sdk.TxEncoder { return func(tx sdk.Tx) ([]byte, error) { txWrapper, ok := tx.(*wrapper) if ok { diff --git a/x/ibc/core/03-connection/types/codec.go b/x/ibc/core/03-connection/types/codec.go index dec4826210e3..9caa35332be6 100644 --- a/x/ibc/core/03-connection/types/codec.go +++ b/x/ibc/core/03-connection/types/codec.go @@ -39,7 +39,7 @@ var ( // SubModuleCdc references the global x/ibc/core/03-connection module codec. Note, the codec should // ONLY be used in certain instances of tests and for JSON encoding. // - // The actual codec used for serialization should be provided to x/ibc/core/03-connectionl and + // The actual codec used for serialization should be provided to x/ibc/core/03-connection and // defined at the application level. SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) )