Move client context initialization from viper to separate package

This commit is contained in:
Christopher Goes 2018-04-02 14:05:23 +02:00
parent 0888096677
commit 7214149f1d
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
19 changed files with 67 additions and 68 deletions

19
client/context/viper.go Normal file
View File

@ -0,0 +1,19 @@
package context
import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
)
func NewCoreContextFromViper() core.CoreContext {
return core.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

@ -1,14 +1,5 @@
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
@ -18,17 +9,6 @@ type CoreContext struct {
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),
}
}
func (c CoreContext) WithChainID(chainID string) CoreContext {
c.ChainID = chainID
return c

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
)
type BroadcastTxBody struct {
@ -22,7 +22,7 @@ func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}
res, err := core.NewCoreContextFromViper().BroadcastTx([]byte(m.TxBytes))
res, err := context.NewCoreContextFromViper().BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))

View File

@ -15,7 +15,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/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -40,7 +40,7 @@ func (c commander) queryTx(hashHexStr string, trustNode bool) ([]byte, error) {
}
// get the node
node, err := core.NewCoreContextFromViper().GetNode()
node, err := context.NewCoreContextFromViper().GetNode()
if err != nil {
return nil, err
}

View File

@ -12,7 +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/client/context"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -44,7 +44,7 @@ func (c commander) searchTx(tags []string) ([]byte, error) {
query := strings.Join(tags, " AND ")
// get the node
node, err := core.NewCoreContextFromViper().GetNode()
node, err := context.NewCoreContextFromViper().GetNode()
if err != nil {
return nil, err
}

View File

@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
@ -24,7 +24,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
// get the from address from the name flag
from, err := ctx.GetFromAddress()
@ -60,7 +60,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
// get the from address from the name flag
from, err := ctx.GetFromAddress()

View File

@ -8,7 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -64,7 +64,7 @@ func (c commander) getAccountCmd(cmd *cobra.Command, args []string) error {
}
key := sdk.Address(bz)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, c.storeName)
if err != nil {

View File

@ -8,7 +8,7 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -33,7 +33,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.A
}
key := sdk.Address(bz)
res, err := core.NewCoreContextFromViper().Query(key, c.storeName)
res, err := context.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

@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
@ -38,7 +38,7 @@ type Commander struct {
}
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
// get the from address
from, err := ctx.GetFromAddress()

View File

@ -9,7 +9,7 @@ import (
"github.com/gorilla/mux"
"github.com/tendermint/go-crypto/keys"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank/commands"
@ -71,7 +71,7 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
}
// sign
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
ctx.Sequence = m.Sequence
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {

View File

@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
@ -39,7 +39,7 @@ type sendCommander struct {
}
func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error {
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
// get the from address
from, err := ctx.GetFromAddress()

View File

@ -9,7 +9,7 @@ import (
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
@ -73,7 +73,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.NewCoreContextFromViper().GetFromAddress()
address, err := context.NewCoreContextFromViper().GetFromAddress()
if err != nil {
panic(err)
}
@ -83,7 +83,7 @@ func (c relayCommander) runIBCRelay(cmd *cobra.Command, args []string) {
}
func (c relayCommander) loop(fromChainID, fromChainNode, toChainID, toChainNode string) {
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
// get password
passphrase, err := ctx.GetPassphraseFromStdin(ctx.FromAddressName)
if err != nil {
@ -147,11 +147,11 @@ OUTER:
}
func query(node string, key []byte, storeName string) (res []byte, err error) {
return core.NewCoreContextFromViper().WithNodeURI(node).Query(key, storeName)
return context.NewCoreContextFromViper().WithNodeURI(node).Query(key, storeName)
}
func (c relayCommander) broadcastTx(node string, tx []byte) error {
_, err := core.NewCoreContextFromViper().WithNodeURI(node).WithSequence(c.getSequence(node) + 1).BroadcastTx(tx)
_, err := context.NewCoreContextFromViper().WithNodeURI(node).WithSequence(c.getSequence(node) + 1).BroadcastTx(tx)
return err
}
@ -185,7 +185,7 @@ func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []b
Sequence: sequence,
}
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.SignAndBuild(ctx.FromAddressName, passphrase, msg, c.cdc)
if err != nil {
panic(err)

View File

@ -9,7 +9,7 @@ import (
"github.com/gorilla/mux"
"github.com/tendermint/go-crypto/keys"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank/commands"
@ -70,7 +70,7 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
// sign
// XXX: OMG
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
ctx.Sequence = m.Sequence
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {

View File

@ -10,7 +10,7 @@ import (
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/simplestake"
@ -48,7 +48,7 @@ type commander struct {
}
func (co commander) bondTxCmd(cmd *cobra.Command, args []string) error {
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
from, err := ctx.GetFromAddress()
if err != nil {
@ -84,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.NewCoreContextFromViper().GetFromAddress()
from, err := context.NewCoreContextFromViper().GetFromAddress()
if err != nil {
return err
}
@ -96,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.NewCoreContextFromViper().SignBuildBroadcast(name, msg, co.cdc)
res, err := context.NewCoreContextFromViper().SignBuildBroadcast(name, msg, co.cdc)
if err != nil {
return err
}

View File

@ -11,7 +11,7 @@ import (
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
"github.com/cosmos/cosmos-sdk/x/stake"
@ -47,7 +47,7 @@ func GetCmdQueryCandidates(cdc *wire.Codec, storeName string) *cobra.Command {
key := PrefixedKey(stake.MsgType, stake.CandidatesKey)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
@ -88,7 +88,7 @@ func GetCmdQueryCandidate(cdc *wire.Codec, storeName string) *cobra.Command {
key := PrefixedKey(stake.MsgType, stake.GetCandidateKey(addr))
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
@ -136,7 +136,7 @@ func GetCmdQueryDelegatorBond(cdc *wire.Codec, storeName string) *cobra.Command
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondKey(delegator, addr, cdc))
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
@ -180,7 +180,7 @@ func GetCmdQueryDelegatorBonds(cdc *wire.Codec, storeName string) *cobra.Command
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondsKey(delegator, cdc))
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {

View File

@ -11,7 +11,7 @@ import (
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/core"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/stake"
@ -93,7 +93,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
@ -131,7 +131,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
@ -168,7 +168,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err
@ -216,7 +216,7 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
name := viper.GetString(client.FlagName)
ctx := core.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper()
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
return err