From b98bfc01ae586b0f72f1c30a9a56b62dd4b660b3 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 28 Jul 2017 14:49:28 -0400 Subject: [PATCH 1/4] Add --trust-node flag to cli to skip proofs on queries --- client/commands/proofs/get.go | 11 +++++++++-- client/commands/proofs/root.go | 7 +++++-- tests/cli/basictx.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/client/commands/proofs/get.go b/client/commands/proofs/get.go index 34638698c..e392e96db 100644 --- a/client/commands/proofs/get.go +++ b/client/commands/proofs/get.go @@ -26,6 +26,7 @@ import ( func GetAndParseAppProof(key []byte, data interface{}) (lc.Proof, error) { height := GetHeight() node := commands.GetNode() + prover := proofs.NewAppProver(node) proof, err := GetProof(node, prover, key, height) @@ -43,7 +44,12 @@ func GetProof(node client.Client, prover lc.Prover, key []byte, height int) (pro if err != nil { return } - ph := int(proof.BlockHeight()) + + // short-circuit with no proofs + if viper.GetBool(FlagTrustNode) { + return proof, err + } + // here is the certifier, root of all knowledge cert, err := commands.GetCertifier() if err != nil { @@ -55,6 +61,7 @@ func GetProof(node client.Client, prover lc.Prover, key []byte, height int) (pro // FIXME: cannot use cert.GetByHeight for now, as it also requires // Validators and will fail on querying tendermint for non-current height. // When this is supported, we should use it instead... + ph := int(proof.BlockHeight()) client.WaitForHeight(node, ph, nil) commit, err := node.Commit(ph) if err != nil { @@ -96,7 +103,7 @@ func ParseHexKey(args []string, argname string) ([]byte, error) { } func GetHeight() int { - return viper.GetInt(heightFlag) + return viper.GetInt(FlagHeight) } type proof struct { diff --git a/client/commands/proofs/root.go b/client/commands/proofs/root.go index 15b49b8c9..65691980b 100644 --- a/client/commands/proofs/root.go +++ b/client/commands/proofs/root.go @@ -2,8 +2,10 @@ package proofs import "github.com/spf13/cobra" +// nolint const ( - heightFlag = "height" + FlagHeight = "height" + FlagTrustNode = "trust-node" ) // RootCmd represents the base command when called without any subcommands @@ -19,5 +21,6 @@ data to other peers as needed. } func init() { - RootCmd.Flags().Int(heightFlag, 0, "Height to query (skip to use latest block)") + RootCmd.Flags().Int(FlagHeight, 0, "Height to query (skip to use latest block)") + RootCmd.Flags().Bool(FlagTrustNode, false, "DANGEROUS: blindly trust all results from the server") } diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index dadc833c7..767b097fe 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -66,9 +66,22 @@ test02SendTxWithFee() { # Make sure tx is indexed checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" + # make sure this works without trust also + export BCTRUST_NODE=1 + checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" + unset BCTRUST_NODE + # assert replay protection TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=90mycoin --fee=10mycoin --sequence=2 --to=$RECV --name=$RICH 2>/dev/null) assertFalse "line=${LINENO}, replay: $TX" $? + + # make sure this works without trust also + export BCTRUST_NODE=1 + checkAccount $SENDER "9007199254739900" + checkAccount $RECV "1082" + unset BCTRUST_NODE + + # checking normally checkAccount $SENDER "9007199254739900" checkAccount $RECV "1082" From 8dd2371cc50d70fc482a56c0d59c101aea25dbf4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 28 Jul 2017 15:03:07 -0400 Subject: [PATCH 2/4] Don't require init when --trust-node is given --- client/commands/init.go | 12 +++++++++--- client/commands/proofs/root.go | 8 ++++++-- tests/cli/basictx.sh | 23 ++++++++++++----------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/client/commands/init.go b/client/commands/init.go index fe0109372..a54dbb699 100644 --- a/client/commands/init.go +++ b/client/commands/init.go @@ -29,9 +29,10 @@ var ( //nolint const ( - SeedFlag = "seed" - HashFlag = "valhash" - GenesisFlag = "genesis" + SeedFlag = "seed" + HashFlag = "valhash" + GenesisFlag = "genesis" + FlagTrustNode = "trust-node" ConfigFile = "config.toml" ) @@ -125,6 +126,11 @@ type Runable func(cmd *cobra.Command, args []string) error // and the root command sets up viper, which is needed to find the home dir. func RequireInit(run Runable) Runable { return func(cmd *cobra.Command, args []string) error { + // otherwise, run the wrappped command + if viper.GetBool(FlagTrustNode) { + return run(cmd, args) + } + // first check if we were Init'ed and if not, return an error root := viper.GetString(cli.HomeFlag) init, err := WasInited(root) diff --git a/client/commands/proofs/root.go b/client/commands/proofs/root.go index 65691980b..7a136025a 100644 --- a/client/commands/proofs/root.go +++ b/client/commands/proofs/root.go @@ -1,6 +1,9 @@ package proofs -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" + "github.com/tendermint/basecoin/client/commands" +) // nolint const ( @@ -22,5 +25,6 @@ data to other peers as needed. func init() { RootCmd.Flags().Int(FlagHeight, 0, "Height to query (skip to use latest block)") - RootCmd.Flags().Bool(FlagTrustNode, false, "DANGEROUS: blindly trust all results from the server") + RootCmd.Flags().Bool(commands.FlagTrustNode, false, + "DANGEROUS: blindly trust all results from the server") } diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index 767b097fe..101bb04aa 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -66,21 +66,10 @@ test02SendTxWithFee() { # Make sure tx is indexed checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" - # make sure this works without trust also - export BCTRUST_NODE=1 - checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" - unset BCTRUST_NODE - # assert replay protection TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=90mycoin --fee=10mycoin --sequence=2 --to=$RECV --name=$RICH 2>/dev/null) assertFalse "line=${LINENO}, replay: $TX" $? - # make sure this works without trust also - export BCTRUST_NODE=1 - checkAccount $SENDER "9007199254739900" - checkAccount $RECV "1082" - unset BCTRUST_NODE - # checking normally checkAccount $SENDER "9007199254739900" checkAccount $RECV "1082" @@ -93,6 +82,18 @@ test02SendTxWithFee() { if assertTrue "line=${LINENO}, no nonce query" $?; then assertEquals "line=${LINENO}, proper nonce" "2" $(echo $NONCE | jq .data) fi + + # make sure this works without trust also + OLD_BC_HOME=$BC_HOME + export BC_HOME=/foo + export BCTRUST_NODE=1 + export BCNODE=localhost:46657 + checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" + checkAccount $SENDER "9007199254739900" + checkAccount $RECV "1082" + unset BCTRUST_NODE + unset BCNODE + export BC_HOME=$OLD_BC_HOME } From 4652779a3d1b35f09ac84bb20d624da3e56940c4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 28 Jul 2017 15:13:31 -0400 Subject: [PATCH 3/4] Fix up flag handling --- client/commands/proofs/get.go | 2 +- client/commands/proofs/root.go | 7 +++---- tests/cli/basictx.sh | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client/commands/proofs/get.go b/client/commands/proofs/get.go index e392e96db..c3e510b3b 100644 --- a/client/commands/proofs/get.go +++ b/client/commands/proofs/get.go @@ -46,7 +46,7 @@ func GetProof(node client.Client, prover lc.Prover, key []byte, height int) (pro } // short-circuit with no proofs - if viper.GetBool(FlagTrustNode) { + if viper.GetBool(commands.FlagTrustNode) { return proof, err } diff --git a/client/commands/proofs/root.go b/client/commands/proofs/root.go index 7a136025a..9a1f50d27 100644 --- a/client/commands/proofs/root.go +++ b/client/commands/proofs/root.go @@ -7,8 +7,7 @@ import ( // nolint const ( - FlagHeight = "height" - FlagTrustNode = "trust-node" + FlagHeight = "height" ) // RootCmd represents the base command when called without any subcommands @@ -24,7 +23,7 @@ data to other peers as needed. } func init() { - RootCmd.Flags().Int(FlagHeight, 0, "Height to query (skip to use latest block)") - RootCmd.Flags().Bool(commands.FlagTrustNode, false, + RootCmd.PersistentFlags().Int(FlagHeight, 0, "Height to query (skip to use latest block)") + RootCmd.PersistentFlags().Bool(commands.FlagTrustNode, false, "DANGEROUS: blindly trust all results from the server") } diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index 101bb04aa..1831f76f7 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -86,13 +86,13 @@ test02SendTxWithFee() { # make sure this works without trust also OLD_BC_HOME=$BC_HOME export BC_HOME=/foo - export BCTRUST_NODE=1 - export BCNODE=localhost:46657 + export BC_TRUST_NODE=1 + export BC_NODE=localhost:46657 checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" checkAccount $SENDER "9007199254739900" checkAccount $RECV "1082" - unset BCTRUST_NODE - unset BCNODE + unset BC_TRUST_NODE + unset BC_NODE export BC_HOME=$OLD_BC_HOME } From 75181a78c395d1d5856154c6b7feb43e2737302e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Sat, 29 Jul 2017 17:41:13 -0400 Subject: [PATCH 4/4] Hide trust flag --- client/commands/proofs/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/commands/proofs/root.go b/client/commands/proofs/root.go index 9a1f50d27..b833f3da4 100644 --- a/client/commands/proofs/root.go +++ b/client/commands/proofs/root.go @@ -26,4 +26,5 @@ func init() { RootCmd.PersistentFlags().Int(FlagHeight, 0, "Height to query (skip to use latest block)") RootCmd.PersistentFlags().Bool(commands.FlagTrustNode, false, "DANGEROUS: blindly trust all results from the server") + RootCmd.PersistentFlags().MarkHidden(commands.FlagTrustNode) }