Skip to content

Commit 4656042

Browse files
f update tests for added buffer to amt and cltv delta
1 parent 722e21e commit 4656042

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

lightning/src/ln/max_payment_path_len_tests.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::ln::msgs;
2222
use crate::ln::onion_utils;
2323
use crate::ln::outbound_payment::{RecipientOnionFields, Retry, RetryableSendFailure};
2424
use crate::prelude::*;
25-
use crate::routing::router::{PaymentParameters, RouteParameters};
25+
use crate::routing::router::{DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, PaymentParameters, RouteParameters};
2626
use crate::util::errors::APIError;
2727
use crate::util::ser::Writeable;
2828
use crate::util::test_utils;
@@ -34,6 +34,9 @@ const INTERMED_PAYLOAD_LEN_ESTIMATE: usize = 61;
3434
// Length of the HMAC of an onion payload when encoded into the packet.
3535
const PAYLOAD_HMAC_LEN: usize = 32;
3636

37+
// Copied from onion_utils.
38+
const MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY: u64 = 100_000_000;
39+
3740
#[test]
3841
fn large_payment_metadata() {
3942
// Test that we'll limit our maximum path length based on the size of the provided
@@ -51,24 +54,24 @@ fn large_payment_metadata() {
5154
// without exceeding the max onion packet size.
5255
let final_payload_len_without_metadata = msgs::OutboundOnionPayload::Receive {
5356
payment_data: Some(msgs::FinalOnionHopData {
54-
payment_secret: PaymentSecret([0; 32]), total_msat: amt_msat
57+
payment_secret: PaymentSecret([0; 32]), total_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY
5558
}),
5659
payment_metadata: None,
5760
keysend_preimage: None,
5861
custom_tlvs: &Vec::new(),
59-
sender_intended_htlc_amt_msat: amt_msat,
60-
cltv_expiry_height: nodes[0].best_block_info().1 + TEST_FINAL_CLTV,
62+
sender_intended_htlc_amt_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
63+
cltv_expiry_height: nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
6164
}.serialized_length();
6265
let max_metadata_len = 1300
63-
- 1 // metdata type
66+
- 1 // metadata type
6467
- crate::util::ser::BigSize(1200).serialized_length() // metadata length
6568
- 2 // onion payload varint prefix increased ser size due to metadata
6669
- PAYLOAD_HMAC_LEN
6770
- final_payload_len_without_metadata;
6871
let mut payment_metadata = vec![42; max_metadata_len];
6972

7073
// Check that the maximum-size metadata is sendable.
71-
let (route_0_1, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], &nodes[1], amt_msat);
74+
let (mut route_0_1, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], &nodes[1], amt_msat);
7275
let mut recipient_onion_max_md_size = RecipientOnionFields {
7376
payment_secret: Some(payment_secret),
7477
payment_metadata: Some(payment_metadata.clone()),
@@ -103,7 +106,9 @@ fn large_payment_metadata() {
103106
// Confirm that we'll fail to construct an onion packet given this payment_metadata that's too
104107
// large for even a 1-hop path.
105108
let secp_ctx = Secp256k1::signing_only();
106-
let err = onion_utils::create_payment_onion(&secp_ctx, &route_0_1.paths[0], &test_utils::privkey(42), amt_msat, &recipient_onion_too_large_md, nodes[0].best_block_info().1, &payment_hash, &None, [0; 32]).unwrap_err();
109+
route_0_1.paths[0].hops[0].fee_msat = MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
110+
route_0_1.paths[0].hops[0].cltv_expiry_delta = DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA;
111+
let err = onion_utils::create_payment_onion(&secp_ctx, &route_0_1.paths[0], &test_utils::privkey(42), MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY, &recipient_onion_too_large_md, nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &payment_hash, &None, [0; 32]).unwrap_err();
107112
match err {
108113
APIError::InvalidRoute { err } => {
109114
assert_eq!(err, "Route size too large considering onion data");
@@ -113,22 +118,22 @@ fn large_payment_metadata() {
113118

114119
// If we remove enough payment_metadata bytes to allow for 2 hops, we're now able to send to
115120
// nodes[2].
116-
let mut recipient_onion_allws_2_hops = RecipientOnionFields {
121+
let mut recipient_onion_allows_2_hops = RecipientOnionFields {
117122
payment_secret: Some(payment_secret_2),
118123
payment_metadata: Some(vec![42; max_metadata_len - INTERMED_PAYLOAD_LEN_ESTIMATE]),
119124
custom_tlvs: Vec::new(),
120125
};
121126
let mut route_params_0_2 = route_0_2.route_params.clone().unwrap();
122127
route_params_0_2.payment_params.max_path_length = 2;
123128
nodes[0].router.expect_find_route_query(route_params_0_2);
124-
nodes[0].node.send_payment(payment_hash_2, recipient_onion_allws_2_hops.clone(), PaymentId(payment_hash_2.0), route_0_2.route_params.unwrap(), Retry::Attempts(0)).unwrap();
129+
nodes[0].node.send_payment(payment_hash_2, recipient_onion_allows_2_hops.clone(), PaymentId(payment_hash_2.0), route_0_2.route_params.unwrap(), Retry::Attempts(0)).unwrap();
125130
check_added_monitors!(nodes[0], 1);
126131
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
127132
assert_eq!(events.len(), 1);
128133
let path = &[&nodes[1], &nodes[2]];
129134
let args = PassAlongPathArgs::new(&nodes[0], path, amt_msat, payment_hash_2, events.pop().unwrap())
130135
.with_payment_secret(payment_secret_2)
131-
.with_payment_metadata(recipient_onion_allws_2_hops.payment_metadata.unwrap());
136+
.with_payment_metadata(recipient_onion_allows_2_hops.payment_metadata.unwrap());
132137
do_pass_along_path(args);
133138
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage_2);
134139
}
@@ -169,9 +174,9 @@ fn one_hop_blinded_path_with_custom_tlv() {
169174
// Calculate the maximum custom TLV value size where a valid onion packet is still possible.
170175
const CUSTOM_TLV_TYPE: u64 = 65537;
171176
let final_payload_len_without_custom_tlv = msgs::OutboundOnionPayload::BlindedReceive {
172-
sender_intended_htlc_amt_msat: amt_msat,
173-
total_msat: amt_msat,
174-
cltv_expiry_height: nodes[0].best_block_info().1 + TEST_FINAL_CLTV,
177+
sender_intended_htlc_amt_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
178+
total_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
179+
cltv_expiry_height: nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
175180
encrypted_tlvs: &blinded_path.1.blinded_hops[0].encrypted_payload,
176181
intro_node_blinding_point: Some(blinded_path.1.blinding_point),
177182
keysend_preimage: None,
@@ -180,7 +185,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
180185
let max_custom_tlv_len = 1300
181186
- crate::util::ser::BigSize(CUSTOM_TLV_TYPE).serialized_length() // custom TLV type
182187
- crate::util::ser::BigSize(1200).serialized_length() // custom TLV length
183-
- 2 // onion payload varint prefix increased ser size due to custom TLV
188+
- 1 // onion payload varint prefix increased ser size due to custom TLV
184189
- PAYLOAD_HMAC_LEN
185190
- final_payload_len_without_custom_tlv;
186191

@@ -251,8 +256,9 @@ fn blinded_path_with_custom_tlv() {
251256
const CUSTOM_TLV_TYPE: u64 = 65537;
252257
let mut route = get_route(&nodes[1], &route_params).unwrap();
253258
let reserved_packet_bytes_without_custom_tlv: usize = onion_utils::build_onion_payloads(
254-
&route.paths[0], amt_msat, &RecipientOnionFields::spontaneous_empty(),
255-
nodes[0].best_block_info().1, &None
259+
&route.paths[0], MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
260+
&RecipientOnionFields::spontaneous_empty(),
261+
nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &None
256262
)
257263
.unwrap()
258264
.0
@@ -289,7 +295,9 @@ fn blinded_path_with_custom_tlv() {
289295

290296
// Confirm that we can't construct an onion packet given this too-large custom TLV.
291297
let secp_ctx = Secp256k1::signing_only();
292-
let err = onion_utils::create_payment_onion(&secp_ctx, &route.paths[0], &test_utils::privkey(42), amt_msat, &recipient_onion_too_large_custom_tlv, nodes[0].best_block_info().1, &payment_hash, &None, [0; 32]).unwrap_err();
298+
route.paths[0].hops[0].fee_msat = MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
299+
route.paths[0].hops[0].cltv_expiry_delta = DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA;
300+
let err = onion_utils::create_payment_onion(&secp_ctx, &route.paths[0], &test_utils::privkey(42), MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY, &recipient_onion_too_large_custom_tlv, nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &payment_hash, &None, [0; 32]).unwrap_err();
293301
match err {
294302
APIError::InvalidRoute { err } => {
295303
assert_eq!(err, "Route size too large considering onion data");

0 commit comments

Comments
 (0)