|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/CosmWasm/wasmd/x/wasm/internal/types" |
| 10 | + wasmTypes "github.com/CosmWasm/wasmd/x/wasm/internal/types" |
| 11 | + "github.com/cosmos/cosmos-sdk/store" |
| 12 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 13 | + "github.com/cosmos/cosmos-sdk/x/auth" |
| 14 | + "github.com/cosmos/cosmos-sdk/x/staking" |
| 15 | + fuzz "github.com/google/gofuzz" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + abci "github.com/tendermint/tendermint/abci/types" |
| 18 | + "github.com/tendermint/tendermint/libs/log" |
| 19 | + dbm "github.com/tendermint/tm-db" |
| 20 | +) |
| 21 | + |
| 22 | +func TestGenesisExportImport(t *testing.T) { |
| 23 | + srcKeeper, srcCtx, srcCleanup := setupKeeper(t) |
| 24 | + defer srcCleanup() |
| 25 | + wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") |
| 26 | + require.NoError(t, err) |
| 27 | + |
| 28 | + // store some test data |
| 29 | + f := fuzz.New().Funcs(FuzzAddr, FuzzAbsoluteTxPosition, FuzzContractInfo, FuzzStateModel) |
| 30 | + for i := 0; i < 25; i++ { |
| 31 | + var ( |
| 32 | + codeInfo types.CodeInfo |
| 33 | + contract types.ContractInfo |
| 34 | + stateModels []types.Model |
| 35 | + ) |
| 36 | + f.Fuzz(&codeInfo) |
| 37 | + f.Fuzz(&contract) |
| 38 | + f.Fuzz(&stateModels) |
| 39 | + |
| 40 | + codeID, err := srcKeeper.Create(srcCtx, codeInfo.Creator, wasmCode, codeInfo.Source, codeInfo.Builder) |
| 41 | + require.NoError(t, err) |
| 42 | + contract.CodeID = codeID |
| 43 | + contractAddr := srcKeeper.generateContractAddress(srcCtx, codeID) |
| 44 | + srcKeeper.setContractInfo(srcCtx, contractAddr, &contract) |
| 45 | + srcKeeper.setContractState(srcCtx, contractAddr, stateModels) |
| 46 | + } |
| 47 | + |
| 48 | + // export |
| 49 | + genesisState := ExportGenesis(srcCtx, srcKeeper) |
| 50 | + |
| 51 | + // re-import |
| 52 | + dstKeeper, dstCtx, dstCleanup := setupKeeper(t) |
| 53 | + defer dstCleanup() |
| 54 | + InitGenesis(dstCtx, dstKeeper, genesisState) |
| 55 | + |
| 56 | + // compare whole DB |
| 57 | + srcIT := srcCtx.KVStore(srcKeeper.storeKey).Iterator(nil, nil) |
| 58 | + dstIT := dstCtx.KVStore(dstKeeper.storeKey).Iterator(nil, nil) |
| 59 | + |
| 60 | + for i := 0; srcIT.Valid(); i++ { |
| 61 | + require.True(t, dstIT.Valid(), "destination DB has less elements than source. Missing: %q", srcIT.Key()) |
| 62 | + require.Equal(t, srcIT.Key(), dstIT.Key(), i) |
| 63 | + require.Equal(t, srcIT.Value(), dstIT.Value(), "element (%d): %s", i, srcIT.Key()) |
| 64 | + srcIT.Next() |
| 65 | + dstIT.Next() |
| 66 | + } |
| 67 | + require.False(t, dstIT.Valid()) |
| 68 | +} |
| 69 | + |
| 70 | +func setupKeeper(t *testing.T) (Keeper, sdk.Context, func()) { |
| 71 | + tempDir, err := ioutil.TempDir("", "wasm") |
| 72 | + require.NoError(t, err) |
| 73 | + cleanup := func() { os.RemoveAll(tempDir) } |
| 74 | + //t.Cleanup(cleanup) todo: add with Go 1.14 |
| 75 | + |
| 76 | + keyContract := sdk.NewKVStoreKey(wasmTypes.StoreKey) |
| 77 | + db := dbm.NewMemDB() |
| 78 | + ms := store.NewCommitMultiStore(db) |
| 79 | + ms.MountStoreWithDB(keyContract, sdk.StoreTypeIAVL, db) |
| 80 | + require.NoError(t, ms.LoadLatestVersion()) |
| 81 | + |
| 82 | + ctx := sdk.NewContext(ms, abci.Header{ |
| 83 | + Height: 1234567, |
| 84 | + Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC), |
| 85 | + }, false, log.NewNopLogger()) |
| 86 | + |
| 87 | + cdc := MakeTestCodec() |
| 88 | + wasmConfig := wasmTypes.DefaultWasmConfig() |
| 89 | + |
| 90 | + srcKeeper := NewKeeper(cdc, keyContract, auth.AccountKeeper{}, nil, staking.Keeper{}, nil, tempDir, wasmConfig, "", nil, nil) |
| 91 | + return srcKeeper, ctx, cleanup |
| 92 | +} |
0 commit comments