Skip to content

Commit 28f5129

Browse files
authored
Merge pull request #1296 from carver/atomic-db-for-chains-and-headers
Require BaseAtomicDB in ChainDB and HeaderDB
2 parents 7b1fec6 + 5eb7bb7 commit 28f5129

24 files changed

+72
-84
lines changed

docs/cookbooks/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ Then to initialize, you can start it up with an in-memory database:
4444

4545
.. doctest::
4646

47-
>>> from eth.db.backends.memory import MemoryDB
47+
>>> from eth.db.atomic import AtomicDB
4848
>>> from eth.chains.mainnet import MAINNET_GENESIS_HEADER
4949

5050
>>> # start a fresh in-memory db
5151

5252
>>> # initialize a fresh chain
53-
>>> chain = chain_class.from_genesis_header(MemoryDB(), MAINNET_GENESIS_HEADER)
53+
>>> chain = chain_class.from_genesis_header(AtomicDB(), MAINNET_GENESIS_HEADER)
5454

5555
.. _evm_cookbook_recipe_creating_a_chain_with_custom_state:
5656

@@ -66,7 +66,7 @@ state.
6666
>>> from eth_keys import keys
6767
>>> from eth import constants
6868
>>> from eth.chains.mainnet import MainnetChain
69-
>>> from eth.db.backends.memory import MemoryDB
69+
>>> from eth.db.atomic import AtomicDB
7070
>>> from eth_utils import to_wei, encode_hex
7171

7272

@@ -95,7 +95,7 @@ state.
9595
... 'nonce': constants.GENESIS_NONCE
9696
... }
9797

98-
>>> chain = MainnetChain.from_genesis(MemoryDB(), GENESIS_PARAMS, GENESIS_STATE)
98+
>>> chain = MainnetChain.from_genesis(AtomicDB(), GENESIS_PARAMS, GENESIS_STATE)
9999

100100
.. _evm_cookbook_recipe_getting_the_balance_from_an_account:
101101

docs/guides/eth/building_an_app_that_uses_pyevm.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Next, we'll create a new directory ``app`` and create a file ``main.py`` inside.
7070

7171
.. code-block:: python
7272
73-
from evm import constants
74-
from evm.chains.mainnet import MainnetChain
75-
from evm.db.backends.memory import MemoryDB
73+
from eth import constants
74+
from eth.chains.mainnet import MainnetChain
75+
from eth.db.backends.memory import MemoryDB
7676
7777
from eth_utils import to_wei, encode_hex
7878

docs/guides/eth/understanding_the_mining_process.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ cookbook.
4444

4545
::
4646

47-
from eth.db.backends.memory import MemoryDB
47+
from eth.db.atomic import AtomicDB
4848
from eth.chains.mainnet import MAINNET_GENESIS_HEADER
4949

5050
# initialize a fresh chain
51-
chain = chain_class.from_genesis_header(MemoryDB(), MAINNET_GENESIS_HEADER)
51+
chain = chain_class.from_genesis_header(AtomicDB(), MAINNET_GENESIS_HEADER)
5252

5353
Since we decided to not add any transactions to our block let's just call
5454
:func:`~~eth.chains.base.MiningChain.mine_block` and see what happens.
5555

5656
::
5757

5858
# initialize a fresh chain
59-
chain = chain_class.from_genesis_header(MemoryDB(), MAINNET_GENESIS_HEADER)
59+
chain = chain_class.from_genesis_header(AtomicDB(), MAINNET_GENESIS_HEADER)
6060

6161
chain.mine_block()
6262

@@ -163,15 +163,15 @@ Next, we'll create the chain itself using the defined ``GENESIS_PARAMS`` and the
163163

164164
from eth import MiningChain
165165
from eth.vm.forks.byzantium import ByzantiumVM
166-
from eth.db.backends.memory import MemoryDB
166+
from eth.db.backends.memory import AtomicDB
167167

168168

169169
klass = MiningChain.configure(
170170
__name__='TestChain',
171171
vm_configuration=(
172172
(constants.GENESIS_BLOCK_NUMBER, ByzantiumVM),
173173
))
174-
chain = klass.from_genesis(MemoryDB(), GENESIS_PARAMS)
174+
chain = klass.from_genesis(AtomicDB(), GENESIS_PARAMS)
175175

176176

177177
Now that we have the building blocks available, let's put it all together and mine a proper block!
@@ -319,7 +319,7 @@ zero value transfer transaction.
319319
>>> from eth.chains.base import MiningChain
320320
>>> from eth.consensus.pow import mine_pow_nonce
321321
>>> from eth.vm.forks.byzantium import ByzantiumVM
322-
>>> from eth.db.backends.memory import MemoryDB
322+
>>> from eth.db.atomic import AtomicDB
323323

