Skip to content

Commit 61bb7bf

Browse files
committed
Make attribution_data optional
we need this for deserializing various old objects, eg if we decided to fail an htlc on a previous version, stored that that htlc is pending-failure and then shut down and upgraded.
1 parent b49d0ac commit 61bb7bf

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

lightning/src/ln/channel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4672,7 +4672,7 @@ trait FailHTLCContents {
46724672
impl FailHTLCContents for msgs::OnionErrorPacket {
46734673
type Message = msgs::UpdateFailHTLC;
46744674
fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
4675-
msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data, attribution_data: Some(self.attribution_data) }
4675+
msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data, attribution_data: self.attribution_data }
46764676
}
46774677
fn to_inbound_htlc_state(self) -> InboundHTLCState {
46784678
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(self))
@@ -6700,7 +6700,7 @@ impl<SP: Deref> FundedChannel<SP> where
67006700
channel_id: self.context.channel_id(),
67016701
htlc_id: htlc.htlc_id,
67026702
reason: err_packet.data.clone(),
6703-
attribution_data: Some(err_packet.attribution_data)
6703+
attribution_data: err_packet.attribution_data,
67046704
});
67056705
},
67066706
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4417,7 +4417,7 @@ where
44174417
channel_id: msg.channel_id,
44184418
htlc_id: msg.htlc_id,
44194419
reason: failure.data.clone(),
4420-
attribution_data: Some(failure.attribution_data)
4420+
attribution_data: failure.attribution_data,
44214421
})
44224422
}
44234423

@@ -4448,7 +4448,7 @@ where
44484448
channel_id: msg.channel_id,
44494449
htlc_id: msg.htlc_id,
44504450
reason: failure.data,
4451-
attribution_data: Some(failure.attribution_data)
4451+
attribution_data: failure.attribution_data,
44524452
}));
44534453
}
44544454
}

lightning/src/ln/msgs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,14 @@ pub(crate) struct OnionErrorPacket {
20392039
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
20402040
// (TODO) We limit it in decode to much lower...
20412041
pub(crate) data: Vec<u8>,
2042-
pub(crate) attribution_data: [u8; ATTRIBUTION_DATA_LEN]
2042+
pub(crate) attribution_data: Option<[u8; ATTRIBUTION_DATA_LEN]>,
20432043
}
20442044

20452045
impl From<&UpdateFailHTLC> for OnionErrorPacket {
20462046
fn from(msg: &UpdateFailHTLC) -> Self {
20472047
OnionErrorPacket {
20482048
data: msg.reason.clone(),
2049-
attribution_data: msg.attribution_data.unwrap(), // TODO: Make safe
2049+
attribution_data: msg.attribution_data,
20502050
}
20512051
}
20522052
}

lightning/src/ln/onion_payment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ where
406406
channel_id: msg.channel_id,
407407
htlc_id: msg.htlc_id,
408408
reason: failure.data,
409-
attribution_data: Some(failure.attribution_data)
409+
attribution_data: failure.attribution_data,
410410
}));
411411
};
412412

lightning/src/ln/onion_utils.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ pub(super) fn build_failure_packet(
929929

930930
OnionErrorPacket {
931931
data: data,
932-
attribution_data,
932+
attribution_data: Some(attribution_data),
933933
}
934934
}
935935

@@ -1077,8 +1077,8 @@ where
10771077
// Check attr error hmacs
10781078

10791079
let message = &encrypted_packet.data;
1080-
let payloads = &encrypted_packet.attribution_data[..MAX_HOPS * PAYLOAD_LEN];
1081-
let hmacs = &encrypted_packet.attribution_data[MAX_HOPS * PAYLOAD_LEN..];
1080+
let payloads = &encrypted_packet.attribution_data.as_ref().unwrap()[..MAX_HOPS * PAYLOAD_LEN]; // XXX: This will break if we get an err from an unupgraded node
1081+
let hmacs = &encrypted_packet.attribution_data.as_ref().unwrap()[MAX_HOPS * PAYLOAD_LEN..]; // XXX: This will break if we get an err from an unupgraded node
10821082

