Skip to content

Commit 159d24f

Browse files
Tidy up with Eth2GenesisConfig (ethereum#635)
* don't get genesis from constants cache genesis_root request * rename genesis_hash -> genesis_root * Use Eth2GenesisConfig * dummy * fix lint * fix plugin test * PR feedback: type hint * PR feedback: Fix test fixtures
1 parent c2daa13 commit 159d24f

File tree

24 files changed

+181
-121
lines changed

24 files changed

+181
-121
lines changed

eth2/beacon/chains/base.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
from eth2.beacon.validation import (
5454
validate_slot,
5555
)
56-
from eth2.configs import Eth2Config
56+
from eth2.configs import (
57+
Eth2GenesisConfig,
58+
)
5759

5860
if TYPE_CHECKING:
5961
from eth2.beacon.state_machines.base import ( # noqa: F401
@@ -87,7 +89,7 @@ def from_genesis(cls,
8789
base_db: BaseAtomicDB,
8890
genesis_state: BeaconState,
8991
genesis_block: BaseBeaconBlock,
90-
config: Eth2Config) -> 'BaseBeaconChain':
92+
genesis_config: Eth2GenesisConfig) -> 'BaseBeaconChain':
9193
pass
9294

9395
#
@@ -111,6 +113,11 @@ def get_state_machine_class_for_block_slot(
111113
slot: Slot) -> Type['BaseBeaconStateMachine']:
112114
pass
113115

116+
@classmethod
117+
@abstractmethod
118+
def get_genesis_state_machine_class(self) -> Type['BaseBeaconStateMachine']:
119+
pass
120+
114121
#
115122
# Block API
116123
#
@@ -172,7 +179,7 @@ class BeaconChain(BaseBeaconChain):
172179

173180
chaindb_class = BeaconChainDB # type: Type[BaseBeaconChainDB]
174181

175-
def __init__(self, base_db: BaseAtomicDB, config: Eth2Config) -> None:
182+
def __init__(self, base_db: BaseAtomicDB, genesis_config: Eth2GenesisConfig) -> None:
176183
if not self.sm_configuration:
177184
raise ValueError(
178185
"The Chain class cannot be instantiated with an empty `sm_configuration`"
@@ -182,7 +189,7 @@ def __init__(self, base_db: BaseAtomicDB, config: Eth2Config) -> None:
182189
# validate_sm_configuration(self.sm_configuration)
183190
pass
184191

185-
self.chaindb = self.get_chaindb_class()(base_db, config)
192+
self.chaindb = self.get_chaindb_class()(base_db, genesis_config)
186193

187194
#
188195
# Helpers
@@ -201,7 +208,7 @@ def from_genesis(cls,
201208
base_db: BaseAtomicDB,
202209
genesis_state: BeaconState,
203210
genesis_block: BaseBeaconBlock,
204-
config: Eth2Config) -> 'BaseBeaconChain':
211+
genesis_config: Eth2GenesisConfig) -> 'BaseBeaconChain':
205212
"""
206213
Initialize the ``BeaconChain`` from a genesis state.
207214
"""
@@ -214,21 +221,21 @@ def from_genesis(cls,
214221
)
215222
)
216223

217-
chaindb = cls.get_chaindb_class()(db=base_db, config=config)
224+
chaindb = cls.get_chaindb_class()(db=base_db, genesis_config=genesis_config)
218225
chaindb.persist_state(genesis_state)
219-
return cls._from_genesis_block(base_db, genesis_block, config)
226+
return cls._from_genesis_block(base_db, genesis_block, genesis_config)
220227

221228
@classmethod
222229
def _from_genesis_block(cls,
223230
base_db: BaseAtomicDB,
224231
genesis_block: BaseBeaconBlock,
225-
config: Eth2Config) -> 'BaseBeaconChain':
232+
genesis_config: Eth2GenesisConfig) -> 'BaseBeaconChain':
226233
"""
227234
Initialize the ``BeaconChain`` from the genesis block.
228235
"""
229-
chaindb = cls.get_chaindb_class()(db=base_db, config=config)
236+
chaindb = cls.get_chaindb_class()(db=base_db, genesis_config=genesis_config)
230237
chaindb.persist_block(genesis_block, genesis_block.__class__)
231-
return cls(base_db, config)
238+
return cls(base_db, genesis_config)
232239

