Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactory how account db is used. #611

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions evm/chains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
MAX_UNCLE_DEPTH,
)
from evm.db.chain import AsyncChainDB
from evm.db.trie import make_trie_root_and_nodes
from evm.estimators import (
get_gas_estimator,
)
Expand Down Expand Up @@ -209,6 +210,13 @@ def get_vm(self, header=None):
#
# Execution API
#
@abstractmethod
def apply_transaction(self, transaction):
"""
Applies the transaction to the current tip block.
"""
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def estimate_gas(self, transaction, at_header=None):
"""
Expand Down Expand Up @@ -480,6 +488,26 @@ def from_genesis_header(cls, chaindb, genesis_header):
#
# Mining and Execution API
#
def apply_transaction(self, transaction):
"""
Applies the transaction to the current tip block.
"""
vm = self.get_vm()
block, receipt, computation = vm.apply_transaction(transaction)
receipts = block.get_receipts(self.chaindb) + [receipt]

tx_root_hash, tx_kv_nodes = make_trie_root_and_nodes(block.transactions)
receipt_root_hash, receipt_kv_nodes = make_trie_root_and_nodes(receipts)
self.chaindb.persist_trie_data_dict(tx_kv_nodes)
self.chaindb.persist_trie_data_dict(receipt_kv_nodes)

self.header = block.header.copy(
transaction_root=tx_root_hash,
receipt_root=receipt_root_hash,
)

return block.copy(header=self.header), receipt, computation

def estimate_gas(self, transaction, at_header=None):
if at_header is None:
at_header = self.get_canonical_head()
Expand Down
49 changes: 5 additions & 44 deletions evm/db/state.py → evm/db/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
BLANK_ROOT_HASH,
EMPTY_SHA3,
)
from evm.db.immutable import (
ImmutableDB,
)
from evm.exceptions import DecommissionedStateDB
from evm.rlp.accounts import (
Account,
)
Expand All @@ -48,22 +44,14 @@
account_cache = LRU(2048)


class BaseAccountStateDB(metaclass=ABCMeta):
class BaseAccountDB(metaclass=ABCMeta):

@abstractmethod
def __init__(self) -> None:
raise NotImplementedError(
"Must be implemented by subclasses"
)

@abstractmethod
def apply_state_dict(self, state_dict):
raise NotImplementedError("Must be implemented by subclasses")

@abstractmethod
def decommission(self):
raise NotImplementedError("Must be implemented by subclasses")

# We need to ignore this until https://github.com/python/mypy/issues/4165 is resolved
@property # type: ignore
@abstractmethod
Expand Down Expand Up @@ -127,40 +115,13 @@ def account_is_empty(self, address):
raise NotImplementedError("Must be implemented by subclass")


class MainAccountStateDB(BaseAccountStateDB):

def __init__(self, db, root_hash=BLANK_ROOT_HASH, read_only=False):
class AccountDB(BaseAccountDB):
def __init__(self, db, root_hash=BLANK_ROOT_HASH):
# Keep a reference to the original db instance to use it as part of _get_account()'s cache
# key.
self._unwrapped_db = db
if read_only:
self.db = ImmutableDB(db)
else:
self.db = db
self.__trie = HashTrie(HexaryTrie(self.db, root_hash))

@property
def _trie(self):
if self.__trie is None:
raise DecommissionedStateDB()
return self.__trie

@_trie.setter
def _trie(self, value):
self.__trie = value

def apply_state_dict(self, state_dict):
for account, account_data in state_dict.items():
self.set_balance(account, account_data["balance"])
self.set_nonce(account, account_data["nonce"])
self.set_code(account, account_data["code"])

for slot, value in account_data["storage"].items():
self.set_storage(account, slot, value)

def decommission(self):
self.db = None
self.__trie = None
self.db = db
self._trie = HashTrie(HexaryTrie(self.db, root_hash))

@property
def root_hash(self):
Expand Down
57 changes: 13 additions & 44 deletions evm/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
Tuple,
Type,
TYPE_CHECKING,
Union
)
from uuid import UUID

import rlp

from trie import (
BinaryTrie,
HexaryTrie,
)

Expand All @@ -31,8 +29,6 @@

from evm.constants import (
GENESIS_PARENT_HASH,
BLANK_ROOT_HASH,
EMPTY_SHA3,
)
from evm.exceptions import (
BlockNotFound,
Expand All @@ -46,9 +42,9 @@
from evm.db.journal import (
JournalDB,
)
from evm.db.state import (
BaseAccountStateDB,
MainAccountStateDB,
from evm.db.account import (
BaseAccountDB,
AccountDB,
)
from evm.rlp.headers import (
BlockHeader,
Expand Down Expand Up @@ -89,36 +85,11 @@ class TransactionKey(rlp.Serializable):


class BaseChainDB(metaclass=ABCMeta):
trie_class = None
empty_root_hash = None

#
# Trie
#
def set_trie(self, trie_class: Union[Type[HexaryTrie], Type[BinaryTrie]]) -> None:
"""
Sets trie_class and root_hash.
"""
if trie_class is HexaryTrie:
empty_root_hash = BLANK_ROOT_HASH
elif trie_class is BinaryTrie:
empty_root_hash = EMPTY_SHA3
else:
raise NotImplementedError(
"trie_class {} is not supported.".format(trie_class)
)
self.trie_class = trie_class
self.empty_root_hash = empty_root_hash

def __init__(self,
db: BaseDB,
account_state_class: Type[BaseAccountStateDB] = MainAccountStateDB,
trie_class: Type[HexaryTrie] = HexaryTrie) -> None:
db: BaseDB) -> None:

self.db = db
self.journal_db = JournalDB(db)
self.account_state_class = account_state_class
self.set_trie(trie_class)

#
# Canonical chain API
Expand Down Expand Up @@ -280,7 +251,7 @@ def clear(self) -> None:
@abstractmethod
def get_state_db(self,
state_root: bytes,
read_only: bool) -> BaseAccountStateDB:
read_only: bool) -> BaseAccountDB:
raise NotImplementedError("ChainDB classes must implement this method")


Expand Down Expand Up @@ -485,7 +456,7 @@ def get_block_uncles(self, uncles_hash: bytes) -> List[BlockHeader]:
#
@to_list
def get_receipts(self, header: BlockHeader, receipt_class: Type[Receipt]) -> Iterable[Receipt]:
receipt_db = self.trie_class(db=self.db, root_hash=header.receipt_root)
receipt_db = HexaryTrie(db=self.db, root_hash=header.receipt_root)
for receipt_idx in itertools.count():
receipt_key = rlp.encode(receipt_idx)
if receipt_key in receipt_db:
Expand All @@ -498,7 +469,7 @@ def _get_block_transaction_data(self, block_header: BlockHeader) -> Iterable[byt
'''
:returns: iterable of encoded transactions for the given block header
'''
transaction_db = self.trie_class(self.journal_db, root_hash=block_header.transaction_root)
transaction_db = HexaryTrie(self.journal_db, root_hash=block_header.transaction_root)
for transaction_idx in itertools.count():
transaction_key = rlp.encode(transaction_idx)
if transaction_key in transaction_db:
Expand Down Expand Up @@ -528,7 +499,7 @@ def get_transaction_by_index(
block_header = self.get_canonical_block_header_by_number(block_number)
except KeyError:
raise TransactionNotFound("Block {} is not in the canonical chain".format(block_number))
transaction_db = self.trie_class(self.db, root_hash=block_header.transaction_root)
transaction_db = HexaryTrie(self.db, root_hash=block_header.transaction_root)
encoded_index = rlp.encode(transaction_index)
if encoded_index in transaction_db:
encoded_transaction = transaction_db[encoded_index]
Expand Down Expand Up @@ -593,12 +564,12 @@ def add_transaction(self,
block_header: BlockHeader,
index_key: int,
transaction: 'BaseTransaction') -> bytes:
transaction_db = self.trie_class(self.db, root_hash=block_header.transaction_root)
transaction_db = HexaryTrie(self.db, root_hash=block_header.transaction_root)
transaction_db[index_key] = rlp.encode(transaction)
return transaction_db.root_hash

def add_receipt(self, block_header: BlockHeader, index_key: int, receipt: Receipt) -> bytes:
receipt_db = self.trie_class(db=self.db, root_hash=block_header.receipt_root)
receipt_db = HexaryTrie(db=self.db, root_hash=block_header.receipt_root)
receipt_db[index_key] = rlp.encode(receipt)
return receipt_db.root_hash

Expand Down Expand Up @@ -638,8 +609,8 @@ def clear(self) -> None:
#
def get_state_db(self,
state_root: bytes,
read_only: bool) -> BaseAccountStateDB:
return self.account_state_class(
read_only: bool) -> BaseAccountDB:
return AccountDB(
db=self.journal_db,
root_hash=state_root,
read_only=read_only,
Expand Down Expand Up @@ -683,11 +654,9 @@ async def coro_persist_trie_data_dict(self, *args, **kwargs):

class NonJournaledAsyncChainDB(AsyncChainDB):

def __init__(self, db, account_state_class=MainAccountStateDB, trie_class=HexaryTrie):
def __init__(self, db):
self.db = db
self.journal_db = db
self.account_state_class = account_state_class
self.set_trie(trie_class)

def snapshot(self):
raise NotImplementedError()
Expand Down
6 changes: 0 additions & 6 deletions evm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,3 @@ class OutOfBoundsRead(VMError):
boundaries of the buffer (such as with RETURNDATACOPY)
"""
pass


