Skip to content

Commit

Permalink
Merge pull request #338 from CosmWasm/fix_genesis_335
Browse files Browse the repository at this point in the history
Fix param usage issue in genesis import
  • Loading branch information
alpe authored Dec 14, 2020
2 parents 576f620 + b63a149 commit ae169ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
3 changes: 2 additions & 1 deletion x/wasm/internal/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
//
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) error {
keeper.setParams(ctx, data.Params)

var maxCodeID uint64
for i, code := range data.Codes {
err := keeper.importCode(ctx, code.CodeID, code.CodeInfo, code.CodeBytes)
Expand Down Expand Up @@ -50,7 +52,6 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) error
if keeper.peekAutoIncrementID(ctx, types.KeyLastInstanceID) <= uint64(maxContractID) {
return sdkerrors.Wrapf(types.ErrInvalid, "seq %s must be greater %d ", string(types.KeyLastInstanceID), maxContractID)
}
keeper.setParams(ctx, data.Params)

return nil
}
Expand Down
25 changes: 11 additions & 14 deletions x/wasm/internal/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ import (
const firstCodeID = 1

func TestGenesisExportImport(t *testing.T) {
srcKeeper, srcCtx, srcStoreKeys, srcCleanup := setupKeeper(t)
defer srcCleanup()
srcKeeper, srcCtx, srcStoreKeys := setupKeeper(t)

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

// store some test data
f := fuzz.New().Funcs(ModelFuzzers...)

srcKeeper.setParams(srcCtx, types.DefaultParams())

for i := 0; i < 25; i++ {
var (
codeInfo types.CodeInfo
Expand Down Expand Up @@ -88,8 +91,7 @@ func TestGenesisExportImport(t *testing.T) {
})

// re-import
dstKeeper, dstCtx, dstStoreKeys, dstCleanup := setupKeeper(t)
defer dstCleanup()
dstKeeper, dstCtx, dstStoreKeys := setupKeeper(t)

var importState wasmTypes.GenesisState
err = json.Unmarshal(exportedGenesis, &importState)
Expand Down Expand Up @@ -357,8 +359,7 @@ func TestFailFastImport(t *testing.T) {

for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
keeper, ctx, _, cleanup := setupKeeper(t)
defer cleanup()
keeper, ctx, _ := setupKeeper(t)

require.NoError(t, types.ValidateGenesis(spec.src))
got := InitGenesis(ctx, keeper, spec.src)
Expand Down Expand Up @@ -413,8 +414,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
{"id_key": %q, "value": "2"}
]
}`
keeper, ctx, _, dstCleanup := setupKeeper(t)
defer dstCleanup()
keeper, ctx, _ := setupKeeper(t)

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
Expand Down Expand Up @@ -482,12 +482,11 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
assert.Equal(t, expHistory, keeper.GetContractHistory(ctx, contractAddr))
}

func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey, func()) {
func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey) {
t.Helper()
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
cleanup := func() { os.RemoveAll(tempDir) }
//t.Cleanup(cleanup) todo: add with Go 1.14
t.Cleanup(func() { os.RemoveAll(tempDir) })
var (
keyParams = sdk.NewKVStoreKey(paramtypes.StoreKey)
tkeyParams = sdk.NewTransientStoreKey(paramtypes.TStoreKey)
Expand All @@ -511,7 +510,5 @@ func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey, func()) {
pk := paramskeeper.NewKeeper(encodingConfig.Marshaler, encodingConfig.Amino, keyParams, tkeyParams)

srcKeeper := NewKeeper(encodingConfig.Marshaler, keyWasm, pk.Subspace(wasmTypes.DefaultParamspace), authkeeper.AccountKeeper{}, nil, stakingkeeper.Keeper{}, distributionkeeper.Keeper{}, nil, tempDir, wasmConfig, "", nil, nil)
srcKeeper.setParams(ctx, wasmTypes.DefaultParams())

return &srcKeeper, ctx, []sdk.StoreKey{keyWasm, keyParams}, cleanup
return &srcKeeper, ctx, []sdk.StoreKey{keyWasm, keyParams}
}

0 comments on commit ae169ce

Please sign in to comment.