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 (
@@ -259,7 +260,6 @@ def import_block(
259
260
self .active_state ,
260
261
block ,
261
262
self .chaindb ,
262
- self .config ,
263
263
is_validating_signatures = True ,
264
264
)
265
265
@@ -272,10 +272,17 @@ def import_block(
272
272
273
273
self .block = processing_block
274
274
self ._update_the_states (processed_crystallized_state , processed_active_state )
275
+
275
276
# TODO: persist states in BeaconChain if needed
276
277
277
278
return self .block , self .crystallized_state , self .active_state
278
279
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
+
279
286
#
280
287
# Process block APIs
281
288
#
@@ -286,7 +293,6 @@ def process_block(
286
293
active_state : ActiveState ,
287
294
block : BaseBeaconBlock ,
288
295
chaindb : BaseBeaconChainDB ,
289
- config : BeaconConfig ,
290
296
is_validating_signatures : bool = True
291
297
) -> Tuple [BaseBeaconBlock , CrystallizedState , ActiveState ]:
292
298
"""
@@ -298,14 +304,14 @@ def process_block(
298
304
active_state ,
299
305
block ,
300
306
chaindb ,
301
- config .CYCLE_LENGTH ,
302
307
is_validating_signatures = is_validating_signatures ,
303
308
)
304
309
305
310
# Process per cycle state changes (CrystallizedState and ActiveState)
306
311
processed_crystallized_state , processed_active_state = cls .compute_cycle_transitions (
307
312
crystallized_state ,
308
313
processing_active_state ,
314
+ block ,
309
315
)
310
316
311
317
# Return the copy
@@ -318,12 +324,10 @@ def compute_per_block_transition(cls,
318
324
active_state : ActiveState ,
319
325
block : BaseBeaconBlock ,
320
326
chaindb : BaseBeaconChainDB ,
321
- cycle_length : int ,
322
327
is_validating_signatures : bool = True ) -> ActiveState :
323
328
"""
324
329
Process ``block`` and return the new ActiveState.
325
330
326
-
327
331
TODO: It doesn't match the latest spec.
328
332
There will be more fields need to be updated in ActiveState.
329
333
"""
@@ -340,11 +344,12 @@ def compute_per_block_transition(cls,
340
344
crystallized_state ,
341
345
block ,
342
346
parent_block ,
343
- cycle_length ,
347
+ cls . config . CYCLE_LENGTH ,
344
348
)
345
349
346
350
# TODO: to implement the RANDAO reveal validation.
347
351
cls .validate_randao_reveal ()
352
+
348
353
for attestation in block .attestations :
349
354
validate_attestation (
350
355
block ,
@@ -353,7 +358,7 @@ def compute_per_block_transition(cls,
353
358
recent_block_hashes ,
354
359
attestation ,
355
360
chaindb ,
356
- cycle_length ,
361
+ cls . config . CYCLE_LENGTH ,
357
362
is_validating_signatures = is_validating_signatures ,
358
363
)
359
364
@@ -368,33 +373,112 @@ def compute_per_block_transition(cls,
368
373
def compute_cycle_transitions (
369
374
cls ,
370
375
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
+
373
410
return crystallized_state , active_state
374
411
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
+
375
460
#
376
461
#
377
462
# Proposer APIs
378
463
#
379
464
#
465
+ @classmethod
380
466
def propose_block (
381
- self ,
467
+ cls ,
382
468
crystallized_state : CrystallizedState ,
383
469
active_state : ActiveState ,
384
470
block_proposal : 'BlockProposal' ,
385
471
chaindb : BaseBeaconChainDB ,
386
- config : BeaconConfig ,
387
472
private_key : int
388
473
) -> Tuple [BaseBeaconBlock , CrystallizedState , ActiveState , 'AttestationRecord' ]:
389
474
"""
390
475
Propose the given block.
391
476
"""
392
- block , post_crystallized_state , post_active_state = self .process_block (
477
+ block , post_crystallized_state , post_active_state = cls .process_block (
393
478
crystallized_state ,
394
479
active_state ,
395
480
block_proposal .block ,
396
481
chaindb ,
397
- config ,
398
482
is_validating_signatures = False ,
399
483
)
400
484
@@ -409,28 +493,21 @@ def propose_block(
409
493
shard_block_hash = block_proposal .shard_block_hash ,
410
494
)
411
495
412
- proposer_attestation = self .attest_proposed_block (
496
+ proposer_attestation = cls .attest_proposed_block (
413
497
post_crystallized_state ,
414
498
post_active_state ,
415
499
filled_block_proposal ,
416
500
chaindb ,
417
- config .CYCLE_LENGTH ,
418
501
private_key ,
419
502
)
420
503
return post_block , post_crystallized_state , post_active_state , proposer_attestation
421
504
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 ,
429
507
post_crystallized_state : CrystallizedState ,
430
508
post_active_state : ActiveState ,
431
509
block_proposal : 'BlockProposal' ,
432
510
chaindb : BaseBeaconChainDB ,
433
- cycle_length : int ,
434
511
private_key : int ) -> 'AttestationRecord' :
435
512
"""
436
513
Return the initial attestation by the block proposer.
@@ -440,7 +517,7 @@ def attest_proposed_block(self,
440
517
block_committees_info = get_block_committees_info (
441
518
block_proposal .block ,
442
519
post_crystallized_state ,
443
- cycle_length ,
520
+ cls . config . CYCLE_LENGTH ,
444
521
)
445
522
# Vote
446
523
attester_bitfield = set_voted (
@@ -456,7 +533,7 @@ def attest_proposed_block(self,
456
533
parent_hashes = get_hashes_to_sign (
457
534
post_active_state .recent_block_hashes ,
458
535
block_proposal .block ,
459
- cycle_length ,
536
+ cls . config . CYCLE_LENGTH ,
460
537
)
461
538
462
539
message = create_signing_message (
@@ -471,7 +548,7 @@ def attest_proposed_block(self,
471
548
private_key ,
472
549
)
473
550
474
- return self .get_attestation_record_class ()(
551
+ return cls .get_attestation_record_class ()(
475
552
slot = block_proposal .block .slot_number ,
476
553
shard_id = block_proposal .shard_id ,
477
554
oblique_parent_hashes = (),
0 commit comments