Skip to content

Commit d239e94

Browse files
committed
Add PredictedNextFee
1 parent 3f49662 commit d239e94

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,10 @@ pub(super) struct FundingScope {
19861986
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
19871987
#[cfg(any(test, fuzzing))]
19881988
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1989+
#[cfg(any(test, fuzzing))]
1990+
next_local_fee: Mutex<PredictedNextFee>,
1991+
#[cfg(any(test, fuzzing))]
1992+
next_remote_fee: Mutex<PredictedNextFee>,
19891993

19901994
pub(super) channel_transaction_parameters: ChannelTransactionParameters,
19911995

@@ -2062,6 +2066,10 @@ impl Readable for FundingScope {
20622066
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
20632067
#[cfg(any(test, fuzzing))]
20642068
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2069+
#[cfg(any(test, fuzzing))]
2070+
next_local_fee: Mutex::new(PredictedNextFee::default()),
2071+
#[cfg(any(test, fuzzing))]
2072+
next_remote_fee: Mutex::new(PredictedNextFee::default()),
20652073
})
20662074
}
20672075
}
@@ -3208,6 +3216,10 @@ where
32083216
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
32093217
#[cfg(any(test, fuzzing))]
32103218
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
3219+
#[cfg(any(test, fuzzing))]
3220+
next_local_fee: Mutex::new(PredictedNextFee::default()),
3221+
#[cfg(any(test, fuzzing))]
3222+
next_remote_fee: Mutex::new(PredictedNextFee::default()),
32113223

32123224
channel_transaction_parameters: ChannelTransactionParameters {
32133225
holder_pubkeys: pubkeys,
@@ -3451,6 +3463,10 @@ where
34513463
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
34523464
#[cfg(any(test, fuzzing))]
34533465
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
3466+
#[cfg(any(test, fuzzing))]
3467+
next_local_fee: Mutex::new(PredictedNextFee::default()),
3468+
#[cfg(any(test, fuzzing))]
3469+
next_remote_fee: Mutex::new(PredictedNextFee::default()),
34543470

34553471
channel_transaction_parameters: ChannelTransactionParameters {
34563472
holder_pubkeys: pubkeys,
@@ -4187,6 +4203,23 @@ where
41874203
}
41884204
}
41894205

4206+
#[cfg(any(test, fuzzing))]
4207+
{
4208+
let mut predicted_htlcs = next_commitment_htlcs;
4209+
predicted_htlcs.sort_unstable();
4210+
*funding.next_local_fee.lock().unwrap() = PredictedNextFee {
4211+
predicted_feerate: self.feerate_per_kw,
4212+
predicted_htlcs: predicted_htlcs.clone(),
4213+
predicted_fee_sat: next_commitment_stats.holder_commit_tx_fee_sat,
4214+
};
4215+
4216+
*funding.next_remote_fee.lock().unwrap() = PredictedNextFee {
4217+
predicted_feerate: self.feerate_per_kw,
4218+
predicted_htlcs,
4219+
predicted_fee_sat: next_commitment_stats.counterparty_commit_tx_fee_sat,
4220+
};
4221+
}
4222+
41904223
Ok(())
41914224
}
41924225

@@ -4271,6 +4304,12 @@ where
42714304
}
42724305
}
42734306
}
4307+
let PredictedNextFee { predicted_feerate, predicted_htlcs, predicted_fee_sat } = funding.next_local_fee.lock().unwrap().clone();
4308+
let mut actual_nondust_htlcs: Vec<_> = commitment_data.tx.nondust_htlcs().iter().map(|htlc| HTLCAmountDirection { outbound: htlc.offered, amount_msat: htlc.amount_msat }).collect();
4309+
actual_nondust_htlcs.sort_unstable();
4310+
if predicted_feerate == commitment_data.tx.feerate_per_kw() && predicted_htlcs == actual_nondust_htlcs {
4311+
assert_eq!(predicted_fee_sat, commitment_data.stats.commit_tx_fee_sat);
4312+
}
42744313
}
42754314

42764315
if msg.htlc_signatures.len() != commitment_data.tx.nondust_htlcs().len() {
@@ -5921,6 +5960,14 @@ struct CommitmentTxInfoCached {
59215960
feerate: u32,
59225961
}
59235962

5963+
#[cfg(any(test, fuzzing))]
5964+
#[derive(Clone, Default)]
5965+
struct PredictedNextFee {
5966+
predicted_feerate: u32,
5967+
predicted_htlcs: Vec<HTLCAmountDirection>,
5968+
predicted_fee_sat: u64,
5969+
}
5970+
59245971
/// Contents of a wire message that fails an HTLC backwards. Useful for [`FundedChannel::fail_htlc`] to
59255972
/// fail with either [`msgs::UpdateFailMalformedHTLC`] or [`msgs::UpdateFailHTLC`] as needed.
59265973
trait FailHTLCContents {
@@ -10885,6 +10932,12 @@ where
1088510932
}
1088610933
}
1088710934
}
10935+
let PredictedNextFee { predicted_feerate, predicted_htlcs, predicted_fee_sat } = funding.next_remote_fee.lock().unwrap().clone();
10936+
let mut actual_nondust_htlcs: Vec<_> = counterparty_commitment_tx.nondust_htlcs().iter().map(|htlc| HTLCAmountDirection { outbound: !htlc.offered, amount_msat: htlc.amount_msat }).collect();
10937+
actual_nondust_htlcs.sort_unstable();
10938+
if predicted_feerate == counterparty_commitment_tx.feerate_per_kw() && predicted_htlcs == actual_nondust_htlcs {
10939+
assert_eq!(predicted_fee_sat, commitment_data.stats.commit_tx_fee_sat);
10940+
}
1088810941
}
1088910942

1089010943
(commitment_data.htlcs_included, counterparty_commitment_tx)
@@ -13510,6 +13563,10 @@ where
1351013563
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
1351113564
#[cfg(any(test, fuzzing))]
1351213565
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
13566+
#[cfg(any(test, fuzzing))]
13567+
next_local_fee: Mutex::new(PredictedNextFee::default()),
13568+
#[cfg(any(test, fuzzing))]
13569+
next_remote_fee: Mutex::new(PredictedNextFee::default()),
1351313570

1351413571
channel_transaction_parameters: channel_parameters,
1351513572
funding_transaction,

lightning/src/sign/tx_builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ use crate::prelude::*;
1515
use crate::types::features::ChannelTypeFeatures;
1616
use crate::util::logger::Logger;
1717

18+
#[cfg(any(test, fuzzing))]
19+
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
20+
pub(crate) struct HTLCAmountDirection {
21+
pub outbound: bool,
22+
pub amount_msat: u64,
23+
}
24+
25+
#[cfg(not(any(test, fuzzing)))]
1826
pub(crate) struct HTLCAmountDirection {
1927
pub outbound: bool,
2028
pub amount_msat: u64,

0 commit comments

Comments
 (0)