Skip to content

Commit c264514

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 de46234 commit c264514

File tree

2 files changed

+71
-140
lines changed

2 files changed

+71
-140
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use ln::chan_utils;
3232
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType};
3333
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
3434
use ln::onchaintx::OnchainTxHandler;
35-
use ln::onchain_utils::{InputDescriptors, PackageTemplate};
35+
use ln::onchain_utils::{InputDescriptors, PackageTemplate, OnchainRequest, BumpStrategy};
3636
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
3737
use chain::transaction::OutPoint;
3838
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -428,26 +428,6 @@ impl Readable for RemoteCommitmentTransaction {
428428
}
429429
}
430430

431-
/// ClaimRequest is a descriptor structure to communicate between detection
432-
/// and reaction module. They are generated by ChannelMonitor while parsing
433-
/// onchain txn leaked from a channel and handed over to OnchainTxHandler which
434-
/// is responsible for opportunistic aggregation, selecting and enforcing
435-
/// bumping logic, building and signing transactions.
436-
pub(crate) struct ClaimRequest {
437-
// Block height before which claiming is exclusive to one party,
438-
// after reaching it, claiming may be contentious.
439-
pub(crate) absolute_timelock: u32,
440-
// Timeout tx must have nLocktime set which means aggregating multiple
441-
// ones must take the higher nLocktime among them to satisfy all of them.
442-
// Sadly it has few pitfalls, a) it takes longuer to get fund back b) CLTV_DELTA
443-
// of a sooner-HTLC could be swallowed by the highest nLocktime of the HTLC set.
444-
// Do simplify we mark them as non-aggregable.
445-
pub(crate) aggregable: bool,
446-
// Template (list of outpoint and set of data needed to generate transaction digest
447-
// and satisfy witness program).
448-
pub(crate) package_template: PackageTemplate,
449-
}
450-
451431
/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
452432
/// once they mature to enough confirmations (ANTI_REORG_DELAY)
453433
#[derive(Clone, PartialEq)]
@@ -1351,7 +1331,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13511331
/// HTLC-Success/HTLC-Timeout transactions.
13521332
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
13531333
/// revoked remote commitment tx
1354-
fn check_spend_remote_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<ClaimRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
1334+
fn check_spend_remote_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<OnchainRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
13551335
// Most secp and related errors trying to create keys means we have no hope of constructing
13561336
// a spend transaction...so we return no transactions to broadcast
13571337
let mut claimable_outpoints = Vec::new();
@@ -1384,7 +1364,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13841364
for (idx, outp) in tx.output.iter().enumerate() {
13851365
if outp.script_pubkey == revokeable_p2wsh {
13861366
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.remote_tx_cache.remote_delayed_payment_base_key, self.remote_tx_cache.remote_htlc_base_key, InputDescriptors::RevokedOutput, commitment_txid, idx as u32, outp.value, None, self.remote_tx_cache.on_remote_tx_csv);
1387-
claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, aggregable: true, package_template: malleable_justice_tx});
1367+
claimable_outpoints.push(OnchainRequest { aggregation: true, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, height_original: height, content: malleable_justice_tx});
13881368
}
13891369
}
13901370

@@ -1397,7 +1377,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13971377
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
13981378
}
13991379
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.remote_tx_cache.remote_delayed_payment_base_key, self.remote_tx_cache.remote_htlc_base_key, if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }, commitment_txid, transaction_output_index, htlc.amount_msat / 1000, Some(htlc.clone()), self.remote_tx_cache.on_remote_tx_csv);
1400-
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, package_template: malleable_justice_tx});
1380+
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});
14011381
}
14021382
}
14031383
}
@@ -1529,10 +1509,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15291509
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
15301510
}
15311511
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
1532-
let aggregable = if !htlc.offered { false } else { true };
15331512
if preimage.is_some() || !htlc.offered {
15341513
let remote_htlc_tx = PackageTemplate::build_remote_htlc_tx(*revocation_point, self.remote_tx_cache.remote_delayed_payment_base_key, self.remote_tx_cache.remote_htlc_base_key, preimage, htlc.clone(), commitment_txid, transaction_output_index);
1535-
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, package_template: remote_htlc_tx });
1514+
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: remote_htlc_tx });
15361515
}
15371516
}
15381517
}
@@ -1543,7 +1522,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15431522
}
15441523

