@@ -203,6 +203,13 @@ enum HTLCUpdateAwaitingACK {
203
203
} ,
204
204
}
205
205
206
+ /// There are a few "states" and then a number of flags which can be applied:
207
+ /// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent.
208
+ /// TheirFundingLocked and OurFundingLocked then get set on FundingSent, and when both are set we
209
+ /// move on to ChannelFunded.
210
+ /// Note that PeerDisconnected can be set on both ChannelFunded and FundingSent.
211
+ /// ChannelFunded can then get all remaining flags set on it, until we finish shutdown, then we
212
+ /// move on to ShutdownComplete, at which point most calls into this channel are disallowed.
206
213
enum ChannelState {
207
214
/// Implies we have (or are prepared to) send our open_channel/accept_channel message
208
215
OurInitSent = ( 1 << 0 ) ,
@@ -223,25 +230,29 @@ enum ChannelState {
223
230
/// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
224
231
OurFundingLocked = ( 1 << 5 ) ,
225
232
ChannelFunded = 64 ,
233
+ /// Flag which is set on ChannelFunded and FundingSent indicating remote side is considered
234
+ /// "disconnected" and no updates are allowed until after we've done a channel_reestablish
235
+ /// dance.
236
+ PeerDisconnected = ( 1 << 7 ) ,
226
237
/// Flag which implies that we have sent a commitment_signed but are awaiting the responding
227
238
/// revoke_and_ack message. During this time period, we can't generate new commitment_signed
228
239
/// messages as then we will be unable to determine which HTLCs they included in their
229
240
/// revoke_and_ack implicit ACK, so instead we have to hold them away temporarily to be sent
230
241
/// later.
231
242
/// Flag is set on ChannelFunded.
232
- AwaitingRemoteRevoke = ( 1 << 7 ) ,
243
+ AwaitingRemoteRevoke = ( 1 << 8 ) ,
233
244
/// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from
234
245
/// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected
235
246
/// to respond with our own shutdown message when possible.
236
- RemoteShutdownSent = ( 1 << 8 ) ,
247
+ RemoteShutdownSent = ( 1 << 9 ) ,
237
248
/// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
238
249
/// point, we may not add any new HTLCs to the channel.
239
250
/// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
240
251
/// us their shutdown.
241
- LocalShutdownSent = ( 1 << 9 ) ,
252
+ LocalShutdownSent = ( 1 << 10 ) ,
242
253
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
243
254
/// to drop us, but we store this anyway.
244
- ShutdownComplete = ( 1 << 10 ) ,
255
+ ShutdownComplete = 2048 ,
245
256
}
246
257
const BOTH_SIDES_SHUTDOWN_MASK : u32 = ( ChannelState :: LocalShutdownSent as u32 | ChannelState :: RemoteShutdownSent as u32 ) ;
247
258
@@ -1061,7 +1072,7 @@ impl Channel {
1061
1072
// can claim it even if the channel hits the chain before we see their next commitment.
1062
1073
self . channel_monitor . provide_payment_preimage ( & payment_hash_calc, & payment_preimage_arg) ;
1063
1074
1064
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
1075
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 ) ) != 0 {
1065
1076
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
1066
1077
match pending_update {
1067
1078
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
@@ -1137,7 +1148,7 @@ impl Channel {
1137
1148
}
1138
1149
1139
1150
// Now update local state:
1140
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
1151
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 ) ) != 0 {
1141
1152
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
1142
1153
match pending_update {
1143
1154
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
@@ -1354,12 +1365,23 @@ impl Channel {
1354
1365
}
1355
1366
1356
1367
pub fn funding_locked ( & mut self , msg : & msgs:: FundingLocked ) -> Result < ( ) , HandleError > {
1368
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1369
+ return Err ( HandleError { err : "Peer sent funding_locked when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent funding_locked when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1370
+ }
1357
1371
let non_shutdown_state = self . channel_state & ( !BOTH_SIDES_SHUTDOWN_MASK ) ;
1358
1372
if non_shutdown_state == ChannelState :: FundingSent as u32 {
1359
1373
self . channel_state |= ChannelState :: TheirFundingLocked as u32 ;
1360
1374
} else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
1361
1375
self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & BOTH_SIDES_SHUTDOWN_MASK ) ;
1362
1376
self . channel_update_count += 1 ;
1377
+ } else if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) != 0 &&
1378
+ self . cur_local_commitment_transaction_number == ( 1 << 48 ) - 2 &&
1379
+ self . cur_remote_commitment_transaction_number == ( 1 << 48 ) - 2 {
1380
+ if self . their_cur_commitment_point != Some ( msg. next_per_commitment_point ) {
1381
+ return Err ( HandleError { err : "Peer sent a reconnect funding_locked with a different point" , action : None } ) ;
1382
+ }
1383
+ // They probably disconnected/reconnected and re-sent the funding_locked, which is required
1384
+ return Ok ( ( ) ) ;
1363
1385
} else {
1364
1386
return Err ( HandleError { err : "Peer sent a funding_locked at a strange time" , action : None } ) ;
1365
1387
}
@@ -1411,6 +1433,9 @@ impl Channel {
1411
1433
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 | ChannelState :: RemoteShutdownSent as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1412
1434
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1413
1435
}
1436
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1437
+ return Err ( HandleError { err : "Peer sent update_add_htlc when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent update_add_htlc when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1438
+ }
1414
1439
if msg. amount_msat > self . channel_value_satoshis * 1000 {
1415
1440
return Err ( HandleError { err : "Remote side tried to send more than the total value of the channel" , action : None } ) ;
1416
1441
}
@@ -1489,6 +1514,9 @@ impl Channel {
1489
1514
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1490
1515
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1491
1516
}
1517
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1518
+ return Err ( HandleError { err : "Peer sent update_fulfill_htlc when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent update_fulfill_htlc when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1519
+ }
1492
1520
1493
1521
let mut sha = Sha256 :: new ( ) ;
1494
1522
sha. input ( & msg. payment_preimage ) ;
@@ -1502,6 +1530,9 @@ impl Channel {
1502
1530
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1503
1531
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1504
1532
}
1533
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1534
+ return Err ( HandleError { err : "Peer sent update_fail_htlc when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent update_fail_htlc when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1535
+ }
1505
1536
1506
1537
self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1507
1538
}
@@ -1510,6 +1541,9 @@ impl Channel {
1510
1541
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1511
1542
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1512
1543
}
1544
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1545
+ return Err ( HandleError { err : "Peer sent update_fail_malformed_htlc when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent update_fail_malformed_htlc when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1546
+ }
1513
1547
1514
1548
self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1515
1549
}
@@ -1518,6 +1552,9 @@ impl Channel {
1518
1552
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1519
1553
return Err ( HandleError { err : "Got commitment signed message when channel was not in an operational state" , action : None } ) ;
1520
1554
}
1555
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1556
+ return Err ( HandleError { err : "Peer sent commitment_signed when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent commitment_signed when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1557
+ }
1521
1558
1522
1559
let funding_script = self . get_funding_redeemscript ( ) ;
1523
1560
@@ -1680,6 +1717,9 @@ impl Channel {
1680
1717
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1681
1718
return Err ( HandleError { err : "Got revoke/ACK message when channel was not in an operational state" , action : None } ) ;
1682
1719
}
1720
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1721
+ return Err ( HandleError { err : "Peer sent revoke_and_ack when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent revoke_and_ack when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1722
+ }
1683
1723
if let Some ( their_prev_commitment_point) = self . their_prev_commitment_point {
1684
1724
if PublicKey :: from_secret_key ( & self . secp_ctx , & secp_call ! ( SecretKey :: from_slice( & self . secp_ctx, & msg. per_commitment_secret) , "Peer provided an invalid per_commitment_secret" , self . channel_id( ) ) ) != their_prev_commitment_point {
1685
1725
return Err ( HandleError { err : "Got a revoke commitment secret which didn't correspond to their current pubkey" , action : None } ) ;
@@ -1789,6 +1829,7 @@ impl Channel {
1789
1829
pub fn remove_uncommitted_htlcs ( & mut self ) -> Vec < ( HTLCSource , [ u8 ; 32 ] ) > {
1790
1830
let mut outbound_drops = Vec :: new ( ) ;
1791
1831
1832
+ assert_eq ! ( self . channel_state & ChannelState :: ShutdownComplete as u32 , 0 ) ;
1792
1833
if self . channel_state < ChannelState :: FundingSent as u32 {
1793
1834
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
1794
1835
return outbound_drops;
@@ -1845,13 +1886,19 @@ impl Channel {
1845
1886
if self . channel_outbound {
1846
1887
return Err ( HandleError { err : "Non-funding remote tried to update channel fee" , action : None } ) ;
1847
1888
}
1889
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1890
+ return Err ( HandleError { err : "Peer sent update_fee when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent update_fee when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1891
+ }
1848
1892
Channel :: check_remote_fee ( fee_estimator, msg. feerate_per_kw ) ?;
1849
1893
self . channel_update_count += 1 ;
1850
1894
self . feerate_per_kw = msg. feerate_per_kw as u64 ;
1851
1895
Ok ( ( ) )
1852
1896
}
1853
1897
1854
1898
pub fn shutdown ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Vec < ( HTLCSource , [ u8 ; 32 ] ) > ) , HandleError > {
1899
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1900
+ return Err ( HandleError { err : "Peer sent shutdown when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent shutdown when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
1901
+ }
1855
1902
if self . channel_state < ChannelState :: FundingSent as u32 {
1856
1903
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
1857
1904
self . channel_update_count += 1 ;
@@ -1952,6 +1999,9 @@ impl Channel {
1952
1999
if self . channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
1953
2000
return Err ( HandleError { err : "Remote end sent us a closing_signed before both sides provided a shutdown" , action : None } ) ;
1954
2001
}
2002
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
2003
+ return Err ( HandleError { err : "Peer sent closing_signed when we needed a channel_reestablish" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { data : "Peer sent closing_signed when we needed a channel_reestablish" . to_string ( ) , channel_id : msg. channel_id } } ) } ) ;
2004
+ }
1955
2005
if !self . pending_inbound_htlcs . is_empty ( ) || !self . pending_outbound_htlcs . is_empty ( ) {
1956
2006
return Err ( HandleError { err : "Remote end sent us a closing_signed while there were still pending HTLCs" , action : None } ) ;
1957
2007
}
@@ -2128,7 +2178,7 @@ impl Channel {
2128
2178
/// is_usable() and considers things like the channel being temporarily disabled.
2129
2179
/// Allowed in any state (including after shutdown)
2130
2180
pub fn is_live ( & self ) -> bool {
2131
- self . is_usable ( )
2181
+ self . is_usable ( ) && ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 )
2132
2182
}
2133
2183
2134
2184
/// Returns true if funding_created was sent/received.
@@ -2428,6 +2478,16 @@ impl Channel {
2428
2478
return Err ( HandleError { err : "Cannot send less than their minimum HTLC value" , action : None } ) ;
2429
2479
}
2430
2480
2481
+ if ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) ) == ( ChannelState :: PeerDisconnected as u32 ) {
2482
+ // Note that this should never really happen, if we're !is_live() on receipt of an
2483
+ // incoming HTLC for relay will result in us rejecting the HTLC and we won't allow
2484
+ // the user to send directly into a !is_live() channel. However, if we
2485
+ // disconnected during the time the previous hop was doing the commitment dance we may
2486
+ // end up getting here after the forwarding delay. In any case, returning an
2487
+ // IgnoreError will get ChannelManager to do the right thing and fail backwards now.
2488
+ return Err ( HandleError { err : "Cannot send an HTLC while disconnected" , action : Some ( ErrorAction :: IgnoreError ) } ) ;
2489
+ }
2490
+
2431
2491
let ( _, outbound_htlc_count, htlc_outbound_value_msat, htlc_inbound_value_msat) = self . get_pending_htlc_stats ( false ) ;
2432
2492
if outbound_htlc_count + 1 > self . their_max_accepted_htlcs as u32 {
2433
2493
return Err ( HandleError { err : "Cannot push more than their max accepted HTLCs" , action : None } ) ;
@@ -2492,6 +2552,9 @@ impl Channel {
2492
2552
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
2493
2553
panic ! ( "Cannot create commitment tx until remote revokes their previous commitment" ) ;
2494
2554
}
2555
+ if ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) ) == ( ChannelState :: PeerDisconnected as u32 ) {
2556
+ panic ! ( "Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated" ) ;
2557
+ }
2495
2558
let mut have_updates = false ; // TODO initialize with "have we sent a fee update?"
2496
2559
for htlc in self . pending_outbound_htlcs . iter ( ) {
2497
2560
if htlc. state == OutboundHTLCState :: LocalAnnounced {
@@ -2585,6 +2648,9 @@ impl Channel {
2585
2648
return Err ( HandleError { err : "Shutdown already in progress" , action : None } ) ;
2586
2649
}
2587
2650
assert_eq ! ( self . channel_state & ChannelState :: ShutdownComplete as u32 , 0 ) ;
2651
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
2652
+ return Err ( HandleError { err : "Cannot begin shutdown while peer is disconnected, maybe force-close instead?" , action : None } ) ;
2653
+ }
2588
2654
2589
2655
let our_closing_script = self . get_closing_scriptpubkey ( ) ;
2590
2656
0 commit comments