aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/runtime/instructions_test.go
blob: 55dbab18d193fd352a98253ce6d9cc899ad90cae (plain) (blame)
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
package runtime

import (
    "encoding/hex"
    "math/big"
    "reflect"
    "testing"

    "github.com/dexon-foundation/decimal"
    "github.com/stretchr/testify/suite"

    dexCommon "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/state"
    "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"
    "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
    "github.com/dexon-foundation/dexon/core/vm/sqlvm/schema"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/ethdb"
)

type opLoadSuite struct {
    suite.Suite
    ctx      *common.Context
    headHash dexCommon.Hash
    address  dexCommon.Address
    slotHash []dexCommon.Hash
    raws     []*raw
}

type raw struct {
    Raw
    slotShift uint8
    byteShift uint8
    major     ast.DataTypeMajor
    minor     ast.DataTypeMinor
}

func createSchema(storage *common.Storage, raws []*raw) {
    storage.Schema = schema.Schema{
        schema.Table{
            Name: []byte("Table_A"),
        },
        schema.Table{
            Name:    []byte("Table_B"),
            Columns: make([]schema.Column, len(raws)),
        },
        schema.Table{
            Name: []byte("Table_C"),
        },
    }
    for i := range raws {
        storage.Schema[1].Columns[i] = schema.NewColumn(
            []byte{byte(i)},
            ast.ComposeDataType(raws[i].major, raws[i].minor),
            0, nil, 0, nil,
        )
    }
    storage.Schema.SetupColumnOffset()
}

// setSlotDataInStateDB store data in StateDB, and
// return corresponding slot hash and raw slice.
func setSlotDataInStateDB(head dexCommon.Hash, addr dexCommon.Address,
    storage *common.Storage) ([]dexCommon.Hash, []*raw) {

    hash := dexCommon.Hash{}
    var b []byte
    slotHash := []string{
        "0123112233445566778800000000000000000000000000000000000000000000",
        "48656c6c6f2c20776f726c64210000000000000000000000000000000000001a",
        "3132333435363738393000000000000000000000000000000000000000000000",
        "53514c564d2069732075736566756c2100000000000000000000000000000020",
        "0000000000000000000000000000000000000000000000000000000000000041",
        "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
    }
    uInt256Dt := ast.ComposeDataType(ast.DataTypeMajorUint, ast.DataTypeMinor(31))

    raws := []*raw{
        {
            Raw: Raw{
                Value: decimal.New(0x0123, 0),
                Bytes: nil,
            },
            slotShift: 0,
            byteShift: 0,
            major:     ast.DataTypeMajorUint,
            minor:     ast.DataTypeMinor(1),
        },
        {
            Raw: Raw{
                Value: decimal.New(0x1122334455667788, 0),
                Bytes: nil,
            },
            slotShift: 0,
            byteShift: 2,
            major:     ast.DataTypeMajorUint,
            minor:     ast.DataTypeMinor(7),
        },
        {
            Raw: Raw{
                Value: dec.False,
                Bytes: nil,
            },
            slotShift: 0,
            byteShift: 10,
            major:     ast.DataTypeMajorBool,
            minor:     ast.DataTypeMinor(0),
        },
        {
            Raw: Raw{
                Bytes: []byte("Hello, world!"),
            },
            slotShift: 1,
            byteShift: 0,
            major:     ast.DataTypeMajorDynamicBytes,
            minor:     ast.DataTypeMinor(0),
        },
        {
            Raw: Raw{
                Bytes: hexToBytes(slotHash[2][:20]),
            },
            slotShift: 2,
            byteShift: 0,
            major:     ast.DataTypeMajorFixedBytes,
            minor:     ast.DataTypeMinor(9),
        },
        {
            Raw: Raw{
                Bytes: []byte("SQLVM is useful!"),
            },
            slotShift: 3,
            byteShift: 0,
            major:     ast.DataTypeMajorDynamicBytes,
            minor:     ast.DataTypeMinor(0),
        },
        {
            Raw: Raw{
                Bytes: []byte("Hello world. Hello DEXON, SQLVM."),
            },
            slotShift: 4,
            byteShift: 0,
            major:     ast.DataTypeMajorDynamicBytes,
            minor:     ast.DataTypeMinor(0),
        },
        {
            Raw: Raw{
                Value: hexToDec(slotHash[5], uInt256Dt),
                Bytes: nil,
            },
            slotShift: 5,
            byteShift: 0,
            major:     ast.DataTypeMajorUint,
            minor:     ast.DataTypeMinor(31),
        },
    }

    // set slot hash
    hData := make([]dexCommon.Hash, len(slotHash))
    ptr := head
    for i, s := range slotHash {
        b, _ = hex.DecodeString(s)
        hData[i].SetBytes(b)
        storage.SetState(addr, ptr, hData[i])
        ptr = storage.ShiftHashUint64(ptr, uint64(1))
    }

    // set dynamic bytes data
    longDBytesLoc := 6
    longRaw := raws[longDBytesLoc]
    hash.SetBytes(longRaw.Bytes)
    ptr = storage.ShiftHashUint64(head, uint64(longRaw.slotShift))
    ptr = crypto.Keccak256Hash(ptr.Bytes())
    storage.SetState(addr, ptr, hash)

    return hData, raws
}

