Skip to content

Commit 2e95d36

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 63ca6e1 commit 2e95d36

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17615,19 +17615,18 @@ where
1761517615
// Merge legacy pending_outbound_payments fields into a single HashMap.
1761617616
// Priority: pending_outbound_payments (TLV 3) > pending_outbound_payments_no_retry (TLV 1)
1761717617
// > pending_outbound_payments_compat (non-TLV legacy)
17618-
let pending_outbound_payments = if let Some(payments) = pending_outbound_payments {
17619-
payments
17620-
} else if let Some(mut pending_outbound_payments_no_retry) =
17621-
pending_outbound_payments_no_retry
17622-
{
17623-
let mut outbounds = new_hash_map();
17624-
for (id, session_privs) in pending_outbound_payments_no_retry.drain() {
17625-
outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
17626-
}
17627-
outbounds
17628-
} else {
17629-
pending_outbound_payments_compat
17630-
};
17618+
let pending_outbound_payments = pending_outbound_payments
17619+
.or_else(|| {
17620+
pending_outbound_payments_no_retry.map(|no_retry| {
17621+
no_retry
17622+
.into_iter()
17623+
.map(|(id, session_privs)| {
17624+
(id, PendingOutboundPayment::Legacy { session_privs })
17625+
})
17626+
.collect()
17627+
})
17628+
})
17629+
.unwrap_or(pending_outbound_payments_compat);
1763117630

1763217631
// Merge legacy in-flight monitor updates (keyed by OutPoint) into the new format (keyed by
1763317632
// ChannelId).
@@ -17636,24 +17635,30 @@ where
1763617635
if legacy_in_flight_upds.is_empty() {
1763717636
return Err(DecodeError::InvalidValue);
1763817637
}
17639-
if in_flight_monitor_updates.is_none() {
17640-
let in_flight_upds = in_flight_monitor_updates.get_or_insert_with(new_hash_map);
17641-
for ((counterparty_node_id, funding_txo), updates) in legacy_in_flight_upds {
17638+
match &in_flight_monitor_updates {
17639+
None => {
17640+
// Convert legacy format (OutPoint) to new format (ChannelId).
1764217641
// All channels with legacy in flight monitor updates are v1 channels.
17643-
let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17644-
in_flight_upds.insert((counterparty_node_id, channel_id), updates);
17645-
}
17646-
} else if in_flight_monitor_updates.as_ref().unwrap().is_empty() {
17647-
// Both TLVs present - the new one takes precedence but must not be empty.
17648-
return Err(DecodeError::InvalidValue);
17642+
in_flight_monitor_updates = Some(
17643+
legacy_in_flight_upds
17644+
.into_iter()
17645+
.map(|((counterparty_node_id, funding_txo), updates)| {
17646+
let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17647+
((counterparty_node_id, channel_id), updates)
17648+
})
17649+
.collect(),
17650+
);
17651+
},
17652+
Some(upds) if upds.is_empty() => {
17653+
// Both TLVs present but new one is empty - invalid.
17654+
return Err(DecodeError::InvalidValue);
17655+
},
17656+
Some(_) => {}, // New format takes precedence, nothing to do.
1764917657
}
1765017658
}
1765117659

1765217660
// Resolve events_override: if present, it replaces pending_events.
17653-
let mut pending_events_read = pending_events_read;
17654-
if let Some(events) = events_override {
17655-
pending_events_read = events;
17656-
}
17661+
let pending_events_read = events_override.unwrap_or(pending_events_read);
1765717662

1765817663
Ok(ChannelManagerData {
1765917664
chain_hash,

0 commit comments

Comments
 (0)