Skip to content

Commit 254d565

Browse files
committed
forbide payself with trampoline routing
1 parent 86acc50 commit 254d565

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

crates/fiber-lib/src/fiber/payment.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,11 @@ impl SendPaymentDataBuilder {
394394
if hops.iter().collect::<HashSet<_>>().len() != hops.len() {
395395
return Err("trampoline_hops must not contain duplicates".to_string());
396396
}
397+
if self.allow_self_payment {
398+
return Err(
399+
"allow_self_payment cannot be enabled with trampoline routing".to_string(),
400+
);
401+
}
397402
}
398403

399404
if self.udt_type_script.is_none() && amount >= u64::MAX as u128 {

crates/fiber-lib/src/fiber/tests/trampoline.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,3 +3779,43 @@ async fn test_trampoline_mpp_with_oneway() {
37793779
run_with_hash_algo(&node1, &node2, &node4, HashAlgorithm::CkbHash).await;
37803780
run_with_hash_algo(&node1, &node2, &node4, HashAlgorithm::Sha256).await;
37813781
}
3782+
3783+
#[tokio::test]
3784+
async fn test_trampoline_routing_with_self_payment_should_fail() {
3785+
init_tracing();
3786+
3787+
// Create a simple network with 2 nodes
3788+
let (nodes, _channels) = create_n_nodes_network_with_visibility(
3789+
&[((0, 1), (MIN_RESERVED_CKB + 100000, HUGE_CKB_AMOUNT), true)],
3790+
2,
3791+
)
3792+
.await;
3793+
3794+
let [node_a, node_b] = nodes.try_into().expect("2 nodes");
3795+
3796+
// Wait until A learns B supports trampoline routing.
3797+
wait_until_node_supports_trampoline_routing(&node_a, &node_b).await;
3798+
3799+
let amount: u128 = 1000;
3800+
3801+
// Try to send payment to self with trampoline routing enabled
3802+
// This should fail with validation error
3803+
let res = node_a
3804+
.send_payment(SendPaymentCommand {
3805+
target_pubkey: Some(node_a.get_public_key()),
3806+
amount: Some(amount),
3807+
allow_self_payment: true,
3808+
trampoline_hops: Some(vec![node_b.get_public_key()]),
3809+
max_fee_amount: Some(500),
3810+
keysend: Some(true),
3811+
..Default::default()
3812+
})
3813+
.await;
3814+
3815+
let err_msg = res.err().unwrap();
3816+
assert!(
3817+
err_msg.contains("allow_self_payment cannot be enabled with trampoline routing"),
3818+
"Expected error, got: {}",
3819+
err_msg
3820+
);
3821+
}

0 commit comments

Comments
 (0)