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

Tx CLI proto module interface #5989

Merged
merged 58 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
7d8b68c
WIP
aaronc Apr 9, 2020
c37ce56
WIP
aaronc Apr 9, 2020
66d5f7a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc Apr 13, 2020
11947b1
WIP on removing x/auth dependency from client/tx
aaronc Apr 13, 2020
387a963
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc Apr 28, 2020
6a0fa18
Revert unneeded changes
aaronc Apr 28, 2020
026a8ea
Simplify cli tx UX
aaronc Apr 28, 2020
d0cd45a
Wire up bank tx REST routes
aaronc Apr 28, 2020
97d3537
Fix assignment issue
aaronc Apr 28, 2020
db78c99
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 15, 2020
77a96a0
Wire up bank NewSendTxCmd
aaronc May 15, 2020
31ba870
fix lint
aaronc May 18, 2020
89bc11d
revert file
aaronc May 18, 2020
a2c9f54
revert file
aaronc May 18, 2020
fcc4e07
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 18, 2020
b7600bd
fix simcli
aaronc May 18, 2020
2f99c3f
Refactor AccountRetriever
aaronc May 19, 2020
cab64cd
Fix build
aaronc May 19, 2020
6af906d
Fix build
aaronc May 19, 2020
457a612
Fix build
aaronc May 19, 2020
d702137
Fix integration tests
aaronc May 19, 2020
3ab0141
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 19, 2020
7203652
Fix tests
aaronc May 19, 2020
fc0b01a
Docs, linting
aaronc May 19, 2020
aacd246
Linting
aaronc May 19, 2020
8ef7861
WIP on all modules
aaronc May 19, 2020
848c5e4
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 19, 2020
b6918f6
Implement other module new tx cmd's
aaronc May 19, 2020
ed494ce
Fix cmd's
aaronc May 19, 2020
c51730b
Refactor existing GetTxCmd
aaronc May 19, 2020
992cef3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 19, 2020
0c624ce
Fix cmd
aaronc May 20, 2020
414ef2c
Removing deprecated code
aaronc May 20, 2020
50c1044
Update ADR 020 & CHANGELOG
aaronc May 20, 2020
b47fdc1
Lint
aaronc May 20, 2020
aa16184
Lint
aaronc May 20, 2020
3d24b15
Lint
aaronc May 20, 2020
865bcd0
Lint
aaronc May 20, 2020
b3f030d
Lint
aaronc May 20, 2020
f952ed3
Lint
aaronc May 20, 2020
7dc136b
Lint
aaronc May 20, 2020
c19ba5b
Fix client/tx tests
aaronc May 20, 2020
e6d1ff3
Fix mocks
aaronc May 20, 2020
0d74f75
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 20, 2020
06ceb42
Fix tests
aaronc May 20, 2020
0632d70
Lint fixes
aaronc May 20, 2020
0eb278c
REST tx migration
aaronc May 20, 2020
4d9c978
Wire up REST
aaronc May 20, 2020
4ac82f0
Linting
aaronc May 20, 2020
753b792
Update CHANGELOG, docs
aaronc May 20, 2020
4ffd783
Fix tests
aaronc May 20, 2020
952a3da
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 20, 2020
b5460b5
lint
aaronc May 20, 2020
01f7f55
Address review feedback
aaronc May 21, 2020
d4c047f
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/586…
aaronc May 21, 2020
30b0c06
Update CHANGELOG.md
aaronc May 21, 2020
22f3b92
Update CHANGELOG.md
aaronc May 21, 2020
dd99d83
group vars
aaronc May 21, 2020
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
62 changes: 62 additions & 0 deletions client/context/client_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package context

import (
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types"
)

// AccountRetriever defines the interfaces required by transactions to
// ensure an account exists and to be able to query for account fields necessary
// for signing.
type AccountRetriever interface {
EnsureExists(addr types.AccAddress) error
GetAccountNumberSequence(addr types.AccAddress) (uint64, uint64, error)
}

