Skip to content

Commit cfff997

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

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 {
@@ -6289,6 +6290,7 @@ where
62896290
self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
62906291
payment_preimage: payment_preimage_arg,
62916292
htlc_id: htlc_id_arg,
6293+
attribution_data: None,
62926294
});
62936295
return UpdateFulfillFetch::NewClaim {
62946296
monitor_update,
@@ -12543,7 +12545,7 @@ where
1254312545
Vec::with_capacity(holding_cell_htlc_update_count);
1254412546
let mut holding_cell_blinding_points: Vec<Option<PublicKey>> =
1254512547
Vec::with_capacity(holding_cell_htlc_update_count);
12546-
let mut holding_cell_failure_attribution_data: Vec<Option<&AttributionData>> =
12548+
let mut holding_cell_attribution_data: Vec<Option<&AttributionData>> =
1254712549
Vec::with_capacity(holding_cell_htlc_update_count);
1254812550
// Vec of (htlc_id, failure_code, sha256_of_onion)
1254912551
let mut malformed_htlcs: Vec<(u64, u16, [u8; 32])> = Vec::new();
@@ -12569,19 +12571,25 @@ where
1256912571
holding_cell_skimmed_fees.push(skimmed_fee_msat);
1257012572
holding_cell_blinding_points.push(blinding_point);
1257112573
},
12572-
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
12574+
&HTLCUpdateAwaitingACK::ClaimHTLC {
12575+
ref payment_preimage,
12576+
ref htlc_id,
12577+
ref attribution_data,
12578+
} => {
1257312579
1u8.write(writer)?;
1257412580
payment_preimage.write(writer)?;
1257512581
htlc_id.write(writer)?;
12582+
12583+
// Store the attribution data for later writing.
12584+
holding_cell_attribution_data.push(attribution_data.as_ref());
1257612585
},
1257712586
&HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet } => {
1257812587
2u8.write(writer)?;
1257912588
htlc_id.write(writer)?;
1258012589
err_packet.data.write(writer)?;
1258112590

1258212591
// Store the attribution data for later writing.
12583-
holding_cell_failure_attribution_data
12584-
.push(err_packet.attribution_data.as_ref());
12592+
holding_cell_attribution_data.push(err_packet.attribution_data.as_ref());
1258512593
},
1258612594
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
1258712595
htlc_id,
@@ -12598,7 +12606,7 @@ where
1259812606

1259912607
// Push 'None' attribution data for FailMalformedHTLC, because FailMalformedHTLC uses the same
1260012608
// type 2 and is deserialized as a FailHTLC.
12601-
holding_cell_failure_attribution_data.push(None);
12609+
holding_cell_attribution_data.push(None);
1260212610
},
1260312611
}
1260412612
}
@@ -12802,7 +12810,7 @@ where
1280212810
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
1280312811
(54, self.pending_funding, optional_vec), // Added in 0.2
1280412812
(55, removed_htlc_attribution_data, optional_vec), // Added in 0.2
12805-
(57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
12813+
(57, holding_cell_attribution_data, optional_vec), // Added in 0.2
1280612814
(58, self.interactive_tx_signing_session, option), // Added in 0.2
1280712815
(59, self.funding.minimum_depth_override, option), // Added in 0.2
1280812816
(60, self.context.historical_scids, optional_vec), // Added in 0.2
@@ -12976,6 +12984,7 @@ where
1297612984
1 => HTLCUpdateAwaitingACK::ClaimHTLC {
1297712985
payment_preimage: Readable::read(reader)?,
1297812986
htlc_id: Readable::read(reader)?,
12987+
attribution_data: None,
1297912988
},
1298012989
2 => HTLCUpdateAwaitingACK::FailHTLC {
1298112990
htlc_id: Readable::read(reader)?,
@@ -13148,7 +13157,7 @@ where
1314813157
let mut holding_cell_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
1314913158

1315013159
let mut removed_htlc_attribution_data: Option<Vec<Option<AttributionData>>> = None;
13151-
let mut holding_cell_failure_attribution_data: Option<Vec<Option<AttributionData>>> = None;
13160+
let mut holding_cell_attribution_data: Option<Vec<Option<AttributionData>>> = None;
1315213161

1315313162
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
1315413163
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
@@ -13201,7 +13210,7 @@ where
1320113210
(53, funding_tx_broadcast_safe_event_emitted, option),
1320213211
(54, pending_funding, optional_vec), // Added in 0.2
1320313212
(55, removed_htlc_attribution_data, optional_vec),
13204-
(57, holding_cell_failure_attribution_data, optional_vec),
13213+
(57, holding_cell_attribution_data, optional_vec),
1320513214
(58, interactive_tx_signing_session, option), // Added in 0.2
1320613215
(59, minimum_depth_override, option), // Added in 0.2
1320713216
(60, historical_scids, optional_vec), // Added in 0.2
@@ -13329,24 +13338,23 @@ where
1332913338
}
1333013339
}
1333113340

