-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavm.go
428 lines (386 loc) · 14.4 KB
/
wavm.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2019 The go-vnt Authors
// This file is part of the go-vnt library.
//
// The go-vnt library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-vnt library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>.
package wavm
import (
"bytes"
"encoding/json"
"math/big"
"sync/atomic"
"time"
"github.com/vntchain/go-vnt/common"
"github.com/vntchain/go-vnt/core/state"
"github.com/vntchain/go-vnt/core/vm"
errorsmsg "github.com/vntchain/go-vnt/core/vm"
inter "github.com/vntchain/go-vnt/core/vm/interface"
wasmcontract "github.com/vntchain/go-vnt/core/wavm/contract"
"github.com/vntchain/go-vnt/core/wavm/gas"
"github.com/vntchain/go-vnt/core/wavm/storage"
"github.com/vntchain/go-vnt/core/wavm/utils"
"github.com/vntchain/go-vnt/crypto"
"github.com/vntchain/go-vnt/params"
"github.com/vntchain/vnt-wasm/vnt"
)
var emptyCodeHash = crypto.Keccak256Hash(nil)
var electionAddress = common.BytesToAddress([]byte{9})
type WAVM struct {
// Context provides auxiliary blockchain related information
vm.Context
// StateDB gives access to the underlying state
StateDB inter.StateDB
// Depth is the current call stack
depth int
// Mutable is the current call mutable state
// -1:init state,0:unmutable,1:mutable
mutable int
// chainConfig contains information about the current chain
chainConfig *params.ChainConfig
// chain rules contains the chain rules for the current epoch
chainRules params.Rules
// virtual machine configuration options used to initialise the
// wavm.
wavmConfig Config
// global (to this context) vntchain virtual machine
// used throughout the execution of the tx.
abort int32
// callGasTemp holds the gas available for the current call. This is needed because the
// available gas is calculated in gasCall* according to the 63/64 rule and later
// applied in opCall*.
callGasTemp uint64
Wavm *Wavm
}
func (wavm *WAVM) GetCallGasTemp() uint64 {
return wavm.callGasTemp
}
func (wavm *WAVM) SetCallGasTemp(gas uint64) {
wavm.callGasTemp = gas
}
func (wavm *WAVM) GetChainConfig() *params.ChainConfig {
return wavm.chainConfig
}
// run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
func runWavm(wavm *WAVM, contract *wasmcontract.WASMContract, input []byte, isCreate bool) ([]byte, error) {
if contract.CodeAddr != nil {
precompiles := vm.PrecompiledContractsHubble
if p := precompiles[*contract.CodeAddr]; p != nil {
return vm.RunPrecompiledContract(wavm, p, input, contract)
}
}
if len(contract.Code) == 0 {
return nil, nil
}
var code wasmcontract.WasmCode
decode, vmInput, err := utils.DecodeContractCode(contract.Code)
if err != nil {
return nil, err
}
code = decode
if isCreate == true {
input = vmInput
}
abi, err := GetAbi(code.Abi)
if err != nil {
return nil, err
}
gasRule := gas.NewGas(wavm.wavmConfig.DisableFloatingPoint)
gasTable := wavm.ChainConfig().GasTable(wavm.Context.BlockNumber)
gasCounter := gas.NewGasCounter(contract, gasTable)
crx := ChainContext{
CanTransfer: wavm.Context.CanTransfer,
Transfer: wavm.Context.Transfer,
GetHash: wavm.Context.GetHash,
// Message information
Origin: wavm.Context.Origin,
GasPrice: wavm.Context.GasPrice,
// Block information
Coinbase: wavm.Context.Coinbase,
GasLimit: wavm.Context.GasLimit,
BlockNumber: wavm.Context.BlockNumber,
Time: wavm.Context.Time,
Difficulty: wavm.Context.Difficulty,
Contract: contract,
StateDB: wavm.StateDB.(*state.StateDB),
Code: code.Code,
Abi: abi,
Wavm: wavm,
IsCreated: isCreate,
GasRule: gasRule,
GasCounter: gasCounter,
GasTable: gasTable,
StorageMapping: make(map[uint64]storage.StorageMapping),
}
newwawm := NewWavm(crx, wavm.wavmConfig, isCreate)
wavm.Wavm = newwawm
err = newwawm.InstantiateModule(code.Code, []uint8{})
if err != nil {
return nil, err
}
mutable := MutableFunction(abi, newwawm.Module)
var res []byte
if isCreate == true {
// compile the wasm code: add gas counter, add statedb r/w
compiled, err := CompileModule(newwawm.Module, crx, mutable)
if err != nil {
return nil, err
}
res, err = newwawm.Apply(input, compiled, mutable)
if err != nil {
return nil, err
}
compileres, err := json.Marshal(compiled)
if err != nil {
return nil, err
}
code.Compiled = compileres
res = utils.CompressWasmAndAbi(code.Abi, code.Code, code.Compiled)
} else {
var compiled []vnt.Compiled
err = json.Unmarshal(code.Compiled, &compiled)
if err != nil {
return nil, err
}
res, err = newwawm.Apply(input, compiled, mutable)
if err != nil {
return nil, err
}
}
return res, err
}
func NewWAVM(ctx vm.Context, statedb inter.StateDB, chainConfig *params.ChainConfig, vmConfig vm.Config) *WAVM {
wavmConfig := Config{
Debug: vmConfig.Debug,
Tracer: vmConfig.Tracer,
NoRecursion: vmConfig.NoRecursion,
}
wavm := &WAVM{
Context: ctx,
StateDB: statedb,
wavmConfig: wavmConfig,
chainConfig: chainConfig,
chainRules: chainConfig.Rules(ctx.BlockNumber),
mutable: -1,
}
return wavm
}
func (wavm *WAVM) Cancel() {
atomic.StoreInt32(&wavm.abort, 1)
}
func (wavm *WAVM) Create(caller vm.ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
// Depth check execution. Fail if we're trying to execute above the
// limit.
if wavm.depth > int(params.CallCreateDepth) {
return nil, common.Address{}, gas, errorsmsg.ErrDepth
}
if !wavm.CanTransfer(wavm.StateDB, caller.Address(), value) {
return nil, common.Address{}, gas, errorsmsg.ErrInsufficientBalance
}
// Ensure there's no existing contract already at the designated address
nonce := wavm.StateDB.GetNonce(caller.Address())
wavm.StateDB.SetNonce(caller.Address(), nonce+1)
contractAddr = crypto.CreateAddress(caller.Address(), nonce)
contractHash := wavm.StateDB.GetCodeHash(contractAddr)
if wavm.StateDB.GetNonce(contractAddr) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
return nil, common.Address{}, 0, errorsmsg.ErrContractAddressCollision
}
// Create a new account on the state
snapshot := wavm.StateDB.Snapshot()
wavm.StateDB.CreateAccount(contractAddr)
wavm.StateDB.SetNonce(contractAddr, 1)
wavm.Transfer(wavm.StateDB, caller.Address(), contractAddr, value)
// initialise a new contract and set the code that is to be used by the
// wavm. The contract is a scoped environment for this execution context
// only.
contract := wasmcontract.NewWASMContract(caller, vm.AccountRef(contractAddr), value, gas)
contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
if wavm.wavmConfig.NoRecursion && wavm.depth > 0 {
return nil, contractAddr, gas, nil
}
if wavm.wavmConfig.Debug && wavm.depth == 0 {
wavm.wavmConfig.Tracer.CaptureStart(caller.Address(), contractAddr, true, code, gas, value)
}
start := time.Now()
ret, err = runWavm(wavm, contract, nil, true)
// check whether the max code size has been exceeded
maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
// if the contract creation ran successfully and no errors were returned
// calculate the gas required to store the code. If the code could not
// be stored due to not enough gas set an error and let it be handled
// by the error checking condition below.
if err == nil && !maxCodeSizeExceeded {
createDataGas := uint64(len(ret)) * params.CreateDataGas / 2
if contract.UseGas(createDataGas) {
wavm.StateDB.SetCode(contractAddr, ret)
} else {
err = errorsmsg.ErrCodeStoreOutOfGas
}
}
// When an error was returned by the wavm or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// also counts for code storage gas errors.
if maxCodeSizeExceeded || err != nil {
wavm.StateDB.RevertToSnapshot(snapshot)
if err.Error() != errorsmsg.ErrExecutionReverted.Error() {
contract.UseGas(contract.Gas)
}
}
// Assign err if contract code size exceeds the max while the err is still empty.
if maxCodeSizeExceeded && err == nil {
err = errorsmsg.ErrMaxCodeSizeExceeded
}
if wavm.wavmConfig.Debug && wavm.depth == 0 {
wavm.wavmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
}
return ret, contractAddr, contract.Gas, err
}
// Call executes the contract associated with the addr with the given input as
// parameters. It also handles any necessary value transfer required and takes
// the necessary steps to create accounts and reverses the state in case of an
// execution error or failed value transfer.
func (wavm *WAVM) Call(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
if wavm.wavmConfig.NoRecursion && wavm.depth > 0 {
return nil, gas, nil
}
// Fail if we're trying to execute above the call depth limit
if wavm.depth > int(params.CallCreateDepth) {
return nil, gas, errorsmsg.ErrDepth
}
// Fail if we're trying to transfer more than the available balance
if !wavm.Context.CanTransfer(wavm.StateDB, caller.Address(), value) {
return nil, gas, errorsmsg.ErrInsufficientBalance
}
var (
to = vm.AccountRef(addr)
snapshot = wavm.StateDB.Snapshot()
)
if !wavm.StateDB.Exist(addr) {
precompiles := vm.PrecompiledContractsHubble
if precompiles[addr] == nil && value.Sign() == 0 {
// Calling a non existing account, don't do antything, but ping the tracer
if wavm.wavmConfig.Debug && wavm.depth == 0 {
wavm.wavmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
wavm.wavmConfig.Tracer.CaptureEnd(ret, 0, 0, nil)
}
return nil, gas, nil
}
wavm.StateDB.CreateAccount(addr)
}
wavm.Transfer(wavm.StateDB, caller.Address(), to.Address(), value)
// Initialise a new contract and set the code that is to be used by the WAVM.
// The contract is a scoped environment for this execution context only.
contract := wasmcontract.NewWASMContract(caller, to, value, gas)
code := wavm.StateDB.GetCode(addr)
contract.SetCallCode(&addr, wavm.StateDB.GetCodeHash(addr), code)
start := time.Now()
// Capture the tracer start/end events in debug mode
if wavm.wavmConfig.Debug && wavm.depth == 0 {
wavm.wavmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
defer func() { // Lazy evaluation of the parameters
wavm.wavmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
}()
}
ret, err = runWavm(wavm, contract, input, false)
// When an error was returned by the WAVM or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// this also counts for code storage gas errors.
if err != nil {
wavm.StateDB.RevertToSnapshot(snapshot)
if err.Error() != errorsmsg.ErrExecutionReverted.Error() && !bytes.Equal(to.Address().Bytes(), electionAddress.Bytes()) {
contract.UseGas(contract.Gas)
}
}
return ret, contract.Gas, err
}
// CallCode executes the contract associated with the addr with the given input
// as parameters. It also handles any necessary value transfer required and takes
// the necessary steps to create accounts and reverses the state in case of an
// execution error or failed value transfer.
//
// CallCode differs from Call in the sense that it executes the given address'
// code with the caller as context.
func (wavm *WAVM) CallCode(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
if wavm.wavmConfig.NoRecursion && wavm.depth > 0 {
return nil, gas, nil
}
// Fail if we're trying to execute above the call depth limit
if wavm.depth > int(params.CallCreateDepth) {
return nil, gas, errorsmsg.ErrDepth
}
// Fail if we're trying to transfer more than the available balance
if !wavm.CanTransfer(wavm.StateDB, caller.Address(), value) {
return nil, gas, errorsmsg.ErrInsufficientBalance
}
var (
snapshot = wavm.StateDB.Snapshot()
to = vm.AccountRef(caller.Address())
)
// initialise a new contract and set the code that is to be used by the
// WAVM. The contract is a scoped environment for this execution context
// only.
contract := wasmcontract.NewWASMContract(caller, to, value, gas)
code := wavm.StateDB.GetCode(addr)
contract.SetCallCode(&addr, wavm.StateDB.GetCodeHash(addr), code)
ret, err = runWavm(wavm, contract, input, false)
if err != nil {
wavm.StateDB.RevertToSnapshot(snapshot)
// if err != errExecutionReverted {
// contract.UseGas(contract.Gas)
// }
}
return ret, contract.Gas, err
}
func (wavm *WAVM) DelegateCall(caller vm.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
if wavm.wavmConfig.NoRecursion && wavm.depth > 0 {
return nil, gas, nil
}
// Fail if we're trying to execute above the call depth limit
if wavm.depth > int(params.CallCreateDepth) {
return nil, gas, errorsmsg.ErrDepth
}
var (
snapshot = wavm.StateDB.Snapshot()
to = vm.AccountRef(caller.Address())
)
// Initialise a new contract and make initialise the delegate values
ctr := wasmcontract.NewWASMContract(caller, to, nil, gas).AsDelegate()
contract := ctr.(*wasmcontract.WASMContract)
code := wavm.StateDB.GetCode(addr)
contract.SetCallCode(&addr, wavm.StateDB.GetCodeHash(addr), code)
ret, err = runWavm(wavm, contract, input, false)
if err != nil {
wavm.StateDB.RevertToSnapshot(snapshot)
// if err != errExecutionReverted {
// contract.UseGas(contract.Gas)
// }
}
return ret, contract.Gas, err
}
func (wavm *WAVM) StaticCall(caller vm.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
return nil, 1, nil
}
func (wavm *WAVM) GetStateDb() inter.StateDB {
return wavm.StateDB
}
func (wavm *WAVM) ChainConfig() *params.ChainConfig {
return wavm.chainConfig
}
func (wavm *WAVM) GetContext() vm.Context {
return wavm.Context
}
func (wavm *WAVM) GetOrigin() common.Address {
return wavm.Origin
}
func (wavm *WAVM) GetTime() *big.Int {
return wavm.Time
}