IRISHUB-238: move certifier creation to a function

This commit is contained in:
HaoyangLiu 2018-08-31 13:31:24 +08:00
parent 6d2fb8edef
commit b977baec73
1 changed files with 35 additions and 22 deletions

View File

@ -2,7 +2,8 @@ package context
import ( import (
"io" "io"
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
@ -10,11 +11,9 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/cli"
tendermintLite "github.com/tendermint/tendermint/lite"
tmlite "github.com/tendermint/tendermint/lite" tmlite "github.com/tendermint/tendermint/lite"
tendermintLiteProxy "github.com/tendermint/tendermint/lite/proxy" tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client" rpcclient "github.com/tendermint/tendermint/rpc/client"
"fmt"
) )
const ctxAccStoreName = "acc" const ctxAccStoreName = "acc"
@ -50,22 +49,6 @@ func NewCLIContext() CLIContext {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket") rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
} }
trustNode := viper.GetBool(client.FlagTrustNode)
var certifier tendermintLite.Certifier
if !trustNode {
chainID := viper.GetString(client.FlagChainID)
home := viper.GetString(cli.HomeFlag)
if chainID != "" && home != "" && nodeURI != "" {
var err error
certifier, err = tendermintLiteProxy.GetCertifier(chainID, home, nodeURI)
if err != nil {
panic(err)
}
} else {
panic(fmt.Errorf("can't create certifier for distrust mode, values from these options may be empty: --chain-id, --home or --node"))
}
}
return CLIContext{ return CLIContext{
Client: rpc, Client: rpc,
NodeURI: nodeURI, NodeURI: nodeURI,
@ -74,15 +57,45 @@ func NewCLIContext() CLIContext {
Height: viper.GetInt64(client.FlagHeight), Height: viper.GetInt64(client.FlagHeight),
Gas: viper.GetInt64(client.FlagGas), Gas: viper.GetInt64(client.FlagGas),
GasAdjustment: viper.GetFloat64(client.FlagGasAdjustment), GasAdjustment: viper.GetFloat64(client.FlagGasAdjustment),
TrustNode: trustNode, TrustNode: viper.GetBool(client.FlagTrustNode),
UseLedger: viper.GetBool(client.FlagUseLedger), UseLedger: viper.GetBool(client.FlagUseLedger),
Async: viper.GetBool(client.FlagAsync), Async: viper.GetBool(client.FlagAsync),
JSON: viper.GetBool(client.FlagJson), JSON: viper.GetBool(client.FlagJson),
PrintResponse: viper.GetBool(client.FlagPrintResponse), PrintResponse: viper.GetBool(client.FlagPrintResponse),
Certifier: certifier, Certifier: createCertifier(),
} }
} }
func createCertifier() tmlite.Certifier {
trustNode := viper.GetBool(client.FlagTrustNode)
if !trustNode {
chainID := viper.GetString(client.FlagChainID)
home := viper.GetString(cli.HomeFlag)
nodeURI := viper.GetString(client.FlagNode)
var errMsg bytes.Buffer
if chainID == "" {
errMsg.WriteString("chain-id ")
}
if home == "" {
errMsg.WriteString("home ")
}
if nodeURI == "" {
errMsg.WriteString("node ")
}
// errMsg is not empty
if errMsg.Len() != 0 {
panic(fmt.Errorf("can't create certifier for distrust mode, empty values from these options: %s", errMsg.String()))
}
certifier, err := tmliteProxy.GetCertifier(chainID, home, nodeURI)
if err != nil {
panic(err)
}
return certifier
}
return nil
}
// WithCodec returns a copy of the context with an updated codec. // WithCodec returns a copy of the context with an updated codec.
func (ctx CLIContext) WithCodec(cdc *wire.Codec) CLIContext { func (ctx CLIContext) WithCodec(cdc *wire.Codec) CLIContext {
ctx.Codec = cdc ctx.Codec = cdc