From d61ca4013edb3edae656856aca751338cd52c4bc Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 25 Mar 2022 11:26:11 +0100 Subject: [PATCH] Close iterators --- x/poe/keeper/historical_info.go | 8 ++++---- x/poe/keeper/keeper.go | 1 + x/twasm/keeper/privileged.go | 14 +++++++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/x/poe/keeper/historical_info.go b/x/poe/keeper/historical_info.go index 266e40db..c8770b74 100644 --- a/x/poe/keeper/historical_info.go +++ b/x/poe/keeper/historical_info.go @@ -47,11 +47,11 @@ func (k Keeper) DeleteHistoricalInfo(ctx sdk.Context, height int64) { func (k Keeper) iterateHistoricalInfo(ctx sdk.Context, cb func(stakingtypes.HistoricalInfo) bool) { store := ctx.KVStore(k.storeKey) - iterator := sdk.KVStorePrefixIterator(store, types.HistoricalInfoKey) - defer iterator.Close() + iter := sdk.KVStorePrefixIterator(store, types.HistoricalInfoKey) + defer iter.Close() - for ; iterator.Valid(); iterator.Next() { - histInfo := stakingtypes.MustUnmarshalHistoricalInfo(k.codec, iterator.Value()) + for ; iter.Valid(); iter.Next() { + histInfo := stakingtypes.MustUnmarshalHistoricalInfo(k.codec, iter.Value()) if cb(histInfo) { break } diff --git a/x/poe/keeper/keeper.go b/x/poe/keeper/keeper.go index 1be3d886..fe94554e 100644 --- a/x/poe/keeper/keeper.go +++ b/x/poe/keeper/keeper.go @@ -70,6 +70,7 @@ func (k Keeper) GetPoEContractAddress(ctx sdk.Context, ctype types.PoEContractTy func (k Keeper) IteratePoEContracts(ctx sdk.Context, cb func(types.PoEContractType, sdk.AccAddress) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractPrefix) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() for ; iter.Valid(); iter.Next() { // cb returns true to stop early ctype := types.PoEContractType_value[string(iter.Key())] diff --git a/x/twasm/keeper/privileged.go b/x/twasm/keeper/privileged.go index afb6cc5f..835ac507 100644 --- a/x/twasm/keeper/privileged.go +++ b/x/twasm/keeper/privileged.go @@ -156,6 +156,7 @@ func (k Keeper) IsPrivileged(ctx sdk.Context, contractAddr sdk.AccAddress) bool func (k Keeper) IteratePrivileged(ctx sdk.Context, cb func(sdk.AccAddress) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), privilegedContractsSecondaryIndexPrefix) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() for ; iter.Valid(); iter.Next() { // cb returns true to stop early if cb(iter.Key()) { @@ -170,9 +171,10 @@ func (k Keeper) appendToPrivilegedContracts(ctx sdk.Context, privilegeType types // find last position value for privilege type var pos uint8 - it := prefix.NewStore(store, getContractPrivilegesSecondaryIndexPrefix(privilegeType)).ReverseIterator(nil, nil) - if it.Valid() { - key := it.Key() + iter := prefix.NewStore(store, getContractPrivilegesSecondaryIndexPrefix(privilegeType)).ReverseIterator(nil, nil) + defer iter.Close() + if iter.Valid() { + key := iter.Key() pos = key[0] if privilegeType.IsSingleton() { return 0, wasmtypes.ErrDuplicate @@ -233,14 +235,16 @@ func (k Keeper) ExistsAnyPrivilegedContract(ctx sdk.Context, privilegeType types end := []byte{math.MaxUint8} prefixStore := prefix.NewStore(store, getContractPrivilegesSecondaryIndexPrefix(privilegeType)) - it := prefixStore.Iterator(start, end) - return it.Valid() + iter := prefixStore.Iterator(start, end) + defer iter.Close() + return iter.Valid() } // IteratePrivilegedContractsByType iterates through all contracts for the given type by position and address ASC func (k Keeper) IteratePrivilegedContractsByType(ctx sdk.Context, privilegeType types.PrivilegeType, cb func(prio uint8, contractAddr sdk.AccAddress) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), getContractPrivilegesSecondaryIndexPrefix(privilegeType)) iter := prefixStore.Iterator(nil, nil) + defer iter.Close() for ; iter.Valid(); iter.Next() { // cb returns true to stop early if cb(parseContractPosition(iter.Key()), iter.Value()) {