Skip to content

Commit 2032c33

Browse files
authored
Merge pull request #794 from CosmWasm/792_contract_state_callback
Use callback pattern for contract state iterator
2 parents 49ee92a + a543aa5 commit 2032c33

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

x/wasm/keeper/genesis.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,18 @@ func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState {
102102
})
103103

104104
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool {
105-
contractStateIterator := keeper.GetContractState(ctx, addr)
106105
var state []types.Model
107-
for ; contractStateIterator.Valid(); contractStateIterator.Next() {
108-
m := types.Model{
109-
Key: contractStateIterator.Key(),
110-
Value: contractStateIterator.Value(),
111-
}
112-
state = append(state, m)
113-
}
106+
keeper.IterateContractState(ctx, addr, func(key, value []byte) bool {
107+
state = append(state, types.Model{Key: key, Value: value})
108+
return false
109+
})
114110
// redact contract info
115111
contract.Created = nil
116112
genState.Contracts = append(genState.Contracts, types.Contract{
117113
ContractAddress: addr.String(),
118114
ContractInfo: contract,
119115
ContractState: state,
120116
})
121-
122-
contractStateIterator.Close()
123-
124117
return false
125118
})
126119

x/wasm/keeper/keeper.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,19 @@ func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, typ
688688
}
689689
}
690690

691-
func (k Keeper) GetContractState(ctx sdk.Context, contractAddress sdk.AccAddress) sdk.Iterator {
691+
// IterateContractState iterates through all elements of the key value store for the given contract address and passes
692+
// them to the provided callback function. The callback method can return true to abort early.
693+
func (k Keeper) IterateContractState(ctx sdk.Context, contractAddress sdk.AccAddress, cb func(key, value []byte) bool) {
692694
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
693695
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
694-
return prefixStore.Iterator(nil, nil)
696+
iter := prefixStore.Iterator(nil, nil)
697+
defer iter.Close()
698+
699+
for ; iter.Valid(); iter.Next() {
700+
if cb(iter.Key(), iter.Value()) {
701+
break
702+
}
703+
}
695704
}
696705

697706
func (k Keeper) importContractState(ctx sdk.Context, contractAddress sdk.AccAddress, models []types.Model) error {

x/wasm/keeper/legacy_querier.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,10 @@ func queryContractState(ctx sdk.Context, bech, queryMethod string, data []byte,
9393
case QueryMethodContractStateAll:
9494
resultData := make([]types.Model, 0)
9595
// this returns a serialized json object (which internally encoded binary fields properly)
96-
iter := keeper.GetContractState(ctx, contractAddr)
97-
defer iter.Close()
98-
99-
for ; iter.Valid(); iter.Next() {
100-
resultData = append(resultData, types.Model{
101-
Key: iter.Key(),
102-
Value: iter.Value(),
103-
})
104-
}
96+
keeper.IterateContractState(ctx, contractAddr, func(key, value []byte) bool {
97+
resultData = append(resultData, types.Model{Key: key, Value: value})
98+
return false
99+
})
105100
bz, err := json.Marshal(resultData)
106101
if err != nil {
107102
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())

x/wasm/types/exported_keepers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ViewKeeper interface {
1515
GetContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress) *ContractInfo
1616
IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, ContractInfo) bool)
1717
IterateContractsByCode(ctx sdk.Context, codeID uint64, cb func(address sdk.AccAddress) bool)
18-
GetContractState(ctx sdk.Context, contractAddress sdk.AccAddress) sdk.Iterator
18+
IterateContractState(ctx sdk.Context, contractAddress sdk.AccAddress, cb func(key, value []byte) bool)
1919
GetCodeInfo(ctx sdk.Context, codeID uint64) *CodeInfo
2020
IterateCodeInfos(ctx sdk.Context, cb func(uint64, CodeInfo) bool)
2121
GetByteCode(ctx sdk.Context, codeID uint64) ([]byte, error)

0 commit comments

Comments
 (0)