10831083
let um = gen_um_from_shared_secret(shared_secret.as_ref());
10841084
let mut hmac = HmacEngine::<Sha256>::new(&um);
@@ -1108,11 +1108,11 @@ where
11081108
}
11091109

11101110
// Shift payloads left.
1111-
let payloads = &mut encrypted_packet.attribution_data[..MAX_HOPS * PAYLOAD_LEN];
1111+
let payloads = &mut encrypted_packet.attribution_data.as_mut().unwrap()[..MAX_HOPS * PAYLOAD_LEN]; // XXX: This will break if we get an err from an unupgraded node
11121112
payloads.copy_within(PAYLOAD_LEN.., 0);
11131113

11141114
// Shift hmacs left.
1115-
let hmacs = &mut encrypted_packet.attribution_data[MAX_HOPS * PAYLOAD_LEN..];
1115+
let hmacs = &mut encrypted_packet.attribution_data.as_mut().unwrap()[MAX_HOPS * PAYLOAD_LEN..]; // XXX: This will break if we get an err from an unupgraded node
11161116
let mut src_idx = MAX_HOPS;
11171117
let mut dest_idx = 1;
11181118
let mut copy_len = MAX_HOPS - 1;
@@ -1428,7 +1428,7 @@ impl HTLCFailReason {
14281428
}
14291429

14301430
pub(super) fn from_msg(msg: &msgs::UpdateFailHTLC) -> Self {
1431-
Self(HTLCFailReasonRepr::LightningError { err: OnionErrorPacket{ data: msg.reason.clone(), attribution_data: msg.attribution_data.unwrap() } }) // TODO: Make safe
1431+
Self(HTLCFailReasonRepr::LightningError { err: OnionErrorPacket{ data: msg.reason.clone(), attribution_data: msg.attribution_data } })
14321432
}
14331433

14341434
pub(super) fn get_encrypted_failure_packet(
@@ -1922,7 +1922,7 @@ fn process_failure_packet(onion_error: &mut OnionErrorPacket, shared_secret: &[u
19221922

19231923
// Shift payloads right.
19241924
{
1925-
let payloads = &onion_error.attribution_data[..MAX_HOPS * PAYLOAD_LEN];
1925+
let payloads = &onion_error.attribution_data.as_ref().unwrap()[..MAX_HOPS * PAYLOAD_LEN]; // XXX: This will break if we get an err from an unupgraded node
19261926
processed_packet[PAYLOAD_LEN..MAX_HOPS * PAYLOAD_LEN].copy_from_slice(&payloads[..payloads.len()-PAYLOAD_LEN]);
19271927

19281928
// Add this node's payload.
@@ -1931,7 +1931,7 @@ fn process_failure_packet(onion_error: &mut OnionErrorPacket, shared_secret: &[u
19311931

19321932
// Shift hmacs right.
19331933
{
1934-
let hmacs = &onion_error.attribution_data[MAX_HOPS * PAYLOAD_LEN..];
1934+
let hmacs = &onion_error.attribution_data.as_ref().unwrap()[MAX_HOPS * PAYLOAD_LEN..]; // XXX: This will break if we get an err from an unupgraded node
19351935
let processed_hmacs = &mut processed_packet[MAX_HOPS * PAYLOAD_LEN..];
19361936

19371937
let mut src_idx = HMAC_COUNT - 2;
@@ -1956,7 +1956,7 @@ fn process_failure_packet(onion_error: &mut OnionErrorPacket, shared_secret: &[u
19561956
// Add this node's hmacs.
19571957
add_hmacs(&shared_secret, &onion_error.data, &mut processed_packet);
19581958

1959-
onion_error.attribution_data = processed_packet;
1959+
onion_error.attribution_data = Some(processed_packet);
19601960
}
19611961

19621962

0 commit comments

Comments
 (0)