forked from CosmWasm/wasmvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
228 lines (194 loc) · 6.51 KB
/
lib_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cosmwasm
import (
"io/ioutil"
"os"
"testing"
"github.com/CosmWasm/wasmvm/api"
"github.com/CosmWasm/wasmvm/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
TESTING_FEATURES = "staking,stargate,iterator"
TESTING_PRINT_DEBUG = false
TESTING_GAS_LIMIT = uint64(500_000_000_000) // ~0.5ms
TESTING_MEMORY_LIMIT = 32 // MiB
TESTING_CACHE_SIZE = 100 // MiB
)
const HACKATOM_TEST_CONTRACT = "./api/testdata/hackatom.wasm"
func withVM(t *testing.T) *VM {
tmpdir, err := ioutil.TempDir("", "wasmvm-testing")
require.NoError(t, err)
vm, err := NewVM(tmpdir, TESTING_FEATURES, TESTING_MEMORY_LIMIT, TESTING_PRINT_DEBUG, TESTING_CACHE_SIZE)
require.NoError(t, err)
t.Cleanup(func() {
vm.Cleanup()
os.RemoveAll(tmpdir)
})
return vm
}
func createTestContract(t *testing.T, vm *VM, path string) Checksum {
wasm, err := ioutil.ReadFile(path)
require.NoError(t, err)
checksum, err := vm.Create(wasm)
require.NoError(t, err)
return checksum
}
func TestCreateAndGet(t *testing.T) {
vm := withVM(t)
wasm, err := ioutil.ReadFile(HACKATOM_TEST_CONTRACT)
require.NoError(t, err)
checksum, err := vm.Create(wasm)
require.NoError(t, err)
code, err := vm.GetCode(checksum)
require.NoError(t, err)
require.Equal(t, WasmCode(wasm), code)
}
func TestHappyPath(t *testing.T) {
vm := withVM(t)
checksum := createTestContract(t, vm, HACKATOM_TEST_CONTRACT)
deserCost := types.UFraction{1, 1}
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{types.NewCoin(250, "ATOM")}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)
// instantiate
env := api.MockEnv()
info := api.MockInfo("creator", nil)
msg := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)
ires, _, err := vm.Instantiate(checksum, env, info, msg, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// execute
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
env = api.MockEnv()
info = api.MockInfo("fred", nil)
hres, _, err := vm.Execute(checksum, env, info, []byte(`{"release":{}}`), store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 1, len(hres.Messages))
// make sure it read the balance properly and we got 250 atoms
dispatch := hres.Messages[0].Msg
require.NotNil(t, dispatch.Bank, "%#v", dispatch)
require.NotNil(t, dispatch.Bank.Send, "%#v", dispatch)
send := dispatch.Bank.Send
assert.Equal(t, "bob", send.ToAddress)
assert.Equal(t, balance, send.Amount)
// check the data is properly formatted
expectedData := []byte{0xF0, 0x0B, 0xAA}
assert.Equal(t, expectedData, hres.Data)
}
func TestGetMetrics(t *testing.T) {
vm := withVM(t)
// GetMetrics 1
metrics, err := vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{}, metrics)
// Create contract
checksum := createTestContract(t, vm, HACKATOM_TEST_CONTRACT)
deserCost := types.UFraction{1, 1}
// GetMetrics 2
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{}, metrics)
// Instantiate 1
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{types.NewCoin(250, "ATOM")}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)
env := api.MockEnv()
info := api.MockInfo("creator", nil)
msg1 := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)
ires, _, err := vm.Instantiate(checksum, env, info, msg1, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// GetMetrics 3
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsFsCache: 1,
ElementsMemoryCache: 1,
SizeMemoryCache: 5665691,
}, metrics)
// Instantiate 2
msg2 := []byte(`{"verifier": "fred", "beneficiary": "susi"}`)
ires, _, err = vm.Instantiate(checksum, env, info, msg2, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// GetMetrics 4
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsMemoryCache: 1,
HitsFsCache: 1,
ElementsMemoryCache: 1,
SizeMemoryCache: 5665691,
}, metrics)
// Pin
err = vm.Pin(checksum)
require.NoError(t, err)
// GetMetrics 5
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsMemoryCache: 2,
HitsFsCache: 1,
ElementsPinnedMemoryCache: 1,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 5665691,
SizeMemoryCache: 5665691,
}, metrics)
// Instantiate 3
msg3 := []byte(`{"verifier": "fred", "beneficiary": "bert"}`)
ires, _, err = vm.Instantiate(checksum, env, info, msg3, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// GetMetrics 6
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsPinnedMemoryCache: 1,
HitsMemoryCache: 2,
HitsFsCache: 1,
ElementsPinnedMemoryCache: 1,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 5665691,
SizeMemoryCache: 5665691,
}, metrics)
// Unpin
err = vm.Unpin(checksum)
require.NoError(t, err)
// GetMetrics 7
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsPinnedMemoryCache: 1,
HitsMemoryCache: 2,
HitsFsCache: 1,
ElementsPinnedMemoryCache: 0,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 0,
SizeMemoryCache: 5665691,
}, metrics)
// Instantiate 4
msg4 := []byte(`{"verifier": "fred", "beneficiary": "jeff"}`)
ires, _, err = vm.Instantiate(checksum, env, info, msg4, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// GetMetrics 8
metrics, err = vm.GetMetrics()
require.NoError(t, err)
assert.Equal(t, &types.Metrics{
HitsPinnedMemoryCache: 1,
HitsMemoryCache: 3,
HitsFsCache: 1,
ElementsPinnedMemoryCache: 0,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 0,
SizeMemoryCache: 5665691,
}, metrics)
}