@@ -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
@@ -915,7 +926,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
915
926
916
927
/// Returns true if we've ever received a message from the remote end for this Channel
917
928
pub fn have_received_message(&self) -> bool {
918
- self.channel_state > (ChannelState::OurInitSent as u32)
929
+ self.channel_state & !STATE_FLAGS > (ChannelState::OurInitSent as u32)
919
930
}
920
931
921
932
/// Returns true if this channel is fully established and not known to be closing.
@@ -1193,7 +1204,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1193
1204
/// Returns true if funding_signed was sent/received and the
1194
1205
/// funding transaction has been broadcast if necessary.
1195
1206
pub fn is_funding_initiated(&self) -> bool {
1196
- self.channel_state >= ChannelState::FundingSent as u32 &&
1207
+ self.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 &&
1197
1208
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
1198
1209
}
1199
1210
@@ -2612,6 +2623,8 @@ impl<SP: Deref> Channel<SP> where
2612
2623
2613
2624
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2614
2625
2626
+ // If the WaitingForBatch flag is set, we can receive their channel_ready, but our
2627
+ // channel_ready shouldn't have been sent and we shouldn't move to ChannelReady.
2615
2628
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2616
2629
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2617
2630
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -3111,7 +3124,7 @@ impl<SP: Deref> Channel<SP> where
3111
3124
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
3112
3125
where F::Target: FeeEstimator, L::Target: Logger
3113
3126
{
3114
- if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
3127
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 &&
3115
3128
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
3116
3129
self.free_holding_cell_htlcs(fee_estimator, logger)
3117
3130
} else { (None, Vec::new()) }
@@ -3588,7 +3601,7 @@ impl<SP: Deref> Channel<SP> where
3588
3601
/// completed.
3589
3602
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
3590
3603
assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
3591
- if self.context.channel_state < ChannelState::FundingSent as u32 {
3604
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
3592
3605
self.context.channel_state = ChannelState::ShutdownComplete as u32;
3593
3606
return;
3594
3607
}
@@ -3702,13 +3715,13 @@ impl<SP: Deref> Channel<SP> where
3702
3715
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3703
3716
// first received the funding_signed.
3704
3717
let mut funding_broadcastable =
3705
- 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 {
3718
+ if self.context.is_outbound() && self.context.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
3706
3719
self.context.funding_txid.take();
3707
3720
self.context.funding_transaction.take()
3708
3721
} else { None };
3709
3722
// That said, if the funding transaction is already confirmed (ie we're active with a
3710
3723
// minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
3711
- if self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3724
+ if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
3712
3725
funding_broadcastable = None;
3713
3726
}
3714
3727
@@ -4211,7 +4224,7 @@ impl<SP: Deref> Channel<SP> where
4211
4224
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
4212
4225
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish".to_owned()));
4213
4226
}
4214
- if self.context.channel_state < ChannelState::FundingSent as u32 {
4227
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
4215
4228
// Spec says we should fail the connection, not the channel, but that's nonsense, there
4216
4229
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
4217
4230
// can do that via error message without getting a connection fail anyway...
@@ -4636,7 +4649,7 @@ impl<SP: Deref> Channel<SP> where
4636
4649
4637
4650
/// Returns true if our channel_ready has been sent
4638
4651
pub fn is_our_channel_ready(&self) -> bool {
4639
- (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state >= ChannelState::ChannelReady as u32
4652
+ (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32
4640
4653
}
4641
4654
4642
4655
/// Returns true if our peer has either initiated or agreed to shut down the channel.
@@ -4699,7 +4712,7 @@ impl<SP: Deref> Channel<SP> where
4699
4712
// We got a reorg but not enough to trigger a force close, just ignore.
4700
4713
false
4701
4714
} else {
4702
- if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state < ChannelState::ChannelReady as u32 {
4715
+ if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state & !STATE_FLAGS < ChannelState::ChannelReady as u32 {
4703
4716
// We should never see a funding transaction on-chain until we've received
4704
4717
// funding_signed (if we're an outbound channel), or seen funding_generated (if we're
4705
4718
// an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -4860,7 +4873,7 @@ impl<SP: Deref> Channel<SP> where
4860
4873
}
4861
4874
4862
4875
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4863
- if non_shutdown_state >= ChannelState::ChannelReady as u32 ||
4876
+ if non_shutdown_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 ||
4864
4877
(non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
4865
4878
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
4866
4879
if self.context.funding_tx_confirmation_height == 0 {
@@ -4888,7 +4901,7 @@ impl<SP: Deref> Channel<SP> where
4888
4901
height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
4889
4902
log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.context.channel_id));
4890
4903
// If funding_tx_confirmed_in is unset, the channel must not be active
4891
- assert!(non_shutdown_state <= ChannelState::ChannelReady as u32);
4904
+ assert!(non_shutdown_state & !STATE_FLAGS <= ChannelState::ChannelReady as u32);
4892
4905
assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
4893
4906
return Err(ClosureReason::FundingTimedOut);
4894
4907
}
@@ -5508,7 +5521,7 @@ impl<SP: Deref> Channel<SP> where
5508
5521
// If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
5509
5522
// script is set, we just force-close and call it a day.
5510
5523
let mut chan_closed = false;
5511
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5524
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5512
5525
chan_closed = true;
5513
5526
}
5514
5527
@@ -5537,7 +5550,7 @@ impl<SP: Deref> Channel<SP> where
5537
5550
5538
5551
// From here on out, we may not fail!
5539
5552
self.context.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
5540
- if self.context.channel_state < ChannelState::FundingSent as u32 {
5553
+ if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
5541
5554
self.context.channel_state = ChannelState::ShutdownComplete as u32;
5542
5555
} else {
5543
5556
self.context.channel_state |= ChannelState::LocalShutdownSent as u32;
@@ -7336,7 +7349,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7336
7349
// If we've gotten to the funding stage of the channel, populate the signer with its
7337
7350
// required channel parameters.
7338
7351
let non_shutdown_state = channel_state & (!MULTI_STATE_FLAGS);
7339
- if non_shutdown_state >= (ChannelState::FundingCreated as u32) {
7352
+ if non_shutdown_state & !STATE_FLAGS >= (ChannelState::FundingCreated as u32) {
7340
7353
holder_signer.provide_channel_parameters(&channel_parameters);
7341
7354
}
7342
7355
(channel_keys_id, holder_signer)
@@ -9061,13 +9074,10 @@ mod tests {
9061
9074
&config,
9062
9075
0,
9063
9076
&&logger,
9064
- 42,
9077
+ true, // Allow node b to send a 0conf channel_ready.
9065
9078
).unwrap();
9066
9079
9067
- // Allow node b to send a 0conf channel_ready.
9068
- node_b_chan.set_0conf();
9069
-
9070
- let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9080
+ let accept_channel_msg = node_b_chan.accept_inbound_channel();
9071
9081
node_a_chan.accept_channel(
9072
9082
&accept_channel_msg,
9073
9083
&config.channel_handshake_limits,
0 commit comments