func hexToDec(s string, dt ast.DataType) decimal.Decimal {
    b, _ := hex.DecodeString(s)
    d, _ := ast.DecimalDecode(dt, b)
    return d
}

func hexToBytes(s string) []byte {
    b, _ := hex.DecodeString(s)
    return b
}

type opLoadTestCase struct {
    title          string
    outputIdx      uint
    expectedOutput *Operand
    expectedErr    error
    ids            []uint64
    fields         []uint8
    tableRef       schema.TableRef
}

func (s *opLoadSuite) SetupTest() {
    s.ctx = &common.Context{}
    s.ctx.Storage = newStorage()
    targetTableRef := schema.TableRef(1)
    s.headHash = s.ctx.Storage.GetRowPathHash(targetTableRef, uint64(123456))
    s.address = dexCommon.HexToAddress("0x6655")
    s.ctx.Storage.CreateAccount(s.address)
    s.ctx.Contract = vm.NewContract(vm.AccountRef(s.address),
        vm.AccountRef(s.address), new(big.Int), 0)
    s.slotHash, s.raws = setSlotDataInStateDB(s.headHash, s.address, s.ctx.Storage)
    createSchema(s.ctx.Storage, s.raws)
    s.setColData(targetTableRef, 654321)
}

func (s *opLoadSuite) setColData(tableRef schema.TableRef, id uint64) {
    h := s.ctx.Storage.GetRowPathHash(tableRef, id)
    setSlotDataInStateDB(h, s.address, s.ctx.Storage)
}

func (s *opLoadSuite) getOpLoadTestCases(raws []*raw) []opLoadTestCase {
    testCases := []opLoadTestCase{
        {
            title:          "NIL_RESULT",
            outputIdx:      0,
            expectedOutput: &Operand{Meta: make([]ast.DataType, 0), Data: make([]Tuple, 0)},
            expectedErr:    nil,
            ids:            nil,
            fields:         nil,
            tableRef:       0,
        },
        {
            title:          "NOT_EXIST_TABLE",
            outputIdx:      0,
            expectedOutput: nil,
            expectedErr:    errors.ErrorCodeIndexOutOfRange,
            ids:            nil,
            fields:         nil,
            tableRef:       13,
        },
        {
            title:          "OK_CASE",
            outputIdx:      0,
            expectedOutput: s.getOKCaseOutput(raws),
            expectedErr:    nil,
            ids:            []uint64{123456, 654321},
            fields:         s.getOKCaseFields(raws),
            tableRef:       1,
        },
    }
    return testCases
}

func (s *opLoadSuite) getOKCaseOutput(raws []*raw) *Operand {
    rValue := &Operand{}
    size := len(raws)
    rValue.Meta = make([]ast.DataType, size)
    rValue.Data = make([]Tuple, 2)
    for j := range rValue.Data {
        rValue.Data[j] = make([]*Raw, size)
        for i, raw := range raws {
            rValue.Meta[i] = ast.ComposeDataType(raw.major, raw.minor)
            rValue.Data[j][i] = &raw.Raw
        }
    }
    return rValue
}

