-
Notifications
You must be signed in to change notification settings - Fork 363
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
Add min mempool estimate for feerate updates on anchor channels #2415
Merged
TheBlueMatt
merged 3 commits into
lightningdevkit:main
from
wpaulino:update-fee-anchors
Jul 17, 2023
Merged
Changes from all commits
Commits
Show all changes
3 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 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 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 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 |
---|---|---|
|
@@ -2045,20 +2045,35 @@ struct CommitmentTxInfoCached { | |
} | ||
|
||
impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> { | ||
fn check_remote_fee<F: Deref, L: Deref>(fee_estimator: &LowerBoundedFeeEstimator<F>, | ||
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L) | ||
-> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger, | ||
fn check_remote_fee<F: Deref, L: Deref>( | ||
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>, | ||
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. Could we make this a member function instead of passing in 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. No because we don't have a |
||
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L | ||
) -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger, | ||
{ | ||
// We only bound the fee updates on the upper side to prevent completely absurd feerates, | ||
// always accepting up to 25 sat/vByte or 10x our fee estimator's "High Priority" fee. | ||
// We generally don't care too much if they set the feerate to something very high, but it | ||
// could result in the channel being useless due to everything being dust. | ||
let upper_limit = cmp::max(250 * 25, | ||
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10); | ||
if feerate_per_kw as u64 > upper_limit { | ||
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit))); | ||
} | ||
let lower_limit = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background); | ||
// could result in the channel being useless due to everything being dust. This doesn't | ||
// apply to channels supporting anchor outputs since HTLC transactions are pre-signed with a | ||
// zero fee, so their fee is no longer considered to determine dust limits. | ||
if !channel_type.supports_anchors_zero_fee_htlc_tx() { | ||
let upper_limit = cmp::max(250 * 25, | ||
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10); | ||
if feerate_per_kw as u64 > upper_limit { | ||
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit))); | ||
} | ||
} | ||
|
||
// We can afford to use a lower bound with anchors than previously since we can now bump | ||
// fees when broadcasting our commitment. However, we must still make sure we meet the | ||
// minimum mempool feerate, until package relay is deployed, such that we can ensure the | ||
// commitment transaction propagates throughout node mempools on its own. | ||
let lower_limit_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() { | ||
ConfirmationTarget::MempoolMinimum | ||
} else { | ||
ConfirmationTarget::Background | ||
}; | ||
let lower_limit = fee_estimator.bounded_sat_per_1000_weight(lower_limit_conf_target); | ||
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing | ||
// occasional issues with feerate disagreements between an initiator that wants a feerate | ||
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250 | ||
|
@@ -3688,7 +3703,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> { | |
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { | ||
return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish".to_owned())); | ||
} | ||
Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger)?; | ||
Channel::<Signer>::check_remote_fee(&self.context.channel_type, fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger)?; | ||
let feerate_over_dust_buffer = msg.feerate_per_kw > self.context.get_dust_buffer_feerate(None); | ||
|
||
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced)); | ||
|
@@ -5502,10 +5517,15 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> { | |
let channel_type = Self::get_initial_channel_type(&config, their_features); | ||
debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config))); | ||
|
||
let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal); | ||
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() { | ||
ConfirmationTarget::MempoolMinimum | ||
} else { | ||
ConfirmationTarget::Normal | ||
}; | ||
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target); | ||
|
||
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat; | ||
let commitment_tx_fee = commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type); | ||
let commitment_tx_fee = commit_tx_fee_msat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type); | ||
if value_to_self_msat < commitment_tx_fee { | ||
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) }); | ||
} | ||
|
@@ -5599,7 +5619,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> { | |
short_channel_id: None, | ||
channel_creation_height: current_chain_height, | ||
|
||
feerate_per_kw: feerate, | ||
feerate_per_kw: commitment_feerate, | ||
counterparty_dust_limit_satoshis: 0, | ||
holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS, | ||
counterparty_max_htlc_value_in_flight_msat: 0, | ||
|
@@ -5753,7 +5773,12 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> { | |
/// If we receive an error message, it may only be a rejection of the channel type we tried, | ||
/// not of our ability to open any channel at all. Thus, on error, we should first call this | ||
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed. | ||
pub(crate) fn maybe_handle_error_without_close(&mut self, chain_hash: BlockHash) -> Result<msgs::OpenChannel, ()> { | ||
pub(crate) fn maybe_handle_error_without_close<F: Deref>( | ||
&mut self, chain_hash: BlockHash, fee_estimator: &LowerBoundedFeeEstimator<F> | ||
) -> Result<msgs::OpenChannel, ()> | ||
where | ||
F::Target: FeeEstimator | ||
{ | ||
if !self.context.is_outbound() || self.context.channel_state != ChannelState::OurInitSent as u32 { return Err(()); } | ||
if self.context.channel_type == ChannelTypeFeatures::only_static_remote_key() { | ||
// We've exhausted our options | ||
|
@@ -5770,6 +5795,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> { | |
// whatever reason. | ||
if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() { | ||
self.context.channel_type.clear_anchors_zero_fee_htlc_tx(); | ||
self.context.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal); | ||
assert!(!self.context.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx()); | ||
} else if self.context.channel_type.supports_scid_privacy() { | ||
self.context.channel_type.clear_scid_privacy(); | ||
|
@@ -6039,7 +6065,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> { | |
if msg.htlc_minimum_msat >= full_channel_value_msat { | ||
return Err(ChannelError::Close(format!("Minimum htlc value ({}) was larger than full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat))); | ||
} | ||
Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, None, logger)?; | ||
Channel::<Signer>::check_remote_fee(&channel_type, fee_estimator, msg.feerate_per_kw, None, logger)?; | ||
|
||
let max_counterparty_selected_contest_delay = u16::min(config.channel_handshake_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT); | ||
if msg.to_self_delay > max_counterparty_selected_contest_delay { | ||
|
@@ -7441,7 +7467,8 @@ mod tests { | |
// arithmetic, causing a panic with debug assertions enabled. | ||
let fee_est = TestFeeEstimator { fee_est: 42 }; | ||
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_est); | ||
assert!(Channel::<InMemorySigner>::check_remote_fee(&bounded_fee_estimator, | ||
assert!(Channel::<InMemorySigner>::check_remote_fee( | ||
&ChannelTypeFeatures::only_static_remote_key(), &bounded_fee_estimator, | ||
u32::max_value(), None, &&test_utils::TestLogger::new()).is_err()); | ||
} | ||
|
||
|
This file contains 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
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.
nit: s/not able/unable