15451524
/// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
1546-
fn check_spend_remote_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<ClaimRequest>, Option<(Txid, Vec<TxOut>)>) where L::Target: Logger {
1525+
fn check_spend_remote_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<OnchainRequest>, Option<(Txid, Vec<TxOut>)>) where L::Target: Logger {
15471526
let htlc_txid = tx.txid();
15481527
if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {
15491528
return (Vec::new(), None)
@@ -1564,11 +1543,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15641543

15651544
log_trace!(logger, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
15661545
let malleable_justice_tx = PackageTemplate::build_malleable_justice_tx(per_commitment_point, per_commitment_key, self.remote_tx_cache.remote_delayed_payment_base_key, self.remote_tx_cache.remote_htlc_base_key, InputDescriptors::RevokedOutput, htlc_txid, 0, tx.output[0].value, None, self.remote_tx_cache.on_remote_tx_csv);
1567-
let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, aggregable: true, package_template: malleable_justice_tx });
1546+
let claimable_outpoints = vec!(OnchainRequest { aggregation: true, bump_strategy: BumpStrategy::RBF, feerate_previous: 0, height_timer: None, absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, height_original: height, content: malleable_justice_tx });
15681547
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
15691548
}
15701549

1571-
fn broadcast_by_local_state(&self, commitment_tx: &Transaction, local_tx: &LocalSignedTx) -> (Vec<ClaimRequest>, Vec<TxOut>, Option<(Script, PublicKey, PublicKey)>) {
1550+
fn broadcast_by_local_state(&self, commitment_tx: &Transaction, local_tx: &LocalSignedTx, height: u32) -> (Vec<OnchainRequest>, Vec<TxOut>, Option<(Script, PublicKey, PublicKey)>) {
15721551
let mut claim_requests = Vec::with_capacity(local_tx.htlc_outputs.len());
15731552
let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
15741553

@@ -1585,7 +1564,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15851564
continue;
15861565
}
15871566
} else { None }, htlc.amount_msat, local_tx.txid, transaction_output_index);
1588-
claim_requests.push(ClaimRequest { absolute_timelock: ::std::u32::MAX, aggregable: false, package_template: local_htlc_tx });
1567+
claim_requests.push(OnchainRequest { aggregation: false, bump_strategy: BumpStrategy::CPFP, feerate_previous: 0, height_timer: None, absolute_timelock: height, height_original: height, content: local_htlc_tx });
15891568
watch_outputs.push(commitment_tx.output[transaction_output_index as usize].clone());
15901569
}
15911570
}
@@ -1596,7 +1575,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15961575
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
15971576
/// revoked using data in local_claimable_outpoints.
15981577
/// Should not be used if check_spend_revoked_transaction succeeds.
1599-
fn check_spend_local_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<ClaimRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
1578+
fn check_spend_local_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<OnchainRequest>, (Txid, Vec<TxOut>)) where L::Target: Logger {
16001579
let commitment_txid = tx.txid();
16011580
let mut claim_requests = Vec::new();
16021581
let mut watch_outputs = Vec::new();
@@ -1638,13 +1617,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16381617
if self.current_local_commitment_tx.txid == commitment_txid {
16391618
is_local_tx = true;
16401619
log_trace!(logger, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
1641-
let mut res = self.broadcast_by_local_state(tx, &self.current_local_commitment_tx);
1620+
let mut res = self.broadcast_by_local_state(tx, &self.current_local_commitment_tx, height);
16421621
append_onchain_update!(res);
16431622
} else if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
16441623
if local_tx.txid == commitment_txid {
16451624
is_local_tx = true;
16461625
log_trace!(logger, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
1647-
let mut res = self.broadcast_by_local_state(tx, local_tx);
1626+
let mut res = self.broadcast_by_local_state(tx, local_tx, height);
16481627
append_onchain_update!(res);
16491628
}
16501629
}
@@ -1799,11 +1778,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17991778
let should_broadcast = self.would_broadcast_at_height(height, &logger);
18001779
if should_broadcast {
18011780
let local_commitment_tx = PackageTemplate::build_local_commitment_tx(self.funding_redeemscript.clone(), self.funding_info.0.txid.clone(), self.funding_info.0.index as u32);
1802-
claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, package_template: local_commitment_tx });
1781+
claimable_outpoints.push(OnchainRequest { aggregation: false, bump_strategy: BumpStrategy::CPFP, feerate_previous: 0, height_timer: None, absolute_timelock: height, height_original: height, content: local_commitment_tx });
18031782
}
18041783
if should_broadcast {
18051784
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx(&self.funding_redeemscript) {
1806-
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_local_state(&commitment_tx, &self.current_local_commitment_tx);
1785+
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_local_state(&commitment_tx, &self.current_local_commitment_tx, height);
18071786
if !new_outputs.is_empty() {
18081787
watch_outputs.push((self.current_local_commitment_tx.txid.clone(), new_outputs));
18091788
}

0 commit comments

Comments
 (0)