Skip to content

Commit 978dc35

Browse files
joostjagerclaude
andcommitted
Simplify legacy TLV resolution in ChannelManagerData::read
The previous commit intentionally kept the code as close to a move as possible to ease review. This follow-up applies idiomatic simplifications: - Use unwrap_or for events_override resolution - Use unwrap_or_else with iterator chains for pending_outbound_payments - Use match with into_iter().collect() for in_flight_monitor_updates Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d4f9945 commit 978dc35

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17537,44 +17537,49 @@ where
1753717537
// Merge legacy pending_outbound_payments fields into a single HashMap.
1753817538
// Priority: pending_outbound_payments (TLV 3) > pending_outbound_payments_no_retry (TLV 1)
1753917539
// > pending_outbound_payments_compat (non-TLV legacy)
17540-
let pending_outbound_payments = if let Some(payments) = pending_outbound_payments {
17541-
payments
17542-
} else if let Some(mut pending_outbound_payments_no_retry) = pending_outbound_payments_no_retry
17543-
{
17544-
let mut outbounds = new_hash_map();
17545-
for (id, session_privs) in pending_outbound_payments_no_retry.drain() {
17546-
outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
17547-
}
17548-
outbounds
17549-
} else {
17550-
pending_outbound_payments_compat
17551-
};
17540+
let pending_outbound_payments = pending_outbound_payments.unwrap_or_else(|| {
17541+
pending_outbound_payments_no_retry
17542+
.map(|no_retry| {
17543+
no_retry
17544+
.into_iter()
17545+
.map(|(id, session_privs)| {
17546+
(id, PendingOutboundPayment::Legacy { session_privs })
17547+
})
17548+
.collect()
17549+
})
17550+
.unwrap_or(pending_outbound_payments_compat)
17551+
});
1755217552

1755317553
// Merge legacy in-flight monitor updates (keyed by OutPoint) into the new format (keyed by
1755417554
// ChannelId).
1755517555
if let Some(legacy_in_flight_upds) = legacy_in_flight_monitor_updates {
17556-
// We should never serialize an empty map.
1755717556
if legacy_in_flight_upds.is_empty() {
1755817557
return Err(DecodeError::InvalidValue);
1755917558
}
17560-
if in_flight_monitor_updates.is_none() {
17561-
let in_flight_upds = in_flight_monitor_updates.get_or_insert_with(new_hash_map);
17562-
for ((counterparty_node_id, funding_txo), updates) in legacy_in_flight_upds {
17559+
match &in_flight_monitor_updates {
17560+
None => {
17561+
// Convert legacy format (OutPoint) to new format (ChannelId).
1756317562
// All channels with legacy in flight monitor updates are v1 channels.
17564-
let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17565-
in_flight_upds.insert((counterparty_node_id, channel_id), updates);
17566-
}
17567-
} else if in_flight_monitor_updates.as_ref().unwrap().is_empty() {
17568-
// Both TLVs present - the new one takes precedence but must not be empty.
17569-
return Err(DecodeError::InvalidValue);
17563+
in_flight_monitor_updates = Some(
17564+
legacy_in_flight_upds
17565+
.into_iter()
17566+
.map(|((counterparty_node_id, funding_txo), updates)| {
17567+
let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17568+
((counterparty_node_id, channel_id), updates)
17569+
})
17570+
.collect(),
17571+
);
17572+
},
17573+
Some(upds) if upds.is_empty() => {
17574+
// Both TLVs present but new one is empty - invalid.
17575+
return Err(DecodeError::InvalidValue);
17576+
},
17577+
Some(_) => {}, // New format takes precedence, nothing to do.
1757017578
}
1757117579
}
1757217580

1757317581
// Resolve events_override: if present, it replaces pending_events.
17574-
let mut pending_events = pending_events;
17575-
if let Some(events) = events_override {
17576-
pending_events = events;
17577-
}
17582+
let pending_events = events_override.unwrap_or(pending_events);
1757817583

1757917584
Ok(ChannelManagerData {
1758017585
chain_hash,

0 commit comments

Comments
 (0)