2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
2021-04-22 18:12:16 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2021-04-26 12:03:20 +00:00
|
|
|
# This source must have the <vm2_enabled> compiler flag set.
|
|
|
|
#
|
|
|
|
# why:
|
|
|
|
# ../config, ../transaction, etc include ../vm_* interface files which in
|
|
|
|
# turn will refer to the ../vm/* definitions rather than ../vm2/* unless the
|
|
|
|
# <vm2_enabled> compiler flag is set.
|
|
|
|
#
|
|
|
|
when not defined(vm2_enabled):
|
|
|
|
{.error: "NIM flag must be set: -d:vm2_enabled".}
|
2021-04-08 15:13:27 +00:00
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
import
|
2021-04-26 12:03:20 +00:00
|
|
|
../config,
|
|
|
|
../constants,
|
2021-06-01 11:27:05 +00:00
|
|
|
../forks,
|
2021-04-26 12:03:20 +00:00
|
|
|
../db/accounts_cache,
|
2021-04-08 14:52:10 +00:00
|
|
|
../transaction,
|
2021-04-26 12:03:20 +00:00
|
|
|
./computation,
|
2021-04-26 16:00:46 +00:00
|
|
|
./interpreter_dispatch,
|
2021-06-01 11:27:05 +00:00
|
|
|
./interpreter/[gas_costs, gas_meter],
|
2021-04-26 14:08:17 +00:00
|
|
|
./message,
|
2021-04-26 12:03:20 +00:00
|
|
|
./state,
|
|
|
|
./types,
|
2021-04-22 18:12:16 +00:00
|
|
|
chronicles,
|
2021-04-26 12:03:20 +00:00
|
|
|
eth/common,
|
2021-04-22 18:12:16 +00:00
|
|
|
eth/common/eth_types,
|
|
|
|
options,
|
|
|
|
sets
|
|
|
|
|
2021-04-21 10:12:46 +00:00
|
|
|
proc setupTxContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt, forkOverride=none(Fork)) =
|
|
|
|
## this proc will be called each time a new transaction
|
|
|
|
## is going to be executed
|
|
|
|
vmState.txOrigin = origin
|
|
|
|
vmState.txGasPrice = gasPrice
|
|
|
|
vmState.fork =
|
|
|
|
if forkOverride.isSome:
|
|
|
|
forkOverride.get
|
|
|
|
else:
|
|
|
|
vmState.chainDB.config.toFork(vmState.blockHeader.blockNumber)
|
|
|
|
vmState.gasCosts = vmState.fork.forkToSchedule
|
|
|
|
|
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, fork: Fork) : Computation =
|
|
|
|
var gas = tx.gasLimit - tx.intrinsicGas(fork)
|
|
|
|
assert gas >= 0
|
|
|
|
|
|
|
|
vmState.setupTxContext(
|
|
|
|
origin = sender,
|
|
|
|
gasPrice = tx.gasPrice,
|
|
|
|
forkOverride = some(fork)
|
|
|
|
)
|
|
|
|
|
|
|
|
let msg = Message(
|
2021-06-27 04:19:43 +00:00
|
|
|
kind: if tx.contractCreation: evmcCreate else: evmcCall,
|
2021-04-08 14:52:10 +00:00
|
|
|
depth: 0,
|
|
|
|
gas: gas,
|
|
|
|
sender: sender,
|
2021-05-04 06:27:18 +00:00
|
|
|
contractAddress: tx.getRecipient(sender),
|
2021-06-27 04:19:43 +00:00
|
|
|
codeAddress: tx.destination,
|
2021-04-08 14:52:10 +00:00
|
|
|
value: tx.value,
|
|
|
|
data: tx.payload
|
|
|
|
)
|
|
|
|
|
|
|
|
result = newComputation(vmState, msg)
|
|
|
|
doAssert result.isOriginComputation
|
|
|
|
|
2021-04-26 12:03:20 +00:00
|
|
|
|
|
|
|
proc refundGas*(c: Computation, tx: Transaction, sender: EthAddress) =
|
|
|
|
let maxRefund = (tx.gasLimit - c.gasMeter.gasRemaining) div 2
|
|
|
|
c.gasMeter.returnGas min(c.getGasRefund(), maxRefund)
|
|
|
|
c.vmState.mutateStateDB:
|
|
|
|
db.addBalance(sender, c.gasMeter.gasRemaining.u256 * tx.gasPrice.u256)
|
|
|
|
|
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
proc execComputation*(c: Computation) =
|
|
|
|
if not c.msg.isCreate:
|
|
|
|
c.vmState.mutateStateDB:
|
|
|
|
db.incNonce(c.msg.sender)
|
|
|
|
|
|
|
|
c.execCallOrCreate()
|
|
|
|
|
|
|
|
if c.isSuccess:
|
|
|
|
c.refundSelfDestruct()
|
2021-05-11 09:05:58 +00:00
|
|
|
shallowCopy(c.vmState.selfDestructs, c.selfDestructs)
|
2021-04-08 14:52:10 +00:00
|
|
|
shallowCopy(c.vmState.logEntries, c.logEntries)
|
|
|
|
c.vmState.touchedAccounts.incl c.touchedAccounts
|
|
|
|
|
|
|
|
c.vmstate.status = c.isSuccess
|