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
|
|
|
|
strformat, tables,
|
2018-08-23 03:38:00 +00:00
|
|
|
./constants, ./errors, ./vm/computation,
|
2018-06-15 09:11:25 +00:00
|
|
|
./transaction, ./vm_types, ./vm_state, ./block_types, ./db/db_chain, ./utils/header
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-04-14 10:40:41 +00:00
|
|
|
method executeTransaction(vmState: var BaseVMState, transaction: BaseTransaction): (BaseComputation, 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")
|
|
|
|
|
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
method addTransaction*(vmState: var BaseVMState, transaction: BaseTransaction, computation: BaseComputation, 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*(
|
|
|
|
vmState: var BaseVMState,
|
|
|
|
transaction: BaseTransaction,
|
|
|
|
b: Block,
|
2018-01-31 12:57:05 +00:00
|
|
|
isStateless: bool): (BaseComputation, 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]())
|