Skip to content

Commit b272c8f

Browse files
authored
Merge pull request ethereum#1508 from hwwhww/rework_types
Remove outdated code and rework `eth/beacon/types` & constants
2 parents cb0a2bb + c6ed7ea commit b272c8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1153
-2807
lines changed

eth/beacon/db/chain.py

Lines changed: 28 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
)
1212

1313
import rlp
14-
from rlp.sedes import (
15-
CountableList,
16-
)
1714
from eth_typing import (
1815
Hash32,
1916
)
@@ -36,16 +33,12 @@
3633
ParentNotFound,
3734
StateRootNotFound,
3835
)
39-
from eth.rlp.sedes import (
40-
hash32,
41-
)
4236
from eth.validation import (
4337
validate_word,
4438
)
4539

46-
from eth.beacon.types.active_states import ActiveState # noqa: F401
40+
from eth.beacon.types.states import BeaconState # noqa: F401
4741
from eth.beacon.types.blocks import BaseBeaconBlock # noqa: F401
48-
from eth.beacon.types.crystallized_states import CrystallizedState # noqa: F401
4942
from eth.beacon.validation import (
5043
validate_slot,
5144
)
@@ -100,36 +93,15 @@ def persist_block_chain(
10093
pass
10194

10295
#
103-
# Crystallized State
96+
# Beacon State
10497
#
10598
@abstractmethod
106-
def get_crystallized_state_by_root(self, state_root: Hash32) -> CrystallizedState:
99+
def get_state_by_root(self, state_root: Hash32) -> BeaconState:
107100
pass
108101

109102
@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:
133105
pass
134106

135107
#
@@ -333,7 +305,7 @@ def _persist_block_chain(
333305
)
334306

335307
# TODO: It's a stub before we implement fork choice rule
336-
score += block.slot_number
308+
score = block.slot
337309

338310
db.set(
339311
SchemaV1.make_block_hash_to_score_lookup_key(block.hash),
@@ -374,7 +346,7 @@ def _set_as_canonical_chain_head(
374346

375347
for block in new_canonical_blocks:
376348
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)
378350
except BlockNotFound:
379351
# no old_canonical block, and no more possible
380352
break
@@ -405,7 +377,7 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
405377
"""
406378
while True:
407379
try:
408-
orig = cls._get_canonical_block_by_slot(db, block.slot_number)
380+
orig = cls._get_canonical_block_by_slot(db, block.slot)
409381
except BlockNotFound:
410382
# This just means the block is not on the canonical chain.
411383
pass
@@ -429,207 +401,48 @@ def _add_block_slot_to_hash_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
429401
block slot.
430402
"""
431403
block_slot_to_hash_key = SchemaV1.make_block_slot_to_hash_lookup_key(
432-
block.slot_number
404+
block.slot
433405
)
434406
db.set(
435407
block_slot_to_hash_key,
436408
rlp.encode(block.hash, sedes=rlp.sedes.binary),
437409
)
438410

439411
#
440-
# Crystallized State API
412+
# Beacon State API
441413
#
442-
def get_crystallized_state_by_root(self, state_root: Hash32) -> CrystallizedState:
443-
return self._get_crystallized_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)
444416

445417
@staticmethod
446-
def _get_crystallized_state_by_root(db: BaseDB, state_root: Hash32) -> CrystallizedState:
418+
def _get_state_by_root(db: BaseDB, state_root: Hash32) -> BeaconState:
447419
"""
448-
Return the requested crystallized state as specified by state hash.
420+
Return the requested beacon state as specified by state hash.
449421
450422
Raises StateRootNotFound if it is not present in the db.
451423
"""
452-
# TODO: validate_crystallized_state_root
424+
# TODO: validate_state_root
453425
try:
454426
state_rlp = db[state_root]
455427
except KeyError:
456428
raise StateRootNotFound("No state with root {0} found".format(
457429
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)
430+
return _decode_state(state_rlp)
481431

482-
def persist_crystallized_state(self,
483-
crystallized_state: CrystallizedState) -> None:
432+
def persist_state(self,
433+
state: BeaconState) -> None:
484434
"""
485-
Persist the given CrystallizedState.
435+
Persist the given BeaconState.
486436
"""
487-
return self._persist_crystallized_state(self.db, crystallized_state)
437+
return self._persist_state(self.db, state)
488438

489439
@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)
440+
def _persist_state(cls,
441+
db: BaseDB,
442+
state: BeaconState) -> None:
494443
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-
#
551-
# Active State API
552-
#
553-
def get_active_state_by_root(self, state_root: Hash32) -> ActiveState:
554-
return self._get_active_state_by_root(self.db, state_root)
555-
556-
@staticmethod
557-
def _get_active_state_by_root(db: BaseDB, state_root: Hash32) -> ActiveState:
558-
"""
559-
Return the requested crystallized state as specified by state hash.
560-
561-
Raises StateRootNotFound if it is not present in the db.
562-
"""
563-
# TODO: validate_active_state_root
564-
try:
565-
state_rlp = db[state_root]
566-
except KeyError:
567-
raise StateRootNotFound("No state with root {0} found".format(
568-
encode_hex(state_rlp)))
569-
return _decode_active_state(state_rlp)
570-
571-
def get_active_state_root_by_crystallized(self, crystallized_state_root: Hash32) -> Hash32:
572-
"""
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.
577-
"""
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-
)
617-
618-
@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-
)
630-
db.set(
631-
slot_to_hash_key,
632-
rlp.encode(active_state.hash, sedes=rlp.sedes.binary),
444+
state.hash,
445+
rlp.encode(state),
633446
)
634447

