-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
307 lines (251 loc) · 7.53 KB
/
wrapper.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package wasmtime
import (
"fmt"
"reflect"
"time"
wasmtime "github.com/bytecodealliance/wasmtime-go/v20"
"github.com/pkg/errors"
types "github.com/artela-network/aspect-runtime/types"
)
func Wrap(api *types.HostAPIRegistry, module types.Module, ns types.NameSpace, method types.MethodName,
hostFunc *types.HostFuncWithGasRule) (interface{}, error) {
errNotSupport := errors.New("host function not supported")
fn := hostFunc.Func
gasRule := hostFunc.GasRule
hostCtx := hostFunc.HostContext
t := reflect.TypeOf(fn)
if t.NumOut() > 2 || t.NumOut() == 0 {
return nil, errNotSupport
}
if t.NumOut() == 1 {
switch t.NumIn() {
case 0:
return func() *wasmtime.Trap {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
_, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn)
return trap
}, nil
case 1:
return func(arg int32) *wasmtime.Trap {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
_, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg)
return trap
}, nil
case 2:
return func(arg1 int32, arg2 int32) *wasmtime.Trap {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
_, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg1, arg2)
return trap
}, nil
case 3:
return func(arg1 int32, arg2 int32, arg3 int32) *wasmtime.Trap {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
_, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg1, arg2, arg3)
return trap
}, nil
}
} else if t.NumOut() == 2 {
switch t.NumIn() {
case 0:
return func() (int32, *wasmtime.Trap) {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
out, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn)
if trap != nil {
return 0, trap
}
return out[0], nil
}, nil
case 1:
return func(arg int32) (int32, *wasmtime.Trap) {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
out, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg)
if trap != nil {
return 0, trap
}
return out[0], nil
}, nil
case 2:
return func(arg1 int32, arg2 int32) (int32, *wasmtime.Trap) {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
out, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg1, arg2)
if trap != nil {
return 0, trap
}
return out[0], nil
}, nil
case 3:
return func(arg1 int32, arg2 int32, arg3 int32) (int32, *wasmtime.Trap) {
startTime := time.Now()
defer func() {
api.Context().Logger().Debug("host func done",
"duration", time.Since(startTime).String(),
"module", module,
"namespace", ns,
"method", method)
}()
out, trap := executeWrapper(api.Context(), hostCtx, gasRule, fn, arg1, arg2, arg3)
if trap != nil {
return 0, trap
}
return out[0], nil
}, nil
}
}
return nil, errNotSupport
}
func executeWrapper(vmCtx types.VMContext, hostCtx types.HostContext, gasRule types.HostFuncGasRule, fn interface{}, ptrs ...int32) ([]int32, *wasmtime.Trap) {
gasRule.SetContext(vmCtx)
args, paramSize, err := paramsRead(vmCtx, ptrs...)
if paramSize > 0 {
if err := gasRule.ConsumeGas(paramSize); err != nil {
return nil, wasmtime.NewTrap(err.Error())
}
}
if err != nil {
return nil, wasmtime.NewTrap(fmt.Sprintf("read params failed"))
}
v := reflect.ValueOf(fn)
// need to sync the gas in vm to host
remaining, err := vmCtx.RemainingWASMGas()
if err != nil {
return nil, wasmtime.NewTrap(fmt.Sprintf("read gas failed, %v", err))
}
// NOTE: during the host call, the gas consumed is in EVM metric, so it will be 1000x than WASM metric,
// e.g. if the gas in WASM remaining is 11111, when put to EVM is will be 10,
// after the host call, the gas put back to WASM should be (10 - EVM Gas Cost) * 1000 + 1111
remainderGas := remaining % types.EVMGasToWASMGasMultiplier
hostCtx.SetGas(uint64(remaining) / types.EVMGasToWASMGasMultiplier)
res := v.Call(args)
// after the host call we need to sync the gas in host back to vm
remaining = int64(hostCtx.RemainingGas())
if err := vmCtx.SetWASMGas(remaining*types.EVMGasToWASMGasMultiplier + remainderGas); err != nil {
return nil, nil
}
outPtrs, err := paramListWrite(vmCtx, res)
if err != nil && err.Error() == types.OutOfGasError.Error() {
return nil, wasmtime.NewTrap(types.OutOfGasError.Error())
}
if err != nil {
return nil, wasmtime.NewTrap(fmt.Sprintf("write params failed, %v", err))
}
return outPtrs, nil
}
func paramsRead(ctx types.VMContext, ptrs ...int32) ([]reflect.Value, int64, error) {
args := make([]reflect.Value, len(ptrs))
paramSize := int64(0)
if len(args) == 0 {
return args, paramSize, nil
}
for i, ptr := range ptrs {
h := &types.TypeHeader{}
header, err := ctx.ReadMemory(ptr, types.HeaderLen)
if err != nil {
return nil, paramSize, err
}
dataType, dataLen, err := h.Unmarshal(header)
if err != nil {
return nil, paramSize, err
}
paramSize += int64(dataLen)
reqType, err := types.TypeObjectMapping(dataType)
if err != nil {
return nil, paramSize, err
}
reqData, err := ctx.ReadMemory(ptr, types.HeaderLen+dataLen)
if err != nil {
return nil, paramSize, err
}
value, err := reqType.Unmarshal(reqData)
if err != nil {
return nil, paramSize, err
}
args[i] = reflect.ValueOf(value)
}
return args, paramSize, nil
}
func storeValue(ctx types.VMContext, value reflect.Value) (int32, error) {
retIndex := types.AssertType(value.Interface())
resType, err := types.TypeObjectMapping(retIndex)
if err != nil {
return 0, err
}
data := resType.Marshal(value.Interface())
ptr, err := ctx.AllocMemory(int32(len(data)))
if err != nil {
return 0, err
}
if err := ctx.WriteMemory(ptr, data); err != nil {
return 0, err
}
return ptr, nil
}
func paramListWrite(ctx types.VMContext, values []reflect.Value) ([]int32, error) {
if len(values) == 0 {
return nil, nil
}
lastRetVal := values[len(values)-1].Interface()
if _, ok := lastRetVal.(error); !ok && lastRetVal != nil {
return nil, errors.New("invalid host func, last return value must be error")
} else if ok && lastRetVal != nil {
return nil, lastRetVal.(error)
}
valuesLen := len(values) - 1
int32Ary := make([]int32, valuesLen)
for i, value := range values[:valuesLen] {
i2, err := storeValue(ctx, value)
if err != nil {
return nil, err
}
int32Ary[i] = i2
}
return int32Ary, nil
}