Skip to content

Commit 8f1af36

Browse files
committed
Review comments
1 parent 476c5dd commit 8f1af36

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: ChannelId,

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

@@ -918,7 +929,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
918929

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

924935
/// 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 {
11961207
/// Returns true if funding_signed was sent/received and the
11971208
/// funding transaction has been broadcast if necessary.
11981209
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 &&
12001211
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
12011212
}
12021213

@@ -2616,6 +2627,8 @@ impl<SP: Deref> Channel<SP> where
26162627

26172628
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26182629

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.
26192632
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26202633
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26212634
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -3115,7 +3128,7 @@ impl<SP: Deref> Channel<SP> where
31153128
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
31163129
where F::Target: FeeEstimator, L::Target: Logger
31173130
{
3118-
if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
3131+
if self.context.channel_state & !STATE_FLAGS >= ChannelState::ChannelReady as u32 &&
31193132
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
31203133
self.free_holding_cell_htlcs(fee_estimator, logger)
31213134
} else { (None, Vec::new()) }
@@ -3591,7 +3604,7 @@ impl<SP: Deref> Channel<SP> where
35913604
/// completed.
35923605
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
35933606
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 {
35953608
self.context.channel_state = ChannelState::ShutdownComplete as u32;
35963609
return;
35973610
}
@@ -3705,13 +3718,13 @@ impl<SP: Deref> Channel<SP> where
37053718
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
37063719
// first received the funding_signed.
37073720
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 {
37093722
self.context.funding_txid.take();
37103723
self.context.funding_transaction.take()
37113724
} else { None };
37123725
// That said, if the funding transaction is already confirmed (ie we're active with a
37133726
// 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) {
37153728
funding_broadcastable = None;
37163729
}
37173730

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

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

46454658
/// 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
47024715
// We got a reorg but not enough to trigger a force close, just ignore.
47034716
false
47044717
} 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 {
47064719
// We should never see a funding transaction on-chain until we've received
47074720
// funding_signed (if we're an outbound channel), or seen funding_generated (if we're
47084721
// an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -4863,7 +4876,7 @@ impl<SP: Deref> Channel<SP> where
48634876
}
48644877

48654878
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 ||
48674880
(non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
48684881
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
48694882
if self.context.funding_tx_confirmation_height == 0 {
@@ -4891,7 +4904,7 @@ impl<SP: Deref> Channel<SP> where
48914904
height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
48924905
log_info!(logger, "Closing channel {} due to funding timeout", &self.context.channel_id);
48934906
// 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);
48954908
assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
48964909
return Err(ClosureReason::FundingTimedOut);
48974910
}
@@ -5511,7 +5524,7 @@ impl<SP: Deref> Channel<SP> where
55115524
// If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
55125525
// script is set, we just force-close and call it a day.
55135526
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 {
55155528
chan_closed = true;
55165529
}
55175530

@@ -5540,7 +5553,7 @@ impl<SP: Deref> Channel<SP> where
55405553

55415554
// From here on out, we may not fail!
55425555
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 {
55445557
self.context.channel_state = ChannelState::ShutdownComplete as u32;
55455558
} else {
55465559
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
73397352
// If we've gotten to the funding stage of the channel, populate the signer with its
73407353
// required channel parameters.
73417354
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) {
73437356
holder_signer.provide_channel_parameters(&channel_parameters);
73447357
}
73457358
(channel_keys_id, holder_signer)
@@ -9064,13 +9077,10 @@ mod tests {
90649077
&config,
90659078
0,
90669079
&&logger,
9067-
42,
9080+
true, // Allow node b to send a 0conf channel_ready.
90689081
).unwrap();
90699082

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();
90749084
node_a_chan.accept_channel(
90759085
&accept_channel_msg,
90769086
&config.channel_handshake_limits,

0 commit comments

Comments
 (0)