@@ -94,7 +94,9 @@ def get_block_uncles(self, uncles_hash: Hash32) -> List[BlockHeader]:
9494 # Block API
9595 #
9696 @abstractmethod
97- def persist_block (self , block : 'BaseBlock' ) -> None :
97+ def persist_block (self ,
98+ block : 'BaseBlock'
99+ ) -> Tuple [Tuple [bytes , ...], Tuple [bytes , ...]]:
98100 raise NotImplementedError ("ChainDB classes must implement this method" )
99101
100102 @abstractmethod
@@ -186,7 +188,9 @@ def get_block_uncles(self, uncles_hash: Hash32) -> List[BlockHeader]:
186188
187189 # TODO: This method should take a chain of headers as that's the most common use case
188190 # and it'd be much faster than inserting each header individually.
189- def persist_header (self , header : BlockHeader ) -> Tuple [BlockHeader , ...]:
191+ def persist_header (self ,
192+ header : BlockHeader
193+ ) -> Tuple [Tuple [BlockHeader , ...], Tuple [BlockHeader , ...]]:
190194 """
191195 Returns iterable of headers newly on the canonical chain
192196 """
@@ -214,17 +218,26 @@ def persist_header(self, header: BlockHeader) -> Tuple[BlockHeader, ...]:
214218 try :
215219 head_score = self .get_score (self .get_canonical_head ().hash )
216220 except CanonicalHeadNotFound :
217- new_headers = self ._set_as_canonical_chain_head (header )
221+ (
222+ new_canonical_headers ,
223+ orphaned_canonical_headers
224+ ) = self ._set_as_canonical_chain_head (header )
218225 else :
219226 if score > head_score :
220- new_headers = self ._set_as_canonical_chain_head (header )
227+ (
228+ new_canonical_headers ,
229+ orphaned_canonical_headers
230+ ) = self ._set_as_canonical_chain_head (header )
221231 else :
222- new_headers = tuple ()
232+ new_canonical_headers = tuple ()
233+ orphaned_canonical_headers = tuple ()
223234
224- return new_headers
235+ return new_canonical_headers , orphaned_canonical_headers
225236
226237 # TODO: update this to take a `hash` rather than a full header object.
227- def _set_as_canonical_chain_head (self , header : BlockHeader ) -> Tuple [BlockHeader , ...]:
238+ def _set_as_canonical_chain_head (self ,
239+ header : BlockHeader
240+ ) -> Tuple [Tuple [BlockHeader , ...], Tuple [BlockHeader , ...]]:
228241 """
229242 Returns iterable of headers newly on the canonical head
230243 """
@@ -235,7 +248,7 @@ def _set_as_canonical_chain_head(self, header: BlockHeader) -> Tuple[BlockHeader
235248 header .hash ))
236249
237250 new_canonical_headers = tuple (reversed (self ._find_new_ancestors (header )))
238-
251+ orphaned_canonical_headers = []
239252 # remove transaction lookups for blocks that are no longer canonical
240253 for h in new_canonical_headers :
241254 try :
@@ -244,29 +257,30 @@ def _set_as_canonical_chain_head(self, header: BlockHeader) -> Tuple[BlockHeader
244257 # no old block, and no more possible
245258 break
246259 else :
247- old_header = self .get_block_header_by_hash (old_hash )
248- for transaction_hash in self .get_block_transaction_hashes (old_header ):
260+ orphaned_header = self .get_block_header_by_hash (old_hash )
261+ orphaned_canonical_headers .append (orphaned_header )
262+ for transaction_hash in self .get_block_transaction_hashes (orphaned_header ):
249263 self ._remove_transaction_from_canonical_chain (transaction_hash )
250- # TODO re-add txn to internal pending pool (only if local sender)
251- pass
252264
253265 for h in new_canonical_headers :
254266 self ._add_block_number_to_hash_lookup (h )
255267
256268 self .db .set (SchemaV1 .make_canonical_head_hash_lookup_key (), header .hash )
257269
258- return new_canonical_headers
270+ return new_canonical_headers , tuple ( orphaned_canonical_headers )
259271
260272 #
261273 # Block API
262274 #
263- def persist_block (self , block : 'BaseBlock' ) -> None :
275+ def persist_block (self ,
276+ block : 'BaseBlock'
277+ ) -> Tuple [Tuple [bytes , ...], Tuple [bytes , ...]]:
264278 '''
265279 Persist the given block's header and uncles.
266280
267281 Assumes all block transactions have been persisted already.
268282 '''
269- new_canonical_headers = self .persist_header (block .header )
283+ new_canonical_headers , orphaned_canonical_headers = self .persist_header (block .header )
270284
271285 for header in new_canonical_headers :
272286 if header .hash == block .hash :
@@ -288,6 +302,11 @@ def persist_block(self, block: 'BaseBlock') -> None:
288302 raise ValidationError (
289303 "Block's uncles_hash (%s) does not match actual uncles' hash (%s)" ,
290304 block .header .uncles_hash , uncles_hash )
305+ new_canonical_header_hashes = tuple (header .hash for header in new_canonical_headers )
306+ orphaned_canonical_header_hashes = tuple (
307+ header .hash for header in orphaned_canonical_headers )
308+
309+ return new_canonical_header_hashes , orphaned_canonical_header_hashes
291310
292311 def persist_uncles (self , uncles : Tuple [BlockHeader ]) -> Hash32 :
293312 """
0 commit comments