Skip to content

Commit d8f9df1

Browse files
Add MPP ID to pending_outbound_htlcs
We'll use this to correlate MPP shards in upcoming commits
1 parent bc1cbd0 commit d8f9df1

File tree

1 file changed

+92
-46
lines changed

1 file changed

+92
-46
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,11 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
491491
/// which may generate a claim event, we may receive similar duplicate claim/fail MonitorEvents
492492
/// after reloading from disk while replaying blocks against ChannelMonitors.
493493
///
494+
/// Each payment has each of its MPP fragment's session_priv bytes in the HashSet of the map
495+
/// (even payments over a single path).
496+
///
494497
/// Locked *after* channel_state.
495-
pending_outbound_payments: Mutex<HashSet<[u8; 32]>>,
498+
pending_outbound_payments: Mutex<HashMap<MppId, HashSet<[u8; 32]>>>,
496499

497500
our_network_key: SecretKey,
498501
our_network_pubkey: PublicKey,
@@ -1156,7 +1159,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
11561159
pending_msg_events: Vec::new(),
11571160
}),
11581161
pending_inbound_payments: Mutex::new(HashMap::new()),
1159-
pending_outbound_payments: Mutex::new(HashSet::new()),
1162+
pending_outbound_payments: Mutex::new(HashMap::new()),
11601163

11611164
our_network_key: keys_manager.get_node_secret(),
11621165
our_network_pubkey: PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()),
@@ -1853,7 +1856,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
18531856
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
18541857

18551858
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
1856-
assert!(self.pending_outbound_payments.lock().unwrap().insert(session_priv_bytes));
1859+
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
1860+
let sessions = pending_outbounds.entry(mpp_id).or_insert(HashSet::new());
1861+
assert!(sessions.insert(session_priv_bytes));
18571862

