Skip to content

Commit ba7deab

Browse files
committed
Add AttributionData to HTLCUpdateAwaitingACK::ClaimHTLC
Necessary to preserve attribution data when the HTLC is in the holding cell.
1 parent f212530 commit ba7deab

File tree

1 file changed

+73
-50
lines changed

1 file changed

+73
-50
lines changed

lightning/src/ln/channel.rs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ enum HTLCUpdateAwaitingACK {
468468
},
469469
ClaimHTLC {
470470
payment_preimage: PaymentPreimage,
471+
attribution_data: Option<AttributionData>,
471472
htlc_id: u64,
472473
},
473474
FailHTLC {
@@ -6271,6 +6272,7 @@ where
62716272
self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
62726273
payment_preimage: payment_preimage_arg,
62736274
htlc_id: htlc_id_arg,
6275+
attribution_data: None,
62746276
});
62756277
return UpdateFulfillFetch::NewClaim {
62766278
monitor_update,
@@ -12518,7 +12520,7 @@ where
1251812520
Vec::with_capacity(holding_cell_htlc_update_count);
1251912521
let mut holding_cell_blinding_points: Vec<Option<PublicKey>> =
1252012522
Vec::with_capacity(holding_cell_htlc_update_count);
12521-
let mut holding_cell_failure_attribution_data: Vec<Option<&AttributionData>> =
12523+
let mut holding_cell_attribution_data: Vec<Option<&AttributionData>> =
1252212524
Vec::with_capacity(holding_cell_htlc_update_count);
1252312525
// Vec of (htlc_id, failure_code, sha256_of_onion)
1252412526
let mut malformed_htlcs: Vec<(u64, u16, [u8; 32])> = Vec::new();
@@ -12544,19 +12546,25 @@ where
1254412546
holding_cell_skimmed_fees.push(skimmed_fee_msat);
1254512547
holding_cell_blinding_points.push(blinding_point);
1254612548
},
12547-
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
12549+
&HTLCUpdateAwaitingACK::ClaimHTLC {
12550+
ref payment_preimage,
12551+
ref htlc_id,
12552+
ref attribution_data,
12553+
} => {
1254812554
1u8.write(writer)?;
1254912555
payment_preimage.write(writer)?;
1255012556
htlc_id.write(writer)?;
12557+
12558+
// Store the attribution data for later writing.
12559+
holding_cell_attribution_data.push(attribution_data.as_ref());
1255112560
},
1255212561
&HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet } => {
1255312562
2u8.write(writer)?;
1255412563
htlc_id.write(writer)?;
1255512564
err_packet.data.write(writer)?;
1255612565

1255712566
// Store the attribution data for later writing.
12558-
holding_cell_failure_attribution_data
12559-
.push(err_packet.attribution_data.as_ref());
12567+
holding_cell_attribution_data.push(err_packet.attribution_data.as_ref());
1256012568
},
1256112569
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
1256212570
htlc_id,
@@ -12573,7 +12581,7 @@ where
1257312581

1257412582
// Push 'None' attribution data for FailMalformedHTLC, because FailMalformedHTLC uses the same
1257512583
// type 2 and is deserialized as a FailHTLC.
12576-
holding_cell_failure_attribution_data.push(None);
12584+
holding_cell_attribution_data.push(None);
1257712585
},
1257812586
}
1257912587
}
@@ -12777,7 +12785,7 @@ where
1277712785
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
1277812786
(54, self.pending_funding, optional_vec), // Added in 0.2
1277912787
(55, removed_htlc_attribution_data, optional_vec), // Added in 0.2
12780-
(57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
12788+
(57, holding_cell_attribution_data, optional_vec), // Added in 0.2
1278112789
(58, self.interactive_tx_signing_session, option), // Added in 0.2
1278212790
(59, self.funding.minimum_depth_override, option), // Added in 0.2
1278312791
(60, self.context.historical_scids, optional_vec), // Added in 0.2
@@ -12951,6 +12959,7 @@ where
1295112959
1 => HTLCUpdateAwaitingACK::ClaimHTLC {
1295212960
payment_preimage: Readable::read(reader)?,
1295312961
htlc_id: Readable::read(reader)?,
12962+
attribution_data: None,
1295412963
},
1295512964
2 => HTLCUpdateAwaitingACK::FailHTLC {
1295612965
htlc_id: Readable::read(reader)?,
@@ -13123,7 +13132,7 @@ where
1312313132
let mut holding_cell_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
1312413133

1312513134
let mut removed_htlc_attribution_data: Option<Vec<Option<AttributionData>>> = None;
13126-
let mut holding_cell_failure_attribution_data: Option<Vec<Option<AttributionData>>> = None;
13135+
let mut holding_cell_attribution_data: Option<Vec<Option<AttributionData>>> = None;
1312713136

1312813137
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
1312913138
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
@@ -13176,7 +13185,7 @@ where
1317613185
(53, funding_tx_broadcast_safe_event_emitted, option),
1317713186
(54, pending_funding, optional_vec), // Added in 0.2
1317813187
(55, removed_htlc_attribution_data, optional_vec),
13179-
(57, holding_cell_failure_attribution_data, optional_vec),
13188+
(57, holding_cell_attribution_data, optional_vec),
1318013189
(58, interactive_tx_signing_session, option), // Added in 0.2
1318113190
(59, minimum_depth_override, option), // Added in 0.2
1318213191
(60, historical_scids, optional_vec), // Added in 0.2
@@ -13304,24 +13313,23 @@ where
1330413313
}
1330513314
}
1330613315

