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

Close iterators #318

Merged
merged 1 commit into from
Mar 28, 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
8 changes: 4 additions & 4 deletions x/poe/keeper/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions x/poe/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())]
Expand Down
14 changes: 9 additions & 5 deletions x/twasm/keeper/privileged.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down