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 (
@@ -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
0 commit comments