client: rename CliContext to Context (#6290)
* Refactor CliContext as Context * Fix lint issues * Fix goimports * Fix gov tests * Resolved ci-lint issues * Add changelog * Rename cliCtx to clientCtx * Fix mocks and routes * Add changelog * Update changelog * Apply suggestions from code review Co-authored-by: Alessio Treglia <alessio@tendermint.com> * merge client/rpc/ro{ot,utes}.go * Update docs * client/rpc: remove redundant client/rpc.RegisterRPCRoutes * regenerate mocks * Update ADRs Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Alessio Treglia
Federico Kunze
mergify[bot]
parent
654b2fdd10
commit
39f53ac22f
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
@@ -18,4 +18,4 @@ type NodeQuerier interface {
|
||||
QueryWithData(path string, data []byte) ([]byte, int64, error)
|
||||
}
|
||||
|
||||
var _ NodeQuerier = CLIContext{}
|
||||
var _ NodeQuerier = Context{}
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -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,
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -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
|
||||
@@ -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 (
|
||||
@@ -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 {
|
||||
@@ -237,76 +237,76 @@ 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
|
||||
}
|
||||
@@ -314,7 +314,7 @@ func (ctx CLIContext) WithAccountRetriever(retriever AccountRetriever) CLIContex
|
||||
// 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
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package context_test
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -10,20 +10,20 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
)
|
||||
|
||||
func TestCLIContext_WithOffline(t *testing.T) {
|
||||
func TestContext_WithOffline(t *testing.T) {
|
||||
viper.Set(flags.FlagOffline, true)
|
||||
viper.Set(flags.FlagNode, "tcp://localhost:26657")
|
||||
|
||||
ctx := context.NewCLIContext()
|
||||
ctx := client.NewContext()
|
||||
require.True(t, ctx.Offline)
|
||||
require.Nil(t, ctx.Client)
|
||||
}
|
||||
|
||||
func TestCLIContext_WithGenOnly(t *testing.T) {
|
||||
func TestContext_WithGenOnly(t *testing.T) {
|
||||
viper.Set(flags.FlagGenerateOnly, true)
|
||||
|
||||
validFromAddr := "cosmos1q7380u26f7ntke3facjmynajs4umlr329vr4ja"
|
||||
@@ -53,7 +53,7 @@ func TestCLIContext_WithGenOnly(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.NewCLIContextWithFrom(tt.from)
|
||||
ctx := client.NewContextWithFrom(tt.from)
|
||||
|
||||
require.Equal(t, tt.expectedFromAddr, ctx.FromAddress)
|
||||
require.Equal(t, tt.expectedFromName, ctx.FromName)
|
||||
@@ -61,9 +61,9 @@ func TestCLIContext_WithGenOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIContext_WithKeyring(t *testing.T) {
|
||||
func TestContext_WithKeyring(t *testing.T) {
|
||||
viper.Set(flags.FlagGenerateOnly, true)
|
||||
ctx := context.NewCLIContextWithFrom("cosmos1q7380u26f7ntke3facjmynajs4umlr329vr4ja")
|
||||
ctx := client.NewContextWithFrom("cosmos1q7380u26f7ntke3facjmynajs4umlr329vr4ja")
|
||||
require.NotNil(t, ctx.Keyring)
|
||||
kr := ctx.Keyring
|
||||
ctx = ctx.WithKeyring(nil)
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
+7
-7
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmrpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
@@ -26,8 +26,8 @@ import (
|
||||
|
||||
// RestServer represents the Light Client Rest server
|
||||
type RestServer struct {
|
||||
Mux *mux.Router
|
||||
CliCtx context.CLIContext
|
||||
Mux *mux.Router
|
||||
ClientCtx client.Context
|
||||
|
||||
log log.Logger
|
||||
listener net.Listener
|
||||
@@ -36,13 +36,13 @@ type RestServer struct {
|
||||
// NewRestServer creates a new rest server instance
|
||||
func NewRestServer(cdc *codec.Codec) *RestServer {
|
||||
r := mux.NewRouter()
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
clientCtx := client.NewContext().WithCodec(cdc)
|
||||
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "rest-server")
|
||||
|
||||
return &RestServer{
|
||||
Mux: r,
|
||||
CliCtx: cliCtx,
|
||||
log: logger,
|
||||
Mux: r,
|
||||
ClientCtx: clientCtx,
|
||||
log: logger,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
// GetNode returns an RPC client. If the context's client is not defined, an
|
||||
// error is returned.
|
||||
func (ctx CLIContext) GetNode() (rpcclient.Client, error) {
|
||||
func (ctx Context) GetNode() (rpcclient.Client, error) {
|
||||
if ctx.Client == nil {
|
||||
return nil, errors.New("no RPC client is defined in offline mode")
|
||||
}
|
||||
@@ -31,34 +31,34 @@ func (ctx CLIContext) GetNode() (rpcclient.Client, error) {
|
||||
// Query performs a query to a Tendermint node with the provided path.
|
||||
// It returns the result and height of the query upon success or an error if
|
||||
// the query fails.
|
||||
func (ctx CLIContext) Query(path string) ([]byte, int64, error) {
|
||||
func (ctx Context) Query(path string) ([]byte, int64, error) {
|
||||
return ctx.query(path, nil)
|
||||
}
|
||||
|
||||
// QueryWithData performs a query to a Tendermint node with the provided path
|
||||
// and a data payload. It returns the result and height of the query upon success
|
||||
// or an error if the query fails.
|
||||
func (ctx CLIContext) QueryWithData(path string, data []byte) ([]byte, int64, error) {
|
||||
func (ctx Context) QueryWithData(path string, data []byte) ([]byte, int64, error) {
|
||||
return ctx.query(path, data)
|
||||
}
|
||||
|
||||
// QueryStore performs a query to a Tendermint node with the provided key and
|
||||
// store name. It returns the result and height of the query upon success
|
||||
// or an error if the query fails.
|
||||
func (ctx CLIContext) QueryStore(key tmbytes.HexBytes, storeName string) ([]byte, int64, error) {
|
||||
func (ctx Context) QueryStore(key tmbytes.HexBytes, storeName string) ([]byte, int64, error) {
|
||||
return ctx.queryStore(key, storeName, "key")
|
||||
}
|
||||
|
||||
// QueryABCI performs a query to a Tendermint node with the provide RequestQuery.
|
||||
// It returns the ResultQuery obtained from the query.
|
||||
func (ctx CLIContext) QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
||||
func (ctx Context) QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
||||
return ctx.queryABCI(req)
|
||||
}
|
||||
|
||||
// QuerySubspace performs a query to a Tendermint node with the provided
|
||||
// store name and subspace. It returns key value pair and height of the query
|
||||
// upon success or an error if the query fails.
|
||||
func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, height int64, err error) {
|
||||
func (ctx Context) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, height int64, err error) {
|
||||
resRaw, height, err := ctx.queryStore(subspace, storeName, "subspace")
|
||||
if err != nil {
|
||||
return res, height, err
|
||||
@@ -69,16 +69,16 @@ func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sd
|
||||
}
|
||||
|
||||
// GetFromAddress returns the from address from the context's name.
|
||||
func (ctx CLIContext) GetFromAddress() sdk.AccAddress {
|
||||
func (ctx Context) GetFromAddress() sdk.AccAddress {
|
||||
return ctx.FromAddress
|
||||
}
|
||||
|
||||
// GetFromName returns the key name for the current context.
|
||||
func (ctx CLIContext) GetFromName() string {
|
||||
func (ctx Context) GetFromName() string {
|
||||
return ctx.FromName
|
||||
}
|
||||
|
||||
func (ctx CLIContext) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
||||
func (ctx Context) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
||||
node, err := ctx.GetNode()
|
||||
if err != nil {
|
||||
return abci.ResponseQuery{}, err
|
||||
@@ -115,7 +115,7 @@ func (ctx CLIContext) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, erro
|
||||
// or an error if the query fails. In addition, it will verify the returned
|
||||
// proof if TrustNode is disabled. If proof verification fails or the query
|
||||
// height is invalid, an error will be returned.
|
||||
func (ctx CLIContext) query(path string, key tmbytes.HexBytes) ([]byte, int64, error) {
|
||||
func (ctx Context) query(path string, key tmbytes.HexBytes) ([]byte, int64, error) {
|
||||
resp, err := ctx.queryABCI(abci.RequestQuery{
|
||||
Path: path,
|
||||
Data: key,
|
||||
@@ -128,7 +128,7 @@ func (ctx CLIContext) query(path string, key tmbytes.HexBytes) ([]byte, int64, e
|
||||
}
|
||||
|
||||
// Verify verifies the consensus proof at given height.
|
||||
func (ctx CLIContext) Verify(height int64) (tmtypes.SignedHeader, error) {
|
||||
func (ctx Context) Verify(height int64) (tmtypes.SignedHeader, error) {
|
||||
if ctx.Verifier == nil {
|
||||
return tmtypes.SignedHeader{}, fmt.Errorf("missing valid certifier to verify data from distrusted node")
|
||||
}
|
||||
@@ -146,7 +146,7 @@ func (ctx CLIContext) Verify(height int64) (tmtypes.SignedHeader, error) {
|
||||
}
|
||||
|
||||
// verifyProof perform response proof verification.
|
||||
func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) error {
|
||||
func (ctx Context) verifyProof(queryPath string, resp abci.ResponseQuery) error {
|
||||
if ctx.Verifier == nil {
|
||||
return fmt.Errorf("missing valid certifier to verify data from distrusted node")
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) err
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Instead of reconstructing, stash on CLIContext field?
|
||||
// TODO: Instead of reconstructing, stash on Context field?
|
||||
prt := rootmulti.DefaultProofRuntime()
|
||||
|
||||
// TODO: Better convention for path?
|
||||
@@ -188,7 +188,7 @@ func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) err
|
||||
// queryStore performs a query to a Tendermint node with the provided a store
|
||||
// name and path. It returns the result and height of the query upon success
|
||||
// or an error if the query fails.
|
||||
func (ctx CLIContext) queryStore(key tmbytes.HexBytes, storeName, endPath string) ([]byte, int64, error) {
|
||||
func (ctx Context) queryStore(key tmbytes.HexBytes, storeName, endPath string) ([]byte, int64, error) {
|
||||
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
|
||||
return ctx.query(path, key)
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client/rpc"
|
||||
)
|
||||
|
||||
// Register routes
|
||||
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||
rpc.RegisterRPCRoutes(cliCtx, r)
|
||||
}
|
||||
+16
-16
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||
@@ -34,9 +34,9 @@ func BlockCommand() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
||||
func getBlock(clientCtx client.Context, height *int64) ([]byte, error) {
|
||||
// get the node
|
||||
node, err := cliCtx.GetNode()
|
||||
node, err := clientCtx.GetNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -49,8 +49,8 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !cliCtx.TrustNode {
|
||||
check, err := cliCtx.Verify(res.Block.Height)
|
||||
if !clientCtx.TrustNode {
|
||||
check, err := clientCtx.Verify(res.Block.Height)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if cliCtx.Indent {
|
||||
if clientCtx.Indent {
|
||||
return codec.Cdc.MarshalJSONIndent(res, "", " ")
|
||||
}
|
||||
|
||||
@@ -72,8 +72,8 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
||||
}
|
||||
|
||||
// get the current blockchain height
|
||||
func GetChainHeight(cliCtx context.CLIContext) (int64, error) {
|
||||
node, err := cliCtx.GetNode()
|
||||
func GetChainHeight(clientCtx client.Context) (int64, error) {
|
||||
node, err := clientCtx.GetNode()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
output, err := getBlock(context.NewCLIContext(), height)
|
||||
output, err := getBlock(client.NewContext(), height)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
|
||||
// REST
|
||||
|
||||
// REST handler to get a block
|
||||
func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func BlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
|
||||
@@ -126,7 +126,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
chainHeight, err := GetChainHeight(cliCtx)
|
||||
chainHeight, err := GetChainHeight(clientCtx)
|
||||
if err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
|
||||
return
|
||||
@@ -137,23 +137,23 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
output, err := getBlock(cliCtx, &height)
|
||||
output, err := getBlock(clientCtx, &height)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
|
||||
rest.PostProcessResponseBare(w, cliCtx, output)
|
||||
rest.PostProcessResponseBare(w, clientCtx, output)
|
||||
}
|
||||
}
|
||||
|
||||
// REST handler to get the latest block
|
||||
func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func LatestBlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
output, err := getBlock(cliCtx, nil)
|
||||
output, err := getBlock(clientCtx, nil)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
|
||||
rest.PostProcessResponseBare(w, cliCtx, output)
|
||||
rest.PostProcessResponseBare(w, clientCtx, output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
)
|
||||
|
||||
// Register REST endpoints
|
||||
func RegisterRPCRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||
r.HandleFunc("/node_info", NodeInfoRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/syncing", NodeSyncingRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/latest", LatestBlockRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/{height}", BlockRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
)
|
||||
|
||||
// Register REST endpoints.
|
||||
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
|
||||
r.HandleFunc("/node_info", NodeInfoRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/syncing", NodeSyncingRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/latest", LatestBlockRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/{height}", BlockRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
}
|
||||
+12
-12
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||
@@ -32,8 +32,8 @@ func StatusCommand() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
|
||||
node, err := cliCtx.GetNode()
|
||||
func getNodeStatus(clientCtx client.Context) (*ctypes.ResultStatus, error) {
|
||||
node, err := clientCtx.GetNode()
|
||||
if err != nil {
|
||||
return &ctypes.ResultStatus{}, err
|
||||
}
|
||||
@@ -47,14 +47,14 @@ func printNodeStatus(_ *cobra.Command, _ []string) error {
|
||||
// No need to verify proof in getting node status
|
||||
viper.Set(flags.FlagKeyringBackend, flags.DefaultKeyringBackend)
|
||||
|
||||
cliCtx := context.NewCLIContext()
|
||||
status, err := getNodeStatus(cliCtx)
|
||||
clientCtx := client.NewContext()
|
||||
status, err := getNodeStatus(clientCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var output []byte
|
||||
if cliCtx.Indent {
|
||||
if clientCtx.Indent {
|
||||
output, err = codec.Cdc.MarshalJSONIndent(status, "", " ")
|
||||
} else {
|
||||
output, err = codec.Cdc.MarshalJSON(status)
|
||||
@@ -76,9 +76,9 @@ type NodeInfoResponse struct {
|
||||
}
|
||||
|
||||
// REST handler for node info
|
||||
func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func NodeInfoRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := getNodeStatus(cliCtx)
|
||||
status, err := getNodeStatus(clientCtx)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
DefaultNodeInfo: status.NodeInfo,
|
||||
ApplicationVersion: version.NewInfo(),
|
||||
}
|
||||
rest.PostProcessResponseBare(w, cliCtx, resp)
|
||||
rest.PostProcessResponseBare(w, clientCtx, resp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,13 +97,13 @@ type SyncingResponse struct {
|
||||
}
|
||||
|
||||
// REST handler for node syncing
|
||||
func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func NodeSyncingRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := getNodeStatus(cliCtx)
|
||||
status, err := getNodeStatus(clientCtx)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
|
||||
rest.PostProcessResponseBare(w, cliCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
|
||||
rest.PostProcessResponseBare(w, clientCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
|
||||
}
|
||||
}
|
||||
|
||||
+15
-15
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -43,14 +43,14 @@ func ValidatorCommand(cdc *codec.Codec) *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
clientCtx := client.NewContext().WithCodec(cdc)
|
||||
|
||||
result, err := GetValidators(cliCtx, height, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))
|
||||
result, err := GetValidators(clientCtx, height, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cliCtx.PrintOutput(result)
|
||||
return clientCtx.PrintOutput(result)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -119,9 +119,9 @@ func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error
|
||||
}
|
||||
|
||||
// GetValidators from client
|
||||
func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (ResultValidatorsOutput, error) {
|
||||
func GetValidators(clientCtx client.Context, height *int64, page, limit int) (ResultValidatorsOutput, error) {
|
||||
// get the node
|
||||
node, err := cliCtx.GetNode()
|
||||
node, err := clientCtx.GetNode()
|
||||
if err != nil {
|
||||
return ResultValidatorsOutput{}, err
|
||||
}
|
||||
@@ -131,8 +131,8 @@ func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (R
|
||||
return ResultValidatorsOutput{}, err
|
||||
}
|
||||
|
||||
if !cliCtx.TrustNode {
|
||||
check, err := cliCtx.Verify(validatorsRes.BlockHeight)
|
||||
if !clientCtx.TrustNode {
|
||||
check, err := clientCtx.Verify(validatorsRes.BlockHeight)
|
||||
if err != nil {
|
||||
return ResultValidatorsOutput{}, err
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (R
|
||||
// REST
|
||||
|
||||
// Validator Set at a height REST handler
|
||||
func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func ValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
|
||||
if err != nil {
|
||||
@@ -175,7 +175,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
chainHeight, err := GetChainHeight(cliCtx)
|
||||
chainHeight, err := GetChainHeight(clientCtx)
|
||||
if err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
|
||||
return
|
||||
@@ -185,16 +185,16 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
output, err := GetValidators(cliCtx, &height, page, limit)
|
||||
output, err := GetValidators(clientCtx, &height, page, limit)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
rest.PostProcessResponse(w, cliCtx, output)
|
||||
rest.PostProcessResponse(w, clientCtx, output)
|
||||
}
|
||||
}
|
||||
|
||||
// Latest Validator Set REST handler
|
||||
func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func LatestValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
|
||||
if err != nil {
|
||||
@@ -202,11 +202,11 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF
|
||||
return
|
||||
}
|
||||
|
||||
output, err := GetValidators(cliCtx, nil, page, limit)
|
||||
output, err := GetValidators(clientCtx, nil, page, limit)
|
||||
if rest.CheckInternalServerError(w, err) {
|
||||
return
|
||||
}
|
||||
|
||||
rest.PostProcessResponse(w, cliCtx, output)
|
||||
rest.PostProcessResponse(w, clientCtx, output)
|
||||
}
|
||||
}
|
||||
|
||||
+15
-15
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
// signing an application-specific transaction.
|
||||
type Factory struct {
|
||||
keybase keyring.Keyring
|
||||
txGenerator context.TxGenerator
|
||||
accountRetriever context.AccountRetriever
|
||||
txGenerator client.TxGenerator
|
||||
accountRetriever client.AccountRetriever
|
||||
accountNumber uint64
|
||||
sequence uint64
|
||||
gas uint64
|
||||
@@ -56,29 +56,29 @@ func NewFactoryFromCLI(input io.Reader) Factory {
|
||||
return f
|
||||
}
|
||||
|
||||
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 }
|
||||
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() client.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 TxGenerator.
|
||||
func (f Factory) WithTxGenerator(g context.TxGenerator) Factory {
|
||||
func (f Factory) WithTxGenerator(g client.TxGenerator) Factory {
|
||||
f.txGenerator = g
|
||||
return f
|
||||
}
|
||||
|
||||
// WithAccountRetriever returns a copy of the Factory with an updated AccountRetriever.
|
||||
func (f Factory) WithAccountRetriever(ar context.AccountRetriever) Factory {
|
||||
func (f Factory) WithAccountRetriever(ar client.AccountRetriever) Factory {
|
||||
f.accountRetriever = ar
|
||||
return f
|
||||
}
|
||||
|
||||
+29
-29
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
"github.com/gogo/protobuf/jsonpb"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/client/input"
|
||||
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
|
||||
@@ -21,32 +21,32 @@ import (
|
||||
|
||||
// GenerateOrBroadcastTx will either generate and print and unsigned transaction
|
||||
// or sign it and broadcast it returning an error upon failure.
|
||||
func GenerateOrBroadcastTx(ctx context.CLIContext, msgs ...sdk.Msg) error {
|
||||
txf := NewFactoryFromCLI(ctx.Input).WithTxGenerator(ctx.TxGenerator).WithAccountRetriever(ctx.AccountRetriever)
|
||||
return GenerateOrBroadcastTxWithFactory(ctx, txf, msgs...)
|
||||
func GenerateOrBroadcastTx(clientCtx client.Context, msgs ...sdk.Msg) error {
|
||||
txf := NewFactoryFromCLI(clientCtx.Input).WithTxGenerator(clientCtx.TxGenerator).WithAccountRetriever(clientCtx.AccountRetriever)
|
||||
return GenerateOrBroadcastTxWithFactory(clientCtx, txf, msgs...)
|
||||
}
|
||||
|
||||
// GenerateOrBroadcastTxWithFactory will either generate and print and unsigned transaction
|
||||
// or sign it and broadcast it returning an error upon failure.
|
||||
func GenerateOrBroadcastTxWithFactory(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
if ctx.GenerateOnly {
|
||||
return GenerateTx(ctx, txf, msgs...)
|
||||
func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
||||
if clientCtx.GenerateOnly {
|
||||
return GenerateTx(clientCtx, txf, msgs...)
|
||||
}
|
||||
|
||||
return BroadcastTx(ctx, txf, msgs...)
|
||||
return BroadcastTx(clientCtx, txf, msgs...)
|
||||
}
|
||||
|
||||
// GenerateTx will generate an unsigned transaction and print it to the writer
|
||||
// specified by ctx.Output. If simulation was requested, the gas will be
|
||||
// simulated and also printed to the same writer before the transaction is
|
||||
// printed.
|
||||
func GenerateTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
func GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
||||
if txf.SimulateAndExecute() {
|
||||
if ctx.Offline {
|
||||
if clientCtx.Offline {
|
||||
return errors.New("cannot estimate gas in offline mode")
|
||||
}
|
||||
|
||||
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...)
|
||||
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -60,20 +60,20 @@ func GenerateTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.Println(tx.GetTx())
|
||||
return clientCtx.Println(tx.GetTx())
|
||||
}
|
||||
|
||||
// BroadcastTx attempts to generate, sign and broadcast a transaction with the
|
||||
// given set of messages. It will also simulate gas requirements if necessary.
|
||||
// It will return an error upon failure.
|
||||
func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
txf, err := PrepareFactory(ctx, txf)
|
||||
func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
||||
txf, err := PrepareFactory(clientCtx, txf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if txf.SimulateAndExecute() || ctx.Simulate {
|
||||
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...)
|
||||
if txf.SimulateAndExecute() || clientCtx.Simulate {
|
||||
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "%s\n", GasEstimateResponse{GasEstimate: txf.Gas()})
|
||||
}
|
||||
|
||||
if ctx.Simulate {
|
||||
if clientCtx.Simulate {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -91,8 +91,8 @@ func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !ctx.SkipConfirm {
|
||||
out, err := ctx.JSONMarshaler.MarshalJSON(tx)
|
||||
if !clientCtx.SkipConfirm {
|
||||
out, err := clientCtx.JSONMarshaler.MarshalJSON(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,25 +108,25 @@ func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
|
||||
}
|
||||
}
|
||||
|
||||
txBytes, err := Sign(txf, ctx.GetFromName(), clientkeys.DefaultKeyPass, tx)
|
||||
txBytes, err := Sign(txf, clientCtx.GetFromName(), clientkeys.DefaultKeyPass, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// broadcast to a Tendermint node
|
||||
res, err := ctx.BroadcastTx(txBytes)
|
||||
res, err := clientCtx.BroadcastTx(txBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.Println(res)
|
||||
return clientCtx.Println(res)
|
||||
}
|
||||
|
||||
// WriteGeneratedTxResponse writes a generated unsigned transaction to the
|
||||
// provided http.ResponseWriter. It will simulate gas costs if requested by the
|
||||
// BaseReq. Upon any error, the error will be written to the http.ResponseWriter.
|
||||
func WriteGeneratedTxResponse(
|
||||
ctx context.CLIContext, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg,
|
||||
ctx client.Context, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg,
|
||||
) {
|
||||
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
|
||||
if !ok {
|
||||
@@ -185,7 +185,7 @@ func WriteGeneratedTxResponse(
|
||||
// BuildUnsignedTx builds a transaction to be signed given a set of messages. The
|
||||
// transaction is initially created via the provided factory's generator. Once
|
||||
// created, the fee, memo, and messages are set.
|
||||
func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (context.TxBuilder, error) {
|
||||
func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
|
||||
if txf.chainID == "" {
|
||||
return nil, fmt.Errorf("chain ID required but not specified")
|
||||
}
|
||||
@@ -278,16 +278,16 @@ func CalculateGas(
|
||||
// if the account number and/or the account sequence number are zero (not set),
|
||||
// they will be queried for and set on the provided Factory. A new Factory with
|
||||
// the updated fields will be returned.
|
||||
func PrepareFactory(ctx context.CLIContext, txf Factory) (Factory, error) {
|
||||
from := ctx.GetFromAddress()
|
||||
func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
|
||||
from := clientCtx.GetFromAddress()
|
||||
|
||||
if err := txf.accountRetriever.EnsureExists(ctx, from); err != nil {
|
||||
if err := txf.accountRetriever.EnsureExists(clientCtx, from); err != nil {
|
||||
return txf, err
|
||||
}
|
||||
|
||||
initNum, initSeq := txf.accountNumber, txf.sequence
|
||||
if initNum == 0 || initSeq == 0 {
|
||||
num, seq, err := txf.accountRetriever.GetAccountNumberSequence(ctx, from)
|
||||
num, seq, err := txf.accountRetriever.GetAccountNumberSequence(clientCtx, from)
|
||||
if err != nil {
|
||||
return txf, err
|
||||
}
|
||||
@@ -312,7 +312,7 @@ func PrepareFactory(ctx context.CLIContext, txf Factory) (Factory, error) {
|
||||
//
|
||||
// Note, It is assumed the Factory has the necessary fields set that are required
|
||||
// by the CanonicalSignBytes call.
|
||||
func Sign(txf Factory, name, passphrase string, tx context.TxBuilder) ([]byte, error) {
|
||||
func Sign(txf Factory, name, passphrase string, tx client.TxBuilder) ([]byte, error) {
|
||||
if txf.keybase == nil {
|
||||
return nil, errors.New("keybase must be set prior to signing a transaction")
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/tx"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
)
|
||||
|
||||
func NewTestTxGenerator() context.TxGenerator {
|
||||
func NewTestTxGenerator() client.TxGenerator {
|
||||
_, cdc := simapp.MakeCodecs()
|
||||
return types.StdTxGenerator{Cdc: cdc}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
@@ -12,18 +12,18 @@ type (
|
||||
// implement TxBuilder.
|
||||
TxGenerator interface {
|
||||
NewTx() TxBuilder
|
||||
NewFee() ClientFee
|
||||
NewSignature() ClientSignature
|
||||
NewFee() Fee
|
||||
NewSignature() Signature
|
||||
MarshalTx(tx types.Tx) ([]byte, error)
|
||||
}
|
||||
|
||||
ClientFee interface {
|
||||
Fee interface {
|
||||
types.Fee
|
||||
SetGas(uint64)
|
||||
SetAmount(types.Coins)
|
||||
}
|
||||
|
||||
ClientSignature interface {
|
||||
Signature interface {
|
||||
types.Signature
|
||||
SetPubKey(crypto.PubKey) error
|
||||
SetSignature([]byte)
|
||||
@@ -38,9 +38,9 @@ type (
|
||||
|
||||
SetMsgs(...types.Msg) error
|
||||
GetSignatures() []types.Signature
|
||||
SetSignatures(...ClientSignature) error
|
||||
SetSignatures(...Signature) error
|
||||
GetFee() types.Fee
|
||||
SetFee(ClientFee) error
|
||||
SetFee(Fee) error
|
||||
GetMemo() string
|
||||
SetMemo(string)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package context
|
||||
package client
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
@@ -17,12 +17,12 @@ const (
|
||||
DefaultVerifierCacheSize = 10
|
||||
)
|
||||
|
||||
// CreateVerifier returns a Tendermint verifier from a CLIContext object and
|
||||
// cache size. An error is returned if the CLIContext is missing required values
|
||||
// or if the verifier could not be created. A CLIContext must at the very least
|
||||
// have the chain ID and home directory set. If the CLIContext has TrustNode
|
||||
// CreateVerifier returns a Tendermint verifier from a Context object and
|
||||
// cache size. An error is returned if the Context is missing required values
|
||||
// or if the verifier could not be created. A Context must at the very least
|
||||
// have the chain ID and home directory set. If the Context has TrustNode
|
||||
// enabled, no verifier will be created.
|
||||
func CreateVerifier(ctx CLIContext, cacheSize int) (tmlite.Verifier, error) {
|
||||
func CreateVerifier(ctx Context, cacheSize int) (tmlite.Verifier, error) {
|
||||
if ctx.TrustNode {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package context_test
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
)
|
||||
|
||||
func TestCreateVerifier(t *testing.T) {
|
||||
@@ -15,18 +15,18 @@ func TestCreateVerifier(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
ctx context.CLIContext
|
||||
ctx client.Context
|
||||
expectErr bool
|
||||
}{
|
||||
{"no chain ID", context.CLIContext{}, true},
|
||||
{"no home directory", context.CLIContext{}.WithChainID("test"), true},
|
||||
{"no client or RPC URI", context.CLIContext{HomeDir: tmpDir}.WithChainID("test"), true},
|
||||
{"no chain ID", client.Context{}, true},
|
||||
{"no home directory", client.Context{}.WithChainID("test"), true},
|
||||
{"no client or RPC URI", client.Context{HomeDir: tmpDir}.WithChainID("test"), true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
verifier, err := context.CreateVerifier(tc.ctx, context.DefaultVerifierCacheSize)
|
||||
verifier, err := client.CreateVerifier(tc.ctx, client.DefaultVerifierCacheSize)
|
||||
require.Equal(t, tc.expectErr, err != nil, err)
|
||||
|
||||
if !tc.expectErr {
|
||||
Reference in New Issue
Block a user