func (s *opLoadSuite) getOKCaseFields(raws []*raw) []uint8 {
    rValue := make([]uint8, len(raws))
    for i := range raws {
        rValue[i] = uint8(i)
    }
    return rValue
}

type decodeTestCase struct {
    dt             ast.DataType
    expectData     *Raw
    expectSlotHash dexCommon.Hash
    shift          uint64
    inputBytes     []byte
    dBytes         []byte
}

func (s *opLoadSuite) getDecodeTestCases(headHash dexCommon.Hash,
    address dexCommon.Address, storage *common.Storage) []decodeTestCase {

    slotHash, raws := setSlotDataInStateDB(headHash, address, storage)
    createSchema(storage, raws)
    testCases := make([]decodeTestCase, len(raws))

    for i := range testCases {
        r := raws[i]
        testCases[i].dt = ast.ComposeDataType(r.major, r.minor)
        testCases[i].shift = uint64(r.slotShift)
        testCases[i].expectSlotHash = slotHash[r.slotShift]
        testCases[i].expectData = &r.Raw
        slot := slotHash[r.slotShift]
        start := r.byteShift
        end := r.byteShift + testCases[i].dt.Size()
        testCases[i].inputBytes = slot.Bytes()[start:end]
    }
    return testCases
}

func (s *opLoadSuite) newRegisters(tableRef schema.TableRef, ids []uint64, fields []uint8) []*Operand {
    o := make([]*Operand, 4)
    o[1] = newTableRefOperand(tableRef)
    o[2] = newIDsOperand(ids)
    o[3] = newFieldsOperand(fields)
    return o
}

func newInput(nums []int) []*Operand {
    o := make([]*Operand, len(nums))
    for i, n := range nums {
        o[i] = &Operand{
            IsImmediate:   false,
            RegisterIndex: uint(n),
        }
    }
    return o
}

func newTableRefOperand(tableRef schema.TableRef) *Operand {
    if tableRef < 0 {
        return nil
    }
    o := &Operand{
        Meta: []ast.DataType{
            ast.ComposeDataType(ast.DataTypeMajorUint, 0),
        },
        Data: []Tuple{
            []*Raw{
                {
                    Value: decimal.New(int64(tableRef), 0),
                },
            },
        },
    }
    return o
}

func newIDsOperand(ids []uint64) *Operand {
    o := &Operand{
        Meta: []ast.DataType{
            ast.ComposeDataType(ast.DataTypeMajorUint, 7),
        },
    }
    o.Data = make([]Tuple, len(ids))
    for i := range o.Data {
        o.Data[i] = make([]*Raw, 1)
        o.Data[i][0] = &Raw{
            Value: decimal.New(int64(ids[i]), 0),
        }
    }
    return o
}

func newFieldsOperand(fields []uint8) *Operand {
    o := &Operand{
        Meta: []ast.DataType{
            ast.ComposeDataType(ast.DataTypeMajorUint, 0),
        },
    }
    o.Data = make([]Tuple, len(fields))
    for i := range o.Data {
        o.Data[i] = make([]*Raw, 1)
        o.Data[i][0] = &Raw{
            Value: decimal.New(int64(fields[i]), 0),
        }
    }
    return o
}

func newStorage() *common.Storage {
    db := ethdb.NewMemDatabase()
    state, _ := state.New(dexCommon.Hash{}, state.NewDatabase(db))
    storage := common.NewStorage(state)
    return storage
}

func (s *opLoadSuite) TestDecode() {
    testCases := s.getDecodeTestCases(s.headHash, s.address, s.ctx.Storage)
    for _, tt := range testCases {
        M, _ := ast.DecomposeDataType(tt.dt)
        slot := s.ctx.Storage.ShiftHashUint64(s.headHash, tt.shift)
        slotHash := s.ctx.Storage.GetState(s.address, slot)
        s.Require().Equal(tt.expectSlotHash, slotHash)

        data, err := decode(s.ctx, tt.dt, slot, tt.inputBytes)
        s.Require().Nil(err)

        if M == ast.DataTypeMajorDynamicBytes {
            s.Require().Equal(tt.expectData.Bytes, data.Bytes)
        } else {
            s.Require().True(tt.expectData.Value.Equal(data.Value))
        }
    }
}

