Skip to content

Commit

Permalink
Avoid && in public API for onion peeling/decoding.
Browse files Browse the repository at this point in the history
No reason to take a reference to a Deref.
  • Loading branch information
valentinewallace committed Jul 24, 2024
1 parent 884e85f commit 1f7d3fa
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions fuzz/src/onion_hop_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn onion_hop_data_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
let node_signer = test_utils::TestNodeSigner::new(test_utils::privkey(42));
let _ = <lightning::ln::msgs::InboundOnionPayload as ReadableArgs<(
Option<PublicKey>,
&&test_utils::TestNodeSigner,
&test_utils::TestNodeSigner,
)>>::read(&mut r, (None, &&node_signer));
}

Expand All @@ -34,6 +34,6 @@ pub extern "C" fn onion_hop_data_run(data: *const u8, datalen: usize) {
let node_signer = test_utils::TestNodeSigner::new(test_utils::privkey(42));
let _ = <lightning::ln::msgs::InboundOnionPayload as ReadableArgs<(
Option<PublicKey>,
&&test_utils::TestNodeSigner,
&test_utils::TestNodeSigner,
)>>::read(&mut r, (None, &&node_signer));
}
8 changes: 4 additions & 4 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ fn route_blinding_spec_test_vector() {
// < MIN_CLTV_EXPIRY_DELTA).
let (bob_peeled_onion, _, next_packet_details_opt) =
match onion_payment::decode_incoming_update_add_htlc_onion(
&bob_update_add, &&bob_node_signer, &&logger, &secp_ctx
&bob_update_add, &bob_node_signer, &logger, &secp_ctx
) {
Ok(res) => res,
_ => panic!("Unexpected error")
Expand Down Expand Up @@ -1519,7 +1519,7 @@ fn route_blinding_spec_test_vector() {
let carol_node_signer = TestEcdhSigner { node_secret: carol_secret };
let (carol_peeled_onion, _, next_packet_details_opt) =
match onion_payment::decode_incoming_update_add_htlc_onion(
&carol_update_add, &&carol_node_signer, &&logger, &secp_ctx
&carol_update_add, &carol_node_signer, &logger, &secp_ctx
) {
Ok(res) => res,
_ => panic!("Unexpected error")
Expand Down Expand Up @@ -1553,7 +1553,7 @@ fn route_blinding_spec_test_vector() {
let dave_node_signer = TestEcdhSigner { node_secret: dave_secret };
let (dave_peeled_onion, _, next_packet_details_opt) =
match onion_payment::decode_incoming_update_add_htlc_onion(
&dave_update_add, &&dave_node_signer, &&logger, &secp_ctx
&dave_update_add, &dave_node_signer, &logger, &secp_ctx
) {
Ok(res) => res,
_ => panic!("Unexpected error")
Expand Down Expand Up @@ -1588,7 +1588,7 @@ fn route_blinding_spec_test_vector() {
// We can't decode the final payload because it contains a path_id and is missing some LDK
// specific fields.
match onion_payment::decode_incoming_update_add_htlc_onion(
&eve_update_add, &&eve_node_signer, &&logger, &secp_ctx
&eve_update_add, &eve_node_signer, &logger, &secp_ctx
) {
Err(HTLCFailureMsg::Malformed(msg)) => assert_eq!(msg.failure_code, INVALID_ONION_BLINDING),
_ => panic!("Unexpected error")
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3832,7 +3832,7 @@ where
(onion_utils::Hop, [u8; 32], Option<Result<PublicKey, secp256k1::Error>>), HTLCFailureMsg
> {
let (next_hop, shared_secret, next_packet_details_opt) = decode_incoming_update_add_htlc_onion(
msg, &self.node_signer, &self.logger, &self.secp_ctx
msg, &*self.node_signer, &*self.logger, &self.secp_ctx
)?;

let next_packet_details = match next_packet_details_opt {
Expand Down Expand Up @@ -5044,7 +5044,7 @@ where
let mut htlc_fails = Vec::new();
for update_add_htlc in &update_add_htlcs {
let (next_hop, shared_secret, next_packet_details_opt) = match decode_incoming_update_add_htlc_onion(
&update_add_htlc, &self.node_signer, &self.logger, &self.secp_ctx
&update_add_htlc, &*self.node_signer, &*self.logger, &self.secp_ctx
) {
Ok(decoded_onion) => decoded_onion,
Err(htlc_fail) => {
Expand Down Expand Up @@ -5221,7 +5221,7 @@ where
let phantom_shared_secret = self.node_signer.ecdh(Recipient::PhantomNode, &onion_packet.public_key.unwrap(), None).unwrap().secret_bytes();
let next_hop = match onion_utils::decode_next_payment_hop(
phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac,
payment_hash, None, &self.node_signer
payment_hash, None, &*self.node_signer
) {
Ok(res) => res,
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
Expand Down
18 changes: 9 additions & 9 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2706,8 +2706,8 @@ impl Writeable for OutboundTrampolinePayload {
}


impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload where NS::Target: NodeSigner {
fn read<R: Read>(r: &mut R, args: (Option<PublicKey>, &NS)) -> Result<Self, DecodeError> {
impl<NS: Deref> ReadableArgs<(Option<PublicKey>, NS)> for InboundOnionPayload where NS::Target: NodeSigner {
fn read<R: Read>(r: &mut R, args: (Option<PublicKey>, NS)) -> Result<Self, DecodeError> {
let (update_add_blinding_point, node_signer) = args;

let mut amt = None;
Expand Down Expand Up @@ -4406,7 +4406,7 @@ mod tests {
assert_eq!(encoded_value, target_value);

let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &node_signer)).unwrap();
if let msgs::InboundOnionPayload::Forward {
short_channel_id, amt_to_forward, outgoing_cltv_value
} = inbound_msg {
Expand All @@ -4431,7 +4431,7 @@ mod tests {
assert_eq!(encoded_value, target_value);

let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &node_signer)).unwrap();
if let msgs::InboundOnionPayload::Receive {
payment_data: None, sender_intended_htlc_amt_msat, cltv_expiry_height, ..
} = inbound_msg {
Expand Down Expand Up @@ -4459,7 +4459,7 @@ mod tests {
assert_eq!(encoded_value, target_value);

let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &node_signer)).unwrap();
if let msgs::InboundOnionPayload::Receive {
payment_data: Some(FinalOnionHopData {
payment_secret,
Expand Down Expand Up @@ -4495,7 +4495,7 @@ mod tests {
};
let encoded_value = msg.encode();
let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
assert!(msgs::InboundOnionPayload::read(&mut Cursor::new(&encoded_value[..]), (None, &&node_signer)).is_err());
assert!(msgs::InboundOnionPayload::read(&mut Cursor::new(&encoded_value[..]), (None, &node_signer)).is_err());
let good_type_range_tlvs = vec![
((1 << 16) - 3, vec![42]),
((1 << 16) - 1, vec![42; 32]),
Expand All @@ -4504,7 +4504,7 @@ mod tests {
*custom_tlvs = &good_type_range_tlvs;
}
let encoded_value = msg.encode();
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&encoded_value[..]), (None, &&node_signer)).unwrap();
let inbound_msg = ReadableArgs::read(&mut Cursor::new(&encoded_value[..]), (None, &node_signer)).unwrap();
match inbound_msg {
msgs::InboundOnionPayload::Receive { custom_tlvs, .. } => assert!(custom_tlvs.is_empty()),
_ => panic!(),
Expand All @@ -4529,7 +4529,7 @@ mod tests {
let target_value = <Vec<u8>>::from_hex("2e02080badf00d010203040404ffffffffff0000000146c6616b021234ff0000000146c6616f084242424242424242").unwrap();
assert_eq!(encoded_value, target_value);
let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
let inbound_msg: msgs::InboundOnionPayload = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
let inbound_msg: msgs::InboundOnionPayload = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &node_signer)).unwrap();
if let msgs::InboundOnionPayload::Receive {
payment_data: None,
payment_metadata: None,
Expand Down Expand Up @@ -4759,7 +4759,7 @@ mod tests {
let mut rd = Cursor::new(&big_payload[..]);

let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
<msgs::InboundOnionPayload as ReadableArgs<(Option<PublicKey>, &&test_utils::TestKeysInterface)>>
<msgs::InboundOnionPayload as ReadableArgs<(Option<PublicKey>, &test_utils::TestKeysInterface)>>
::read(&mut rd, (None, &&node_signer)).unwrap();
}
// see above test, needs to be a separate method for use of the serialization macros.
Expand Down
8 changes: 4 additions & 4 deletions lightning/src/ln/onion_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub(super) fn create_recv_pending_htlc_info(
///
/// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
pub fn peel_payment_onion<NS: Deref, L: Deref, T: secp256k1::Verification>(
msg: &msgs::UpdateAddHTLC, node_signer: &NS, logger: &L, secp_ctx: &Secp256k1<T>,
msg: &msgs::UpdateAddHTLC, node_signer: NS, logger: L, secp_ctx: &Secp256k1<T>,
cur_height: u32, accept_mpp_keysend: bool, allow_skimmed_fees: bool,
) -> Result<PendingHTLCInfo, InboundHTLCErr>
where
Expand Down Expand Up @@ -346,7 +346,7 @@ pub(super) struct NextPacketDetails {
}

pub(super) fn decode_incoming_update_add_htlc_onion<NS: Deref, L: Deref, T: secp256k1::Verification>(
msg: &msgs::UpdateAddHTLC, node_signer: &NS, logger: &L, secp_ctx: &Secp256k1<T>,
msg: &msgs::UpdateAddHTLC, node_signer: NS, logger: L, secp_ctx: &Secp256k1<T>,
) -> Result<(onion_utils::Hop, [u8; 32], Option<NextPacketDetails>), HTLCFailureMsg>
where
NS::Target: NodeSigner,
Expand Down Expand Up @@ -574,7 +574,7 @@ mod tests {
let msg = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, onion);
let logger = test_utils::TestLogger::with_id("bob".to_string());

let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true, false)
let peeled = peel_payment_onion(&msg, &bob, &logger, &secp_ctx, cur_height, true, false)
.map_err(|e| e.msg).unwrap();

let next_onion = match peeled.routing {
Expand All @@ -585,7 +585,7 @@ mod tests {
};

let msg2 = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, next_onion);
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true, false)
let peeled2 = peel_payment_onion(&msg2, &charlie, &logger, &secp_ctx, cur_height, true, false)
.map_err(|e| e.msg).unwrap();

match peeled2.routing {
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ pub(crate) enum OnionDecodeErr {

pub(crate) fn decode_next_payment_hop<NS: Deref>(
shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash,
blinding_point: Option<PublicKey>, node_signer: &NS,
blinding_point: Option<PublicKey>, node_signer: NS,
) -> Result<Hop, OnionDecodeErr>
where
NS::Target: NodeSigner,
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4283,7 +4283,7 @@ fn peel_payment_onion_custom_tlvs() {
blinding_point: None,
};
let peeled_onion = crate::ln::onion_payment::peel_payment_onion(
&update_add, &&chanmon_cfgs[1].keys_manager, &&chanmon_cfgs[1].logger, &secp_ctx,
&update_add, &chanmon_cfgs[1].keys_manager, &chanmon_cfgs[1].logger, &secp_ctx,
nodes[1].best_block_info().1, true, false
).unwrap();
assert_eq!(peeled_onion.incoming_amt_msat, Some(amt_msat));
Expand Down

0 comments on commit 1f7d3fa

Please sign in to comment.