233240
#
234241
# StateMachine API
@@ -268,6 +275,10 @@ def get_state_machine(self, at_block: BaseBeaconBlock=None) -> 'BaseBeaconStateM
268275
block=block,
269276
)
270277

278+
@classmethod
279+
def get_genesis_state_machine_class(cls) -> Type['BaseBeaconStateMachine']:
280+
return cls.sm_configuration[0][1]
281+
271282
#
272283
# Block API
273284
#

eth2/beacon/db/chain.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
from eth.validation import (
4040
validate_word,
4141
)
42-
43-
from eth2.configs import Eth2Config
4442
from eth2.beacon.helpers import (
4543
slot_to_epoch,
4644
)
@@ -63,12 +61,16 @@
6361
)
6462
from eth2.beacon.db.schema import SchemaV1
6563

64+
from eth2.configs import (
65+
Eth2GenesisConfig,
66+
)
67+
6668

6769
class BaseBeaconChainDB(ABC):
6870
db = None # type: BaseAtomicDB
6971

7072
@abstractmethod
71-
def __init__(self, db: BaseAtomicDB, config: Eth2Config) -> None:
73+
def __init__(self, db: BaseAtomicDB, genesis_config: Eth2GenesisConfig) -> None:
7274
pass
7375

7476
#
@@ -160,9 +162,9 @@ def get(self, key: bytes) -> bytes:
160162

161163

162164
class BeaconChainDB(BaseBeaconChainDB):
163-
def __init__(self, db: BaseAtomicDB, config: Eth2Config) -> None:
165+
def __init__(self, db: BaseAtomicDB, genesis_config: Eth2GenesisConfig) -> None:
164166
self.db = db
165-
self.config = config
167+
self.genesis_config = genesis_config
166168

167169
self._finalized_root = self._get_finalized_root_if_present(db)
168170
self._highest_justified_epoch = self._get_highest_justified_epoch(db)
@@ -177,9 +179,9 @@ def _get_highest_justified_epoch(self, db: BaseDB) -> Epoch:
177179
try:
178180
justified_head_root = self._get_justified_head_root(db)
179181
slot = self.get_slot_by_root(justified_head_root)
180-
return slot_to_epoch(slot, self.config.SLOTS_PER_EPOCH)
182+
return slot_to_epoch(slot, self.genesis_config.SLOTS_PER_EPOCH)
181183
except JustifiedHeadNotFound:
182-
return self.config.GENESIS_EPOCH
184+
return self.genesis_config.GENESIS_EPOCH
183185

