-
Notifications
You must be signed in to change notification settings - Fork 407
Add min feerate checks #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheBlueMatt
merged 2 commits into
lightningdevkit:main
from
dunxen:2022-06-checkminrelayfee
Jul 13, 2022
Merged
Add min feerate checks #1552
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
//! Includes traits for monitoring and receiving notifications of new blocks and block | ||
//! disconnections, transaction broadcasting, and feerate information requests. | ||
|
||
use core::{cmp, ops::Deref}; | ||
|
||
use bitcoin::blockdata::transaction::Transaction; | ||
|
||
/// An interface to send a transaction to the Bitcoin network. | ||
|
@@ -41,14 +43,79 @@ pub enum ConfirmationTarget { | |
pub trait FeeEstimator { | ||
/// Gets estimated satoshis of fee required per 1000 Weight-Units. | ||
/// | ||
/// Must return a value no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later | ||
/// round-downs don't put us below 1 satoshi-per-byte). | ||
/// LDK will wrap this method and ensure that the value returned is no smaller than 253 | ||
/// (ie 1 satoshi-per-byte rounded up to ensure later round-downs don't put us below 1 satoshi-per-byte). | ||
/// | ||
/// This method can be implemented with the following unit conversions: | ||
/// * max(satoshis-per-byte * 250, 253) | ||
/// * max(satoshis-per-kbyte / 4, 253) | ||
/// The following unit conversions can be used to convert to sats/KW: | ||
/// * satoshis-per-byte * 250 | ||
/// * satoshis-per-kbyte / 4 | ||
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32; | ||
} | ||
|
||
// We need `FeeEstimator` implemented so that in some places where we only have a shared | ||
// reference to a `Deref` to a `FeeEstimator`, we can still wrap it. | ||
impl<D: Deref> FeeEstimator for D where D::Target: FeeEstimator { | ||
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 { | ||
(**self).get_est_sat_per_1000_weight(confirmation_target) | ||
} | ||
} | ||
|
||
/// Minimum relay fee as required by bitcoin network mempool policy. | ||
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000; | ||
/// Minimum feerate that takes a sane approach to bitcoind weight-to-vbytes rounding. | ||
/// See the following Core Lightning commit for an explanation: | ||
/// https://github.com/ElementsProject/lightning/commit/2e687b9b352c9092b5e8bd4a688916ac50b44af0 | ||
pub const FEERATE_FLOOR_SATS_PER_KW: u32 = 253; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Minimum feerate that takes a sane approach to bitcoind weight-to-vbytes rounding" I don't know if this subtlety is documented anywhere well, so feel free to ref Rusty's commit : ElementsProject/lightning@2e687b9 |
||
|
||
/// Wraps a `Deref` to a `FeeEstimator` so that any fee estimations provided by it | ||
/// are bounded below by `FEERATE_FLOOR_SATS_PER_KW` (253 sats/KW) | ||
pub(crate) struct LowerBoundedFeeEstimator<F: Deref>(pub F) where F::Target: FeeEstimator; | ||
|
||
impl<F: Deref> LowerBoundedFeeEstimator<F> where F::Target: FeeEstimator { | ||
/// Creates a new `LowerBoundedFeeEstimator` which wraps the provided fee_estimator | ||
pub fn new(fee_estimator: F) -> Self { | ||
LowerBoundedFeeEstimator(fee_estimator) | ||
} | ||
} | ||
|
||
impl<F: Deref> FeeEstimator for LowerBoundedFeeEstimator<F> where F::Target: FeeEstimator { | ||
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 { | ||
cmp::max( | ||
self.0.get_est_sat_per_1000_weight(confirmation_target), | ||
FEERATE_FLOOR_SATS_PER_KW, | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{FEERATE_FLOOR_SATS_PER_KW, LowerBoundedFeeEstimator, ConfirmationTarget, FeeEstimator}; | ||
|
||
struct TestFeeEstimator { | ||
sat_per_kw: u32, | ||
} | ||
|
||
impl FeeEstimator for TestFeeEstimator { | ||
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 { | ||
self.sat_per_kw | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_fee_estimator_less_than_floor() { | ||
let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW - 1; | ||
let test_fee_estimator = &TestFeeEstimator { sat_per_kw }; | ||
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator); | ||
|
||
assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), FEERATE_FLOOR_SATS_PER_KW); | ||
} | ||
|
||
#[test] | ||
fn test_fee_estimator_greater_than_floor() { | ||
let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW + 1; | ||
let test_fee_estimator = &TestFeeEstimator { sat_per_kw }; | ||
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator); | ||
|
||
assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), sat_per_kw); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unit conversions are still really useful.