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

Use callback pattern for contract state iterator #794

Merged
merged 1 commit into from
Mar 28, 2022

Conversation

alpe
Copy link
Contributor

@alpe alpe commented Mar 25, 2022

Follow up from #792

Thanks @albertchon for bringing this up! 💐

@alpe alpe requested a review from pinosu March 25, 2022 09:58
@codecov
Copy link

codecov bot commented Mar 25, 2022

Codecov Report

Merging #794 (a543aa5) into master (49ee92a) will decrease coverage by 0.04%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
x/wasm/keeper/keeper.go 87.47% <66.66%> (-0.27%) ⬇️
x/wasm/keeper/genesis.go 89.33% <100.00%> (-0.42%) ⬇️
x/wasm/keeper/legacy_querier.go 69.56% <100.00%> (-0.97%) ⬇️

Copy link
Contributor

@pinosu pinosu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 💯

Comment on lines +691 to +701
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Comment on lines +96 to +99
keeper.IterateContractState(ctx, contractAddr, func(key, value []byte) bool {
resultData = append(resultData, types.Model{Key: key, Value: value})
return false
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +106 to +109
keeper.IterateContractState(ctx, addr, func(key, value []byte) bool {
state = append(state, types.Model{Key: key, Value: value})
return false
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@alpe alpe merged commit 2032c33 into master Mar 28, 2022
@alpe alpe deleted the 792_contract_state_callback branch March 28, 2022 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants