From 140b79e728e965cfc391a30a8cfadb59970112b5 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 17 Jan 2023 15:00:21 +0100 Subject: [PATCH 1/2] fix closing iterator in 07-tendermint --- modules/light-clients/07-tendermint/store.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/light-clients/07-tendermint/store.go b/modules/light-clients/07-tendermint/store.go index ce513c2cd68..d447832fb8b 100644 --- a/modules/light-clients/07-tendermint/store.go +++ b/modules/light-clients/07-tendermint/store.go @@ -80,7 +80,6 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool) iterator := sdk.KVStorePrefixIterator(store, []byte(host.KeyConsensusStatePrefix)) // iterate over processed time and processed height - defer iterator.Close() for ; iterator.Valid(); iterator.Next() { keySplit := strings.Split(string(iterator.Key()), "/") // processed time key in prefix store has format: "consensusState//processedTime" @@ -99,6 +98,8 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool) } } + iterator.Close() + // iterate over iteration keys iterator = sdk.KVStorePrefixIterator(store, []byte(KeyIterateConsensusStatePrefix)) From 7fd91fd2c440e014963970cd6a27f9bd33b7c97e Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Wed, 18 Jan 2023 10:34:55 +0100 Subject: [PATCH 2/2] maintain defer pattern but use new variable to avoiid reassignment issues --- modules/light-clients/07-tendermint/store.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/light-clients/07-tendermint/store.go b/modules/light-clients/07-tendermint/store.go index d447832fb8b..fd37787e40c 100644 --- a/modules/light-clients/07-tendermint/store.go +++ b/modules/light-clients/07-tendermint/store.go @@ -80,6 +80,7 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool) iterator := sdk.KVStorePrefixIterator(store, []byte(host.KeyConsensusStatePrefix)) // iterate over processed time and processed height + defer iterator.Close() for ; iterator.Valid(); iterator.Next() { keySplit := strings.Split(string(iterator.Key()), "/") // processed time key in prefix store has format: "consensusState//processedTime" @@ -98,14 +99,12 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool) } } - iterator.Close() - // iterate over iteration keys - iterator = sdk.KVStorePrefixIterator(store, []byte(KeyIterateConsensusStatePrefix)) + iter := sdk.KVStorePrefixIterator(store, []byte(KeyIterateConsensusStatePrefix)) - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - if cb(iterator.Key(), iterator.Value()) { + defer iter.Close() + for ; iter.Valid(); iter.Next() { + if cb(iter.Key(), iter.Value()) { break } }