Skip to content

Commit 384fda6

Browse files
author
Antoine Riard
committed
Integrate OnchainRequest
This commit effectively replaces legacy data structures in ChannelMonitor/OnchainTxHandler. This doesn't change behavior.
1 parent 3d22a8b commit 384fda6

File tree

2 files changed

+74
-142
lines changed

2 files changed

+74
-142
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use ln::chan_utils;
3939
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction, HTLCType};
4040
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
4141
use ln::onchaintx::OnchainTxHandler;
42-
use ln::onchain_utils::{InputDescriptors, PackageTemplate};
42+
use ln::onchain_utils::{InputDescriptors, PackageTemplate, OnchainRequest, BumpStrategy};
4343
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
4444
use chain::transaction::OutPoint;
4545
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -458,26 +458,6 @@ impl Readable for CounterpartyCommitmentTransaction {
458458
}
459459
}
460460

461-
/// ClaimRequest is a descriptor structure to communicate between detection
462-
/// and reaction module. They are generated by ChannelMonitor while parsing
463-
/// onchain txn leaked from a channel and handed over to OnchainTxHandler which
464-
/// is responsible for opportunistic aggregation, selecting and enforcing
465-
/// bumping logic, building and signing transactions.
466-
pub(crate) struct ClaimRequest {
467-
// Block height before which claiming is exclusive to one party,
468-
// after reaching it, claiming may be contentious.
469-
pub(crate) absolute_timelock: u32,
470-
// Timeout tx must have nLocktime set which means aggregating multiple
471-
// ones must take the higher nLocktime among them to satisfy all of them.
472-
// Sadly it has few pitfalls, a) it takes longuer to get fund back b) CLTV_DELTA
473-
// of a sooner-HTLC could be swallowed by the highest nLocktime of the HTLC set.
474-
// Do simplify we mark them as non-aggregable.
475-
pub(crate) aggregable: bool,
476-
// Template (list of outpoint and set of data needed to generate transaction digest
477-
// and satisfy witness program).
478-
pub(crate) package_template: PackageTemplate,
479-
}
480-
481461
/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
482462
/// once they mature to enough confirmations (ANTI_REORG_DELAY)
483463
#[derive(Clone, PartialEq)]
@@ -1368,7 +1348,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13681348
/// HTLC-Success/HTLC-Timeout transactions.
13691349
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
13701350
/// revoked counterparty commitment tx
1371-
fn check_spend_counterparty_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<ClaimRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
1351+
fn check_spend_counterparty_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<OnchainRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
13721352
// Most secp and related errors trying to create keys means we have no hope of constructing
13731353
// a spend transaction...so we return no transactions to broadcast
13741354
let mut claimable_outpoints = Vec::new();
@@ -1401,7 +1381,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14011381
for (idx, outp) in tx.output.iter().enumerate() {
14021382
if outp.script_pubkey == revokeable_p2wsh {
14031383
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, InputDescriptors::RevokedOutput, commitment_txid, idx as u32, outp.value, None, self.counterparty_tx_cache.on_counterparty_tx_csv);
1404-
claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, aggregable: true, package_template: malleable_justice_tx});
1384+
claimable_outpoints.push(OnchainRequest { aggregation: true, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, height_original: height, content: malleable_justice_tx});
14051385
}
14061386
}
14071387

@@ -1414,7 +1394,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14141394
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
14151395
}
14161396
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }, commitment_txid, transaction_output_index, htlc.amount_msat / 1000, Some(htlc.clone()), self.counterparty_tx_cache.on_counterparty_tx_csv);
1417-
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, package_template: malleable_justice_tx});
1397+
claimable_outpoints.push(OnchainRequest { aggregation: true, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: htlc.cltv_expiry, height_original: height, content: malleable_justice_tx});
14181398
}
14191399
}
14201400
}
@@ -1546,10 +1526,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15461526
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
15471527
}
15481528
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
1549-
let aggregable = if !htlc.offered { false } else { true };
15501529
if preimage.is_some() || !htlc.offered {
15511530
let counterparty_htlc_tx = PackageTemplate::build_counterparty_htlc_tx(*revocation_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, preimage, htlc.clone(), commitment_txid, transaction_output_index);
1552-
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, package_template: counterparty_htlc_tx });
1531+
claimable_outpoints.push(OnchainRequest { aggregation: if !htlc.offered { false } else { true }, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: htlc.cltv_expiry, height_original: height, content: counterparty_htlc_tx });
15531532
}
15541533
}
15551534
}
@@ -1560,7 +1539,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15601539
}
15611540

