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