Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close iterators #792

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState {
ContractState: state,
})

contractStateIterator.Close()

return false
})

Expand Down
7 changes: 6 additions & 1 deletion x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ 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
newHistory := x.ResetFromGenesis(dstCtx)
wasmKeeper.storeContractInfo(srcCtx, address, x)
wasmKeeper.addToContractCodeSecondaryIndex(srcCtx, address, newHistory)
wasmKeeper.appendToContractHistory(srcCtx, address, newHistory)
iter.Close()
return false
})

Expand All @@ -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()
}
}

Expand Down
20 changes: 18 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion x/wasm/keeper/legacy_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions x/wasm/types/iavl_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestIavlRangeBounds(t *testing.T) {
}
items := consume(iter)
require.Equal(t, tc.expected, items)
iter.Close()
})
}
}
Expand All @@ -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()
Expand Down