Skip to content

Commit 97a4337

Browse files
f test-only method
1 parent c5bd26f commit 97a4337

File tree

2 files changed

+84
-80
lines changed

2 files changed

+84
-80
lines changed

lightning/src/ln/async_payments_tests.rs

+73-77
Original file line numberDiff line numberDiff line change
@@ -637,30 +637,29 @@ fn amount_doesnt_match_invreq() {
637637

638638
// Replace the invoice request contained within outbound_payments before sending so the invreq
639639
// amount doesn't match the onion amount when the HTLC gets to the recipient.
640-
let mut outbounds =
641-
nodes[0].node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
642-
let valid_invreq;
643-
match outbounds.get_mut(&payment_id) {
644-
Some(PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. }) => {
645-
valid_invreq = invoice_request.clone();
646-
*invoice_request = offer
647-
.request_invoice(
648-
&nodes[0].keys_manager.get_inbound_payment_key(),
649-
Nonce::from_entropy_source(nodes[0].keys_manager),
650-
&secp_ctx,
651-
payment_id,
652-
)
653-
.unwrap()
654-
.amount_msats(amt_msat + 1)
655-
.unwrap()
656-
.chain_hash(ChainHash::using_genesis_block(Network::Testnet))
657-
.unwrap()
658-
.build_and_sign()
659-
.unwrap();
660-
},
661-
_ => panic!(),
662-
}
663-
core::mem::drop(outbounds);
640+
let mut valid_invreq = None;
641+
nodes[0].node.test_modify_pending_payment(&payment_id, |pmt| {
642+
match pmt {
643+
PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. } => {
644+
valid_invreq = Some(invoice_request.clone());
645+
*invoice_request = offer
646+
.request_invoice(
647+
&nodes[0].keys_manager.get_inbound_payment_key(),
648+
Nonce::from_entropy_source(nodes[0].keys_manager),
649+
&secp_ctx,
650+
payment_id,
651+
)
652+
.unwrap()
653+
.amount_msats(amt_msat + 1)
654+
.unwrap()
655+
.chain_hash(ChainHash::using_genesis_block(Network::Testnet))
656+
.unwrap()
657+
.build_and_sign()
658+
.unwrap();
659+
},
660+
_ => panic!()
661+
}
662+
});
664663

665664
nodes[0]
666665
.onion_messenger
@@ -683,15 +682,14 @@ fn amount_doesnt_match_invreq() {
683682

684683
// Modify the invoice request stored in our outbounds to be the correct one, to make sure the
685684
// payment retry will succeed after we finish failing the invalid HTLC back.
686-
let mut outbounds =
687-
nodes[0].node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
688-
match outbounds.get_mut(&payment_id) {
689-
Some(PendingOutboundPayment::Retryable { invoice_request, .. }) => {
690-
*invoice_request = Some(valid_invreq);
691-
},
692-
_ => panic!(),
693-
}
694-
core::mem::drop(outbounds);
685+
nodes[0].node.test_modify_pending_payment(&payment_id, |pmt| {
686+
match pmt {
687+
PendingOutboundPayment::Retryable { invoice_request, .. } => {
688+
*invoice_request = valid_invreq.take();
689+
},
690+
_ => panic!(),
691+
}
692+
});
695693

696694
fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1], &nodes[3]], true);
697695

@@ -720,27 +718,26 @@ fn reject_missing_invreq() {
720718
|sender, _, payment_id| {
721719
// Remove the invoice request from our Retryable payment so we don't include it in the onion on
722720
// retry.
723-
let mut outbounds =
724-
sender.node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
725-
let valid_invreq_from_outbounds = match outbounds.get_mut(&payment_id) {
726-
Some(PendingOutboundPayment::Retryable { invoice_request, .. }) => {
727-
assert!(invoice_request.is_some());
728-
invoice_request.take()
729-
},
730-
_ => panic!(),
731-
};
732-
*valid_invreq.lock().unwrap() = valid_invreq_from_outbounds;
721+
sender.node.test_modify_pending_payment(&payment_id, |pmt| {
722+
match pmt {
723+
PendingOutboundPayment::Retryable { invoice_request, .. } => {
724+
assert!(invoice_request.is_some());
725+
*valid_invreq.lock().unwrap() = invoice_request.take();
726+
},
727+
_ => panic!(),
728+
}
729+
});
733730
},
734731
|sender, payment_id| {
735732
// Re-add the invoice request so we include it in the onion on the next retry.
736-
let mut outbounds =
737-
sender.node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
738-
match outbounds.get_mut(&payment_id) {
739-
Some(PendingOutboundPayment::Retryable { invoice_request, .. }) => {
740-
*invoice_request = valid_invreq.lock().unwrap().take();
741-
},
742-
_ => panic!(),
743-
}
733+
sender.node.test_modify_pending_payment(&payment_id, |pmt| {
734+
match pmt {
735+
PendingOutboundPayment::Retryable { invoice_request, .. } => {
736+
*invoice_request = valid_invreq.lock().unwrap().take();
737+
},
738+
_ => panic!(),
739+
}
740+
});
744741
},
745742
);
746743
}
@@ -773,35 +770,34 @@ fn reject_bad_payment_secret() {
773770
}
774771