func (s *opLoadSuite) TestOpLoad() {
    testCases := s.getOpLoadTestCases(s.raws)
    for _, t := range testCases {
        input := newInput([]int{1, 2, 3})
        reg := s.newRegisters(t.tableRef, t.ids, t.fields)

        loadRegister(input, reg)
        err := opLoad(s.ctx, input, reg, t.outputIdx)

        s.Require().Equalf(t.expectedErr, err, "testcase: [%v]", t.title)
        s.Require().Truef(reflect.DeepEqual(t.expectedOutput, reg[t.outputIdx]),
            "testcase: [%v], expect: %+v, result: %+v",
            t.title, t.expectedOutput, reg[t.outputIdx],
        )
    }
}

type opRepeatPKSuite struct{ suite.Suite }

type repeatPKTestCase struct {
    tableRef    schema.TableRef
    address     dexCommon.Address
    title       string
    expectedErr error
    expectedIDs []uint64
}

func (s opRepeatPKSuite) newInput(tableRef schema.TableRef) []*Operand {
    o := make([]*Operand, 1)
    o[0] = newTableRefOperand(tableRef)
    return o
}

func (s opRepeatPKSuite) newRegisters(tableRef schema.TableRef) []*Operand {
    o := make([]*Operand, 2)
    o[1] = newTableRefOperand(tableRef)
    return o
}

func (s opRepeatPKSuite) getTestCases(storage *common.Storage) []repeatPKTestCase {
    testCases := []repeatPKTestCase{
        {
            tableRef:    0,
            address:     dexCommon.BytesToAddress([]byte("0")),
            title:       "no IDs",
            expectedErr: nil,
            expectedIDs: []uint64{},
        },
        {
            tableRef:    1,
            address:     dexCommon.BytesToAddress([]byte("1")),
            title:       "ok case",
            expectedErr: nil,
            expectedIDs: []uint64{1, 2, 3, 4},
        },
    }
    for _, t := range testCases {
        headerSlot := storage.GetPrimaryPathHash(t.tableRef)
        storage.SetPK(t.address, headerSlot, t.expectedIDs)
    }
    return testCases
}

func (s opRepeatPKSuite) TestRepeatPK() {
    ctx := &common.Context{}
    ctx.Storage = newStorage()
    testCases := s.getTestCases(ctx.Storage)
    for _, t := range testCases {
        address := t.address
        ctx.Contract = vm.NewContract(vm.AccountRef(address),
            vm.AccountRef(address), new(big.Int), uint64(0))
        reg := s.newRegisters(t.tableRef)
        input := newInput([]int{1})
        loadRegister(input, reg)
        err := opRepeatPK(ctx, input, reg, 0)
        s.Require().Equalf(t.expectedErr, err, "testcase: [%v]", t.title)
        result, _ := reg[0].toUint64()
        s.Require().Equalf(t.expectedIDs, result, "testcase: [%v]", t.title)
    }
}

func makeOperand(im bool, meta []ast.DataType, pTuple []Tuple) (op *Operand) {
    op = &Operand{IsImmediate: im, Meta: meta, Data: pTuple}
    return
}

func loadRegister(input, registers []*Operand) {
    for i, operand := range input {
        if operand != nil && !operand.IsImmediate {
            input[i] = registers[operand.RegisterIndex]
        }
    }
}

type opTestcase struct {
    Name   string
    In     Instruction
    Output *Operand
    Err    error
}

type instructionSuite struct {
    suite.Suite
}

