@@ -710,6 +710,11 @@ pub(super) struct Channel<Signer: Sign> {
710
710
// Our counterparty can offer us SCID aliases which they will map to this channel when routing
711
711
// outbound payments. These can be used in invoice route hints to avoid explicitly revealing
712
712
// the channel's funding UTXO.
713
+ //
714
+ // We also use this when sending our peer a channel_update that isn't to be broadcasted
715
+ // publicly - allowing them to re-use their map of SCID -> channel for channel_update ->
716
+ // associated channel mapping.
717
+ //
713
718
// We only bother storing the most recent SCID alias at any time, though our counterparty has
714
719
// to store all of them.
715
720
latest_inbound_scid_alias : Option < u64 > ,
@@ -1307,7 +1312,7 @@ impl<Signer: Sign> Channel<Signer> {
1307
1312
counterparty_htlc_minimum_msat : msg. htlc_minimum_msat ,
1308
1313
holder_htlc_minimum_msat : if config. own_channel_config . our_htlc_minimum_msat == 0 { 1 } else { config. own_channel_config . our_htlc_minimum_msat } ,
1309
1314
counterparty_max_accepted_htlcs : msg. max_accepted_htlcs ,
1310
- minimum_depth : Some ( config. own_channel_config . minimum_depth ) ,
1315
+ minimum_depth : Some ( cmp :: max ( config. own_channel_config . minimum_depth , 1 ) ) ,
1311
1316
1312
1317
counterparty_forwarding_info : None ,
1313
1318
@@ -1987,12 +1992,6 @@ impl<Signer: Sign> Channel<Signer> {
1987
1992
if msg. minimum_depth > peer_limits. max_minimum_depth {
1988
1993
return Err ( ChannelError :: Close ( format ! ( "We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})" , peer_limits. max_minimum_depth, msg. minimum_depth) ) ) ;
1989
1994
}
1990
- if msg. minimum_depth == 0 {
1991
- // Note that if this changes we should update the serialization minimum version to
1992
- // indicate to older clients that they don't understand some features of the current
1993
- // channel.
1994
- return Err ( ChannelError :: Close ( "Minimum confirmation depth must be at least 1" . to_owned ( ) ) ) ;
1995
- }
1996
1995
1997
1996
if let Some ( ty) = & msg. channel_type {
1998
1997
if * ty != self . channel_type {
@@ -2029,7 +2028,12 @@ impl<Signer: Sign> Channel<Signer> {
2029
2028
self . counterparty_selected_channel_reserve_satoshis = Some ( msg. channel_reserve_satoshis ) ;
2030
2029
self . counterparty_htlc_minimum_msat = msg. htlc_minimum_msat ;
2031
2030
self . counterparty_max_accepted_htlcs = msg. max_accepted_htlcs ;
2032
- self . minimum_depth = Some ( msg. minimum_depth ) ;
2031
+
2032
+ if peer_limits. trust_own_funding_0conf {
2033
+ self . minimum_depth = Some ( msg. minimum_depth ) ;
2034
+ } else {
2035
+ self . minimum_depth = Some ( cmp:: max ( 1 , msg. minimum_depth ) ) ;
2036
+ }
2033
2037
2034
2038
let counterparty_pubkeys = ChannelPublicKeys {
2035
2039
funding_pubkey : msg. funding_pubkey ,
@@ -2089,7 +2093,7 @@ impl<Signer: Sign> Channel<Signer> {
2089
2093
& self . get_counterparty_pubkeys ( ) . funding_pubkey
2090
2094
}
2091
2095
2092
- pub fn funding_created < L : Deref > ( & mut self , msg : & msgs:: FundingCreated , best_block : BestBlock , logger : & L ) -> Result < ( msgs:: FundingSigned , ChannelMonitor < Signer > ) , ChannelError > where L :: Target : Logger {
2096
+ pub fn funding_created < L : Deref > ( & mut self , msg : & msgs:: FundingCreated , best_block : BestBlock , logger : & L ) -> Result < ( msgs:: FundingSigned , ChannelMonitor < Signer > , Option < msgs :: FundingLocked > ) , ChannelError > where L :: Target : Logger {
2093
2097
if self . is_outbound ( ) {
2094
2098
return Err ( ChannelError :: Close ( "Received funding_created for an outbound channel?" . to_owned ( ) ) ) ;
2095
2099
}
@@ -2164,12 +2168,12 @@ impl<Signer: Sign> Channel<Signer> {
2164
2168
Ok ( ( msgs:: FundingSigned {
2165
2169
channel_id : self . channel_id ,
2166
2170
signature
2167
- } , channel_monitor) )
2171
+ } , channel_monitor, self . check_get_funding_locked ( 0 ) ) )
2168
2172
}
2169
2173
2170
2174
/// Handles a funding_signed message from the remote end.
2171
2175
/// If this call is successful, broadcast the funding transaction (and not before!)
2172
- pub fn funding_signed < L : Deref > ( & mut self , msg : & msgs:: FundingSigned , best_block : BestBlock , logger : & L ) -> Result < ( ChannelMonitor < Signer > , Transaction ) , ChannelError > where L :: Target : Logger {
2176
+ pub fn funding_signed < L : Deref > ( & mut self , msg : & msgs:: FundingSigned , best_block : BestBlock , logger : & L ) -> Result < ( ChannelMonitor < Signer > , Transaction , Option < msgs :: FundingLocked > ) , ChannelError > where L :: Target : Logger {
2173
2177
if !self . is_outbound ( ) {
2174
2178
return Err ( ChannelError :: Close ( "Received funding_signed for an inbound channel?" . to_owned ( ) ) ) ;
2175
2179
}
@@ -2238,7 +2242,7 @@ impl<Signer: Sign> Channel<Signer> {
2238
2242
2239
2243
log_info ! ( logger, "Received funding_signed from peer for channel {}" , log_bytes!( self . channel_id( ) ) ) ;
2240
2244
2241
- Ok ( ( channel_monitor, self . funding_transaction . as_ref ( ) . cloned ( ) . unwrap ( ) ) )
2245
+ Ok ( ( channel_monitor, self . funding_transaction . as_ref ( ) . cloned ( ) . unwrap ( ) , self . check_get_funding_locked ( 0 ) ) )
2242
2246
}
2243
2247
2244
2248
/// Handles a funding_locked message from our peer. If we've already sent our funding_locked
@@ -3540,12 +3544,13 @@ impl<Signer: Sign> Channel<Signer> {
3540
3544
/// monitor update failure must *not* have been sent to the remote end, and must instead
3541
3545
/// have been dropped. They will be regenerated when monitor_updating_restored is called.
3542
3546
pub fn monitor_update_failed ( & mut self , resend_raa : bool , resend_commitment : bool ,
3543
- mut pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
3547
+ resend_funding_locked : bool , mut pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
3544
3548
mut pending_fails : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
3545
3549
mut pending_finalized_claimed_htlcs : Vec < HTLCSource >
3546
3550
) {
3547
3551
self . monitor_pending_revoke_and_ack |= resend_raa;
3548
3552
self . monitor_pending_commitment_signed |= resend_commitment;
3553
+ self . monitor_pending_funding_locked |= resend_funding_locked;
3549
3554
self . monitor_pending_forwards . append ( & mut pending_forwards) ;
3550
3555
self . monitor_pending_failures . append ( & mut pending_fails) ;
3551
3556
self . monitor_pending_finalized_fulfills . append ( & mut pending_finalized_claimed_htlcs) ;
@@ -3559,17 +3564,28 @@ impl<Signer: Sign> Channel<Signer> {
3559
3564
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
3560
3565
self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
3561
3566
3562
- let funding_broadcastable = if self . channel_state & ( ChannelState :: FundingSent as u32 ) != 0 && self . is_outbound ( ) {
3563
- self . funding_transaction . take ( )
3564
- } else { None } ;
3567
+ // If we're past (or at) the FundingSent stage on an outbound channel, try to
3568
+ // (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3569
+ // first received the funding_signed.
3570
+ let mut funding_broadcastable =
3571
+ if self . is_outbound ( ) && self . channel_state & !MULTI_STATE_FLAGS >= ChannelState :: FundingSent as u32 {
3572
+ self . funding_transaction . take ( )
3573
+ } else { None } ;
3574
+ // That said, if the funding transaction is already confirmed (ie we're active with a
3575
+ // minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
3576
+ if self . channel_state & !MULTI_STATE_FLAGS >= ChannelState :: ChannelFunded as u32 && self . minimum_depth != Some ( 0 ) {
3577
+ funding_broadcastable = None ;
3578
+ }
3565
3579
3566
3580
// We will never broadcast the funding transaction when we're in MonitorUpdateFailed (and
3567
3581
// we assume the user never directly broadcasts the funding transaction and waits for us to
3568
- // do it). Thus, we can only ever hit monitor_pending_funding_locked when we're an inbound
3569
- // channel which failed to persist the monitor on funding_created, and we got the funding
3570
- // transaction confirmed before the monitor was persisted.
3582
+ // do it). Thus, we can only ever hit monitor_pending_funding_locked when we're
3583
+ // * an inbound channel that failed to persist the monitor on funding_created and we got
3584
+ // the funding transaction confirmed before the monitor was persisted, or
3585
+ // * a 0-conf channel and intended to send the funding_locked before any broadcast at all.
3571
3586
let funding_locked = if self . monitor_pending_funding_locked {
3572
- assert ! ( !self . is_outbound( ) , "Funding transaction broadcast by the local client before it should have - LDK didn't do it!" ) ;
3587
+ assert ! ( !self . is_outbound( ) || self . minimum_depth == Some ( 0 ) ,
3588
+ "Funding transaction broadcast by the local client before it should have - LDK didn't do it!" ) ;
3573
3589
self . monitor_pending_funding_locked = false ;
3574
3590
let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3575
3591
Some ( msgs:: FundingLocked {
@@ -4551,6 +4567,11 @@ impl<Signer: Sign> Channel<Signer> {
4551
4567
self . channel_state >= ChannelState :: FundingSent as u32
4552
4568
}
4553
4569
4570
+ /// Returns true if our funding_locked has been sent
4571
+ pub fn is_our_funding_locked ( & self ) -> bool {
4572
+ ( self . channel_state & ChannelState :: OurFundingLocked as u32 ) != 0 || self . channel_state >= ChannelState :: ChannelFunded as u32
4573
+ }
4574
+
4554
4575
/// Returns true if our peer has either initiated or agreed to shut down the channel.
4555
4576
pub fn received_shutdown ( & self ) -> bool {
4556
4577
( self . channel_state & ChannelState :: RemoteShutdownSent as u32 ) != 0
@@ -4581,7 +4602,7 @@ impl<Signer: Sign> Channel<Signer> {
4581
4602
}
4582
4603
4583
4604
fn check_get_funding_locked ( & mut self , height : u32 ) -> Option < msgs:: FundingLocked > {
4584
- if self . funding_tx_confirmation_height == 0 {
4605
+ if self . funding_tx_confirmation_height == 0 && self . minimum_depth != Some ( 0 ) {
4585
4606
return None ;
4586
4607
}
4587
4608
@@ -4636,12 +4657,11 @@ impl<Signer: Sign> Channel<Signer> {
4636
4657
pub fn transactions_confirmed < L : Deref > ( & mut self , block_hash : & BlockHash , height : u32 ,
4637
4658
txdata : & TransactionData , genesis_block_hash : BlockHash , node_pk : PublicKey , logger : & L )
4638
4659
-> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: AnnouncementSignatures > ) , ClosureReason > where L :: Target : Logger {
4639
- let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
4640
4660
if let Some ( funding_txo) = self . get_funding_txo ( ) {
4641
4661
for & ( index_in_block, tx) in txdata. iter ( ) {
4642
- // If we haven't yet sent a funding_locked, but are in FundingSent (ignoring
4643
- // whether they've sent a funding_locked or not), check if we should send one .
4644
- if non_shutdown_state & ! ( ChannelState :: TheirFundingLocked as u32 ) == ChannelState :: FundingSent as u32 {
4662
+ // Check if the transaction is the expected funding transaction, and if it is,
4663
+ // check that it pays the right amount to the right script .
4664
+ if self . funding_tx_confirmation_height == 0 {
4645
4665
if tx. txid ( ) == funding_txo. txid {
4646
4666
let txo_idx = funding_txo. index as usize ;
4647
4667
if txo_idx >= tx. output . len ( ) || tx. output [ txo_idx] . script_pubkey != self . get_funding_redeemscript ( ) . to_v0_p2wsh ( ) ||
@@ -4758,9 +4778,9 @@ impl<Signer: Sign> Channel<Signer> {
4758
4778
// close the channel and hope we can get the latest state on chain (because presumably
4759
4779
// the funding transaction is at least still in the mempool of most nodes).
4760
4780
//
4761
- // Note that ideally we wouldn't force-close if we see *any* reorg on a 1-conf channel,
4762
- // but not doing so may lead to the `ChannelManager::short_to_id` map being
4763
- // inconsistent, so we currently have to.
4781
+ // Note that ideally we wouldn't force-close if we see *any* reorg on a 1-conf or
4782
+ // 0-conf channel, but not doing so may lead to the `ChannelManager::short_to_id` map
4783
+ // being inconsistent, so we currently have to.
4764
4784
if funding_tx_confirmations == 0 && self . funding_tx_confirmed_in . is_some ( ) {
4765
4785
let err_reason = format ! ( "Funding transaction was un-confirmed. Locked at {} confs, now have {} confs." ,
4766
4786
self . minimum_depth. unwrap( ) , funding_tx_confirmations) ;
@@ -4857,6 +4877,12 @@ impl<Signer: Sign> Channel<Signer> {
4857
4877
self . inbound_awaiting_accept
4858
4878
}
4859
4879
4880
+ /// Sets this channel to accepting 0conf, must be done before `get_accept_channel`
4881
+ pub fn set_0conf ( & mut self ) {
4882
+ assert ! ( self . inbound_awaiting_accept) ;
4883
+ self . minimum_depth = Some ( 0 ) ;
4884
+ }
4885
+
4860
4886
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
4861
4887
/// should be sent back to the counterparty node.
4862
4888
///
@@ -5619,7 +5645,7 @@ impl<Signer: Sign> Channel<Signer> {
5619
5645
}
5620
5646
5621
5647
const SERIALIZATION_VERSION : u8 = 2 ;
5622
- const MIN_SERIALIZATION_VERSION : u8 = 1 ;
5648
+ const MIN_SERIALIZATION_VERSION : u8 = 2 ;
5623
5649
5624
5650
impl_writeable_tlv_based_enum ! ( InboundHTLCRemovalReason , ;
5625
5651
( 0 , FailRelay ) ,
@@ -5684,12 +5710,10 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5684
5710
5685
5711
self . user_id . write ( writer) ?;
5686
5712
5687
- // Write out the old serialization for the config object. This is read by version-1
5688
- // deserializers, but we will read the version in the TLV at the end instead.
5689
- self . config . forwarding_fee_proportional_millionths . write ( writer) ?;
5690
- self . config . cltv_expiry_delta . write ( writer) ?;
5691
- self . config . announced_channel . write ( writer) ?;
5692
- self . config . commit_upfront_shutdown_pubkey . write ( writer) ?;
5713
+ // Version 1 deserializers expected to read parts of the config object here. Version 2
5714
+ // deserializers (0.0.99) now read config through TLVs, and as we now require them for
5715
+ // `minimum_depth` we simply write dummy values here.
5716
+ writer. write_all ( & [ 0 ; 8 ] ) ?;
5693
5717
5694
5718
self . channel_id . write ( writer) ?;
5695
5719
( self . channel_state | ChannelState :: PeerDisconnected as u32 ) . write ( writer) ?;
@@ -6667,7 +6691,7 @@ mod tests {
6667
6691
} ] } ;
6668
6692
let funding_outpoint = OutPoint { txid : tx. txid ( ) , index : 0 } ;
6669
6693
let funding_created_msg = node_a_chan. get_outbound_funding_created ( tx. clone ( ) , funding_outpoint, & & logger) . unwrap ( ) ;
6670
- let ( funding_signed_msg, _) = node_b_chan. funding_created ( & funding_created_msg, best_block, & & logger) . unwrap ( ) ;
6694
+ let ( funding_signed_msg, _, _ ) = node_b_chan. funding_created ( & funding_created_msg, best_block, & & logger) . unwrap ( ) ;
6671
6695
6672
6696
// Node B --> Node A: funding signed
6673
6697
let _ = node_a_chan. funding_signed ( & funding_signed_msg, best_block, & & logger) ;
0 commit comments