@@ -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,6 +1365,9 @@ 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 ;
@@ -1411,6 +1425,9 @@ impl Channel {
1411
1425
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 | ChannelState :: RemoteShutdownSent as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1412
1426
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1413
1427
}
1428
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1429
+ 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 } } ) } ) ;
1430
+ }
1414
1431
if msg. amount_msat > self . channel_value_satoshis * 1000 {
1415
1432
return Err ( HandleError { err : "Remote side tried to send more than the total value of the channel" , action : None } ) ;
1416
1433
}
@@ -1489,6 +1506,9 @@ impl Channel {
1489
1506
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1490
1507
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1491
1508
}
1509
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1510
+ 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 } } ) } ) ;
1511
+ }
1492
1512
1493
1513
let mut sha = Sha256 :: new ( ) ;
1494
1514
sha. input ( & msg. payment_preimage ) ;
@@ -1502,6 +1522,9 @@ impl Channel {
1502
1522
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1503
1523
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1504
1524
}
1525
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1526
+ 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 } } ) } ) ;
1527
+ }
1505
1528
1506
1529
self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1507
1530
}
@@ -1510,6 +1533,9 @@ impl Channel {
1510
1533
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1511
1534
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1512
1535
}
1536
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1537
+ 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 } } ) } ) ;
1538
+ }
1513
1539
1514
1540
self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1515
1541
}
@@ -1518,6 +1544,9 @@ impl Channel {
1518
1544
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1519
1545
return Err ( HandleError { err : "Got commitment signed message when channel was not in an operational state" , action : None } ) ;
1520
1546
}
1547
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1548
+ 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 } } ) } ) ;
1549
+ }
1521
1550
1522
1551
let funding_script = self . get_funding_redeemscript ( ) ;
1523
1552
@@ -1680,6 +1709,9 @@ impl Channel {
1680
1709
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1681
1710
return Err ( HandleError { err : "Got revoke/ACK message when channel was not in an operational state" , action : None } ) ;
1682
1711
}
1712
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1713
+ 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 } } ) } ) ;
1714
+ }
1683
1715
if let Some ( their_prev_commitment_point) = self . their_prev_commitment_point {
1684
1716
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
1717
return Err ( HandleError { err : "Got a revoke commitment secret which didn't correspond to their current pubkey" , action : None } ) ;
@@ -1789,6 +1821,7 @@ impl Channel {
1789
1821
pub fn remove_uncommitted_htlcs ( & mut self ) -> Vec < ( HTLCSource , [ u8 ; 32 ] ) > {
1790
1822
let mut outbound_drops = Vec :: new ( ) ;
1791
1823
1824
+ assert_eq ! ( self . channel_state & ChannelState :: ShutdownComplete as u32 , 0 ) ;
1792
1825
if self . channel_state < ChannelState :: FundingSent as u32 {
1793
1826
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
1794
1827
return outbound_drops;
@@ -1845,13 +1878,19 @@ impl Channel {
1845
1878
if self . channel_outbound {
1846
1879
return Err ( HandleError { err : "Non-funding remote tried to update channel fee" , action : None } ) ;
1847
1880
}
1881
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1882
+ 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 } } ) } ) ;
1883
+ }
1848
1884
Channel :: check_remote_fee ( fee_estimator, msg. feerate_per_kw ) ?;
1849
1885
self . channel_update_count += 1 ;
1850
1886
self . feerate_per_kw = msg. feerate_per_kw as u64 ;
1851
1887
Ok ( ( ) )
1852
1888
}
1853
1889
1854
1890
pub fn shutdown ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Vec < ( HTLCSource , [ u8 ; 32 ] ) > ) , HandleError > {
1891
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1892
+ 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 } } ) } ) ;
1893
+ }
1855
1894
if self . channel_state < ChannelState :: FundingSent as u32 {
1856
1895
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
1857
1896
self . channel_update_count += 1 ;
@@ -1952,6 +1991,9 @@ impl Channel {
1952
1991
if self . channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
1953
1992
return Err ( HandleError { err : "Remote end sent us a closing_signed before both sides provided a shutdown" , action : None } ) ;
1954
1993
}
1994
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1995
+ 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 } } ) } ) ;
1996
+ }
1955
1997
if !self . pending_inbound_htlcs . is_empty ( ) || !self . pending_outbound_htlcs . is_empty ( ) {
1956
1998
return Err ( HandleError { err : "Remote end sent us a closing_signed while there were still pending HTLCs" , action : None } ) ;
1957
1999
}
@@ -2128,7 +2170,7 @@ impl Channel {
2128
2170
/// is_usable() and considers things like the channel being temporarily disabled.
2129
2171
/// Allowed in any state (including after shutdown)
2130
2172
pub fn is_live ( & self ) -> bool {
2131
- self . is_usable ( )
2173
+ self . is_usable ( ) && ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 )
2132
2174
}
2133
2175
2134
2176
/// Returns true if funding_created was sent/received.
@@ -2428,6 +2470,16 @@ impl Channel {
2428
2470
return Err ( HandleError { err : "Cannot send less than their minimum HTLC value" , action : None } ) ;
2429
2471
}
2430
2472
2473
+ if ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) ) == ( ChannelState :: PeerDisconnected as u32 ) {
2474
+ // Note that this should never really happen, if we're !is_live() on receipt of an
2475
+ // incoming HTLC for relay will result in us rejecting the HTLC and we won't allow
2476
+ // the user to send directly into a !is_live() channel. However, if we we
2477
+ // disconnected during the time the previous hop was doing the commitment dance we may
2478
+ // end up getting here after the forwarding delay. In any case, returning an
2479
+ // IgnoreError will get ChannelManager to do the right thing and fail backwards now.
2480
+ return Err ( HandleError { err : "Cannot send an HTLC while disconnected" , action : Some ( ErrorAction :: IgnoreError ) } ) ;
2481
+ }
2482
+
2431
2483
let ( _, outbound_htlc_count, htlc_outbound_value_msat, htlc_inbound_value_msat) = self . get_pending_htlc_stats ( false ) ;
2432
2484
if outbound_htlc_count + 1 > self . their_max_accepted_htlcs as u32 {
2433
2485
return Err ( HandleError { err : "Cannot push more than their max accepted HTLCs" , action : None } ) ;
@@ -2492,6 +2544,9 @@ impl Channel {
2492
2544
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
2493
2545
panic ! ( "Cannot create commitment tx until remote revokes their previous commitment" ) ;
2494
2546
}
2547
+ if ( self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) ) == ( ChannelState :: PeerDisconnected as u32 ) {
2548
+ panic ! ( "Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated" ) ;
2549
+ }
2495
2550
let mut have_updates = false ; // TODO initialize with "have we sent a fee update?"
2496
2551
for htlc in self . pending_outbound_htlcs . iter ( ) {
2497
2552
if htlc. state == OutboundHTLCState :: LocalAnnounced {
@@ -2585,6 +2640,9 @@ impl Channel {
2585
2640
return Err ( HandleError { err : "Shutdown already in progress" , action : None } ) ;
2586
2641
}
2587
2642
assert_eq ! ( self . channel_state & ChannelState :: ShutdownComplete as u32 , 0 ) ;
2643
+ if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
2644
+ return Err ( HandleError { err : "Cannot begin shutdown while peer is disconnected, maybe force-close instead?" , action : None } ) ;
2645
+ }
2588
2646
2589
2647
let our_closing_script = self . get_closing_scriptpubkey ( ) ;
2590
2648
0 commit comments