-
Notifications
You must be signed in to change notification settings - Fork 410
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
Use callback pattern for contract state iterator #794
Conversation
Codecov Report
@@ Coverage Diff @@
## master #794 +/- ##
==========================================
- Coverage 58.73% 58.68% -0.05%
==========================================
Files 50 50
Lines 5857 5855 -2
==========================================
- Hits 3440 3436 -4
- Misses 2166 2167 +1
- Partials 251 252 +1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 💯
// IterateContractState iterates through all elements of the key value store for the given contract address and passes | ||
// them to the provided callback function. The callback method can return true to abort early. | ||
func (k Keeper) IterateContractState(ctx sdk.Context, contractAddress sdk.AccAddress, cb func(key, value []byte) bool) { | ||
prefixStoreKey := types.GetContractStorePrefix(contractAddress) | ||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey) | ||
return prefixStore.Iterator(nil, nil) | ||
iter := prefixStore.Iterator(nil, nil) | ||
defer iter.Close() | ||
|
||
for ; iter.Valid(); iter.Next() { | ||
if cb(iter.Key(), iter.Value()) { | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
keeper.IterateContractState(ctx, contractAddr, func(key, value []byte) bool { | ||
resultData = append(resultData, types.Model{Key: key, Value: value}) | ||
return false | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
keeper.IterateContractState(ctx, addr, func(key, value []byte) bool { | ||
state = append(state, types.Model{Key: key, Value: value}) | ||
return false | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Follow up from #792
Thanks @albertchon for bringing this up! 💐