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

client: rename CLIContext to Context #6290

Merged
merged 25 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
36534f0
Refactor CliContext as Context
sahith-narahari May 27, 2020
f59741e
Fix lint issues
sahith-narahari May 27, 2020
64f7227
Fix goimports
sahith-narahari May 27, 2020
c5dc178
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/ref…
sahith-narahari May 27, 2020
4c7c91c
Fix gov tests
sahith-narahari May 27, 2020
42754e5
Resolved ci-lint issues
sahith-narahari May 27, 2020
29717fa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/ref…
sahith-narahari May 27, 2020
3158bcd
Add changelog
sahith-narahari May 27, 2020
89af179
Merge branch 'master' into sahith/refactor-cli-ctx
May 28, 2020
5971595
Rename cliCtx to clientCtx
sahith-narahari May 29, 2020
e282a79
Merge branch 'sahith/refactor-cli-ctx' of github.com:cosmos/cosmos-sd…
sahith-narahari May 29, 2020
d8852d1
Fix mocks and routes
sahith-narahari May 29, 2020
85f1cc6
Add changelog
sahith-narahari May 29, 2020
8ebd1f2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/ref…
sahith-narahari May 29, 2020
2f6ed7e
Update changelog
sahith-narahari May 29, 2020
3324981
Apply suggestions from code review
fedekunze May 29, 2020
e9100a5
Merge branch 'master' into sahith/refactor-cli-ctx
May 29, 2020
f67895a
merge client/rpc/ro{ot,utes}.go
May 29, 2020
ca2698a
Update docs
sahith-narahari May 29, 2020
5d0ecd4
Merge branch 'sahith/refactor-cli-ctx' of github.com:cosmos/cosmos-sd…
sahith-narahari May 29, 2020
d0d8624
client/rpc: remove redundant client/rpc.RegisterRPCRoutes
May 29, 2020
21ee9e5
regenerate mocks
May 29, 2020
dfd8688
Update ADRs
sahith-narahari May 30, 2020
e74b39a
Merge branch 'sahith/refactor-cli-ctx' of github.com:cosmos/cosmos-sd…
sahith-narahari May 30, 2020
81fb833
Merge branch 'master' into sahith/refactor-cli-ctx
mergify[bot] May 31, 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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
- TxBuilder.BuildAndSign
- TxBuilder.Sign
- TxBuilder.SignStdTx
* (client) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `CLIContext` is renamed to `Context`. `Context` and all related methods have been moved from package context to client.

Migration guide:

```go
cliCtx := context.CLIContext{}
```

Now becomes:

```go
clientCtx = client.Context{}
```
* (client/rpc) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `RegisterRoutes` of rpc is moved from package client to client/rpc and client/rpc.RegisterRPCRoutes is removed.
* (client/lcd) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `CliCtx` of struct `RestServer` in package client/lcd has been renamed to `ClientCtx`.

### Features

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build-sim: go.sum
build-sim

mocks: $(MOCKS_DIR)
mockgen -source=client/context/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
mockgen -source=client/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
mockgen -package mocks -destination tests/mocks/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
mockgen -source=types/module/module.go -package mocks -destination tests/mocks/types_module_module.go
mockgen -source=types/invariant.go -package mocks -destination tests/mocks/types_invariant.go
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package context
package client

import "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -18,4 +18,4 @@ type NodeQuerier interface {
QueryWithData(path string, data []byte) ([]byte, int64, error)
}

var _ NodeQuerier = CLIContext{}
var _ NodeQuerier = Context{}
10 changes: 5 additions & 5 deletions client/context/broadcast.go → client/broadcast.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package context
package client

