-
Notifications
You must be signed in to change notification settings - Fork 246
/
extension_native.go
150 lines (133 loc) · 3.96 KB
/
extension_native.go
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
// Copyright (c) 2019 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package builtin
import (
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/xenv"
)
const (
blake2b256WordGas uint64 = 3
blake2b256Gas uint64 = 15
)
func init() {
defines := []struct {
name string
run func(env *xenv.Environment) []interface{}
}{
{"native_blake2b256", func(env *xenv.Environment) []interface{} {
var data []byte
env.ParseArgs(&data)
env.UseGas(uint64(len(data)+31)/32*blake2b256WordGas + blake2b256Gas)
output := thor.Blake2b(data)
return []interface{}{output}
}},
{"native_blockID", func(env *xenv.Environment) []interface{} {
var blockNum uint32
env.ParseArgs(&blockNum)
if blockNum >= env.BlockContext().Number {
return []interface{}{thor.Bytes32{}}
}
env.UseGas(thor.SloadGas)
output, err := env.Chain().GetBlockID(blockNum)
if err != nil {
panic(err)
}
return []interface{}{output}
}},
{"native_blockTotalScore", func(env *xenv.Environment) []interface{} {
var blockNum uint32
env.ParseArgs(&blockNum)
if blockNum > env.BlockContext().Number {
return []interface{}{uint64(0)}
}
if blockNum == env.BlockContext().Number {
return []interface{}{env.BlockContext().TotalScore}
}
env.UseGas(thor.SloadGas)
env.UseGas(thor.SloadGas)
header, err := env.Chain().GetBlockHeader(blockNum)
if err != nil {
panic(err)
}
return []interface{}{header.TotalScore()}
}},
{"native_blockTime", func(env *xenv.Environment) []interface{} {
var blockNum uint32
env.ParseArgs(&blockNum)
if blockNum > env.BlockContext().Number {
return []interface{}{uint64(0)}
}
if blockNum == env.BlockContext().Number {
return []interface{}{env.BlockContext().Time}
}
env.UseGas(thor.SloadGas)
env.UseGas(thor.SloadGas)
header, err := env.Chain().GetBlockHeader(blockNum)
if err != nil {
panic(err)
}
return []interface{}{header.Timestamp()}
}},
{"native_blockSigner", func(env *xenv.Environment) []interface{} {
var blockNum uint32
env.ParseArgs(&blockNum)
if blockNum > env.BlockContext().Number {
return []interface{}{thor.Address{}}
}
if blockNum == env.BlockContext().Number {
return []interface{}{env.BlockContext().Signer}
}
env.UseGas(thor.SloadGas)
env.UseGas(thor.SloadGas)
header, err := env.Chain().GetBlockHeader(blockNum)
if err != nil {
panic(err)
}
signer, err := header.Signer()
if err != nil {
panic(err)
}
return []interface{}{signer}
}},
{"native_totalSupply", func(env *xenv.Environment) []interface{} {
env.UseGas(thor.SloadGas)
output, err := Energy.Native(env.State(), env.BlockContext().Time).TokenTotalSupply()
if err != nil {
panic(err)
}
return []interface{}{output}
}},
{"native_txProvedWork", func(env *xenv.Environment) []interface{} {
output := env.TransactionContext().ProvedWork
return []interface{}{output}
}},
{"native_txID", func(env *xenv.Environment) []interface{} {
output := env.TransactionContext().ID
return []interface{}{output}
}},
{"native_txBlockRef", func(env *xenv.Environment) []interface{} {
output := env.TransactionContext().BlockRef
return []interface{}{output}
}},
{"native_txExpiration", func(env *xenv.Environment) []interface{} {
output := env.TransactionContext().Expiration
return []interface{}{output}
}},
{"native_txGasPayer", func(env *xenv.Environment) []interface{} {
output := env.TransactionContext().GasPayer
return []interface{}{output}
}},
}
abi := Extension.V2.NativeABI()
for _, def := range defines {
if method, found := abi.MethodByName(def.name); found {
nativeMethods[methodKey{Extension.Address, method.ID()}] = &nativeMethod{
abi: method,
run: def.run,
}
} else {
panic("method not found: " + def.name)
}
}
}