Skip to content

Commit 4502d95

Browse files
committed
Normalize wasm store
1 parent ba4da49 commit 4502d95

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

app/sim_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"os"
77
"path/filepath"
88
"testing"
9+
"time"
10+
11+
"github.com/cosmos/cosmos-sdk/store"
12+
"github.com/cosmos/cosmos-sdk/store/prefix"
913

1014
"github.com/cosmos/cosmos-sdk/baseapp"
1115
"github.com/cosmos/cosmos-sdk/codec"
@@ -192,6 +196,37 @@ func TestAppImportExport(t *testing.T) {
192196
// delete persistent tx counter value
193197
ctxA.KVStore(app.keys[wasm.StoreKey]).Delete(wasmtypes.TXCounterPrefix)
194198

199+
// reset contract code index in source DB for comparison with dest DB
200+
dropContractHistory := func(s store.KVStore, keys ...[]byte) {
201+
for _, key := range keys {
202+
prefixStore := prefix.NewStore(s, key)
203+
iter := prefixStore.Iterator(nil, nil)
204+
for ; iter.Valid(); iter.Next() {
205+
prefixStore.Delete(iter.Key())
206+
}
207+
iter.Close()
208+
}
209+
}
210+
prefixes := [][]byte{wasmtypes.ContractCodeHistoryElementPrefix, wasmtypes.ContractByCodeIDAndCreatedSecondaryIndexPrefix}
211+
dropContractHistory(ctxA.KVStore(app.keys[wasm.StoreKey]), prefixes...)
212+
dropContractHistory(ctxB.KVStore(newApp.keys[wasm.StoreKey]), prefixes...)
213+
214+
normalizeContractInfo := func(ctx sdk.Context, app *WasmApp) {
215+
var index uint64
216+
app.wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info wasmtypes.ContractInfo) bool {
217+
created := &wasmtypes.AbsoluteTxPosition{
218+
BlockHeight: uint64(0),
219+
TxIndex: index,
220+
}
221+
info.Created = created
222+
store := ctx.KVStore(app.keys[wasm.StoreKey])
223+
store.Set(wasmtypes.GetContractAddressKey(address), app.appCodec.MustMarshal(&info))
224+
index++
225+
return false
226+
})
227+
}
228+
normalizeContractInfo(ctxA, app)
229+
normalizeContractInfo(ctxB, newApp)
195230
// diff both stores
196231
for _, skp := range storeKeysPrefixes {
197232
storeA := ctxA.KVStore(skp.A)
@@ -250,5 +285,8 @@ func TestFullAppSimulation(t *testing.T) {
250285
func AppStateFn(codec codec.Codec, manager *module.SimulationManager) simtypes.AppStateFn {
251286
// quick hack to setup app state genesis with our app modules
252287
simapp.ModuleBasics = ModuleBasics
288+
if simapp.FlagGenesisTimeValue == 0 { // always set to have a block time
289+
simapp.FlagGenesisTimeValue = time.Now().Unix()
290+
}
253291
return simapp.AppStateFn(codec, manager)
254292
}

x/wasm/module.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
sdk "github.com/cosmos/cosmos-sdk/types"
1414
"github.com/cosmos/cosmos-sdk/types/module"
1515
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
16-
simKeeper "github.com/cosmos/cosmos-sdk/x/simulation"
1716
"github.com/gorilla/mux"
1817
"github.com/grpc-ecosystem/grpc-gateway/runtime"
1918
"github.com/spf13/cast"
@@ -105,7 +104,7 @@ type AppModule struct {
105104
keeper *Keeper
106105
validatorSetSource keeper.ValidatorSetSource
107106
accountKeeper types.AccountKeeper // for simulation
108-
bankKeeper simKeeper.BankKeeper
107+
bankKeeper simulation.BankKeeper
109108
}
110109

111110
// ConsensusVersion is a sequence number for state-breaking change of the
@@ -120,7 +119,7 @@ func NewAppModule(
120119
keeper *Keeper,
121120
validatorSetSource keeper.ValidatorSetSource,
122121
ak types.AccountKeeper,
123-
bk simKeeper.BankKeeper,
122+
bk simulation.BankKeeper,
124123
) AppModule {
125124
return AppModule{
126125
AppModuleBasic: AppModuleBasic{},

x/wasm/simulation/operations.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ type WasmKeeper interface {
2828
GetParams(ctx sdk.Context) types.Params
2929
IterateCodeInfos(ctx sdk.Context, cb func(uint64, types.CodeInfo) bool)
3030
}
31+
type BankKeeper interface {
32+
simulation.BankKeeper
33+
IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool
34+
}
3135

3236
// WeightedOperations returns all the operations from the module with their respective weights
3337
func WeightedOperations(
3438
simstate *module.SimulationState,
3539
ak types.AccountKeeper,
36-
bk simulation.BankKeeper,
40+
bk BankKeeper,
3741
wasmKeeper WasmKeeper,
3842
) simulation.WeightedOperations {
3943
var (
@@ -136,7 +140,7 @@ func DefaultSimulationCodeIDSelector(ctx sdk.Context, wasmKeeper WasmKeeper) uin
136140
}
137141

138142
// SimulateMsgInstantiateContract generates a MsgInstantiateContract with random values
139-
func SimulateMsgInstantiateContract(ak types.AccountKeeper, bk simulation.BankKeeper, wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.Operation {
143+
func SimulateMsgInstantiateContract(ak types.AccountKeeper, bk BankKeeper, wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.Operation {
140144
return func(
141145
r *rand.Rand,
142146
app *baseapp.BaseApp,
@@ -150,8 +154,14 @@ func SimulateMsgInstantiateContract(ak types.AccountKeeper, bk simulation.BankKe
150154
if codeID == 0 {
151155
return simtypes.NoOpMsg(types.ModuleName, types.MsgInstantiateContract{}.Type(), "no codes with permission available"), nil, nil
152156
}
157+
deposit := sdk.Coins{}
158+
spendableCoins := bk.SpendableCoins(ctx, simAccount.Address)
159+
for _, v := range spendableCoins {
160+
if bk.IsSendEnabledCoin(ctx, v) {
161+
deposit = deposit.Add(simtypes.RandSubsetCoins(r, sdk.NewCoins(v))...)
162+
}
163+
}
153164

154-
deposit := simtypes.RandSubsetCoins(r, bk.SpendableCoins(ctx, simAccount.Address))
155165
msg := types.MsgInstantiateContract{
156166
Sender: simAccount.Address.String(),
157167
Admin: simtypes.RandomAccounts(r, 1)[0].Address.String(),

0 commit comments

Comments
 (0)