class DecommissionedStateDB(Exception):
"""
An attempt was made to use a StateDB object used after leaving the context.
"""
14 changes: 7 additions & 7 deletions evm/tools/test_builder/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
List,
)

from evm.db.state import (
MainAccountStateDB,
from evm.db.account import (
AccountDB,
)
from evm.tools.fixture_tests import (
hash_log_entries,
Expand Down Expand Up @@ -108,11 +108,11 @@ def get_default_transaction(networks):
]

ACCOUNT_STATE_DB_CLASSES = {
"Frontier": MainAccountStateDB,
"Homestead": MainAccountStateDB,
"EIP150": MainAccountStateDB,
"EIP158": MainAccountStateDB,
"Byzantium": MainAccountStateDB,
"Frontier": AccountDB,
"Homestead": AccountDB,
"EIP150": AccountDB,
"EIP158": AccountDB,
"Byzantium": AccountDB,
}
assert all(network in ACCOUNT_STATE_DB_CLASSES for network in ALL_NETWORKS)

Expand Down
27 changes: 0 additions & 27 deletions evm/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
from trie import (
BinaryTrie,
HexaryTrie,
)

from evm.constants import (
BLANK_ROOT_HASH,
EMPTY_SHA3,
)

from evm.rlp.headers import BlockHeader

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -48,20 +38,3 @@ def get_block_header_by_hash(block_hash: BlockHeader, db: 'BaseChainDB') -> Bloc
Returns the header for the parent block.
"""
return db.get_block_header_by_hash(block_hash)


def get_empty_root_hash(db: 'BaseChainDB') -> bytes:
root_hash = None
if db.trie_class is HexaryTrie:
root_hash = BLANK_ROOT_HASH
elif db.trie_class is BinaryTrie:
root_hash = EMPTY_SHA3
elif db.trie_class is None:
raise AttributeError(
"BaseChainDB must declare a trie_class."
)
else:
raise NotImplementedError(
"db.trie_class {} is not supported.".format(db.trie_class)
)
return root_hash
Loading