Skip to content

Commit e7d370b

Browse files
committed
Introduce test for padding
1. Add test to verify blinded message and payment path padding.
1 parent 37925db commit e7d370b

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,12 @@ impl<T: Writeable> Writeable for WithPadding<T> {
199199

200200
self.tlvs.write(writer)
201201
}
202-
}
202+
}
203+
204+
#[cfg(test)]
205+
/// Checks if all the packets in the blinded path are properly padded, ensuring they are of equal size.
206+
pub fn is_properly_padded(path: &BlindedPath) -> bool {
207+
let first_hop = path.blinded_hops.first().expect("BlindedPath must have at least one hop");
208+
let first_payload_size = first_hop.encrypted_payload.len();
209+
path.blinded_hops.iter().all(|hop| hop.encrypted_payload.len() == first_payload_size)
210+
}

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// licenses.
99

1010
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
11+
use crate::blinded_path::utils::is_properly_padded;
1112
use crate::blinded_path::BlindedPath;
1213
use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, ReceiveTlvs};
1314
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentFailureReason};
@@ -1332,3 +1333,39 @@ fn custom_tlvs_to_blinded_path() {
13321333
.with_custom_tlvs(recipient_onion_fields.custom_tlvs.clone())
13331334
);
13341335
}
1336+
1337+
#[test]
1338+
fn blinded_payment_path_padding() {
1339+
// Make sure that for a blinded payment path, all encrypted payloads are padded to equal lengths.
1340+
let chanmon_cfgs = create_chanmon_cfgs(5);
1341+
let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
1342+
let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
1343+
let mut nodes = create_network(5, &node_cfgs, &node_chanmgrs);
1344+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
1345+
create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
1346+
let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
1347+
let chan_upd_3_4 = create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 1_000_000, 0).0.contents;
1348+
1349+
// Get all our nodes onto the same height so payments don't fail for CLTV violations.
1350+
connect_blocks(&nodes[0], nodes[4].best_block_info().1 - nodes[0].best_block_info().1);
1351+
connect_blocks(&nodes[1], nodes[4].best_block_info().1 - nodes[1].best_block_info().1);
1352+
connect_blocks(&nodes[2], nodes[4].best_block_info().1 - nodes[2].best_block_info().1);
1353+
assert_eq!(nodes[4].best_block_info().1, nodes[3].best_block_info().1);
1354+
1355+
let amt_msat = 5000;
1356+
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[4], Some(amt_msat), None);
1357+
1358+
let blinded_path = blinded_payment_path(payment_secret, 1, 1_0000_0000,
1359+
nodes.iter().skip(2).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_2_3, &chan_upd_3_4],
1360+
&chanmon_cfgs[4].keys_manager
1361+
);
1362+
1363+
is_properly_padded(&blinded_path.1);
1364+
1365+
let route_params = RouteParameters::from_payment_params_and_value(PaymentParameters::blinded(vec![blinded_path]), amt_msat);
1366+
1367+
nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
1368+
check_added_monitors(&nodes[0], 1);
1369+
pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2], &nodes[3], &nodes[4]]], amt_msat, payment_hash, payment_secret);
1370+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2], &nodes[3], &nodes[4]], payment_preimage);
1371+
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! Onion message testing and test utilities live here.
1111
12+
use crate::blinded_path::utils::is_properly_padded;
1213
use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
1314
use crate::blinded_path::message::{ForwardNode, MessageContext, OffersContext};
1415
use crate::events::{Event, EventsProvider};
@@ -539,6 +540,29 @@ fn too_big_packet_error() {
539540
assert_eq!(err, SendError::TooBigPacket);
540541
}
541542

543+
#[test]
544+
fn blinded_path_padding() {
545+
// Make sure that for a blinded path, all encrypted payloads are padded to equal lengths.
546+
let nodes = create_nodes(4);
547+
let test_msg = TestCustomMessage::Pong;
548+
549+
let secp_ctx = Secp256k1::new();
550+
let intermediate_nodes = [
551+
ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
552+
ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
553+
];
554+
let context = MessageContext::Custom(Vec::new());
555+
let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
556+
557+
assert!(is_properly_padded(&blinded_path));
558+
559+
let destination = Destination::BlindedPath(blinded_path);
560+
561+
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
562+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Pong);
563+
pass_along_path(&nodes);
564+
}
565+
542566
#[test]
543567
fn we_are_intro_node() {
544568
// If we are sending straight to a blinded path and we are the introduction node, we need to

0 commit comments

Comments
 (0)