Skip to content

Commit f12c191

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 f12c191

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

lightning/src/ln/htlc_reserve_unit_tests.rs

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ln::chan_utils::{
77
};
88
use crate::ln::channel::{
99
get_holder_selected_channel_reserve_satoshis, Channel, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE,
10-
MIN_AFFORDABLE_HTLC_COUNT,
10+
MIN_AFFORDABLE_HTLC_COUNT, MIN_CHAN_DUST_LIMIT_SATOSHIS,
1111
};
1212
use crate::ln::channelmanager::{PaymentId, RAACommitmentOrder};
1313
use crate::ln::functional_test_utils::*;
@@ -16,6 +16,7 @@ use crate::ln::onion_utils::{self, AttributionData};
1616
use crate::ln::outbound_payment::RecipientOnionFields;
1717
use crate::routing::router::PaymentParameters;
1818
use crate::sign::ecdsa::EcdsaChannelSigner;
19+
use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
1920
use crate::types::features::ChannelTypeFeatures;
2021
use crate::types::payment::PaymentPreimage;
2122
use crate::util::config::UserConfig;
@@ -772,16 +773,22 @@ pub fn test_basic_channel_reserve() {
772773
}
773774

774775
#[xtest(feature = "_externalize_tests")]
775-
pub fn test_fee_spike_violation_fails_htlc() {
776+
fn test_fee_spike_violation_fails_htlc() {
777+
do_test_fee_spike_buffer(None, true)
778+
}
779+
pub fn do_test_fee_spike_buffer(cfg: Option<UserConfig>, htlc_fails: bool) {
776780
let chanmon_cfgs = create_chanmon_cfgs(2);
777781
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
778-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
782+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[cfg.clone(), cfg]);
779783
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
780784

781785
let node_a_id = nodes[0].node.get_our_node_id();
782786
let node_b_id = nodes[1].node.get_our_node_id();
783787

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

786793
let (mut route, payment_hash, _, payment_secret) =
787794
get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
@@ -792,11 +799,12 @@ pub fn test_fee_spike_violation_fails_htlc() {
792799

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

802+
let payment_amt_msat = 3460001;
795803
let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv);
796804
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
797805
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
798806
&route.paths[0],
799-
3460001,
807+
payment_amt_msat,
800808
&recipient_onion_fields,
801809
cur_height,
802810
&None,
@@ -858,16 +866,15 @@ pub fn test_fee_spike_violation_fails_htlc() {
858866

859867
// Build the remote commitment transaction so we can sign it, and then later use the
860868
// signature for the commitment_signed message.
861-
let local_chan_balance = 1313;
862-
863869
let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
864870
offered: false,
865-
amount_msat: 3460001,
871+
amount_msat: payment_amt_msat,
866872
cltv_expiry: htlc_cltv,
867873
payment_hash,
868874
transaction_output_index: Some(1),
869875
};
870876

877+
let local_chan_balance_msat = chan_amt_sat * 1000 - push_amt_msat;
871878
let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
872879

873880
let res = {
@@ -877,15 +884,17 @@ pub fn test_fee_spike_violation_fails_htlc() {
877884
let channel = get_channel_ref!(nodes[0], nodes[1], per_peer_lock, peer_state_lock, chan.2);
878885
let chan_signer = channel.as_funded().unwrap().get_signer();
879886

880-
let commitment_tx = CommitmentTransaction::new(
887+
let (commitment_tx, _stats) = SpecTxBuilder {}.build_commitment_transaction(
888+
false,
881889
commitment_number,
882890
&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(),
891+
&channel.funding().channel_transaction_parameters,
888892
&secp_ctx,
893+
local_chan_balance_msat,
894+
vec![accepted_htlc_info],
895+
feerate_per_kw,
896+
MIN_CHAN_DUST_LIMIT_SATOSHIS,
897+
&nodes[0].logger,
889898
);
890899
let params = &channel.funding().channel_transaction_parameters;
891900
chan_signer
@@ -918,28 +927,35 @@ pub fn test_fee_spike_violation_fails_htlc() {
918927
};
919928
nodes[1].node.handle_revoke_and_ack(node_a_id, &raa_msg);
920929
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-
);
925930

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

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

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

0 commit comments

Comments
 (0)