18581863
let err: Result<(), _> = loop {
18591864
let mut channel_lock = self.channel_state.lock().unwrap();
@@ -2832,25 +2837,29 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28322837
self.fail_htlc_backwards_internal(channel_state,
28332838
htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data});
28342839
},
2835-
HTLCSource::OutboundRoute { session_priv, .. } => {
2836-
if {
2837-
let mut session_priv_bytes = [0; 32];
2838-
session_priv_bytes.copy_from_slice(&session_priv[..]);
2839-
self.pending_outbound_payments.lock().unwrap().remove(&session_priv_bytes)
2840-
} {
2841-
self.pending_events.lock().unwrap().push(
2842-
events::Event::PaymentFailed {
2843-
payment_hash,
2844-
rejected_by_dest: false,
2845-
#[cfg(test)]
2846-
error_code: None,
2847-
#[cfg(test)]
2848-
error_data: None,
2840+
HTLCSource::OutboundRoute { session_priv, mpp_id, .. } => {
2841+
let mut session_priv_bytes = [0; 32];
2842+
session_priv_bytes.copy_from_slice(&session_priv[..]);
2843+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
2844+
if let Some(sessions) = outbounds.get_mut(&mpp_id) {
2845+
if sessions.remove(&session_priv_bytes) {
2846+
self.pending_events.lock().unwrap().push(
2847+
events::Event::PaymentFailed {
2848+
payment_hash,
2849+
rejected_by_dest: false,
2850+
#[cfg(test)]
2851+
error_code: None,
2852+
#[cfg(test)]
2853+
error_data: None,
2854+
}
2855+
);
2856+
if sessions.len() == 0 {
2857+
outbounds.remove(&mpp_id);
28492858
}
2850-
)
2851-
} else {
2852-
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
2859+
continue
2860+
}
28532861
}
2862+
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
28542863
},
28552864
};
28562865
}
@@ -2872,14 +2881,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28722881
// from block_connected which may run during initialization prior to the chain_monitor
28732882
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
28742883
match source {
2875-
HTLCSource::OutboundRoute { ref path, session_priv, .. } => {
2876-
if {
2877-
let mut session_priv_bytes = [0; 32];
2878-
session_priv_bytes.copy_from_slice(&session_priv[..]);
2879-
!self.pending_outbound_payments.lock().unwrap().remove(&session_priv_bytes)
2880-
} {
2884+
HTLCSource::OutboundRoute { ref path, session_priv, mpp_id, .. } => {
2885+
let mut session_priv_bytes = [0; 32];
2886+
session_priv_bytes.copy_from_slice(&session_priv[..]);
2887+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
2888+
if let Some(sessions) = outbounds.get_mut(&mpp_id) {
2889+
if !sessions.remove(&session_priv_bytes) {
2890+
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
2891+
return;
2892+
}
2893+
if sessions.len() == 0 {
2894+
outbounds.remove(&mpp_id);
2895+
}
2896+
} else {
28812897
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
2882-
return;
2898+
return
28832899
}
28842900
log_trace!(self.logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
28852901
mem::drop(channel_state_lock);
@@ -3123,20 +3139,23 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31233139

31243140
fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_preimage: PaymentPreimage, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool) {
31253141
match source {
3126-
HTLCSource::OutboundRoute { session_priv, .. } => {
3142+
HTLCSource::OutboundRoute { session_priv, mpp_id, .. } => {
31273143
mem::drop(channel_state_lock);
3128-
if {
3129-
let mut session_priv_bytes = [0; 32];
3130-
session_priv_bytes.copy_from_slice(&session_priv[..]);
3131-
self.pending_outbound_payments.lock().unwrap().remove(&session_priv_bytes)
3132-
} {
3133-
let mut pending_events = self.pending_events.lock().unwrap();
3134-
pending_events.push(events::Event::PaymentSent {
3135-
payment_preimage
3136-
});
3137-
} else {
3138-
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
3144+
let mut session_priv_bytes = [0; 32];
3145+
session_priv_bytes.copy_from_slice(&session_priv[..]);
3146+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
3147+
if let Some(sessions) = outbounds.get_mut(&mpp_id) {
3148+
if sessions.remove(&session_priv_bytes) {
3149+
self.pending_events.lock().unwrap().push(
3150+
events::Event::PaymentSent { payment_preimage }
3151+
);
3152+
if sessions.len() == 0 {
3153+
outbounds.remove(&mpp_id);
3154+
}
3155+
return
3156+
}
31393157
}
3158+
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
31403159
},
31413160
HTLCSource::PreviousHopData(hop_data) => {
31423161
let prev_outpoint = hop_data.outpoint;
@@ -5110,10 +5129,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
51105129
pending_payment.write(writer)?;
51115130
}
51125131

5132+
(0 as u64).write(writer)?; // for backwards compat with the old format of pending_outbound_payments
51135133
let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
51145134
(pending_outbound_payments.len() as u64).write(writer)?;
5115-
for session_priv in pending_outbound_payments.iter() {
5116-
session_priv.write(writer)?;
5135+
for (mpp_id, session_priv_set) in pending_outbound_payments.iter() {
5136+
mpp_id.write(writer)?;
5137+
(session_priv_set.len() as u64).write(writer)?;
5138+
for session_priv in session_priv_set.iter() {
5139+
session_priv.write(writer)?;
5140+
}
51175141
}
51185142

51195143
write_tlv_fields!(writer, {});
@@ -5369,11 +5393,33 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
53695393
}
53705394
}
53715395

5372-
let pending_outbound_payments_count: u64 = Readable::read(reader)?;
5373-
let mut pending_outbound_payments: HashSet<[u8; 32]> = HashSet::with_capacity(cmp::min(pending_outbound_payments_count as usize, MAX_ALLOC_SIZE/32));
5374-
for _ in 0..pending_outbound_payments_count {
5375-
if !pending_outbound_payments.insert(Readable::read(reader)?) {
5376-
return Err(DecodeError::InvalidValue);
5396+
let pending_outbound_payments_count_compat: u64 = Readable::read(reader)?;
5397+
let mut pending_outbound_payments: HashMap<MppId, HashSet<[u8; 32]>> =
5398+
HashMap::with_capacity(cmp::min(pending_outbound_payments_count_compat as usize, MAX_ALLOC_SIZE/32));
5399+
5400+
match pending_outbound_payments_count_compat {
5401+
0 => {
5402+
// TODO: is this line OK for old clients?
5403+
let pending_outbound_payments_count: u64 = Readable::read(reader)?;
5404+
for _ in 0..pending_outbound_payments_count {
5405+
let mpp_id = Readable::read(reader)?;
5406+
let mut sessions = HashSet::new();
5407+
let session_count: u64 = Readable::read(reader)?;
5408+
for _ in 0..session_count {
5409+
if !sessions.insert(Readable::read(reader)?) {
5410+
return Err(DecodeError::InvalidValue);
5411+
}
5412+
}
5413+
pending_outbound_payments.insert(mpp_id, sessions);
5414+
}
5415+
},
5416+
n => {
5417+
for _ in 0..n {
5418+
let session_priv = Readable::read(reader)?;
5419+
if pending_outbound_payments.insert(MppId(session_priv), [session_priv].iter().cloned().collect()).is_some() {
5420+
return Err(DecodeError::InvalidValue);
5421+
};
5422+
}
53775423
}
53785424
}
53795425

0 commit comments

Comments
 (0)