forked from CosmWasm/wasmvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocks.go
653 lines (562 loc) · 15.6 KB
/
mocks.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package api
import (
"encoding/json"
"errors"
"fmt"
"math"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/CosmWasm/wasmvm/v2/internal/api/testdb"
"github.com/CosmWasm/wasmvm/v2/types"
)
/** helper constructors **/
const MOCK_CONTRACT_ADDR = "contract"
func MockEnv() types.Env {
return types.Env{
Block: types.BlockInfo{
Height: 123,
Time: 1578939743_987654321,
ChainID: "foobar",
},
Transaction: &types.TransactionInfo{
Index: 4,
},
Contract: types.ContractInfo{
Address: MOCK_CONTRACT_ADDR,
},
}
}
func MockEnvBin(t testing.TB) []byte {
bin, err := json.Marshal(MockEnv())
require.NoError(t, err)
return bin
}
func MockInfo(sender types.HumanAddress, funds []types.Coin) types.MessageInfo {
return types.MessageInfo{
Sender: sender,
Funds: funds,
}
}
func MockInfoWithFunds(sender types.HumanAddress) types.MessageInfo {
return MockInfo(sender, []types.Coin{{
Denom: "ATOM",
Amount: "100",
}})
}
func MockInfoBin(t testing.TB, sender types.HumanAddress) []byte {
bin, err := json.Marshal(MockInfoWithFunds(sender))
require.NoError(t, err)
return bin
}
func MockIBCChannel(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannel {
return types.IBCChannel{
Endpoint: types.IBCEndpoint{
PortID: "my_port",
ChannelID: channelID,
},
CounterpartyEndpoint: types.IBCEndpoint{
PortID: "their_port",
ChannelID: "channel-7",
},
Order: ordering,
Version: ibcVersion,
ConnectionID: "connection-3",
}
}
func MockIBCChannelOpenInit(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelOpenMsg {
return types.IBCChannelOpenMsg{
OpenInit: &types.IBCOpenInit{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
OpenTry: nil,
}
}
func MockIBCChannelOpenTry(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelOpenMsg {
return types.IBCChannelOpenMsg{
OpenInit: nil,
OpenTry: &types.IBCOpenTry{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
CounterpartyVersion: ibcVersion,
},
}
}
func MockIBCChannelConnectAck(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelConnectMsg {
return types.IBCChannelConnectMsg{
OpenAck: &types.IBCOpenAck{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
CounterpartyVersion: ibcVersion,
},
OpenConfirm: nil,
}
}
func MockIBCChannelConnectConfirm(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelConnectMsg {
return types.IBCChannelConnectMsg{
OpenAck: nil,
OpenConfirm: &types.IBCOpenConfirm{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
}
}
func MockIBCChannelCloseInit(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelCloseMsg {
return types.IBCChannelCloseMsg{
CloseInit: &types.IBCCloseInit{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
CloseConfirm: nil,
}
}
func MockIBCChannelCloseConfirm(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelCloseMsg {
return types.IBCChannelCloseMsg{
CloseInit: nil,
CloseConfirm: &types.IBCCloseConfirm{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
}
}
func MockIBCPacket(myChannel string, data []byte) types.IBCPacket {
return types.IBCPacket{
Data: data,
Src: types.IBCEndpoint{
PortID: "their_port",
ChannelID: "channel-7",
},
Dest: types.IBCEndpoint{
PortID: "my_port",
ChannelID: myChannel,
},
Sequence: 15,
Timeout: types.IBCTimeout{
Block: &types.IBCTimeoutBlock{
Revision: 1,
Height: 123456,
},
},
}
}
func MockIBCPacketReceive(myChannel string, data []byte) types.IBCPacketReceiveMsg {
return types.IBCPacketReceiveMsg{
Packet: MockIBCPacket(myChannel, data),
}
}
func MockIBCPacketAck(myChannel string, data []byte, ack types.IBCAcknowledgement) types.IBCPacketAckMsg {
packet := MockIBCPacket(myChannel, data)
return types.IBCPacketAckMsg{
Acknowledgement: ack,
OriginalPacket: packet,
}
}
func MockIBCPacketTimeout(myChannel string, data []byte) types.IBCPacketTimeoutMsg {
packet := MockIBCPacket(myChannel, data)
return types.IBCPacketTimeoutMsg{
Packet: packet,
}
}
/*** Mock GasMeter ****/
// This code is borrowed from Cosmos-SDK store/types/gas.go
// ErrorOutOfGas defines an error thrown when an action results in out of gas.
type ErrorOutOfGas struct {
Descriptor string
}
// ErrorGasOverflow defines an error thrown when an action results gas consumption
// unsigned integer overflow.
type ErrorGasOverflow struct {
Descriptor string
}
type MockGasMeter interface {
types.GasMeter
ConsumeGas(amount types.Gas, descriptor string)
}
type mockGasMeter struct {
limit types.Gas
consumed types.Gas
}
// NewMockGasMeter returns a reference to a new mockGasMeter.
func NewMockGasMeter(limit types.Gas) MockGasMeter {
return &mockGasMeter{
limit: limit,
consumed: 0,
}
}
func (g *mockGasMeter) GasConsumed() types.Gas {
return g.consumed
}
func (g *mockGasMeter) Limit() types.Gas {
return g.limit
}
// addUint64Overflow performs the addition operation on two uint64 integers and
// returns a boolean on whether or not the result overflows.
func addUint64Overflow(a, b uint64) (uint64, bool) {
if math.MaxUint64-a < b {
return 0, true
}
return a + b, false
}
func (g *mockGasMeter) ConsumeGas(amount types.Gas, descriptor string) {
var overflow bool
// TODO: Should we set the consumed field after overflow checking?
g.consumed, overflow = addUint64Overflow(g.consumed, amount)
if overflow {
panic(ErrorGasOverflow{descriptor})
}
if g.consumed > g.limit {
panic(ErrorOutOfGas{descriptor})
}
}
/*** Mock types.KVStore ****/
// Much of this code is borrowed from Cosmos-SDK store/transient.go
// Note: these gas prices are all in *wasmer gas* and (sdk gas * 100)
//
// We making simple values and non-clear multiples so it is easy to see their impact in test output
// Also note we do not charge for each read on an iterator (out of simplicity and not needed for tests)
const (
GetPrice uint64 = 99000
SetPrice uint64 = 187000
RemovePrice uint64 = 142000
RangePrice uint64 = 261000
)
type Lookup struct {
db *testdb.MemDB
meter MockGasMeter
}
func NewLookup(meter MockGasMeter) *Lookup {
return &Lookup{
db: testdb.NewMemDB(),
meter: meter,
}
}
func (l *Lookup) SetGasMeter(meter MockGasMeter) {
l.meter = meter
}
func (l *Lookup) WithGasMeter(meter MockGasMeter) *Lookup {
return &Lookup{
db: l.db,
meter: meter,
}
}
// Get wraps the underlying DB's Get method panicing on error.
func (l Lookup) Get(key []byte) []byte {
l.meter.ConsumeGas(GetPrice, "get")
v, err := l.db.Get(key)
if err != nil {
panic(err)
}
return v
}
// Set wraps the underlying DB's Set method panicing on error.
func (l Lookup) Set(key, value []byte) {
l.meter.ConsumeGas(SetPrice, "set")
if err := l.db.Set(key, value); err != nil {
panic(err)
}
}
// Delete wraps the underlying DB's Delete method panicing on error.
func (l Lookup) Delete(key []byte) {
l.meter.ConsumeGas(RemovePrice, "remove")
if err := l.db.Delete(key); err != nil {
panic(err)
}
}
// Iterator wraps the underlying DB's Iterator method panicing on error.
func (l Lookup) Iterator(start, end []byte) types.Iterator {
l.meter.ConsumeGas(RangePrice, "range")
iter, err := l.db.Iterator(start, end)
if err != nil {
panic(err)
}
return iter
}
// ReverseIterator wraps the underlying DB's ReverseIterator method panicing on error.
func (l Lookup) ReverseIterator(start, end []byte) types.Iterator {
l.meter.ConsumeGas(RangePrice, "range")
iter, err := l.db.ReverseIterator(start, end)
if err != nil {
panic(err)
}
return iter
}
var _ types.KVStore = (*Lookup)(nil)
/***** Mock types.GoAPI ****/
const CanonicalLength = 32
const (
CostCanonical uint64 = 440
CostHuman uint64 = 550
)
func MockCanonicalizeAddress(human string) ([]byte, uint64, error) {
if len(human) > CanonicalLength {
return nil, 0, fmt.Errorf("human encoding too long")
}
res := make([]byte, CanonicalLength)
copy(res, []byte(human))
return res, CostCanonical, nil
}
func MockHumanizeAddress(canon []byte) (string, uint64, error) {
if len(canon) != CanonicalLength {
return "", 0, fmt.Errorf("wrong canonical length")
}
cut := CanonicalLength
for i, v := range canon {
if v == 0 {
cut = i
break
}
}
human := string(canon[:cut])
return human, CostHuman, nil
}
func MockValidateAddress(input string) (gasCost uint64, _ error) {
canonicalized, gasCostCanonicalize, err := MockCanonicalizeAddress(input)
gasCost += gasCostCanonicalize
if err != nil {
return gasCost, err
}
humanized, gasCostHumanize, err := MockHumanizeAddress(canonicalized)
gasCost += gasCostHumanize
if err != nil {
return gasCost, err
}
if humanized != strings.ToLower(input) {
return gasCost, fmt.Errorf("address validation failed")
}
return gasCost, nil
}
func NewMockAPI() *types.GoAPI {
return &types.GoAPI{
HumanizeAddress: MockHumanizeAddress,
CanonicalizeAddress: MockCanonicalizeAddress,
ValidateAddress: MockValidateAddress,
}
}
func TestMockApi(t *testing.T) {
human := "foobar"
canon, cost, err := MockCanonicalizeAddress(human)
require.NoError(t, err)
assert.Equal(t, CanonicalLength, len(canon))
assert.Equal(t, CostCanonical, cost)
recover, cost, err := MockHumanizeAddress(canon)
require.NoError(t, err)
assert.Equal(t, recover, human)
assert.Equal(t, CostHuman, cost)
}
/**** MockQuerier ****/
const DEFAULT_QUERIER_GAS_LIMIT = 1_000_000
type MockQuerier struct {
Bank BankQuerier
Custom CustomQuerier
usedGas uint64
}
var _ types.Querier = &MockQuerier{}
func DefaultQuerier(contractAddr string, coins types.Array[types.Coin]) types.Querier {
balances := map[string]types.Array[types.Coin]{
contractAddr: coins,
}
return &MockQuerier{
Bank: NewBankQuerier(balances),
Custom: NoCustom{},
usedGas: 0,
}
}
func (q *MockQuerier) Query(request types.QueryRequest, _gasLimit uint64) ([]byte, error) {
marshaled, err := json.Marshal(request)
if err != nil {
return nil, err
}
q.usedGas += uint64(len(marshaled))
if request.Bank != nil {
return q.Bank.Query(request.Bank)
}
if request.Custom != nil {
return q.Custom.Query(request.Custom)
}
if request.Staking != nil {
return nil, types.UnsupportedRequest{Kind: "staking"}
}
if request.Wasm != nil {
return nil, types.UnsupportedRequest{Kind: "wasm"}
}
return nil, types.Unknown{}
}
func (q MockQuerier) GasConsumed() uint64 {
return q.usedGas
}
type BankQuerier struct {
Balances map[string]types.Array[types.Coin]
}
func NewBankQuerier(balances map[string]types.Array[types.Coin]) BankQuerier {
bal := make(map[string]types.Array[types.Coin], len(balances))
for k, v := range balances {
dst := make([]types.Coin, len(v))
copy(dst, v)
bal[k] = dst
}
return BankQuerier{
Balances: bal,
}
}
func (q BankQuerier) Query(request *types.BankQuery) ([]byte, error) {
if request.Balance != nil {
denom := request.Balance.Denom
coin := types.NewCoin(0, denom)
for _, c := range q.Balances[request.Balance.Address] {
if c.Denom == denom {
coin = c
}
}
resp := types.BalanceResponse{
Amount: coin,
}
return json.Marshal(resp)
}
if request.AllBalances != nil {
coins := q.Balances[request.AllBalances.Address]
resp := types.AllBalancesResponse{
Amount: coins,
}
return json.Marshal(resp)
}
return nil, types.UnsupportedRequest{Kind: "Empty BankQuery"}
}
type CustomQuerier interface {
Query(request json.RawMessage) ([]byte, error)
}
type NoCustom struct{}
var _ CustomQuerier = NoCustom{}
func (q NoCustom) Query(request json.RawMessage) ([]byte, error) {
return nil, types.UnsupportedRequest{Kind: "custom"}
}
// ReflectCustom fulfills the requirements for testing `reflect` contract
type ReflectCustom struct{}
var _ CustomQuerier = ReflectCustom{}
type CustomQuery struct {
Ping *struct{} `json:"ping,omitempty"`
Capitalized *CapitalizedQuery `json:"capitalized,omitempty"`
}
type CapitalizedQuery struct {
Text string `json:"text"`
}
// CustomResponse is the response for all `CustomQuery`s
type CustomResponse struct {
Msg string `json:"msg"`
}
func (q ReflectCustom) Query(request json.RawMessage) ([]byte, error) {
var query CustomQuery
err := json.Unmarshal(request, &query)
if err != nil {
return nil, err
}
var resp CustomResponse
if query.Ping != nil {
resp.Msg = "PONG"
} else if query.Capitalized != nil {
resp.Msg = strings.ToUpper(query.Capitalized.Text)
} else {
return nil, errors.New("Unsupported query")
}
return json.Marshal(resp)
}
//************ test code for mocks *************************//
func TestBankQuerierAllBalances(t *testing.T) {
addr := "foobar"
balance := types.Array[types.Coin]{types.NewCoin(12345678, "ATOM"), types.NewCoin(54321, "ETH")}
q := DefaultQuerier(addr, balance)
// query existing account
req := types.QueryRequest{
Bank: &types.BankQuery{
AllBalances: &types.AllBalancesQuery{
Address: addr,
},
},
}
res, err := q.Query(req, DEFAULT_QUERIER_GAS_LIMIT)
require.NoError(t, err)
var resp types.AllBalancesResponse
err = json.Unmarshal(res, &resp)
require.NoError(t, err)
assert.Equal(t, resp.Amount, balance)
// query missing account
req2 := types.QueryRequest{
Bank: &types.BankQuery{
AllBalances: &types.AllBalancesQuery{
Address: "someone-else",
},
},
}
res, err = q.Query(req2, DEFAULT_QUERIER_GAS_LIMIT)
require.NoError(t, err)
var resp2 types.AllBalancesResponse
err = json.Unmarshal(res, &resp2)
require.NoError(t, err)
assert.Nil(t, resp2.Amount)
}
func TestBankQuerierBalance(t *testing.T) {
addr := "foobar"
balance := types.Array[types.Coin]{types.NewCoin(12345678, "ATOM"), types.NewCoin(54321, "ETH")}
q := DefaultQuerier(addr, balance)
// query existing account with matching denom
req := types.QueryRequest{
Bank: &types.BankQuery{
Balance: &types.BalanceQuery{
Address: addr,
Denom: "ATOM",
},
},
}
res, err := q.Query(req, DEFAULT_QUERIER_GAS_LIMIT)
require.NoError(t, err)
var resp types.BalanceResponse
err = json.Unmarshal(res, &resp)
require.NoError(t, err)
assert.Equal(t, resp.Amount, types.NewCoin(12345678, "ATOM"))
// query existing account with missing denom
req2 := types.QueryRequest{
Bank: &types.BankQuery{
Balance: &types.BalanceQuery{
Address: addr,
Denom: "BTC",
},
},
}
res, err = q.Query(req2, DEFAULT_QUERIER_GAS_LIMIT)
require.NoError(t, err)
var resp2 types.BalanceResponse
err = json.Unmarshal(res, &resp2)
require.NoError(t, err)
assert.Equal(t, resp2.Amount, types.NewCoin(0, "BTC"))
// query missing account
req3 := types.QueryRequest{
Bank: &types.BankQuery{
Balance: &types.BalanceQuery{
Address: "someone-else",
Denom: "ATOM",
},
},
}
res, err = q.Query(req3, DEFAULT_QUERIER_GAS_LIMIT)
require.NoError(t, err)
var resp3 types.BalanceResponse
err = json.Unmarshal(res, &resp3)
require.NoError(t, err)
assert.Equal(t, resp3.Amount, types.NewCoin(0, "ATOM"))
}
func TestReflectCustomQuerier(t *testing.T) {
q := ReflectCustom{}
// try ping
msg, err := json.Marshal(CustomQuery{Ping: &struct{}{}})
require.NoError(t, err)
bz, err := q.Query(msg)
require.NoError(t, err)
var resp CustomResponse
err = json.Unmarshal(bz, &resp)
require.NoError(t, err)
assert.Equal(t, resp.Msg, "PONG")
// try capital
msg2, err := json.Marshal(CustomQuery{Capitalized: &CapitalizedQuery{Text: "small."}})
require.NoError(t, err)
bz, err = q.Query(msg2)
require.NoError(t, err)
var resp2 CustomResponse
err = json.Unmarshal(bz, &resp2)
require.NoError(t, err)
assert.Equal(t, resp2.Msg, "SMALL.")
}