Skip to content

Commit 8e39b04

Browse files
committed
Rewrite test_update_fee_that_funder_cannot_afford to avoid magic
Instead of magic hard-coded constants, its better for tests to derive the values used so that they change if constants are changed and so that it is easier to re-derive constants in the future as needed.
1 parent e06b59c commit 8e39b04

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,14 @@ fn test_update_fee_that_funder_cannot_afford() {
584584
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
585585
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
586586
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
587-
let channel_value = 1888;
588-
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 700000, InitFeatures::known(), InitFeatures::known());
587+
let channel_value = 5000;
588+
let push_sats = 700;
589+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, push_sats * 1000, InitFeatures::known(), InitFeatures::known());
589590
let channel_id = chan.2;
591+
let bs_channel_reserve_sats = Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(channel_value);
590592

591-
let feerate = 260;
593+
// Calculate the maximum feerate that A can afford:
594+
let feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / COMMITMENT_TX_BASE_WEIGHT) as u32;
592595
{
593596
let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
594597
*feerate_lock = feerate;
@@ -601,24 +604,22 @@ fn test_update_fee_that_funder_cannot_afford() {
601604

602605
commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
603606

604-
//Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
605-
//This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
607+
// Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate set above.
606608
{
607609
let commitment_tx = get_local_commitment_txn!(nodes[1], channel_id)[0].clone();
608610

609-
//We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
610-
let num_htlcs = commitment_tx.output.len() - 2;
611-
let total_fee: u64 = feerate as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
611+
//We made sure neither party's funds are below the dust limit and there are no HTLCs here
612+
assert_eq!(commitment_tx.output.len(), 2);
613+
let total_fee: u64 = feerate as u64 * COMMITMENT_TX_BASE_WEIGHT / 1000;
612614
let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
613615
actual_fee = channel_value - actual_fee;
614616
assert_eq!(total_fee, actual_fee);
615617
}
616618

617-
//Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
618-
//fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
619619
{
620+
// Increment the feerate by a small constant, accouting for rounding errors
620621
let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
621-
*feerate_lock = feerate + 2;
622+
*feerate_lock += 4;
622623
}
623624
nodes[0].node.timer_tick_occurred();
624625
check_added_monitors!(nodes[0], 1);

0 commit comments

Comments
 (0)