From f7036d641406242a5683ae769f478675c38959ad Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Mon, 24 Dec 2018 11:19:50 +0800 Subject: [PATCH 01/11] Add absence proof verification --- client/context/query.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/context/query.go b/client/context/query.go index aba8df19095e..77901460ce3f 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -223,6 +223,13 @@ 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 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") From 3c34019c8c6b66fb5da612d0b1b78c3ca023f561 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Wed, 26 Dec 2018 14:35:16 +0800 Subject: [PATCH 02/11] Enable distrust mode in lcd test --- 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 e6e2d2fa40a8..0e5e848448c9 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -304,8 +304,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) From 789a9c66d05fedc72e270818735874c7fee6550c Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 20 Jan 2019 15:57:57 +0100 Subject: [PATCH 03/11] separate chainids in lcd test, make clictx verifier parallel --- client/context/context.go | 22 ++++++++++++++-------- client/lcd/test_helpers.go | 11 +++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index 86404d45c729..fcd30fb642b4 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -25,7 +25,7 @@ import ( ) var ( - verifier tmlite.Verifier + verifiers map[string]tmlite.Verifier ) // CLIContext implements a typical CLI context created in SDK modules for @@ -65,11 +65,6 @@ func NewCLIContext() CLIContext { from := viper.GetString(client.FlagFrom) fromAddress, fromName := fromFields(from) - // We need to use a single verifier for all contexts - if verifier == nil { - verifier = createVerifier() - } - return CLIContext{ Client: rpc, Output: os.Stdout, @@ -82,7 +77,7 @@ func NewCLIContext() CLIContext { Async: viper.GetBool(client.FlagAsync), JSON: viper.GetBool(client.FlagJson), PrintResponse: viper.GetBool(client.FlagPrintResponse), - Verifier: verifier, + Verifier: getVerifier(), Simulate: viper.GetBool(client.FlagDryRun), GenerateOnly: viper.GetBool(client.FlagGenerateOnly), fromAddress: fromAddress, @@ -91,7 +86,11 @@ func NewCLIContext() CLIContext { } } -func createVerifier() tmlite.Verifier { +func getVerifier() tmlite.Verifier { + if verifiers == nil { + verifiers = make(map[string]tmlite.Verifier) + } + trustNodeDefined := viper.IsSet(client.FlagTrustNode) if !trustNodeDefined { return nil @@ -121,6 +120,11 @@ func createVerifier() tmlite.Verifier { os.Exit(1) } + // We need to use a single verifier for each chain + if verifier, ok := verifiers[chainID]; ok { + return verifier + } + node := rpcclient.NewHTTP(nodeURI, "/websocket") cacheSize := 10 // TODO: determine appropriate cache size verifier, err := tmliteProxy.NewVerifier( @@ -134,6 +138,8 @@ func createVerifier() tmlite.Verifier { os.Exit(1) } + verifiers[chainID] = verifier + return verifier } diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 0e5e848448c9..6d51b75f95bf 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -7,6 +7,7 @@ import ( "regexp" "io/ioutil" + "math/rand" "net" "net/http" "os" @@ -67,20 +68,22 @@ import ( // makePathname creates a unique pathname for each test. It will panic if it // cannot get the current working directory. -func makePathname() string { +func makePathname() (string, string) { p, err := os.Getwd() if err != nil { panic(err) } + uniq := fmt.Sprintf("%d", rand.Int()%1000000) + sep := string(filepath.Separator) - return strings.Replace(p, sep, "_", -1) + return strings.Replace(p, sep, "_", -1), uniq } // GetConfig returns a Tendermint config for the test cases. func GetConfig() *tmcfg.Config { - pathname := makePathname() - config := tmcfg.ResetTestRoot(pathname) + pathname, uniq := makePathname() + config := tmcfg.ResetTestRoot(pathname, uniq) tmAddr, _, err := server.FreeTCPAddr() if err != nil { From b5cb13b373da61dc6856b30469f7629aa560dfe9 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 20 Jan 2019 17:34:23 +0100 Subject: [PATCH 04/11] rename ResetTestRoot -> ResetTestRootWithChainID --- client/lcd/test_helpers.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 6d51b75f95bf..90e32366b55d 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -74,16 +74,16 @@ func makePathname() (string, string) { panic(err) } - uniq := fmt.Sprintf("%d", rand.Int()%1000000) + chainid := fmt.Sprintf("%d", rand.Int()%1000000) sep := string(filepath.Separator) - return strings.Replace(p, sep, "_", -1), uniq + return strings.Replace(p, sep, "_", -1), chainid } // GetConfig returns a Tendermint config for the test cases. func GetConfig() *tmcfg.Config { - pathname, uniq := makePathname() - config := tmcfg.ResetTestRoot(pathname, uniq) + pathname, chainid := makePathname() + config := tmcfg.ResetTestRootWithChainID(pathname, chainid) tmAddr, _, err := server.FreeTCPAddr() if err != nil { From 3307b670254b0660d96136218c5c531eee5be172 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Mon, 21 Jan 2019 14:42:04 +0800 Subject: [PATCH 05/11] Revert "Fix LCD tests dependency" --- client/context/context.go | 22 ++++++++-------------- client/lcd/test_helpers.go | 11 ++++------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index fcd30fb642b4..86404d45c729 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -25,7 +25,7 @@ import ( ) var ( - verifiers map[string]tmlite.Verifier + verifier tmlite.Verifier ) // CLIContext implements a typical CLI context created in SDK modules for @@ -65,6 +65,11 @@ func NewCLIContext() CLIContext { from := viper.GetString(client.FlagFrom) fromAddress, fromName := fromFields(from) + // We need to use a single verifier for all contexts + if verifier == nil { + verifier = createVerifier() + } + return CLIContext{ Client: rpc, Output: os.Stdout, @@ -77,7 +82,7 @@ func NewCLIContext() CLIContext { Async: viper.GetBool(client.FlagAsync), JSON: viper.GetBool(client.FlagJson), PrintResponse: viper.GetBool(client.FlagPrintResponse), - Verifier: getVerifier(), + Verifier: verifier, Simulate: viper.GetBool(client.FlagDryRun), GenerateOnly: viper.GetBool(client.FlagGenerateOnly), fromAddress: fromAddress, @@ -86,11 +91,7 @@ func NewCLIContext() CLIContext { } } -func getVerifier() tmlite.Verifier { - if verifiers == nil { - verifiers = make(map[string]tmlite.Verifier) - } - +func createVerifier() tmlite.Verifier { trustNodeDefined := viper.IsSet(client.FlagTrustNode) if !trustNodeDefined { return nil @@ -120,11 +121,6 @@ func getVerifier() tmlite.Verifier { os.Exit(1) } - // We need to use a single verifier for each chain - if verifier, ok := verifiers[chainID]; ok { - return verifier - } - node := rpcclient.NewHTTP(nodeURI, "/websocket") cacheSize := 10 // TODO: determine appropriate cache size verifier, err := tmliteProxy.NewVerifier( @@ -138,8 +134,6 @@ func getVerifier() tmlite.Verifier { os.Exit(1) } - verifiers[chainID] = verifier - return verifier } diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 4b2b0dab82c6..3f8030de9932 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -7,7 +7,6 @@ import ( "regexp" "io/ioutil" - "math/rand" "net" "net/http" "os" @@ -68,22 +67,20 @@ import ( // makePathname creates a unique pathname for each test. It will panic if it // cannot get the current working directory. -func makePathname() (string, string) { +func makePathname() string { p, err := os.Getwd() if err != nil { panic(err) } - chainid := fmt.Sprintf("%d", rand.Int()%1000000) - sep := string(filepath.Separator) - return strings.Replace(p, sep, "_", -1), chainid + return strings.Replace(p, sep, "_", -1) } // GetConfig returns a Tendermint config for the test cases. func GetConfig() *tmcfg.Config { - pathname, chainid := makePathname() - config := tmcfg.ResetTestRootWithChainID(pathname, chainid) + pathname := makePathname() + config := tmcfg.ResetTestRoot(pathname) tmAddr, _, err := server.FreeTCPAddr() if err != nil { From 00793770e0c6ba51a8d828108e493c001ec51151 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 19:56:26 +0100 Subject: [PATCH 06/11] Update PENDING.md --- PENDING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/PENDING.md b/PENDING.md index d84b7ddb9f4f..fe0d438fabbf 100644 --- a/PENDING.md +++ b/PENDING.md @@ -24,6 +24,7 @@ FEATURES * Gaia CLI (`gaiacli`) * [\#3429](https://github.com/cosmos/cosmos-sdk/issues/3429) Support querying for all delegator distribution rewards. + * \#3449 Proof verification now works with absence proofs * Gaia - [\#3397](https://github.com/cosmos/cosmos-sdk/pull/3397) Implement genesis file sanitization to avoid failures at chain init. From af9ba80286922292a267df465663b65b1844d075 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 20:09:04 +0100 Subject: [PATCH 07/11] Use isolated test directories --- client/lcd/test_helpers.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 27f9fdb442cb..272209e7b03a 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -10,7 +10,6 @@ import ( "net" "net/http" "os" - "path/filepath" "sort" "strings" "testing" @@ -72,9 +71,11 @@ func makePathname() string { if err != nil { panic(err) } - - sep := string(filepath.Separator) - return strings.Replace(p, sep, "_", -1) + name, err := ioutil.TempDir(p, "lcd_test") + if err != nil { + panic(err) + } + return name } // GetConfig returns a Tendermint config for the test cases. From e8ed057f624c15873828b2c1b7b7183c7b7df120 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 20:13:27 +0100 Subject: [PATCH 08/11] Set home dir --- client/lcd/test_helpers.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 272209e7b03a..3b1e329ed4da 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -67,15 +67,12 @@ import ( // makePathname creates a unique pathname for each test. It will panic if it // cannot get the current working directory. func makePathname() string { - p, err := os.Getwd() - if err != nil { - panic(err) - } - name, err := ioutil.TempDir(p, "lcd_test") + dir, err := ioutil.TempDir("", "lcd_test") if err != nil { panic(err) } - return name + viper.Set(cli.HomeFlag, dir) + return dir } // GetConfig returns a Tendermint config for the test cases. From 3da68ed45184ed31c279d48c75c0db96c8bdec29 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 20:19:51 +0100 Subject: [PATCH 09/11] Clear up directory creation --- client/lcd/test_helpers.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 3b1e329ed4da..8b5a6a9ae28d 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -71,13 +71,13 @@ func makePathname() string { if err != nil { panic(err) } - viper.Set(cli.HomeFlag, dir) return dir } // GetConfig returns a Tendermint config for the test cases. func GetConfig() *tmcfg.Config { pathname := makePathname() + viper.Set(cli.HomeFlag, pathname) config := tmcfg.ResetTestRoot(pathname) tmAddr, _, err := server.FreeTCPAddr() @@ -108,14 +108,8 @@ func GetConfig() *tmcfg.Config { // NOTE: memDB cannot be used because the request is expecting to interact with // the default location. func GetKeyBase(t *testing.T) crkeys.Keybase { - dir, err := ioutil.TempDir("", "lcd_test") - require.NoError(t, err) - - viper.Set(cli.HomeFlag, dir) - keybase, err := keys.GetKeyBaseWithWritePerm() require.NoError(t, err) - return keybase } @@ -303,9 +297,6 @@ func InitializeTestLCD( viper.Set(client.FlagNode, config.RPC.ListenAddress) viper.Set(client.FlagChainID, genDoc.ChainID) viper.Set(client.FlagTrustNode, false) - dir, err := ioutil.TempDir("", "lcd_test") - require.NoError(t, err) - viper.Set(cli.HomeFlag, dir) node, err := startTM(config, logger, genDoc, privVal, app) require.NoError(t, err) From d5ed0d9ce17a1b16b468b82547b94518f4c03b37 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 20:26:34 +0100 Subject: [PATCH 10/11] Recreate verifier on home dir change --- client/context/context.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index a253a88fae55..a9744408d7d4 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -24,7 +24,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -var verifier tmlite.Verifier +var ( + verifier tmlite.Verifier + verifierHome string +) // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. @@ -43,6 +46,7 @@ type CLIContext struct { Async bool PrintResponse bool Verifier tmlite.Verifier + VerifierHome string Simulate bool GenerateOnly bool FromAddress sdk.AccAddress @@ -68,8 +72,9 @@ func NewCLIContext() CLIContext { } // 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{ From 1e4b527db4d3466d34b567a5b6cd22fcc5f11cc3 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 30 Jan 2019 20:29:23 +0100 Subject: [PATCH 11/11] Revert dir changes --- client/lcd/test_helpers.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 8b5a6a9ae28d..71c2a38d6ac2 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -10,6 +10,7 @@ import ( "net" "net/http" "os" + "path/filepath" "sort" "strings" "testing" @@ -67,17 +68,18 @@ import ( // makePathname creates a unique pathname for each test. It will panic if it // cannot get the current working directory. func makePathname() string { - dir, err := ioutil.TempDir("", "lcd_test") + p, err := os.Getwd() if err != nil { panic(err) } - return dir + + sep := string(filepath.Separator) + return strings.Replace(p, sep, "_", -1) } // GetConfig returns a Tendermint config for the test cases. func GetConfig() *tmcfg.Config { pathname := makePathname() - viper.Set(cli.HomeFlag, pathname) config := tmcfg.ResetTestRoot(pathname) tmAddr, _, err := server.FreeTCPAddr() @@ -108,8 +110,14 @@ func GetConfig() *tmcfg.Config { // NOTE: memDB cannot be used because the request is expecting to interact with // the default location. func GetKeyBase(t *testing.T) crkeys.Keybase { + dir, err := ioutil.TempDir("", "lcd_test") + require.NoError(t, err) + + viper.Set(cli.HomeFlag, dir) + keybase, err := keys.GetKeyBaseWithWritePerm() require.NoError(t, err) + return keybase } @@ -296,7 +304,11 @@ 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) - viper.Set(client.FlagTrustNode, false) + // TODO Set to false once the upstream Tendermint proof verification issue is fixed. + viper.Set(client.FlagTrustNode, true) + dir, err := ioutil.TempDir("", "lcd_test") + require.NoError(t, err) + viper.Set(cli.HomeFlag, dir) node, err := startTM(config, logger, genDoc, privVal, app) require.NoError(t, err)