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
|
package runtime
import (
"math"
"math/big"
"testing"
"github.com/dexon-foundation/decimal"
"github.com/stretchr/testify/suite"
dexCommon "github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/core/vm"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/ast"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/common"
dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal"
)
func TestFunction(t *testing.T) {
suite.Run(t, new(FunctionSuite))
}
type FunctionSuite struct {
suite.Suite
}
var (
hash1 = dexCommon.BigToHash(big.NewInt(1))
hash255 = dexCommon.BigToHash(big.NewInt(255))
)
var mockNumberHashTable = map[uint64]dexCommon.Hash{1: hash1, 255: hash255}
func mockGetHashFunc(u uint64) dexCommon.Hash { return mockNumberHashTable[u] }
func (s *FunctionSuite) TestFnBlockHash() {
type blockHashCase struct {
Name string
Ops []*Operand
Length uint64
Res [][]byte
Cur *big.Int
Err error
}
testcases := []blockHashCase{
{"Immediate OP", []*Operand{
{IsImmediate: true, Meta: nil, Data: []Tuple{{&Raw{Value: decimal.New(1, 0)}}}},
}, 2, [][]byte{hash1.Bytes(), hash1.Bytes()}, big.NewInt(255), nil},
{"OP", []*Operand{
{IsImmediate: false, Meta: nil, Data: []Tuple{
{&Raw{Value: decimal.New(255, 0)}},
{&Raw{Value: decimal.New(515, 0)}},
}},
}, 2, [][]byte{hash255.Bytes(), make([]byte, 32)}, big.NewInt(256), nil},
{"Older than 257 block", []*Operand{
{IsImmediate: false, Meta: nil, Data: []Tuple{
{&Raw{Value: decimal.New(1, 0)}},
}},
}, 1, [][]byte{make([]byte, 32)}, big.NewInt(512), nil},
}
callFn := func(c blockHashCase) (*Operand, error) {
return fnBlockHash(
&common.Context{
Context: vm.Context{
GetHash: mockGetHashFunc,
BlockNumber: c.Cur,
},
},
c.Ops,
c.Length,
)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 3)}
for idx, tCase := range testcases {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().Equal(
tCase.Res[i], r.Data[i][0].Bytes,
"TestCase Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res[i], r.Data[i][0].Bytes)
}
}
}
func (s *FunctionSuite) TestFnBlockNumber() {
type blockNumberCase struct {
Name string
RawNum *big.Int
Length uint64
ResNum decimal.Decimal
Err error
AsserPanic bool
}
testcases := []blockNumberCase{
{"number 1 with length 1", big.NewInt(1), 1, decimal.New(1, 0), nil, false},
{"number 10 with length 10", big.NewInt(10), 10, decimal.New(10, 0), nil, false},
{"number 1 with length 0", big.NewInt(1), 0, decimal.New(1, 0), nil, false},
{"panic on invalid context", nil, 0, decimal.New(1, 0), nil, true},
}
callFn := func(c blockNumberCase) (*Operand, error) {
return fnBlockNumber(
&common.Context{
Context: vm.Context{BlockNumber: c.RawNum},
},
nil,
c.Length)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}
for idx, tCase := range testcases {
if tCase.AsserPanic {
s.Require().Panicsf(
func() { callFn(tCase) },
"Index: %v. Not Panic on '%v'", idx, tCase.Name,
)
} else {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().True(
tCase.ResNum.Equal(r.Data[i][0].Value),
"Index: %v Data index: %v. Value not equal: %v != %v",
idx, i, tCase.ResNum, r.Data[i][0].Value)
}
}
}
}
func (s *FunctionSuite) TestFnBlockTimestamp() {
type blockTimestampCase struct {
Name string
Timestamp *big.Int
Length uint64
Res decimal.Decimal
Err error
AsserPanic bool
}
testcases := []blockTimestampCase{
{"number 1 with length 1", big.NewInt(1), 1, decimal.New(1, 0), nil, false},
{"number 10 with length 10", big.NewInt(10), 10, decimal.New(10, 0), nil, false},
{"number 1 with length 0", big.NewInt(1), 0, decimal.New(1, 0), nil, false},
{"panic on invalid context", nil, 0, decimal.New(1, 0), nil, true},
}
callFn := func(c blockTimestampCase) (*Operand, error) {
return fnBlockTimestamp(
&common.Context{
Context: vm.Context{Time: c.Timestamp},
},
nil,
c.Length)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}
for idx, tCase := range testcases {
if tCase.AsserPanic {
s.Require().Panicsf(
func() { callFn(tCase) },
"Index: %v. Not Panic on '%v'", idx, tCase.Name,
)
} else {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().True(
tCase.Res.Equal(r.Data[i][0].Value),
"Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res, r.Data[i][0].Value)
}
}
}
}
func (s *FunctionSuite) TestFnCoinBase() {
type blockCoinBaseCase struct {
Name string
Address dexCommon.Address
Length uint64
Res []byte
Err error
}
res := []byte{
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67}
address := dexCommon.BytesToAddress(res)
testcases := []blockCoinBaseCase{
{"address with length 1", address, 1, res, nil},
{"address with length 10", address, 10, res, nil},
{"address with length 0", address, 0, res, nil},
}
callFn := func(c blockCoinBaseCase) (*Operand, error) {
return fnBlockCoinBase(
&common.Context{
Context: vm.Context{Coinbase: c.Address},
},
nil,
c.Length)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}
for idx, tCase := range testcases {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().Equal(
tCase.Res, r.Data[i][0].Bytes,
"Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res, r.Data[i][0].Bytes)
}
}
}
func (s *FunctionSuite) TestFnGasLimit() {
type blockGasLimitCase struct {
Name string
Limit uint64
Length uint64
Res decimal.Decimal
Err error
}
testcases := []blockGasLimitCase{
{"max int64 with length 1", uint64(math.MaxInt64), 1, dec.MaxInt64, nil},
{"max uint64 with length 1", math.MaxUint64, 10, dec.MaxUint64, nil},
{"address with length 0", math.MaxUint64, 0, decimal.Zero, nil},
}
callFn := func(c blockGasLimitCase) (*Operand, error) {
return fnBlockGasLimit(
&common.Context{
Context: vm.Context{GasLimit: c.Limit},
},
nil,
c.Length)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 7)}
for idx, tCase := range testcases {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().True(
tCase.Res.Equal(r.Data[i][0].Value),
"Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res, r.Data[i][0].Value)
}
}
}
func (s *FunctionSuite) TestFnMsgSender() {
type txMsgSenderCase struct {
Name string
Address dexCommon.Address
Length uint64
Res []byte
Err error
}
res := []byte{
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67}
address := dexCommon.BytesToAddress(res)
testcases := []txMsgSenderCase{
{"address with length 1", address, 1, res, nil},
{"address with length 10", address, 10, res, nil},
{"address with length 0", address, 0, res, nil},
}
callFn := func(c txMsgSenderCase) (*Operand, error) {
return fnMsgSender(
&common.Context{
Contract: &vm.Contract{CallerAddress: c.Address},
},
nil,
c.Length)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}
for idx, tCase := range testcases {
r, err := callFn(tCase)
s.Require().Equal(
tCase.Err, err,
"Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
s.Require().Equal(
meta, r.Meta,
"Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
s.Require().Equal(
uint64(len(r.Data)), tCase.Length,
"Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
for i := 0; i < len(r.Data); i++ {
s.Require().Equal(
tCase.Res, r.Data[i][0].Bytes,
"Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res, r.Data[i][0].Bytes)
}
}
}
|