-
Notifications
You must be signed in to change notification settings - Fork 248
/
runtime.go
484 lines (428 loc) · 13.8 KB
/
runtime.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2018 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 runtime
import (
"math/big"
"sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/pkg/errors"
"github.com/vechain/thor/abi"
"github.com/vechain/thor/builtin"
"github.com/vechain/thor/chain"
"github.com/vechain/thor/runtime/statedb"
"github.com/vechain/thor/state"
"github.com/vechain/thor/thor"
"github.com/vechain/thor/tx"
Tx "github.com/vechain/thor/tx"
"github.com/vechain/thor/vm"
"github.com/vechain/thor/xenv"
)
var (
energyTransferEvent *abi.Event
prototypeSetMasterEvent *abi.Event
nativeCallReturnGas uint64 = 1562 // see test case for calculation
EmptyRuntimeBytecode = []byte{0x60, 0x60, 0x60, 0x40, 0x52, 0x60, 0x02, 0x56}
)
func init() {
var found bool
if energyTransferEvent, found = builtin.Energy.ABI.EventByName("Transfer"); !found {
panic("transfer event not found")
}
if prototypeSetMasterEvent, found = builtin.Prototype.Events().EventByName("$Master"); !found {
panic("$Master event not found")
}
}
var baseChainConfig = vm.ChainConfig{
ChainConfig: params.ChainConfig{
ChainID: big.NewInt(0),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: big.NewInt(0),
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP150Hash: common.Hash{},
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: nil,
Ethash: nil,
Clique: nil,
},
IstanbulBlock: nil,
}
// Output output of clause execution.
type Output struct {
Data []byte
Events tx.Events
Transfers tx.Transfers
LeftOverGas uint64
RefundGas uint64
VMErr error // VMErr identify the execution result of the contract function, not evm function's err.
ContractAddress *thor.Address // if create a new contract, or is nil.
}
type TransactionExecutor struct {
HasNextClause func() bool
NextClause func() (gasUsed uint64, output *Output, err error)
Finalize func() (*tx.Receipt, error)
}
// Runtime bases on EVM and VeChain Thor builtins.
type Runtime struct {
vmConfig vm.Config
chain *chain.Chain
state *state.State
ctx *xenv.BlockContext
chainConfig vm.ChainConfig
}
// New create a Runtime object.
func New(
chain *chain.Chain,
state *state.State,
ctx *xenv.BlockContext,
forkConfig thor.ForkConfig,
) *Runtime {
currentChainConfig := baseChainConfig
currentChainConfig.ConstantinopleBlock = big.NewInt(int64(forkConfig.ETH_CONST))
currentChainConfig.IstanbulBlock = big.NewInt(int64(forkConfig.ETH_IST))
if chain != nil {
// use genesis id as chain id
currentChainConfig.ChainID = new(big.Int).SetBytes(chain.GenesisID().Bytes())
}
// alloc precompiled contracts
if forkConfig.ETH_IST == ctx.Number {
for addr := range vm.PrecompiledContractsIstanbul {
if err := state.SetCode(thor.Address(addr), EmptyRuntimeBytecode); err != nil {
panic(err)
}
}
} else if ctx.Number == 0 {
for addr := range vm.PrecompiledContractsByzantium {
if err := state.SetCode(thor.Address(addr), EmptyRuntimeBytecode); err != nil {
panic(err)
}
}
}
// VIP191
if forkConfig.VIP191 == ctx.Number {
// upgrade extension contract to V2
if err := state.SetCode(builtin.Extension.Address, builtin.Extension.V2.RuntimeBytecodes()); err != nil {
panic(err)
}
}
rt := Runtime{
chain: chain,
state: state,
ctx: ctx,
chainConfig: currentChainConfig,
}
return &rt
}
func (rt *Runtime) Chain() *chain.Chain { return rt.chain }
func (rt *Runtime) State() *state.State { return rt.state }
func (rt *Runtime) Context() *xenv.BlockContext { return rt.ctx }
// SetVMConfig config VM.
// Returns this runtime.
func (rt *Runtime) SetVMConfig(config vm.Config) *Runtime {
rt.vmConfig = config
return rt
}
func (rt *Runtime) newEVM(stateDB *statedb.StateDB, clauseIndex uint32, txCtx *xenv.TransactionContext) *vm.EVM {
var lastNonNativeCallGas uint64
return vm.NewEVM(vm.Context{
CanTransfer: func(_ vm.StateDB, addr common.Address, amount *big.Int) bool {
return stateDB.GetBalance(addr).Cmp(amount) >= 0
},
Transfer: func(_ vm.StateDB, sender, recipient common.Address, amount *big.Int) {
if amount.Sign() == 0 {
return
}
// touch energy balance when token balance changed
// SHOULD be performed before transfer
senderEnergy, err := rt.state.GetEnergy(thor.Address(sender), rt.ctx.Time)
if err != nil {
panic(err)
}
recipientEnergy, err := rt.state.GetEnergy(thor.Address(recipient), rt.ctx.Time)
if err != nil {
panic(err)
}
if err := rt.state.SetEnergy(thor.Address(sender), senderEnergy, rt.ctx.Time); err != nil {
panic(err)
}
if err := rt.state.SetEnergy(thor.Address(recipient), recipientEnergy, rt.ctx.Time); err != nil {
panic(err)
}
stateDB.SubBalance(common.Address(sender), amount)
stateDB.AddBalance(common.Address(recipient), amount)
stateDB.AddTransfer(&tx.Transfer{
Sender: thor.Address(sender),
Recipient: thor.Address(recipient),
Amount: new(big.Int).Set(amount),
})
},
GetHash: func(num uint64) common.Hash {
id, err := rt.chain.GetBlockID(uint32(num))
if err != nil {
panic(err)
}
return common.Hash(id)
},
NewContractAddress: func(_ *vm.EVM, counter uint32) common.Address {
return common.Address(thor.CreateContractAddress(txCtx.ID, clauseIndex, counter))
},
InterceptContractCall: func(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error, bool) {
if evm.Depth() < 2 {
lastNonNativeCallGas = contract.Gas
// skip direct calls
return nil, nil, false
}
if contract.Address() != contract.Caller() {
lastNonNativeCallGas = contract.Gas
// skip native calls from other contract
return nil, nil, false
}
abi, run, found := builtin.FindNativeCall(thor.Address(contract.Address()), contract.Input)
if !found {
lastNonNativeCallGas = contract.Gas
return nil, nil, false
}
if readonly && !abi.Const() {
panic("invoke non-const method in readonly env")
}
if contract.Value().Sign() != 0 {
// reject value transfer on call
panic("value transfer not allowed")
}
// here we return call gas and extcodeSize gas for native calls, to make
// builtin contract cheap.
contract.Gas += nativeCallReturnGas
if contract.Gas > lastNonNativeCallGas {
panic("serious bug: native call returned gas over consumed")
}
ret, err := xenv.New(abi, rt.chain, rt.state, rt.ctx, txCtx, evm, contract).Call(run)
return ret, err, true
},
OnCreateContract: func(_ *vm.EVM, contractAddr, caller common.Address) {
// set master for created contract
if err := rt.state.SetMaster(thor.Address(contractAddr), thor.Address(caller)); err != nil {
panic(err)
}
data, err := prototypeSetMasterEvent.Encode(caller)
if err != nil {
panic(err)
}
stateDB.AddLog(&types.Log{
Address: common.Address(contractAddr),
Topics: []common.Hash{common.Hash(prototypeSetMasterEvent.ID())},
Data: data,
})
},
OnSuicideContract: func(_ *vm.EVM, contractAddr, tokenReceiver common.Address) {
// it's IMPORTANT to process energy before token
amount, err := rt.state.GetEnergy(thor.Address(contractAddr), rt.ctx.Time)
if err != nil {
panic(err)
}
if amount.Sign() != 0 {
// add remained energy of suiciding contract to receiver.
// no need to clear contract's energy, vm will delete the whole contract later.
receiverEnergy, err := rt.state.GetEnergy(thor.Address(tokenReceiver), rt.ctx.Time)
if err != nil {
panic(err)
}
if err := rt.state.SetEnergy(
thor.Address(tokenReceiver),
new(big.Int).Add(receiverEnergy, amount),
rt.ctx.Time); err != nil {
panic(err)
}
// see ERC20's Transfer event
topics := []common.Hash{
common.Hash(energyTransferEvent.ID()),
common.BytesToHash(contractAddr[:]),
common.BytesToHash(tokenReceiver[:]),
}
data, err := energyTransferEvent.Encode(amount)
if err != nil {
panic(err)
}
stateDB.AddLog(&types.Log{
Address: common.Address(builtin.Energy.Address),
Topics: topics,
Data: data,
})
}
if amount := stateDB.GetBalance(contractAddr); amount.Sign() != 0 {
stateDB.AddBalance(tokenReceiver, amount)
stateDB.AddTransfer(&tx.Transfer{
Sender: thor.Address(contractAddr),
Recipient: thor.Address(tokenReceiver),
Amount: amount,
})
}
},
Origin: common.Address(txCtx.Origin),
GasPrice: txCtx.GasPrice,
Coinbase: common.Address(rt.ctx.Beneficiary),
GasLimit: rt.ctx.GasLimit,
BlockNumber: new(big.Int).SetUint64(uint64(rt.ctx.Number)),
Time: new(big.Int).SetUint64(rt.ctx.Time),
Difficulty: &big.Int{},
}, stateDB, &rt.chainConfig, rt.vmConfig)
}
// PrepareClause prepare to execute clause.
// It allows to interrupt execution.
func (rt *Runtime) PrepareClause(
clause *tx.Clause,
clauseIndex uint32,
gas uint64,
txCtx *xenv.TransactionContext,
) (exec func() (output *Output, interrupted bool, err error), interrupt func()) {
var (
stateDB = statedb.New(rt.state)
evm = rt.newEVM(stateDB, clauseIndex, txCtx)
data []byte
leftOverGas uint64
vmErr error
contractAddr *thor.Address
interruptFlag uint32
)
exec = func() (output *Output, interrupted bool, err error) {
defer func() {
if e := recover(); e != nil {
// caught state error
err = e.(error)
}
}()
if clause.To() == nil {
var caddr common.Address
data, caddr, leftOverGas, vmErr = evm.Create(vm.AccountRef(txCtx.Origin), clause.Data(), gas, clause.Value())
contractAddr = (*thor.Address)(&caddr)
} else {
data, leftOverGas, vmErr = evm.Call(vm.AccountRef(txCtx.Origin), common.Address(*clause.To()), clause.Data(), gas, clause.Value())
}
interrupted = atomic.LoadUint32(&interruptFlag) != 0
output = &Output{
Data: data,
LeftOverGas: leftOverGas,
RefundGas: stateDB.GetRefund(),
VMErr: vmErr,
ContractAddress: contractAddr,
}
output.Events, output.Transfers = stateDB.GetLogs()
return output, interrupted, nil
}
interrupt = func() {
atomic.StoreUint32(&interruptFlag, 1)
evm.Cancel()
}
return
}
// ExecuteTransaction executes a transaction.
// If some clause failed, receipt.Outputs will be nil and vmOutputs may shorter than clause count.
func (rt *Runtime) ExecuteTransaction(tx *tx.Transaction) (receipt *tx.Receipt, err error) {
executor, err := rt.PrepareTransaction(tx)
if err != nil {
return nil, err
}
for executor.HasNextClause() {
if _, _, err := executor.NextClause(); err != nil {
return nil, err
}
}
return executor.Finalize()
}
// PrepareTransaction prepare to execute tx.
func (rt *Runtime) PrepareTransaction(tx *tx.Transaction) (*TransactionExecutor, error) {
resolvedTx, err := ResolveTransaction(tx)
if err != nil {
return nil, err
}
baseGasPrice, gasPrice, payer, returnGas, err := resolvedTx.BuyGas(rt.state, rt.ctx.Time)
if err != nil {
return nil, err
}
txCtx, err := resolvedTx.ToContext(gasPrice, payer, rt.ctx.Number, rt.chain.GetBlockID)
if err != nil {
return nil, err
}
// ResolveTransaction has checked that tx.Gas() >= IntrinsicGas
leftOverGas := tx.Gas() - resolvedTx.IntrinsicGas
// checkpoint to be reverted when clause failure.
checkpoint := rt.state.NewCheckpoint()
txOutputs := make([]*Tx.Output, 0, len(resolvedTx.Clauses))
reverted := false
finalized := false
hasNext := func() bool {
return !reverted && len(txOutputs) < len(resolvedTx.Clauses)
}
return &TransactionExecutor{
HasNextClause: hasNext,
NextClause: func() (gasUsed uint64, output *Output, err error) {
nextClauseIndex := uint32(len(txOutputs))
exec, _ := rt.PrepareClause(resolvedTx.Clauses[nextClauseIndex], nextClauseIndex, leftOverGas, txCtx)
output, _, err = exec()
if err != nil {
return 0, nil, err
}
gasUsed = leftOverGas - output.LeftOverGas
leftOverGas = output.LeftOverGas
// Apply refund counter, capped to half of the used gas.
refund := gasUsed / 2
if refund > output.RefundGas {
refund = output.RefundGas
}
// won't overflow
leftOverGas += refund
if output.VMErr != nil {
// vm exception here
// revert all executed clauses
rt.state.RevertTo(checkpoint)
reverted = true
txOutputs = nil
return
}
txOutputs = append(txOutputs, &Tx.Output{Events: output.Events, Transfers: output.Transfers})
return
},
Finalize: func() (*Tx.Receipt, error) {
if hasNext() {
return nil, errors.New("not all clauses processed")
}
if finalized {
return nil, errors.New("already finalized")
}
finalized = true
receipt := &Tx.Receipt{
Reverted: reverted,
Outputs: txOutputs,
GasUsed: tx.Gas() - leftOverGas,
GasPayer: payer,
}
receipt.Paid = new(big.Int).Mul(new(big.Int).SetUint64(receipt.GasUsed), gasPrice)
if err := returnGas(leftOverGas); err != nil {
return nil, err
}
// reward
rewardRatio, err := builtin.Params.Native(rt.state).Get(thor.KeyRewardRatio)
if err != nil {
return nil, err
}
provedWork, err := tx.ProvedWork(rt.ctx.Number-1, rt.chain.GetBlockID)
if err != nil {
return nil, err
}
overallGasPrice := tx.OverallGasPrice(baseGasPrice, provedWork)
reward := new(big.Int).SetUint64(receipt.GasUsed)
reward.Mul(reward, overallGasPrice)
reward.Mul(reward, rewardRatio)
reward.Div(reward, big.NewInt(1e18))
if err := builtin.Energy.Native(rt.state, rt.ctx.Time).Add(rt.ctx.Beneficiary, reward); err != nil {
return nil, err
}
receipt.Reward = reward
return receipt, nil
},
}, nil
}