@@ -58,31 +58,31 @@ def persist_block(self,
58
58
pass
59
59
60
60
@abstractmethod
61
- def get_canonical_block_hash (self , slot : int ) -> Hash32 :
61
+ def get_canonical_block_root (self , slot : int ) -> Hash32 :
62
62
pass
63
63
64
64
@abstractmethod
65
65
def get_canonical_block_by_slot (self , slot : int ) -> BaseBeaconBlock :
66
66
pass
67
67
68
68
@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 :
70
70
pass
71
71
72
72
@abstractmethod
73
73
def get_canonical_head (self ) -> BaseBeaconBlock :
74
74
pass
75
75
76
76
@abstractmethod
77
- def get_block_by_hash (self , block_hash : Hash32 ) -> BaseBeaconBlock :
77
+ def get_block_by_root (self , block_root : Hash32 ) -> BaseBeaconBlock :
78
78
pass
79
79
80
80
@abstractmethod
81
- def get_score (self , block_hash : Hash32 ) -> int :
81
+ def get_score (self , block_root : Hash32 ) -> int :
82
82
pass
83
83
84
84
@abstractmethod
85
- def block_exists (self , block_hash : Hash32 ) -> bool :
85
+ def block_exists (self , block_root : Hash32 ) -> bool :
86
86
pass
87
87
88
88
@abstractmethod
@@ -147,21 +147,21 @@ def _persist_block(
147
147
#
148
148
# Canonical Chain API
149
149
#
150
- def get_canonical_block_hash (self , slot : int ) -> Hash32 :
150
+ def get_canonical_block_root (self , slot : int ) -> Hash32 :
151
151
"""
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.
153
153
154
154
Raise BlockNotFound if there's no block with the given number in the
155
155
canonical chain.
156
156
"""
157
- return self ._get_canonical_block_hash (self .db , slot )
157
+ return self ._get_canonical_block_root (self .db , slot )
158
158
159
159
@staticmethod
160
- def _get_canonical_block_hash (db : BaseDB , slot : int ) -> Hash32 :
160
+ def _get_canonical_block_root (db : BaseDB , slot : int ) -> Hash32 :
161
161
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 )
163
163
try :
164
- encoded_key = db [slot_to_hash_key ]
164
+ encoded_key = db [slot_to_root_key ]
165
165
except KeyError :
166
166
raise BlockNotFound (
167
167
"No canonical block for block slot #{0}" .format (slot )
@@ -183,25 +183,25 @@ def _get_canonical_block_by_slot(
183
183
cls ,
184
184
db : BaseDB ,
185
185
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 )
188
188
189
- def get_canonical_block_hash_by_slot (self , slot : int ) -> Hash32 :
189
+ def get_canonical_block_root_by_slot (self , slot : int ) -> Hash32 :
190
190
"""
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.
192
192
193
193
Raise BlockNotFound if there's no block with the given slot in the
194
194
canonical chain.
195
195
"""
196
- return self ._get_canonical_block_hash_by_slot (self .db , slot )
196
+ return self ._get_canonical_block_root_by_slot (self .db , slot )
197
197
198
198
@classmethod
199
- def _get_canonical_block_hash_by_slot (
199
+ def _get_canonical_block_root_by_slot (
200
200
cls ,
201
201
db : BaseDB ,
202
202
slot : int ) -> Hash32 :
203
203
validate_slot (slot )
204
- return cls ._get_canonical_block_hash (db , slot )
204
+ return cls ._get_canonical_block_root (db , slot )
205
205
206
206
def get_canonical_head (self ) -> BaseBeaconBlock :
207
207
"""
@@ -212,48 +212,48 @@ def get_canonical_head(self) -> BaseBeaconBlock:
212
212
@classmethod
213
213
def _get_canonical_head (cls , db : BaseDB ) -> BaseBeaconBlock :
214
214
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 ()]
216
216
except KeyError :
217
217
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 ))
219
219
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 )
222
222
223
223
@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 :
225
225
"""
226
- Return the requested block header as specified by block hash .
226
+ Return the requested block header as specified by block root .
227
227
228
228
Raise BlockNotFound if it is not present in the db.
229
229
"""
230
- validate_word (block_hash , title = "Block Hash " )
230
+ validate_word (block_root , title = "block root " )
231
231
try :
232
- block_rlp = db [block_hash ]
232
+ block_rlp = db [block_root ]
233
233
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 )))
236
236
return _decode_block (block_rlp )
237
237
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 )
240
240
241
241
@staticmethod
242
- def _get_score (db : BaseDB , block_hash : Hash32 ) -> int :
242
+ def _get_score (db : BaseDB , block_root : Hash32 ) -> int :
243
243
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 )]
245
245
except KeyError :
246
246
raise BlockNotFound ("No block with hash {0} found" .format (
247
- encode_hex (block_hash )))
247
+ encode_hex (block_root )))
248
248
return rlp .decode (encoded_score , sedes = rlp .sedes .big_endian_int )
249
249
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 )
252
252
253
253
@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
257
257
258
258
def persist_block_chain (
259
259
self ,
@@ -278,11 +278,11 @@ def _persist_block_chain(
278
278
return tuple (), tuple ()
279
279
else :
280
280
for parent , child in sliding_window (2 , blocks ):
281
- if parent .hash != child .parent_root :
281
+ if parent .root != child .parent_root :
282
282
raise ValidationError (
283
283
"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 ),
286
286
encode_hex (child .parent_root ),
287
287
)
288
288
)
@@ -291,7 +291,7 @@ def _persist_block_chain(
291
291
if not is_genesis and not cls ._block_exists (db , first_block .parent_root ):
292
292
raise ParentNotFound (
293
293
"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 )))
295
295
296
296
if is_genesis :
297
297
score = 0
@@ -300,64 +300,64 @@ def _persist_block_chain(
300
300
301
301
for block in blocks :
302
302
db .set (
303
- block .hash ,
303
+ block .root ,
304
304
rlp .encode (block ),
305
305
)
306
306
307
307
# TODO: It's a stub before we implement fork choice rule
308
308
score = block .slot
309
309
310
310
db .set (
311
- SchemaV1 .make_block_hash_to_score_lookup_key (block .hash ),
311
+ SchemaV1 .make_block_root_to_score_lookup_key (block .root ),
312
312
rlp .encode (score , sedes = rlp .sedes .big_endian_int ),
313
313
)
314
314
315
315
try :
316
- previous_canonical_head = cls ._get_canonical_head (db ).hash
316
+ previous_canonical_head = cls ._get_canonical_head (db ).root
317
317
head_score = cls ._get_score (db , previous_canonical_head )
318
318
except CanonicalHeadNotFound :
319
- return cls ._set_as_canonical_chain_head (db , block .hash )
319
+ return cls ._set_as_canonical_chain_head (db , block .root )
320
320
321
321
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 )
323
323
else :
324
324
return tuple (), tuple ()
325
325
326
326
@classmethod
327
327
def _set_as_canonical_chain_head (
328
328
cls , db : BaseDB ,
329
- block_hash : Hash32 ) -> Tuple [Tuple [BaseBeaconBlock , ...], Tuple [BaseBeaconBlock , ...]]:
329
+ block_root : Hash32 ) -> Tuple [Tuple [BaseBeaconBlock , ...], Tuple [BaseBeaconBlock , ...]]:
330
330
"""
331
331
Set the canonical chain HEAD to the block as specified by the
332
- given block hash .
332
+ given block root .
333
333
334
334
:return: a tuple of the blocks that are newly in the canonical chain, and the blocks that
335
335
are no longer in the canonical chain
336
336
"""
337
337
try :
338
- block = cls ._get_block_by_hash (db , block_hash )
338
+ block = cls ._get_block_by_root (db , block_root )
339
339
except BlockNotFound :
340
340
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 )
342
342
)
343
343
344
344
new_canonical_blocks = tuple (reversed (cls ._find_new_ancestors (db , block )))
345
345
old_canonical_blocks = []
346
346
347
347
for block in new_canonical_blocks :
348
348
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 )
350
350
except BlockNotFound :
351
351
# no old_canonical block, and no more possible
352
352
break
353
353
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 )
355
355
old_canonical_blocks .append (old_canonical_block )
356
356
357
357
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 )
359
359
360
- db .set (SchemaV1 .make_canonical_head_hash_lookup_key (), block .hash )
360
+ db .set (SchemaV1 .make_canonical_head_root_lookup_key (), block .root )
361
361
362
362
return new_canonical_blocks , tuple (old_canonical_blocks )
363
363
@@ -382,7 +382,7 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
382
382
# This just means the block is not on the canonical chain.
383
383
pass
384
384
else :
385
- if orig .hash == block .hash :
385
+ if orig .root == block .root :
386
386
# Found the common ancestor, stop.
387
387
break
388
388
@@ -392,20 +392,20 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
392
392
if block .parent_root == GENESIS_PARENT_HASH :
393
393
break
394
394
else :
395
- block = cls ._get_block_by_hash (db , block .parent_root )
395
+ block = cls ._get_block_by_root (db , block .parent_root )
396
396
397
397
@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 :
399
399
"""
400
400
Set a record in the database to allow looking up this block by its
401
401
block slot.
402
402
"""
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 (
404
404
block .slot
405
405
)
406
406
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 ),
409
409
)
410
410
411
411
#
@@ -441,7 +441,7 @@ def _persist_state(cls,
441
441
db : BaseDB ,
442
442
state : BeaconState ) -> None :
443
443
db .set (
444
- state .hash ,
444
+ state .root ,
445
445
rlp .encode (state ),
446
446
)
447
447
0 commit comments