Skip to content

Commit 672ce24

Browse files
committed
*_hash -> *_root and sync block data structures
1 parent 026b14b commit 672ce24

20 files changed

+221
-254
lines changed

eth/beacon/db/chain.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,31 @@ def persist_block(self,
5858
pass
5959

6060
@abstractmethod
61-
def get_canonical_block_hash(self, slot: int) -> Hash32:
61+
def get_canonical_block_root(self, slot: int) -> Hash32:
6262
pass
6363

6464
@abstractmethod
6565
def get_canonical_block_by_slot(self, slot: int) -> BaseBeaconBlock:
6666
pass
6767

6868
@abstractmethod
69-
def get_canonical_block_hash_by_slot(self, slot: int) -> Hash32:
69+
def get_canonical_block_root_by_slot(self, slot: int) -> Hash32:
7070
pass
7171

7272
@abstractmethod
7373
def get_canonical_head(self) -> BaseBeaconBlock:
7474
pass
7575

7676
@abstractmethod
77-
def get_block_by_hash(self, block_hash: Hash32) -> BaseBeaconBlock:
77+
def get_block_by_root(self, block_root: Hash32) -> BaseBeaconBlock:
7878
pass
7979

8080
@abstractmethod
81-
def get_score(self, block_hash: Hash32) -> int:
81+
def get_score(self, block_root: Hash32) -> int:
8282
pass
8383

8484
@abstractmethod
85-
def block_exists(self, block_hash: Hash32) -> bool:
85+
def block_exists(self, block_root: Hash32) -> bool:
8686
pass
8787

8888
@abstractmethod
@@ -147,21 +147,21 @@ def _persist_block(
147147
#
148148
# Canonical Chain API
149149
#
150-
def get_canonical_block_hash(self, slot: int) -> Hash32:
150+
def get_canonical_block_root(self, slot: int) -> Hash32:
151151
"""
152-
Return the block hash for the canonical block at the given number.
152+
Return the block root for the canonical block at the given number.
153153
154154
Raise BlockNotFound if there's no block with the given number in the
155155
canonical chain.
156156
"""
157-
return self._get_canonical_block_hash(self.db, slot)
157+
return self._get_canonical_block_root(self.db, slot)
158158

159159
@staticmethod
160-
def _get_canonical_block_hash(db: BaseDB, slot: int) -> Hash32:
160+
def _get_canonical_block_root(db: BaseDB, slot: int) -> Hash32:
161161
validate_slot(slot)
162-
slot_to_hash_key = SchemaV1.make_block_slot_to_hash_lookup_key(slot)
162+
slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(slot)
163163
try:
164-
encoded_key = db[slot_to_hash_key]
164+
encoded_key = db[slot_to_root_key]
165165
except KeyError:
166166
raise BlockNotFound(
167167
"No canonical block for block slot #{0}".format(slot)
@@ -183,25 +183,25 @@ def _get_canonical_block_by_slot(
183183
cls,
184184
db: BaseDB,
185185
slot: int) -> BaseBeaconBlock:
186-
canonical_block_hash = cls._get_canonical_block_hash_by_slot(db, slot)
187-
return cls._get_block_by_hash(db, canonical_block_hash)
186+
canonical_block_root = cls._get_canonical_block_root_by_slot(db, slot)
187+
return cls._get_block_by_root(db, canonical_block_root)
188188

189-
def get_canonical_block_hash_by_slot(self, slot: int) -> Hash32:
189+
def get_canonical_block_root_by_slot(self, slot: int) -> Hash32:
190190
"""
191-
Return the block hash with the given slot in the canonical chain.
191+
Return the block root with the given slot in the canonical chain.
192192
193193
Raise BlockNotFound if there's no block with the given slot in the
194194
canonical chain.
195195
"""
196-
return self._get_canonical_block_hash_by_slot(self.db, slot)
196+
return self._get_canonical_block_root_by_slot(self.db, slot)
197197

198198
@classmethod
199-
def _get_canonical_block_hash_by_slot(
199+
def _get_canonical_block_root_by_slot(
200200
cls,
201201
db: BaseDB,
202202
slot: int) -> Hash32:
203203
validate_slot(slot)
204-
return cls._get_canonical_block_hash(db, slot)
204+
return cls._get_canonical_block_root(db, slot)
205205

206206
def get_canonical_head(self) -> BaseBeaconBlock:
207207
"""
@@ -212,48 +212,48 @@ def get_canonical_head(self) -> BaseBeaconBlock:
212212
@classmethod
213213
def _get_canonical_head(cls, db: BaseDB) -> BaseBeaconBlock:
214214
try:
215-
canonical_head_hash = db[SchemaV1.make_canonical_head_hash_lookup_key()]
215+
canonical_head_root = db[SchemaV1.make_canonical_head_root_lookup_key()]
216216
except KeyError:
217217
raise CanonicalHeadNotFound("No canonical head set for this chain")
218-
return cls._get_block_by_hash(db, Hash32(canonical_head_hash))
218+
return cls._get_block_by_root(db, Hash32(canonical_head_root))
219219

220-
def get_block_by_hash(self, block_hash: Hash32) -> BaseBeaconBlock:
221-
return self._get_block_by_hash(self.db, block_hash)
220+
def get_block_by_root(self, block_root: Hash32) -> BaseBeaconBlock:
221+
return self._get_block_by_root(self.db, block_root)
222222

223223
@staticmethod
224-
def _get_block_by_hash(db: BaseDB, block_hash: Hash32) -> BaseBeaconBlock:
224+
def _get_block_by_root(db: BaseDB, block_root: Hash32) -> BaseBeaconBlock:
225225
"""
226-
Return the requested block header as specified by block hash.
226+
Return the requested block header as specified by block root.
227227
228228
Raise BlockNotFound if it is not present in the db.
229229
"""
230-
validate_word(block_hash, title="Block Hash")
230+
validate_word(block_root, title="block root")
231231
try:
232-
block_rlp = db[block_hash]
232+
block_rlp = db[block_root]
233233
except KeyError:
234-
raise BlockNotFound("No block with hash {0} found".format(
235-
encode_hex(block_hash)))
234+
raise BlockNotFound("No block with root {0} found".format(
235+
encode_hex(block_root)))
236236
return _decode_block(block_rlp)
237237

238-
def get_score(self, block_hash: Hash32) -> int:
239-
return self._get_score(self.db, block_hash)
238+
def get_score(self, block_root: Hash32) -> int:
239+
return self._get_score(self.db, block_root)
240240

241241
@staticmethod
242-
def _get_score(db: BaseDB, block_hash: Hash32) -> int:
242+
def _get_score(db: BaseDB, block_root: Hash32) -> int:
243243
try:
244-
encoded_score = db[SchemaV1.make_block_hash_to_score_lookup_key(block_hash)]
244+
encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key(block_root)]
245245
except KeyError:
246246
raise BlockNotFound("No block with hash {0} found".format(
247-
encode_hex(block_hash)))
247+
encode_hex(block_root)))
248248
return rlp.decode(encoded_score, sedes=rlp.sedes.big_endian_int)
249249

250-
def block_exists(self, block_hash: Hash32) -> bool:
251-
return self._block_exists(self.db, block_hash)
250+
def block_exists(self, block_root: Hash32) -> bool:
251+
return self._block_exists(self.db, block_root)
252252

253253
@staticmethod
254-
def _block_exists(db: BaseDB, block_hash: Hash32) -> bool:
255-
validate_word(block_hash, title="Block Hash")
256-
return block_hash in db
254+
def _block_exists(db: BaseDB, block_root: Hash32) -> bool:
255+
validate_word(block_root, title="block root")
256+
return block_root in db
257257

258258
def persist_block_chain(
259259
self,
@@ -308,7 +308,7 @@ def _persist_block_chain(
308308
score = block.slot
309309

310310
db.set(
311-
SchemaV1.make_block_hash_to_score_lookup_key(block.hash),
311+
SchemaV1.make_block_root_to_score_lookup_key(block.hash),
312312
rlp.encode(score, sedes=rlp.sedes.big_endian_int),
313313
)
314314

@@ -326,38 +326,38 @@ def _persist_block_chain(
326326
@classmethod
327327
def _set_as_canonical_chain_head(
328328
cls, db: BaseDB,
329-
block_hash: Hash32) -> Tuple[Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...]]:
329+
block_root: Hash32) -> Tuple[Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...]]:
330330
"""
331331
Set the canonical chain HEAD to the block as specified by the
332-
given block hash.
332+
given block root.
333333
334334
:return: a tuple of the blocks that are newly in the canonical chain, and the blocks that
335335
are no longer in the canonical chain
336336
"""
337337
try:
338-
block = cls._get_block_by_hash(db, block_hash)
338+
block = cls._get_block_by_root(db, block_root)
339339
except BlockNotFound:
340340
raise ValueError(
341-
"Cannot use unknown block hash as canonical head: {}".format(block_hash)
341+
"Cannot use unknown block root as canonical head: {}".format(block_root)
342342
)
343343

344344
new_canonical_blocks = tuple(reversed(cls._find_new_ancestors(db, block)))
345345
old_canonical_blocks = []
346346

347347
for block in new_canonical_blocks:
348348
try:
349-
old_canonical_hash = cls._get_canonical_block_hash(db, block.slot)
349+
old_canonical_root = cls._get_canonical_block_root(db, block.slot)
350350
except BlockNotFound:
351351
# no old_canonical block, and no more possible
352352
break
353353
else:
354-
old_canonical_block = cls._get_block_by_hash(db, old_canonical_hash)
354+
old_canonical_block = cls._get_block_by_root(db, old_canonical_root)
355355
old_canonical_blocks.append(old_canonical_block)
356356

357357
for block in new_canonical_blocks:
358-
cls._add_block_slot_to_hash_lookup(db, block)
358+
cls._add_block_slot_to_root_lookup(db, block)
359359

360-
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), block.hash)
360+
db.set(SchemaV1.make_canonical_head_root_lookup_key(), block.hash)
361361

362362
return new_canonical_blocks, tuple(old_canonical_blocks)
363363

@@ -392,19 +392,19 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
392392
if block.parent_root == GENESIS_PARENT_HASH:
393393
break
394394
else:
395-
block = cls._get_block_by_hash(db, block.parent_root)
395+
block = cls._get_block_by_root(db, block.parent_root)
396396

397397
@staticmethod
398-
def _add_block_slot_to_hash_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
398+
def _add_block_slot_to_root_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
399399
"""
400400
Set a record in the database to allow looking up this block by its
401401
block slot.
402402
"""
403-
block_slot_to_hash_key = SchemaV1.make_block_slot_to_hash_lookup_key(
403+
block_slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(
404404
block.slot
405405
)
406406
db.set(
407-
block_slot_to_hash_key,
407+
block_slot_to_root_key,
408408
rlp.encode(block.hash, sedes=rlp.sedes.binary),
409409
)
410410

eth/beacon/db/schema.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ class BaseSchema(ABC):
1111
#
1212
@staticmethod
1313
@abstractmethod
14-
def make_canonical_head_hash_lookup_key() -> bytes:
14+
def make_canonical_head_root_lookup_key() -> bytes:
1515
pass
1616

1717
@staticmethod
1818
@abstractmethod
19-
def make_block_slot_to_hash_lookup_key(slot: int) -> bytes:
19+
def make_block_slot_to_root_lookup_key(slot: int) -> bytes:
2020
pass
2121

2222
@staticmethod
2323
@abstractmethod
24-
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
24+
def make_block_root_to_score_lookup_key(block_root: Hash32) -> bytes:
2525
pass
2626

2727

@@ -30,14 +30,14 @@ class SchemaV1(BaseSchema):
3030
# Block
3131
#
3232
@staticmethod
33-
def make_canonical_head_hash_lookup_key() -> bytes:
34-
return b'v1:beacon:canonical-head-hash'
33+
def make_canonical_head_root_lookup_key() -> bytes:
34+
return b'v1:beacon:canonical-head-root'
3535

3636
@staticmethod
37-
def make_block_slot_to_hash_lookup_key(slot: int) -> bytes:
38-
slot_to_hash_key = b'v1:beacon:block-slot-to-hash:%d' % slot
39-
return slot_to_hash_key
37+
def make_block_slot_to_root_lookup_key(slot: int) -> bytes:
38+
slot_to_root_key = b'v1:beacon:block-slot-to-root:%d' % slot
39+
return slot_to_root_key
4040

4141
@staticmethod
42-
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
43-
return b'v1:beacon:block-hash-to-score:%s' % block_hash
42+
def make_block_root_to_score_lookup_key(block_root: Hash32) -> bytes:
43+
return b'v1:beacon:block-root-to-score:%s' % block_root

eth/beacon/helpers.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,29 @@ def _get_element_from_recent_list(
7676

7777

7878
#
79-
# Get block hash(es)
79+
# Get block root
8080
#
81-
def get_block_hash(
82-
latest_block_hashes: Sequence[Hash32],
81+
def get_block_root(
82+
latest_block_roots: Sequence[Hash32],
8383
current_slot: int,
8484
slot: int) -> Hash32:
8585
"""
86-
Returns the block hash at a recent ``slot``.
86+
Returns the block root at a recent ``slot``.
8787
"""
88-
slot_relative_position = current_slot - len(latest_block_hashes)
88+
slot_relative_position = current_slot - len(latest_block_roots)
8989
return _get_element_from_recent_list(
90-
latest_block_hashes,
90+
latest_block_roots,
9191
slot,
9292
slot_relative_position,
9393
)
9494

9595

96-
@to_tuple
97-
def get_hashes_from_latest_block_hashes(
98-
latest_block_hashes: Sequence[Hash32],
99-
current_slot: int,
100-
from_slot: int,
101-
to_slot: int) -> Iterable[Hash32]:
102-
"""
103-
Returns the block hashes between ``from_slot`` and ``to_slot``.
104-
"""
105-
for slot in range(from_slot, to_slot + 1):
106-
yield get_block_hash(
107-
latest_block_hashes,
108-
current_slot,
109-
slot,
110-
)
111-
112-
11396
#
11497
# Get shards_committees or indices
11598
#
11699
@to_tuple
117100
def _get_shard_committees_at_slot(
118-
latest_state_recalculation_slot: int,
101+
state_slot: int,
119102
shard_committees_at_slots: Sequence[Sequence[ShardCommittee]],
120103
slot: int,
121104
epoch_length: int) -> Iterable[ShardCommittee]:
@@ -127,7 +110,7 @@ def _get_shard_committees_at_slot(
127110
)
128111
)
129112

130-
slot_relative_position = latest_state_recalculation_slot - epoch_length
113+
slot_relative_position = state_slot - epoch_length
131114

132115
yield from _get_element_from_recent_list(
133116
shard_committees_at_slots,
@@ -143,7 +126,7 @@ def get_shard_committees_at_slot(state: 'BeaconState',
143126
Return the ``ShardCommittee`` for the ``slot``.
144127
"""
145128
return _get_shard_committees_at_slot(
146-
latest_state_recalculation_slot=state.latest_state_recalculation_slot,
129+
state_slot=state.slot,
147130
shard_committees_at_slots=state.shard_committees_at_slots,
148131
slot=slot,
149132
epoch_length=epoch_length,

0 commit comments

Comments
 (0)