Skip to content

Commit 3fc3790

Browse files
committed
refactors
1 parent b4aa281 commit 3fc3790

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

eth/beacon/state_machines/base.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ class BaseBeaconStateMachine(Configurable, ABC):
3939
config = None # type: BeaconConfig
4040

4141
block = None # type: BaseBeaconBlock
42-
block_class = None # type: Type[BaseBeaconBlock]
4342

43+
block_class = None # type: Type[BaseBeaconBlock]
4444
crystallized_state_class = None # type: Type[CrystallizedState]
45-
4645
active_state_class = None # type: Type[ActiveState]
4746

47+
# TODO: Add abstractmethods
48+
4849

4950
class BeaconStateMachine(BaseBeaconStateMachine):
5051
"""
@@ -74,7 +75,7 @@ def logger(self):
7475
@classmethod
7576
def get_block_class(cls) -> Type[BaseBeaconBlock]:
7677
"""
77-
Return the :class:`~eth.beacon.types.blocks.BeaconBlock` class that this
78+
Returns the :class:`~eth.beacon.types.blocks.BeaconBlock` class that this
7879
StateMachine uses for blocks.
7980
"""
8081
if cls.block_class is None:
@@ -83,25 +84,30 @@ def get_block_class(cls) -> Type[BaseBeaconBlock]:
8384
return cls.block_class
8485

8586
@classmethod
86-
@functools.lru_cache(maxsize=32)
8787
@to_tuple
88-
def get_prev_blocks_to_slot(cls,
89-
last_block_hash: Hash32,
90-
chaindb: BaseBeaconChainDB,
91-
depth: int,
92-
start_slot: int) -> Iterable[BaseBeaconBlock]:
88+
def get_prev_blocks(cls,
89+
last_block_hash: Hash32,
90+
chaindb: BaseBeaconChainDB,
91+
searching_depth: int,
92+
target_slot_number: int) -> Iterable[BaseBeaconBlock]:
93+
"""
94+
Returns the previous blocks.
95+
96+
Since the slot numbers might be not continuous. The searching depth is ``searching_depth``
97+
and the target slot depth is ``target_slot_number``.
98+
"""
9399
if last_block_hash == GENESIS_PARENT_HASH:
94100
return
95101

96102
block = chaindb.get_block_by_hash(last_block_hash)
97103

98-
for _ in range(depth):
104+
for _ in range(searching_depth):
99105
yield block
100106
try:
101107
block = chaindb.get_block_by_hash(block.parent_hash)
102108
except (IndexError, BlockNotFound):
103109
break
104-
if block.slot_number < start_slot:
110+
if block.slot_number < target_slot_number:
105111
break
106112

107113
@property
@@ -127,7 +133,7 @@ def crystallized_state(self) -> CrystallizedState:
127133
@classmethod
128134
def get_crystallized_state_class(cls) -> Type[CrystallizedState]:
129135
"""
130-
Return the :class:`~eth.beacon.types.crystallized_states.CrystallizedState` class that this
136+
Returns the :class:`~eth.beacon.types.crystallized_states.CrystallizedState` class that this
131137
StateMachine uses for crystallized_state.
132138
"""
133139
if cls.crystallized_state_class is None:
@@ -144,38 +150,44 @@ def active_state(self) -> ActiveState:
144150
Returns latest active state.
145151
146152
It was backed up per cycle. The latest ActiveState could be reproduced by
147-
``old_active_state`` and recent blocks.
153+
``backup_active_state`` and recent blocks.
148154
149155
NOTE: The following logic in beacon chain spec will be changed with the current spec.
150156
"""
151157
if self._active_state is None:
152158
# Reproduce ActiveState
153-
old_active_state_root = self.chaindb.get_active_state_root_by_crystallized(
159+
backup_active_state_root = self.chaindb.get_active_state_root_by_crystallized(
154160
self.crystallized_state.hash
155161
)
156-
old_active_state = self.chaindb.get_active_state_by_root(old_active_state_root)
157-
old_active_state_slot = self.crystallized_state.last_state_recalc
158-
159-
depth = self.config.CYCLE_LENGTH * 2
160-
blocks = self.get_prev_blocks_to_slot(
161-
self.parent_block.hash,
162-
self.chaindb,
163-
depth,
164-
start_slot=old_active_state_slot
165-
)
166-
blocks = blocks[::-1]
167-
168-
self._active_state = self.get_active_state_class().from_old_active_state_and_blocks(
169-
old_active_state,
170-
blocks,
171-
)
162+
backup_active_state = self.chaindb.get_active_state_by_root(backup_active_state_root)
163+
backup_active_state_slot = self.crystallized_state.last_state_recalc
164+
165+
if backup_active_state_root == self.parent_block.active_state_root:
166+
# The backup ActiveState matches current block.
167+
self._active_state = backup_active_state
168+
else:
169+
# Get recent blocks after last ActiveState backup.
170+
searching_depth = self.config.CYCLE_LENGTH * 2
171+
blocks = self.get_prev_blocks(
172+
last_block_hash=self.parent_block.hash,
173+
chaindb=self.chaindb,
174+
searching_depth=searching_depth,
175+
target_slot_number=backup_active_state_slot
176+
)
177+
blocks = blocks[::-1]
178+
179+
self._active_state = self.get_active_state_class(
180+
).from_backup_active_state_and_blocks(
181+
backup_active_state,
182+
blocks,
183+
)
172184

173185
return self._active_state
174186

175187
@classmethod
176188
def get_active_state_class(cls) -> Type[ActiveState]:
177189
"""
178-
Return the :class:`~eth.beacon.types.active_states.ActiveState` class that this
190+
Returns the :class:`~eth.beacon.types.active_states.ActiveState` class that this
179191
StateMachine uses for crystallized_state.
180192
"""
181193
if cls.active_state_class is None:

eth/beacon/state_machines/forks/serenity/active_states.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
)
1111

1212
if TYPE_CHECKING:
13-
from eth.beacon.types.blocks import BaseBeaconBlock
13+
from eth.beacon.types.blocks import BaseBeaconBlock # noqa: F401
1414

1515

1616
class SerenityActiveState(ActiveState):
1717
@classmethod
18-
def from_old_active_state_and_blocks(cls,
19-
old_active_state: ActiveState,
20-
blocks: Sequence['BaseBlock']):
21-
recent_block_hashes = old_active_state.recent_block_hashes
22-
pending_attestations = old_active_state.pending_attestations
18+
def from_backup_active_state_and_blocks(cls,
19+
backup_active_state: ActiveState,
20+
blocks: Sequence['BaseBeaconBlock']) -> ActiveState:
21+
recent_block_hashes = backup_active_state.recent_block_hashes
22+
pending_attestations = backup_active_state.pending_attestations
2323

2424
index = 0
2525
while index < len(blocks) - 1:

tests/beacon/types/test_crystallized_state.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def test_num_crosslink_records(expected,
8585
(20),
8686
]
8787
)
88-
def test_total_deposits(num_active_validators, deposit_size, default_end_dynasty, empty_crystallized_state):
88+
def test_total_deposits(num_active_validators,
89+
deposit_size,
90+
default_end_dynasty,
91+
empty_crystallized_state):
8992
start_dynasty = 10
9093
active_validators = [
9194
mock_validator_record(

0 commit comments

Comments
 (0)