Skip to content

Commit f3fd374

Browse files
authored
Merge pull request #1610 from hwwhww/state_slot
State slot and *_hash to *_root and constants updates
2 parents 4bd796d + 319b88b commit f3fd374

24 files changed

+315
-313
lines changed

eth/beacon/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
RAND_BYTES = 3
1111
# The highest possible result of the RNG.
1212
RAND_MAX = 2 ** (RAND_BYTES * 8) - 1
13+
14+
EMPTY_SIGNATURE = (0, 0)

eth/beacon/db/chain.py

Lines changed: 62 additions & 62 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,
@@ -278,11 +278,11 @@ def _persist_block_chain(
278278
return tuple(), tuple()
279279
else:
280280
for parent, child in sliding_window(2, blocks):
281-
if parent.hash != child.parent_root:
281+
if parent.root != child.parent_root:
282282
raise ValidationError(
283283
"Non-contiguous chain. Expected {} to have {} as parent but was {}".format(
284-
encode_hex(child.hash),
285-
encode_hex(parent.hash),
284+
encode_hex(child.root),
285+
encode_hex(parent.root),
286286
encode_hex(child.parent_root),
287287
)
288288
)
@@ -291,7 +291,7 @@ def _persist_block_chain(
291291
if not is_genesis and not cls._block_exists(db, first_block.parent_root):
292292
raise ParentNotFound(
293293
"Cannot persist block ({}) with unknown parent ({})".format(
294-
encode_hex(first_block.hash), encode_hex(first_block.parent_root)))
294+
encode_hex(first_block.root), encode_hex(first_block.parent_root)))
295295

296296
if is_genesis:
297297
score = 0
@@ -300,64 +300,64 @@ def _persist_block_chain(
300300

301301
for block in blocks:
302302
db.set(
303-
block.hash,
303+
block.root,
304304
rlp.encode(block),
305305
)
306306

307307
# TODO: It's a stub before we implement fork choice rule
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.root),
312312
rlp.encode(score, sedes=rlp.sedes.big_endian_int),
313313
)
314314

315315
try:
316-
previous_canonical_head = cls._get_canonical_head(db).hash
316+
previous_canonical_head = cls._get_canonical_head(db).root
317317
head_score = cls._get_score(db, previous_canonical_head)
318318
except CanonicalHeadNotFound:
319-
return cls._set_as_canonical_chain_head(db, block.hash)
319+
return cls._set_as_canonical_chain_head(db, block.root)
320320

321321
if score > head_score:
322-
return cls._set_as_canonical_chain_head(db, block.hash)
322+
return cls._set_as_canonical_chain_head(db, block.root)
323323
else:
324324
return tuple(), tuple()
325325

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.root)
361361

362362
return new_canonical_blocks, tuple(old_canonical_blocks)
363363

@@ -382,7 +382,7 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
382382
# This just means the block is not on the canonical chain.
383383
pass
384384
else:
385-
if orig.hash == block.hash:
385+
if orig.root == block.root:
386386
# Found the common ancestor, stop.
387387
break
388388

@@ -392,20 +392,20 @@ 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,
408-
rlp.encode(block.hash, sedes=rlp.sedes.binary),
407+
block_slot_to_root_key,
408+
rlp.encode(block.root, sedes=rlp.sedes.binary),
409409
)
410410

411411
#
@@ -441,7 +441,7 @@ def _persist_state(cls,
441441
db: BaseDB,
442442
state: BeaconState) -> None:
443443
db.set(
444-
state.hash,
444+
state.root,
445445
rlp.encode(state),
446446
)
447447

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

0 commit comments

Comments
 (0)