diff --git a/client/context/viper.go b/client/context/viper.go new file mode 100644 index 000000000..75441bc25 --- /dev/null +++ b/client/context/viper.go @@ -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), + } +} diff --git a/client/core/context.go b/client/core/context.go index b3ae1f4f0..7134a5f03 100644 --- a/client/core/context.go +++ b/client/core/context.go @@ -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 diff --git a/client/rpc/block.go b/client/rpc/block.go index cb39113d3..f42a15bc2 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -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 } diff --git a/client/rpc/status.go b/client/rpc/status.go index 0c79d0763..8ea8a5ad6 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -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 } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 5fbc044b2..9bf1505db 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -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 } diff --git a/client/tx/broadcast.go b/client/tx/broadcast.go index 44fb12544..998e2b0f1 100644 --- a/client/tx/broadcast.go +++ b/client/tx/broadcast.go @@ -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())) diff --git a/client/tx/query.go b/client/tx/query.go index f64fba801..63ab2ff04 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -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 } diff --git a/client/tx/search.go b/client/tx/search.go index d5cc642f1..df3be5077 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -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 } diff --git a/examples/democoin/x/cool/commands/tx.go b/examples/democoin/x/cool/commands/tx.go index 710a22558..8deaac405 100644 --- a/examples/democoin/x/cool/commands/tx.go +++ b/examples/democoin/x/cool/commands/tx.go @@ -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() diff --git a/x/auth/commands/account.go b/x/auth/commands/account.go index 470aa0eea..b87b95769 100644 --- a/x/auth/commands/account.go +++ b/x/auth/commands/account.go @@ -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 { diff --git a/x/auth/rest/query.go b/x/auth/rest/query.go index d30af4507..8fce027f2 100644 --- a/x/auth/rest/query.go +++ b/x/auth/rest/query.go @@ -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()))) diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index 67b600787..a543ce829 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -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() diff --git a/x/bank/rest/sendtx.go b/x/bank/rest/sendtx.go index a545661ca..9b12fdd8f 100644 --- a/x/bank/rest/sendtx.go +++ b/x/bank/rest/sendtx.go @@ -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 { diff --git a/x/ibc/commands/ibctx.go b/x/ibc/commands/ibctx.go index bff751545..c760292ad 100644 --- a/x/ibc/commands/ibctx.go +++ b/x/ibc/commands/ibctx.go @@ -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() diff --git a/x/ibc/commands/relay.go b/x/ibc/commands/relay.go index 3c70ac958..5817d628d 100644 --- a/x/ibc/commands/relay.go +++ b/x/ibc/commands/relay.go @@ -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) diff --git a/x/ibc/rest/transfer.go b/x/ibc/rest/transfer.go index 1c641124f..c26f9b53a 100644 --- a/x/ibc/rest/transfer.go +++ b/x/ibc/rest/transfer.go @@ -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 { diff --git a/x/simplestake/commands/commands.go b/x/simplestake/commands/commands.go index b1ef72aa8..f23da4bd8 100644 --- a/x/simplestake/commands/commands.go +++ b/x/simplestake/commands/commands.go @@ -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 } diff --git a/x/stake/commands/query.go b/x/stake/commands/query.go index 2fedcfc4a..7bc6a8aa9 100644 --- a/x/stake/commands/query.go +++ b/x/stake/commands/query.go @@ -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 { diff --git a/x/stake/commands/tx.go b/x/stake/commands/tx.go index f7df5ef7c..dbc4dfdf3 100644 --- a/x/stake/commands/tx.go +++ b/x/stake/commands/tx.go @@ -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