Skip to content

Commit ecb3b25

Browse files
committed
Implement pending claim rebroadcast on force-closed channels
This attempts to rebroadcast/fee-bump each pending claim a monitor is tracking for a force-closed channel. This is crucial in preventing certain classes of pinning attacks and ensures reliability if broadcasting fails. For implementations of `FeeEstimator` that also support mempool fee estimation, we may broadcast a fee-bumped claim instead, ensuring we can also react to mempool fee spikes between blocks.
1 parent 563b680 commit ecb3b25

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,14 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> Deref for LockedChannelMonitor<
217217
/// or used independently to monitor channels remotely. See the [module-level documentation] for
218218
/// details.
219219
///
220+
/// Note that `ChainMonitor` should regularly trigger rebroadcasts/fee bumps of pending claims from
221+
/// a force-closed channel. This is crucial in preventing certain classes of pinning attacks and
222+
/// ensures reliability if broadcasting fails. In order to do so, we recommend invoking
223+
/// [`rebroadcast_pending_claims`] roughly once per minute, though it doesn't have to be perfect.
224+
///
220225
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
221226
/// [module-level documentation]: crate::chain::chainmonitor
227+
/// [`rebroadcast_pending_claims`]: Self::rebroadcast_pending_claims
222228
pub struct ChainMonitor<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
223229
where C::Target: chain::Filter,
224230
T::Target: BroadcasterInterface,
@@ -533,6 +539,19 @@ where C::Target: chain::Filter,
533539
pub fn get_update_future(&self) -> Future {
534540
self.event_notifier.get_future()
535541
}
542+
543+
/// Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
544+
/// crucial in preventing certain classes of pinning attacks and ensures reliability if
545+
/// broadcasting fails. We recommend invoking this roughly once per minute, though it doesn't
546+
/// have to be perfect.
547+
pub fn rebroadcast_pending_claims(&self) {
548+
let monitors = self.monitors.read().unwrap();
549+
for (_, monitor_holder) in &*monitors {
550+
monitor_holder.monitor.rebroadcast_pending_claims(
551+
&*self.broadcaster, &*self.fee_estimator, &*self.logger
552+
)
553+
}
554+
}
536555
}
537556

538557
impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>

lightning/src/chain/channelmonitor.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,26 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
14671467
pub fn current_best_block(&self) -> BestBlock {
14681468
self.inner.lock().unwrap().best_block.clone()
14691469
}
1470+
1471+
/// Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
1472+
/// crucial in preventing certain classes of pinning attacks and ensures reliability if
1473+
/// broadcasting fails. We recommend invoking this roughly once per minute, though it doesn't
1474+
/// have to be perfect.
1475+
pub fn rebroadcast_pending_claims<B: Deref, F: Deref, L: Deref>(
1476+
&self, broadcaster: B, fee_estimator: F, logger: L,
1477+
)
1478+
where
1479+
B::Target: BroadcasterInterface,
1480+
F::Target: FeeEstimator,
1481+
L::Target: Logger,
1482+
{
1483+
let fee_estimator = LowerBoundedFeeEstimator::new(fee_estimator);
1484+
let mut inner = self.inner.lock().unwrap();
1485+
let current_height = inner.best_block.height;
1486+
inner.onchain_tx_handler.rebroadcast_pending_claims(
1487+
current_height, &broadcaster, &fee_estimator, &logger,
1488+
);
1489+
}
14701490
}
14711491

14721492
impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {

lightning/src/chain/onchaintx.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,54 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
481481
events.into_iter().map(|(_, event)| event).collect()
482482
}
483483

