@@ -634,20 +634,25 @@ impl<A: Anchor> TxGraph<A> {
634
634
/// The [`ChangeSet`] returned will be empty if graph already knows that `txid` exists in
635
635
/// `anchor`.
636
636
pub fn insert_anchor ( & mut self , txid : Txid , anchor : A ) -> ChangeSet < A > {
637
+ // These two variables are used to determine how to modify the `txid`'s entry in
638
+ // `txs_by_highest_conf_heights`.
639
+ // We want to remove `(old_top_h?, txid)` and insert `(new_top_h?, txid)`.
637
640
let mut old_top_h = None ;
638
641
let mut new_top_h = anchor. confirmation_height_upper_bound ( ) ;
639
642
640
643
let is_changed = match self . anchors . entry ( txid) {
641
644
hash_map:: Entry :: Occupied ( mut e) => {
642
- fn top_anchor_height < A : Anchor > ( anchors : & BTreeSet < A > ) -> Option < u32 > {
643
- anchors
644
- . iter ( )
645
- . last ( )
646
- . map ( Anchor :: confirmation_height_upper_bound)
645
+ old_top_h = e
646
+ . get ( )
647
+ . iter ( )
648
+ . last ( )
649
+ . map ( Anchor :: confirmation_height_upper_bound) ;
650
+ if let Some ( old_top_h) = old_top_h {
651
+ if old_top_h > new_top_h {
652
+ new_top_h = old_top_h;
653
+ }
647
654
}
648
- old_top_h = top_anchor_height ( e. get ( ) ) ;
649
655
let is_changed = e. get_mut ( ) . insert ( anchor. clone ( ) ) ;
650
- new_top_h = top_anchor_height ( e. get ( ) ) . expect ( "must have anchor" ) ;
651
656
is_changed
652
657
}
653
658
hash_map:: Entry :: Vacant ( e) => {
@@ -658,7 +663,12 @@ impl<A: Anchor> TxGraph<A> {
658
663
659
664
let mut changeset = ChangeSet :: < A > :: default ( ) ;
660
665
if is_changed {
661
- if old_top_h != Some ( new_top_h) {
666
+ let new_top_is_changed = match old_top_h {
667
+ None => true ,
668
+ Some ( old_top_h) if old_top_h != new_top_h => true ,
669
+ _ => false ,
670
+ } ;
671
+ if new_top_is_changed {
662
672
if let Some ( prev_top_h) = old_top_h {
663
673
self . txs_by_highest_conf_heights . remove ( & ( prev_top_h, txid) ) ;
664
674
}
0 commit comments