diff --git a/x/wasm/keeper/genesis.go b/x/wasm/keeper/genesis.go index 15978a9170..3c95e1f8cd 100644 --- a/x/wasm/keeper/genesis.go +++ b/x/wasm/keeper/genesis.go @@ -119,6 +119,8 @@ func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState { ContractState: state, }) + contractStateIterator.Close() + return false }) diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index b1850e044d..9711741d3b 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -113,7 +113,9 @@ func TestGenesisExportImport(t *testing.T) { wasmKeeper.IterateContractInfo(srcCtx, func(address sdk.AccAddress, info wasmTypes.ContractInfo) bool { wasmKeeper.removeFromContractCodeSecondaryIndex(srcCtx, address, wasmKeeper.getLastContractHistoryEntry(srcCtx, address)) prefixStore := prefix.NewStore(srcCtx.KVStore(wasmKeeper.storeKey), types.GetContractCodeHistoryElementPrefix(address)) - for iter := prefixStore.Iterator(nil, nil); iter.Valid(); iter.Next() { + iter := prefixStore.Iterator(nil, nil) + + for ; iter.Valid(); iter.Next() { prefixStore.Delete(iter.Key()) } x := &info @@ -121,6 +123,7 @@ func TestGenesisExportImport(t *testing.T) { wasmKeeper.storeContractInfo(srcCtx, address, x) wasmKeeper.addToContractCodeSecondaryIndex(srcCtx, address, newHistory) wasmKeeper.appendToContractHistory(srcCtx, address, newHistory) + iter.Close() return false }) @@ -145,6 +148,8 @@ func TestGenesisExportImport(t *testing.T) { if !assert.False(t, dstIT.Valid()) { t.Fatalf("dest Iterator still has key :%X", dstIT.Key()) } + srcIT.Close() + dstIT.Close() } } diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index b56faa1638..2f897855df 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -524,7 +524,10 @@ func (k Keeper) removeFromContractCodeSecondaryIndex(ctx sdk.Context, contractAd // IterateContractsByCode iterates over all contracts with given codeID ASC on code update time. func (k Keeper) IterateContractsByCode(ctx sdk.Context, codeID uint64, cb func(address sdk.AccAddress) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetContractByCodeIDSecondaryIndexPrefix(codeID)) - for iter := prefixStore.Iterator(nil, nil); iter.Valid(); iter.Next() { + iter := prefixStore.Iterator(nil, nil) + defer iter.Close() + + for ; iter.Valid(); iter.Next() { key := iter.Key() if cb(key[types.AbsoluteTxPositionLen:]) { return @@ -550,7 +553,10 @@ func (k Keeper) appendToContractHistory(ctx sdk.Context, contractAddr sdk.AccAdd // find last element position var pos uint64 prefixStore := prefix.NewStore(store, types.GetContractCodeHistoryElementPrefix(contractAddr)) - if iter := prefixStore.ReverseIterator(nil, nil); iter.Valid() { + iter := prefixStore.ReverseIterator(nil, nil) + defer iter.Close() + + if iter.Valid() { pos = sdk.BigEndianToUint64(iter.Value()) } // then store with incrementing position @@ -565,6 +571,8 @@ func (k Keeper) GetContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress) prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetContractCodeHistoryElementPrefix(contractAddr)) r := make([]types.ContractCodeHistoryEntry, 0) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() + for ; iter.Valid(); iter.Next() { var e types.ContractCodeHistoryEntry k.cdc.MustUnmarshal(iter.Value(), &e) @@ -577,6 +585,8 @@ func (k Keeper) GetContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress) func (k Keeper) getLastContractHistoryEntry(ctx sdk.Context, contractAddr sdk.AccAddress) types.ContractCodeHistoryEntry { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetContractCodeHistoryElementPrefix(contractAddr)) iter := prefixStore.ReverseIterator(nil, nil) + defer iter.Close() + var r types.ContractCodeHistoryEntry if !iter.Valid() { // all contracts have a history @@ -666,6 +676,8 @@ func (k Keeper) storeContractInfo(ctx sdk.Context, contractAddress sdk.AccAddres func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractKeyPrefix) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() + for ; iter.Valid(); iter.Next() { var contract types.ContractInfo k.cdc.MustUnmarshal(iter.Value(), &contract) @@ -716,6 +728,8 @@ func (k Keeper) containsCodeInfo(ctx sdk.Context, codeID uint64) bool { func (k Keeper) IterateCodeInfos(ctx sdk.Context, cb func(uint64, types.CodeInfo) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.CodeKeyPrefix) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() + for ; iter.Valid(); iter.Next() { var c types.CodeInfo k.cdc.MustUnmarshal(iter.Value(), &c) @@ -788,6 +802,8 @@ func (k Keeper) IsPinnedCode(ctx sdk.Context, codeID uint64) bool { func (k Keeper) InitializePinnedCodes(ctx sdk.Context) error { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PinnedCodeIndexPrefix) iter := store.Iterator(nil, nil) + defer iter.Close() + for ; iter.Valid(); iter.Next() { codeInfo := k.GetCodeInfo(ctx, types.ParsePinnedCodeIndex(iter.Key())) if codeInfo == nil { diff --git a/x/wasm/keeper/legacy_querier.go b/x/wasm/keeper/legacy_querier.go index 768c3def4d..78e23f0690 100644 --- a/x/wasm/keeper/legacy_querier.go +++ b/x/wasm/keeper/legacy_querier.go @@ -93,7 +93,10 @@ func queryContractState(ctx sdk.Context, bech, queryMethod string, data []byte, case QueryMethodContractStateAll: resultData := make([]types.Model, 0) // this returns a serialized json object (which internally encoded binary fields properly) - for iter := keeper.GetContractState(ctx, contractAddr); iter.Valid(); iter.Next() { + iter := keeper.GetContractState(ctx, contractAddr) + defer iter.Close() + + for ; iter.Valid(); iter.Next() { resultData = append(resultData, types.Model{ Key: iter.Key(), Value: iter.Value(), diff --git a/x/wasm/types/iavl_range_test.go b/x/wasm/types/iavl_range_test.go index aedfaea4f3..1e5bdc92ab 100644 --- a/x/wasm/types/iavl_range_test.go +++ b/x/wasm/types/iavl_range_test.go @@ -63,6 +63,7 @@ func TestIavlRangeBounds(t *testing.T) { } items := consume(iter) require.Equal(t, tc.expected, items) + iter.Close() }) } } @@ -73,8 +74,6 @@ type KV struct { } func consume(itr store.Iterator) []KV { - defer itr.Close() - var res []KV for ; itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value()