Skip to content

Commit ba8d54e

Browse files
committed
Add min feerate checks
1 parent abf6564 commit ba8d54e

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ pub trait FeeEstimator {
5252

5353
/// Minimum relay fee as required by bitcoin network mempool policy.
5454
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
55+
/// Minimum feerate that takes a sane approach to rounding
56+
pub const FEERATE_FLOOR_SATS_PER_KW: u32 = 253;

lightning/src/chain/package.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ln::PaymentPreimage;
2424
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
2525
use ln::chan_utils;
2626
use ln::msgs::DecodeError;
27-
use chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
27+
use chain::chaininterface::{FeeEstimator, FEERATE_FLOOR_SATS_PER_KW, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2828
use chain::keysinterface::Sign;
2929
use chain::onchaintx::OnchainTxHandler;
3030
use util::byte_utils;
@@ -777,12 +777,15 @@ fn compute_fee_from_spent_amounts<F: Deref, L: Deref>(input_amounts: u64, predic
777777
L::Target: Logger,
778778
{
779779
let mut updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64;
780+
assert!(updated_feerate >= FEERATE_FLOOR_SATS_PER_KW as u64);
780781
let mut fee = updated_feerate * (predicted_weight as u64) / 1000;
781782
if input_amounts <= fee {
782783
updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) as u64;
784+
assert!(updated_feerate >= FEERATE_FLOOR_SATS_PER_KW as u64);
783785
fee = updated_feerate * (predicted_weight as u64) / 1000;
784786
if input_amounts <= fee {
785787
updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u64;
788+
assert!(updated_feerate >= FEERATE_FLOOR_SATS_PER_KW as u64);
786789
fee = updated_feerate * (predicted_weight as u64) / 1000;
787790
if input_amounts <= fee {
788791
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ln::channelmanager::{CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSour
3131
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight, htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
3232
use ln::chan_utils;
3333
use chain::BestBlock;
34-
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
34+
use chain::chaininterface::{FeeEstimator, FEERATE_FLOOR_SATS_PER_KW, ConfirmationTarget};
3535
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
3636
use chain::transaction::{OutPoint, TransactionData};
3737
use chain::keysinterface::{Sign, KeysInterface};
@@ -908,6 +908,7 @@ impl<Signer: Sign> Channel<Signer> {
908908
}
909909

910910
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
911+
assert!(feerate >= FEERATE_FLOOR_SATS_PER_KW);
911912

912913
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
913914
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors);
@@ -1053,10 +1054,12 @@ impl<Signer: Sign> Channel<Signer> {
10531054
// could result in the channel being useless due to everything being dust.
10541055
let upper_limit = cmp::max(250 * 25,
10551056
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
1057+
assert!(upper_limit >= FEERATE_FLOOR_SATS_PER_KW as u64);
10561058
if feerate_per_kw as u64 > upper_limit {
10571059
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
10581060
}
10591061
let lower_limit = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
1062+
assert!(lower_limit >= FEERATE_FLOOR_SATS_PER_KW);
10601063
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
10611064
// occasional issues with feerate disagreements between an initiator that wants a feerate
10621065
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
@@ -3992,7 +3995,9 @@ impl<Signer: Sign> Channel<Signer> {
39923995
// force_close_avoidance_max_fee_satoshis.
39933996
// If we fail to come to consensus, we'll have to force-close.
39943997
let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
3998+
assert!(proposed_feerate >= FEERATE_FLOOR_SATS_PER_KW);
39953999
let normal_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
4000+
assert!(normal_feerate >= FEERATE_FLOOR_SATS_PER_KW);
39964001
let mut proposed_max_feerate = if self.is_outbound() { normal_feerate } else { u32::max_value() };
39974002

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

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use bitcoin::secp256k1;
3636

3737
use chain;
3838
use chain::{Confirm, ChannelMonitorUpdateErr, Watch, BestBlock};
39-
use chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
39+
use chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator, FEERATE_FLOOR_SATS_PER_KW};
4040
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
4141
use chain::transaction::{OutPoint, TransactionData};
4242
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
@@ -3437,6 +3437,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34373437
let mut should_persist = NotifyOption::SkipPersist;
34383438

34393439
let new_feerate = self.fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
3440+
assert!(new_feerate >= FEERATE_FLOOR_SATS_PER_KW);
34403441

34413442
let mut handle_errors = Vec::new();
34423443
{
@@ -3474,6 +3475,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34743475
if self.process_background_events() { should_persist = NotifyOption::DoPersist; }
34753476

34763477
let new_feerate = self.fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
3478+
assert!(new_feerate >= FEERATE_FLOOR_SATS_PER_KW);
34773479

34783480
let mut handle_errors = Vec::new();
34793481
let mut timed_out_mpp_htlcs = Vec::new();

0 commit comments

Comments
 (0)