diff --git a/docs/api.state.rst b/docs/api.state.rst index e26578e7c8..0adfc3aad6 100644 --- a/docs/api.state.rst +++ b/docs/api.state.rst @@ -19,7 +19,6 @@ configured. - ``block_class``: The `~evm.rlp.blocks.Block` class for blocks in this VM ruleset. - ``computation_class``: The `~evm.vm.computation.BaseComputation` class for vm execution. -- ``trie_class``: The class that is used to house the state trie. - ``transaction_context_class``: The `~evm.vm.transaction_context.TransactionContext` class for vm execution. diff --git a/evm/chains/base.py b/evm/chains/base.py index 6f4bdaa2e8..efe4f72585 100644 --- a/evm/chains/base.py +++ b/evm/chains/base.py @@ -19,6 +19,7 @@ check_pow, ) from evm.constants import ( + BLANK_ROOT_HASH, MAX_UNCLE_DEPTH, ) from evm.db.chain import AsyncChainDB @@ -436,7 +437,7 @@ def from_genesis(cls, """ Initializes the Chain from a genesis state. """ - state_db = chaindb.get_state_db(chaindb.empty_root_hash, read_only=False) + state_db = chaindb.get_state_db(BLANK_ROOT_HASH, read_only=False) if genesis_state is None: genesis_state = {} diff --git a/evm/db/chain.py b/evm/db/chain.py index 952cb0c9b7..8c56099de6 100644 --- a/evm/db/chain.py +++ b/evm/db/chain.py @@ -12,13 +12,11 @@ Tuple, Type, TYPE_CHECKING, - Union ) import rlp from trie import ( - BinaryTrie, HexaryTrie, ) @@ -31,8 +29,6 @@ from evm.constants import ( GENESIS_PARENT_HASH, - BLANK_ROOT_HASH, - EMPTY_SHA3, ) from evm.exceptions import ( BlockNotFound, @@ -88,36 +84,14 @@ 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[BaseAccountDB] = AccountDB, - 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 @@ -454,7 +428,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: @@ -467,7 +441,7 @@ def _get_block_transaction_data(self, transaction_root: bytes) -> Iterable[bytes ''' :returns: iterable of encoded transactions for the given block header ''' - transaction_db = self.trie_class(self.db, root_hash=transaction_root) + transaction_db = HexaryTrie(self.db, root_hash=transaction_root) for transaction_idx in itertools.count(): transaction_key = rlp.encode(transaction_idx) if transaction_key in transaction_db: @@ -504,7 +478,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] @@ -547,12 +521,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 @@ -575,7 +549,7 @@ def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None: def get_state_db(self, state_root: bytes, read_only: bool) -> BaseAccountDB: - return self.account_state_class( + return AccountDB( db=self.journal_db, root_hash=state_root, read_only=read_only, diff --git a/evm/utils/db.py b/evm/utils/db.py index 19203a52ee..691b048dd3 100644 --- a/evm/utils/db.py +++ b/evm/utils/db.py @@ -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 @@ -43,23 +33,6 @@ def get_block_header_by_hash(block_hash: BlockHeader, db: 'BaseChainDB') -> Bloc 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 - - def apply_state_dict(account_db, state_dict): for account, account_data in state_dict.items(): account_db.set_balance(account, account_data["balance"]) diff --git a/evm/vm/forks/frontier/state.py b/evm/vm/forks/frontier/state.py index 79c776b89c..5622930c55 100644 --- a/evm/vm/forks/frontier/state.py +++ b/evm/vm/forks/frontier/state.py @@ -1,10 +1,6 @@ from __future__ import absolute_import from typing import Type # noqa: F401 -from trie import ( - HexaryTrie, -) - from eth_hash.auto import keccak from evm import constants @@ -215,7 +211,6 @@ def _make_frontier_receipt(state, transaction, computation): class FrontierState(BaseState, FrontierTransactionExecutor): block_class = FrontierBlock computation_class = FrontierComputation - trie_class = HexaryTrie transaction_context_class = FrontierTransactionContext # type: Type[BaseTransactionContext] def make_receipt(self, transaction, computation): diff --git a/evm/vm/state.py b/evm/vm/state.py index 34c0b0f912..0dd97091f8 100644 --- a/evm/vm/state.py +++ b/evm/vm/state.py @@ -52,7 +52,6 @@ class BaseState(Configurable, metaclass=ABCMeta): block_class = None # type: Type[BaseBlock] computation_class = None # type: Type[BaseComputation] - trie_class = None transaction_context_class = None # type: Type[BaseTransactionContext] def __init__(self, chaindb, execution_context, state_root, gas_used): diff --git a/tests/database/test_chaindb.py b/tests/database/test_chaindb.py index 6470267a89..e11607e520 100644 --- a/tests/database/test_chaindb.py +++ b/tests/database/test_chaindb.py @@ -6,22 +6,18 @@ ) import rlp -from trie import ( - BinaryTrie, - HexaryTrie, -) from eth_hash.auto import keccak +from evm.constants import ( + BLANK_ROOT_HASH, +) from evm.db import ( get_db_backend, ) from evm.db.chain import ( ChainDB, ) -from evm.db.account import ( - AccountDB, -) from evm.exceptions import ( BlockNotFound, ParentNotFound, @@ -33,7 +29,6 @@ assert_rlp_equal, ) from evm.utils.db import ( - get_empty_root_hash, make_block_hash_to_score_lookup_key, make_block_number_to_hash_lookup_key, ) @@ -50,25 +45,16 @@ def set_empty_root(chaindb, header): - root_hash = get_empty_root_hash(chaindb) return header.copy( - transaction_root=root_hash, - receipt_root=root_hash, - state_root=root_hash, + transaction_root=BLANK_ROOT_HASH, + receipt_root=BLANK_ROOT_HASH, + state_root=BLANK_ROOT_HASH, ) -@pytest.fixture(params=[AccountDB]) +@pytest.fixture def chaindb(request): - if request.param is AccountDB: - trie_class = HexaryTrie - else: - trie_class = BinaryTrie - return ChainDB( - get_db_backend(), - account_state_class=request.param, - trie_class=trie_class, - ) + return ChainDB(get_db_backend()) @pytest.fixture(params=[0, 10, 999]) diff --git a/tests/json-fixtures/test_state.py b/tests/json-fixtures/test_state.py index dfc675d81e..acbe6b5e28 100644 --- a/tests/json-fixtures/test_state.py +++ b/tests/json-fixtures/test_state.py @@ -4,10 +4,6 @@ from eth_keys import keys -from trie import ( - HexaryTrie, -) - from evm.db import ( get_db_backend, ) @@ -20,9 +16,6 @@ from eth_hash.auto import keccak from evm.db.chain import ChainDB -from evm.db.account import ( - AccountDB, -) from evm.exceptions import ( ValidationError, ) @@ -266,8 +259,6 @@ def fixture_vm_class(fixture_data): def test_state_fixtures(fixture, fixture_vm_class): - account_state_class = AccountDB - trie_class = HexaryTrie header = BlockHeader( coinbase=fixture['env']['currentCoinbase'], difficulty=fixture['env']['currentDifficulty'], @@ -277,11 +268,7 @@ def test_state_fixtures(fixture, fixture_vm_class): parent_hash=fixture['env']['previousHash'], ) - chaindb = ChainDB( - get_db_backend(), - account_state_class=account_state_class, - trie_class=trie_class - ) + chaindb = ChainDB(get_db_backend()) vm = fixture_vm_class(header=header, chaindb=chaindb) state = vm.state