From e6f6a7a4a7029734ef1055b64cce1e0c81399603 Mon Sep 17 00:00:00 2001 From: Piper Merriam Date: Mon, 30 Apr 2018 11:33:11 -0600 Subject: [PATCH] Remove apply_state_dict API --- evm/chains/base.py | 6 +++++- evm/db/account.py | 13 ------------- evm/tools/test_builder/builder_utils.py | 6 +++++- evm/utils/db.py | 12 ++++++++++++ tests/json-fixtures/test_state.py | 5 ++++- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/evm/chains/base.py b/evm/chains/base.py index f84925aa94..6f4bdaa2e8 100644 --- a/evm/chains/base.py +++ b/evm/chains/base.py @@ -40,6 +40,9 @@ from evm.rlp.headers import ( BlockHeader, ) +from evm.utils.db import ( + apply_state_dict, +) from evm.utils.chain import ( generate_vms_by_range, ) @@ -438,7 +441,8 @@ def from_genesis(cls, if genesis_state is None: genesis_state = {} - state_db.apply_state_dict(genesis_state) + # mutation + apply_state_dict(state_db, genesis_state) if 'state_root' not in genesis_params: # If the genesis state_root was not specified, use the value diff --git a/evm/db/account.py b/evm/db/account.py index b165fd3ea5..ce88824b18 100644 --- a/evm/db/account.py +++ b/evm/db/account.py @@ -55,10 +55,6 @@ def __init__(self) -> None: "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") @@ -167,15 +163,6 @@ def _trie(self): 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 diff --git a/evm/tools/test_builder/builder_utils.py b/evm/tools/test_builder/builder_utils.py index e302c3896f..e4a9e7731b 100644 --- a/evm/tools/test_builder/builder_utils.py +++ b/evm/tools/test_builder/builder_utils.py @@ -17,6 +17,10 @@ to_text, int_to_big_endian, ) + +from evm.utils.db import ( + apply_state_dict, +) from evm.utils.padding import ( pad32, ) @@ -111,7 +115,7 @@ def get_version_from_git(): def calc_state_root(state, account_state_db_class): state_db = account_state_db_class(MemoryDB()) - state_db.apply_state_dict(state) + apply_state_dict(state_db, state) return state_db.root_hash diff --git a/evm/utils/db.py b/evm/utils/db.py index 31427034c8..19203a52ee 100644 --- a/evm/utils/db.py +++ b/evm/utils/db.py @@ -58,3 +58,15 @@ def get_empty_root_hash(db: 'BaseChainDB') -> bytes: "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"]) + account_db.set_nonce(account, account_data["nonce"]) + account_db.set_code(account, account_data["code"]) + + for slot, value in account_data["storage"].items(): + account_db.set_storage(account, slot, value) + + return account_db diff --git a/tests/json-fixtures/test_state.py b/tests/json-fixtures/test_state.py index 0f4f064d93..dfc675d81e 100644 --- a/tests/json-fixtures/test_state.py +++ b/tests/json-fixtures/test_state.py @@ -50,6 +50,9 @@ normalize_statetest_fixture, should_run_slow_tests, ) +from evm.utils.db import ( + apply_state_dict, +) from eth_typing.enums import ( ForkName @@ -283,7 +286,7 @@ def test_state_fixtures(fixture, fixture_vm_class): state = vm.state with state.mutable_state_db() as state_db: - state_db.apply_state_dict(fixture['pre']) + apply_state_dict(state_db, fixture['pre']) # Update state_root manually vm.block = vm.block.copy(header=vm.block.header.copy(state_root=state.state_root)) if 'secretKey' in fixture['transaction']: