1515 to_tuple ,
1616)
1717
18-
1918from eth .constants import (
2019 GENESIS_PARENT_HASH ,
2120)
4443from eth .beacon .types .active_states import ActiveState
4544from eth .beacon .types .attestation_records import AttestationRecord # noqa: F401
4645from eth .beacon .types .blocks import BaseBeaconBlock
46+ from eth .beacon .types .crosslink_records import CrosslinkRecord # noqa: F401
4747from eth .beacon .types .crystallized_states import CrystallizedState
48+ from eth .beacon .types .validator_records import ValidatorRecord # noqa: F401
4849from eth .beacon .state_machines .configs import BeaconConfig # noqa: F401
4950
5051from .validation import (
@@ -259,7 +260,6 @@ def import_block(
259260 self .active_state ,
260261 block ,
261262 self .chaindb ,
262- self .config ,
263263 is_validating_signatures = True ,
264264 )
265265
@@ -272,10 +272,17 @@ def import_block(
272272
273273 self .block = processing_block
274274 self ._update_the_states (processed_crystallized_state , processed_active_state )
275+
275276 # TODO: persist states in BeaconChain if needed
276277
277278 return self .block , self .crystallized_state , self .active_state
278279
280+ def _update_the_states (self ,
281+ crystallized_state : CrystallizedState ,
282+ active_state : ActiveState ) -> None :
283+ self ._crytallized_state = crystallized_state
284+ self ._active_state = active_state
285+
279286 #
280287 # Process block APIs
281288 #
@@ -286,7 +293,6 @@ def process_block(
286293 active_state : ActiveState ,
287294 block : BaseBeaconBlock ,
288295 chaindb : BaseBeaconChainDB ,
289- config : BeaconConfig ,
290296 is_validating_signatures : bool = True
291297 ) -> Tuple [BaseBeaconBlock , CrystallizedState , ActiveState ]:
292298 """
@@ -298,14 +304,14 @@ def process_block(
298304 active_state ,
299305 block ,
300306 chaindb ,
301- config .CYCLE_LENGTH ,
302307 is_validating_signatures = is_validating_signatures ,
303308 )
304309
305310 # Process per cycle state changes (CrystallizedState and ActiveState)
306311 processed_crystallized_state , processed_active_state = cls .compute_cycle_transitions (
307312 crystallized_state ,
308313 processing_active_state ,
314+ block ,
309315 )
310316
311317 # Return the copy
@@ -318,12 +324,10 @@ def compute_per_block_transition(cls,
318324 active_state : ActiveState ,
319325 block : BaseBeaconBlock ,
320326 chaindb : BaseBeaconChainDB ,
321- cycle_length : int ,
322327 is_validating_signatures : bool = True ) -> ActiveState :
323328 """
324329 Process ``block`` and return the new ActiveState.
325330
326-
327331 TODO: It doesn't match the latest spec.
328332 There will be more fields need to be updated in ActiveState.
329333 """
@@ -340,11 +344,12 @@ def compute_per_block_transition(cls,
340344 crystallized_state ,
341345 block ,
342346 parent_block ,
343- cycle_length ,
347+ cls . config . CYCLE_LENGTH ,
344348 )
345349
346350 # TODO: to implement the RANDAO reveal validation.
347351 cls .validate_randao_reveal ()
352+
348353 for attestation in block .attestations :
349354 validate_attestation (
350355 block ,
@@ -353,7 +358,7 @@ def compute_per_block_transition(cls,
353358 recent_block_hashes ,
354359 attestation ,
355360 chaindb ,
356- cycle_length ,
361+ cls . config . CYCLE_LENGTH ,
357362 is_validating_signatures = is_validating_signatures ,
358363 )
359364
@@ -368,33 +373,112 @@ def compute_per_block_transition(cls,
368373 def compute_cycle_transitions (
369374 cls ,
370375 crystallized_state : CrystallizedState ,
371- active_state : ActiveState ) -> Tuple [CrystallizedState , ActiveState ]:
372- # TODO: it's a stub
376+ active_state : ActiveState ,
377+ block : BaseBeaconBlock ) -> Tuple [CrystallizedState , ActiveState ]:
378+ """
379+ Compute the cycle transitions and return processed CrystallizedState and ActiveState.
380+ """
381+ while block .slot_number >= crystallized_state .last_state_recalc + cls .config .CYCLE_LENGTH :
382+ crystallized_state , active_state = cls .compute_per_cycle_transition (
383+ crystallized_state ,
384+ active_state ,
385+ block ,
386+ )
387+
388+ if cls .ready_for_dynasty_transition (crystallized_state , block ):
389+ crystallized_state = cls .compute_dynasty_transition (
390+ crystallized_state ,
391+ block ,
392+ )
393+
394+ return crystallized_state , active_state
395+
396+ @classmethod
397+ def compute_per_cycle_transition (
398+ cls ,
399+ crystallized_state : CrystallizedState ,
400+ active_state : ActiveState ,
401+ block : BaseBeaconBlock ) -> Tuple [CrystallizedState , ActiveState ]:
402+ """
403+ Initialize a new cycle.
404+ """
405+ # TODO: it's a STUB before we implement compute_per_cycle_transition
406+ crystallized_state = crystallized_state .copy (
407+ last_state_recalc = crystallized_state .last_state_recalc + cls .config .CYCLE_LENGTH
408+ )
409+
373410 return crystallized_state , active_state
374411
412+ #
413+ # Crosslinks
414+ #
415+ @classmethod
416+ def update_crosslinks (cls ,
417+ crystallized_state : CrystallizedState ,
418+ active_state : ActiveState ,
419+ block : BaseBeaconBlock ) -> Tuple ['CrosslinkRecord' , ...]:
420+ # TODO
421+ return ()
422+
423+ #
424+ # Rewards and penalties
425+ #
426+ @classmethod
427+ def apply_rewards_and_penalties (cls ,
428+ crystallized_state : CrystallizedState ,
429+ active_state : ActiveState ,
430+ block : BaseBeaconBlock ) -> Tuple ['ValidatorRecord' , ...]:
431+ """
432+ Apply the rewards and penalties to the validators and return the updated ValidatorRecords.
433+ """
434+ # TODO
435+ return ()
436+
437+ #
438+ # Dynasty
439+ #
440+ @classmethod
441+ def ready_for_dynasty_transition (cls ,
442+ crystallized_state : CrystallizedState ,
443+ block : BaseBeaconBlock ) -> bool :
444+ """
445+ Check if it's ready for dynasty transition.
446+ """
447+ # TODO
448+ return False
449+
450+ @classmethod
451+ def compute_dynasty_transition (cls ,
452+ crystallized_state : CrystallizedState ,
453+ block : BaseBeaconBlock ) -> CrystallizedState :
454+ """
455+ Compute the dynasty transition.
456+ """
457+ # TODO
458+ return crystallized_state
459+
375460 #
376461 #
377462 # Proposer APIs
378463 #
379464 #
465+ @classmethod
380466 def propose_block (
381- self ,
467+ cls ,
382468 crystallized_state : CrystallizedState ,
383469 active_state : ActiveState ,
384470 block_proposal : 'BlockProposal' ,
385471 chaindb : BaseBeaconChainDB ,
386- config : BeaconConfig ,
387472 private_key : int
388473 ) -> Tuple [BaseBeaconBlock , CrystallizedState , ActiveState , 'AttestationRecord' ]:
389474 """
390475 Propose the given block.
391476 """
392- block , post_crystallized_state , post_active_state = self .process_block (
477+ block , post_crystallized_state , post_active_state = cls .process_block (
393478 crystallized_state ,
394479 active_state ,
395480 block_proposal .block ,
396481 chaindb ,
397- config ,
398482 is_validating_signatures = False ,
399483 )
400484
@@ -409,28 +493,21 @@ def propose_block(
409493 shard_block_hash = block_proposal .shard_block_hash ,
410494 )
411495
412- proposer_attestation = self .attest_proposed_block (
496+ proposer_attestation = cls .attest_proposed_block (
413497 post_crystallized_state ,
414498 post_active_state ,
415499 filled_block_proposal ,
416500 chaindb ,
417- config .CYCLE_LENGTH ,
418501 private_key ,
419502 )
420503 return post_block , post_crystallized_state , post_active_state , proposer_attestation
421504
422- def _update_the_states (self ,
423- crystallized_state : CrystallizedState ,
424- active_state : ActiveState ) -> None :
425- self ._crytallized_state = crystallized_state
426- self ._active_state = active_state
427-
428- def attest_proposed_block (self ,
505+ @classmethod
506+ def attest_proposed_block (cls ,
429507 post_crystallized_state : CrystallizedState ,
430508 post_active_state : ActiveState ,
431509 block_proposal : 'BlockProposal' ,
432510 chaindb : BaseBeaconChainDB ,
433- cycle_length : int ,
434511 private_key : int ) -> 'AttestationRecord' :
435512 """
436513 Return the initial attestation by the block proposer.
@@ -440,7 +517,7 @@ def attest_proposed_block(self,
440517 block_committees_info = get_block_committees_info (
441518 block_proposal .block ,
442519 post_crystallized_state ,
443- cycle_length ,
520+ cls . config . CYCLE_LENGTH ,
444521 )
445522 # Vote
446523 attester_bitfield = set_voted (
@@ -456,7 +533,7 @@ def attest_proposed_block(self,
456533 parent_hashes = get_hashes_to_sign (
457534 post_active_state .recent_block_hashes ,
458535 block_proposal .block ,
459- cycle_length ,
536+ cls . config . CYCLE_LENGTH ,
460537 )
461538
462539 message = create_signing_message (
@@ -471,7 +548,7 @@ def attest_proposed_block(self,
471548 private_key ,
472549 )
473550
474- return self .get_attestation_record_class ()(
551+ return cls .get_attestation_record_class ()(
475552 slot = block_proposal .block .slot_number ,
476553 shard_id = block_proposal .shard_id ,
477554 oblique_parent_hashes = (),
0 commit comments