diff --git a/x/wasm/genesis_test.go b/x/wasm/genesis_test.go new file mode 100644 index 00000000000..f165c193e8f --- /dev/null +++ b/x/wasm/genesis_test.go @@ -0,0 +1,93 @@ +package wasm + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestInitGenesis(t *testing.T) { + data, cleanup := setupTest(t) + defer cleanup() + + deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000)) + topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000)) + creator := createFakeFundedAccount(data.ctx, data.acctKeeper, deposit.Add(deposit)) + fred := createFakeFundedAccount(data.ctx, data.acctKeeper, topUp) + + h := data.module.NewHandler() + q := data.module.NewQuerierHandler() + + msg := MsgStoreCode{ + Sender: creator, + WASMByteCode: testContract, + } + res := h(data.ctx, msg) + require.True(t, res.IsOK()) + require.Equal(t, res.Data, []byte("1")) + + _, _, bob := keyPubAddr() + initMsg := initMsg{ + Verifier: fred.String(), + Beneficiary: bob.String(), + } + initMsgBz, err := json.Marshal(initMsg) + require.NoError(t, err) + + initCmd := MsgInstantiateContract{ + Sender: creator, + Code: 1, + InitMsg: initMsgBz, + InitFunds: deposit, + } + res = h(data.ctx, initCmd) + require.True(t, res.IsOK()) + contractAddr := sdk.AccAddress(res.Data) + + execCmd := MsgExecuteContract{ + Sender: fred, + Contract: contractAddr, + Msg: []byte("{}"), + SentFunds: topUp, + } + res = h(data.ctx, execCmd) + require.True(t, res.IsOK()) + + // ensure all contract state is as after init + assertCodeList(t, q, data.ctx, 1) + assertCodeBytes(t, q, data.ctx, 1, testContract) + + assertContractList(t, q, data.ctx, []string{contractAddr.String()}) + assertContractInfo(t, q, data.ctx, contractAddr, 1, creator) + assertContractState(t, q, data.ctx, contractAddr, state{ + Verifier: fred.String(), + Beneficiary: bob.String(), + Funder: creator.String(), + }) + + // export into genstate + genState := ExportGenesis(data.ctx, data.keeper) + + // create new app to import genstate into + newData, newCleanup := setupTest(t) + defer newCleanup() + q2 := newData.module.NewQuerierHandler() + + // initialize new app with genstate + InitGenesis(newData.ctx, newData.keeper, genState) + + // run same checks again on newdata, to make sure it was reinitialized correctly + assertCodeList(t, q2, newData.ctx, 1) + assertCodeBytes(t, q2, newData.ctx, 1, testContract) + + assertContractList(t, q2, newData.ctx, []string{contractAddr.String()}) + assertContractInfo(t, q2, newData.ctx, contractAddr, 1, creator) + assertContractState(t, q2, newData.ctx, contractAddr, state{ + Verifier: fred.String(), + Beneficiary: bob.String(), + Funder: creator.String(), + }) +} diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index eb9d83a6c82..15dafb3533c 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -23,6 +23,7 @@ type testData struct { module module.AppModule ctx sdk.Context acctKeeper auth.AccountKeeper + keeper Keeper } // returns a cleanup function, which must be defered on @@ -35,6 +36,7 @@ func setupTest(t *testing.T) (testData, func()) { module: NewAppModule(keeper), ctx: ctx, acctKeeper: acctKeeper, + keeper: keeper, } cleanup := func() { os.RemoveAll(tempDir) } return data, cleanup