Skip to content

Commit 72444ae

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 72444ae

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17537,44 +17537,48 @@ 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 {
17563-
// 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);
17559+
match &in_flight_monitor_updates {
17560+
None => {
17561+
// Convert legacy format (OutPoint) to new format (ChannelId).
17562+
in_flight_monitor_updates = Some(
17563+
legacy_in_flight_upds
17564+
.into_iter()
17565+
.map(|((counterparty_node_id, funding_txo), updates)| {
17566+
let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17567+
((counterparty_node_id, channel_id), updates)
17568+
})
17569+
.collect(),
17570+
);
17571+
},
17572+
Some(upds) if upds.is_empty() => {
17573+
// Both TLVs present but new one is empty - invalid.
17574+
return Err(DecodeError::InvalidValue);
17575+
},
17576+
Some(_) => {}, // New format takes precedence, nothing to do.
1757017577
}
1757117578
}
1757217579

1757317580
// 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-
}
17581+
let pending_events = events_override.unwrap_or(pending_events);
1757817582

1757917583
Ok(ChannelManagerData {
1758017584
chain_hash,

0 commit comments

Comments
 (0)