13307-
if let Some(attribution_data_list) = holding_cell_failure_attribution_data {
13308-
let mut holding_cell_failures =
13309-
holding_cell_htlc_updates.iter_mut().filter_map(|upd| {
13310-
if let HTLCUpdateAwaitingACK::FailHTLC {
13316+
if let Some(attribution_data_list) = holding_cell_attribution_data {
13317+
let mut holding_cell_htlcs =
13318+
holding_cell_htlc_updates.iter_mut().filter_map(|upd| match upd {
13319+
HTLCUpdateAwaitingACK::FailHTLC {
1331113320
err_packet: OnionErrorPacket { ref mut attribution_data, .. },
1331213321
..
13313-
} = upd
13314-
{
13322+
} => Some(attribution_data),
13323+
HTLCUpdateAwaitingACK::ClaimHTLC { attribution_data, .. } => {
1331513324
Some(attribution_data)
13316-
} else {
13317-
None
13318-
}
13325+
},
13326+
_ => None,
1331913327
});
1332013328

1332113329
for attribution_data in attribution_data_list {
13322-
*holding_cell_failures.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
13330+
*holding_cell_htlcs.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
1332313331
}
13324-
if holding_cell_failures.next().is_some() {
13332+
if holding_cell_htlcs.next().is_some() {
1332513333
return Err(DecodeError::InvalidValue);
1332613334
}
1332713335
}
@@ -14315,47 +14323,62 @@ mod tests {
1431514323
skimmed_fee_msat: None,
1431614324
blinding_point: None,
1431714325
};
14318-
let dummy_holding_cell_claim_htlc = HTLCUpdateAwaitingACK::ClaimHTLC {
14326+
let dummy_holding_cell_claim_htlc = |attribution_data| HTLCUpdateAwaitingACK::ClaimHTLC {
1431914327
payment_preimage: PaymentPreimage([42; 32]),
1432014328
htlc_id: 0,
14329+
attribution_data,
1432114330
};
14322-
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
14323-
htlc_id,
14324-
err_packet: msgs::OnionErrorPacket {
14325-
data: vec![42],
14326-
attribution_data: Some(AttributionData::new()),
14327-
},
14328-
};
14331+
let dummy_holding_cell_failed_htlc =
14332+
|htlc_id, attribution_data| HTLCUpdateAwaitingACK::FailHTLC {
14333+
htlc_id,
14334+
err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data },
14335+
};
1432914336
let dummy_holding_cell_malformed_htlc =
1433014337
|htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
1433114338
htlc_id,
1433214339
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
1433314340
sha256_of_onion: [0; 32],
1433414341
};
1433514342
let mut holding_cell_htlc_updates = Vec::with_capacity(12);
14336-
for i in 0..12 {
14337-
if i % 5 == 0 {
14338-
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
14339-
} else if i % 5 == 1 {
14340-
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc.clone());
14341-
} else if i % 5 == 2 {
14342-
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
14343-
if let HTLCUpdateAwaitingACK::AddHTLC {
14344-
ref mut blinding_point,
14345-
ref mut skimmed_fee_msat,
14346-
..
14347-
} = &mut dummy_add
14348-
{
14349-
*blinding_point = Some(test_utils::pubkey(42 + i));
14350-
*skimmed_fee_msat = Some(42);
14351-
} else {
14352-
panic!()
14353-
}
14354-
holding_cell_htlc_updates.push(dummy_add);
14355-
} else if i % 5 == 3 {
14356-
holding_cell_htlc_updates.push(dummy_holding_cell_malformed_htlc(i as u64));
14357-
} else {
14358-
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(i as u64));
14343+
for i in 0..16 {
14344+
match i % 7 {
14345+
0 => {
14346+
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
14347+
},
14348+
1 => {
14349+
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc(None));
14350+
},
14351+
2 => {
14352+
holding_cell_htlc_updates
14353+
.push(dummy_holding_cell_claim_htlc(Some(AttributionData::new())));
14354+
},
14355+
3 => {
14356+
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
14357+
if let HTLCUpdateAwaitingACK::AddHTLC {
14358+
ref mut blinding_point,
14359+
ref mut skimmed_fee_msat,
14360+
..
14361+
} = &mut dummy_add
14362+
{
14363+
*blinding_point = Some(test_utils::pubkey(42 + i));
14364+
*skimmed_fee_msat = Some(42);
14365+
} else {
14366+
panic!()
14367+
}
14368+
holding_cell_htlc_updates.push(dummy_add);
14369+
},
14370+
4 => {
14371+
holding_cell_htlc_updates.push(dummy_holding_cell_malformed_htlc(i as u64));
14372+
},
14373+
5 => {
14374+
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(i as u64, None));
14375+
},
14376+
_ => {
14377+
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(
14378+
i as u64,
14379+
Some(AttributionData::new()),
14380+
));
14381+
},
1435914382
}
1436014383
}
1436114384
chan.context.holding_cell_htlc_updates = holding_cell_htlc_updates.clone();

0 commit comments

Comments
 (0)