Skip to content

Commit 07801f9

Browse files
Get rid of unnecessary result in create_inbound_payment_for_hash
1 parent 3487263 commit 07801f9

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

fuzz/src/chanmon_consistency.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,15 @@ fn check_payment_err(send_err: PaymentSendFailure) {
283283
type ChanMan = ChannelManager<EnforcingSigner, Arc<TestChainMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>;
284284

285285
#[inline]
286-
fn get_payment_secret_hash(dest: &ChanMan, payment_id: &mut u8) -> Option<(PaymentSecret, PaymentHash)> {
287-
let mut payment_hash;
288-
for _ in 0..256 {
289-
payment_hash = PaymentHash(Sha256::hash(&[*payment_id; 1]).into_inner());
290-
if let Ok(payment_secret) = dest.create_inbound_payment_for_hash(payment_hash, None, 3600) {
291-
return Some((payment_secret, payment_hash));
292-
}
293-
*payment_id = payment_id.wrapping_add(1);
294-
}
295-
None
286+
fn get_payment_secret_hash(dest: &ChanMan, payment_id: &mut u8) -> (PaymentSecret, PaymentHash) {
287+
let payment_hash = PaymentHash(Sha256::hash(&[*payment_id; 1]).into_inner());
288+
let payment_secret = dest.create_inbound_payment_for_hash(payment_hash, None, 3600);
289+
(payment_secret, payment_hash)
296290
}
297291

298292
#[inline]
299293
fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, payment_id: &mut u8) -> bool {
300-
let (payment_secret, payment_hash) =
301-
if let Some((secret, hash)) = get_payment_secret_hash(dest, payment_id) { (secret, hash) } else { return true; };
294+
let (payment_secret, payment_hash) = get_payment_secret_hash(dest, payment_id);
302295
if let Err(err) = source.send_payment(&Route {
303296
paths: vec![vec![RouteHop {
304297
pubkey: dest.get_our_node_id(),
@@ -316,8 +309,7 @@ fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, p
316309
}
317310
#[inline]
318311
fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, dest: &ChanMan, dest_chan_id: u64, amt: u64, payment_id: &mut u8) -> bool {
319-
let (payment_secret, payment_hash) =
320-
if let Some((secret, hash)) = get_payment_secret_hash(dest, payment_id) { (secret, hash) } else { return true; };
312+
let (payment_secret, payment_hash) = get_payment_secret_hash(dest, payment_id);
321313
if let Err(err) = source.send_payment(&Route {
322314
paths: vec![vec![RouteHop {
323315
pubkey: middle.get_our_node_id(),

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4701,7 +4701,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
47014701
/// [`create_inbound_payment`]: Self::create_inbound_payment
47024702
/// [`PaymentReceived`]: events::Event::PaymentReceived
47034703
// For details on the implementation of this method, see `verify_inbound_payment_data`.
4704-
pub fn create_inbound_payment_for_hash(&self, payment_hash: PaymentHash, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32) -> Result<PaymentSecret, APIError> {
4704+
pub fn create_inbound_payment_for_hash(&self, payment_hash: PaymentHash, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32) -> PaymentSecret {
47054705
let (metadata_key, user_pmt_hash_key, _) = hkdf_extract_expand(&vec![0], &self.inbound_payment_key);
47064706
let mut min_amt_msat_bytes: [u8; 8] = match min_value_msat {
47074707
Some(amt) => amt.to_be_bytes(),
@@ -4741,7 +4741,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
47414741
encrypted_metadata_slice[i] = chacha_block[i] ^ metadata_bytes[i];
47424742
}
47434743
}
4744-
Ok(PaymentSecret(payment_secret_bytes))
4744+
PaymentSecret(payment_secret_bytes)
47454745
}
47464746

47474747
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
@@ -6831,7 +6831,7 @@ pub mod bench {
68316831
payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
68326832
payment_count += 1;
68336833
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
6834-
let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200).unwrap();
6834+
let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200);
68356835

68366836
$node_a.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
68376837
let payment_event = SendEvent::from_event($node_a.get_and_clear_pending_msg_events().pop().unwrap());

lightning/src/ln/functional_test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ macro_rules! get_payment_preimage_hash {
10071007
let payment_preimage = PaymentPreimage([*payment_count; 32]);
10081008
*payment_count += 1;
10091009
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
1010-
let payment_secret = $dest_node.node.create_inbound_payment_for_hash(payment_hash, $min_value_msat, 7200).unwrap();
1010+
let payment_secret = $dest_node.node.create_inbound_payment_for_hash(payment_hash, $min_value_msat, 7200);
10111011
(payment_preimage, payment_hash, payment_secret)
10121012
}
10131013
}

lightning/src/ln/functional_tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ fn test_duplicate_htlc_different_direction_onchain() {
12071207
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
12081208

12091209
let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 800_000);
1210-
let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200).unwrap();
1210+
let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200);
12111211
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
12121212

12131213
// Provide preimage to node 0 by claiming payment
@@ -5012,7 +5012,7 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
50125012

50135013
let (our_payment_preimage, duplicate_payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000);
50145014

5015-
let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200).unwrap();
5015+
let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200);
50165016
// We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
50175017
// script push size limit so that the below script length checks match
50185018
// ACCEPTED_HTLC_SCRIPT_WEIGHT.
@@ -5215,30 +5215,30 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
52155215
let (_, payment_hash_2, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
52165216
let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
52175217
// 2nd HTLC:
5218-
send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_1, nodes[5].node.create_inbound_payment_for_hash(payment_hash_1, None, 7200).unwrap()); // not added < dust limit + HTLC tx fee
5218+
send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_1, nodes[5].node.create_inbound_payment_for_hash(payment_hash_1, None, 7200)); // not added < dust limit + HTLC tx fee
52195219
// 3rd HTLC:
5220-
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_2, nodes[5].node.create_inbound_payment_for_hash(payment_hash_2, None, 7200).unwrap()); // not added < dust limit + HTLC tx fee
5220+
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_2, nodes[5].node.create_inbound_payment_for_hash(payment_hash_2, None, 7200)); // not added < dust limit + HTLC tx fee
52215221
// 4th HTLC:
52225222
let (_, payment_hash_3, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
52235223
// 5th HTLC:
52245224
let (_, payment_hash_4, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
52255225
let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
52265226
// 6th HTLC:
5227-
send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_3, nodes[5].node.create_inbound_payment_for_hash(payment_hash_3, None, 7200).unwrap());
5227+
send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_3, nodes[5].node.create_inbound_payment_for_hash(payment_hash_3, None, 7200));
52285228
// 7th HTLC:
5229-
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_4, nodes[5].node.create_inbound_payment_for_hash(payment_hash_4, None, 7200).unwrap());
5229+
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_4, nodes[5].node.create_inbound_payment_for_hash(payment_hash_4, None, 7200));
52305230

