Skip to content

Commit 02775fa

Browse files
committed
Test fee rate bumping
Create some tests for various `feerate_bump` scenarios and ensure among other thigns that there are no underflows.
1 parent f48b5d3 commit 02775fa

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

lightning/src/chain/package.rs

+84-1
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ where
13411341

13421342
#[cfg(test)]
13431343
mod tests {
1344-
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedHTLCOutput, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc};
1344+
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedHTLCOutput, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc, feerate_bump};
13451345
use crate::chain::Txid;
13461346
use crate::ln::chan_utils::HTLCOutputInCommitment;
13471347
use crate::types::payment::{PaymentPreimage, PaymentHash};
@@ -1359,7 +1359,10 @@ mod tests {
13591359

13601360
use bitcoin::secp256k1::{PublicKey,SecretKey};
13611361
use bitcoin::secp256k1::Secp256k1;
1362+
use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, FEERATE_FLOOR_SATS_PER_KW, LowerBoundedFeeEstimator};
1363+
use crate::chain::onchaintx::FeerateStrategy;
13621364
use crate::types::features::ChannelTypeFeatures;
1365+
use crate::util::test_utils::TestLogger;
13631366

13641367
fn fake_txid(n: u64) -> Txid {
13651368
Transaction {
@@ -1669,4 +1672,84 @@ mod tests {
16691672
}
16701673
}
16711674
}
1675+
1676+
struct TestFeeEstimator {
1677+
sat_per_kw: u32,
1678+
}
1679+
1680+
impl FeeEstimator for TestFeeEstimator {
1681+
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 {
1682+
self.sat_per_kw
1683+
}
1684+
}
1685+
1686+
#[test]
1687+
fn test_feerate_bump() {
1688+
let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW;
1689+
let test_fee_estimator = &TestFeeEstimator { sat_per_kw };
1690+
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator);
1691+
let fee_rate_strategy = FeerateStrategy::ForceBump;
1692+
let confirmation_target = ConfirmationTarget::UrgentOnChainSweep;
1693+
1694+
{
1695+
// Check underflow doesn't occur
1696+
let predicted_weight_units = 1000;
1697+
let input_satoshis = 505;
1698+
1699+
let logger = TestLogger::new();
1700+
let bumped_fee_rate = feerate_bump(predicted_weight_units, input_satoshis, 546, 253, &fee_rate_strategy, confirmation_target, &fee_estimator, &logger);
1701+
assert!(bumped_fee_rate.is_none());
1702+
logger.assert_log_regex("lightning::chain::package", regex::Regex::new(r"Can't bump new claiming tx, input amount 505 is too small").unwrap(), 1);
1703+
}
1704+
1705+
{
1706+
// Check post-25%-bump-underflow scenario satisfying the following constraints:
1707+
// input - fee = 546
1708+
// input - fee * 1.25 = -1
1709+
1710+
// We accomplish that scenario with the following values:
1711+
// input = 2734
1712+
// fee = 2188
1713+
1714+
let predicted_weight_units = 1000;
1715+
let input_satoshis = 2734;
1716+
1717+
let logger = TestLogger::new();
1718+
let bumped_fee_rate = feerate_bump(predicted_weight_units, input_satoshis, 546, 2188, &fee_rate_strategy, confirmation_target, &fee_estimator, &logger);
1719+
assert!(bumped_fee_rate.is_none());
1720+
logger.assert_log_regex("lightning::chain::package", regex::Regex::new(r"Can't bump new claiming tx, output amount 0 would end up below dust threshold 546").unwrap(), 1);
1721+
}
1722+
1723+
{
1724+
// Check that an output amount of 0 is caught
1725+
let predicted_weight_units = 1000;
1726+
let input_satoshis = 506;
1727+
1728+
let logger = TestLogger::new();
1729+
let bumped_fee_rate = feerate_bump(predicted_weight_units, input_satoshis, 546, 253, &fee_rate_strategy, confirmation_target, &fee_estimator, &logger);
1730+
assert!(bumped_fee_rate.is_none());
1731+
logger.assert_log_regex("lightning::chain::package", regex::Regex::new(r"Can't bump new claiming tx, output amount 0 would end up below dust threshold 546").unwrap(), 1);
1732+
}
1733+
1734+
{
1735+
// Check that dust_threshold - 1 is blocked
1736+
let predicted_weight_units = 1000;
1737+
let input_satoshis = 1051;
1738+
1739+
let logger = TestLogger::new();
1740+
let bumped_fee_rate = feerate_bump(predicted_weight_units, input_satoshis, 546, 253, &fee_rate_strategy, confirmation_target, &fee_estimator, &logger);
1741+
assert!(bumped_fee_rate.is_none());
1742+
logger.assert_log_regex("lightning::chain::package", regex::Regex::new(r"Can't bump new claiming tx, output amount 545 would end up below dust threshold 546").unwrap(), 1);
1743+
}
1744+
1745+
{
1746+
let predicted_weight_units = 1000;
1747+
let input_satoshis = 1052;
1748+
1749+
let logger = TestLogger::new();
1750+
let bumped_fee_rate = feerate_bump(predicted_weight_units, input_satoshis, 546, 253, &fee_rate_strategy, confirmation_target, &fee_estimator, &logger).unwrap();
1751+
assert_eq!(bumped_fee_rate, (506, 506));
1752+
logger.assert_log_regex("lightning::chain::package", regex::Regex::new(r"Naive fee bump of 63s does not meet min relay fee requirements of 253s").unwrap(), 1);
1753+
}
1754+
}
16721755
}

0 commit comments

Comments
 (0)