Skip to content

Commit d8cca98

Browse files
authored
Merge pull request #1611 from TheBlueMatt/2022-07-lower-bounded-estimator-nit
Use a separate (non-trait) fee-estimation fn in LowerBoundedEstimator
2 parents 1988cb2 + af7e9b6 commit d8cca98

File tree

7 files changed

+25
-32
lines changed

7 files changed

+25
-32
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
140140
};
141141
let deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::
142142
read(&mut Cursor::new(&map_entry.get().1), &*self.keys).unwrap().1;
143-
deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &&FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
143+
deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
144144
let mut ser = VecWriter(Vec::new());
145145
deserialized_monitor.write(&mut ser).unwrap();
146146
map_entry.insert((update.update_id, ser.0));

lightning/src/chain/chaininterface.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ pub trait FeeEstimator {
5252
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32;
5353
}
5454

55-
// We need `FeeEstimator` implemented so that in some places where we only have a shared
56-
// reference to a `Deref` to a `FeeEstimator`, we can still wrap it.
57-
impl<D: Deref> FeeEstimator for D where D::Target: FeeEstimator {
58-
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
59-
(**self).get_est_sat_per_1000_weight(confirmation_target)
60-
}
61-
}
62-
6355
/// Minimum relay fee as required by bitcoin network mempool policy.
6456
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
6557
/// Minimum feerate that takes a sane approach to bitcoind weight-to-vbytes rounding.
@@ -68,18 +60,19 @@ pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
6860
pub const FEERATE_FLOOR_SATS_PER_KW: u32 = 253;
6961

7062
/// Wraps a `Deref` to a `FeeEstimator` so that any fee estimations provided by it
71-
/// are bounded below by `FEERATE_FLOOR_SATS_PER_KW` (253 sats/KW)
63+
/// are bounded below by `FEERATE_FLOOR_SATS_PER_KW` (253 sats/KW).
64+
///
65+
/// Note that this does *not* implement [`FeeEstimator`] to make it harder to accidentally mix the
66+
/// two.
7267
pub(crate) struct LowerBoundedFeeEstimator<F: Deref>(pub F) where F::Target: FeeEstimator;
7368

7469
impl<F: Deref> LowerBoundedFeeEstimator<F> where F::Target: FeeEstimator {
7570
/// Creates a new `LowerBoundedFeeEstimator` which wraps the provided fee_estimator
7671
pub fn new(fee_estimator: F) -> Self {
7772
LowerBoundedFeeEstimator(fee_estimator)
7873
}
79-
}
8074

81-
impl<F: Deref> FeeEstimator for LowerBoundedFeeEstimator<F> where F::Target: FeeEstimator {
82-
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
75+
pub fn bounded_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
8376
cmp::max(
8477
self.0.get_est_sat_per_1000_weight(confirmation_target),
8578
FEERATE_FLOOR_SATS_PER_KW,
@@ -107,7 +100,7 @@ mod tests {
107100
let test_fee_estimator = &TestFeeEstimator { sat_per_kw };
108101
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator);
109102

110-
assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), FEERATE_FLOOR_SATS_PER_KW);
103+
assert_eq!(fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background), FEERATE_FLOOR_SATS_PER_KW);
111104
}
112105

113106
#[test]
@@ -116,6 +109,6 @@ mod tests {
116109
let test_fee_estimator = &TestFeeEstimator { sat_per_kw };
117110
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator);
118111

119-
assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), sat_per_kw);
112+
assert_eq!(fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background), sat_per_kw);
120113
}
121114
}

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ where C::Target: chain::Filter,
636636
Some(monitor_state) => {
637637
let monitor = &monitor_state.monitor;
638638
log_trace!(self.logger, "Updating ChannelMonitor for channel {}", log_funding_info!(monitor));
639-
let update_res = monitor.update_monitor(&update, &self.broadcaster, &self.fee_estimator, &self.logger);
639+
let update_res = monitor.update_monitor(&update, &self.broadcaster, &*self.fee_estimator, &self.logger);
640640
if update_res.is_err() {
641641
log_error!(self.logger, "Failed to update ChannelMonitor for channel {}.", log_funding_info!(monitor));
642642
}

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
11391139
&self,
11401140
updates: &ChannelMonitorUpdate,
11411141
broadcaster: &B,
1142-
fee_estimator: &F,
1142+
fee_estimator: F,
11431143
logger: &L,
11441144
) -> Result<(), ()>
11451145
where
@@ -1949,10 +1949,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19491949
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
19501950
}
19511951