52315231
// 8th HTLC:
52325232
let (_, payment_hash_5, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
52335233
// 9th HTLC:
52345234
let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5235-
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_5, nodes[5].node.create_inbound_payment_for_hash(payment_hash_5, None, 7200).unwrap()); // not added < dust limit + HTLC tx fee
5235+
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_5, nodes[5].node.create_inbound_payment_for_hash(payment_hash_5, None, 7200)); // not added < dust limit + HTLC tx fee
52365236

52375237
// 10th HTLC:
52385238
let (_, payment_hash_6, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
52395239
// 11th HTLC:
52405240
let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5241-
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_6, nodes[5].node.create_inbound_payment_for_hash(payment_hash_6, None, 7200).unwrap());
5241+
send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_6, nodes[5].node.create_inbound_payment_for_hash(payment_hash_6, None, 7200));
52425242

52435243
// Double-check that six of the new HTLC were added
52445244
// We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
@@ -7219,7 +7219,7 @@ fn test_check_htlc_underpaying() {
72197219
let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known());
72207220
let route = get_route(&nodes[0].node.get_our_node_id(), &payee, nodes[0].network_graph, None, 10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer).unwrap();
72217221
let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
7222-
let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200).unwrap();
7222+
let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200);
72237223
nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
72247224
check_added_monitors!(nodes[0], 1);
72257225

0 commit comments

Comments
 (0)