15
15
to_tuple ,
16
16
)
17
17
18
-
19
18
from eth .constants import (
20
19
GENESIS_PARENT_HASH ,
21
20
)
44
43
from eth .beacon .types .active_states import ActiveState
45
44
from eth .beacon .types .attestation_records import AttestationRecord # noqa: F401
46
45
from eth .beacon .types .blocks import BaseBeaconBlock
46
+ from eth .beacon .types .crosslink_records import CrosslinkRecord # noqa: F401
47
47
from eth .beacon .types .crystallized_states import CrystallizedState
48
+ from eth .beacon .types .validator_records import ValidatorRecord # noqa: F401
48
49
from eth .beacon .state_machines .configs import BeaconConfig # noqa: F401
49
50
50
51
from .validation import (
@@ -272,16 +273,16 @@ def import_block(
272
273
273
274
self .block = processing_block
274
275
self ._update_the_states (processed_crystallized_state , processed_active_state )
276
+
275
277
# TODO: persist states in BeaconChain if needed
276
278
277
279
return self .block , self .crystallized_state , self .active_state
278
280
279
281
#
280
282
# Process block APIs
281
283
#
282
- @classmethod
283
284
def process_block (
284
- cls ,
285
+ self ,
285
286
crystallized_state : CrystallizedState ,
286
287
active_state : ActiveState ,
287
288
block : BaseBeaconBlock ,
@@ -293,7 +294,7 @@ def process_block(
293
294
Process ``block`` and return the new crystallized state and active state.
294
295
"""
295
296
# Process per block state changes (ActiveState)
296
- processing_active_state = cls .compute_per_block_transition (
297
+ processing_active_state = self .compute_per_block_transition (
297
298
crystallized_state ,
298
299
active_state ,
299
300
block ,
@@ -303,17 +304,18 @@ def process_block(
303
304
)
304
305
305
306
# 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 (
307
308
crystallized_state ,
308
309
processing_active_state ,
310
+ block ,
311
+ config ,
309
312
)
310
313
311
314
# Return the copy
312
315
result_block = block .copy ()
313
316
return result_block , processed_crystallized_state , processed_active_state
314
317
315
- @classmethod
316
- def compute_per_block_transition (cls ,
318
+ def compute_per_block_transition (self ,
317
319
crystallized_state : CrystallizedState ,
318
320
active_state : ActiveState ,
319
321
block : BaseBeaconBlock ,
@@ -323,7 +325,6 @@ def compute_per_block_transition(cls,
323
325
"""
324
326
Process ``block`` and return the new ActiveState.
325
327
326
-
327
328
TODO: It doesn't match the latest spec.
328
329
There will be more fields need to be updated in ActiveState.
329
330
"""
@@ -344,7 +345,8 @@ def compute_per_block_transition(cls,
344
345
)
345
346
346
347
# TODO: to implement the RANDAO reveal validation.
347
- cls .validate_randao_reveal ()
348
+ self .validate_randao_reveal ()
349
+
348
350
for attestation in block .attestations :
349
351
validate_attestation (
350
352
block ,
@@ -364,14 +366,92 @@ def compute_per_block_transition(cls,
364
366
),
365
367
)
366
368
367
- @classmethod
368
369
def compute_cycle_transitions (
369
- cls ,
370
+ self ,
370
371
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
+
373
393
return crystallized_state , active_state
374
394
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
+
375
455
#
376
456
#
377
457
# Proposer APIs
0 commit comments