Skip to content

Commit 30f44af

Browse files
test: add test for self payment
1 parent d5f4fba commit 30f44af

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lightning/src/ln/outbound_payment.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

1616
use crate::sign::{EntropySource, NodeSigner, Recipient};
1717
use crate::events::{self, PaymentFailureReason, Event, PaymentPurpose};
18-
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
18+
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret, features::{ NodeFeatures, ChannelFeatures}};
1919
use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId};
2020
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2121
use crate::offers::invoice::Bolt12Invoice;
22-
use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
22+
use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router, RouteHop};
2323
use crate::util::errors::APIError;
2424
use crate::util::logger::Logger;
2525
use crate::util::time::Time;
@@ -921,6 +921,29 @@ impl OutboundPayments {
921921
payment_preimage: Some(payment_preimage),
922922
payment_secret,
923923
};
924+
let dummy_route = Route {
925+
paths: vec![Path {
926+
hops: vec![RouteHop {
927+
pubkey: payer,
928+
node_features: NodeFeatures::empty(),
929+
short_channel_id: 0,
930+
channel_features: ChannelFeatures::empty(),
931+
fee_msat: 0,
932+
cltv_expiry_delta: 0,
933+
maybe_announced_channel: false,
934+
}],
935+
blinded_tail: None,
936+
}],
937+
route_params: Some(route_params.clone()),
938+
};
939+
let _ = self.add_new_pending_payment(payment_hash,
940+
recipient_onion.clone(), payment_id, keysend_preimage, &dummy_route, Some(retry_strategy),
941+
Some(route_params.payment_params.clone()), entropy_source, best_block_height)
942+
.map_err(|_| {
943+
log_error!(logger, "Payment with id {} is already pending. New payment had payment hash {}",
944+
payment_id, payment_hash);
945+
RetryableSendFailure::DuplicatePayment
946+
})?;
924947
let mut pending_outbounds_lock = self.pending_outbound_payments.lock().unwrap();
925948
let payment = pending_outbounds_lock.get_mut(&payment_id).unwrap();
926949
payment.mark_fulfilled();

lightning/src/ln/payment_tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,30 @@ fn preflight_probes_yield_event_and_skip() {
15291529
assert!(!nodes[0].node.has_pending_payments());
15301530
}
15311531

1532+
#[test]
1533+
fn test_self_payment() {
1534+
let chanmon_cfg = create_chanmon_cfgs(1);
1535+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
1536+
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None, None]);
1537+
let nodes = create_network(1, &node_cfg, &node_chanmgr);
1538+
let (payment_hash, payment_secret) = nodes[0].node.create_inbound_payment(Some(1000), 60, None).unwrap();
1539+
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), TEST_FINAL_CLTV);
1540+
let route_params = RouteParameters {
1541+
payment_params,
1542+
final_value_msat: 100000,
1543+
max_total_routing_fee_msat: None,
1544+
};
1545+
let res = nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(0));
1546+
assert!(res.is_ok());
1547+
let events = nodes[0].node.get_and_clear_pending_events();
1548+
assert_eq!(events.len(), 2);
1549+
matches!(events[0], Event::PaymentSent { .. });
1550+
matches!(events[1], Event::PaymentClaimable { .. });
1551+
let pending_payments = nodes[0].node.list_recent_payments();
1552+
assert_eq!(pending_payments.len(), 1);
1553+
matches!(pending_payments[0], RecentPaymentDetails::Fulfilled { .. });
1554+
}
1555+
15321556
#[test]
15331557
fn claimed_send_payment_idempotent() {
15341558
// Tests that `send_payment` (and friends) are (reasonably) idempotent.

0 commit comments

Comments
 (0)