-
Notifications
You must be signed in to change notification settings - Fork 443
feat: allow configurable force-close buffer for claimable HTLCs #4401
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
Open
okekefrancis112
wants to merge
3
commits into
lightningdevkit:main
Choose a base branch
from
okekefrancis112:claimable_htcl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−39
Open
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 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 |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; | |
| #[cfg(peer_storage)] | ||
| use crate::chain::channelmonitor::write_chanmon_internal; | ||
| use crate::chain::channelmonitor::{ | ||
| Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs, | ||
| self, Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs, | ||
| WithChannelMonitor, | ||
| }; | ||
| use crate::chain::transaction::{OutPoint, TransactionData}; | ||
|
|
@@ -350,6 +350,12 @@ pub struct ChainMonitor< | |
| P::Target: Persist<ChannelSigner>, | ||
| { | ||
| monitors: RwLock<HashMap<ChannelId, MonitorHolder<ChannelSigner>>>, | ||
|
|
||
| /// Memory-only per-channel configuration for the CLTV buffer used when deciding | ||
| /// to force-close channels with claimable inbound HTLCs. This is not persisted | ||
| /// and is rebuilt from channel state on restart. | ||
| channel_force_close_buffers: RwLock<HashMap<ChannelId, u32>>, | ||
|
|
||
| chain_source: Option<C>, | ||
| broadcaster: T, | ||
| logger: L, | ||
|
|
@@ -402,6 +408,7 @@ where | |
| let event_notifier = Arc::new(Notifier::new()); | ||
| Self { | ||
| monitors: RwLock::new(new_hash_map()), | ||
| channel_force_close_buffers: RwLock::new(new_hash_map()), | ||
| chain_source, | ||
| broadcaster, | ||
| logger, | ||
|
|
@@ -607,6 +614,7 @@ where | |
| ) -> Self { | ||
| Self { | ||
| monitors: RwLock::new(new_hash_map()), | ||
| channel_force_close_buffers: RwLock::new(new_hash_map()), | ||
| chain_source, | ||
| broadcaster, | ||
| logger, | ||
|
|
@@ -622,6 +630,27 @@ where | |
| } | ||
| } | ||
|
|
||
| /// Updates the force-close buffer configuration for a channel. | ||
| /// | ||
| /// This is a memory-only update and does not trigger persistence. The buffer value | ||
| /// determines how many blocks before an inbound HTLC's CLTV expiry the channel will | ||
| /// be force-closed to claim it on-chain. | ||
| /// | ||
| /// Returns an error if the buffer value is below [`CLTV_CLAIM_BUFFER`]. | ||
| /// | ||
| /// [`CLTV_CLAIM_BUFFER`]: channelmonitor::CLTV_CLAIM_BUFFER | ||
| pub fn update_channel_force_close_buffer( | ||
|
Collaborator
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. Rather than a setter, can we just have this be a parameter on the constructor? |
||
| &self, channel_id: ChannelId, force_close_buffer: u32, | ||
| ) -> Result<(), ()> { | ||
| if force_close_buffer < channelmonitor::CLTV_CLAIM_BUFFER { | ||
| return Err(()); | ||
| } | ||
|
|
||
| let mut buffers = self.channel_force_close_buffers.write().unwrap(); | ||
| buffers.insert(channel_id, force_close_buffer); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or | ||
| /// claims which are awaiting confirmation. | ||
| /// | ||
|
|
@@ -1128,10 +1157,16 @@ where | |
| height | ||
| ); | ||
| self.process_chain_data(header, Some(height), &txdata, |monitor, txdata| { | ||
| let channel_id = monitor.channel_id(); | ||
| let buffers = self.channel_force_close_buffers.read().unwrap(); | ||
| let force_close_buffer = | ||
| buffers.get(&channel_id).copied().unwrap_or(channelmonitor::CLTV_CLAIM_BUFFER); | ||
|
|
||
| monitor.block_connected( | ||
| header, | ||
| txdata, | ||
| height, | ||
| force_close_buffer, | ||
| &self.broadcaster, | ||
| &self.fee_estimator, | ||
| &self.logger, | ||
|
|
@@ -1188,10 +1223,16 @@ where | |
| header.block_hash() | ||
| ); | ||
| self.process_chain_data(header, None, txdata, |monitor, txdata| { | ||
| let channel_id = monitor.channel_id(); | ||
| let buffers = self.channel_force_close_buffers.read().unwrap(); | ||
| let force_close_buffer = | ||
| buffers.get(&channel_id).copied().unwrap_or(channelmonitor::CLTV_CLAIM_BUFFER); | ||
|
|
||
| monitor.transactions_confirmed( | ||
| header, | ||
| txdata, | ||
| height, | ||
| force_close_buffer, | ||
| &self.broadcaster, | ||
| &self.fee_estimator, | ||
| &self.logger, | ||
|
|
@@ -1225,9 +1266,15 @@ where | |
| // While in practice there shouldn't be any recursive calls when given empty txdata, | ||
| // it's still possible if a chain::Filter implementation returns a transaction. | ||
| debug_assert!(txdata.is_empty()); | ||
| let channel_id = monitor.channel_id(); | ||
| let buffers = self.channel_force_close_buffers.read().unwrap(); | ||
| let force_close_buffer = | ||
| buffers.get(&channel_id).copied().unwrap_or(channelmonitor::CLTV_CLAIM_BUFFER); | ||
|
|
||
| monitor.best_block_updated( | ||
| header, | ||
| height, | ||
| force_close_buffer, | ||
| &self.broadcaster, | ||
| &self.fee_estimator, | ||
| &self.logger, | ||
|
|
@@ -1282,6 +1329,7 @@ where | |
| hash_map::Entry::Vacant(e) => e, | ||
| }; | ||
| log_trace!(logger, "Got new ChannelMonitor"); | ||
| let initial_buffer = monitor.get_initial_force_close_buffer(); | ||
| let update_id = monitor.get_latest_update_id(); | ||
| let mut pending_monitor_updates = Vec::new(); | ||
| let persist_res = self.persister.persist_new_channel(monitor.persistence_key(), &monitor); | ||
|
|
@@ -1306,6 +1354,10 @@ where | |
| monitor, | ||
| pending_monitor_updates: Mutex::new(pending_monitor_updates), | ||
| }); | ||
|
|
||
| let mut buffers = self.channel_force_close_buffers.write().unwrap(); | ||
| buffers.insert(channel_id, initial_buffer); | ||
|
|
||
| Ok(persist_res) | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Hmm, is there a reason to want this per-channel vs just setting a global value and calling it done?