@@ -781,15 +781,15 @@ macro_rules! convert_chan_err {
781
781
$short_to_id. remove( & short_id) ;
782
782
}
783
783
let shutdown_res = $channel. force_shutdown( true ) ;
784
- ( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, shutdown_res, $self. get_channel_update ( & $channel) . ok( ) ) )
784
+ ( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, shutdown_res, $self. get_channel_update_for_broadcast ( & $channel) . ok( ) ) )
785
785
} ,
786
786
ChannelError :: CloseDelayBroadcast ( msg) => {
787
787
log_error!( $self. logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}" , log_bytes!( $channel_id[ ..] ) , msg) ;
788
788
if let Some ( short_id) = $channel. get_short_channel_id( ) {
789
789
$short_to_id. remove( & short_id) ;
790
790
}
791
791
let shutdown_res = $channel. force_shutdown( false ) ;
792
- ( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, shutdown_res, $self. get_channel_update ( & $channel) . ok( ) ) )
792
+ ( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, shutdown_res, $self. get_channel_update_for_broadcast ( & $channel) . ok( ) ) )
793
793
}
794
794
}
795
795
}
@@ -845,7 +845,8 @@ macro_rules! handle_monitor_err {
845
845
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
846
846
// given up the preimage yet, so might as well just wait until the payment is
847
847
// retried, avoiding the on-chain fees.
848
- let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" . to_owned( ) , * $chan_id, $chan. force_shutdown( true ) , $self. get_channel_update( & $chan) . ok( ) ) ) ;
848
+ let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" . to_owned( ) , * $chan_id,
849
+ $chan. force_shutdown( true ) , $self. get_channel_update_for_broadcast( & $chan) . ok( ) ) ) ;
849
850
( res, true )
850
851
} ,
851
852
ChannelMonitorUpdateErr :: TemporaryFailure => {
@@ -1225,9 +1226,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1225
1226
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 8 , data : Vec :: new ( ) } ) ;
1226
1227
}
1227
1228
let chan_update = if let Some ( chan) = chan_option {
1228
- if let Ok ( update) = self . get_channel_update ( & chan) {
1229
- Some ( update)
1230
- } else { None }
1229
+ self . get_channel_update_for_broadcast ( & chan) . ok ( )
1231
1230
} else { None } ;
1232
1231
1233
1232
if let Some ( update) = chan_update {
@@ -1276,7 +1275,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1276
1275
} ;
1277
1276
log_trace ! ( self . logger, "Force-closing channel {}" , log_bytes!( channel_id[ ..] ) ) ;
1278
1277
self . finish_force_close_channel ( chan. force_shutdown ( true ) ) ;
1279
- if let Ok ( update) = self . get_channel_update ( & chan) {
1278
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
1280
1279
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1281
1280
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1282
1281
msg : update
@@ -1536,31 +1535,31 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1536
1535
// hopefully an attacker trying to path-trace payments cannot make this occur
1537
1536
// on a small/per-node/per-channel scale.
1538
1537
if !chan. is_live ( ) { // channel_disabled
1539
- break Some ( ( "Forwarding channel is not in a ready state." , 0x1000 | 20 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1538
+ break Some ( ( "Forwarding channel is not in a ready state." , 0x1000 | 20 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1540
1539
}
1541
1540
if * amt_to_forward < chan. get_counterparty_htlc_minimum_msat ( ) { // amount_below_minimum
1542
- break Some ( ( "HTLC amount was below the htlc_minimum_msat" , 0x1000 | 11 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1541
+ break Some ( ( "HTLC amount was below the htlc_minimum_msat" , 0x1000 | 11 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1543
1542
}
1544
1543
let fee = amt_to_forward. checked_mul ( chan. get_fee_proportional_millionths ( ) as u64 ) . and_then ( |prop_fee| { ( prop_fee / 1000000 ) . checked_add ( chan. get_holder_fee_base_msat ( & self . fee_estimator ) as u64 ) } ) ;
1545
1544
if fee. is_none ( ) || msg. amount_msat < fee. unwrap ( ) || ( msg. amount_msat - fee. unwrap ( ) ) < * amt_to_forward { // fee_insufficient
1546
- break Some ( ( "Prior hop has deviated from specified fees parameters or origin node has obsolete ones" , 0x1000 | 12 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1545
+ break Some ( ( "Prior hop has deviated from specified fees parameters or origin node has obsolete ones" , 0x1000 | 12 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1547
1546
}
1548
1547
if ( msg. cltv_expiry as u64 ) < ( * outgoing_cltv_value) as u64 + chan. get_cltv_expiry_delta ( ) as u64 { // incorrect_cltv_expiry
1549
- break Some ( ( "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta" , 0x1000 | 13 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1548
+ break Some ( ( "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta" , 0x1000 | 13 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1550
1549
}
1551
1550
let cur_height = self . best_block . read ( ) . unwrap ( ) . height ( ) + 1 ;
1552
1551
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now, but we want to be robust wrt to counterparty
1553
1552
// packet sanitization (see HTLC_FAIL_BACK_BUFFER rational)
1554
1553
if msg. cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
1555
- break Some ( ( "CLTV expiry is too close" , 0x1000 | 14 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1554
+ break Some ( ( "CLTV expiry is too close" , 0x1000 | 14 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1556
1555
}
1557
1556
if msg. cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
1558
1557
break Some ( ( "CLTV expiry is too far in the future" , 21 , None ) ) ;
1559
1558
}
1560
1559
// In theory, we would be safe against unitentional channel-closure, if we only required a margin of LATENCY_GRACE_PERIOD_BLOCKS.
1561
1560
// But, to be safe against policy reception, we use a longuer delay.
1562
1561
if ( * outgoing_cltv_value) as u64 <= ( cur_height + HTLC_FAIL_BACK_BUFFER ) as u64 {
1563
- break Some ( ( "Outgoing CLTV value is too soon" , 0x1000 | 14 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
1562
+ break Some ( ( "Outgoing CLTV value is too soon" , 0x1000 | 14 , Some ( self . get_channel_update_for_unicast ( chan) . unwrap ( ) ) ) ) ;
1564
1563
}
1565
1564
1566
1565
break None ;
@@ -1588,9 +1587,27 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1588
1587
( pending_forward_info, channel_state. unwrap ( ) )
1589
1588
}
1590
1589
1591
- /// only fails if the channel does not yet have an assigned short_id
1590
+ /// Gets the current channel_update for the given channel. This first checks if the channel is
1591
+ /// public, and thus should be called whenever the result is going to be passed out in a
1592
+ /// [`MessageSendEvent::BroadcastChannelUpdate`] event.
1593
+ ///
1594
+ /// May be called with channel_state already locked!
1595
+ fn get_channel_update_for_broadcast ( & self , chan : & Channel < Signer > ) -> Result < msgs:: ChannelUpdate , LightningError > {
1596
+ if !chan. should_announce ( ) {
1597
+ return Err ( LightningError {
1598
+ err : "Cannot broadcast a channel_update for a private channel" . to_owned ( ) ,
1599
+ action : msgs:: ErrorAction :: IgnoreError
1600
+ } ) ;
1601
+ }
1602
+ self . get_channel_update_for_unicast ( chan)
1603
+ }
1604
+
1605
+ /// Gets the current channel_update for the given channel. This does not check if the channel
1606
+ /// is public (only returning an Err if the channel does not yet have an assigned short_id),
1607
+ /// and thus MUST NOT be called unless the recipient of the resulting message has already
1608
+ /// provided evidence that they know about the existence of the channel.
1592
1609
/// May be called with channel_state already locked!
1593
- fn get_channel_update ( & self , chan : & Channel < Signer > ) -> Result < msgs:: ChannelUpdate , LightningError > {
1610
+ fn get_channel_update_for_unicast ( & self , chan : & Channel < Signer > ) -> Result < msgs:: ChannelUpdate , LightningError > {
1594
1611
let short_channel_id = match chan. get_short_channel_id ( ) {
1595
1612
None => return Err ( LightningError { err : "Channel not yet established" . to_owned ( ) , action : msgs:: ErrorAction :: IgnoreError } ) ,
1596
1613
Some ( id) => id,
@@ -1982,7 +1999,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1982
1999
if let Some ( msg) = chan. get_signed_channel_announcement ( & self . our_network_key , self . get_our_node_id ( ) , self . genesis_hash . clone ( ) ) {
1983
2000
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelAnnouncement {
1984
2001
msg,
1985
- update_msg : match self . get_channel_update ( chan) {
2002
+ update_msg : match self . get_channel_update_for_broadcast ( chan) {
1986
2003
Ok ( msg) => msg,
1987
2004
Err ( _) => continue ,
1988
2005
} ,
@@ -2074,7 +2091,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2074
2091
} else {
2075
2092
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
2076
2093
}
2077
- let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
2094
+ let chan_update = self . get_channel_update_for_unicast ( chan. get ( ) ) . unwrap ( ) ;
2078
2095
failed_forwards. push ( ( htlc_source, payment_hash,
2079
2096
HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : chan_update. encode_with_len ( ) }
2080
2097
) ) ;
@@ -2146,7 +2163,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2146
2163
if let Some ( short_id) = channel. get_short_channel_id ( ) {
2147
2164
channel_state. short_to_id . remove ( & short_id) ;
2148
2165
}
2149
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( true ) , self . get_channel_update ( & channel) . ok ( ) ) )
2166
+ Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
2150
2167
} ,
2151
2168
ChannelError :: CloseDelayBroadcast ( _) => { panic ! ( "Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here" ) ; }
2152
2169
} ;
@@ -2347,7 +2364,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2347
2364
ChannelUpdateStatus :: DisabledStaged if chan. is_live ( ) => chan. set_channel_update_status ( ChannelUpdateStatus :: Enabled ) ,
2348
2365
ChannelUpdateStatus :: EnabledStaged if !chan. is_live ( ) => chan. set_channel_update_status ( ChannelUpdateStatus :: Disabled ) ,
2349
2366
ChannelUpdateStatus :: DisabledStaged if !chan. is_live ( ) => {
2350
- if let Ok ( update) = self . get_channel_update ( & chan) {
2367
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
2351
2368
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2352
2369
msg : update
2353
2370
} ) ;
@@ -2356,7 +2373,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2356
2373
chan. set_channel_update_status ( ChannelUpdateStatus :: Disabled ) ;
2357
2374
} ,
2358
2375
ChannelUpdateStatus :: EnabledStaged if chan. is_live ( ) => {
2359
- if let Ok ( update) = self . get_channel_update ( & chan) {
2376
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
2360
2377
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2361
2378
msg : update
2362
2379
} ) ;
@@ -2406,7 +2423,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2406
2423
let ( failure_code, onion_failure_data) =
2407
2424
match self . channel_state . lock ( ) . unwrap ( ) . by_id . entry ( channel_id) {
2408
2425
hash_map:: Entry :: Occupied ( chan_entry) => {
2409
- if let Ok ( upd) = self . get_channel_update ( & chan_entry. get ( ) ) {
2426
+ if let Ok ( upd) = self . get_channel_update_for_unicast ( & chan_entry. get ( ) ) {
2410
2427
( 0x1000 |7 , upd. encode_with_len ( ) )
2411
2428
} else {
2412
2429
( 0x4000 |10 , Vec :: new ( ) )
@@ -2987,7 +3004,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2987
3004
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 8 , data : Vec :: new ( ) } ) ;
2988
3005
}
2989
3006
if let Some ( chan) = chan_option {
2990
- if let Ok ( update) = self . get_channel_update ( & chan) {
3007
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
2991
3008
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
2992
3009
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2993
3010
msg : update
@@ -3033,7 +3050,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3033
3050
self . tx_broadcaster . broadcast_transaction ( & broadcast_tx) ;
3034
3051
}
3035
3052
if let Some ( chan) = chan_option {
3036
- if let Ok ( update) = self . get_channel_update ( & chan) {
3053
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
3037
3054
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3038
3055
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
3039
3056
msg : update
@@ -3071,7 +3088,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3071
3088
// want to reject the new HTLC and fail it backwards instead of forwarding.
3072
3089
match pending_forward_info {
3073
3090
PendingHTLCStatus :: Forward ( PendingHTLCInfo { ref incoming_shared_secret, .. } ) => {
3074
- let reason = if let Ok ( upd) = self . get_channel_update ( chan) {
3091
+ let reason = if let Ok ( upd) = self . get_channel_update_for_unicast ( chan) {
3075
3092
onion_utils:: build_first_hop_failure_packet ( incoming_shared_secret, error_code, & {
3076
3093
let mut res = Vec :: with_capacity ( 8 + 128 ) ;
3077
3094
// TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
@@ -3333,7 +3350,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3333
3350
3334
3351
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelAnnouncement {
3335
3352
msg : try_chan_entry ! ( self , chan. get_mut( ) . announcement_signatures( & self . our_network_key, self . get_our_node_id( ) , self . genesis_hash. clone( ) , msg) , channel_state, chan) ,
3336
- update_msg : self . get_channel_update ( chan. get ( ) ) . unwrap ( ) , // can only fail if we're not in a ready state
3353
+ // Note that announcement_signatures fails if the channel cannot be announced,
3354
+ // so get_channel_update_for_broadcast will never fail by the time we get here.
3355
+ update_msg : self . get_channel_update_for_broadcast ( chan. get ( ) ) . unwrap ( ) ,
3337
3356
} ) ;
3338
3357
} ,
3339
3358
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
@@ -3478,7 +3497,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3478
3497
short_to_id. remove ( & short_id) ;
3479
3498
}
3480
3499
failed_channels. push ( chan. force_shutdown ( false ) ) ;
3481
- if let Ok ( update) = self . get_channel_update ( & chan) {
3500
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
3482
3501
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
3483
3502
msg : update
3484
3503
} ) ;
@@ -3917,7 +3936,7 @@ where
3917
3936
let res = f ( channel) ;
3918
3937
if let Ok ( ( chan_res, mut timed_out_pending_htlcs) ) = res {
3919
3938
for ( source, payment_hash) in timed_out_pending_htlcs. drain ( ..) {
3920
- let chan_update = self . get_channel_update ( & channel) . map ( |u| u. encode_with_len ( ) ) . unwrap ( ) ; // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3939
+ let chan_update = self . get_channel_update_for_unicast ( & channel) . map ( |u| u. encode_with_len ( ) ) . unwrap ( ) ; // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3921
3940
timed_out_htlcs. push ( ( source, payment_hash, HTLCFailReason :: Reason {
3922
3941
failure_code : 0x1000 | 14 , // expiry_too_soon, or at least it is now
3923
3942
data : chan_update,
@@ -3946,7 +3965,7 @@ where
3946
3965
// It looks like our counterparty went on-chain or funding transaction was
3947
3966
// reorged out of the main chain. Close the channel.
3948
3967
failed_channels. push ( channel. force_shutdown ( true ) ) ;
3949
- if let Ok ( update) = self . get_channel_update ( & channel) {
3968
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & channel) {
3950
3969
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
3951
3970
msg : update
3952
3971
} ) ;
@@ -4125,7 +4144,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
4125
4144
short_to_id. remove ( & short_id) ;
4126
4145
}
4127
4146
failed_channels. push ( chan. force_shutdown ( true ) ) ;
4128
- if let Ok ( update) = self . get_channel_update ( & chan) {
4147
+ if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
4129
4148
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
4130
4149
msg : update
4131
4150
} ) ;
0 commit comments