635448
#
@@ -659,12 +472,6 @@ def _decode_block(block_rlp: bytes) -> BaseBeaconBlock:
659472

660473

661474
@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)

eth/beacon/db/schema.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,6 @@ def make_block_slot_to_hash_lookup_key(slot: int) -> bytes:
2424
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
2525
pass
2626

27-
#
28-
# States
29-
#
30-
@staticmethod
31-
@abstractmethod
32-
def make_slot_to_crystallized_state_lookup_key(slot: int) -> bytes:
33-
pass
34-
35-
@staticmethod
36-
@abstractmethod
37-
def make_crystallized_to_active_state_root_lookup_key(state_root: Hash32) -> bytes:
38-
pass
39-
40-
@staticmethod
41-
@abstractmethod
42-
def make_deletable_state_roots_lookup_key() -> bytes:
43-
pass
44-
4527

4628
class SchemaV1(BaseSchema):
4729
#
@@ -59,18 +41,3 @@ def make_block_slot_to_hash_lookup_key(slot: int) -> bytes:
5941
@staticmethod
6042
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
6143
return b'v1:beacon:block-hash-to-score:%s' % block_hash
62-
63-
#
64-
# States
65-
#
66-
@staticmethod
67-
def make_slot_to_crystallized_state_lookup_key(slot: int) -> bytes:
68-
return b'v1:beacon:slot-to-crystallized-state:%d' % slot
69-
70-
@staticmethod
71-
def make_crystallized_to_active_state_root_lookup_key(state_root: Hash32) -> bytes:
72-
return b'v1:beacon:crystallized-root-to-active-state-root:%s' % state_root
73-
74-
@staticmethod
75-
def make_deletable_state_roots_lookup_key() -> bytes:
76-
return b'v1:beacon:make-deletable-state-roots'

eth/beacon/enums/bls_domain.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from enum import IntEnum
2+
3+
4+
class BLSDomain(IntEnum):
5+
DOMAIN_DEPOSIT = 0
6+
DOMAIN_ATTESTATION = 1
7+
DOMAIN_PROPOSAL = 2
8+
DOMAIN_LOGOUT = 3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from enum import IntEnum
2+
3+
4+
class SpecialRecordTypes(IntEnum):
5+
LOGOUT = 0
6+
CASPER_SLASHING = 1
7+
PROPOSER_SLASHING = 2
8+
DEPOSIT_PROOF = 3

0 commit comments

Comments
 (0)