Add CoreContext for CLI/REST options

This commit is contained in:
Christopher Goes 2018-03-30 14:57:53 +02:00
parent 720b37c6f2
commit 579e5d4cdc
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
20 changed files with 125 additions and 76 deletions

30
client/core/context.go Normal file
View File

@ -0,0 +1,30 @@
package core
import (
// "fmt"
// "github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
)
type CoreContext struct {
ChainID string
Height int64
TrustNode bool
NodeURI string
FromAddressName string
Sequence int64
}
func NewCoreContextFromViper() CoreContext {
return CoreContext{
ChainID: viper.GetString(client.FlagChainID),
Height: viper.GetInt64(client.FlagHeight),
TrustNode: viper.GetBool(client.FlagTrustNode),
NodeURI: viper.GetString(client.FlagNode),
FromAddressName: viper.GetString(client.FlagName),
Sequence: viper.GetInt64(client.FlagSequence),
}
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/wire"
rpcclient "github.com/tendermint/tendermint/rpc/client"
@ -17,9 +16,9 @@ import (
)
// Broadcast the transaction bytes to Tendermint
func BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
func (ctx CoreContext) BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
node, err := client.GetNode()
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
@ -43,17 +42,17 @@ func BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
}
// Query from Tendermint with the provided key and storename
func Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
func (ctx CoreContext) Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
path := fmt.Sprintf("/%s/key", storeName)
node, err := client.GetNode()
node, err := ctx.GetNode()
if err != nil {
return res, err
}
opts := rpcclient.ABCIQueryOptions{
Height: viper.GetInt64(client.FlagHeight),
Trusted: viper.GetBool(client.FlagTrustNode),
Height: ctx.Height,
Trusted: ctx.TrustNode,
}
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
@ -67,16 +66,16 @@ func Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
}
// Get the from address from the name flag
func GetFromAddress() (from sdk.Address, err error) {
func (ctx CoreContext) GetFromAddress() (from sdk.Address, err error) {
keybase, err := keys.GetKeyBase()
if err != nil {
return nil, err
}
name := viper.GetString(client.FlagName)
name := ctx.FromAddressName
if name == "" {
return nil, errors.Errorf("must provide a name using --name")
return nil, errors.Errorf("must provide a from address name")
}
info, err := keybase.Get(name)
@ -88,11 +87,11 @@ func GetFromAddress() (from sdk.Address, err error) {
}
// sign and build the transaction from the msg
func SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte, error) {
func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte, error) {
// build the Sign Messsage from the Standard Message
chainID := viper.GetString(client.FlagChainID)
sequence := int64(viper.GetInt(client.FlagSequence))
chainID := ctx.ChainID
sequence := ctx.Sequence
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Sequences: []int64{sequence},
@ -114,7 +113,7 @@ func SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte
sigs := []sdk.StdSignature{{
PubKey: pubkey,
Signature: sig,
Sequence: viper.GetInt64(client.FlagSequence),
Sequence: sequence,
}}
// marshal bytes
@ -124,23 +123,31 @@ func SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte
}
// sign and build the transaction from the msg
func SignBuildBroadcast(name string, msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) {
passphrase, err := GetPassphraseFromStdin(name)
func (ctx CoreContext) SignBuildBroadcast(name string, msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) {
passphrase, err := ctx.GetPassphraseFromStdin(name)
if err != nil {
return nil, err
}
txBytes, err := SignAndBuild(name, passphrase, msg, cdc)
txBytes, err := ctx.SignAndBuild(name, passphrase, msg, cdc)
if err != nil {
return nil, err
}
return BroadcastTx(txBytes)
return ctx.BroadcastTx(txBytes)
}
// get passphrase from std input
func GetPassphraseFromStdin(name string) (pass string, err error) {
func (ctx CoreContext) GetPassphraseFromStdin(name string) (pass string, err error) {
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
return client.GetPassword(prompt, buf)
}
// GetNode prepares a simple rpc.Client
func (ctx CoreContext) GetNode() (rpcclient.Client, error) {
if ctx.NodeURI == "" {
return nil, errors.New("Must define node URI")
}
return rpcclient.NewHTTP(ctx.NodeURI, "/websocket"), nil
}

View File

@ -1,17 +0,0 @@
package client
import (
"github.com/pkg/errors"
"github.com/spf13/viper"
rpcclient "github.com/tendermint/tendermint/rpc/client"
)
// GetNode prepares a simple rpc.Client from the flags
func GetNode() (rpcclient.Client, error) {
uri := viper.GetString(FlagNode)
if uri == "" {
return nil, errors.New("Must define node using --node")
}
return rpcclient.NewHTTP(uri, "/websocket"), nil
}

View File

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
)
const (
@ -31,7 +32,8 @@ func blockCommand() *cobra.Command {
func getBlock(height *int64) ([]byte, error) {
// get the node
node, err := client.GetNode()
ctx := core.NewCoreContextFromViper()
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
@ -55,7 +57,7 @@ func getBlock(height *int64) ([]byte, error) {
}
func GetChainHeight() (int64, error) {
node, err := client.GetNode()
node, err := core.NewCoreContextFromViper().GetNode()
if err != nil {
return -1, err
}

View File

@ -10,6 +10,7 @@ import (
wire "github.com/tendermint/go-wire"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
@ -25,7 +26,7 @@ func statusCommand() *cobra.Command {
func getNodeStatus() (*ctypes.ResultStatus, error) {
// get the node
node, err := client.GetNode()
node, err := core.NewCoreContextFromViper().GetNode()
if err != nil {
return &ctypes.ResultStatus{}, err
}

View File

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
)
func validatorCommand() *cobra.Command {
@ -26,7 +27,7 @@ func validatorCommand() *cobra.Command {
func GetValidators(height *int64) ([]byte, error) {
// get the node
node, err := client.GetNode()
node, err := core.NewCoreContextFromViper().GetNode()
if err != nil {
return nil, err
}

View File

@ -22,7 +22,7 @@ func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}
res, err := core.BroadcastTx([]byte(m.TxBytes))
res, err := core.NewCoreContextFromViper().BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))

View File

@ -15,6 +15,7 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -39,7 +40,7 @@ func (c commander) queryTx(hashHexStr string, trustNode bool) ([]byte, error) {
}
// get the node
node, err := client.GetNode()
node, err := core.NewCoreContextFromViper().GetNode()
if err != nil {
return nil, err
}

View File

@ -12,6 +12,7 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -43,7 +44,7 @@ func (c commander) searchTx(tags []string) ([]byte, error) {
query := strings.Join(tags, " AND ")
// get the node
node, err := client.GetNode()
node, err := core.NewCoreContextFromViper().GetNode()
if err != nil {
return nil, err
}

View File

@ -24,8 +24,10 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := core.NewCoreContextFromViper()
// get the from address from the name flag
from, err := core.GetFromAddress()
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
@ -37,7 +39,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
name := viper.GetString(client.FlagName)
// build and sign the transaction, then broadcast to Tendermint
res, err := core.SignBuildBroadcast(name, msg, cdc)
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}
@ -58,8 +60,10 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := core.NewCoreContextFromViper()
// get the from address from the name flag
from, err := core.GetFromAddress()
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
@ -71,7 +75,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
msg := cool.NewSetTrendMsg(from, args[0])
// build and sign the transaction, then broadcast to Tendermint
res, err := core.SignBuildBroadcast(name, msg, cdc)
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}

View File

@ -64,7 +64,9 @@ func (c commander) getAccountCmd(cmd *cobra.Command, args []string) error {
}
key := sdk.Address(bz)
res, err := core.Query(key, c.storeName)
ctx := core.NewCoreContextFromViper()
res, err := ctx.Query(key, c.storeName)
if err != nil {
return err
}

View File

@ -33,7 +33,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.A
}
key := sdk.Address(bz)
res, err := core.Query(key, c.storeName)
res, err := core.NewCoreContextFromViper().Query(key, c.storeName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Could't query account. Error: %s", err.Error())))

View File

@ -38,8 +38,10 @@ type Commander struct {
}
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
ctx := core.NewCoreContextFromViper()
// get the from address
from, err := core.GetFromAddress()
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
@ -66,7 +68,7 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
msg := BuildMsg(from, to, coins)
// build and sign the transaction, then broadcast to Tendermint
res, err := core.SignBuildBroadcast(name, msg, c.Cdc)
res, err := ctx.SignBuildBroadcast(name, msg, c.Cdc)
if err != nil {
return err
}

View File

@ -75,7 +75,8 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
// sign
// XXX: OMG
viper.Set(client.FlagSequence, m.Sequence)
txBytes, err := core.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
ctx := core.NewCoreContextFromViper()
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(err.Error()))
@ -83,7 +84,7 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
}
// send
res, err := core.BroadcastTx(txBytes)
res, err := ctx.BroadcastTx(txBytes)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

View File

@ -39,8 +39,10 @@ type sendCommander struct {
}
func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error {
ctx := core.NewCoreContextFromViper()
// get the from address
from, err := core.GetFromAddress()
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
@ -54,7 +56,7 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error
// get password
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, c.cdc)
res, err := ctx.SignBuildBroadcast(name, msg, c.cdc)
if err != nil {
return err
}

View File

@ -74,7 +74,7 @@ func (c relayCommander) runIBCRelay(cmd *cobra.Command, args []string) {
fromChainNode := viper.GetString(FlagFromChainNode)
toChainID := viper.GetString(FlagToChainID)
toChainNode := viper.GetString(FlagToChainNode)
address, err := core.GetFromAddress()
address, err := core.NewCoreContextFromViper().GetFromAddress()
if err != nil {
panic(err)
}
@ -84,9 +84,10 @@ func (c relayCommander) runIBCRelay(cmd *cobra.Command, args []string) {
}
func (c relayCommander) loop(fromChainID, fromChainNode, toChainID, toChainNode string) {
ctx := core.NewCoreContextFromViper()
// get password
name := viper.GetString(client.FlagName)
passphrase, err := core.GetPassphraseFromStdin(name)
passphrase, err := ctx.GetPassphraseFromStdin(name)
if err != nil {
panic(err)
}
@ -150,16 +151,13 @@ OUTER:
func query(node string, key []byte, storeName string) (res []byte, err error) {
orig := viper.GetString(client.FlagNode)
viper.Set(client.FlagNode, node)
res, err = core.Query(key, storeName)
res, err = core.NewCoreContextFromViper().Query(key, storeName)
viper.Set(client.FlagNode, orig)
return res, err
}
func (c relayCommander) broadcastTx(node string, tx []byte) error {
viper.Set(client.FlagNode, node)
seq := c.getSequence(node) + 1
viper.Set(client.FlagSequence, seq)
_, err := core.BroadcastTx(tx)
_, err := core.NewCoreContextFromViper().WithSequence(c.getSequence(node) + 1).WithNodeURI(node).BroadcastTx(tx)
return err
}
@ -194,7 +192,7 @@ func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []b
}
name := viper.GetString(client.FlagName)
res, err := core.SignAndBuild(name, passphrase, msg, c.cdc)
res, err := core.NewCoreContextFromViper().SignAndBuild(name, passphrase, msg, c.cdc)
if err != nil {
panic(err)
}

View File

@ -73,7 +73,8 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
// sign
// XXX: OMG
viper.Set(client.FlagSequence, m.Sequence)
txBytes, err := core.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
ctx := core.NewCoreContextFromViper()
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(err.Error()))
@ -81,7 +82,7 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
}
// send
res, err := core.BroadcastTx(txBytes)
res, err := ctx.BroadcastTx(txBytes)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

View File

@ -48,7 +48,9 @@ type commander struct {
}
func (co commander) bondTxCmd(cmd *cobra.Command, args []string) error {
from, err := core.GetFromAddress()
ctx := core.NewCoreContextFromViper()
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
@ -82,7 +84,7 @@ func (co commander) bondTxCmd(cmd *cobra.Command, args []string) error {
}
func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error {
from, err := core.GetFromAddress()
from, err := core.NewCoreContextFromViper().GetFromAddress()
if err != nil {
return err
}
@ -94,7 +96,7 @@ func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error {
func (co commander) sendMsg(msg sdk.Msg) error {
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, co.cdc)
res, err := core.NewCoreContextFromViper().SignBuildBroadcast(name, msg, co.cdc)
if err != nil {
return err
}

View File

@ -47,7 +47,8 @@ func GetCmdQueryCandidates(cdc *wire.Codec, storeName string) *cobra.Command {
key := PrefixedKey(stake.MsgType, stake.CandidatesKey)
res, err := core.Query(key, storeName)
ctx := core.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
@ -87,7 +88,9 @@ func GetCmdQueryCandidate(cdc *wire.Codec, storeName string) *cobra.Command {
key := PrefixedKey(stake.MsgType, stake.GetCandidateKey(addr))
res, err := core.Query(key, storeName)
ctx := core.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
@ -133,7 +136,9 @@ func GetCmdQueryDelegatorBond(cdc *wire.Codec, storeName string) *cobra.Command
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondKey(delegator, addr, cdc))
res, err := core.Query(key, storeName)
ctx := core.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
@ -175,7 +180,9 @@ func GetCmdQueryDelegatorBonds(cdc *wire.Codec, storeName string) *cobra.Command
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondsKey(delegator, cdc))
res, err := core.Query(key, storeName)
ctx := core.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}

View File

@ -93,7 +93,8 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, cdc)
ctx := core.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}
@ -130,7 +131,8 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, cdc)
ctx := core.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}
@ -166,7 +168,8 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, cdc)
ctx := core.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}
@ -213,7 +216,8 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
res, err := core.SignBuildBroadcast(name, msg, cdc)
ctx := core.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
}