import (
"fmt"
Expand All @@ -16,7 +16,7 @@ import (
// based on the context parameters. The result of the broadcast is parsed into
// an intermediate structure which is logged if the context has a logger
// defined.
func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
func (ctx Context) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
switch ctx.BroadcastMode {
case flags.BroadcastSync:
res, err = ctx.BroadcastTxSync(txBytes)
Expand Down Expand Up @@ -84,7 +84,7 @@ func CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
// NOTE: This should ideally not be used as the request may timeout but the tx
// may still be included in a block. Use BroadcastTxAsync or BroadcastTxSync
// instead.
func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
Expand Down Expand Up @@ -112,7 +112,7 @@ func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error)

// BroadcastTxSync broadcasts transaction bytes to a Tendermint node
// synchronously (i.e. returns after CheckTx execution).
func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
Expand All @@ -128,7 +128,7 @@ func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {

// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
// asynchronously (i.e. returns immediately).
func (ctx CLIContext) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
Expand Down
6 changes: 3 additions & 3 deletions client/context/broadcast_test.go → client/broadcast_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package context
package client

import (
"fmt"
Expand Down Expand Up @@ -32,8 +32,8 @@ func (c MockClient) BroadcastTxSync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, e
return nil, c.err
}

func CreateContextWithErrorAndMode(err error, mode string) CLIContext {
return CLIContext{
func CreateContextWithErrorAndMode(err error, mode string) Context {
return Context{
Client: MockClient{err: err},
BroadcastMode: mode,
}
Expand Down
102 changes: 51 additions & 51 deletions client/context/context.go → client/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package context
package client

import (
"bufio"
Expand All @@ -21,9 +21,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// CLIContext implements a typical CLI context created in SDK modules for
// Context implements a typical context created in SDK modules for
// transaction handling and queries.
type CLIContext struct {
type Context struct {
FromAddress sdk.AccAddress
Client rpcclient.Client
ChainID string
Expand Down Expand Up @@ -53,40 +53,40 @@ type CLIContext struct {
Codec *codec.Codec
}

// NewCLIContextWithInputAndFrom returns a new initialized CLIContext with parameters from the
// NewContextWithInputAndFrom returns a new initialized Context with parameters from the
// command line using Viper. It takes a io.Reader and and key name or address and populates
// the FromName and FromAddress field accordingly. It will also create Tendermint verifier
// using the chain ID, home directory and RPC URI provided by the command line. If using
// a CLIContext in tests or any non CLI-based environment, the verifier will not be created
// a Context in tests or any non CLI-based environment, the verifier will not be created
// and will be set as nil because FlagTrustNode must be set.
func NewCLIContextWithInputAndFrom(input io.Reader, from string) CLIContext {
ctx := CLIContext{}
func NewContextWithInputAndFrom(input io.Reader, from string) Context {
ctx := Context{}
return ctx.InitWithInputAndFrom(input, from)
}

// NewCLIContextWithFrom returns a new initialized CLIContext with parameters from the
// NewContextWithFrom returns a new initialized Context with parameters from the
// command line using Viper. It takes a key name or address and populates the FromName and
// FromAddress field accordingly. It will also create Tendermint verifier using
// the chain ID, home directory and RPC URI provided by the command line. If using
// a CLIContext in tests or any non CLI-based environment, the verifier will not
// a Context in tests or any non CLI-based environment, the verifier will not
// be created and will be set as nil because FlagTrustNode must be set.
func NewCLIContextWithFrom(from string) CLIContext {
return NewCLIContextWithInputAndFrom(os.Stdin, from)
func NewContextWithFrom(from string) Context {
return NewContextWithInputAndFrom(os.Stdin, from)
}

// NewCLIContext returns a new initialized CLIContext with parameters from the
// NewContext returns a new initialized Context with parameters from the
// command line using Viper.
func NewCLIContext() CLIContext { return NewCLIContextWithFrom(viper.GetString(flags.FlagFrom)) }
func NewContext() Context { return NewContextWithFrom(viper.GetString(flags.FlagFrom)) }

// NewCLIContextWithInput returns a new initialized CLIContext with a io.Reader and parameters
// NewContextWithInput returns a new initialized Context with a io.Reader and parameters
// from the command line using Viper.
func NewCLIContextWithInput(input io.Reader) CLIContext {
return NewCLIContextWithInputAndFrom(input, viper.GetString(flags.FlagFrom))
func NewContextWithInput(input io.Reader) Context {
return NewContextWithInputAndFrom(input, viper.GetString(flags.FlagFrom))
}

// InitWithInputAndFrom returns a new CLIContext re-initialized from an existing
// CLIContext with a new io.Reader and from parameter
func (ctx CLIContext) InitWithInputAndFrom(input io.Reader, from string) CLIContext {
// InitWithInputAndFrom returns a new Context re-initialized from an existing
// Context with a new io.Reader and from parameter
func (ctx Context) InitWithInputAndFrom(input io.Reader, from string) Context {
input = bufio.NewReader(input)

var (
Expand Down Expand Up @@ -165,67 +165,67 @@ func (ctx CLIContext) InitWithInputAndFrom(input io.Reader, from string) CLICont
return ctx
}

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

// Init returns a new CLIContext re-initialized from an existing
// CLIContext with parameters from the command line using Viper.
func (ctx CLIContext) Init() CLIContext { return ctx.InitWithFrom(viper.GetString(flags.FlagFrom)) }
// Init returns a new Context re-initialized from an existing
// Context with parameters from the command line using Viper.
func (ctx Context) Init() Context { return ctx.InitWithFrom(viper.GetString(flags.FlagFrom)) }

// InitWithInput returns a new CLIContext re-initialized from an existing
// CLIContext with a new io.Reader and from parameter
func (ctx CLIContext) InitWithInput(input io.Reader) CLIContext {
// InitWithInput returns a new Context re-initialized from an existing
// Context with a new io.Reader and from parameter
func (ctx Context) InitWithInput(input io.Reader) Context {
return ctx.InitWithInputAndFrom(input, viper.GetString(flags.FlagFrom))
}

// WithKeyring returns a copy of the context with an updated keyring.
func (ctx CLIContext) WithKeyring(k keyring.Keyring) CLIContext {
func (ctx Context) WithKeyring(k keyring.Keyring) Context {
ctx.Keyring = k
return ctx
}

// WithInput returns a copy of the context with an updated input.
func (ctx CLIContext) WithInput(r io.Reader) CLIContext {
func (ctx Context) WithInput(r io.Reader) Context {
ctx.Input = r
return ctx
}

// WithJSONMarshaler returns a copy of the CLIContext with an updated JSONMarshaler.
func (ctx CLIContext) WithJSONMarshaler(m codec.JSONMarshaler) CLIContext {
// WithJSONMarshaler returns a copy of the Context with an updated JSONMarshaler.
func (ctx Context) WithJSONMarshaler(m codec.JSONMarshaler) Context {
ctx.JSONMarshaler = 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 {
func (ctx Context) WithCodec(cdc *codec.Codec) Context {
ctx.Codec = cdc
return ctx
}

// WithOutput returns a copy of the context with an updated output writer (e.g. stdout).
func (ctx CLIContext) WithOutput(w io.Writer) CLIContext {
func (ctx Context) WithOutput(w io.Writer) Context {
ctx.Output = w
return ctx
}

// WithFrom returns a copy of the context with an updated from address or name.
func (ctx CLIContext) WithFrom(from string) CLIContext {
func (ctx Context) WithFrom(from string) Context {
ctx.From = from
return ctx
}

// WithTrustNode returns a copy of the context with an updated TrustNode flag.
func (ctx CLIContext) WithTrustNode(trustNode bool) CLIContext {
func (ctx Context) WithTrustNode(trustNode bool) Context {
ctx.TrustNode = trustNode
return ctx
}

// WithNodeURI returns a copy of the context with an updated node URI.
func (ctx CLIContext) WithNodeURI(nodeURI string) CLIContext {
func (ctx Context) WithNodeURI(nodeURI string) Context {
ctx.NodeURI = nodeURI
client, err := rpchttp.New(nodeURI, "/websocket")
if err != nil {
Expand All @@ -237,84 +237,84 @@ func (ctx CLIContext) WithNodeURI(nodeURI string) CLIContext {
}

// WithHeight returns a copy of the context with an updated height.
func (ctx CLIContext) WithHeight(height int64) CLIContext {
func (ctx Context) WithHeight(height int64) Context {
ctx.Height = height
return ctx
}

// WithClient returns a copy of the context with an updated RPC client
// instance.
func (ctx CLIContext) WithClient(client rpcclient.Client) CLIContext {
func (ctx Context) WithClient(client rpcclient.Client) Context {
ctx.Client = client
return ctx
}

// WithUseLedger returns a copy of the context with an updated UseLedger flag.
func (ctx CLIContext) WithUseLedger(useLedger bool) CLIContext {
func (ctx Context) WithUseLedger(useLedger bool) Context {
ctx.UseLedger = useLedger
return ctx
}

// WithVerifier returns a copy of the context with an updated Verifier.
func (ctx CLIContext) WithVerifier(verifier tmlite.Verifier) CLIContext {
func (ctx Context) WithVerifier(verifier tmlite.Verifier) Context {
ctx.Verifier = verifier
return ctx
}

// WithChainID returns a copy of the context with an updated chain ID.
func (ctx CLIContext) WithChainID(chainID string) CLIContext {
func (ctx Context) WithChainID(chainID string) Context {
ctx.ChainID = chainID
return ctx
}

// WithGenerateOnly returns a copy of the context with updated GenerateOnly value
func (ctx CLIContext) WithGenerateOnly(generateOnly bool) CLIContext {
func (ctx Context) WithGenerateOnly(generateOnly bool) Context {
ctx.GenerateOnly = generateOnly
return ctx
}

// WithSimulation returns a copy of the context with updated Simulate value
func (ctx CLIContext) WithSimulation(simulate bool) CLIContext {
func (ctx Context) WithSimulation(simulate bool) Context {
ctx.Simulate = simulate
return ctx
}

// WithFromName returns a copy of the context with an updated from account name.
func (ctx CLIContext) WithFromName(name string) CLIContext {
func (ctx Context) WithFromName(name string) Context {
ctx.FromName = name
return ctx
}

// WithFromAddress returns a copy of the context with an updated from account
// address.
func (ctx CLIContext) WithFromAddress(addr sdk.AccAddress) CLIContext {
func (ctx Context) WithFromAddress(addr sdk.AccAddress) Context {
ctx.FromAddress = addr
return ctx
}

// WithBroadcastMode returns a copy of the context with an updated broadcast
// mode.
func (ctx CLIContext) WithBroadcastMode(mode string) CLIContext {
func (ctx Context) WithBroadcastMode(mode string) Context {
ctx.BroadcastMode = mode
return ctx
}

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

// WithAccountRetriever returns the context with an updated AccountRetriever
func (ctx CLIContext) WithAccountRetriever(retriever AccountRetriever) CLIContext {
func (ctx Context) WithAccountRetriever(retriever AccountRetriever) Context {
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.JSONMarshaler. An error is returned upon failure.
func (ctx CLIContext) Println(toPrint interface{}) error {
func (ctx Context) Println(toPrint interface{}) error {
var (
out []byte
err error
Expand Down Expand Up @@ -349,7 +349,7 @@ func (ctx CLIContext) Println(toPrint interface{}) error {
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
func (ctx CLIContext) PrintOutput(toPrint interface{}) error {
func (ctx Context) PrintOutput(toPrint interface{}) error {
var (
out []byte
err error
Expand Down
Loading