2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * 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.
|
|
|
|
|
2018-01-17 12:57:50 +00:00
|
|
|
import
|
2020-04-20 18:12:44 +00:00
|
|
|
options, sets,
|
2020-05-30 03:14:59 +00:00
|
|
|
eth/common, chronicles, ./db/accounts_cache,
|
2019-11-13 14:49:39 +00:00
|
|
|
transaction, vm_types, vm_state,
|
|
|
|
./vm/[computation, interpreter]
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2019-08-26 15:19:18 +00:00
|
|
|
proc validateTransaction*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, fork: Fork): bool =
|
2020-05-30 03:14:59 +00:00
|
|
|
let balance = vmState.readOnlyStateDB.getBalance(sender)
|
|
|
|
let nonce = vmState.readOnlyStateDB.getNonce(sender)
|
2018-09-27 19:09:26 +00:00
|
|
|
|
2020-01-24 12:52:55 +00:00
|
|
|
if vmState.cumulativeGasUsed + tx.gasLimit > vmState.blockHeader.gasLimit:
|
|
|
|
debug "invalid tx: block header gasLimit reached",
|
|
|
|
maxLimit=vmState.blockHeader.gasLimit,
|
|
|
|
gasUsed=vmState.cumulativeGasUsed,
|
|
|
|
addition=tx.gasLimit
|
|
|
|
return
|
2018-09-27 19:09:26 +00:00
|
|
|
|
2020-01-30 15:22:38 +00:00
|
|
|
let totalCost = tx.gasLimit.u256 * tx.gasPrice.u256 + tx.value
|
2020-05-30 03:14:59 +00:00
|
|
|
if totalCost > balance:
|
2020-01-24 12:52:55 +00:00
|
|
|
debug "invalid tx: not enough cash",
|
2020-05-30 03:14:59 +00:00
|
|
|
available=balance,
|
2020-01-24 12:52:55 +00:00
|
|
|
require=totalCost
|
|
|
|
return
|
|
|
|
|
|
|
|
if tx.gasLimit < tx.intrinsicGas(fork):
|
|
|
|
debug "invalid tx: not enough gas to perform calculation",
|
|
|
|
available=tx.gasLimit,
|
|
|
|
require=tx.intrinsicGas(fork)
|
|
|
|
return
|
2019-02-27 02:28:02 +00:00
|
|
|
|
2020-05-30 03:14:59 +00:00
|
|
|
if tx.accountNonce != nonce:
|
2020-01-24 12:52:55 +00:00
|
|
|
debug "invalid tx: account nonce mismatch",
|
|
|
|
txNonce=tx.accountnonce,
|
2020-05-30 03:14:59 +00:00
|
|
|
accountNonce=nonce
|
2019-03-16 15:23:15 +00:00
|
|
|
return
|
|
|
|
|
2020-01-24 12:52:55 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, fork: Fork) : Computation =
|
|
|
|
var gas = tx.gasLimit - tx.intrinsicGas(fork)
|
|
|
|
assert gas >= 0
|
|
|
|
|
2020-01-20 17:59:15 +00:00
|
|
|
vmState.setupTxContext(
|
2020-01-16 06:36:58 +00:00
|
|
|
origin = sender,
|
2020-01-16 07:01:59 +00:00
|
|
|
gasPrice = tx.gasPrice,
|
|
|
|
forkOverride = some(fork)
|
2020-01-16 06:36:58 +00:00
|
|
|
)
|
|
|
|
|
2020-01-07 15:44:38 +00:00
|
|
|
let msg = Message(
|
2020-01-08 03:12:06 +00:00
|
|
|
kind: if tx.isContractCreation: evmcCreate else: evmcCall,
|
2020-01-07 15:44:38 +00:00
|
|
|
depth: 0,
|
|
|
|
gas: gas,
|
|
|
|
sender: sender,
|
2020-01-20 11:59:46 +00:00
|
|
|
contractAddress: tx.getRecipient(),
|
2020-01-07 15:44:38 +00:00
|
|
|
codeAddress: tx.to,
|
|
|
|
value: tx.value,
|
2020-01-20 14:02:06 +00:00
|
|
|
data: tx.payload
|
2020-01-07 15:44:38 +00:00
|
|
|
)
|
2019-02-27 02:28:02 +00:00
|
|
|
|
2020-01-16 07:01:59 +00:00
|
|
|
result = newComputation(vmState, msg)
|
2018-09-27 19:09:26 +00:00
|
|
|
doAssert result.isOriginComputation
|
|
|
|
|
2020-01-10 01:26:17 +00:00
|
|
|
proc execComputation*(c: Computation) =
|
|
|
|
if c.msg.isCreate:
|
2020-02-04 11:18:30 +00:00
|
|
|
c.execCreate()
|
2019-03-19 16:54:54 +00:00
|
|
|
else:
|
2020-01-20 12:32:01 +00:00
|
|
|
c.vmState.mutateStateDB:
|
|
|
|
db.incNonce(c.msg.sender)
|
2020-02-04 11:18:30 +00:00
|
|
|
c.execCall()
|
2019-04-23 12:50:45 +00:00
|
|
|
|
2020-01-15 06:28:05 +00:00
|
|
|
if c.isSuccess:
|
|
|
|
c.refundSelfDestruct()
|
2020-01-30 10:26:08 +00:00
|
|
|
shallowCopy(c.vmState.suicides, c.suicides)
|
|
|
|
shallowCopy(c.vmState.logEntries, c.logEntries)
|
2020-02-04 11:18:30 +00:00
|
|
|
c.vmState.touchedAccounts.incl c.touchedAccounts
|
2020-01-15 06:28:05 +00:00
|
|
|
|
2020-01-10 01:26:17 +00:00
|
|
|
c.vmstate.status = c.isSuccess
|
2018-12-31 03:27:02 +00:00
|
|
|
|
2020-01-30 15:54:29 +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)
|
2020-01-10 01:26:17 +00:00
|
|
|
c.vmState.mutateStateDB:
|
2020-01-30 15:54:29 +00:00
|
|
|
db.addBalance(sender, c.gasMeter.gasRemaining.u256 * tx.gasPrice.u256)
|
2019-03-07 07:10:05 +00:00
|
|
|
|
2019-02-26 07:04:12 +00:00
|
|
|
#[
|
2020-01-10 01:26:17 +00:00
|
|
|
method executeTransaction(vmState: BaseVMState, transaction: Transaction): (Computation, BlockHeader) {.base.}=
|
2018-01-17 12:57:50 +00:00
|
|
|
# Execute the transaction in the vm
|
2018-04-14 10:40:41 +00:00
|
|
|
# TODO: introduced here: https://github.com/ethereum/py-evm/commit/21c57f2d56ab91bb62723c3f9ebe291d0b132dde
|
|
|
|
# Refactored/Removed here: https://github.com/ethereum/py-evm/commit/cc991bf
|
|
|
|
# Deleted here: https://github.com/ethereum/py-evm/commit/746defb6f8e83cee2c352a0ab8690e1281c4227c
|
2018-01-17 12:57:50 +00:00
|
|
|
raise newException(ValueError, "Must be implemented by subclasses")
|
|
|
|
|
|
|
|
|
2020-01-10 01:26:17 +00:00
|
|
|
method addTransaction*(vmState: BaseVMState, transaction: Transaction, c: Computation, b: Block): (Block, Table[string, string]) =
|
2018-01-17 12:57:50 +00:00
|
|
|
# Add a transaction to the given block and
|
|
|
|
# return `trieData` to store the transaction data in chaindb in VM layer
|
|
|
|
# Update the bloomFilter, transaction trie and receipt trie roots, bloom_filter,
|
|
|
|
# bloom, and usedGas of the block
|
|
|
|
# transaction: the executed transaction
|
|
|
|
# computation: the Computation object with executed result
|
|
|
|
# block: the Block which the transaction is added in
|
|
|
|
# var receipt = vmState.makeReceipt(transaction, computation)
|
|
|
|
# vmState.add_receipt(receipt)
|
|
|
|
|
|
|
|
# block.transactions.append(transaction)
|
|
|
|
|
|
|
|
# # Get trie roots and changed key-values.
|
|
|
|
# tx_root_hash, tx_kv_nodes = make_trie_root_and_nodes(block.transactions)
|
|
|
|
# receipt_root_hash, receipt_kv_nodes = make_trie_root_and_nodes(self.receipts)
|
|
|
|
|
|
|
|
# trie_data = merge(tx_kv_nodes, receipt_kv_nodes)
|
|
|
|
|
|
|
|
# block.bloom_filter |= receipt.bloom
|
|
|
|
|
|
|
|
# block.header.transaction_root = tx_root_hash
|
|
|
|
# block.header.receipt_root = receipt_root_hash
|
|
|
|
# block.header.bloom = int(block.bloom_filter)
|
|
|
|
# block.header.gas_used = receipt.gas_used
|
|
|
|
|
|
|
|
# return block, trie_data
|
2018-01-31 12:57:05 +00:00
|
|
|
result = (b, initTable[string, string]())
|
2018-01-17 12:57:50 +00:00
|
|
|
|
|
|
|
method applyTransaction*(
|
2018-12-03 10:54:19 +00:00
|
|
|
vmState: BaseVMState,
|
2018-09-27 19:09:26 +00:00
|
|
|
transaction: Transaction,
|
2018-01-17 12:57:50 +00:00
|
|
|
b: Block,
|
2020-01-10 01:26:17 +00:00
|
|
|
isStateless: bool): (Computation, Block, Table[string, string]) =
|
2018-01-17 12:57:50 +00:00
|
|
|
# Apply transaction to the given block
|
|
|
|
# transaction: the transaction need to be applied
|
|
|
|
# b: the block which the transaction applies on
|
|
|
|
# isStateless: if isStateless, call vmState.addTransaction to set block
|
2018-04-06 14:25:01 +00:00
|
|
|
|
2018-01-17 12:57:50 +00:00
|
|
|
if isStateless:
|
|
|
|
var ourBlock = b # deepcopy
|
|
|
|
vmState.blockHeader = b.header
|
|
|
|
var (computation, blockHeader) = vmState.executeTransaction(transaction)
|
|
|
|
|
|
|
|
ourBlock.header = blockHeader
|
2018-01-31 12:57:05 +00:00
|
|
|
var trieData: Table[string, string]
|
2018-01-17 12:57:50 +00:00
|
|
|
(ourBlock, trieData) = vmState.addTransaction(transaction, computation, ourBlock)
|
|
|
|
|
|
|
|
result = (computation, ourBlock, trieData)
|
|
|
|
else:
|
|
|
|
var (computation, blockHeader) = vmState.executeTransaction(transaction)
|
2018-01-31 12:57:05 +00:00
|
|
|
return (computation, nil, initTable[string, string]())
|
2019-02-26 07:04:12 +00:00
|
|
|
]#
|