Skip to content

Commit 0ad701a

Browse files
committed
Review comments
1 parent 04416c3 commit 0ad701a

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

lightning/src/events/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ pub enum Event {
841841
},
842842
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
843843
/// inputs for another purpose.
844+
///
845+
/// This event is not guaranteed to be generated for channels that are closed due to a restart.
844846
DiscardFunding {
845847
/// The channel_id of the channel which has been closed.
846848
channel_id: [u8; 32],

lightning/src/ln/channel.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,19 @@ enum ChannelState {
305305
/// have received funding_signed and have their monitors persisted.
306306
WaitingForBatch = 1 << 13,
307307
}
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;
310321

311322
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
312323

@@ -915,7 +926,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
915926

916927
/// Returns true if we've ever received a message from the remote end for this Channel
917928
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)
919930
}
920931

921932
/// 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 {
11931204
/// Returns true if funding_signed was sent/received and the
11941205
/// funding transaction has been broadcast if necessary.
11951206
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 &&
11971208
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
11981209
}
11991210

@@ -2612,6 +2623,8 @@ impl<SP: Deref> Channel<SP> where
26122623

26132624
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26142625

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.
26152628
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26162629
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26172630
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -3111,7 +3124,7 @@ impl<SP: Deref> Channel<SP> where
31113124
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
31123125
where F::Target: FeeEstimator, L::Target: Logger
31133126
{
3114-
if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
3127+
if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 &&
31153128
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
31163129
self.free_holding_cell_htlcs(fee_estimator, logger)
31173130
} else { (None, Vec::new()) }
@@ -3588,7 +3601,7 @@ impl<SP: Deref> Channel<SP> where
35883601
/// completed.
35893602
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
35903603
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 {
35923605
self.context.channel_state = ChannelState::ShutdownComplete as u32;
35933606
return;
35943607
}
@@ -3702,13 +3715,13 @@ impl<SP: Deref> Channel<SP> where
37023715
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
37033716
// first received the funding_signed.
37043717
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 {
37063719
self.context.funding_txid.take();
37073720
self.context.funding_transaction.take()
37083721
} else { None };
37093722
// That said, if the funding transaction is already confirmed (ie we're active with a
37103723
// 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) {
37123725
funding_broadcastable = None;
37133726
}
37143727

@@ -4211,7 +4224,7 @@ impl<SP: Deref> Channel<SP> where
42114224
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
42124225
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish".to_owned()));
42134226
}
4214-
if self.context.channel_state < ChannelState::FundingSent as u32 {
4227+
if self.context.channel_state & !STATE_FLAGS < ChannelState::FundingSent as u32 {
42154228
// Spec says we should fail the connection, not the channel, but that's nonsense, there
42164229
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
42174230
// can do that via error message without getting a connection fail anyway...
@@ -4636,7 +4649,7 @@ impl<SP: Deref> Channel<SP> where
46364649

46374650
/// Returns true if our channel_ready has been sent
46384651
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
46404653
}
46414654

46424655
/// 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
46994712
// We got a reorg but not enough to trigger a force close, just ignore.
47004713
false
47014714
} 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 {
47034716
// We should never see a funding transaction on-chain until we've received
47044717
// funding_signed (if we're an outbound channel), or seen funding_generated (if we're
47054718
// an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -4860,7 +4873,7 @@ impl<SP: Deref> Channel<SP> where
48604873
}
48614874

48624875
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 ||
48644877
(non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
48654878
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
48664879
if self.context.funding_tx_confirmation_height == 0 {
@@ -4888,7 +4901,7 @@ impl<SP: Deref> Channel<SP> where
48884901
height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
48894902
log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.context.channel_id));
48904903
// 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);
48924905
assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
48934906
return Err(ClosureReason::FundingTimedOut);
48944907
}
@@ -5508,7 +5521,7 @@ impl<SP: Deref> Channel<SP> where
55085521
// If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
55095522
// script is set, we just force-close and call it a day.
55105523
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 {
55125525
chan_closed = true;
55135526
}
55145527

@@ -5537,7 +5550,7 @@ impl<SP: Deref> Channel<SP> where
55375550

55385551
// From here on out, we may not fail!
55395552
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 {
55415554
self.context.channel_state = ChannelState::ShutdownComplete as u32;
55425555
} else {
55435556
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
73367349
// If we've gotten to the funding stage of the channel, populate the signer with its
73377350
// required channel parameters.
73387351
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) {
73407353
holder_signer.provide_channel_parameters(&channel_parameters);
73417354
}
73427355
(channel_keys_id, holder_signer)
@@ -9061,13 +9074,10 @@ mod tests {
90619074
&config,
90629075
0,
90639076
&&logger,
9064-
42,
9077+
true, // Allow node b to send a 0conf channel_ready.
90659078
).unwrap();
90669079

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();
90719081
node_a_chan.accept_channel(
90729082
&accept_channel_msg,
90739083
&config.channel_handshake_limits,

0 commit comments

Comments
 (0)