15621541
/// Attempts to claim a counterparty HTLC-Success/HTLC-Timeout's outputs using the revocation key
1563-
fn check_spend_counterparty_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<ClaimRequest>, Option<(Txid, Vec<TxOut>)>) where L::Target: Logger {
1542+
fn check_spend_counterparty_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<OnchainRequest>, Option<(Txid, Vec<TxOut>)>) where L::Target: Logger {
15641543
let htlc_txid = tx.txid();
15651544
if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {
15661545
return (Vec::new(), None)
@@ -1581,11 +1560,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15811560

15821561
log_trace!(logger, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
15831562
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, InputDescriptors::RevokedOutput, htlc_txid, 0, tx.output[0].value, None, self.counterparty_tx_cache.on_counterparty_tx_csv);
1584-
let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, aggregable: true, package_template: malleable_justice_tx });
1563+
let claimable_outpoints = vec!(OnchainRequest { aggregation: true, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, height_original: height, content: malleable_justice_tx });
15851564
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
15861565
}
15871566

1588-
fn broadcast_by_holder_state(&self, commitment_tx: &Transaction, holder_tx: &HolderSignedTx) -> (Vec<ClaimRequest>, Vec<TxOut>, Option<(Script, PublicKey, PublicKey)>) {
1567+
fn broadcast_by_holder_state(&self, commitment_tx: &Transaction, holder_tx: &HolderSignedTx, height: u32) -> (Vec<OnchainRequest>, Vec<TxOut>, Option<(Script, PublicKey, PublicKey)>) {
15891568
let mut claim_requests = Vec::with_capacity(holder_tx.htlc_outputs.len());
15901569
let mut watch_outputs = Vec::with_capacity(holder_tx.htlc_outputs.len());
15911570

@@ -1602,7 +1581,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16021581
continue;
16031582
}
16041583
} else { None }, htlc.amount_msat, holder_tx.txid, transaction_output_index);
1605-
claim_requests.push(ClaimRequest { absolute_timelock: ::std::u32::MAX, aggregable: false, package_template: holder_htlc_tx });
1584+
claim_requests.push(OnchainRequest { aggregation: false, bump_strategy: BumpStrategy::CPFP, feerate_previous: 0, height_timer: None, absolute_timelock: height, height_original: height, content: holder_htlc_tx });
16061585
watch_outputs.push(commitment_tx.output[transaction_output_index as usize].clone());
16071586
}
16081587
}
@@ -1613,7 +1592,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16131592
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
16141593
/// revoked using data in holder_claimable_outpoints.
16151594
/// Should not be used if check_spend_revoked_transaction succeeds.
1616-
fn check_spend_holder_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<ClaimRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
1595+
fn check_spend_holder_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<OnchainRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
16171596
let commitment_txid = tx.txid();
16181597
let mut claim_requests = Vec::new();
16191598
let mut watch_outputs = Vec::new();
@@ -1655,13 +1634,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16551634
if self.current_holder_commitment_tx.txid == commitment_txid {
16561635
is_holder_tx = true;
16571636
log_trace!(logger, "Got latest holder commitment tx broadcast, searching for available HTLCs to claim");
1658-
let mut res = self.broadcast_by_holder_state(tx, &self.current_holder_commitment_tx);
1637+
let mut res = self.broadcast_by_holder_state(tx, &self.current_holder_commitment_tx, height);
16591638
append_onchain_update!(res);
16601639
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
16611640
if holder_tx.txid == commitment_txid {
16621641
is_holder_tx = true;
16631642
log_trace!(logger, "Got previous holder commitment tx broadcast, searching for available HTLCs to claim");
1664-
let mut res = self.broadcast_by_holder_state(tx, holder_tx);
1643+
let mut res = self.broadcast_by_holder_state(tx, holder_tx, height);
16651644
append_onchain_update!(res);
16661645
}
16671646
}
@@ -1816,13 +1795,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18161795
let should_broadcast = self.would_broadcast_at_height(height, &logger);
18171796
if should_broadcast {
18181797
let holder_commitment_tx = PackageTemplate::build_holder_commitment_tx(self.funding_redeemscript.clone(), self.funding_info.0.txid.clone(), self.funding_info.0.index as u32);
1819-
claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, package_template: holder_commitment_tx });
1798+
claimable_outpoints.push(OnchainRequest { aggregation: false, bump_strategy: BumpStrategy::CPFP, feerate_previous: 0, height_timer: None, absolute_timelock: height, height_original: height, content: holder_commitment_tx });
18201799
}
18211800
if should_broadcast {
18221801
self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
18231802
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript) {
18241803
self.holder_tx_signed = true;
1825-
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_holder_state(&commitment_tx, &self.current_holder_commitment_tx);
1804+
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_holder_state(&commitment_tx, &self.current_holder_commitment_tx, height);
18261805
if !new_outputs.is_empty() {
18271806
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
18281807
}

0 commit comments

Comments
 (0)