aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/runtime/functions_test.go
blob: e90e9b6c6346ac873c24897ea1f9fb7aad690aca (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
package runtime

import (
    "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"
)

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)
            }
        }
    }
}