Skip to content

Commit 1762459

Browse files
committed
Review comments
1 parent 5a19c53 commit 1762459

File tree

5 files changed

+383
-47
lines changed

5 files changed

+383
-47
lines changed

lightning/src/ln/channel.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
11941194

11951195
// Checks whether we should emit a `ChannelPending` event.
11961196
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
1197-
self.is_funding_initiated() && !self.channel_pending_event_emitted
1197+
self.is_funding_broadcast() && !self.channel_pending_event_emitted
11981198
}
11991199

12001200
// Returns whether we already emitted a `ChannelPending` event.
@@ -1255,7 +1255,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
12551255

12561256
/// Returns true if funding_signed was sent/received and the
12571257
/// funding transaction has been broadcast if necessary.
1258-
pub fn is_funding_initiated(&self) -> bool {
1258+
pub fn is_funding_broadcast(&self) -> bool {
12591259
self.channel_state & !STATE_FLAGS >= ChannelState::FundingSent as u32 &&
12601260
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
12611261
}
@@ -2676,8 +2676,12 @@ impl<SP: Deref> Channel<SP> where
26762676

26772677
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26782678

2679-
// If the WaitingForBatch flag is set, we can receive their channel_ready, but our
2680-
// channel_ready shouldn't have been sent and we shouldn't move to ChannelReady.
2679+
// Our channel_ready shouldn't have been sent if we are waiting for other channels in the
2680+
// batch, but we can receive channel_ready messages.
2681+
debug_assert!(
2682+
non_shutdown_state & ChannelState::OurChannelReady as u32 == 0 ||
2683+
non_shutdown_state & ChannelState::WaitingForBatch as u32 == 0
2684+
);
26812685
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26822686
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26832687
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
@@ -9108,7 +9112,7 @@ mod tests {
91089112

91099113
// Create a channel from node a to node b that will be part of batch funding.
91109114
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
9111-
let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
9115+
let mut node_a_chan = OutboundV1Channel::<&TestKeysInterface>::new(
91129116
&feeest,
91139117
&&keys_provider,
91149118
&&keys_provider,
@@ -9124,7 +9128,7 @@ mod tests {
91249128

91259129
let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
91269130
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9127-
let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9131+
let mut node_b_chan = InboundV1Channel::<&TestKeysInterface>::new(
91289132
&feeest,
91299133
&&keys_provider,
91309134
&&keys_provider,

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,15 +1198,15 @@ where
11981198
/// `PersistenceNotifierGuard::notify_on_drop(..)` and pass the lock to it, to ensure the
11991199
/// Notifier the lock contains sends out a notification when the lock is released.
12001200
total_consistency_lock: RwLock<()>,
1201-
/// Tracks the progress of channels going through batch v1 channel establishment by whether
1202-
/// funding_signed was received and the monitor has been persisted.
1201+
/// Tracks the progress of channels going through batch funding by whether funding_signed was
1202+
/// received and the monitor has been persisted.
12031203
///
12041204
/// This information does not need to be persisted as funding nodes can forget
12051205
/// unfunded channels upon disconnection.
1206-
v1_funding_batch_states: FairRwLock<HashMap<Txid, Mutex<HashMap<([u8;32], PublicKey), bool>>>>,
1206+
funding_batch_states: FairRwLock<HashMap<Txid, Mutex<HashMap<([u8;32], PublicKey), bool>>>>,
12071207
/// Remaining channels in a funding batch need to be closed when one channel closes.
12081208
/// These batches are maintained here to be periodically processed to simplify locking behavior.
1209-
v1_funding_batches_to_be_closed: Mutex<Vec<Txid>>,
1209+
funding_batches_to_be_closed: Mutex<Vec<Txid>>,
12101210

12111211
background_events_processed_since_startup: AtomicBool,
12121212

@@ -1818,10 +1818,10 @@ macro_rules! update_maps_on_chan_removal {
18181818
short_to_chan_info.remove(&$channel_context.outbound_scid_alias());
18191819
// If the channel was part of a batch funding transaction, all channels in that
18201820
// batch are affected.
1821-
let v1_funding_batch_states = $self.v1_funding_batch_states.read().unwrap();
1821+
let funding_batch_states = $self.funding_batch_states.read().unwrap();
18221822
$channel_context.unbroadcasted_funding_txid().map(|txid| {
1823-
if v1_funding_batch_states.contains_key(&txid) {
1824-
$self.v1_funding_batches_to_be_closed.lock().unwrap().push(txid);
1823+
if funding_batch_states.contains_key(&txid) {
1824+
$self.funding_batches_to_be_closed.lock().unwrap().push(txid);
18251825
}
18261826
})
18271827
}}
@@ -1972,9 +1972,9 @@ macro_rules! handle_monitor_update_completion {
19721972
// should be updated as we have received funding_signed and persisted the monitor.
19731973
let mut completed_batch = None;
19741974
{
1975-
let v1_funding_batch_states = $self.v1_funding_batch_states.read().unwrap();
1975+
let funding_batch_states = $self.funding_batch_states.read().unwrap();
19761976
let batch_state_key_value = $chan.context.unbroadcasted_funding_txid()
1977-
.and_then(|txid| v1_funding_batch_states.get_key_value(&txid));
1977+
.and_then(|txid| funding_batch_states.get_key_value(&txid));
19781978
if let Some((txid, batch_state)) = batch_state_key_value {
19791979
let mut batch_state = batch_state.lock().unwrap();
19801980
batch_state.insert(
@@ -2024,7 +2024,7 @@ macro_rules! handle_monitor_update_completion {
20242024
// When all channels in a batched funding transaction have become ready, it is not necessary
20252025
// to track the progress of the batch anymore and the state of the channels can be updated.
20262026
if let Some(txid) = completed_batch {
2027-
let other_channel_ids = $self.v1_funding_batch_states.write().unwrap()
2027+
let other_channel_ids = $self.funding_batch_states.write().unwrap()
20282028
.remove(&txid)
20292029
.map(|batch_state| batch_state.into_inner().unwrap().into_iter().map(|(k, _)| k))
20302030
.into_iter().flatten()
@@ -2259,8 +2259,8 @@ where
22592259
total_consistency_lock: RwLock::new(()),
22602260
background_events_processed_since_startup: AtomicBool::new(false),
22612261
persistence_notifier: Notifier::new(),
2262-
v1_funding_batch_states: FairRwLock::new(HashMap::new()),
2263-
v1_funding_batches_to_be_closed: Mutex::new(Vec::new()),
2262+
funding_batch_states: FairRwLock::new(HashMap::new()),
2263+
funding_batches_to_be_closed: Mutex::new(Vec::new()),
22642264

22652265
entropy_source,
22662266
node_signer,
@@ -3664,7 +3664,7 @@ where
36643664
}
36653665

36663666
let is_batch_funding = temporary_channels.len() > 1;
3667-
let v1_funding_batch_state = RefCell::new(HashMap::new());
3667+
let funding_batch_state = RefCell::new(HashMap::new());
36683668
for (temporary_channel_id, counterparty_node_id) in temporary_channels {
36693669
result = result.and_then(|_| self.funding_transaction_generated_intern(
36703670
temporary_channel_id,
@@ -3689,7 +3689,7 @@ where
36893689
});
36903690
}
36913691
let outpoint = OutPoint { txid: tx.txid(), index: output_index.unwrap() };
3692-
v1_funding_batch_state.borrow_mut().insert((outpoint.to_channel_id(), (*counterparty_node_id).clone()), false);
3692+
funding_batch_state.borrow_mut().insert((outpoint.to_channel_id(), (*counterparty_node_id).clone()), false);
36933693
Ok(outpoint)
36943694
})
36953695
);
@@ -3707,7 +3707,7 @@ where
37073707
self.issue_channel_close_events(&chan.context, ClosureReason::ProcessingError { err: e.clone() });
37083708
});
37093709
}
3710-
for (channel_id, counterparty_node_id) in v1_funding_batch_state.borrow().keys() {
3710+
for (channel_id, counterparty_node_id) in funding_batch_state.borrow().keys() {
37113711
per_peer_state.get(counterparty_node_id)
37123712
.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
37133713
.and_then(|mut peer_state| peer_state.channel_by_id.remove(channel_id))
@@ -3718,9 +3718,9 @@ where
37183718
}
37193719
} else if is_batch_funding {
37203720
// Initialize the state of the batch.
3721-
self.v1_funding_batch_states.write().unwrap().insert(
3721+
self.funding_batch_states.write().unwrap().insert(
37223722
funding_transaction.txid(),
3723-
Mutex::new(v1_funding_batch_state.into_inner()),
3723+
Mutex::new(funding_batch_state.into_inner()),
37243724
);
37253725
}
37263726
result
@@ -4784,9 +4784,9 @@ where
47844784
// Close remaining channels in funding batches when one channel closes.
47854785
let mut affected_channels = Vec::new();
47864786
{
4787-
let mut v1_funding_batch_states = self.v1_funding_batch_states.write().unwrap();
4788-
for txid in self.v1_funding_batches_to_be_closed.lock().unwrap().drain(..) {
4789-
affected_channels.extend(v1_funding_batch_states
4787+
let mut funding_batch_states = self.funding_batch_states.write().unwrap();
4788+
for txid in self.funding_batches_to_be_closed.lock().unwrap().drain(..) {
4789+
affected_channels.extend(funding_batch_states
47904790
.remove(&txid)
47914791
.map(|state| state.into_inner().unwrap().into_iter().map(|(k, _)| k))
47924792
.into_iter().flatten()
@@ -5850,7 +5850,7 @@ where
58505850
match peer_state.channel_by_id.entry(msg.channel_id) {
58515851
hash_map::Entry::Occupied(mut chan) => {
58525852
let is_batch_funding = chan.get().context.unbroadcasted_funding_txid()
5853-
.map(|txid| self.v1_funding_batch_states.read().unwrap().contains_key(&txid))
5853+
.map(|txid| self.funding_batch_states.read().unwrap().contains_key(&txid))
58545854
.unwrap_or(false);
58555855
let monitor = try_chan_entry!(self,
58565856
chan.get_mut().funding_signed(&msg, best_block, &self.signer_provider, is_batch_funding, &self.logger), chan);
@@ -7552,20 +7552,24 @@ where
75527552
let peer_state = &mut *peer_state_lock;
75537553

75547554
peer_state.channel_by_id.retain(|_, chan| {
7555-
if !chan.context.is_funding_initiated() {
7555+
if !chan.context.is_funding_broadcast() {
7556+
update_maps_on_chan_removal!(self, &chan.context);
7557+
self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
75567558
// It is possible to have persisted the monitor upon funding_signed
75577559
// but not have broadcast the transaction, especially for batch funding.
75587560
// The monitor should be moved to the correct state.
75597561
self.finish_force_close_channel(chan.context.force_shutdown(false));
7562+
false
75607563
} else {
75617564
chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
7565+
if chan.is_shutdown() {
7566+
update_maps_on_chan_removal!(self, &chan.context);
7567+
self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
7568+
false
7569+
} else {
7570+
true
7571+
}
75627572
}
7563-
if chan.is_shutdown() {
7564-
update_maps_on_chan_removal!(self, &chan.context);
7565-
self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
7566-
return false;
7567-
}
7568-
true
75697573
});
75707574
peer_state.inbound_v1_channel_by_id.retain(|_, chan| {
75717575
update_maps_on_chan_removal!(self, &chan.context);
@@ -8370,7 +8374,7 @@ where
83708374
}
83718375
number_of_channels += peer_state.channel_by_id.len();
83728376
for (_, channel) in peer_state.channel_by_id.iter() {
8373-
if !channel.context.is_funding_initiated() {
8377+
if !channel.context.is_funding_broadcast() {
83748378
unfunded_channels += 1;
83758379
}
83768380
}
@@ -8382,7 +8386,7 @@ where
83828386
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
83838387
let peer_state = &mut *peer_state_lock;
83848388
for (_, channel) in peer_state.channel_by_id.iter() {
8385-
if channel.context.is_funding_initiated() {
8389+
if channel.context.is_funding_broadcast() {
83868390
channel.write(writer)?;
83878391
}
83888392
}
@@ -8829,7 +8833,7 @@ where
88298833
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
88308834
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
88318835
}
8832-
if channel.context.is_funding_initiated() {
8836+
if channel.context.is_funding_broadcast() {
88338837
id_to_peer.insert(channel.context.channel_id(), channel.context.get_counterparty_node_id());
88348838
}
88358839
match peer_channels.entry(channel.context.get_counterparty_node_id()) {
@@ -9526,8 +9530,8 @@ where
95269530
total_consistency_lock: RwLock::new(()),
95279531
background_events_processed_since_startup: AtomicBool::new(false),
95289532
persistence_notifier: Notifier::new(),
9529-
v1_funding_batch_states: FairRwLock::new(HashMap::new()),
9530-
v1_funding_batches_to_be_closed: Mutex::new(Vec::new()),
9533+
funding_batch_states: FairRwLock::new(HashMap::new()),
9534+
funding_batches_to_be_closed: Mutex::new(Vec::new()),
95319535

95329536
entropy_source: args.entropy_source,
95339537
node_signer: args.node_signer,

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,12 +1447,12 @@ pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: Clo
14471447
let events = node.node.get_and_clear_pending_events();
14481448
assert_eq!(events.len(), events_count, "{:?}", events);
14491449
let mut issues_discard_funding = false;
1450-
for (idx, event) in events.into_iter().enumerate() {
1450+
for event in events {
14511451
match event {
1452-
Event::ChannelClosed { ref reason, counterparty_node_id,
1452+
Event::ChannelClosed { ref reason, counterparty_node_id,
14531453
channel_capacity_sats, .. } => {
14541454
assert_eq!(*reason, expected_reason);
1455-
assert_eq!(counterparty_node_id.unwrap(), expected_counterparty_node_ids[idx]);
1455+
assert!(expected_counterparty_node_ids.iter().any(|id| id == &counterparty_node_id.unwrap()));
14561456
assert_eq!(channel_capacity_sats.unwrap(), expected_channel_capacity);
14571457
},
14581458
Event::DiscardFunding { .. } => {
@@ -1473,7 +1473,7 @@ macro_rules! check_closed_event {
14731473
check_closed_event!($node, $events, $reason, false, $counterparty_node_ids, $channel_capacity);
14741474
};
14751475
($node: expr, $events: expr, $reason: expr, $is_check_discard_funding: expr, $counterparty_node_ids: expr, $channel_capacity: expr) => {
1476-
$crate::ln::functional_test_utils::check_closed_event(&$node, $events, $reason,
1476+
$crate::ln::functional_test_utils::check_closed_event(&$node, $events, $reason,
14771477
$is_check_discard_funding, &$counterparty_node_ids, $channel_capacity);
14781478
}
14791479
}

0 commit comments

Comments
 (0)