Skip to content

Commit ea4f30b

Browse files
committed
Add compute_cycle_transitions and Rename total_deposits to total_balance as spec
1 parent e4a203a commit ea4f30b

File tree

4 files changed

+102
-22
lines changed

4 files changed

+102
-22
lines changed

eth/beacon/state_machines/base.py

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
to_tuple,
1616
)
1717

18-
1918
from eth.constants import (
2019
GENESIS_PARENT_HASH,
2120
)
@@ -44,7 +43,9 @@
4443
from eth.beacon.types.active_states import ActiveState
4544
from eth.beacon.types.attestation_records import AttestationRecord # noqa: F401
4645
from eth.beacon.types.blocks import BaseBeaconBlock
46+
from eth.beacon.types.crosslink_records import CrosslinkRecord # noqa: F401
4747
from eth.beacon.types.crystallized_states import CrystallizedState
48+
from eth.beacon.types.validator_records import ValidatorRecord # noqa: F401
4849
from eth.beacon.state_machines.configs import BeaconConfig # noqa: F401
4950

5051
from .validation import (
@@ -272,16 +273,16 @@ def import_block(
272273

273274
self.block = processing_block
274275
self._update_the_states(processed_crystallized_state, processed_active_state)
276+
275277
# TODO: persist states in BeaconChain if needed
276278

277279
return self.block, self.crystallized_state, self.active_state
278280

279281
#
280282
# Process block APIs
281283
#
282-
@classmethod
283284
def process_block(
284-
cls,
285+
self,
285286
crystallized_state: CrystallizedState,
286287
active_state: ActiveState,
287288
block: BaseBeaconBlock,
@@ -293,7 +294,7 @@ def process_block(
293294
Process ``block`` and return the new crystallized state and active state.
294295
"""
295296
# Process per block state changes (ActiveState)
296-
processing_active_state = cls.compute_per_block_transition(
297+
processing_active_state = self.compute_per_block_transition(
297298
crystallized_state,
298299
active_state,
299300
block,
@@ -303,17 +304,18 @@ def process_block(
303304
)
304305

305306
# Process per cycle state changes (CrystallizedState and ActiveState)
306-
processed_crystallized_state, processed_active_state = cls.compute_cycle_transitions(
307+
processed_crystallized_state, processed_active_state = self.compute_cycle_transitions(
307308
crystallized_state,
308309
processing_active_state,
310+
block,
311+
config,
309312
)
310313

311314
# Return the copy
312315
result_block = block.copy()
313316
return result_block, processed_crystallized_state, processed_active_state
314317

315-
@classmethod
316-
def compute_per_block_transition(cls,
318+
def compute_per_block_transition(self,
317319
crystallized_state: CrystallizedState,
318320
active_state: ActiveState,
319321
block: BaseBeaconBlock,
@@ -323,7 +325,6 @@ def compute_per_block_transition(cls,
323325
"""
324326
Process ``block`` and return the new ActiveState.
325327
326-
327328
TODO: It doesn't match the latest spec.
328329
There will be more fields need to be updated in ActiveState.
329330
"""
@@ -344,7 +345,8 @@ def compute_per_block_transition(cls,
344345
)
345346

346347
# TODO: to implement the RANDAO reveal validation.
347-
cls.validate_randao_reveal()
348+
self.validate_randao_reveal()
349+
348350
for attestation in block.attestations:
349351
validate_attestation(
350352
block,
@@ -364,14 +366,92 @@ def compute_per_block_transition(cls,
364366
),
365367
)
366368

367-
@classmethod
368369
def compute_cycle_transitions(
369-
cls,
370+
self,
370371
crystallized_state: CrystallizedState,
371-
active_state: ActiveState) -> Tuple[CrystallizedState, ActiveState]:
372-
# TODO: it's a stub
372+
active_state: ActiveState,
373+
block: BaseBeaconBlock,
374+
config: BeaconConfig) -> Tuple[CrystallizedState, ActiveState]:
375+
"""
376+
Compute the cycle transitions and return processed CrystallizedState and ActiveState.
377+
"""
378+
while block.slot_number >= crystallized_state.last_state_recalc + config.CYCLE_LENGTH:
379+
crystallized_state, active_state = self.compute_per_cycle_transition(
380+
crystallized_state,
381+
active_state,
382+
block,
383+
config,
384+
)
385+
386+
if self.ready_for_dynasty_transition(crystallized_state, block, config):
387+
crystallized_state = self.compute_dynasty_transition(
388+
crystallized_state,
389+
block,
390+
config
391+
)
392+
373393
return crystallized_state, active_state
374394

395+
def compute_per_cycle_transition(
396+
self,
397+
crystallized_state: CrystallizedState,
398+
active_state: ActiveState,
399+
block: BaseBeaconBlock,
400+
config: BeaconConfig) -> Tuple[CrystallizedState, ActiveState]:
401+
"""
402+
Initialize a new cycle.
403+
"""
404+
# TODO
405+
return crystallized_state, active_state
406+
407+
#
408+
# Crosslinks
409+
#
410+
def compute_crosslinks(self,
411+
crystallized_state: CrystallizedState,
412+
active_state: ActiveState,
413+
block: BaseBeaconBlock,
414+
config: BeaconConfig) -> Tuple['CrosslinkRecord', ...]:
415+
# TODO
416+
return ()
417+
418+
#
419+
# Rewards and penalties
420+
#
421+
def apply_rewards_and_penalties(self,
422+
crystallized_state: CrystallizedState,
423+
active_state: ActiveState,
424+
block: BaseBeaconBlock,
425+
config: BeaconConfig) -> Tuple['ValidatorRecord', ...]:
426+
"""
427+
Apply the rewards and penalties to the validators and return the updated ValidatorRecords.
428+
"""
429+
# TODO
430+
return ()
431+
432+
#
433+
# Dynasty
434+
#
435+
def ready_for_dynasty_transition(self,
436+
crystallized_state: CrystallizedState,
437+
block: BaseBeaconBlock,
438+
config: BeaconConfig) -> bool:
439+
"""
440+
Check if it's ready for dynasty transition.
441+
"""
442+
# TODO
443+
return False
444+
445+
def compute_dynasty_transition(self,
446+
crystallized_state: CrystallizedState,
447+
block: BaseBeaconBlock,
448+
config: BeaconConfig) -> CrystallizedState:
449+
"""
450+
Compute the dynasty transition.
451+
"""
452+
# TODO
453+
return crystallized_state
454+
375455
#
376456
#
377457
# Proposer APIs

eth/beacon/types/crystallized_states.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def active_validator_indices(self) -> Tuple[int]:
106106
)
107107

108108
@property
109-
def total_deposits(self) -> int:
109+
def total_balance(self) -> int:
110110
return sum(
111111
self.validators[index].balance
112112
for index in self.active_validator_indices

tests/beacon/test_genesis_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_get_genesis_crystallized_state(genesis_validators,
2828
shard_count,
2929
)
3030
len_shard_and_committee_for_slots = cycle_length * 2
31-
total_deposits = deposit_size * len(genesis_validators)
31+
total_balance = deposit_size * len(genesis_validators)
3232

3333
assert crystallized_state.validators == genesis_validators
3434
assert crystallized_state.last_state_recalc == 0
@@ -43,7 +43,7 @@ def test_get_genesis_crystallized_state(genesis_validators,
4343
assert crosslink.hash == ZERO_HASH32
4444
assert crosslink.slot == 0
4545
assert crosslink.dynasty == 0
46-
assert crystallized_state.total_deposits == total_deposits
46+
assert crystallized_state.total_balance == total_balance
4747
assert crystallized_state.dynasty_seed == init_shuffling_seed
4848
assert crystallized_state.dynasty_start == 0
4949

tests/beacon/types/test_crystallized_state.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def test_num_crosslink_records(expected,
8585
(20),
8686
]
8787
)
88-
def test_total_deposits(num_active_validators,
89-
deposit_size,
90-
default_end_dynasty,
91-
empty_crystallized_state):
88+
def test_total_balance(num_active_validators,
89+
deposit_size,
90+
default_end_dynasty,
91+
empty_crystallized_state):
9292
start_dynasty = 10
9393
active_validators = [
9494
mock_validator_record(
@@ -115,8 +115,8 @@ def test_total_deposits(num_active_validators,
115115

116116
assert len(crystallized_state.active_validator_indices) == len(active_validators)
117117

118-
expected_total_deposits = deposit_size * num_active_validators
119-
assert crystallized_state.total_deposits == expected_total_deposits
118+
expected_total_balance = deposit_size * num_active_validators
119+
assert crystallized_state.total_balance == expected_total_balance
120120

121121

122122
def test_hash(sample_crystallized_state_params):

0 commit comments

Comments
 (0)