484+
/// Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
485+
/// crucial in preventing certain classes of pinning attacks and ensures reliability if
486+
/// broadcasting fails. We recommend invoking this roughly once per minute, though it doesn't
487+
/// have to be perfect.
488+
pub(crate) fn rebroadcast_pending_claims<B: Deref, F: Deref, L: Deref>(
489+
&mut self, current_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>,
490+
logger: &L,
491+
)
492+
where
493+
B::Target: BroadcasterInterface,
494+
F::Target: FeeEstimator,
495+
L::Target: Logger,
496+
{
497+
let mut bump_requests = Vec::with_capacity(self.pending_claim_requests.len());
498+
for (package_id, request) in self.pending_claim_requests.iter() {
499+
let inputs = request.outpoints();
500+
log_info!(logger, "Triggering rebroadcast/fee-bump for request with inputs {:?}", inputs);
501+
bump_requests.push((*package_id, request.clone()));
502+
}
503+
for (package_id, request) in bump_requests {
504+
self.generate_claim(current_height, &request, false /* manually_bump_feerate */, fee_estimator, logger)
505+
.map(|(_, new_feerate, claim)| {
506+
if let Some(mut_request) = self.pending_claim_requests.get_mut(&package_id) {
507+
mut_request.set_feerate(new_feerate);
508+
}
509+
match claim {
510+
OnchainClaim::Tx(tx) => {
511+
log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(tx));
512+
broadcaster.broadcast_transaction(&tx);
513+
},
514+
#[cfg(anchors)]
515+
OnchainClaim::Event(event) => {
516+
log_info!(logger, "Yielding fee-bumped onchain event to spend inputs {:?}",
517+
request.outpoints());
518+
#[cfg(debug_assertions)] {
519+
debug_assert!(request.requires_external_funding());
520+
let num_existing = self.pending_claim_events.iter()
521+
.filter(|entry| entry.0 == package_id).count();
522+
assert!(num_existing == 0 || num_existing == 1);
523+
}
524+
self.pending_claim_events.retain(|event| event.0 != package_id);
525+
self.pending_claim_events.push((package_id, event));
526+
}
527+
}
528+
});
529+
}
530+
}
531+
484532
/// Lightning security model (i.e being able to redeem/timeout HTLC or penalize counterparty
485533
/// onchain) lays on the assumption of claim transactions getting confirmed before timelock
486534
/// expiration (CSV or CLTV following cases). In case of high-fee spikes, claim tx may get stuck

lightning/src/ln/functional_test_utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ impl ConnectStyle {
151151
}
152152
}
153153

