From e871a418d509682376963c95d84139776d4b489d Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Tue, 16 Apr 2019 20:54:19 +0800 Subject: [PATCH 1/3] Fix merkle proof verification --- client/context/context.go | 7 +++++-- client/context/query.go | 10 +++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index e8cfd4264..6b74730cb 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -26,7 +26,8 @@ import ( const ctxAccStoreName = "acc" var ( - verifier tmlite.Verifier + verifier tmlite.Verifier + verifierHome string ) // CLIContext implements a typical CLI context created in SDK modules for @@ -46,6 +47,7 @@ type CLIContext struct { JSON bool PrintResponse bool Verifier tmlite.Verifier + VerifierHome string DryRun bool GenerateOnly bool fromAddress types.AccAddress @@ -67,8 +69,9 @@ func NewCLIContext() CLIContext { fromAddress, fromName := fromFields(from) // We need to use a single verifier for all contexts - if verifier == nil { + if verifier == nil || verifierHome != viper.GetString(cli.HomeFlag) { verifier = createVerifier() + verifierHome = viper.GetString(cli.HomeFlag) } return CLIContext{ diff --git a/client/context/query.go b/client/context/query.go index 7e4c237f8..b89700d19 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -220,9 +220,17 @@ func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) err kp = kp.AppendKey([]byte(storeName), merkle.KeyEncodingURL) kp = kp.AppendKey(resp.Key, merkle.KeyEncodingURL) + if resp.Value == nil { + err = prt.VerifyAbsence(resp.Proof, commit.Header.AppHash, kp.String()) + if err != nil { + return errors.Wrap(err, "failed to prove merkle absence proof") + } + return nil + } + err = prt.VerifyValue(resp.Proof, commit.Header.AppHash, kp.String(), resp.Value) if err != nil { - return errors.Wrap(err, "failed to prove merkle proof") + return errors.Wrap(err, "failed to prove merkle existence proof") } return nil From 2cc6b4116863eecefbfbe7e93a90e712815e0c06 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Wed, 17 Apr 2019 10:32:57 +0800 Subject: [PATCH 2/3] existence iavl proof and iavl absence proof --- store/iavlstore.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/store/iavlstore.go b/store/iavlstore.go index b636720a7..bfdf15fa7 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -204,8 +204,21 @@ func (st *IavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { res.Log = err.Error() break } - res.Value = value - res.Proof = &merkle.Proof{Ops: []merkle.ProofOp{iavl.NewIAVLValueOp(key, proof).ProofOp()}} + if proof == nil { + // Proof == nil implies that the store is empty. + if value != nil { + panic("unexpected value for an empty proof") + } + } + if value != nil { + // value was found + res.Value = value + res.Proof = &merkle.Proof{Ops: []merkle.ProofOp{iavl.NewIAVLValueOp(key, proof).ProofOp()}} + } else { + // value wasn't found + res.Value = nil + res.Proof = &merkle.Proof{Ops: []merkle.ProofOp{iavl.NewIAVLAbsenceOp(key, proof).ProofOp()}} + } } else { _, res.Value = tree.GetVersioned(key, res.Height) } From 4242ff8f4e246b9a83912c32ac5c5be7b6a6d43b Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Wed, 17 Apr 2019 10:37:04 +0800 Subject: [PATCH 3/3] make lcd test with proof verification --- client/lcd/test_helpers.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 77929dcc2..1f5b8147c 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -264,8 +264,7 @@ func InitializeTestLCD( // XXX: Need to set this so LCD knows the tendermint node address! viper.Set(client.FlagNode, config.RPC.ListenAddress) viper.Set(client.FlagChainID, genDoc.ChainID) - // TODO Set to false once the upstream Tendermint proof verification issue is fixed. - viper.Set(client.FlagTrustNode, true) + viper.Set(client.FlagTrustNode, false) dir, err := ioutil.TempDir("", "lcd_test") require.NoError(t, err) viper.Set(cli.HomeFlag, dir)