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
|
package ast
import (
"fmt"
"math/big"
"github.com/shopspring/decimal"
"github.com/dexon-foundation/dexon/common"
dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal"
se "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
)
var (
bigIntOne = big.NewInt(1)
bigIntTen = big.NewInt(10)
)
// DataTypeMajor defines type for high byte of DataType.
type DataTypeMajor uint8
// DataTypeMinor defines type for low byte of DataType.
type DataTypeMinor uint8
// DataType defines type for data type encoded.
type DataType uint16
// DataTypeMajor enums.
const (
DataTypeMajorUnknown DataTypeMajor = iota
DataTypeMajorSpecial
DataTypeMajorBool
DataTypeMajorAddress
DataTypeMajorInt
DataTypeMajorUint
DataTypeMajorFixedBytes
DataTypeMajorDynamicBytes
DataTypeMajorFixed DataTypeMajor = 0x10
DataTypeMajorUfixed DataTypeMajor = 0x30
)
// DataTypeMinor enums.
const (
DataTypeMinorDontCare DataTypeMinor = 0x00
DataTypeMinorSpecialNull DataTypeMinor = 0x00
DataTypeMinorSpecialAny DataTypeMinor = 0x01
DataTypeMinorSpecialDefault DataTypeMinor = 0x02
)
// DataTypeUnknown for unknown data type.
const DataTypeUnknown DataType = 0
// DecomposeDataType to major and minor part with given data type.
func DecomposeDataType(t DataType) (DataTypeMajor, DataTypeMinor) {
return DataTypeMajor(t >> 8), DataTypeMinor(t & 0xff)
}
// ComposeDataType to concrete type with major and minor part.
func ComposeDataType(major DataTypeMajor, minor DataTypeMinor) DataType {
return (DataType(major) << 8) | DataType(minor)
}
// IsFixedRange checks if major is in range of DataTypeMajorFixed.
func (d DataTypeMajor) IsFixedRange() bool {
return d >= DataTypeMajorFixed && d-DataTypeMajorFixed <= 0x1f
}
// IsUfixedRange checks if major is in range of DataTypeMajorUfixed.
func (d DataTypeMajor) IsUfixedRange() bool {
return d >= DataTypeMajorUfixed && d-DataTypeMajorUfixed <= 0x1f
}
// Size return the bytes of the data type occupied.
func (dt DataType) Size() uint8 {
major, minor := DecomposeDataType(dt)
if major.IsFixedRange() {
return uint8(major - DataTypeMajorFixed + 1)
}
if major.IsUfixedRange() {
return uint8(major - DataTypeMajorUfixed + 1)
}
switch major {
case DataTypeMajorBool:
return 1
case DataTypeMajorDynamicBytes:
return common.HashLength
case DataTypeMajorAddress:
return common.AddressLength
case DataTypeMajorInt, DataTypeMajorUint, DataTypeMajorFixedBytes:
return uint8(minor + 1)
default:
panic(fmt.Sprintf("unknown data type %v", dt))
}
}
// GetNode constructs an AST node from a data type.
func (dt DataType) GetNode() TypeNode {
major, minor := DecomposeDataType(dt)
switch major {
case DataTypeMajorBool:
return &BoolTypeNode{}
case DataTypeMajorAddress:
return &AddressTypeNode{}
case DataTypeMajorInt:
if minor <= 0x1f {
size := (uint32(minor) + 1) * 8
return &IntTypeNode{Unsigned: false, Size: size}
}
case DataTypeMajorUint:
if minor <= 0x1f {
size := (uint32(minor) + 1) * 8
return &IntTypeNode{Unsigned: true, Size: size}
}
case DataTypeMajorFixedBytes:
if minor <= 0x1f {
size := uint32(minor) + 1
return &FixedBytesTypeNode{Size: size}
}
case DataTypeMajorDynamicBytes:
return &DynamicBytesTypeNode{}
}
switch {
case major.IsFixedRange():
if minor <= 80 {
size := (uint32(major-DataTypeMajorFixed) + 1) * 8
fractionalDigits := uint32(minor)
return &FixedTypeNode{
Unsigned: false,
Size: size,
FractionalDigits: fractionalDigits,
}
}
case major.IsUfixedRange():
if minor <= 80 {
size := (uint32(major-DataTypeMajorUfixed) + 1) * 8
fractionalDigits := uint32(minor)
return &FixedTypeNode{
Unsigned: true,
Size: size,
FractionalDigits: fractionalDigits,
}
}
}
return nil
}
type decimalMinMaxPair struct {
Min, Max decimal.Decimal
}
var decimalMinMaxMap = func() map[DataType]decimalMinMaxPair {
m := make(map[DataType]decimalMinMaxPair)
// bool
{
dt := ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
m[dt] = decimalMinMaxPair{Min: dec.False, Max: dec.True}
}
// address
{
dt := ComposeDataType(DataTypeMajorAddress, DataTypeMinorDontCare)
bigMax := new(big.Int)
bigMax.Lsh(bigIntOne, common.AddressLength*8)
bigMax.Sub(bigMax, bigIntOne)
max := decimal.NewFromBigInt(bigMax, 0)
m[dt] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
}
// uint, bytes{X}
for i := uint(0); i <= 0x1f; i++ {
dtUint := ComposeDataType(DataTypeMajorUint, DataTypeMinor(i))
dtBytes := ComposeDataType(DataTypeMajorFixedBytes, DataTypeMinor(i))
bigMax := new(big.Int)
bigMax.Lsh(bigIntOne, (i+1)*8)
bigMax.Sub(bigMax, bigIntOne)
max := decimal.NewFromBigInt(bigMax, 0)
m[dtUint] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
m[dtBytes] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
}
// int
for i := uint(0); i <= 0x1f; i++ {
dt := ComposeDataType(DataTypeMajorInt, DataTypeMinor(i))
bigMax := new(big.Int)
bigMax.Lsh(bigIntOne, (i+1)*8-1)
bigMin := new(big.Int)
bigMin.Neg(bigMax)
bigMax.Sub(bigMax, bigIntOne)
min := decimal.NewFromBigInt(bigMin, 0)
max := decimal.NewFromBigInt(bigMax, 0)
m[dt] = decimalMinMaxPair{Min: min, Max: max}
}
return m
}()
// GetMinMax returns min, max pair according to given data type.
func (dt DataType) GetMinMax() (decimal.Decimal, decimal.Decimal, bool) {
var (
pair decimalMinMaxPair
ok bool
)
pair, ok = decimalMinMaxMap[dt]
return pair.Min, pair.Max, ok
}
func decimalToBig(d decimal.Decimal) (b *big.Int) {
if exponent := int64(d.Exponent()); exponent >= 0 {
exp := new(big.Int).Exp(bigIntTen, big.NewInt(exponent), nil)
b = new(big.Int).Mul(d.Coefficient(), exp)
} else {
exp := new(big.Int).Exp(bigIntTen, big.NewInt(-exponent), nil)
b = new(big.Int).Div(d.Coefficient(), exp)
}
return
}
// Don't handle overflow here.
func decimalEncode(size int, d decimal.Decimal) []byte {
ret := make([]byte, size)
s := d.Sign()
if s == 0 {
return ret
}
b := decimalToBig(d)
if s > 0 {
bs := b.Bytes()
copy(ret[size-len(bs):], bs)
return ret
}
b.Add(b, bigIntOne)
bs := b.Bytes()
copy(ret[size-len(bs):], bs)
for idx := range ret {
ret[idx] = ^ret[idx]
}
return ret
}
// Don't handle overflow here.
func decimalDecode(signed bool, bs []byte) decimal.Decimal {
neg := false
if signed && (bs[0]&0x80 != 0) {
neg = true
for idx := range bs {
bs[idx] = ^bs[idx]
}
}
b := new(big.Int).SetBytes(bs)
if neg {
b.Add(b, bigIntOne)
b.Neg(b)
}
return decimal.NewFromBigInt(b, 0)
}
// DecimalEncode encodes decimal to bytes depend on data type.
func DecimalEncode(dt DataType, d decimal.Decimal) ([]byte, error) {
major, minor := DecomposeDataType(dt)
switch major {
case DataTypeMajorInt,
DataTypeMajorUint,
DataTypeMajorFixedBytes:
return decimalEncode(int(minor)+1, d), nil
case DataTypeMajorAddress:
return decimalEncode(common.AddressLength, d), nil
}
switch {
case major.IsFixedRange():
return decimalEncode(
int(major-DataTypeMajorFixed)+1,
d.Shift(int32(minor))), nil
case major.IsUfixedRange():
return decimalEncode(
int(major-DataTypeMajorUfixed)+1,
d.Shift(int32(minor))), nil
}
return nil, se.ErrorCodeDecimalEncode
}
// DecimalDecode decodes decimal from bytes.
func DecimalDecode(dt DataType, b []byte) (decimal.Decimal, error) {
major, minor := DecomposeDataType(dt)
switch major {
case DataTypeMajorInt:
return decimalDecode(true, b), nil
case DataTypeMajorUint,
DataTypeMajorFixedBytes,
DataTypeMajorAddress:
return decimalDecode(false, b), nil
}
switch {
case major.IsFixedRange():
return decimalDecode(true, b).Shift(-int32(minor)), nil
case major.IsUfixedRange():
return decimalDecode(false, b).Shift(-int32(minor)), nil
}
return decimal.Zero, se.ErrorCodeDecimalDecode
}
// DecimalToUint64 convert decimal to uint64.
// Negative case will return error, and decimal part will be trancated.
func DecimalToUint64(d decimal.Decimal) (uint64, error) {
s := d.Sign()
if s == 0 {
return 0, nil
}
if s < 0 {
return 0, se.ErrorCodeNegDecimalToUint64
}
return decimalToBig(d).Uint64(), nil
}
|