Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Trampoline Packet deserialization for Inbound::Receive #2806

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11867,6 +11867,7 @@ mod tests {
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
}),
custom_tlvs: Vec::new(),
trampoline_packet: None
};
// Check that if the amount we received + the penultimate hop extra fee is less than the sender
// intended amount, we fail the payment.
Expand All @@ -11889,6 +11890,7 @@ mod tests {
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
}),
custom_tlvs: Vec::new(),
trampoline_packet: None
};
let current_height: u32 = node[0].node.best_block.read().unwrap().height();
assert!(create_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
Expand All @@ -11913,6 +11915,7 @@ mod tests {
payment_secret: PaymentSecret([0; 32]), total_msat: 100,
}),
custom_tlvs: Vec::new(),
trampoline_packet: None
}, [0; 32], PaymentHash([0; 32]), 100, 23, None, true, None, current_height,
node[0].node.default_configuration.accept_mpp_keysend);

Expand Down
20 changes: 20 additions & 0 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@ mod fuzzy_internal_msgs {
custom_tlvs: Vec<(u64, Vec<u8>)>,
amt_msat: u64,
outgoing_cltv_value: u32,
trampoline_packet: Option<VariableLengthOnionPacket>
},
BlindedForward {
short_channel_id: u64,
Expand Down Expand Up @@ -1806,6 +1807,21 @@ pub struct VariableLengthOnionPacket {
pub hmac: [u8; 32],
}

impl Readable for VariableLengthOnionPacket {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
Ok(VariableLengthOnionPacket {
version: Readable::read(r)?,
public_key: {
let mut buf = [0u8;33];
r.read_exact(&mut buf)?;
PublicKey::from_slice(&buf)
},
hop_data: Readable::read(r)?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a unit test to make sure this works? The current spec doesn't encode the length of the hop_data, so I think there's a chance this might break without using a reader with an a priori specified fixed length.

hmac: Readable::read(r)?,
})
}
}

impl fmt::Debug for VariableLengthOnionPacket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!("VariableLengthOnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
Expand Down Expand Up @@ -2373,6 +2389,7 @@ impl<NS: Deref> ReadableArgs<&NS> for InboundOnionPayload where NS::Target: Node
let mut total_msat = None;
let mut keysend_preimage: Option<PaymentPreimage> = None;
let mut custom_tlvs = Vec::new();
let mut trampoline_packet: Option<VariableLengthOnionPacket> = None;

let tlv_len = BigSize::read(r)?;
let rd = FixedLengthReader::new(r, tlv_len.0);
Expand All @@ -2385,6 +2402,7 @@ impl<NS: Deref> ReadableArgs<&NS> for InboundOnionPayload where NS::Target: Node
(12, intro_node_blinding_point, option),
(16, payment_metadata, option),
(18, total_msat, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
(66100, trampoline_packet, option),
// See https://github.com/lightning/blips/blob/master/blip-0003.md
(5482373484, keysend_preimage, option)
}, |msg_type: u64, msg_reader: &mut FixedLengthReader<_>| -> Result<bool, DecodeError> {
Expand Down Expand Up @@ -2461,6 +2479,7 @@ impl<NS: Deref> ReadableArgs<&NS> for InboundOnionPayload where NS::Target: Node
amt_msat: amt.ok_or(DecodeError::InvalidValue)?,
outgoing_cltv_value: cltv_value.ok_or(DecodeError::InvalidValue)?,
custom_tlvs,
trampoline_packet
})
}
}
Expand Down Expand Up @@ -4094,6 +4113,7 @@ mod tests {
payment_metadata: None,
keysend_preimage: None,
custom_tlvs,
..
} = inbound_msg {
assert_eq!(payment_secret, expected_payment_secret);
assert_eq!(amt_msat, 0x0badf00d01020304);
Expand Down