13332-
if let Some(attribution_data_list) = holding_cell_failure_attribution_data {
13333-
let mut holding_cell_failures =
13334-
holding_cell_htlc_updates.iter_mut().filter_map(|upd| {
13335-
if let HTLCUpdateAwaitingACK::FailHTLC {
13341+
if let Some(attribution_data_list) = holding_cell_attribution_data {
13342+
let mut holding_cell_htlcs =
13343+
holding_cell_htlc_updates.iter_mut().filter_map(|upd| match upd {
13344+
HTLCUpdateAwaitingACK::FailHTLC {
1333613345
err_packet: OnionErrorPacket { ref mut attribution_data, .. },
1333713346
..
13338-
} = upd
13339-
{
13347+
} => Some(attribution_data),
13348+
HTLCUpdateAwaitingACK::ClaimHTLC { attribution_data, .. } => {
1334013349
Some(attribution_data)
13341-
} else {
13342-
None
13343-
}
13350+
},
13351+
_ => None,
1334413352
});
1334513353

1334613354
for attribution_data in attribution_data_list {
13347-
*holding_cell_failures.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
13355+
*holding_cell_htlcs.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
1334813356
}
13349-
if holding_cell_failures.next().is_some() {
13357+
if holding_cell_htlcs.next().is_some() {
1335013358
return Err(DecodeError::InvalidValue);
1335113359
}
1335213360
}
@@ -14340,47 +14348,62 @@ mod tests {
1434014348
skimmed_fee_msat: None,
1434114349
blinding_point: None,
1434214350
};
14343-
let dummy_holding_cell_claim_htlc = HTLCUpdateAwaitingACK::ClaimHTLC {
14351+
let dummy_holding_cell_claim_htlc = |attribution_data| HTLCUpdateAwaitingACK::ClaimHTLC {
1434414352
payment_preimage: PaymentPreimage([42; 32]),
1434514353
htlc_id: 0,
14354+
attribution_data,
1434614355
};
14347-
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
14348-
htlc_id,
14349-
err_packet: msgs::OnionErrorPacket {
14350-
data: vec![42],
14351-
attribution_data: Some(AttributionData::new()),
14352-
},
14353-
};
14356+
let dummy_holding_cell_failed_htlc =
14357+
|htlc_id, attribution_data| HTLCUpdateAwaitingACK::FailHTLC {
14358+
htlc_id,
14359+
err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data },
14360+
};
1435414361
let dummy_holding_cell_malformed_htlc =
1435514362
|htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
1435614363
htlc_id,
1435714364
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
1435814365
sha256_of_onion: [0; 32],
1435914366
};
1436014367
let mut holding_cell_htlc_updates = Vec::with_capacity(12);
14361-
for i in 0..12 {
14362-
if i % 5 == 0 {
14363-
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
14364-
} else if i % 5 == 1 {
14365-
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc.clone());
14366-
} else if i % 5 == 2 {
14367-
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
14368-
if let HTLCUpdateAwaitingACK::AddHTLC {
14369-
ref mut blinding_point,
14370-
ref mut skimmed_fee_msat,
14371-
..
14372-
} = &mut dummy_add
14373-
{
14374-
*blinding_point = Some(test_utils::pubkey(42 + i));
14375-
*skimmed_fee_msat = Some(42);
14376-
} else {
14377-
panic!()
14378-
}
14379-
holding_cell_htlc_updates.push(dummy_add);
14380-
} else if i % 5 == 3 {
14381-
holding_cell_htlc_updates.push(dummy_holding_cell_malformed_htlc(i as u64));
14382-
} else {
14383-
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(i as u64));
14368+
for i in 0..16 {
14369+
match i % 7 {
14370+
0 => {
14371+
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
14372+
},
14373+
1 => {
14374+
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc(None));
14375+
},
14376+
2 => {
14377+
holding_cell_htlc_updates
14378+
.push(dummy_holding_cell_claim_htlc(Some(AttributionData::new())));
14379+
},
14380+
3 => {
14381+
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
14382+
if let HTLCUpdateAwaitingACK::AddHTLC {
14383+
ref mut blinding_point,
14384+
ref mut skimmed_fee_msat,
14385+
..
14386+
} = &mut dummy_add
14387+
{
14388+
*blinding_point = Some(test_utils::pubkey(42 + i));
14389+
*skimmed_fee_msat = Some(42);
14390+
} else {
14391+
panic!()
14392+
}
14393+
holding_cell_htlc_updates.push(dummy_add);
14394+
},
14395+
4 => {
14396+
holding_cell_htlc_updates.push(dummy_holding_cell_malformed_htlc(i as u64));
14397+
},
14398+
5 => {
14399+
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(i as u64, None));
14400+
},
14401+
_ => {
14402+
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(
14403+
i as u64,
14404+
Some(AttributionData::new()),
14405+
));
14406+
},
1438414407
}
1438514408
}
1438614409
chan.context.holding_cell_htlc_updates = holding_cell_htlc_updates.clone();

0 commit comments

Comments
 (0)