1952-
pub fn update_monitor<B: Deref, F: Deref, L: Deref>(&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: &F, logger: &L) -> Result<(), ()>
1952+
pub fn update_monitor<B: Deref, F: Deref, L: Deref>(&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: F, logger: &L) -> Result<(), ()>
19531953
where B::Target: BroadcasterInterface,
1954-
F::Target: FeeEstimator,
1955-
L::Target: Logger,
1954+
F::Target: FeeEstimator,
1955+
L::Target: Logger,
19561956
{
19571957
log_info!(logger, "Applying update to monitor {}, bringing update_id from {} to {} with {} changes.",
19581958
log_funding_info!(self), self.latest_update_id, updates.update_id, updates.updates.len());
@@ -1990,7 +1990,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19901990
},
19911991
ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } => {
19921992
log_trace!(logger, "Updating ChannelMonitor with payment preimage");
1993-
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(fee_estimator);
1993+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&*fee_estimator);
19941994
self.provide_payment_preimage(&PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()), &payment_preimage, broadcaster, &bounded_fee_estimator, logger)
19951995
},
19961996
ChannelMonitorUpdateStep::CommitmentSecret { idx, secret } => {
@@ -3537,7 +3537,7 @@ mod tests {
35373537

35383538
let broadcaster = TestBroadcaster::new(Arc::clone(&nodes[1].blocks));
35393539
assert!(
3540-
pre_update_monitor.update_monitor(&replay_update, &&broadcaster, &&chanmon_cfgs[1].fee_estimator, &nodes[1].logger)
3540+
pre_update_monitor.update_monitor(&replay_update, &&broadcaster, &chanmon_cfgs[1].fee_estimator, &nodes[1].logger)
35413541
.is_err());
35423542
// Even though we error'd on the first update, we should still have generated an HTLC claim
35433543
// transaction

lightning/src/chain/package.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,13 @@ fn compute_fee_from_spent_amounts<F: Deref, L: Deref>(input_amounts: u64, predic
778778
where F::Target: FeeEstimator,
779779
L::Target: Logger,
780780
{
781-
let mut updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64;
781+
let mut updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64;
782782
let mut fee = updated_feerate * (predicted_weight as u64) / 1000;
783783
if input_amounts <= fee {
784-
updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) as u64;
784+
updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal) as u64;
785785
fee = updated_feerate * (predicted_weight as u64) / 1000;
786786
if input_amounts <= fee {
787-
updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u64;
787+
updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background) as u64;
788788
fee = updated_feerate * (predicted_weight as u64) / 1000;
789789
if input_amounts <= fee {
790790
log_error!(logger, "Failed to generate an on-chain punishment tx as even low priority fee ({} sat) was more than the entire claim balance ({} sat)",

lightning/src/ln/channel.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ impl<Signer: Sign> Channel<Signer> {
917917
return Err(APIError::APIMisuseError { err: format!("Holder selected channel reserve below implemention limit dust_limit_satoshis {}", holder_selected_channel_reserve_satoshis) });
918918
}
919919

920-
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
920+
let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
921921

922922
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
923923
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors);
@@ -1064,11 +1064,11 @@ impl<Signer: Sign> Channel<Signer> {
10641064
// We generally don't care too much if they set the feerate to something very high, but it
10651065
// could result in the channel being useless due to everything being dust.
10661066
let upper_limit = cmp::max(250 * 25,
1067-
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
1067+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
10681068
if feerate_per_kw as u64 > upper_limit {
10691069
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
10701070
}
1071-
let lower_limit = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
1071+
let lower_limit = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
10721072
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
10731073
// occasional issues with feerate disagreements between an initiator that wants a feerate
10741074
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
@@ -4022,8 +4022,8 @@ impl<Signer: Sign> Channel<Signer> {
40224022
// Propose a range from our current Background feerate to our Normal feerate plus our
40234023
// force_close_avoidance_max_fee_satoshis.
40244024
// If we fail to come to consensus, we'll have to force-close.
4025-
let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
4026-
let normal_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
4025+
let mut proposed_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
4026+
let normal_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
40274027
let mut proposed_max_feerate = if self.is_outbound() { normal_feerate } else { u32::max_value() };
40284028

40294029
// The spec requires that (when the channel does not have anchors) we only send absolute

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,7 +3577,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35773577
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
35783578
let mut should_persist = NotifyOption::SkipPersist;
35793579

3580-
let new_feerate = self.fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
3580+
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
35813581

35823582
let mut handle_errors = Vec::new();
35833583
{
@@ -3616,7 +3616,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
36163616
let mut should_persist = NotifyOption::SkipPersist;
36173617
if self.process_background_events() { should_persist = NotifyOption::DoPersist; }
36183618

3619-
let new_feerate = self.fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
3619+
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
36203620

36213621
let mut handle_errors = Vec::new();
36223622
let mut timed_out_mpp_htlcs = Vec::new();

0 commit comments

Comments
 (0)