@@ -305,8 +305,19 @@ enum ChannelState {
305
305
/// have received funding_signed and have their monitors persisted.
306
306
WaitingForBatch = 1 << 13,
307
307
}
308
- const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
309
- const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32;
308
+ const BOTH_SIDES_SHUTDOWN_MASK: u32 =
309
+ ChannelState::LocalShutdownSent as u32 |
310
+ ChannelState::RemoteShutdownSent as u32;
311
+ const MULTI_STATE_FLAGS: u32 =
312
+ BOTH_SIDES_SHUTDOWN_MASK |
313
+ ChannelState::PeerDisconnected as u32 |
314
+ ChannelState::MonitorUpdateInProgress as u32;
315
+ const STATE_FLAGS: u32 =
316
+ MULTI_STATE_FLAGS |
317
+ ChannelState::TheirChannelReady as u32 |
318
+ ChannelState::OurChannelReady as u32 |
319
+ ChannelState::AwaitingRemoteRevoke as u32 |
320
+ ChannelState::WaitingForBatch as u32;
310
321
311
322
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
312
323
@@ -918,7 +929,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
918
929
919
930
/// Returns true if we've ever received a message from the remote end for this Channel
920
931
pub fn have_received_message(&self) -> bool {
921
- self.channel_state > (ChannelState::OurInitSent as u32)
932
+ self.channel_state & !STATE_FLAGS > (ChannelState::OurInitSent as u32)
922
933
}
923
934
924
935
/// Returns true if this channel is fully established and not known to be closing.
@@ -1196,7 +1207,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1196
1207
/// Returns true if funding_signed was sent/received and the
1197
1208
/// funding transaction has been broadcast if necessary.
1198
1209
pub fn is_funding_initiated(&self) -> bool {
1199
- self.channel_state >= ChannelState::FundingSent as u32 &&
1210
+ self.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 &&
1200
1211
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
1201
1212
}
1202
1213
@@ -2616,6 +2627,8 @@ impl<SP: Deref> Channel<SP> where
2616
2627
2617
2628
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2618
2629
2630
+ // If the WaitingForBatch flag is set, we can receive their channel_ready, but our
2631
+ // channel_ready shouldn't have been sent and we shouldn't move to ChannelReady.
2619
2632
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2620
2633
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2621
2634
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -3115,7 +3128,7 @@ impl<SP: Deref> Channel<SP> where
3115
3128
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
3116
3129
where F::Target: FeeEstimator, L::Target: Logger
3117
3130
{
3118
- if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
3131
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 &&
3119
3132
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
3120
3133
self.free_holding_cell_htlcs(fee_estimator, logger)
3121
3134
} else { (None, Vec::new()) }
@@ -3591,7 +3604,7 @@ impl<SP: Deref> Channel<SP> where
3591
3604
/// completed.
3592
3605
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
3593
3606
assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
3594
- if self.context.channel_state < ChannelState::FundingSent as u32 {
3607
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
3595
3608
self.context.channel_state = ChannelState::ShutdownComplete as u32;
3596
3609
return;
3597
3610
}
@@ -3705,13 +3718,13 @@ impl<SP: Deref> Channel<SP> where
3705
3718
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3706
3719
// first received the funding_signed.
3707
3720
let mut funding_broadcastable =
3708
- 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 {
3721
+ if self.context.is_outbound() && self.context.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
3709
3722
self.context.funding_txid.take();
3710
3723
self.context.funding_transaction.take()
3711
3724
} else { None };
3712
3725
// That said, if the funding transaction is already confirmed (ie we're active with a
3713
3726
// minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
3714
- if self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3727
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3715
3728
funding_broadcastable = None;
3716
3729
}
3717
3730
@@ -4214,7 +4227,7 @@ impl<SP: Deref> Channel<SP> where
4214
4227
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
4215
4228
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish".to_owned()));
4216
4229
}
4217
- if self.context.channel_state < ChannelState::FundingSent as u32 {
4230
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
4218
4231
// Spec says we should fail the connection, not the channel, but that's nonsense, there
4219
4232
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
4220
4233
// can do that via error message without getting a connection fail anyway...
@@ -4639,7 +4652,7 @@ impl<SP: Deref> Channel<SP> where
4639
4652
4640
4653
/// Returns true if our channel_ready has been sent
4641
4654
pub fn is_our_channel_ready(&self) -> bool {
4642
- (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state >= ChannelState::ChannelReady as u32
4655
+ (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32
4643
4656
}
4644
4657
4645
4658
/// Returns true if our peer has either initiated or agreed to shut down the channel.
@@ -4702,7 +4715,7 @@ impl<SP: Deref> Channel<SP> where
4702
4715
// We got a reorg but not enough to trigger a force close, just ignore.
4703
4716
false
4704
4717
} else {
4705
- if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state < ChannelState::ChannelReady as u32 {
4718
+ if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state & !STATE_FLAGS < ChannelState::ChannelReady as u32 {
4706
4719
// We should never see a funding transaction on-chain until we've received
4707
4720
// funding_signed (if we're an outbound channel), or seen funding_generated (if we're
4708
4721
// an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -4863,7 +4876,7 @@ impl<SP: Deref> Channel<SP> where
4863
4876
}
4864
4877
4865
4878
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4866
- if non_shutdown_state >= ChannelState::ChannelReady as u32 ||
4879
+ if non_shutdown_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 ||
4867
4880
(non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
4868
4881
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
4869
4882
if self.context.funding_tx_confirmation_height == 0 {
@@ -4891,7 +4904,7 @@ impl<SP: Deref> Channel<SP> where
4891
4904
height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
4892
4905
log_info!(logger, "Closing channel {} due to funding timeout", &self.context.channel_id);
4893
4906
// If funding_tx_confirmed_in is unset, the channel must not be active
4894
- assert!(non_shutdown_state <= ChannelState::ChannelReady as u32);
4907
+ assert!(non_shutdown_state & !STATE_FLAGS <= ChannelState::ChannelReady as u32);
4895
4908
assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
4896
4909
return Err(ClosureReason::FundingTimedOut);
4897
4910
}
@@ -5511,7 +5524,7 @@ impl<SP: Deref> Channel<SP> where
5511
5524
// If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
5512
5525
// script is set, we just force-close and call it a day.
5513
5526
let mut chan_closed = false;
5514
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5527
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5515
5528
chan_closed = true;
5516
5529
}
5517
5530
@@ -5540,7 +5553,7 @@ impl<SP: Deref> Channel<SP> where
5540
5553
5541
5554
// From here on out, we may not fail!
5542
5555
self.context.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
5543
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5556
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5544
5557
self.context.channel_state = ChannelState::ShutdownComplete as u32;
5545
5558
} else {
5546
5559
self.context.channel_state |= ChannelState::LocalShutdownSent as u32;
@@ -7339,7 +7352,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7339
7352
// If we've gotten to the funding stage of the channel, populate the signer with its
7340
7353
// required channel parameters.
7341
7354
let non_shutdown_state = channel_state & (!MULTI_STATE_FLAGS);
7342
- if non_shutdown_state >= (ChannelState::FundingCreated as u32) {
7355
+ if non_shutdown_state & !STATE_FLAGS >= (ChannelState::FundingCreated as u32) {
7343
7356
holder_signer.provide_channel_parameters(&channel_parameters);
7344
7357
}
7345
7358
(channel_keys_id, holder_signer)
@@ -9064,13 +9077,10 @@ mod tests {
9064
9077
&config,
9065
9078
0,
9066
9079
&&logger,
9067
- 42,
9080
+ true, // Allow node b to send a 0conf channel_ready.
9068
9081
).unwrap();
9069
9082
9070
- // Allow node b to send a 0conf channel_ready.
9071
- node_b_chan.set_0conf();
9072
-
9073
- let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9083
+ let accept_channel_msg = node_b_chan.accept_inbound_channel();
9074
9084
node_a_chan.accept_channel(
9075
9085
&accept_channel_msg,
9076
9086
&config.channel_handshake_limits,
0 commit comments