func (s *instructionSuite) run(testcases []opTestcase, opfunc OpFunction) {
    for idx, c := range testcases {
        registers := make([]*Operand, len(c.In.Input))

        for i, j := 0, 0; i < len(c.In.Input); i++ {
            if !c.In.Input[i].IsImmediate {
                registers[j] = c.In.Input[i]
                j++
            }
        }
        err := opfunc(
            &common.Context{Opt: common.Option{SafeMath: true}},
            c.In.Input, registers, c.In.Output)
        s.Require().Equal(
            c.Err, err,
            "idx: %v, op: %v, case: %v\nerror not equal: %v != %v",
            idx, c.In.Op, c.Name, c.Err, err,
        )
        if c.Err != nil {
            continue
        }

        result := registers[0]
        s.Require().True(
            c.Output.Equal(result),
            "idx: %v, op: %v, case: %v\noutput not equal.\nExpect: %v\nResult: %v\n",
            idx, c.In.Op, c.Name, c.Output, result,
        )
    }
}

type autoIncSuite struct {
    suite.Suite
    ctx *common.Context
}

func (s *autoIncSuite) SetupTest() {
    s.ctx = &common.Context{}
    s.ctx.Storage = newStorage()
    address := dexCommon.HexToAddress("0x6655")
    s.ctx.Storage.CreateAccount(address)
    s.ctx.Contract = vm.NewContract(vm.AccountRef(address),
        vm.AccountRef(address), new(big.Int), 0)
    s.ctx.Storage.Schema = schema.Schema{
        schema.Table{
            Name: []byte("normal_case"),
            Columns: []schema.Column{
                schema.NewColumn(
                    []byte("c1"),
                    ast.ComposeDataType(ast.DataTypeMajorInt, 0),
                    schema.ColumnAttrHasSequence,
                    nil,
                    0,
                    nil,
                ),
                schema.NewColumn(
                    []byte("c2"),
                    ast.ComposeDataType(ast.DataTypeMajorUint, 0),
                    schema.ColumnAttrHasSequence,
                    nil,
                    1,
                    nil,
                ),
            },
        },
        schema.Table{
            Name: []byte("overflow_int_case"),
            Columns: []schema.Column{
                schema.NewColumn(
                    []byte("c1"),
                    ast.ComposeDataType(ast.DataTypeMajorInt, 0),
                    schema.ColumnAttrHasSequence,
                    nil,
                    0,
                    nil,
                ),
            },
        },
        schema.Table{
            Name: []byte("overflow_uint_case"),
            Columns: []schema.Column{
                schema.NewColumn(
                    []byte("c1"),
                    ast.ComposeDataType(ast.DataTypeMajorUint, 0),
                    schema.ColumnAttrHasSequence,
                    nil,
                    0,
                    nil,
                ),
            },
        },
    }
    s.SetOverflow(1, 0, ast.ComposeDataType(ast.DataTypeMajorInt, 0))
    s.SetOverflow(2, 0, ast.ComposeDataType(ast.DataTypeMajorUint, 0))
    s.ctx.Storage.Schema.SetupColumnOffset()
}

func (s *autoIncSuite) SetOverflow(tableRef schema.TableRef, seqIdx uint8, dt ast.DataType) {
    storage := s.ctx.Storage
    seqPath := storage.GetSequencePathHash(tableRef, seqIdx)
    newHash := make([]byte, dexCommon.HashLength)
    _, max, _ := dt.GetMinMax()
    bs, _ := ast.DecimalEncode(dt, max)
    copy(newHash[len(newHash)-len(bs):], bs)
    storage.SetState(s.ctx.Contract.Address(), seqPath, dexCommon.BytesToHash(newHash))
}

func (s *autoIncSuite) TestFillAutoInc() {
    type testcase struct {
        name     string
        col      schema.Column
        tableRef schema.TableRef
        result   *Operand
        err      error
    }
    tt := []testcase{
        {
            name:     "normal case 1",
            col:      s.ctx.Storage.Schema[0].Columns[0],
            tableRef: schema.TableRef(0),
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorInt, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Value: decimal.New(1, 0),
                        },
                    },
                },
            },
            err: nil,
        },
        {
            name:     "normal case 2",
            col:      s.ctx.Storage.Schema[0].Columns[1],
            tableRef: schema.TableRef(0),
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Value: decimal.New(1, 0),
                        },
                    },
                },
            },
            err: nil,
        },
        {
            name:     "int overflow",
            col:      s.ctx.Storage.Schema[1].Columns[0],
            tableRef: schema.TableRef(1),
            result:   nil,
            err:      errors.ErrorCodeOverflow,
        },
        {
            name:     "unt overflow",
            col:      s.ctx.Storage.Schema[2].Columns[0],
            tableRef: schema.TableRef(2),
            result:   nil,
            err:      errors.ErrorCodeOverflow,
        },
    }

    for _, t := range tt {
        r, err := fillAutoInc(s.ctx, t.col, t.tableRef)
        s.Require().Equalf(t.err, err, "testcase %v\n", t.name)
        if t.err == nil {
            s.Require().Truef(r.Equal(t.result),
                "testcase: %v", t.name)
        }
    }
}

