@@ -32,6 +32,7 @@ use crate::ln::PaymentPreimage;
32
32
#[ cfg( anchors) ]
33
33
use crate :: ln:: chan_utils:: { self , HTLCOutputInCommitment } ;
34
34
use crate :: ln:: chan_utils:: { ChannelTransactionParameters , HolderCommitmentTransaction } ;
35
+ use crate :: chain:: ClaimId ;
35
36
#[ cfg( anchors) ]
36
37
use crate :: chain:: chaininterface:: ConfirmationTarget ;
37
38
use crate :: chain:: chaininterface:: { FeeEstimator , BroadcasterInterface , LowerBoundedFeeEstimator } ;
@@ -85,7 +86,7 @@ enum OnchainEvent {
85
86
/// transaction has met [`ANTI_REORG_DELAY`] confirmations, we consider it final and remove the
86
87
/// pending request.
87
88
Claim {
88
- package_id : PackageID ,
89
+ claim_id : ClaimId ,
89
90
} ,
90
91
/// The counterparty has claimed an outpoint from one of our pending requests through a
91
92
/// different transaction than ours. If our transaction was attempting to claim multiple
@@ -128,7 +129,7 @@ impl MaybeReadable for OnchainEventEntry {
128
129
129
130
impl_writeable_tlv_based_enum_upgradable ! ( OnchainEvent ,
130
131
( 0 , Claim ) => {
131
- ( 0 , package_id , required) ,
132
+ ( 0 , claim_id , required) ,
132
133
} ,
133
134
( 1 , ContentiousOutpoint ) => {
134
135
( 0 , package, required) ,
@@ -220,9 +221,6 @@ pub(crate) enum OnchainClaim {
220
221
Event ( ClaimEvent ) ,
221
222
}
222
223
223
- /// An internal identifier to track pending package claims within the `OnchainTxHandler`.
224
- type PackageID = [ u8 ; 32 ] ;
225
-
226
224
/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
227
225
/// do RBF bumping if possible.
228
226
pub struct OnchainTxHandler < ChannelSigner : WriteableEcdsaChannelSigner > {
@@ -250,13 +248,13 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
250
248
// us and is immutable until all outpoint of the claimable set are post-anti-reorg-delay solved.
251
249
// Entry is cache of elements need to generate a bumped claiming transaction (see ClaimTxBumpMaterial)
252
250
#[ 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 > ,
254
252
#[ cfg( not( test) ) ]
255
- pending_claim_requests : HashMap < PackageID , PackageTemplate > ,
253
+ pending_claim_requests : HashMap < ClaimId , PackageTemplate > ,
256
254
257
255
// Used to track external events that need to be forwarded to the `ChainMonitor`. This `Vec`
258
256
// 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
260
258
// a new block has been connected, resulting in a new claim, the previous will be replaced with
261
259
// the new.
262
260
//
@@ -265,7 +263,7 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
265
263
// - A block being connected/disconnected
266
264
// - Learning the preimage for an HTLC we can claim onchain
267
265
#[ cfg( anchors) ]
268
- pending_claim_events : Vec < ( PackageID , ClaimEvent ) > ,
266
+ pending_claim_events : Vec < ( ClaimId , ClaimEvent ) > ,
269
267
270
268
// Used to link outpoints claimed in a connected block to a pending claim request. The keys
271
269
// represent the outpoints that our `ChannelMonitor` has detected we have keys/scripts to
@@ -274,9 +272,9 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
274
272
// [`ANTI_REORG_DELAY`]. The initial confirmation block height is used to remove the entry if
275
273
// the block gets disconnected.
276
274
#[ 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 ) > ,
278
276
#[ cfg( not( test) ) ]
279
- claimable_outpoints : HashMap < BitcoinOutPoint , ( PackageID , u32 ) > ,
277
+ claimable_outpoints : HashMap < BitcoinOutPoint , ( ClaimId , u32 ) > ,
280
278
281
279
locktimed_packages : BTreeMap < u32 , Vec < PackageTemplate > > ,
282
280
@@ -498,16 +496,16 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
498
496
L :: Target : Logger ,
499
497
{
500
498
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 ( ) {
502
500
let inputs = request. outpoints ( ) ;
503
501
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 ( ) ) ) ;
505
503
}
506
- for ( package_id , request) in bump_requests {
504
+ for ( claim_id , request) in bump_requests {
507
505
self . generate_claim ( current_height, & request, false /* force_feerate_bump */ , fee_estimator, logger)
508
506
. map ( |( _, new_feerate, claim) | {
509
507
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 ) {
511
509
bumped_feerate = request. previous_feerate ( ) > new_feerate;
512
510
mut_request. set_feerate ( new_feerate) ;
513
511
}
@@ -525,11 +523,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
525
523
#[ cfg( debug_assertions) ] {
526
524
debug_assert ! ( request. requires_external_funding( ) ) ;
527
525
let num_existing = self . pending_claim_events . iter ( )
528
- . filter ( |entry| entry. 0 == package_id ) . count ( ) ;
526
+ . filter ( |entry| entry. 0 == claim_id ) . count ( ) ;
529
527
assert ! ( num_existing == 0 || num_existing == 1 ) ;
530
528
}
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) ) ;
533
531
}
534
532
}
535
533
} ) ;
@@ -566,12 +564,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
566
564
// transaction is reorged out.
567
565
let mut all_inputs_have_confirmed_spend = true ;
568
566
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) {
570
568
// We check for outpoint spends within claims individually rather than as a set
571
569
// since requests can have outpoints split off.
572
570
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
575
573
} else {
576
574
// The onchain event is not a claim, keep seeking until we find one.
577
575
false
@@ -766,20 +764,20 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
766
764
) {
767
765
req. set_timer ( new_timer) ;
768
766
req. set_feerate ( new_feerate) ;
769
- let package_id = match claim {
767
+ let claim_id = match claim {
770
768
OnchainClaim :: Tx ( tx) => {
771
769
log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( tx) ) ;
772
770
broadcaster. broadcast_transactions ( & [ & tx] ) ;
773
- tx. txid ( ) . into_inner ( )
771
+ ClaimId ( tx. txid ( ) . into_inner ( ) )
774
772
} ,
775
773
#[ cfg( anchors) ]
776
774
OnchainClaim :: Event ( claim_event) => {
777
775
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 {
779
777
ClaimEvent :: BumpCommitment { ref commitment_tx, .. } =>
780
778
// For commitment claims, we can just use their txid as it should
781
779
// already be unique.
782
- commitment_tx. txid ( ) . into_inner ( ) ,
780
+ ClaimId ( commitment_tx. txid ( ) . into_inner ( ) ) ,
783
781
ClaimEvent :: BumpHTLC { ref htlcs, .. } => {
784
782
// For HTLC claims, commit to the entire set of HTLC outputs to
785
783
// claim, which will always be unique per request. Note that, even
@@ -790,20 +788,21 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
790
788
engine. input ( & htlc. commitment_txid . into_inner ( ) ) ;
791
789
engine. input ( & htlc. htlc . transaction_output_index . unwrap ( ) . to_be_bytes ( ) ) ;
792
790
}
793
- Sha256 :: from_engine ( engine) . into_inner ( )
791
+ ClaimId ( Sha256 :: from_engine ( engine) . into_inner ( ) )
794
792
} ,
795
793
} ;
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
800
798
} ,
801
799
} ;
800
+ debug_assert ! ( self . pending_claim_requests. get( & claim_id) . is_none( ) ) ;
802
801
for k in req. outpoints ( ) {
803
802
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) ) ;
805
804
}
806
- self . pending_claim_requests . insert ( package_id , req) ;
805
+ self . pending_claim_requests . insert ( claim_id , req) ;
807
806
}
808
807
}
809
808
}
@@ -830,9 +829,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
830
829
// Scan all input to verify is one of the outpoint spent is of interest for us
831
830
let mut claimed_outputs_material = Vec :: new ( ) ;
832
831
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 ) {
834
833
// 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 ) {
836
835
//... we need to verify equality between transaction outpoints and claim request
837
836
// outpoints to know if transaction is the original claim or a bumped one issued
838
837
// by us.
@@ -852,7 +851,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
852
851
txid: tx. txid( ) ,
853
852
height: conf_height,
854
853
block_hash: Some ( conf_hash) ,
855
- event: OnchainEvent :: Claim { package_id : * package_id }
854
+ event: OnchainEvent :: Claim { claim_id : * claim_id }
856
855
} ;
857
856
if !self . onchain_events_awaiting_threshold_conf. contains( & entry) {
858
857
self . onchain_events_awaiting_threshold_conf. push( entry) ;
@@ -879,7 +878,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
879
878
}
880
879
//TODO: recompute soonest_timelock to avoid wasting a bit on fees
881
880
if at_least_one_drop {
882
- bump_candidates. insert ( * package_id , request. clone ( ) ) ;
881
+ bump_candidates. insert ( * claim_id , request. clone ( ) ) ;
883
882
// If we have any pending claim events for the request being updated
884
883
// that have yet to be consumed, we'll remove them since they will
885
884
// end up producing an invalid transaction by double spending
@@ -889,10 +888,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
889
888
#[ cfg( anchors) ] {
890
889
#[ cfg( debug_assertions) ] {
891
890
let existing = self . pending_claim_events . iter ( )
892
- . filter ( |entry| entry. 0 == * package_id ) . count ( ) ;
891
+ . filter ( |entry| entry. 0 == * claim_id ) . count ( ) ;
893
892
assert ! ( existing == 0 || existing == 1 ) ;
894
893
}
895
- self . pending_claim_events . retain ( |entry| entry. 0 != * package_id ) ;
894
+ self . pending_claim_events . retain ( |entry| entry. 0 != * claim_id ) ;
896
895
}
897
896
}
898
897
}
@@ -921,22 +920,22 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
921
920
for entry in onchain_events_awaiting_threshold_conf {
922
921
if entry. has_reached_confirmation_threshold ( cur_height) {
923
922
match entry. event {
924
- OnchainEvent :: Claim { package_id } => {
923
+ OnchainEvent :: Claim { claim_id } => {
925
924
// We may remove a whole set of claim outpoints here, as these one may have
926
925
// 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 ) {
928
927
for outpoint in request. outpoints ( ) {
929
928
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 ) ) ;
931
930
self . claimable_outpoints . remove ( outpoint) ;
932
931
}
933
932
#[ cfg( anchors) ] {
934
933
#[ cfg( debug_assertions) ] {
935
934
let num_existing = self . pending_claim_events . iter ( )
936
- . filter ( |entry| entry. 0 == package_id ) . count ( ) ;
935
+ . filter ( |entry| entry. 0 == claim_id ) . count ( ) ;
937
936
assert ! ( num_existing == 0 || num_existing == 1 ) ;
938
937
}
939
- self . pending_claim_events . retain ( |( id, _) | * id != package_id ) ;
938
+ self . pending_claim_events . retain ( |( id, _) | * id != claim_id ) ;
940
939
}
941
940
}
942
941
} ,
@@ -952,15 +951,15 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
952
951
}
953
952
954
953
// 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 ( ) {
956
955
if cur_height >= request. timer ( ) {
957
- bump_candidates. insert ( * package_id , request. clone ( ) ) ;
956
+ bump_candidates. insert ( * claim_id , request. clone ( ) ) ;
958
957
}
959
958
}
960
959
961
960
// Build, bump and rebroadcast tx accordingly
962
961
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 ( ) {
964
963
if let Some ( ( new_timer, new_feerate, bump_claim) ) = self . generate_claim (
965
964
cur_height, & request, true /* force_feerate_bump */ , & * fee_estimator, & * logger,
966
965
) {
@@ -974,14 +973,14 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
974
973
log_info ! ( logger, "Yielding RBF-bumped onchain event to spend inputs {:?}" , request. outpoints( ) ) ;
975
974
#[ cfg( debug_assertions) ] {
976
975
let num_existing = self . pending_claim_events . iter ( ) .
977
- filter ( |entry| entry. 0 == * package_id ) . count ( ) ;
976
+ filter ( |entry| entry. 0 == * claim_id ) . count ( ) ;
978
977
assert ! ( num_existing == 0 || num_existing == 1 ) ;
979
978
}
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) ) ;
982
981
} ,
983
982
}
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 ) {
985
984
request. set_timer ( new_timer) ;
986
985
request. set_feerate ( new_feerate) ;
987
986
}
@@ -1042,7 +1041,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1042
1041
self . onchain_events_awaiting_threshold_conf . push ( entry) ;
1043
1042
}
1044
1043
}
1045
- for ( ( _package_id , _) , ref mut request) in bump_candidates. iter_mut ( ) {
1044
+ for ( ( _claim_id , _) , ref mut request) in bump_candidates. iter_mut ( ) {
1046
1045
// `height` is the height being disconnected, so our `current_height` is 1 lower.
1047
1046
let current_height = height - 1 ;
1048
1047
if let Some ( ( new_timer, new_feerate, bump_claim) ) = self . generate_claim (
@@ -1060,11 +1059,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1060
1059
log_info ! ( logger, "Yielding onchain event after reorg to spend inputs {:?}" , request. outpoints( ) ) ;
1061
1060
#[ cfg( debug_assertions) ] {
1062
1061
let num_existing = self . pending_claim_events . iter ( )
1063
- . filter ( |entry| entry. 0 == * _package_id ) . count ( ) ;
1062
+ . filter ( |entry| entry. 0 == * _claim_id ) . count ( ) ;
1064
1063
assert ! ( num_existing == 0 || num_existing == 1 ) ;
1065
1064
}
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) ) ;
1068
1067
} ,
1069
1068
}
1070
1069
}
0 commit comments