@@ -304,8 +304,19 @@ enum ChannelState {
304
304
/// have received funding_signed and have their monitors persisted.
305
305
WaitingForBatch = 1 << 13,
306
306
}
307
- const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
308
- const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32;
307
+ const BOTH_SIDES_SHUTDOWN_MASK: u32 =
308
+ ChannelState::LocalShutdownSent as u32 |
309
+ ChannelState::RemoteShutdownSent as u32;
310
+ const MULTI_STATE_FLAGS: u32 =
311
+ BOTH_SIDES_SHUTDOWN_MASK |
312
+ ChannelState::PeerDisconnected as u32 |
313
+ ChannelState::MonitorUpdateInProgress as u32;
314
+ const STATE_FLAGS: u32 =
315
+ MULTI_STATE_FLAGS |
316
+ ChannelState::TheirChannelReady as u32 |
317
+ ChannelState::OurChannelReady as u32 |
318
+ ChannelState::AwaitingRemoteRevoke as u32 |
319
+ ChannelState::WaitingForBatch as u32;
309
320
310
321
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
311
322
@@ -914,7 +925,7 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
914
925
915
926
/// Returns true if we've ever received a message from the remote end for this Channel
916
927
pub fn have_received_message(&self) -> bool {
917
- self.channel_state > (ChannelState::OurInitSent as u32)
928
+ self.channel_state & !STATE_FLAGS > (ChannelState::OurInitSent as u32)
918
929
}
919
930
920
931
/// Returns true if this channel is fully established and not known to be closing.
@@ -1192,7 +1203,7 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1192
1203
/// Returns true if funding_signed was sent/received and the
1193
1204
/// funding transaction has been broadcast if necessary.
1194
1205
pub fn is_funding_initiated(&self) -> bool {
1195
- self.channel_state >= ChannelState::FundingSent as u32 &&
1206
+ self.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 &&
1196
1207
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
1197
1208
}
1198
1209
@@ -2603,6 +2614,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2603
2614
2604
2615
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2605
2616
2617
+ // If the WaitingForBatch flag is set, we can receive their channel_ready, but our
2618
+ // channel_ready shouldn't have been sent and we shouldn't move to ChannelReady.
2606
2619
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2607
2620
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2608
2621
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -3102,7 +3115,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3102
3115
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
3103
3116
where F::Target: FeeEstimator, L::Target: Logger
3104
3117
{
3105
- if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
3118
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 &&
3106
3119
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
3107
3120
self.free_holding_cell_htlcs(fee_estimator, logger)
3108
3121
} else { (None, Vec::new()) }
@@ -3551,7 +3564,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3551
3564
/// completed.
3552
3565
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
3553
3566
assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
3554
- if self.context.channel_state < ChannelState::FundingSent as u32 {
3567
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
3555
3568
self.context.channel_state = ChannelState::ShutdownComplete as u32;
3556
3569
return;
3557
3570
}
@@ -3665,13 +3678,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3665
3678
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3666
3679
// first received the funding_signed.
3667
3680
let mut funding_broadcastable =
3668
- if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
3681
+ if self.context.is_outbound() && self.context.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
3669
3682
self.context.funding_txid.take();
3670
3683
self.context.funding_transaction.take()
3671
3684
} else { None };
3672
3685
// That said, if the funding transaction is already confirmed (ie we're active with a
3673
3686
// minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
3674
- if self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3687
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3675
3688
funding_broadcastable = None;
3676
3689
}
3677
3690
@@ -4166,7 +4179,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4166
4179
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
4167
4180
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish".to_owned()));
4168
4181
}
4169
- if self.context.channel_state < ChannelState::FundingSent as u32 {
4182
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
4170
4183
// Spec says we should fail the connection, not the channel, but that's nonsense, there
4171
4184
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
4172
4185
// can do that via error message without getting a connection fail anyway...
@@ -4587,7 +4600,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4587
4600
4588
4601
/// Returns true if our channel_ready has been sent
4589
4602
pub fn is_our_channel_ready(&self) -> bool {
4590
- (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state >= ChannelState::ChannelReady as u32
4603
+ (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32
4591
4604
}
4592
4605
4593
4606
/// Returns true if our peer has either initiated or agreed to shut down the channel.
@@ -4650,7 +4663,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4650
4663
// We got a reorg but not enough to trigger a force close, just ignore.
4651
4664
false
4652
4665
} else {
4653
- if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state < ChannelState::ChannelReady as u32 {
4666
+ if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state & !STATE_FLAGS < ChannelState::ChannelReady as u32 {
4654
4667
// We should never see a funding transaction on-chain until we've received
4655
4668
// funding_signed (if we're an outbound channel), or seen funding_generated (if we're
4656
4669
// an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -4811,7 +4824,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4811
4824
}
4812
4825
4813
4826
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4814
- if non_shutdown_state >= ChannelState::ChannelReady as u32 ||
4827
+ if non_shutdown_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 ||
4815
4828
(non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
4816
4829
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
4817
4830
if self.context.funding_tx_confirmation_height == 0 {
@@ -4839,7 +4852,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4839
4852
height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
4840
4853
log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.context.channel_id));
4841
4854
// If funding_tx_confirmed_in is unset, the channel must not be active
4842
- assert!(non_shutdown_state <= ChannelState::ChannelReady as u32);
4855
+ assert!(non_shutdown_state & !STATE_FLAGS <= ChannelState::ChannelReady as u32);
4843
4856
assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
4844
4857
return Err(ClosureReason::FundingTimedOut);
4845
4858
}
@@ -5437,7 +5450,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5437
5450
// If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
5438
5451
// script is set, we just force-close and call it a day.
5439
5452
let mut chan_closed = false;
5440
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5453
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5441
5454
chan_closed = true;
5442
5455
}
5443
5456
@@ -5466,7 +5479,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5466
5479
5467
5480
// From here on out, we may not fail!
5468
5481
self.context.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
5469
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5482
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5470
5483
self.context.channel_state = ChannelState::ShutdownComplete as u32;
5471
5484
} else {
5472
5485
self.context.channel_state |= ChannelState::LocalShutdownSent as u32;
@@ -7252,7 +7265,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7252
7265
// If we've gotten to the funding stage of the channel, populate the signer with its
7253
7266
// required channel parameters.
7254
7267
let non_shutdown_state = channel_state & (!MULTI_STATE_FLAGS);
7255
- if non_shutdown_state >= (ChannelState::FundingCreated as u32) {
7268
+ if non_shutdown_state & !STATE_FLAGS >= (ChannelState::FundingCreated as u32) {
7256
7269
holder_signer.provide_channel_parameters(&channel_parameters);
7257
7270
}
7258
7271
(channel_keys_id, holder_signer)
@@ -8978,13 +8991,10 @@ mod tests {
8978
8991
&config,
8979
8992
0,
8980
8993
&&logger,
8981
- 42,
8994
+ true, // Allow node b to send a 0conf channel_ready.
8982
8995
).unwrap();
8983
8996
8984
- // Allow node b to send a 0conf channel_ready.
8985
- node_b_chan.set_0conf();
8986
-
8987
- let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
8997
+ let accept_channel_msg = node_b_chan.accept_inbound_channel();
8988
8998
node_a_chan.accept_channel(
8989
8999
&accept_channel_msg,
8990
9000
&config.channel_handshake_limits,
0 commit comments