type (
// TxGenerator defines an interface a client can utilize to generate an
// application-defined concrete transaction type. The type returned must
// implement ClientTx.
TxGenerator interface {
NewTx() ClientTx
NewFee() ClientFee
NewSignature() ClientSignature
}

ClientFee interface {
types.Fee
SetGas(uint64)
SetAmount(types.Coins)
}

ClientSignature interface {
types.Signature
SetPubKey(crypto.PubKey) error
SetSignature([]byte)
}

// ClientTx defines an interface which an application-defined concrete transaction
// type must implement. Namely, it must be able to set messages, generate
// signatures, and provide canonical bytes to sign over. The transaction must
// also know how to encode itself.
ClientTx interface {
types.Tx
codec.ProtoMarshaler

SetMsgs(...types.Msg) error
GetSignatures() []types.Signature
SetSignatures(...ClientSignature) error
GetFee() types.Fee
SetFee(ClientFee) error
GetMemo() string
SetMemo(string)

// CanonicalSignBytes returns the canonical JSON bytes to sign over, given a
// chain ID, along with an account and sequence number. The JSON encoding
// ensures all field names adhere to their proto definition, default values
// are omitted, and follows the JSON Canonical Form.
CanonicalSignBytes(cid string, num, seq uint64) ([]byte, error)
}
)
128 changes: 81 additions & 47 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ import (
// CLIContext implements a typical CLI context created in SDK modules for
// transaction handling and queries.
type CLIContext struct {
FromAddress sdk.AccAddress
Client rpcclient.Client
ChainID string
Marshaler codec.Marshaler
Input io.Reader
Keyring keyring.Keyring
Output io.Writer
OutputFormat string
Height int64
HomeDir string
NodeURI string
From string
BroadcastMode string
Verifier tmlite.Verifier
FromName string
TrustNode bool
UseLedger bool
Simulate bool
GenerateOnly bool
Offline bool
Indent bool
SkipConfirm bool
FromAddress sdk.AccAddress
Client rpcclient.Client
ChainID string
Marshaler codec.JSONMarshaler
Input io.Reader
Keyring keyring.Keyring
Output io.Writer
OutputFormat string
Height int64
HomeDir string
NodeURI string
From string
BroadcastMode string
Verifier tmlite.Verifier
FromName string
TrustNode bool
UseLedger bool
Simulate bool
GenerateOnly bool
Offline bool
Indent bool
SkipConfirm bool
TxGenerator TxGenerator
AccountRetriever AccountRetriever

// TODO: Deprecated (remove).
Codec *codec.Codec
Expand All @@ -58,24 +60,7 @@ type CLIContext struct {
func NewCLIContextWithInputAndFrom(input io.Reader, from string) CLIContext {
var nodeURI string
var rpc rpcclient.Client

homedir := viper.GetString(flags.FlagHome)
genOnly := viper.GetBool(flags.FlagGenerateOnly)
backend := viper.GetString(flags.FlagKeyringBackend)
if len(backend) == 0 {
backend = keyring.BackendMemory
}

keyring, err := newKeyringFromFlags(backend, homedir, input, genOnly)
if err != nil {
panic(fmt.Errorf("couldn't acquire keyring: %v", err))
}

fromAddress, fromName, err := GetFromFields(keyring, from, genOnly)
if err != nil {
fmt.Printf("failed to get from fields: %v\n", err)
os.Exit(1)
}
var err error

offline := viper.GetBool(flags.FlagOffline)
if !offline {
Expand All @@ -97,22 +82,19 @@ func NewCLIContextWithInputAndFrom(input io.Reader, from string) CLIContext {
Output: os.Stdout,
NodeURI: nodeURI,
From: viper.GetString(flags.FlagFrom),
Keyring: keyring,
OutputFormat: viper.GetString(cli.OutputFlag),
Height: viper.GetInt64(flags.FlagHeight),
HomeDir: homedir,
TrustNode: trustNode,
UseLedger: viper.GetBool(flags.FlagUseLedger),
BroadcastMode: viper.GetString(flags.FlagBroadcastMode),
Simulate: viper.GetBool(flags.FlagDryRun),
GenerateOnly: genOnly,
Offline: offline,
FromAddress: fromAddress,
FromName: fromName,
Indent: viper.GetBool(flags.FlagIndentResponse),
SkipConfirm: viper.GetBool(flags.FlagSkipConfirmation),
}

ctx.InitWithInputAndFrom(input, from)

if offline {
return ctx
}
Expand Down Expand Up @@ -147,6 +129,46 @@ func NewCLIContextWithInput(input io.Reader) CLIContext {
return NewCLIContextWithInputAndFrom(input, viper.GetString(flags.FlagFrom))
}

// InitWithInputAndFrom re-initializes an existing CLI with a new io.Reader and from parameter
func (ctx *CLIContext) InitWithInputAndFrom(input io.Reader, from string) CLIContext {
homedir := viper.GetString(flags.FlagHome)
genOnly := viper.GetBool(flags.FlagGenerateOnly)
backend := viper.GetString(flags.FlagKeyringBackend)
if len(backend) == 0 {
backend = keyring.BackendMemory
}

kr, err := newKeyringFromFlags(backend, homedir, input, genOnly)
if err != nil {
panic(fmt.Errorf("couldn't acquire keyring: %v", err))
}

fromAddress, fromName, err := GetFromFields(kr, from, genOnly)
if err != nil {
fmt.Printf("failed to get from fields: %v\n", err)
os.Exit(1)
}

ctx.HomeDir = homedir

return ctx.
WithKeyring(kr).
WithFromAddress(fromAddress).
WithFromName(fromName).
WithGenerateOnly(genOnly)

}

// InitWithInputAndFrom re-initializes an existing CLI with a new io.Reader
func (ctx *CLIContext) InitWithInput(input io.Reader) CLIContext {
return ctx.InitWithInputAndFrom(input, viper.GetString(flags.FlagFrom))
}

// InitWithInputAndFrom re-initializes an existing CLI with a new from parameter
func (ctx *CLIContext) InitWithFrom(from string) CLIContext {
return ctx.InitWithInputAndFrom(os.Stdin, from)
}

// WithKeyring returns a copy of the context with an updated keyring.
func (ctx CLIContext) WithKeyring(k keyring.Keyring) CLIContext {
ctx.Keyring = k
Expand All @@ -159,8 +181,8 @@ func (ctx CLIContext) WithInput(r io.Reader) CLIContext {
return ctx
}

// WithMarshaler returns a copy of the CLIContext with an updated Marshaler.
func (ctx CLIContext) WithMarshaler(m codec.Marshaler) CLIContext {
// WithMarshaler returns a copy of the CLIContext with an updated JSONMarshaler.
func (ctx CLIContext) WithMarshaler(m codec.JSONMarshaler) CLIContext {
ctx.Marshaler = m
return ctx
}
Expand Down Expand Up @@ -265,6 +287,18 @@ func (ctx CLIContext) WithBroadcastMode(mode string) CLIContext {
return ctx
}

// WithTxGenerator returns the context with an updated TxGenerator
func (ctx CLIContext) WithTxGenerator(generator TxGenerator) CLIContext {
ctx.TxGenerator = generator
return ctx
}

// WithAccountRetriever returns the context with an updated AccountRetriever
func (ctx CLIContext) WithAccountRetriever(retriever AccountRetriever) CLIContext {
ctx.AccountRetriever = retriever
return ctx
}

// Println outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.Marshaler. An error is returned upon failure.
Expand Down
40 changes: 17 additions & 23 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ package tx
import (
"io"

"github.com/cosmos/cosmos-sdk/client/context"

"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// AccountRetriever defines the interfaces required for use by the Factory to
// ensure an account exists and to be able to query for account fields necessary
// for signing.
type AccountRetriever interface {
EnsureExists(addr sdk.AccAddress) error
GetAccountNumberSequence(addr sdk.AccAddress) (uint64, uint64, error)
}

// Factory defines a client transaction factory that facilitates generating and
// signing an application-specific transaction.
type Factory struct {
keybase keyring.Keyring
txGenerator Generator
accountRetriever AccountRetriever
txGenerator context.TxGenerator
accountRetriever context.AccountRetriever
accountNumber uint64
sequence uint64
gas uint64
Expand Down Expand Up @@ -64,29 +58,29 @@ func NewFactoryFromCLI(input io.Reader) Factory {
}

// nolint
func (f Factory) AccountNumber() uint64 { return f.accountNumber }
func (f Factory) Sequence() uint64 { return f.sequence }
func (f Factory) Gas() uint64 { return f.gas }
func (f Factory) GasAdjustment() float64 { return f.gasAdjustment }
func (f Factory) Keybase() keyring.Keyring { return f.keybase }
func (f Factory) ChainID() string { return f.chainID }
func (f Factory) Memo() string { return f.memo }
func (f Factory) Fees() sdk.Coins { return f.fees }
func (f Factory) GasPrices() sdk.DecCoins { return f.gasPrices }
func (f Factory) AccountRetriever() AccountRetriever { return f.accountRetriever }
func (f Factory) AccountNumber() uint64 { return f.accountNumber }
func (f Factory) Sequence() uint64 { return f.sequence }
func (f Factory) Gas() uint64 { return f.gas }
func (f Factory) GasAdjustment() float64 { return f.gasAdjustment }
func (f Factory) Keybase() keyring.Keyring { return f.keybase }
func (f Factory) ChainID() string { return f.chainID }
func (f Factory) Memo() string { return f.memo }
func (f Factory) Fees() sdk.Coins { return f.fees }
func (f Factory) GasPrices() sdk.DecCoins { return f.gasPrices }
func (f Factory) AccountRetriever() context.AccountRetriever { return f.accountRetriever }

// SimulateAndExecute returns the option to simulate and then execute the transaction
// using the gas from the simulation results
func (f Factory) SimulateAndExecute() bool { return f.simulateAndExecute }

// WithTxGenerator returns a copy of the Factory with an updated Generator.
func (f Factory) WithTxGenerator(g Generator) Factory {
// WithTxGenerator returns a copy of the Factory with an updated TxGenerator.
func (f Factory) WithTxGenerator(g context.TxGenerator) Factory {
f.txGenerator = g
return f
}

// WithAccountRetriever returns a copy of the Factory with an updated AccountRetriever.
func (f Factory) WithAccountRetriever(ar AccountRetriever) Factory {
func (f Factory) WithAccountRetriever(ar context.AccountRetriever) Factory {
f.accountRetriever = ar
return f
}
Expand Down
Loading