aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/common/utilities.go
blob: acff30c9904c70c152ebc39b613da3e82a1b4fd7 (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
package common

import (
    "math/big"

    "github.com/dexon-foundation/decimal"
    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/vm/sqlvm/ast"
    "github.com/dexon-foundation/dexon/core/vm/sqlvm/schema"
)

// TODO(yenlin): Do we really need to use ast encode/decode here?
func uint64ToBytes(id uint64) []byte {
    bigIntID := new(big.Int).SetUint64(id)
    decimalID := decimal.NewFromBigInt(bigIntID, 0)
    dt := ast.ComposeDataType(ast.DataTypeMajorUint, 7)
    byteID, _ := ast.DecimalEncode(dt, decimalID)
    return byteID
}

func bytesToUint64(b []byte) uint64 {
    dt := ast.ComposeDataType(ast.DataTypeMajorUint, 7)
    d, _ := ast.DecimalDecode(dt, b)
    // TODO(yenlin): Not yet a convenient way to extract uint64 from decimal...
    bigInt := d.Rescale(0).Coefficient()
    return bigInt.Uint64()
}

func uint8ToBytes(i uint8) []byte {
    return []byte{i}
}

func tableRefToBytes(t schema.TableRef) []byte {
    return uint8ToBytes(uint8(t))
}

func columnRefToBytes(c schema.ColumnRef) []byte {
    return uint8ToBytes(uint8(c))
}

func indexRefToBytes(i schema.IndexRef) []byte {
    return uint8ToBytes(uint8(i))
}

func hashToAddress(hash common.Hash) common.Address {
    return common.BytesToAddress(hash.Bytes())
}

func addressToHash(addr common.Address) common.Hash {
    return common.BytesToHash(addr.Bytes())
}