775772
// Modify the outbound payment parameters to use payment paths with an invalid payment secret.
776-
let mut outbounds =
777-
sender.node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
778-
let valid_params_from_outbounds = match outbounds.get_mut(&payment_id) {
779-
Some(PendingOutboundPayment::Retryable { ref mut payment_params, .. }) => {
780-
assert!(payment_params.is_some());
781-
let valid_params = payment_params.clone();
782-
if let Payee::Blinded { ref mut route_hints, .. } =
783-
&mut payment_params.as_mut().unwrap().payee
784-
{
785-
core::mem::swap(route_hints, &mut invalid_blinded_payment_paths);
786-
} else {
787-
panic!()
788-
}
789-
valid_params
790-
},
791-
_ => panic!(),
792-
};
793-
*valid_payment_params.lock().unwrap() = valid_params_from_outbounds;
773+
sender.node.test_modify_pending_payment(&payment_id, |pmt| {
774+
match pmt {
775+
PendingOutboundPayment::Retryable { ref mut payment_params, .. } => {
776+
assert!(payment_params.is_some());
777+
let valid_params = payment_params.clone();
778+
if let Payee::Blinded { ref mut route_hints, .. } =
779+
&mut payment_params.as_mut().unwrap().payee
780+
{
781+
core::mem::swap(route_hints, &mut invalid_blinded_payment_paths);
782+
} else {
783+
panic!()
784+
}
785+
*valid_payment_params.lock().unwrap() = valid_params;
786+
},
787+
_ => panic!(),
788+
}
789+
});
794790
},
795791
|sender, payment_id| {
796792
// Re-add the valid payment params so we use the right payment secret on the next retry.
797-
let mut outbounds =
798-
sender.node.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
799-
match outbounds.get_mut(&payment_id) {
800-
Some(PendingOutboundPayment::Retryable { payment_params, .. }) => {
801-
*payment_params = valid_payment_params.lock().unwrap().take();
802-
},
803-
_ => panic!(),
804-
}
793+
sender.node.test_modify_pending_payment(&payment_id, |pmt| {
794+
match pmt {
795+
PendingOutboundPayment::Retryable { payment_params, .. } => {
796+
*payment_params = valid_payment_params.lock().unwrap().take();
797+
},
798+
_ => panic!(),
799+
}
800+
});
805801
},
806802
);
807803
}

lightning/src/ln/channelmanager.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2441,9 +2441,6 @@ where
24412441
/// See `PendingOutboundPayment` documentation for more info.
24422442
///
24432443
/// See `ChannelManager` struct-level documentation for lock order requirements.
2444-
#[cfg(test)]
2445-
pub(super) pending_outbound_payments: OutboundPayments,
2446-
#[cfg(not(test))]
24472444
pending_outbound_payments: OutboundPayments,
24482445

24492446
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
@@ -4696,6 +4693,17 @@ where
46964693
self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, recipient_onion, payment_id, route, None, &self.entropy_source, best_block_height)
46974694
}
46984695

4696+
#[cfg(test)]
4697+
pub(crate) fn test_modify_pending_payment<Fn>(
4698+
&self, payment_id: &PaymentId, mut callback: Fn
4699+
) where Fn: FnMut(&mut PendingOutboundPayment) {
4700+
let mut outbounds = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
4701+
match outbounds.get_mut(payment_id) {
4702+
Some(outb) => callback(outb),
4703+
_ => panic!()
4704+
}
4705+
}
4706+
46994707
#[cfg(test)]
47004708
pub(crate) fn test_set_payment_metadata(&self, payment_id: PaymentId, new_payment_metadata: Option<Vec<u8>>) {
47014709
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);

0 commit comments

Comments
 (0)