@@ -28,7 +28,7 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
28
28
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
29
29
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
30
30
use bitcoin:: hashes:: cmp:: fixed_time_eq;
31
- use bitcoin:: hash_types:: BlockHash ;
31
+ use bitcoin:: hash_types:: { BlockHash , Txid } ;
32
32
33
33
use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
34
34
use bitcoin:: secp256k1:: Secp256k1 ;
@@ -851,6 +851,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
851
851
}
852
852
}
853
853
854
+ /// Gets the current configuration applied to all new channels, as
855
+ pub fn get_current_default_configuration ( & self ) -> & UserConfig {
856
+ & self . default_configuration
857
+ }
858
+
854
859
/// Creates a new outbound channel to the given remote node and with the given value.
855
860
///
856
861
/// user_id will be provided back as user_channel_id in FundingGenerationReady events to allow
@@ -3353,7 +3358,7 @@ where
3353
3358
"Blocks must be disconnected in chain-order - the disconnected block must have the correct height" ) ;
3354
3359
* self . last_block_hash . write ( ) . unwrap ( ) = header. prev_blockhash ;
3355
3360
3356
- self . do_chain_event ( new_height, |channel| channel. update_best_block ( new_height, header. time ) ) ;
3361
+ self . do_chain_event ( Some ( new_height) , |channel| channel. update_best_block ( new_height, header. time ) ) ;
3357
3362
}
3358
3363
}
3359
3364
@@ -3364,8 +3369,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3364
3369
F :: Target : FeeEstimator ,
3365
3370
L :: Target : Logger ,
3366
3371
{
3372
+ /// Calls a function which handles an on-chain event (blocks dis/connected, transactions
3373
+ /// un/confirmed, etc) on each channel, handling any resulting errors or messages generated by
3374
+ /// the function.
3367
3375
fn do_chain_event < FN : Fn ( & mut Channel < Signer > ) -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , msgs:: ErrorMessage > >
3368
- ( & self , height : u32 , f : FN ) {
3376
+ ( & self , height_opt : Option < u32 > , f : FN ) {
3369
3377
// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
3370
3378
// during initialization prior to the chain_monitor being fully configured in some cases.
3371
3379
// See the docs for `ChannelManagerReadArgs` for more.
@@ -3424,24 +3432,26 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3424
3432
true
3425
3433
} ) ;
3426
3434
3427
- channel_state. claimable_htlcs . retain ( |& ( ref payment_hash, _) , htlcs| {
3428
- htlcs. retain ( |htlc| {
3429
- // If height is approaching the number of blocks we think it takes us to get
3430
- // our commitment transaction confirmed before the HTLC expires, plus the
3431
- // number of blocks we generally consider it to take to do a commitment update,
3432
- // just give up on it and fail the HTLC.
3433
- if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
3434
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
3435
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
3436
- timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
3437
- failure_code : 0x4000 | 15 ,
3438
- data : htlc_msat_height_data
3439
- } ) ) ;
3440
- false
3441
- } else { true }
3435
+ if let Some ( height) = height_opt {
3436
+ channel_state. claimable_htlcs . retain ( |& ( ref payment_hash, _) , htlcs| {
3437
+ htlcs. retain ( |htlc| {
3438
+ // If height is approaching the number of blocks we think it takes us to get
3439
+ // our commitment transaction confirmed before the HTLC expires, plus the
3440
+ // number of blocks we generally consider it to take to do a commitment update,
3441
+ // just give up on it and fail the HTLC.
3442
+ if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
3443
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
3444
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
3445
+ timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
3446
+ failure_code : 0x4000 | 15 ,
3447
+ data : htlc_msat_height_data
3448
+ } ) ) ;
3449
+ false
3450
+ } else { true }
3451
+ } ) ;
3452
+ !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
3442
3453
} ) ;
3443
- !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
3444
- } ) ;
3454
+ }
3445
3455
}
3446
3456
3447
3457
self . handle_init_event_channel_failures ( failed_channels) ;
@@ -3477,7 +3487,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3477
3487
log_trace ! ( self . logger, "{} transactions included in block {} at height {} provided" , txdata. len( ) , block_hash, height) ;
3478
3488
3479
3489
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3480
- self . do_chain_event ( height, |channel| channel. transactions_confirmed ( & block_hash, height, txdata, & self . logger ) . map ( |a| ( a, Vec :: new ( ) ) ) ) ;
3490
+ self . do_chain_event ( Some ( height) , |channel| channel. transactions_confirmed ( & block_hash, height, txdata, & self . logger ) . map ( |a| ( a, Vec :: new ( ) ) ) ) ;
3481
3491
}
3482
3492
3483
3493
/// Updates channel state with the current best blockchain tip. You should attempt to call this
@@ -3506,7 +3516,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3506
3516
self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
3507
3517
* self . last_block_hash . write ( ) . unwrap ( ) = block_hash;
3508
3518
3509
- self . do_chain_event ( height, |channel| channel. update_best_block ( height, header. time ) ) ;
3519
+ self . do_chain_event ( Some ( height) , |channel| channel. update_best_block ( height, header. time ) ) ;
3510
3520
3511
3521
loop {
3512
3522
// Update last_node_announcement_serial to be the max of its current value and the
@@ -3522,6 +3532,61 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3522
3532
}
3523
3533
}
3524
3534
3535
+ /// Gets the set of txids which should be monitored for their confirmation state.
3536
+ ///
3537
+ /// If you're providing information about reorganizations via [`transaction_unconfirmed`], this
3538
+ /// is the set of transactions which you may need to call [`transaction_unconfirmed`] for.
3539
+ ///
3540
+ /// This may be useful to poll to determine the set of transactions which must be registered
3541
+ /// with an Electrum server or for which an Electrum server needs to be polled to determine
3542
+ /// transaction confirmation state.
3543
+ ///
3544
+ /// This may update after any [`transactions_confirmed`] or [`block_connected`] call.
3545
+ ///
3546
+ /// Note that this is NOT the set of transactions which must be included in calls to
3547
+ /// [`transactions_confirmed`] if they are confirmed, but a small subset of it.
3548
+ ///
3549
+ /// [`transactions_confirmed`]: Self::transactions_confirmed
3550
+ /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
3551
+ /// [`block_connected`]: chain::Listen::block_connected
3552
+ pub fn get_relevant_txids ( & self ) -> Vec < Txid > {
3553
+ let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3554
+ let mut res = Vec :: with_capacity ( channel_state. short_to_id . len ( ) ) ;
3555
+ for chan in channel_state. by_id . values ( ) {
3556
+ if let Some ( funding_txo) = chan. get_funding_txo ( ) {
3557
+ res. push ( funding_txo. txid ) ;
3558
+ }
3559
+ }
3560
+ res
3561
+ }
3562
+
3563
+ /// Marks a transaction as having been reorganized out of the blockchain.
3564
+ ///
3565
+ /// If a transaction is included in [`get_relevant_txids`], and is no longer in the main branch
3566
+ /// of the blockchain, this function should be called to indicate that the transaction should
3567
+ /// be considered reorganized out.
3568
+ ///
3569
+ /// Once this is called, the given transaction will no longer appear on [`get_relevant_txids`],
3570
+ /// though this may be called repeatedly for a given transaction without issue.
3571
+ ///
3572
+ /// Note that if the transaction is confirmed on the main chain in a different block (indicated
3573
+ /// via a call to [`transactions_confirmed`]), it may re-appear in [`get_relevant_txids`], thus
3574
+ /// be very wary of race-conditions wherein the final state of a transaction indicated via
3575
+ /// these APIs is not the same as its state on the blockchain.
3576
+ ///
3577
+ /// [`transactions_confirmed`]: Self::transactions_confirmed
3578
+ /// [`get_relevant_txids`]: Self::get_relevant_txids
3579
+ pub fn transaction_unconfirmed ( & self , txid : & Txid ) {
3580
+ let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3581
+ self . do_chain_event ( None , |channel| {
3582
+ if let Some ( funding_txo) = channel. get_funding_txo ( ) {
3583
+ if funding_txo. txid == * txid {
3584
+ channel. funding_transaction_unconfirmed ( ) . map ( |_| ( None , Vec :: new ( ) ) )
3585
+ } else { Ok ( ( None , Vec :: new ( ) ) ) }
3586
+ } else { Ok ( ( None , Vec :: new ( ) ) ) }
3587
+ } ) ;
3588
+ }
3589
+
3525
3590
/// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
3526
3591
/// indicating whether persistence is necessary. Only one listener on
3527
3592
/// `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken
0 commit comments