Skip to content

Commit c02b463

Browse files
committed
ln: add test helper for fee spike buffer test and use TxBuilder
Update test helper in preparation to test zero fee commitments, where the local balance will differ from the previously hardcoded value.
1 parent ea0341f commit c02b463

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

lightning/src/ln/htlc_reserve_unit_tests.rs

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
33
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType, PaymentPurpose};
44
use crate::ln::chan_utils::{
5-
self, commitment_tx_base_weight, second_stage_tx_fees_sat, CommitmentTransaction,
6-
COMMITMENT_TX_WEIGHT_PER_HTLC,
5+
self, commitment_tx_base_weight, second_stage_tx_fees_sat, COMMITMENT_TX_WEIGHT_PER_HTLC,
76
};
87
use crate::ln::channel::{
98
get_holder_selected_channel_reserve_satoshis, Channel, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE,
10-
MIN_AFFORDABLE_HTLC_COUNT,
9+
MIN_AFFORDABLE_HTLC_COUNT, MIN_CHAN_DUST_LIMIT_SATOSHIS,
1110
};
1211
use crate::ln::channelmanager::{PaymentId, RAACommitmentOrder};
1312
use crate::ln::functional_test_utils::*;
@@ -16,6 +15,7 @@ use crate::ln::onion_utils::{self, AttributionData};
1615
use crate::ln::outbound_payment::RecipientOnionFields;
1716
use crate::routing::router::PaymentParameters;
1817
use crate::sign::ecdsa::EcdsaChannelSigner;
18+
use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
1919
use crate::types::features::ChannelTypeFeatures;
2020
use crate::types::payment::PaymentPreimage;
2121
use crate::util::config::UserConfig;
@@ -772,16 +772,22 @@ pub fn test_basic_channel_reserve() {
772772
}
773773

774774
#[xtest(feature = "_externalize_tests")]
775-
pub fn test_fee_spike_violation_fails_htlc() {
775+
fn test_fee_spike_violation_fails_htlc() {
776+
do_test_fee_spike_buffer(None, true)
777+
}
778+
pub fn do_test_fee_spike_buffer(cfg: Option<UserConfig>, htlc_fails: bool) {
776779
let chanmon_cfgs = create_chanmon_cfgs(2);
777780
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
778-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
781+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[cfg.clone(), cfg]);
779782
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
780783

781784
let node_a_id = nodes[0].node.get_our_node_id();
782785
let node_b_id = nodes[1].node.get_our_node_id();
783786

784-
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
787+
let chan_amt_sat = 100000;
788+
let push_amt_msat = 95000000;
789+
let chan =
790+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, chan_amt_sat, push_amt_msat);
785791

786792
let (mut route, payment_hash, _, payment_secret) =
787793
get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
@@ -792,11 +798,12 @@ pub fn test_fee_spike_violation_fails_htlc() {
792798

793799
let cur_height = nodes[1].node.best_block.read().unwrap().height + 1;
794800

801+
let payment_amt_msat = 3460001;
795802
let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv);
796803
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
797804
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
798805
&route.paths[0],
799-
3460001,
806+
payment_amt_msat,
800807
&recipient_onion_fields,
801808
cur_height,
802809
&None,
@@ -858,16 +865,15 @@ pub fn test_fee_spike_violation_fails_htlc() {
858865

859866
// Build the remote commitment transaction so we can sign it, and then later use the
860867
// signature for the commitment_signed message.
861-
let local_chan_balance = 1313;
862-
863868
let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
864869
offered: false,
865-
amount_msat: 3460001,
870+
amount_msat: payment_amt_msat,
866871
cltv_expiry: htlc_cltv,
867872
payment_hash,
868873
transaction_output_index: Some(1),
869874
};
870875

876+
let local_chan_balance_msat = chan_amt_sat * 1000 - push_amt_msat;
871877
let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
872878

873879
let res = {
@@ -877,15 +883,17 @@ pub fn test_fee_spike_violation_fails_htlc() {
877883
let channel = get_channel_ref!(nodes[0], nodes[1], per_peer_lock, peer_state_lock, chan.2);
878884
let chan_signer = channel.as_funded().unwrap().get_signer();
879885

880-
let commitment_tx = CommitmentTransaction::new(
886+
let (commitment_tx, _stats) = SpecTxBuilder {}.build_commitment_transaction(
887+
false,
881888
commitment_number,
882889
&remote_point,
883-
95000,
884-
local_chan_balance,
885-
feerate_per_kw,
886-
vec![accepted_htlc_info],
887-
&channel.funding().channel_transaction_parameters.as_counterparty_broadcastable(),
890+
&channel.funding().channel_transaction_parameters,
888891
&secp_ctx,
892+
local_chan_balance_msat,
893+
vec![accepted_htlc_info],
894+
feerate_per_kw,
895+
MIN_CHAN_DUST_LIMIT_SATOSHIS,
896+
&nodes[0].logger,
889897
);
890898
let params = &channel.funding().channel_transaction_parameters;
891899
chan_signer
@@ -918,28 +926,35 @@ pub fn test_fee_spike_violation_fails_htlc() {
918926
};
919927
nodes[1].node.handle_revoke_and_ack(node_a_id, &raa_msg);
920928
expect_pending_htlcs_forwardable!(nodes[1]);
921-
expect_htlc_handling_failed_destinations!(
922-
nodes[1].node.get_and_clear_pending_events(),
923-
&[HTLCHandlingFailureType::Receive { payment_hash }]
924-
);
925929

926-
let events = nodes[1].node.get_and_clear_pending_msg_events();
927-
assert_eq!(events.len(), 1);
928-
// Make sure the HTLC failed in the way we expect.
929-
match events[0] {
930-
MessageSendEvent::UpdateHTLCs {
931-
updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. },
932-
..
933-
} => {
934-
assert_eq!(update_fail_htlcs.len(), 1);
935-
update_fail_htlcs[0].clone()
936-
},
937-
_ => panic!("Unexpected event"),
938-
};
939-
nodes[1].logger.assert_log("lightning::ln::channel",
930+
if htlc_fails {
931+
expect_htlc_handling_failed_destinations!(
932+
nodes[1].node.get_and_clear_pending_events(),
933+
&[HTLCHandlingFailureType::Receive { payment_hash }]
934+
);
935+
936+
let events = nodes[1].node.get_and_clear_pending_msg_events();
937+
assert_eq!(events.len(), 1);
938+
939+
// Make sure the HTLC failed in the way we expect.
940+
match events[0] {
941+
MessageSendEvent::UpdateHTLCs {
942+
updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. },
943+
..
944+
} => {
945+
assert_eq!(update_fail_htlcs.len(), 1);
946+
update_fail_htlcs[0].clone()
947+
},
948+
_ => panic!("Unexpected event"),
949+
};
950+
nodes[1].logger.assert_log("lightning::ln::channel",
940951
format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", raa_msg.channel_id), 1);
941952

942-
check_added_monitors(&nodes[1], 3);
953+
check_added_monitors(&nodes[1], 3);
954+
} else {
955+
expect_payment_claimable!(nodes[1], payment_hash, payment_secret, payment_amt_msat);
956+
check_added_monitors(&nodes[1], 2);
957+
}
943958
}
944959

945960
#[xtest(feature = "_externalize_tests")]

0 commit comments

Comments
 (0)