type setDefaultSuite struct {
    suite.Suite
    ctx *common.Context
}

func (s *setDefaultSuite) SetupTest() {
    s.ctx = &common.Context{}
    s.ctx.Storage = newStorage()
    address := dexCommon.HexToAddress("0x6655")
    s.ctx.Storage.CreateAccount(address)
    s.ctx.Contract = vm.NewContract(vm.AccountRef(address),
        vm.AccountRef(address), new(big.Int), 0)
    s.ctx.Storage.Schema = schema.Schema{
        schema.Table{
            Name: []byte("table1"),
            Columns: []schema.Column{
                schema.NewColumn(
                    []byte("c1"),
                    ast.ComposeDataType(ast.DataTypeMajorInt, 0),
                    schema.ColumnAttrHasDefault,
                    nil,
                    0,
                    decimal.New(127, 0),
                ),
                schema.NewColumn(
                    []byte("c2"),
                    ast.ComposeDataType(ast.DataTypeMajorDynamicBytes, 0),
                    schema.ColumnAttrHasDefault,
                    nil,
                    0,
                    []byte{1, 2, 3, 4},
                ),
                schema.NewColumn(
                    []byte("c3"),
                    ast.ComposeDataType(ast.DataTypeMajorUint, 0),
                    schema.ColumnAttrHasDefault,
                    nil,
                    1,
                    decimal.New(255, 0),
                ),
                schema.NewColumn(
                    []byte("c4"),
                    ast.ComposeDataType(ast.DataTypeMajorAddress, 0),
                    schema.ColumnAttrHasDefault,
                    nil,
                    1,
                    address[:],
                ),
            },
        },
    }
    s.ctx.Storage.Schema.SetupColumnOffset()
}

func (s *setDefaultSuite) TestFillDefault() {
    type testcase struct {
        name   string
        col    schema.Column
        result *Operand
        err    error
    }
    tt := []testcase{
        {
            name: "int8",
            col:  s.ctx.Storage.Schema[0].Columns[0],
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorInt, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Value: decimal.New(127, 0),
                        },
                    },
                },
            },
            err: nil,
        },
        {
            name: "dynamic byes",
            col:  s.ctx.Storage.Schema[0].Columns[1],
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorDynamicBytes, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Bytes: []byte{1, 2, 3, 4},
                        },
                    },
                },
            },
            err: nil,
        },
        {
            name: "uint8",
            col:  s.ctx.Storage.Schema[0].Columns[2],
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Value: decimal.New(255, 0),
                        },
                    },
                },
            },
            err: nil,
        },
        {
            name: "address",
            col:  s.ctx.Storage.Schema[0].Columns[3],
            result: &Operand{
                Meta: []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)},
                Data: []Tuple{
                    {
                        &Raw{
                            Bytes: dexCommon.HexToAddress("0x6655").Bytes(),
                        },
                    },
                },
            },
            err: nil,
        },
    }

    for _, t := range tt {
        r, err := fillDefault(s.ctx, t.col)
        s.Require().Equalf(t.err, err, "testcase %v\n", t.name)
        if t.err == nil {
            s.Require().Truef(r.Equal(t.result),
                "testcase: %v", t.name)
        }
    }
}

func TestInstructions(t *testing.T) {
    suite.Run(t, new(opLoadSuite))
    suite.Run(t, new(opRepeatPKSuite))
    suite.Run(t, new(instructionSuite))
    suite.Run(t, new(autoIncSuite))
    suite.Run(t, new(setDefaultSuite))
}