forked from CosmWasm/wasmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis_test.go
103 lines (84 loc) · 3.12 KB
/
genesis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package wasm
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
func TestInitGenesis(t *testing.T) {
data := setupTest(t)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))
creator := data.faucet.NewFundedRandomAccount(data.ctx, deposit.Add(deposit...)...)
fred := data.faucet.NewFundedRandomAccount(data.ctx, topUp...)
msg := types.MsgStoreCode{
Sender: creator.String(),
WASMByteCode: testContract,
}
h := data.msgServiceRouter.Handler(&msg)
q := data.grpcQueryRouter
err := msg.ValidateBasic()
require.NoError(t, err)
res, err := h(data.ctx, &msg)
require.NoError(t, err)
assertStoreCodeResponse(t, res.Data, 1)
bob := keyPubAddr()
initMsg := initMsg{
Verifier: fred,
Beneficiary: bob,
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
instMsg := types.MsgInstantiateContract{
Sender: creator.String(),
CodeID: firstCodeID,
Msg: initMsgBz,
Funds: deposit,
Label: "testing",
}
h = data.msgServiceRouter.Handler(&instMsg)
res, err = h(data.ctx, &instMsg)
require.NoError(t, err)
contractBech32Addr := parseInitResponse(t, res.Data)
execMsg := types.MsgExecuteContract{
Sender: fred.String(),
Contract: contractBech32Addr,
Msg: []byte(`{"release":{}}`),
Funds: topUp,
}
h = data.msgServiceRouter.Handler(&execMsg)
res, err = h(data.ctx, &execMsg)
require.NoError(t, err)
// from https://github.com/CosmWasm/cosmwasm/blob/master/contracts/hackatom/src/contract.rs#L167
assertExecuteResponse(t, res.Data, []byte{0xf0, 0x0b, 0xaa})
// ensure all contract state is as after init
assertCodeList(t, q, data.ctx, 1, data.encConf.Codec)
assertCodeBytes(t, q, data.ctx, 1, testContract, data.encConf.Codec)
assertContractList(t, q, data.ctx, 1, []string{contractBech32Addr}, data.encConf.Codec)
assertContractInfo(t, q, data.ctx, contractBech32Addr, 1, creator, data.encConf.Codec)
assertContractState(t, q, data.ctx, contractBech32Addr, state{
Verifier: fred.String(),
Beneficiary: bob.String(),
Funder: creator.String(),
}, data.encConf.Codec)
// export into genstate
genState := keeper.ExportGenesis(data.ctx, &data.keeper)
// create new app to import genstate into
newData := setupTest(t)
q2 := newData.grpcQueryRouter
// initialize new app with genstate
_, err = keeper.InitGenesis(newData.ctx, &newData.keeper, *genState)
require.NoError(t, err)
// run same checks again on newdata, to make sure it was reinitialized correctly
assertCodeList(t, q2, newData.ctx, 1, data.encConf.Codec)
assertCodeBytes(t, q2, newData.ctx, 1, testContract, data.encConf.Codec)
assertContractList(t, q2, newData.ctx, 1, []string{contractBech32Addr}, data.encConf.Codec)
assertContractInfo(t, q2, newData.ctx, contractBech32Addr, 1, creator, data.encConf.Codec)
assertContractState(t, q2, newData.ctx, contractBech32Addr, state{
Verifier: fred.String(),
Beneficiary: bob.String(),
Funder: creator.String(),
}, data.encConf.Codec)
}