@@ -32,6 +32,7 @@ use crate::ln::PaymentPreimage;
3232#[ cfg( anchors) ]
3333use crate :: ln:: chan_utils:: { self , HTLCOutputInCommitment } ;
3434use crate :: ln:: chan_utils:: { ChannelTransactionParameters , HolderCommitmentTransaction } ;
35+ use crate :: chain:: ClaimId ;
3536#[ cfg( anchors) ]
3637use crate :: chain:: chaininterface:: ConfirmationTarget ;
3738use crate :: chain:: chaininterface:: { FeeEstimator , BroadcasterInterface , LowerBoundedFeeEstimator } ;
@@ -85,7 +86,7 @@ enum OnchainEvent {
8586 /// transaction has met [`ANTI_REORG_DELAY`] confirmations, we consider it final and remove the
8687 /// pending request.
8788 Claim {
88- package_id : PackageID ,
89+ claim_id : ClaimId ,
8990 } ,
9091 /// The counterparty has claimed an outpoint from one of our pending requests through a
9192 /// different transaction than ours. If our transaction was attempting to claim multiple
@@ -128,7 +129,7 @@ impl MaybeReadable for OnchainEventEntry {
128129
129130impl_writeable_tlv_based_enum_upgradable ! ( OnchainEvent ,
130131 ( 0 , Claim ) => {
131- ( 0 , package_id , required) ,
132+ ( 0 , claim_id , required) ,
132133 } ,
133134 ( 1 , ContentiousOutpoint ) => {
134135 ( 0 , package, required) ,
@@ -220,9 +221,6 @@ pub(crate) enum OnchainClaim {
220221 Event ( ClaimEvent ) ,
221222}
222223
223- /// An internal identifier to track pending package claims within the `OnchainTxHandler`.
224- type PackageID = [ u8 ; 32 ] ;
225-
226224/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
227225/// do RBF bumping if possible.
228226pub struct OnchainTxHandler < ChannelSigner : WriteableEcdsaChannelSigner > {
@@ -250,13 +248,13 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
250248 // us and is immutable until all outpoint of the claimable set are post-anti-reorg-delay solved.
251249 // Entry is cache of elements need to generate a bumped claiming transaction (see ClaimTxBumpMaterial)
252250 #[ cfg( test) ] // Used in functional_test to verify sanitization
253- pub ( crate ) pending_claim_requests : HashMap < PackageID , PackageTemplate > ,
251+ pub ( crate ) pending_claim_requests : HashMap < ClaimId , PackageTemplate > ,
254252 #[ cfg( not( test) ) ]
255- pending_claim_requests : HashMap < PackageID , PackageTemplate > ,
253+ pending_claim_requests : HashMap < ClaimId , PackageTemplate > ,
256254
257255 // Used to track external events that need to be forwarded to the `ChainMonitor`. This `Vec`
258256 // essentially acts as an insertion-ordered `HashMap` – there should only ever be one occurrence
259- // of a `PackageID `, which tracks its latest `ClaimEvent`, i.e., if a pending claim exists, and
257+ // of a `ClaimId `, which tracks its latest `ClaimEvent`, i.e., if a pending claim exists, and
260258 // a new block has been connected, resulting in a new claim, the previous will be replaced with
261259 // the new.
262260 //
@@ -265,7 +263,7 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
265263 // - A block being connected/disconnected
266264 // - Learning the preimage for an HTLC we can claim onchain
267265 #[ cfg( anchors) ]
268- pending_claim_events : Vec < ( PackageID , ClaimEvent ) > ,
266+ pending_claim_events : Vec < ( ClaimId , ClaimEvent ) > ,
269267
270268 // Used to link outpoints claimed in a connected block to a pending claim request. The keys
271269 // represent the outpoints that our `ChannelMonitor` has detected we have keys/scripts to
@@ -274,9 +272,9 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
274272 // [`ANTI_REORG_DELAY`]. The initial confirmation block height is used to remove the entry if
275273 // the block gets disconnected.
276274 #[ cfg( test) ] // Used in functional_test to verify sanitization
277- pub claimable_outpoints : HashMap < BitcoinOutPoint , ( PackageID , u32 ) > ,
275+ pub claimable_outpoints : HashMap < BitcoinOutPoint , ( ClaimId , u32 ) > ,
278276 #[ cfg( not( test) ) ]
279- claimable_outpoints : HashMap < BitcoinOutPoint , ( PackageID , u32 ) > ,
277+ claimable_outpoints : HashMap < BitcoinOutPoint , ( ClaimId , u32 ) > ,
280278
281279 locktimed_packages : BTreeMap < u32 , Vec < PackageTemplate > > ,
282280
@@ -498,16 +496,16 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
498496 L :: Target : Logger ,
499497 {
500498 let mut bump_requests = Vec :: with_capacity ( self . pending_claim_requests . len ( ) ) ;
501- for ( package_id , request) in self . pending_claim_requests . iter ( ) {
499+ for ( claim_id , request) in self . pending_claim_requests . iter ( ) {
502500 let inputs = request. outpoints ( ) ;
503501 log_info ! ( logger, "Triggering rebroadcast/fee-bump for request with inputs {:?}" , inputs) ;
504- bump_requests. push ( ( * package_id , request. clone ( ) ) ) ;
502+ bump_requests. push ( ( * claim_id , request. clone ( ) ) ) ;
505503 }
506- for ( package_id , request) in bump_requests {
504+ for ( claim_id , request) in bump_requests {
507505 self . generate_claim ( current_height, & request, false /* force_feerate_bump */ , fee_estimator, logger)
508506 . map ( |( _, new_feerate, claim) | {
509507 let mut bumped_feerate = false ;
510- if let Some ( mut_request) = self . pending_claim_requests . get_mut ( & package_id ) {
508+ if let Some ( mut_request) = self . pending_claim_requests . get_mut ( & claim_id ) {
511509 bumped_feerate = request. previous_feerate ( ) > new_feerate;
512510 mut_request. set_feerate ( new_feerate) ;
513511 }
@@ -525,11 +523,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
525523 #[ cfg( debug_assertions) ] {
526524 debug_assert ! ( request. requires_external_funding( ) ) ;
527525 let num_existing = self . pending_claim_events . iter ( )
528- . filter ( |entry| entry. 0 == package_id ) . count ( ) ;
526+ . filter ( |entry| entry. 0 == claim_id ) . count ( ) ;
529527 assert ! ( num_existing == 0 || num_existing == 1 ) ;
530528 }
531- self . pending_claim_events . retain ( |event| event. 0 != package_id ) ;
532- self . pending_claim_events . push ( ( package_id , event) ) ;
529+ self . pending_claim_events . retain ( |event| event. 0 != claim_id ) ;
530+ self . pending_claim_events . push ( ( claim_id , event) ) ;
533531 }
534532 }
535533 } ) ;
@@ -566,12 +564,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
566564 // transaction is reorged out.
567565 let mut all_inputs_have_confirmed_spend = true ;
568566 for outpoint in request_outpoints. iter ( ) {
569- if let Some ( ( request_package_id , _) ) = self . claimable_outpoints . get ( * outpoint) {
567+ if let Some ( ( request_claim_id , _) ) = self . claimable_outpoints . get ( * outpoint) {
570568 // We check for outpoint spends within claims individually rather than as a set
571569 // since requests can have outpoints split off.
572570 if !self . onchain_events_awaiting_threshold_conf . iter ( )
573- . any ( |event_entry| if let OnchainEvent :: Claim { package_id } = event_entry. event {
574- * request_package_id == package_id
571+ . any ( |event_entry| if let OnchainEvent :: Claim { claim_id } = event_entry. event {
572+ * request_claim_id == claim_id
575573 } else {
576574 // The onchain event is not a claim, keep seeking until we find one.
577575 false
@@ -766,20 +764,20 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
766764 ) {
767765 req. set_timer ( new_timer) ;
768766 req. set_feerate ( new_feerate) ;
769- let package_id = match claim {
767+ let claim_id = match claim {
770768 OnchainClaim :: Tx ( tx) => {
771769 log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( tx) ) ;
772770 broadcaster. broadcast_transactions ( & [ & tx] ) ;
773- tx. txid ( ) . into_inner ( )
771+ ClaimId ( tx. txid ( ) . into_inner ( ) )
774772 } ,
775773 #[ cfg( anchors) ]
776774 OnchainClaim :: Event ( claim_event) => {
777775 log_info ! ( logger, "Yielding onchain event to spend inputs {:?}" , req. outpoints( ) ) ;
778- let package_id = match claim_event {
776+ let claim_id = match claim_event {
779777 ClaimEvent :: BumpCommitment { ref commitment_tx, .. } =>
780778 // For commitment claims, we can just use their txid as it should
781779 // already be unique.
782- commitment_tx. txid ( ) . into_inner ( ) ,
780+ ClaimId ( commitment_tx. txid ( ) . into_inner ( ) ) ,
783781 ClaimEvent :: BumpHTLC { ref htlcs, .. } => {
784782 // For HTLC claims, commit to the entire set of HTLC outputs to
785783 // claim, which will always be unique per request. Once a claim ID
@@ -790,20 +788,21 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
790788 engine. input ( & htlc. commitment_txid . into_inner ( ) ) ;
791789 engine. input ( & htlc. htlc . transaction_output_index . unwrap ( ) . to_be_bytes ( ) ) ;
792790 }
793- Sha256 :: from_engine ( engine) . into_inner ( )
791+ ClaimId ( Sha256 :: from_engine ( engine) . into_inner ( ) )
794792 } ,
795793 } ;
796- debug_assert ! ( self . pending_claim_requests. get( & package_id ) . is_none( ) ) ;
797- debug_assert_eq ! ( self . pending_claim_events. iter( ) . filter( |entry| entry. 0 == package_id ) . count( ) , 0 ) ;
798- self . pending_claim_events . push ( ( package_id , claim_event) ) ;
799- package_id
794+ debug_assert ! ( self . pending_claim_requests. get( & claim_id ) . is_none( ) ) ;
795+ debug_assert_eq ! ( self . pending_claim_events. iter( ) . filter( |entry| entry. 0 == claim_id ) . count( ) , 0 ) ;
796+ self . pending_claim_events . push ( ( claim_id , claim_event) ) ;
797+ claim_id
800798 } ,
801799 } ;
800+ debug_assert ! ( self . pending_claim_requests. get( & claim_id) . is_none( ) ) ;
802801 for k in req. outpoints ( ) {
803802 log_info ! ( logger, "Registering claiming request for {}:{}" , k. txid, k. vout) ;
804- self . claimable_outpoints . insert ( k. clone ( ) , ( package_id , conf_height) ) ;
803+ self . claimable_outpoints . insert ( k. clone ( ) , ( claim_id , conf_height) ) ;
805804 }
806- self . pending_claim_requests . insert ( package_id , req) ;
805+ self . pending_claim_requests . insert ( claim_id , req) ;
807806 }
808807 }
809808 }
@@ -830,9 +829,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
830829 // Scan all input to verify is one of the outpoint spent is of interest for us
831830 let mut claimed_outputs_material = Vec :: new ( ) ;
832831 for inp in & tx. input {
833- if let Some ( ( package_id , _) ) = self . claimable_outpoints . get ( & inp. previous_output ) {
832+ if let Some ( ( claim_id , _) ) = self . claimable_outpoints . get ( & inp. previous_output ) {
834833 // If outpoint has claim request pending on it...
835- if let Some ( request) = self . pending_claim_requests . get_mut ( package_id ) {
834+ if let Some ( request) = self . pending_claim_requests . get_mut ( claim_id ) {
836835 //... we need to verify equality between transaction outpoints and claim request
837836 // outpoints to know if transaction is the original claim or a bumped one issued
838837 // by us.
@@ -852,7 +851,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
852851 txid: tx. txid( ) ,
853852 height: conf_height,
854853 block_hash: Some ( conf_hash) ,
855- event: OnchainEvent :: Claim { package_id : * package_id }
854+ event: OnchainEvent :: Claim { claim_id : * claim_id }
856855 } ;
857856 if !self . onchain_events_awaiting_threshold_conf. contains( & entry) {
858857 self . onchain_events_awaiting_threshold_conf. push( entry) ;
@@ -879,7 +878,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
879878 }
880879 //TODO: recompute soonest_timelock to avoid wasting a bit on fees
881880 if at_least_one_drop {
882- bump_candidates. insert ( * package_id , request. clone ( ) ) ;
881+ bump_candidates. insert ( * claim_id , request. clone ( ) ) ;
883882 // If we have any pending claim events for the request being updated
884883 // that have yet to be consumed, we'll remove them since they will
885884 // end up producing an invalid transaction by double spending
@@ -889,10 +888,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
889888 #[ cfg( anchors) ] {
890889 #[ cfg( debug_assertions) ] {
891890 let existing = self . pending_claim_events . iter ( )
892- . filter ( |entry| entry. 0 == * package_id ) . count ( ) ;
891+ . filter ( |entry| entry. 0 == * claim_id ) . count ( ) ;
893892 assert ! ( existing == 0 || existing == 1 ) ;
894893 }
895- self . pending_claim_events . retain ( |entry| entry. 0 != * package_id ) ;
894+ self . pending_claim_events . retain ( |entry| entry. 0 != * claim_id ) ;
896895 }
897896 }
898897 }
@@ -921,22 +920,22 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
921920 for entry in onchain_events_awaiting_threshold_conf {
922921 if entry. has_reached_confirmation_threshold ( cur_height) {
923922 match entry. event {
924- OnchainEvent :: Claim { package_id } => {
923+ OnchainEvent :: Claim { claim_id } => {
925924 // We may remove a whole set of claim outpoints here, as these one may have
926925 // been aggregated in a single tx and claimed so atomically
927- if let Some ( request) = self . pending_claim_requests . remove ( & package_id ) {
926+ if let Some ( request) = self . pending_claim_requests . remove ( & claim_id ) {
928927 for outpoint in request. outpoints ( ) {
929928 log_debug ! ( logger, "Removing claim tracking for {} due to maturation of claim package {}." ,
930- outpoint, log_bytes!( package_id ) ) ;
929+ outpoint, log_bytes!( claim_id . 0 ) ) ;
931930 self . claimable_outpoints . remove ( outpoint) ;
932931 }
933932 #[ cfg( anchors) ] {
934933 #[ cfg( debug_assertions) ] {
935934 let num_existing = self . pending_claim_events . iter ( )
936- . filter ( |entry| entry. 0 == package_id ) . count ( ) ;
935+ . filter ( |entry| entry. 0 == claim_id ) . count ( ) ;
937936 assert ! ( num_existing == 0 || num_existing == 1 ) ;
938937 }
939- self . pending_claim_events . retain ( |( id, _) | * id != package_id ) ;
938+ self . pending_claim_events . retain ( |( id, _) | * id != claim_id ) ;
940939 }
941940 }
942941 } ,
@@ -952,15 +951,15 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
952951 }
953952
954953 // Check if any pending claim request must be rescheduled
955- for ( package_id , request) in self . pending_claim_requests . iter ( ) {
954+ for ( claim_id , request) in self . pending_claim_requests . iter ( ) {
956955 if cur_height >= request. timer ( ) {
957- bump_candidates. insert ( * package_id , request. clone ( ) ) ;
956+ bump_candidates. insert ( * claim_id , request. clone ( ) ) ;
958957 }
959958 }
960959
961960 // Build, bump and rebroadcast tx accordingly
962961 log_trace ! ( logger, "Bumping {} candidates" , bump_candidates. len( ) ) ;
963- for ( package_id , request) in bump_candidates. iter ( ) {
962+ for ( claim_id , request) in bump_candidates. iter ( ) {
964963 if let Some ( ( new_timer, new_feerate, bump_claim) ) = self . generate_claim (
965964 cur_height, & request, true /* force_feerate_bump */ , & * fee_estimator, & * logger,
966965 ) {
@@ -974,14 +973,14 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
974973 log_info ! ( logger, "Yielding RBF-bumped onchain event to spend inputs {:?}" , request. outpoints( ) ) ;
975974 #[ cfg( debug_assertions) ] {
976975 let num_existing = self . pending_claim_events . iter ( ) .
977- filter ( |entry| entry. 0 == * package_id ) . count ( ) ;
976+ filter ( |entry| entry. 0 == * claim_id ) . count ( ) ;
978977 assert ! ( num_existing == 0 || num_existing == 1 ) ;
979978 }
980- self . pending_claim_events . retain ( |event| event. 0 != * package_id ) ;
981- self . pending_claim_events . push ( ( * package_id , claim_event) ) ;
979+ self . pending_claim_events . retain ( |event| event. 0 != * claim_id ) ;
980+ self . pending_claim_events . push ( ( * claim_id , claim_event) ) ;
982981 } ,
983982 }
984- if let Some ( request) = self . pending_claim_requests . get_mut ( package_id ) {
983+ if let Some ( request) = self . pending_claim_requests . get_mut ( claim_id ) {
985984 request. set_timer ( new_timer) ;
986985 request. set_feerate ( new_feerate) ;
987986 }
@@ -1042,7 +1041,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
10421041 self . onchain_events_awaiting_threshold_conf . push ( entry) ;
10431042 }
10441043 }
1045- for ( ( _package_id , _) , ref mut request) in bump_candidates. iter_mut ( ) {
1044+ for ( ( _claim_id , _) , ref mut request) in bump_candidates. iter_mut ( ) {
10461045 // `height` is the height being disconnected, so our `current_height` is 1 lower.
10471046 let current_height = height - 1 ;
10481047 if let Some ( ( new_timer, new_feerate, bump_claim) ) = self . generate_claim (
@@ -1060,11 +1059,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
10601059 log_info ! ( logger, "Yielding onchain event after reorg to spend inputs {:?}" , request. outpoints( ) ) ;
10611060 #[ cfg( debug_assertions) ] {
10621061 let num_existing = self . pending_claim_events . iter ( )
1063- . filter ( |entry| entry. 0 == * _package_id ) . count ( ) ;
1062+ . filter ( |entry| entry. 0 == * _claim_id ) . count ( ) ;
10641063 assert ! ( num_existing == 0 || num_existing == 1 ) ;
10651064 }
1066- self . pending_claim_events . retain ( |event| event. 0 != * _package_id ) ;
1067- self . pending_claim_events . push ( ( * _package_id , claim_event) ) ;
1065+ self . pending_claim_events . retain ( |event| event. 0 != * _claim_id ) ;
1066+ self . pending_claim_events . push ( ( * _claim_id , claim_event) ) ;
10681067 } ,
10691068 }
10701069 }
0 commit comments