184186
def persist_block(
185187
self,
@@ -708,7 +710,7 @@ def _handle_exceptional_justification_and_finality(self,
708710
"""
709711
genesis_root = genesis_block.signing_root
710712
self._update_finalized_head(genesis_root)
711-
self._update_justified_head(genesis_root, self.config.GENESIS_EPOCH)
713+
self._update_justified_head(genesis_root, self.genesis_config.GENESIS_EPOCH)
712714

713715
#
714716
# Raw Database API

eth2/beacon/state_machines/forks/xiao_long_bao/configs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
from eth2.beacon.state_machines.forks.serenity.configs import (
55
SERENITY_CONFIG,
66
)
7+
from eth2.beacon.typing import (
8+
Slot,
9+
)
710

811

12+
GENESIS_SLOT = Slot(0)
913
SLOTS_PER_EPOCH = 4
1014

1115
XIAO_LONG_BAO_CONFIG = SERENITY_CONFIG._replace(
16+
GENESIS_SLOT=GENESIS_SLOT,
1217
SLOTS_PER_EPOCH=SLOTS_PER_EPOCH,
13-
GENESIS_EPOCH=slot_to_epoch(SERENITY_CONFIG.GENESIS_SLOT, SLOTS_PER_EPOCH),
18+
GENESIS_EPOCH=slot_to_epoch(GENESIS_SLOT, SLOTS_PER_EPOCH),
1419
TARGET_COMMITTEE_SIZE=2,
1520
SHARD_COUNT=2,
1621
MIN_ATTESTATION_INCLUSION_DELAY=2,

eth2/configs.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@ def __init__(self, config: Eth2Config):
8686
self.ACTIVATION_EXIT_DELAY = config.ACTIVATION_EXIT_DELAY
8787
self.LATEST_ACTIVE_INDEX_ROOTS_LENGTH = config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH
8888
self.LATEST_RANDAO_MIXES_LENGTH = config.LATEST_RANDAO_MIXES_LENGTH
89+
90+
91+
class Eth2GenesisConfig:
92+
"""
93+
Genesis parameters that might lives in
94+
a state or a state machine config
95+
but is assumed unlikely to change between forks.
96+
Pass this to the chains, chain_db, or other objects that need them.
97+
"""
98+
99+
def __init__(self, config: Eth2Config) -> None:
100+
self.GENESIS_SLOT = config.GENESIS_SLOT
101+
self.GENESIS_EPOCH = config.GENESIS_EPOCH
102+
self.SECONDS_PER_SLOT = config.SECONDS_PER_SLOT
103+
self.SLOTS_PER_EPOCH = config.SLOTS_PER_EPOCH

tests/core/p2p-proto/bcc/helpers.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
async_passthrough,
4040
)
4141
from eth2.beacon.state_machines.forks.serenity import SERENITY_CONFIG
42+
from eth2.configs import (
43+
Eth2GenesisConfig,
44+
)
45+
46+
SERENITY_GENESIS_CONFIG = Eth2GenesisConfig(SERENITY_CONFIG)
4247

4348

4449
class FakeAsyncBeaconChainDB(BaseAsyncBeaconChainDB, BeaconChainDB):
@@ -59,9 +64,9 @@ class FakeAsyncBeaconChainDB(BaseAsyncBeaconChainDB, BeaconChainDB):
5964
coro_get = async_passthrough('get')
6065

6166

62-
def create_test_block(parent=None, **kwargs):
67+
def create_test_block(parent=None, genesis_config=SERENITY_GENESIS_CONFIG, **kwargs):
6368
defaults = {
64-
"slot": SERENITY_CONFIG.GENESIS_SLOT,
69+
"slot": genesis_config.GENESIS_SLOT,
6570
"previous_block_root": ZERO_HASH32,
6671
"state_root": ZERO_HASH32, # note: not the actual genesis state root
6772
"signature": EMPTY_SIGNATURE,
@@ -92,16 +97,16 @@ def create_branch(length, root=None, **start_kwargs):
9297
parent = child
9398

9499

95-
async def get_chain_db(blocks=(), config=SERENITY_CONFIG):
100+
async def get_chain_db(blocks=(), genesis_config=SERENITY_GENESIS_CONFIG):
96101
db = AtomicDB()
97-
chain_db = FakeAsyncBeaconChainDB(db=db, config=config)
102+
chain_db = FakeAsyncBeaconChainDB(db=db, genesis_config=genesis_config)
98103
await chain_db.coro_persist_block_chain(blocks, BeaconBlock)
99104
return chain_db
100105

101106

102-
async def get_genesis_chain_db(config=SERENITY_CONFIG):
103-
genesis = create_test_block()
104-
return await get_chain_db((genesis,), config=config)
107+
async def get_genesis_chain_db(genesis_config=SERENITY_GENESIS_CONFIG):
108+
genesis = create_test_block(genesis_config=genesis_config)
109+
return await get_chain_db((genesis,), genesis_config=genesis_config)
105110

106111

107112
async def _setup_alice_and_bob_factories(alice_chain_db, bob_chain_db):

tests/core/p2p-proto/bcc/test_handshake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def test_unidirectional_handshake():
7272

7373
assert msg["protocol_version"] == BCCProtocol.version
7474
assert msg["network_id"] == alice.context.network_id
75-
assert msg["genesis_hash"] == alice_genesis_root
75+
assert msg["genesis_root"] == alice_genesis_root
7676
assert msg["head_slot"] == alice_head_slot
7777

7878
await bob.process_sub_proto_handshake(cmd, msg)

tests/eth2/core/beacon/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from eth2.configs import (
1111
Eth2Config,
1212
CommitteeConfig,
13+
Eth2GenesisConfig,
1314
)
1415
from eth2.beacon.constants import (
1516
FAR_FUTURE_EPOCH,
@@ -50,6 +51,11 @@
5051
mock_validator,
5152
)
5253

54+
from eth2.beacon.db.chain import (
55+
BeaconChainDB,
56+
)
57+
58+
5359
DEFAULT_SHUFFLING_SEED = b'\00' * 32
5460
DEFAULT_RANDAO = b'\45' * 32
5561
DEFAULT_NUM_VALIDATORS = 40
@@ -791,9 +797,20 @@ def fixture_sm_class(config):
791797
)
792798

793799

800+
@pytest.fixture
801+
def genesis_config(config):
802+
return Eth2GenesisConfig(config)
803+
804+
805+
@pytest.fixture
806+
def chaindb(base_db, genesis_config):
807+
return BeaconChainDB(base_db, genesis_config)
808+
794809
#
795810
# CommitteeConfig
796811
#
812+
813+
797814
@pytest.fixture
798815
def committee_config(config):
799816
return CommitteeConfig(config)

tests/eth2/core/beacon/db/test_beacon_chaindb.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
validate_ssz_equal,
2222
)
2323

24-
from eth2.beacon.db.chain import (
25-
BeaconChainDB,
26-
)
2724
from eth2.beacon.db.exceptions import (
2825
FinalizedHeadNotFound,
2926
JustifiedHeadNotFound,
@@ -35,11 +32,6 @@
3532
from eth2.beacon.types.states import BeaconState
3633

3734

38-
@pytest.fixture
39-
def chaindb(base_db, config):
40-
return BeaconChainDB(base_db, config)
41-
42-
4335
@pytest.fixture
4436
def chaindb_at_genesis(chaindb, genesis_state, genesis_block):
4537
chaindb.persist_state(genesis_state)

tests/eth2/core/beacon/state_machines/forks/test_serenity_operation_processing.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from eth2.beacon.committee_helpers import (
1111
get_beacon_proposer_index,
1212
)
13-
from eth2.beacon.db.chain import BeaconChainDB
1413
from eth2.beacon.helpers import (
1514
get_epoch_start_slot,
1615
)
@@ -41,7 +40,7 @@ def test_process_max_attestations(genesis_state,
4140
config,
4241
keymap,
4342
fixture_sm_class,
44-
base_db):
43+
chaindb):
4544
attestation_slot = config.GENESIS_SLOT
4645
current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY
4746
state = genesis_state.copy(
@@ -52,7 +51,7 @@ def test_process_max_attestations(genesis_state,
5251
state=state,
5352
config=config,
5453
state_machine=fixture_sm_class(
55-
BeaconChainDB(base_db, config),
54+
chaindb,
5655
genesis_block,
5756
),
5857
attestation_slot=attestation_slot,
@@ -255,7 +254,7 @@ def test_process_attestations(genesis_state,
255254
config,
256255
keymap,
257256
fixture_sm_class,
258-
base_db,
257+
chaindb,
259258
success):
260259

261260
attestation_slot = 0
@@ -268,7 +267,7 @@ def test_process_attestations(genesis_state,
268267
state=state,
269268
config=config,
270269
state_machine=fixture_sm_class(
271-
BeaconChainDB(base_db, config),
270+
chaindb,
272271
genesis_block,
273272
),
274273
attestation_slot=attestation_slot,

tests/eth2/core/beacon/state_machines/test_state_transition.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
from eth2.beacon.db.chain import BeaconChainDB
43
from eth2.beacon.state_machines.forks.serenity.blocks import (
54
SerenityBeaconBlock,
65
)
@@ -46,14 +45,13 @@
4645
(16, 4, 1, 2, 2, 31, 8),
4746
]
4847
)
49-
def test_per_slot_transition(base_db,
48+
def test_per_slot_transition(chaindb,
5049
genesis_block,
5150
genesis_state,
5251
fixture_sm_class,
5352
config,
5453
state_slot,
5554
keymap):
56-
chaindb = BeaconChainDB(base_db, config)
5755
chaindb.persist_block(genesis_block, SerenityBeaconBlock)
5856
chaindb.persist_state(genesis_state)
5957

0 commit comments

Comments
 (0)