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

Fix param usage issue in genesis import #338

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

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

Much better


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) })
Copy link
Member

Choose a reason for hiding this comment

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

Such a nice trick, cleaning up a lot

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}
}