From c49af0b8dd1db91dfa3a79a749ca8f287922ab7d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 13 Mar 2020 13:59:14 -0400 Subject: [PATCH] Update context --- client/context/context.go | 20 ++++++++++++++++++- client/{ => tx}/tx.go | 10 +--------- codec/std/tx.go | 16 ++++++++++++--- .../adr-020-protobuf-transaction-encoding.md | 13 +++--------- 4 files changed, 36 insertions(+), 23 deletions(-) rename client/{ => tx}/tx.go (75%) diff --git a/client/context/context.go b/client/context/context.go index 28bfe8e83..eec74e7fe 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -14,6 +14,7 @@ import ( rpcclient "github.com/tendermint/tendermint/rpc/client" "github.com/cosmos/cosmos-sdk/client/flags" + clientx "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" @@ -25,6 +26,8 @@ type CLIContext struct { FromAddress sdk.AccAddress Client rpcclient.Client ChainID string + TxGenerator clientx.TxGenerator + Marshaler codec.Marshaler Keybase keys.Keybase Input io.Reader Output io.Writer @@ -36,13 +39,15 @@ type CLIContext struct { BroadcastMode string Verifier tmlite.Verifier FromName string - Codec *codec.Codec TrustNode bool UseLedger bool Simulate bool GenerateOnly bool Indent bool SkipConfirm bool + + // TODO: Deprecated (remove). + Codec *codec.Codec } // NewCLIContextWithInputAndFrom returns a new initialized CLIContext with parameters from the @@ -130,7 +135,20 @@ func (ctx CLIContext) WithInput(r io.Reader) CLIContext { return ctx } +// WithTxGenerator returns a copy of the CLIContext with an updated TxGenerator. +func (ctx CLIContext) WithTxGenerator(txg clientx.TxGenerator) CLIContext { + ctx.TxGenerator = txg + return ctx +} + +// WithMarshaler returns a copy of the CLIContext with an updated Marshaler. +func (ctx CLIContext) WithMarshaler(m codec.Marshaler) CLIContext { + ctx.Marshaler = m + return ctx +} + // WithCodec returns a copy of the context with an updated codec. +// TODO: Deprecated (remove). func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext { ctx.Codec = cdc return ctx diff --git a/client/tx.go b/client/tx/tx.go similarity index 75% rename from client/tx.go rename to client/tx/tx.go index d7fdb8298..ead819f33 100644 --- a/client/tx.go +++ b/client/tx/tx.go @@ -1,4 +1,4 @@ -package client +package tx import ( "github.com/cosmos/cosmos-sdk/codec" @@ -6,14 +6,6 @@ import ( ) type ( - // ClientMarshaler defines an interface that REST and CLI handler will use to - // create application-specific transactions and be able to serialize types - // specific to the application including transactions. - ClientMarshaler interface { - TxGenerator - codec.Marshaler - } - // TxGenerator defines an interface a client can utilize to generate an // application-defined concrete transaction type. The type returned must // implement ClientTx. diff --git a/codec/std/tx.go b/codec/std/tx.go index a8aadf089..2a9b9af29 100644 --- a/codec/std/tx.go +++ b/codec/std/tx.go @@ -1,17 +1,27 @@ package std import ( - "github.com/cosmos/cosmos-sdk/client" + clientx "github.com/cosmos/cosmos-sdk/client/tx" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/auth" ) var ( - _ sdk.Tx = (*Transaction)(nil) - _ client.ClientTx = (*Transaction)(nil) + _ sdk.Tx = (*Transaction)(nil) + _ clientx.ClientTx = (*Transaction)(nil) + _ clientx.TxGenerator = TxGenerator{} ) +// TxGenerator defines a transaction generator that allows clients to construct +// transactions. +type TxGenerator struct{} + +// NewTx returns a reference to an empty Transaction type. +func (TxGenerator) NewTx() clientx.ClientTx { + return &Transaction{} +} + func NewTransaction(fee auth.StdFee, memo string, sdkMsgs []sdk.Msg) (*Transaction, error) { tx := &Transaction{ StdTxBase: auth.NewStdTxBase(fee, nil, memo), diff --git a/docs/architecture/adr-020-protobuf-transaction-encoding.md b/docs/architecture/adr-020-protobuf-transaction-encoding.md index 75cc85e32..d5a72f3af 100644 --- a/docs/architecture/adr-020-protobuf-transaction-encoding.md +++ b/docs/architecture/adr-020-protobuf-transaction-encoding.md @@ -123,18 +123,11 @@ type ClientTx interface { } ``` -We then extend `codec.Marshaler` to also require fulfillment of `TxGenerator`. +We then update `CLIContext` to have two new fields: `TxGenerator` and `TxGenerator`. -```go -type ClientMarshaler interface { - TxGenerator - codec.Marshaler -} -``` - -Then, each module will at the minimum accept a `ClientMarshaler` instead of a concrete +Then, each module will at the minimum accept a `Marshaler` instead of a concrete Amino codec. If the module needs to work with any interface types, it will use -the `Codec` interface defined by the module which also extends `ClientMarshaler`. +the `Codec` interface defined by the module which also extends `Marshaler`. ## Future Improvements