@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
1616use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
1717use bitcoin:: blockdata:: script:: Script ;
1818
19- use bitcoin:: hash_types:: Txid ;
19+ use bitcoin:: hash_types:: { Txid , BlockHash } ;
2020
2121use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature } ;
2222use bitcoin:: secp256k1;
@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
5858struct OnchainEventEntry {
5959 txid : Txid ,
6060 height : u32 ,
61+ block_hash : Option < BlockHash > ,
6162 event : OnchainEvent ,
6263}
6364
@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
9293 fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
9394 write_tlv_fields ! ( writer, {
9495 ( 0 , self . txid, required) ,
96+ ( 1 , self . block_hash, option) ,
9597 ( 2 , self . height, required) ,
9698 ( 4 , self . event, required) ,
9799 } ) ;
@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
103105 fn read < R : io:: Read > ( reader : & mut R ) -> Result < Option < Self > , DecodeError > {
104106 let mut txid = Txid :: all_zeros ( ) ;
105107 let mut height = 0 ;
108+ let mut block_hash = None ;
106109 let mut event = None ;
107110 read_tlv_fields ! ( reader, {
108111 ( 0 , txid, required) ,
112+ ( 1 , block_hash, option) ,
109113 ( 2 , height, required) ,
110114 ( 4 , event, ignorable) ,
111115 } ) ;
112116 if let Some ( ev) = event {
113- Ok ( Some ( Self { txid, height, event : ev } ) )
117+ Ok ( Some ( Self { txid, height, block_hash , event : ev } ) )
114118 } else {
115119 Ok ( None )
116120 }
@@ -543,17 +547,21 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
543547
544548 /// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
545549 /// for this channel, provide new relevant on-chain transactions and/or new claim requests.
546- /// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
547- /// if we receive a preimage after force-close.
548- /// `conf_height` represents the height at which the transactions in `txn_matched` were
549- /// confirmed. This does not need to equal the current blockchain tip height, which should be
550- /// provided via `cur_height`, however it must never be higher than `cur_height`.
551- pub ( crate ) fn update_claims_view < B : Deref , F : Deref , L : Deref > ( & mut self , txn_matched : & [ & Transaction ] , requests : Vec < PackageTemplate > , conf_height : u32 , cur_height : u32 , broadcaster : & B , fee_estimator : & LowerBoundedFeeEstimator < F > , logger : & L )
550+ /// Together with `update_claims_view_from_matched_txn` this used to be named
551+ /// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
552+ /// preimage after force-close.
553+ ///
554+ /// `conf_height` represents the minimal height at which the request could get confirmed. This
555+ /// does not need to equal the current blockchain tip height, which should be provided via
556+ /// `cur_height`, however it must never be higher than `cur_height`.
557+ pub ( crate ) fn update_claims_view_from_requests < B : Deref , F : Deref , L : Deref > ( & mut self ,
558+ requests : Vec < PackageTemplate > , conf_height : u32 , cur_height : u32 , broadcaster : & B ,
559+ fee_estimator : & LowerBoundedFeeEstimator < F > , logger : & L )
552560 where B :: Target : BroadcasterInterface ,
553- F :: Target : FeeEstimator ,
554- L :: Target : Logger ,
561+ F :: Target : FeeEstimator ,
562+ L :: Target : Logger ,
555563 {
556- log_debug ! ( logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests" , cur_height, txn_matched . len ( ) , conf_height , requests. len( ) ) ;
564+ log_debug ! ( logger, "Updating claims view at height {} with {} claim requests" , cur_height, requests. len( ) ) ;
557565 let mut preprocessed_requests = Vec :: with_capacity ( requests. len ( ) ) ;
558566 let mut aggregated_request = None ;
559567
@@ -634,6 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
634642 }
635643 }
636644
645+ self . process_onchain_events_awaiting_threshold_conf ( cur_height, logger) ;
646+ }
647+
648+ /// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
649+ /// for this channel, provide new relevant on-chain transactions and/or new claim requests.
650+ /// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
651+ /// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
652+ ///
653+ /// `conf_height` represents the height at which the transactions in `txn_matched` were
654+ /// confirmed. This does not need to equal the current blockchain tip height, which should be
655+ /// provided via `cur_height`, however it must never be higher than `cur_height`.
656+ pub ( crate ) fn update_claims_view_from_matched_txn < B : Deref , F : Deref , L : Deref > ( & mut self ,
657+ txn_matched : & [ & Transaction ] , conf_height : u32 , conf_hash : BlockHash , cur_height : u32 ,
658+ broadcaster : & B , fee_estimator : & LowerBoundedFeeEstimator < F > , logger : & L )
659+ where B :: Target : BroadcasterInterface ,
660+ F :: Target : FeeEstimator ,
661+ L :: Target : Logger ,
662+ {
663+ log_debug ! ( logger, "Updating claims view at height {} with {} matched transactions in block {}" , cur_height, txn_matched. len( ) , conf_height) ;
637664 let mut bump_candidates = HashMap :: new ( ) ;
638665 for tx in txn_matched {
639666 // Scan all input to verify is one of the outpoint spent is of interest for us
@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
661688 let entry = OnchainEventEntry {
662689 txid: tx. txid( ) ,
663690 height: conf_height,
691+ block_hash: Some ( conf_hash) ,
664692 event: OnchainEvent :: Claim { claim_request: first_claim_txid_height. 0 . clone( ) }
665693 } ;
666694 if !self . onchain_events_awaiting_threshold_conf. contains( & entry) {
@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
701729 let entry = OnchainEventEntry {
702730 txid : tx. txid ( ) ,
703731 height : conf_height,
732+ block_hash : Some ( conf_hash) ,
704733 event : OnchainEvent :: ContentiousOutpoint { package } ,
705734 } ;
706735 if !self . onchain_events_awaiting_threshold_conf . contains ( & entry) {
@@ -709,34 +738,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
709738 }
710739 }
711740
712- // After security delay, either our claim tx got enough confs or outpoint is definetely out of reach
713- let onchain_events_awaiting_threshold_conf =
714- self . onchain_events_awaiting_threshold_conf . drain ( ..) . collect :: < Vec < _ > > ( ) ;
715- for entry in onchain_events_awaiting_threshold_conf {
716- if entry. has_reached_confirmation_threshold ( cur_height) {
717- match entry. event {
718- OnchainEvent :: Claim { claim_request } => {
719- // We may remove a whole set of claim outpoints here, as these one may have
720- // been aggregated in a single tx and claimed so atomically
721- if let Some ( request) = self . pending_claim_requests . remove ( & claim_request) {
722- for outpoint in request. outpoints ( ) {
723- log_debug ! ( logger, "Removing claim tracking for {} due to maturation of claim tx {}." , outpoint, claim_request) ;
724- self . claimable_outpoints . remove ( & outpoint) ;
725- #[ cfg( anchors) ]
726- self . pending_claim_events . remove ( & claim_request) ;
727- }
728- }
729- } ,
730- OnchainEvent :: ContentiousOutpoint { package } => {
731- log_debug ! ( logger, "Removing claim tracking due to maturation of claim tx for outpoints:" ) ;
732- log_debug ! ( logger, " {:?}" , package. outpoints( ) ) ;
733- self . claimable_outpoints . remove ( & package. outpoints ( ) [ 0 ] ) ;
734- }
735- }
736- } else {
737- self . onchain_events_awaiting_threshold_conf . push ( entry) ;
738- }
739- }
741+ self . process_onchain_events_awaiting_threshold_conf ( cur_height, logger) ;
740742
741743 // Check if any pending claim request must be rescheduled
742744 for ( first_claim_txid, ref request) in self . pending_claim_requests . iter ( ) {
@@ -770,6 +772,39 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
770772 }
771773 }
772774
775+ fn process_onchain_events_awaiting_threshold_conf < L : Deref > ( & mut self , cur_height : u32 , logger : & L )
776+ where L :: Target : Logger ,
777+ {
778+ // After security delay, either our claim tx got enough confs or outpoint is definetely out of reach
779+ let onchain_events_awaiting_threshold_conf =
780+ self . onchain_events_awaiting_threshold_conf . drain ( ..) . collect :: < Vec < _ > > ( ) ;
781+ for entry in onchain_events_awaiting_threshold_conf {
782+ if entry. has_reached_confirmation_threshold ( cur_height) {
783+ match entry. event {
784+ OnchainEvent :: Claim { claim_request } => {
785+ // We may remove a whole set of claim outpoints here, as these one may have
786+ // been aggregated in a single tx and claimed so atomically
787+ if let Some ( request) = self . pending_claim_requests . remove ( & claim_request) {
788+ for outpoint in request. outpoints ( ) {
789+ log_debug ! ( logger, "Removing claim tracking for {} due to maturation of claim tx {}." , outpoint, claim_request) ;
790+ self . claimable_outpoints . remove ( & outpoint) ;
791+ #[ cfg( anchors) ]
792+ self . pending_claim_events . remove ( & claim_request) ;
793+ }
794+ }
795+ } ,
796+ OnchainEvent :: ContentiousOutpoint { package } => {
797+ log_debug ! ( logger, "Removing claim tracking due to maturation of claim tx for outpoints:" ) ;
798+ log_debug ! ( logger, " {:?}" , package. outpoints( ) ) ;
799+ self . claimable_outpoints . remove ( & package. outpoints ( ) [ 0 ] ) ;
800+ }
801+ }
802+ } else {
803+ self . onchain_events_awaiting_threshold_conf . push ( entry) ;
804+ }
805+ }
806+ }
807+
773808 pub ( crate ) fn transaction_unconfirmed < B : Deref , F : Deref , L : Deref > (
774809 & mut self ,
775810 txid : & Txid ,
@@ -860,12 +895,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
860895 self . claimable_outpoints . get ( outpoint) . is_some ( )
861896 }
862897
863- pub ( crate ) fn get_relevant_txids ( & self ) -> Vec < Txid > {
864- let mut txids: Vec < Txid > = self . onchain_events_awaiting_threshold_conf
898+ pub ( crate ) fn get_relevant_txids ( & self ) -> Vec < ( Txid , Option < BlockHash > ) > {
899+ let mut txids: Vec < ( Txid , Option < BlockHash > ) > = self . onchain_events_awaiting_threshold_conf
865900 . iter ( )
866- . map ( |entry| entry. txid )
901+ . map ( |entry| ( entry. txid , entry . block_hash ) )
867902 . collect ( ) ;
868- txids. sort_unstable ( ) ;
903+ txids. sort_unstable_by_key ( | ( txid , _ ) | * txid ) ;
869904 txids. dedup ( ) ;
870905 txids
871906 }
0 commit comments