Change CoreContext parameters instead of using viper.Set

This commit is contained in:
Christopher Goes 2018-03-30 15:12:50 +02:00
parent 579e5d4cdc
commit 7a8e00dbb9
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
4 changed files with 37 additions and 14 deletions

View File

@ -28,3 +28,33 @@ func NewCoreContextFromViper() CoreContext {
Sequence: viper.GetInt64(client.FlagSequence),
}
}
func (c CoreContext) WithChainID(chainID string) CoreContext {
c.ChainID = chainID
return c
}
func (c CoreContext) WithHeight(height int64) CoreContext {
c.Height = height
return c
}
func (c CoreContext) WithTrustNode(trustNode bool) CoreContext {
c.TrustNode = trustNode
return c
}
func (c CoreContext) WithNodeURI(nodeURI string) CoreContext {
c.NodeURI = nodeURI
return c
}
func (c CoreContext) WithFromAddressName(fromAddressName string) CoreContext {
c.FromAddressName = fromAddressName
return c
}
func (c CoreContext) WithSequence(sequence int64) CoreContext {
c.Sequence = sequence
return c
}

View File

@ -73,9 +73,8 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
}
// sign
// XXX: OMG
viper.Set(client.FlagSequence, m.Sequence)
ctx := core.NewCoreContextFromViper()
ctx.Sequence = m.Sequence
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)

View File

@ -86,8 +86,7 @@ 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 := ctx.GetPassphraseFromStdin(name)
passphrase, err := ctx.GetPassphraseFromStdin(ctx.FromAddressName)
if err != nil {
panic(err)
}
@ -149,15 +148,11 @@ 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.NewCoreContextFromViper().Query(key, storeName)
viper.Set(client.FlagNode, orig)
return res, err
return core.NewCoreContextFromViper().WithNodeURI(node).Query(key, storeName)
}
func (c relayCommander) broadcastTx(node string, tx []byte) error {
_, err := core.NewCoreContextFromViper().WithSequence(c.getSequence(node) + 1).WithNodeURI(node).BroadcastTx(tx)
_, err := core.NewCoreContextFromViper().WithNodeURI(node).WithSequence(c.getSequence(node) + 1).BroadcastTx(tx)
return err
}
@ -191,8 +186,8 @@ func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []b
Sequence: sequence,
}
name := viper.GetString(client.FlagName)
res, err := core.NewCoreContextFromViper().SignAndBuild(name, passphrase, msg, c.cdc)
ctx := core.NewCoreContextFromViper()
res, err := ctx.SignAndBuild(ctx.FromAddressName, passphrase, msg, c.cdc)
if err != nil {
panic(err)
}

View File

@ -7,7 +7,6 @@ import (
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/viper"
"github.com/tendermint/go-crypto/keys"
"github.com/cosmos/cosmos-sdk/client"
@ -72,8 +71,8 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
// sign
// XXX: OMG
viper.Set(client.FlagSequence, m.Sequence)
ctx := core.NewCoreContextFromViper()
ctx.Sequence = m.Sequence
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)