154+
pub fn updates_best_block_first(&self) -> bool {
155+
match self {
156+
ConnectStyle::BestBlockFirst => true,
157+
ConnectStyle::BestBlockFirstSkippingBlocks => true,
158+
ConnectStyle::BestBlockFirstReorgsOnlyTip => true,
159+
ConnectStyle::TransactionsFirst => false,
160+
ConnectStyle::TransactionsFirstSkippingBlocks => false,
161+
ConnectStyle::TransactionsDuplicativelyFirstSkippingBlocks => false,
162+
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks => false,
163+
ConnectStyle::TransactionsFirstReorgsOnlyTip => false,
164+
ConnectStyle::FullBlockViaListen => false,
165+
}
166+
}
167+
154168
fn random_style() -> ConnectStyle {
155169
#[cfg(feature = "std")] {
156170
use core::hash::{BuildHasher, Hasher};

lightning/src/ln/monitor_tests.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,130 @@ fn test_restored_packages_retry() {
17781778
do_test_restored_packages_retry(true);
17791779
}
17801780

1781+
fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
1782+
// Test that we will retry broadcasting pending claims for a force-closed channel on every
1783+
// `ChainMonitor::rebroadcast_pending_claims` call.
1784+
if anchors {
1785+
assert!(cfg!(anchors));
1786+
}
1787+
let mut chanmon_cfgs = create_chanmon_cfgs(2);
1788+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1789+
let mut config = test_default_channel_config();
1790+
if anchors {
1791+
#[cfg(anchors)] {
1792+
config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1793+
}
1794+
}
1795+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
1796+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1797+
1798+
let (_, _, _, chan_id, funding_tx) = create_chan_between_nodes_with_value(
1799+
&nodes[0], &nodes[1], 1_000_000, 500_000_000
1800+
);
1801+
const HTLC_AMT_MSAT: u64 = 1_000_000;
1802+
const HTLC_AMT_SAT: u64 = HTLC_AMT_MSAT / 1000;
1803+
route_payment(&nodes[0], &[&nodes[1]], HTLC_AMT_MSAT);
1804+
1805+
let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1;
1806+
1807+
let commitment_txn = get_local_commitment_txn!(&nodes[0], &chan_id);
1808+
assert_eq!(commitment_txn.len(), if anchors { 1 /* commitment tx only */} else { 2 /* commitment and htlc timeout tx */ });
1809+
check_spends!(&commitment_txn[0], &funding_tx);
1810+
mine_transaction(&nodes[0], &commitment_txn[0]);
1811+
check_closed_broadcast!(&nodes[0], true);
1812+
check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false);
1813+
check_added_monitors(&nodes[0], 1);
1814+
1815+
// Set up a helper closure we'll use throughout our test. We should only expect retries without
1816+
// bumps if fees have not increased after a block has been connected (assuming the height timer
1817+
// re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
1818+
let mut prev_htlc_tx_feerate = None;
1819+
let mut check_htlc_retry = |should_retry: bool, should_bump: bool| {
1820+
let htlc_tx_feerate = if anchors {
1821+
assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
1822+
let mut events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1823+
assert_eq!(events.len(), if should_retry { 1 } else { 0 });
1824+
if !should_retry {
1825+
return;
1826+
}
1827+
#[allow(unused_assignments)]
1828+
let mut feerate = 0;
1829+
#[cfg(anchors)] {
1830+
let (htlcs, htlc_tx_feerate) = if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
1831+
target_feerate_sat_per_1000_weight, htlc_descriptors, ..
1832+
}) = events.pop().unwrap() {
1833+
(htlc_descriptors, target_feerate_sat_per_1000_weight)
1834+
} else { panic!("unexpected event"); };
1835+
assert_eq!(htlcs.len(), 1);
1836+
assert_eq!(htlcs[0].commitment_txid, commitment_txn[0].txid());
1837+
assert!((htlcs[0].htlc.transaction_output_index.unwrap() as usize) < commitment_txn[0].output.len());
1838+
feerate = htlc_tx_feerate as u64;
1839+
}
1840+
feerate
1841+
} else {
1842+
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1843+
let txn = nodes[0].tx_broadcaster.txn_broadcast();
1844+
assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
1845+
if !should_retry {
1846+
return;
1847+
}
1848+
check_spends!(txn[0], commitment_txn[0]);
1849+
let htlc_tx_fee = HTLC_AMT_SAT - txn[0].output[0].value;
1850+
htlc_tx_fee * 1000 / txn[0].weight() as u64
1851+
};
1852+
if should_bump {
1853+
assert!(htlc_tx_feerate > prev_htlc_tx_feerate.unwrap());
1854+
} else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
1855+
assert!(htlc_tx_feerate <= prev_feerate);
1856+
}
1857+
prev_htlc_tx_feerate = Some(htlc_tx_feerate);
1858+
};
1859+
1860+
// Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
1861+
connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 2);
1862+
check_htlc_retry(false, false);
1863+
1864+
// Connect one more block, producing our first claim.
1865+
connect_blocks(&nodes[0], 1);
1866+
check_htlc_retry(true, false);
1867+
1868+
// Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
1869+
// transactions pre-anchors.
1870+
connect_blocks(&nodes[0], 1);
1871+
check_htlc_retry(true, anchors);
1872+
1873+
// Trigger a call and we should have another retry, but without a bump.
1874+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1875+
check_htlc_retry(true, false);
1876+
1877+
// Double the feerate and trigger a call, expecting a fee-bumped retry.
1878+
*nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
1879+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1880+
check_htlc_retry(true, anchors);
1881+
1882+
// Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
1883+
// transactions pre-anchors.
1884+
connect_blocks(&nodes[0], 1);
1885+
check_htlc_retry(true, anchors);
1886+
1887+
if !anchors {
1888+
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
1889+
mine_transaction(&nodes[0], &commitment_txn[1]);
1890+
if nodes[0].connect_style.borrow().updates_best_block_first() {
1891+
check_htlc_retry(true, false);
1892+
}
1893+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1894+
check_htlc_retry(false, false);
1895+
}
1896+
}
1897+
1898+
#[test]
1899+
fn test_monitor_timer_based_claim() {
1900+
do_test_monitor_rebroadcast_pending_claims(false);
1901+
#[cfg(anchors)]
1902+
do_test_monitor_rebroadcast_pending_claims(true);
1903+
}
1904+
17811905
#[cfg(anchors)]
17821906
#[test]
17831907
fn test_yield_anchors_events() {

0 commit comments

Comments
 (0)