|
| 1 | +use crate::ln::channelmanager::{ |
| 2 | + HTLCForwardInfo, PaymentId, PendingAddHTLCInfo, PendingHTLCInfo, RecipientOnionFields, Retry, |
| 3 | +}; |
| 4 | +use crate::ln::functional_test_utils::*; |
| 5 | +use crate::ln::msgs::{accountable_from_bool, ChannelMessageHandler, ExperimentalAccountable}; |
| 6 | +use crate::routing::router::{PaymentParameters, RouteParameters}; |
| 7 | + |
| 8 | +fn test_accountable_forwarding_with_override( |
| 9 | + override_accountable: ExperimentalAccountable, expected_forwarded: bool, |
| 10 | +) { |
| 11 | + let chanmon_cfgs = create_chanmon_cfgs(3); |
| 12 | + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); |
| 13 | + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); |
| 14 | + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); |
| 15 | + |
| 16 | + let _chan_ab = create_announced_chan_between_nodes(&nodes, 0, 1); |
| 17 | + let _chan_bc = create_announced_chan_between_nodes(&nodes, 1, 2); |
| 18 | + |
| 19 | + let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]); |
| 20 | + let route_params = RouteParameters::from_payment_params_and_value( |
| 21 | + PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV), |
| 22 | + 100_000, |
| 23 | + ); |
| 24 | + let onion_fields = RecipientOnionFields::secret_only(payment_secret); |
| 25 | + let payment_id = PaymentId(payment_hash.0); |
| 26 | + nodes[0] |
| 27 | + .node |
| 28 | + .send_payment(payment_hash, onion_fields, payment_id, route_params, Retry::Attempts(0)) |
| 29 | + .unwrap(); |
| 30 | + check_added_monitors(&nodes[0], 1); |
| 31 | + |
| 32 | + let updates_ab = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id()); |
| 33 | + assert_eq!(updates_ab.update_add_htlcs.len(), 1); |
| 34 | + let mut htlc_ab = updates_ab.update_add_htlcs[0].clone(); |
| 35 | + assert_eq!(htlc_ab.accountable, accountable_from_bool(false)); |
| 36 | + |
| 37 | + // Override accountable value if requested |
| 38 | + if let Some(override_value) = override_accountable { |
| 39 | + htlc_ab.accountable = Some(override_value); |
| 40 | + } |
| 41 | + |
| 42 | + nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &htlc_ab); |
| 43 | + do_commitment_signed_dance(&nodes[1], &nodes[0], &updates_ab.commitment_signed, false, false); |
| 44 | + expect_and_process_pending_htlcs(&nodes[1], false); |
| 45 | + check_added_monitors(&nodes[1], 1); |
| 46 | + |
| 47 | + let updates_bc = get_htlc_update_msgs(&nodes[1], &nodes[2].node.get_our_node_id()); |
| 48 | + assert_eq!(updates_bc.update_add_htlcs.len(), 1); |
| 49 | + let htlc_bc = &updates_bc.update_add_htlcs[0]; |
| 50 | + let expected_acountable_signal = accountable_from_bool(expected_forwarded); |
| 51 | + assert_eq!( |
| 52 | + htlc_bc.accountable, expected_acountable_signal, |
| 53 | + "B -> C should have accountable = {:?}", |
| 54 | + expected_acountable_signal |
| 55 | + ); |
| 56 | + |
| 57 | + nodes[2].node.handle_update_add_htlc(nodes[1].node.get_our_node_id(), htlc_bc); |
| 58 | + do_commitment_signed_dance(&nodes[2], &nodes[1], &updates_bc.commitment_signed, false, false); |
| 59 | + |
| 60 | + // Accountable signal is not surfaced in PaymentClaimable, so we do our next-best and check |
| 61 | + // that the received htlcs that will be processed has the signal set as we expect. We manually |
| 62 | + // process pending update adds so that we can access the htlc in forward_htlcs. |
| 63 | + nodes[2].node.test_process_pending_update_add_htlcs(); |
| 64 | + { |
| 65 | + let fwds_lock = nodes[2].node.forward_htlcs.lock().unwrap(); |
| 66 | + let recvs = fwds_lock.get(&0).unwrap(); |
| 67 | + assert_eq!(recvs.len(), 1); |
| 68 | + match recvs[0] { |
| 69 | + HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { |
| 70 | + forward_info: PendingHTLCInfo { incoming_accountable, .. }, |
| 71 | + .. |
| 72 | + }) => { |
| 73 | + assert_eq!(incoming_accountable, expected_forwarded) |
| 74 | + }, |
| 75 | + _ => panic!("Unexpected forward"), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + expect_and_process_pending_htlcs(&nodes[2], false); |
| 80 | + check_added_monitors(&nodes[2], 0); |
| 81 | + expect_payment_claimable!(nodes[2], payment_hash, payment_secret, 100_000); |
| 82 | + claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn test_accountable_signal() { |
| 87 | + // Tests forwarding of accountable signal for various incoming signal values. |
| 88 | + test_accountable_forwarding_with_override(None, false); |
| 89 | + test_accountable_forwarding_with_override(Some(7), true); |
| 90 | + test_accountable_forwarding_with_override(Some(3), false); |
| 91 | +} |
0 commit comments