324324

325325
>>> GENESIS_PARAMS = {
@@ -350,7 +350,7 @@ zero value transfer transaction.
350350
... (constants.GENESIS_BLOCK_NUMBER, ByzantiumVM),
351351
... ))
352352

353-
>>> chain = klass.from_genesis(MemoryDB(), GENESIS_PARAMS)
353+
>>> chain = klass.from_genesis(AtomicDB(), GENESIS_PARAMS)
354354
>>> vm = chain.get_vm()
355355

356356
>>> nonce = vm.state.account_db.get_nonce(SENDER)

eth/chains/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
ValidationError,
4343
)
4444

45-
from eth.db.backends.base import BaseDB
45+
from eth.db.backends.base import BaseAtomicDB
4646
from eth.db.chain import (
4747
BaseChainDB,
4848
ChainDB,
@@ -144,15 +144,15 @@ def get_vm_configuration(cls) -> Tuple[Tuple[int, Type['BaseVM']], ...]:
144144
@classmethod
145145
@abstractmethod
146146
def from_genesis(cls,
147-
base_db: BaseDB,
147+
base_db: BaseAtomicDB,
148148
genesis_params: Dict[str, HeaderParams],
149149
genesis_state: AccountState=None) -> 'BaseChain':
150150
raise NotImplementedError("Chain classes must implement this method")
151151

152152
@classmethod
153153
@abstractmethod
154154
def from_genesis_header(cls,
155-
base_db: BaseDB,
155+
base_db: BaseAtomicDB,
156156
genesis_header: BlockHeader) -> 'BaseChain':
157157
raise NotImplementedError("Chain classes must implement this method")
158158

@@ -322,7 +322,7 @@ class Chain(BaseChain):
322322

323323
chaindb_class = ChainDB # type: Type[BaseChainDB]
324324

325-
def __init__(self, base_db: BaseDB) -> None:
325+
def __init__(self, base_db: BaseAtomicDB) -> None:
326326
if not self.vm_configuration:
327327
raise ValueError(
328328
"The Chain class cannot be instantiated with an empty `vm_configuration`"
@@ -349,7 +349,7 @@ def get_chaindb_class(cls) -> Type[BaseChainDB]:
349349
#
350350
@classmethod
351351
def from_genesis(cls,
352-
base_db: BaseDB,
352+
base_db: BaseAtomicDB,
353353
genesis_params: Dict[str, HeaderParams],
354354
genesis_state: AccountState=None) -> 'BaseChain':
355355
"""
@@ -389,7 +389,7 @@ def from_genesis(cls,
389389

390390
@classmethod
391391
def from_genesis_header(cls,
392-
base_db: BaseDB,
392+
base_db: BaseAtomicDB,
393393
genesis_header: BlockHeader) -> 'BaseChain':
394394
"""
395395
Initializes the chain from the genesis header.
@@ -824,7 +824,7 @@ def _extract_uncle_hashes(blocks):
824824
class MiningChain(Chain):
825825
header = None # type: BlockHeader
826826

827-
def __init__(self, base_db: BaseDB, header: BlockHeader=None) -> None:
827+
def __init__(self, base_db: BaseAtomicDB, header: BlockHeader=None) -> None:
828828
super().__init__(base_db)
829829
self.header = self.ensure_header(header)
830830

eth/db/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import_string,
99
)
1010
from eth.db.backends.base import (
11-
BaseDB
11+
BaseAtomicDB,
1212
)
1313

14-
DEFAULT_DB_BACKEND = 'eth.db.backends.memory.MemoryDB'
14+
DEFAULT_DB_BACKEND = 'eth.db.atomic.AtomicDB'
1515

1616

17-
def get_db_backend_class(import_path: str = None) -> Type[BaseDB]:
17+
def get_db_backend_class(import_path: str = None) -> Type[BaseAtomicDB]:
1818
if import_path is None:
1919
import_path = os.environ.get(
2020
'CHAIN_DB_BACKEND_CLASS',
@@ -23,6 +23,6 @@ def get_db_backend_class(import_path: str = None) -> Type[BaseDB]:
2323
return import_string(import_path)
2424

2525

26-
def get_db_backend(import_path: str = None, **init_kwargs: Any) -> BaseDB:
26+
def get_db_backend(import_path: str = None, **init_kwargs: Any) -> BaseAtomicDB:
2727
backend_class = get_db_backend_class(import_path)
2828
return backend_class(**init_kwargs)

eth/db/chain.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
)
4242
from eth.db.header import BaseHeaderDB, HeaderDB
4343
from eth.db.backends.base import (
44-
BaseDB
44+
BaseAtomicDB
4545
)
4646
from eth.db.schema import SchemaV1
4747
from eth.rlp.headers import (
@@ -74,10 +74,10 @@ class TransactionKey(rlp.Serializable):
7474

7575

7676
class BaseChainDB(BaseHeaderDB):
77-
db = None # type: BaseDB
77+
db = None # type: BaseAtomicDB
7878

7979
@abstractmethod
80-
def __init__(self, db: BaseDB) -> None:
80+
def __init__(self, db: BaseAtomicDB) -> None:
8181
raise NotImplementedError("ChainDB classes must implement this method")
8282

8383
#
@@ -161,7 +161,7 @@ def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None:
161161

162162

163163
class ChainDB(HeaderDB, BaseChainDB):
164-
def __init__(self, db: BaseDB) -> None:
164+
def __init__(self, db: BaseAtomicDB) -> None:
165165
self.db = db
166166

167167
#

eth/db/header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
HeaderNotFound,
2929
ParentNotFound,
3030
)
31-
from eth.db import BaseDB
31+
from eth.db.backends.base import BaseDB
3232
from eth.db.schema import SchemaV1
3333
from eth.rlp.headers import BlockHeader
3434
from eth.validation import (

eth/tools/builder/chain/builders.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
MiningChain,
2121
)
2222
from eth.db.backends.memory import MemoryDB
23+
from eth.db.atomic import AtomicDB
2324
from eth.validation import (
2425
validate_vm_configuration,
2526
)
@@ -318,7 +319,7 @@ def genesis(chain_class, db=None, params=None, state=None):
318319
genesis_params = merge(genesis_params_defaults, params)
319320

320321
if db is None:
321-
base_db = MemoryDB()
322+
base_db = AtomicDB()
322323
else:
323324
base_db = db
324325

@@ -383,10 +384,14 @@ def copy(chain):
383384
if not isinstance(chain, MiningChain):
384385
raise ValidationError("`at_block_number` may only be used with 'MiningChain")
385386
base_db = chain.chaindb.db
386-
if not isinstance(base_db, MemoryDB):
387+
if not isinstance(base_db, AtomicDB):
387388
raise ValidationError("Unsupported database type: {0}".format(type(base_db)))
388389

389-
db = MemoryDB(base_db.kv_store.copy())
390+
if isinstance(base_db.wrapped_db, MemoryDB):
391+
db = AtomicDB(MemoryDB(base_db.wrapped_db.kv_store.copy()))
392+
else:
393+
raise ValidationError("Unsupported wrapped database: {0}".format(type(base_db.wrapped_db)))
394+
390395
chain_copy = type(chain)(db, chain.header)
391396
return chain_copy
392397

eth/tools/fixtures/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from eth import MainnetChain
12-
from eth.db.backends.memory import MemoryDB
12+
from eth.db.atomic import AtomicDB
1313
from eth.utils.state import (
1414
diff_account_db,
1515
)
@@ -147,7 +147,7 @@ def genesis_params_from_fixture(fixture):
147147

148148

149149
def new_chain_from_fixture(fixture):
150-
base_db = MemoryDB()
150+
base_db = AtomicDB()
151151

152152
vm_config = chain_vm_configuration(fixture)
153153

scripts/benchmark/utils/chain_plumbing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from eth.chains.mainnet import (
3232
BaseMainnetChain,
3333
)
34-
from eth.db.backends.memory import (
35-
MemoryDB
34+
from eth.db.atomic import (
35+
AtomicDB,
3636
)
3737

3838
AddressSetup = NamedTuple('AddressSetup', [
@@ -83,7 +83,7 @@ def genesis_state(setup: Iterable[AddressSetup]) -> Any:
8383

8484

8585
def chain_without_pow(
86-
base_db: MemoryDB,
86+
base_db: AtomicDB,
8787
vm: Type[BaseVM],
8888
genesis_params: Any,
8989
genesis_state: Any) -> MiningChain:
@@ -101,7 +101,7 @@ def chain_without_pow(
101101

102102
def get_chain(vm: Type[BaseVM]) -> MiningChain:
103103
return chain_without_pow(
104-
MemoryDB(),
104+
AtomicDB(),
105105
vm,
106106
GENESIS_PARAMS,
107107
genesis_state([

0 commit comments

Comments
 (0)