@@ -22,7 +22,7 @@ use crate::ln::msgs;
22
22
use crate :: ln:: onion_utils;
23
23
use crate :: ln:: outbound_payment:: { RecipientOnionFields , Retry , RetryableSendFailure } ;
24
24
use crate :: prelude:: * ;
25
- use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
25
+ use crate :: routing:: router:: { DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA , PaymentParameters , RouteParameters } ;
26
26
use crate :: util:: errors:: APIError ;
27
27
use crate :: util:: ser:: Writeable ;
28
28
use crate :: util:: test_utils;
@@ -34,6 +34,9 @@ const INTERMED_PAYLOAD_LEN_ESTIMATE: usize = 61;
34
34
// Length of the HMAC of an onion payload when encoded into the packet.
35
35
const PAYLOAD_HMAC_LEN : usize = 32 ;
36
36
37
+ // Copied from onion_utils.
38
+ const MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY : u64 = 100_000_000 ;
39
+
37
40
#[ test]
38
41
fn large_payment_metadata ( ) {
39
42
// Test that we'll limit our maximum path length based on the size of the provided
@@ -51,24 +54,24 @@ fn large_payment_metadata() {
51
54
// without exceeding the max onion packet size.
52
55
let final_payload_len_without_metadata = msgs:: OutboundOnionPayload :: Receive {
53
56
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
55
58
} ) ,
56
59
payment_metadata : None ,
57
60
keysend_preimage : None ,
58
61
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 ,
61
64
} . serialized_length ( ) ;
62
65
let max_metadata_len = 1300
63
- - 1 // metdata type
66
+ - 1 // metadata type
64
67
- crate :: util:: ser:: BigSize ( 1200 ) . serialized_length ( ) // metadata length
65
68
- 2 // onion payload varint prefix increased ser size due to metadata
66
69
- PAYLOAD_HMAC_LEN
67
70
- final_payload_len_without_metadata;
68
71
let mut payment_metadata = vec ! [ 42 ; max_metadata_len] ;
69
72
70
73
// 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) ;
72
75
let mut recipient_onion_max_md_size = RecipientOnionFields {
73
76
payment_secret : Some ( payment_secret) ,
74
77
payment_metadata : Some ( payment_metadata. clone ( ) ) ,
@@ -103,7 +106,9 @@ fn large_payment_metadata() {
103
106
// Confirm that we'll fail to construct an onion packet given this payment_metadata that's too
104
107
// large for even a 1-hop path.
105
108
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 ( ) ;
107
112
match err {
108
113
APIError :: InvalidRoute { err } => {
109
114
assert_eq ! ( err, "Route size too large considering onion data" ) ;
@@ -113,22 +118,22 @@ fn large_payment_metadata() {
113
118
114
119
// If we remove enough payment_metadata bytes to allow for 2 hops, we're now able to send to
115
120
// nodes[2].
116
- let mut recipient_onion_allws_2_hops = RecipientOnionFields {
121
+ let mut recipient_onion_allows_2_hops = RecipientOnionFields {
117
122
payment_secret : Some ( payment_secret_2) ,
118
123
payment_metadata : Some ( vec ! [ 42 ; max_metadata_len - INTERMED_PAYLOAD_LEN_ESTIMATE ] ) ,
119
124
custom_tlvs : Vec :: new ( ) ,
120
125
} ;
121
126
let mut route_params_0_2 = route_0_2. route_params . clone ( ) . unwrap ( ) ;
122
127
route_params_0_2. payment_params . max_path_length = 2 ;
123
128
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 ( ) ;
125
130
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
126
131
let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
127
132
assert_eq ! ( events. len( ) , 1 ) ;
128
133
let path = & [ & nodes[ 1 ] , & nodes[ 2 ] ] ;
129
134
let args = PassAlongPathArgs :: new ( & nodes[ 0 ] , path, amt_msat, payment_hash_2, events. pop ( ) . unwrap ( ) )
130
135
. 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 ( ) ) ;
132
137
do_pass_along_path ( args) ;
133
138
claim_payment_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] , false , payment_preimage_2) ;
134
139
}
@@ -169,9 +174,9 @@ fn one_hop_blinded_path_with_custom_tlv() {
169
174
// Calculate the maximum custom TLV value size where a valid onion packet is still possible.
170
175
const CUSTOM_TLV_TYPE : u64 = 65537 ;
171
176
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 ,
175
180
encrypted_tlvs : & blinded_path. 1 . blinded_hops [ 0 ] . encrypted_payload ,
176
181
intro_node_blinding_point : Some ( blinded_path. 1 . blinding_point ) ,
177
182
keysend_preimage : None ,
@@ -180,7 +185,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
180
185
let max_custom_tlv_len = 1300
181
186
- crate :: util:: ser:: BigSize ( CUSTOM_TLV_TYPE ) . serialized_length ( ) // custom TLV type
182
187
- 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
184
189
- PAYLOAD_HMAC_LEN
185
190
- final_payload_len_without_custom_tlv;
186
191
@@ -251,8 +256,9 @@ fn blinded_path_with_custom_tlv() {
251
256
const CUSTOM_TLV_TYPE : u64 = 65537 ;
252
257
let mut route = get_route ( & nodes[ 1 ] , & route_params) . unwrap ( ) ;
253
258
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
256
262
)
257
263
. unwrap ( )
258
264
. 0
@@ -289,7 +295,9 @@ fn blinded_path_with_custom_tlv() {
289
295
290
296
// Confirm that we can't construct an onion packet given this too-large custom TLV.
291
297
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 ( ) ;
293
301
match err {
294
302
APIError :: InvalidRoute { err } => {
295
303
assert_eq ! ( err, "Route size too large considering onion data" ) ;
0 commit comments