11
11
)
12
12
13
13
import rlp
14
- from rlp .sedes import (
15
- CountableList ,
16
- )
17
14
from eth_typing import (
18
15
Hash32 ,
19
16
)
36
33
ParentNotFound ,
37
34
StateRootNotFound ,
38
35
)
39
- from eth .rlp .sedes import (
40
- hash32 ,
41
- )
42
36
from eth .validation import (
43
37
validate_word ,
44
38
)
45
39
46
- from eth .beacon .types .active_states import ActiveState # noqa: F401
40
+ from eth .beacon .types .states import BeaconState # noqa: F401
47
41
from eth .beacon .types .blocks import BaseBeaconBlock # noqa: F401
48
- from eth .beacon .types .crystallized_states import CrystallizedState # noqa: F401
49
42
from eth .beacon .validation import (
50
43
validate_slot ,
51
44
)
@@ -100,36 +93,15 @@ def persist_block_chain(
100
93
pass
101
94
102
95
#
103
- # Crystallized State
96
+ # Beacon State
104
97
#
105
98
@abstractmethod
106
- def get_crystallized_state_by_root (self , state_root : Hash32 ) -> CrystallizedState :
99
+ def get_state_by_root (self , state_root : Hash32 ) -> BeaconState :
107
100
pass
108
101
109
102
@abstractmethod
110
- def get_canonical_crystallized_state_root (self , slot : int ) -> Hash32 :
111
- pass
112
-
113
- @abstractmethod
114
- def persist_crystallized_state (self ,
115
- crystallized_state : CrystallizedState ) -> None :
116
- pass
117
-
118
- #
119
- # Active State
120
- #
121
- @abstractmethod
122
- def get_active_state_by_root (self , state_root : Hash32 ) -> ActiveState :
123
- pass
124
-
125
- @abstractmethod
126
- def get_active_state_root_by_crystallized (self , crystallized_state_root : Hash32 ) -> Hash32 :
127
- pass
128
-
129
- @abstractmethod
130
- def persist_active_state (self ,
131
- active_state : ActiveState ,
132
- crystallized_state_root : Hash32 ) -> None :
103
+ def persist_state (self ,
104
+ state : BeaconState ) -> None :
133
105
pass
134
106
135
107
#
@@ -306,25 +278,25 @@ def _persist_block_chain(
306
278
return tuple (), tuple ()
307
279
else :
308
280
for parent , child in sliding_window (2 , blocks ):
309
- if parent .hash != child .parent_hash :
281
+ if parent .hash != child .ancestor_hashes [ 0 ] :
310
282
raise ValidationError (
311
283
"Non-contiguous chain. Expected {} to have {} as parent but was {}" .format (
312
284
encode_hex (child .hash ),
313
285
encode_hex (parent .hash ),
314
- encode_hex (child .parent_hash ),
286
+ encode_hex (child .ancestor_hashes [ 0 ] ),
315
287
)
316
288
)
317
289
318
- is_genesis = first_block .parent_hash == GENESIS_PARENT_HASH
319
- if not is_genesis and not cls ._block_exists (db , first_block .parent_hash ):
290
+ is_genesis = first_block .ancestor_hashes [ 0 ] == GENESIS_PARENT_HASH
291
+ if not is_genesis and not cls ._block_exists (db , first_block .ancestor_hashes [ 0 ] ):
320
292
raise ParentNotFound (
321
293
"Cannot persist block ({}) with unknown parent ({})" .format (
322
- encode_hex (first_block .hash ), encode_hex (first_block .parent_hash )))
294
+ encode_hex (first_block .hash ), encode_hex (first_block .ancestor_hashes [ 0 ] )))
323
295
324
296
if is_genesis :
325
297
score = 0
326
298
else :
327
- score = cls ._get_score (db , first_block .parent_hash )
299
+ score = cls ._get_score (db , first_block .ancestor_hashes [ 0 ] )
328
300
329
301
for block in blocks :
330
302
db .set (
@@ -333,7 +305,7 @@ def _persist_block_chain(
333
305
)
334
306
335
307
# TODO: It's a stub before we implement fork choice rule
336
- score += block .slot_number
308
+ score += block .slot
337
309
338
310
db .set (
339
311
SchemaV1 .make_block_hash_to_score_lookup_key (block .hash ),
@@ -374,7 +346,7 @@ def _set_as_canonical_chain_head(
374
346
375
347
for block in new_canonical_blocks :
376
348
try :
377
- old_canonical_hash = cls ._get_canonical_block_hash (db , block .slot_number )
349
+ old_canonical_hash = cls ._get_canonical_block_hash (db , block .slot )
378
350
except BlockNotFound :
379
351
# no old_canonical block, and no more possible
380
352
break
@@ -405,7 +377,7 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
405
377
"""
406
378
while True :
407
379
try :
408
- orig = cls ._get_canonical_block_by_slot (db , block .slot_number )
380
+ orig = cls ._get_canonical_block_by_slot (db , block .slot )
409
381
except BlockNotFound :
410
382
# This just means the block is not on the canonical chain.
411
383
pass
@@ -417,10 +389,10 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
417
389
# Found a new ancestor
418
390
yield block
419
391
420
- if block .parent_hash == GENESIS_PARENT_HASH :
392
+ if block .ancestor_hashes [ 0 ] == GENESIS_PARENT_HASH :
421
393
break
422
394
else :
423
- block = cls ._get_block_by_hash (db , block .parent_hash )
395
+ block = cls ._get_block_by_hash (db , block .ancestor_hashes [ 0 ] )
424
396
425
397
@staticmethod
426
398
def _add_block_slot_to_hash_lookup (db : BaseDB , block : BaseBeaconBlock ) -> None :
@@ -429,207 +401,48 @@ def _add_block_slot_to_hash_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
429
401
block slot.
430
402
"""
431
403
block_slot_to_hash_key = SchemaV1 .make_block_slot_to_hash_lookup_key (
432
- block .slot_number
404
+ block .slot
433
405
)
434
406
db .set (
435
407
block_slot_to_hash_key ,
436
408
rlp .encode (block .hash , sedes = rlp .sedes .binary ),
437
409
)
438
410
439
- #
440
- # Crystallized State API
441
- #
442
- def get_crystallized_state_by_root (self , state_root : Hash32 ) -> CrystallizedState :
443
- return self ._get_crystallized_state_by_root (self .db , state_root )
444
-
445
- @staticmethod
446
- def _get_crystallized_state_by_root (db : BaseDB , state_root : Hash32 ) -> CrystallizedState :
447
- """
448
- Return the requested crystallized state as specified by state hash.
449
-
450
- Raises StateRootNotFound if it is not present in the db.
451
- """
452
- # TODO: validate_crystallized_state_root
453
- try :
454
- state_rlp = db [state_root ]
455
- except KeyError :
456
- raise StateRootNotFound ("No state with root {0} found" .format (
457
- encode_hex (state_rlp )))
458
- return _decode_crystallized_state (state_rlp )
459
-
460
- def get_canonical_crystallized_state_root (self , slot : int ) -> Hash32 :
461
- """
462
- Return the state hash for the canonical state at the given slot.
463
-
464
- Raises StateRootNotFound if there's no state with the given slot in the
465
- canonical chain.
466
- """
467
- return self ._get_canonical_crystallized_state_root (self .db , slot )
468
-
469
- @staticmethod
470
- def _get_canonical_crystallized_state_root (db : BaseDB , slot : int ) -> Hash32 :
471
- validate_slot (slot )
472
- slot_to_hash_key = SchemaV1 .make_slot_to_crystallized_state_lookup_key (slot )
473
- try :
474
- encoded_key = db [slot_to_hash_key ]
475
- except KeyError :
476
- raise StateRootNotFound (
477
- "No canonical crystallized state for slot #{0}" .format (slot )
478
- )
479
- else :
480
- return rlp .decode (encoded_key , sedes = rlp .sedes .binary )
481
-
482
- def persist_crystallized_state (self ,
483
- crystallized_state : CrystallizedState ) -> None :
484
- """
485
- Persist the given CrystallizedState.
486
- """
487
- return self ._persist_crystallized_state (self .db , crystallized_state )
488
-
489
- @classmethod
490
- def _persist_crystallized_state (cls ,
491
- db : BaseDB ,
492
- crystallized_state : CrystallizedState ) -> None :
493
- cls ._add_slot_to_crystallized_state_lookup (db , crystallized_state )
494
- db .set (
495
- crystallized_state .hash ,
496
- rlp .encode (crystallized_state ),
497
- )
498
-
499
- @classmethod
500
- def _add_slot_to_crystallized_state_lookup (cls ,
501
- db : BaseDB ,
502
- crystallized_state : CrystallizedState ) -> None :
503
- """
504
- Set a record in the database to allow looking up this block by its
505
- last state recalculation slot.
506
-
507
- If it's a fork, store the old state root in `deletable_state_roots`.
508
- """
509
- slot_to_hash_key = SchemaV1 .make_slot_to_crystallized_state_lookup_key (
510
- crystallized_state .last_state_recalc
511
- )
512
- if db .exists (slot_to_hash_key ):
513
- deletable_state_roots = cls ._get_deletable_state_roots (db )
514
- replaced_state_root = rlp .decode (db [slot_to_hash_key ], sedes = rlp .sedes .binary )
515
- cls ._set_deletatable_state (
516
- db ,
517
- deletable_state_roots + (replaced_state_root , ),
518
- )
519
- db .set (
520
- slot_to_hash_key ,
521
- rlp .encode (crystallized_state .hash , sedes = rlp .sedes .binary ),
522
- )
523
-
524
- @staticmethod
525
- def _get_deletable_state_roots (db : BaseDB ) -> Tuple [Hash32 ]:
526
- """
527
- Return deletable_state_roots.
528
- """
529
- lookup_key = SchemaV1 .make_deletable_state_roots_lookup_key ()
530
- if not db .exists (lookup_key ):
531
- db .set (
532
- lookup_key ,
533
- rlp .encode ((), sedes = CountableList (hash32 )),
534
- )
535
- deletable_state_roots = rlp .decode (db [lookup_key ], sedes = CountableList (hash32 ))
536
-
537
- return deletable_state_roots
538
-
539
- @staticmethod
540
- def _set_deletatable_state (db : BaseDB , deletable_state_roots : Iterable [Hash32 ]) -> None :
541
- """
542
- Set deletable_state_roots.
543
- """
544
- lookup_key = SchemaV1 .make_deletable_state_roots_lookup_key ()
545
- db .set (
546
- lookup_key ,
547
- rlp .encode (deletable_state_roots , sedes = CountableList (hash32 )),
548
- )
549
-
550
411
#
551
412
# Active State API
552
413
#
553
- def get_active_state_by_root (self , state_root : Hash32 ) -> ActiveState :
554
- return self ._get_active_state_by_root (self .db , state_root )
414
+ def get_state_by_root (self , state_root : Hash32 ) -> BeaconState :
415
+ return self ._get_state_by_root (self .db , state_root )
555
416
556
417
@staticmethod
557
- def _get_active_state_by_root (db : BaseDB , state_root : Hash32 ) -> ActiveState :
418
+ def _get_state_by_root (db : BaseDB , state_root : Hash32 ) -> BeaconState :
558
419
"""
559
420
Return the requested crystallized state as specified by state hash.
560
421
561
422
Raises StateRootNotFound if it is not present in the db.
562
423
"""
563
- # TODO: validate_active_state_root
424
+ # TODO: validate_state_root
564
425
try :
565
426
state_rlp = db [state_root ]
566
427
except KeyError :
567
428
raise StateRootNotFound ("No state with root {0} found" .format (
568
429
encode_hex (state_rlp )))
569
- return _decode_active_state (state_rlp )
430
+ return _decode_state (state_rlp )
570
431
571
- def get_active_state_root_by_crystallized (self , crystallized_state_root : Hash32 ) -> Hash32 :
432
+ def persist_state (self ,
433
+ state : BeaconState ) -> None :
572
434
"""
573
- Return the state hash for the canonical state at the given crystallized_state_root.
574
-
575
- Raises StateRootNotFound if there's no state with the given slot in the
576
- canonical chain.
435
+ Persist the given BeaconState.
577
436
"""
578
- return self ._get_active_state_root_by_crystallized (self .db , crystallized_state_root )
579
-
580
- @staticmethod
581
- def _get_active_state_root_by_crystallized (db : BaseDB ,
582
- crystallized_state_root : Hash32 ) -> Hash32 :
583
- state_root_to_hash_key = SchemaV1 .make_crystallized_to_active_state_root_lookup_key (
584
- crystallized_state_root
585
- )
586
- try :
587
- encoded_key = db [state_root_to_hash_key ]
588
- except KeyError :
589
- raise StateRootNotFound (
590
- "No canonical active state for crystallized_state_root #{0}" .format (
591
- state_root_to_hash_key
592
- )
593
- )
594
- else :
595
- return rlp .decode (encoded_key , sedes = rlp .sedes .binary )
596
-
597
- def persist_active_state (self ,
598
- active_state : ActiveState ,
599
- crystallized_state_root : Hash32 ) -> None :
600
- """
601
- Persist the given ActiveState.
602
-
603
- NOTE: only persist active state when recalcuate crystallized state.
604
- """
605
- return self ._persist_active_state (self .db , active_state , crystallized_state_root )
606
-
607
- @classmethod
608
- def _persist_active_state (cls ,
609
- db : BaseDB ,
610
- active_state : ActiveState ,
611
- crystallized_state_root : Hash32 ) -> None :
612
- cls ._add_crystallized_to_active_state_lookup (db , active_state , crystallized_state_root )
613
- db .set (
614
- active_state .hash ,
615
- rlp .encode (active_state ),
616
- )
437
+ return self ._persist_state (self .db , state )
617
438
618
439
@classmethod
619
- def _add_crystallized_to_active_state_lookup (cls ,
620
- db : BaseDB ,
621
- active_state : ActiveState ,
622
- crystallized_state_root : Hash32 ) -> None :
623
- """
624
- Set a record in the database to allow looking up this block by its
625
- last state recalculation slot.
626
- """
627
- slot_to_hash_key = SchemaV1 .make_crystallized_to_active_state_root_lookup_key (
628
- crystallized_state_root ,
629
- )
440
+ def _persist_state (cls ,
441
+ db : BaseDB ,
442
+ state : BeaconState ) -> None :
630
443
db .set (
631
- slot_to_hash_key ,
632
- rlp .encode (active_state . hash , sedes = rlp . sedes . binary ),
444
+ state . hash ,
445
+ rlp .encode (state ),
633
446
)
634
447
635
448
#
@@ -659,12 +472,6 @@ def _decode_block(block_rlp: bytes) -> BaseBeaconBlock:
659
472
660
473
661
474
@functools .lru_cache (128 )
662
- def _decode_crystallized_state (crystallized_state_rlp : bytes ) -> CrystallizedState :
663
- # TODO: forkable CrystallizedState fields?
664
- return rlp .decode (crystallized_state_rlp , sedes = CrystallizedState )
665
-
666
-
667
- @functools .lru_cache (128 )
668
- def _decode_active_state (active_state_rlp : bytes ) -> ActiveState :
669
- # TODO: forkable CrystallizedState fields?
670
- return rlp .decode (active_state_rlp , sedes = ActiveState )
475
+ def _decode_state (state_rlp : bytes ) -> BeaconState :
476
+ # TODO: forkable BeaconState fields?
477
+ return rlp .decode (state_rlp , sedes = BeaconState )
0 commit comments