@@ -398,10 +398,18 @@ def _flush_utxo_db(self, batch, flush_data: FlushData) -> None:
398398 batch_put (b'u' + hashX + suffix , value_sats )
399399 flush_data .adds .clear ()
400400
401- # New undo information
401+ # Add new undo information
402402 self .flush_undo_infos (batch_put , flush_data .undo_infos )
403403 flush_data .undo_infos .clear ()
404-
404+ # Delete old undo information
405+ if not self .utxo_db .for_sync : # undo infos were only added if we are nearly caught up anyway
406+ old_min_height = max (0 , self .min_undo_height (self .db_height ))
407+ new_min_height = max (0 , self .min_undo_height (flush_data .height ))
408+ for h in range (old_min_height , new_min_height ):
409+ key = self .undo_key (h )
410+ batch_delete (key )
411+
412+ # Log stats
405413 if self .utxo_db .for_sync :
406414 block_count = flush_data .height - self .db_height
407415 tx_count = flush_data .tx_count - self .db_tx_count
@@ -632,21 +640,23 @@ def write_raw_block(self, block: bytes, height: int) -> None:
632640 with util .open_truncate (self .raw_block_path (height )) as f :
633641 f .write (block )
634642 # Delete old blocks to prevent them accumulating
635- try :
636- del_height = self .min_undo_height (height ) - 1
637- os .remove (self .raw_block_path (del_height ))
638- except FileNotFoundError :
639- pass
643+ del_height = self .min_undo_height (height ) - 1
644+ if del_height >= 0 :
645+ try :
646+ os .remove (self .raw_block_path (del_height ))
647+ except FileNotFoundError :
648+ pass
640649
641650 def clear_excess_undo_info (self ) -> None :
642651 '''Clear excess undo info. Only most recent N are kept.'''
643- prefix = b'U'
652+ # delete aged undo_infos from utxo db
653+ # note: this is just a fallback cleanup path - normally already deleted by _flush_utxo_db()
644654 min_height = self .min_undo_height (self .db_height )
645655 keys = []
646- for key , _hist in self .utxo_db .iterator (prefix = prefix ):
656+ for key , uhist in self .utxo_db .iterator (prefix = b'U' ):
647657 height = unpack_block_height (key [- BHEIGHT_LEN :])
648658 if height >= min_height :
649- break
659+ break # can break as block_height is encoded as big endian
650660 keys .append (key )
651661
652662 if keys :
@@ -656,6 +666,7 @@ def clear_excess_undo_info(self) -> None:
656666 self .logger .info (f'deleted { len (keys ):,d} stale undo entries' )
657667
658668 # delete old block files
669+ # note: this is just a fallback cleanup path - normally already deleted by write_raw_block()
659670 prefix = self .raw_block_prefix ()
660671 paths = [path for path in glob (f'{ prefix } [0-9]*' )
661672 if len (path ) > len (prefix )
0 commit comments