Skip to content

Commit

Permalink
Bugfix and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Jan 4, 2018
1 parent ec9ff9c commit 53f1046
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 57 deletions.
2 changes: 1 addition & 1 deletion evm/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def apply_computation(cls, vm_state, message):
@property
def precompiles(self):
if self._precompiles is None:
return set()
return dict()
else:
return self._precompiles

Expand Down
5 changes: 2 additions & 3 deletions evm/db/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ class AccountStateDB:

def __init__(self, db, root_hash=BLANK_ROOT_HASH, read_only=False):
if read_only:
self.db = ImmutableDB(db)
self.db = TrackedDB(ImmutableDB(db))
else:
self.db = db
self.db = TrackedDB(self.db)
self.db = TrackedDB(db)
self._trie = HashTrie(Trie(self.db, root_hash))

#
Expand Down
9 changes: 4 additions & 5 deletions evm/vm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def pack_block(self, block, *args, **kwargs):
if unknown_fields:
raise AttributeError(
"Unable to set the field(s) {0} on the `BlockHeader` class. "
"Received the following unexpected fields: {0}.".format(
"Received the following unexpected fields: {1}.".format(
", ".join(known_fields),
", ".join(unknown_fields),
)
Expand Down Expand Up @@ -262,8 +262,7 @@ def finalize_block(cls, vm_state, block):
@classmethod
def create_block(
cls,
transactions,
transaction_witnesses,
transaction_packages,
prev_state_root,
parent_header,
coinbase):
Expand Down Expand Up @@ -292,8 +291,8 @@ def create_block(
witness = {}
witness_db = BaseChainDB(MemoryDB(witness))

for index, transaction in enumerate(transactions):
witness.update(transaction_witnesses[index])
for index, (transaction, transaction_witness) in enumerate(transaction_packages):
witness.update(transaction_witness)
witness_db = BaseChainDB(MemoryDB(witness))
vm_state.set_chaindb(witness_db)

Expand Down
11 changes: 3 additions & 8 deletions evm/vm/forks/byzantium/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@


class ByzantiumComputation(SpuriousDragonComputation):
def __init__(self, vm_state, message):
super(ByzantiumComputation, self).__init__(
vm_state,
message,
)
# Override
self.opcodes = BYZANTIUM_OPCODES
self._precompiles = BYZANTIUM_PRECOMPILES
# Override
opcodes = BYZANTIUM_OPCODES
_precompiles = BYZANTIUM_PRECOMPILES
11 changes: 3 additions & 8 deletions evm/vm/forks/frontier/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@


class FrontierComputation(BaseComputation):
def __init__(self, vm_state, message):
super(FrontierComputation, self).__init__(
vm_state,
message,
)
# Override
self.opcodes = FRONTIER_OPCODES
self._precompiles = FRONTIER_PRECOMPILES
# Override
opcodes = FRONTIER_OPCODES
_precompiles = FRONTIER_PRECOMPILES

def apply_message(self):
snapshot = self.vm_state.snapshot()
Expand Down
2 changes: 1 addition & 1 deletion evm/vm/forks/frontier/vm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def validate_block(self, block):
for uncle in block.uncles:
self.validate_uncle(block, uncle)

if not self.is_key_exsits(block.header.state_root):
if not self.is_key_exists(block.header.state_root):
raise ValidationError(
"`state_root` was not found in the db.\n"
"- state_root: {0}".format(
Expand Down
9 changes: 2 additions & 7 deletions evm/vm/forks/homestead/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@


class HomesteadComputation(FrontierComputation):
def __init__(self, vm_state, message):
super(HomesteadComputation, self).__init__(
vm_state,
message,
)
# Overwrite
self.opcodes = HOMESTEAD_OPCODES
# Override
opcodes = HOMESTEAD_OPCODES

def apply_create_message(self):
snapshot = self.vm_state.snapshot()
Expand Down
9 changes: 2 additions & 7 deletions evm/vm/forks/spurious_dragon/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@


class SpuriousDragonComputation(HomesteadComputation):
def __init__(self, vm_state, message):
super(SpuriousDragonComputation, self).__init__(
vm_state,
message,
)
# Override
self.opcodes = SPURIOUS_DRAGON_OPCODES
# Override
opcodes = SPURIOUS_DRAGON_OPCODES

def apply_create_message(self):
snapshot = self.vm_state.snapshot()
Expand Down
9 changes: 2 additions & 7 deletions evm/vm/forks/tangerine_whistle/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@


class TangerineWhistleComputation(HomesteadComputation):
def __init__(self, vm_state, message):
super(TangerineWhistleComputation, self).__init__(
vm_state,
message,
)
# Override
self.opcodes = TANGERINE_WHISTLE_OPCODES
# Override
opcodes = TANGERINE_WHISTLE_OPCODES
18 changes: 10 additions & 8 deletions evm/vm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import copy
import logging

from cytoolz import (
merge,
)
import rlp
from trie import (
Trie,
Expand All @@ -16,13 +19,14 @@ class BaseVMState(object):
_chaindb = None
block_header = None
computation_class = None
access_logs = AccessLogs()
access_logs = None
receipts = None

def __init__(self, chaindb, block_header):
self._chaindb = chaindb
self.block_header = block_header

self.access_logs = AccessLogs()
self.receipts = []

#
Expand Down Expand Up @@ -149,7 +153,7 @@ def get_parent_header(self, block_header):
"""
return self.get_block_header_by_hash(block_header.parent_hash)

def is_key_exsits(self, key):
def is_key_exists(self, key):
"""
Check if the given key exsits in chaindb
"""
Expand All @@ -161,10 +165,10 @@ def is_key_exsits(self, key):
def get_computation(self, message):
"""Return state object
"""
if self.computation_class is not None:
computation = self.computation_class(self, message)
else:
if self.computation_class is None:
raise AttributeError("No `computation_class` has been set for this VMState")
else:
computation = self.computation_class(self, message)
return computation

#
Expand Down Expand Up @@ -228,9 +232,7 @@ def add_transaction(cls, vm_state, transaction, computation, block):
receipt,
block.db,
)
trie_data = {}
trie_data.update(tx_db.wrapped_db.kv_store)
trie_data.update(receipt_db.wrapped_db.kv_store)
trie_data = merge(tx_db.wrapped_db.kv_store, receipt_db.wrapped_db.kv_store)

block.bloom_filter |= receipt.bloom

Expand Down
4 changes: 2 additions & 2 deletions tests/core/vm/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def test_create_block(chain_without_block_validation): # noqa: F811

# (3) Try to create a block by witness
vm2 = copy.deepcopy(vm)
transaction_package = (tx, transaction_witness)
block2, _, _ = vm2.create_block(
transactions=[tx],
transaction_witnesses=[transaction_witness],
transaction_packages=[transaction_package],
prev_state_root=block0.header.state_root,
parent_header=block0.header,
coinbase=recipient,
Expand Down

0 comments on commit 53f1046

Please sign in to comment.