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

import (
    "fmt"
    "math"

    "github.com/dexon-foundation/decimal"

    "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"
    se "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
)

// function identifier
const (
    BLOCKHASH      = "BLOCK_HASH"
    BLOCKNUMBER    = "BLOCK_NUMBER"
    BLOCKTIMESTAMP = "BLOCK_TIMESTAMP"
    BLOCKCOINBASE  = "BLOCK_COINBASE"
    BLOCKGASLIMIT  = "BLOCK_GAS_LIMIT"
    MSGSENDER      = "MSG_SENDER"
    MSGDATA        = "MSG_DATA"
    TXORIGIN       = "TX_ORIGIN"
    NOW            = "NOW"
)

type fn func(*common.Context, []*Operand, uint64) (*Operand, error)

var (
    fnTable = map[string]fn{
        BLOCKHASH:      fnBlockHash,
        BLOCKNUMBER:    fnBlockNumber,
        BLOCKTIMESTAMP: fnBlockTimestamp,
        NOW:            fnBlockTimestamp,
        BLOCKCOINBASE:  fnBlockCoinBase,
        BLOCKGASLIMIT:  fnBlockGasLimit,
        MSGSENDER:      fnMsgSender,
        MSGDATA:        fnMsgData,
        TXORIGIN:       fnTxOrigin,
    }
)

func assignFuncResult(meta []ast.DataType, fn func() *Raw, length uint64) (result *Operand) {
    result = &Operand{Meta: meta, Data: make([]Tuple, length)}
    for i := uint64(0); i < length; i++ {
        result.Data[i] = Tuple{fn()}
    }
    return
}

func evalBlockHash(ctx *common.Context, num, cur decimal.Decimal) (r *Raw, err error) {
    r = &Raw{Bytes: make([]byte, 32)}

    cNum := cur.Sub(dec.Dec257)
    if num.Cmp(cNum) > 0 && num.Cmp(cur) < 0 {
        var num64 uint64
        num64, err = ast.DecimalToUint64(num)
        if err != nil {
            return
        }
        r.Bytes = ctx.GetHash(num64).Bytes()
    }
    return
}

func fnBlockHash(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    if len(ops) != 1 {
        err = se.ErrorCodeInvalidOperandNum
        return
    }

    meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 3)}
    cNum := decimal.NewFromBigInt(ctx.BlockNumber, 0)

    if ops[0].IsImmediate {
        var r *Raw
        r, err = evalBlockHash(ctx, ops[0].Data[0][0].Value, cNum)
        if err != nil {
            return
        }
        result = assignFuncResult(meta, r.clone, length)
    } else {
        result = &Operand{Meta: meta, Data: make([]Tuple, length)}
        for i := uint64(0); i < length; i++ {
            var r *Raw
            r, err = evalBlockHash(ctx, ops[0].Data[i][0].Value, cNum)
            if err != nil {
                return
            }
            result.Data[i] = Tuple{r}
        }
    }
    return
}

func fnBlockNumber(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Value: decimal.NewFromBigInt(ctx.BlockNumber, 0)}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)},
        r.clone, length,
    )
    return
}

func fnBlockTimestamp(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Value: decimal.NewFromBigInt(ctx.Time, 0)}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)},
        r.clone, length,
    )
    return
}

func fnBlockCoinBase(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Bytes: ctx.Coinbase.Bytes()}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)},
        r.clone, length,
    )
    return
}

func fnBlockGasLimit(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{}
    if ctx.GasLimit > uint64(math.MaxInt64) {
        r.Value, err = decimal.NewFromString(fmt.Sprint(ctx.GasLimit))
        if err != nil {
            return
        }
    } else {
        r.Value = decimal.New(int64(ctx.GasLimit), 0)
    }
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 7)},
        r.clone, length,
    )
    return
}

func fnMsgSender(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Bytes: ctx.Contract.CallerAddress.Bytes()}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)},
        r.clone, length,
    )
    return
}

func fnMsgData(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Bytes: ctx.Contract.Input}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorDynamicBytes, 0)},
        r.clone, length,
    )
    return
}

func fnTxOrigin(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) {
    r := &Raw{Bytes: ctx.Origin.Bytes()}
    result = assignFuncResult(
        []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)},
        r.clone, length,
    )
    return
}