/
smallIntOps.go
291 lines (235 loc) · 9.28 KB
/
smallIntOps.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
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
package elrondapi
// // Declare the function signatures (see [cgo](https://golang.org/cmd/cgo/)).
//
// #include <stdlib.h>
// typedef unsigned char uint8_t;
// typedef int int32_t;
//
// extern long long smallIntGetUnsignedArgument(void *context, int32_t id);
// extern long long smallIntGetSignedArgument(void *context, int32_t id);
//
// extern void smallIntFinishUnsigned(void* context, long long value);
// extern void smallIntFinishSigned(void* context, long long value);
//
// extern int32_t smallIntStorageStoreUnsigned(void *context, int32_t keyOffset, int32_t keyLength, long long value);
// extern int32_t smallIntStorageStoreSigned(void *context, int32_t keyOffset, int32_t keyLength, long long value);
// extern long long smallIntStorageLoadUnsigned(void *context, int32_t keyOffset, int32_t keyLength);
// extern long long smallIntStorageLoadSigned(void *context, int32_t keyOffset, int32_t keyLength);
//
// extern long long int64getArgument(void *context, int32_t id);
// extern int32_t int64storageStore(void *context, int32_t keyOffset, int32_t keyLength , long long value);
// extern long long int64storageLoad(void *context, int32_t keyOffset, int32_t keyLength );
// extern void int64finish(void* context, long long value);
//
import "C"
import (
"math/big"
"unsafe"
"github.com/ElrondNetwork/arwen-wasm-vm/arwen"
"github.com/ElrondNetwork/arwen-wasm-vm/wasmer"
twos "github.com/ElrondNetwork/big-int-util/twos-complement"
)
// SmallIntImports creates a new wasmer.Imports populated with the small int (int64/uint64) API methods
func SmallIntImports(imports *wasmer.Imports) (*wasmer.Imports, error) {
imports = imports.Namespace("env")
imports, err := imports.Append("smallIntGetUnsignedArgument", smallIntGetUnsignedArgument, C.smallIntGetUnsignedArgument)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntGetSignedArgument", smallIntGetSignedArgument, C.smallIntGetSignedArgument)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntFinishUnsigned", smallIntFinishUnsigned, C.smallIntFinishUnsigned)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntFinishSigned", smallIntFinishSigned, C.smallIntFinishSigned)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntStorageStoreUnsigned", smallIntStorageStoreUnsigned, C.smallIntStorageStoreUnsigned)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntStorageStoreSigned", smallIntStorageStoreSigned, C.smallIntStorageStoreSigned)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntStorageLoadUnsigned", smallIntStorageLoadUnsigned, C.smallIntStorageLoadUnsigned)
if err != nil {
return nil, err
}
imports, err = imports.Append("smallIntStorageLoadSigned", smallIntStorageLoadSigned, C.smallIntStorageLoadSigned)
if err != nil {
return nil, err
}
// the last are just for backwards compatibility:
imports, err = imports.Append("int64getArgument", int64getArgument, C.int64getArgument)
if err != nil {
return nil, err
}
imports, err = imports.Append("int64storageStore", int64storageStore, C.int64storageStore)
if err != nil {
return nil, err
}
imports, err = imports.Append("int64storageLoad", int64storageLoad, C.int64storageLoad)
if err != nil {
return nil, err
}
imports, err = imports.Append("int64finish", int64finish, C.int64finish)
if err != nil {
return nil, err
}
return imports, nil
}
//export smallIntGetUnsignedArgument
func smallIntGetUnsignedArgument(context unsafe.Pointer, id int32) int64 {
runtime := arwen.GetRuntimeContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64GetArgument
metering.UseGas(gasToUse)
args := runtime.Arguments()
if id < 0 || id >= int32(len(args)) {
arwen.WithFault(arwen.ErrArgIndexOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
arg := args[id]
argBigInt := big.NewInt(0).SetBytes(arg)
if !argBigInt.IsUint64() {
arwen.WithFault(arwen.ErrArgOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
return int64(argBigInt.Uint64())
}
//export smallIntGetSignedArgument
func smallIntGetSignedArgument(context unsafe.Pointer, id int32) int64 {
runtime := arwen.GetRuntimeContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64GetArgument
metering.UseGas(gasToUse)
args := runtime.Arguments()
if id < 0 || id >= int32(len(args)) {
arwen.WithFault(arwen.ErrArgIndexOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
arg := args[id]
argBigInt := twos.SetBytes(big.NewInt(0), arg)
if !argBigInt.IsInt64() {
arwen.WithFault(arwen.ErrArgOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
return argBigInt.Int64()
}
//export smallIntFinishUnsigned
func smallIntFinishUnsigned(context unsafe.Pointer, value int64) {
output := arwen.GetOutputContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64Finish
metering.UseGas(gasToUse)
valueBytes := big.NewInt(0).SetUint64(uint64(value)).Bytes()
output.Finish(valueBytes)
}
//export smallIntFinishSigned
func smallIntFinishSigned(context unsafe.Pointer, value int64) {
output := arwen.GetOutputContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64Finish
metering.UseGas(gasToUse)
valueBytes := twos.ToBytes(big.NewInt(value))
output.Finish(valueBytes)
}
//export smallIntStorageStoreUnsigned
func smallIntStorageStoreUnsigned(context unsafe.Pointer, keyOffset int32, keyLength int32, value int64) int32 {
runtime := arwen.GetRuntimeContext(context)
storage := arwen.GetStorageContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64StorageStore
metering.UseGas(gasToUse)
key, err := runtime.MemLoad(keyOffset, keyLength)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return -1
}
valueBytes := big.NewInt(0).SetUint64(uint64(value)).Bytes()
storageStatus, err := storage.SetStorage(key, valueBytes)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return -1
}
return int32(storageStatus)
}
//export smallIntStorageStoreSigned
func smallIntStorageStoreSigned(context unsafe.Pointer, keyOffset int32, keyLength int32, value int64) int32 {
runtime := arwen.GetRuntimeContext(context)
storage := arwen.GetStorageContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64StorageStore
metering.UseGas(gasToUse)
key, err := runtime.MemLoad(keyOffset, keyLength)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return -1
}
valueBytes := twos.ToBytes(big.NewInt(value))
storageStatus, err := storage.SetStorage(key, valueBytes)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return -1
}
return int32(storageStatus)
}
//export smallIntStorageLoadUnsigned
func smallIntStorageLoadUnsigned(context unsafe.Pointer, keyOffset int32, keyLength int32) int64 {
runtime := arwen.GetRuntimeContext(context)
storage := arwen.GetStorageContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64StorageLoad
metering.UseGas(gasToUse)
key, err := runtime.MemLoad(keyOffset, keyLength)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return 0
}
data := storage.GetStorage(key)
valueBigInt := big.NewInt(0).SetBytes(data)
if !valueBigInt.IsUint64() {
arwen.WithFault(arwen.ErrStorageValueOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
return int64(valueBigInt.Uint64())
}
//export smallIntStorageLoadSigned
func smallIntStorageLoadSigned(context unsafe.Pointer, keyOffset int32, keyLength int32) int64 {
runtime := arwen.GetRuntimeContext(context)
storage := arwen.GetStorageContext(context)
metering := arwen.GetMeteringContext(context)
gasToUse := metering.GasSchedule().ElrondAPICost.Int64StorageLoad
metering.UseGas(gasToUse)
key, err := runtime.MemLoad(keyOffset, keyLength)
if arwen.WithFault(err, context, runtime.ElrondAPIErrorShouldFailExecution()) {
return 0
}
data := storage.GetStorage(key)
valueBigInt := twos.SetBytes(big.NewInt(0), data)
if !valueBigInt.IsInt64() {
arwen.WithFault(arwen.ErrStorageValueOutOfRange, context, runtime.ElrondAPIErrorShouldFailExecution())
return 0
}
return valueBigInt.Int64()
}
//export int64getArgument
func int64getArgument(context unsafe.Pointer, id int32) int64 {
// backwards compatibility
return smallIntGetSignedArgument(context, id)
}
//export int64finish
func int64finish(context unsafe.Pointer, value int64) {
// backwards compatibility
smallIntFinishSigned(context, value)
}
//export int64storageStore
func int64storageStore(context unsafe.Pointer, keyOffset int32, keyLength int32, value int64) int32 {
// backwards compatibility
return smallIntStorageStoreUnsigned(context, keyOffset, keyLength, value)
}
//export int64storageLoad
func int64storageLoad(context unsafe.Pointer, keyOffset int32, keyLength int32) int64 {
// backwards compatibility
return smallIntStorageLoadUnsigned(context, keyOffset, keyLength)
}