Skip to content

Commit 39900bb

Browse files
committed
Minimize reads to counterparty_claimable_outpoints
Reads related to current/previous commitment_tx should be explicit and reads related to older commitment_txs should be minimized.
1 parent 1890e80 commit 39900bb

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,8 +2430,8 @@ macro_rules! fail_unbroadcast_htlcs {
24302430
debug_assert_eq!($commitment_tx_confirmed.txid(), $commitment_txid_confirmed);
24312431

24322432
macro_rules! check_htlc_fails {
2433-
($txid: expr, $commitment_tx: expr) => {
2434-
if let Some(ref latest_outpoints) = $self.counterparty_claimable_outpoints.get($txid) {
2433+
($txid: expr, $commitment_tx: expr, $per_commitment_outpoints: expr) => {
2434+
if let Some(ref latest_outpoints) = $per_commitment_outpoints {
24352435
for &(ref htlc, ref source_option) in latest_outpoints.iter() {
24362436
if let &Some(ref source) = source_option {
24372437
// Check if the HTLC is present in the commitment transaction that was
@@ -2491,10 +2491,10 @@ macro_rules! fail_unbroadcast_htlcs {
24912491
}
24922492
}
24932493
if let Some(ref txid) = $self.current_counterparty_commitment_txid {
2494-
check_htlc_fails!(txid, "current");
2494+
check_htlc_fails!(txid, "current", $self.counterparty_claimable_outpoints.get(txid));
24952495
}
24962496
if let Some(ref txid) = $self.prev_counterparty_commitment_txid {
2497-
check_htlc_fails!(txid, "previous");
2497+
check_htlc_fails!(txid, "previous", $self.counterparty_claimable_outpoints.get(txid));
24982498
}
24992499
} }
25002500
}
@@ -2763,15 +2763,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27632763
// If the channel is force closed, try to claim the output from this preimage.
27642764
// First check if a counterparty commitment transaction has been broadcasted:
27652765
macro_rules! claim_htlcs {
2766-
($commitment_number: expr, $txid: expr) => {
2767-
let (htlc_claim_reqs, _) = self.get_counterparty_output_claim_info($commitment_number, $txid, None);
2766+
($commitment_number: expr, $txid: expr, $htlcs: expr) => {
2767+
let (htlc_claim_reqs, _) = self.get_counterparty_output_claim_info($commitment_number, $txid, None, $htlcs);
27682768
self.onchain_tx_handler.update_claims_view_from_requests(htlc_claim_reqs, self.best_block.height, self.best_block.height, broadcaster, fee_estimator, logger);
27692769
}
27702770
}
27712771
if let Some(txid) = self.current_counterparty_commitment_txid {
27722772
if txid == confirmed_spend_txid {
27732773
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2774-
claim_htlcs!(*commitment_number, txid);
2774+
claim_htlcs!(*commitment_number, txid, self.counterparty_claimable_outpoints.get(&txid));
27752775
} else {
27762776
debug_assert!(false);
27772777
log_error!(logger, "Detected counterparty commitment tx on-chain without tracking commitment number");
@@ -2782,7 +2782,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27822782
if let Some(txid) = self.prev_counterparty_commitment_txid {
27832783
if txid == confirmed_spend_txid {
27842784
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2785-
claim_htlcs!(*commitment_number, txid);
2785+
claim_htlcs!(*commitment_number, txid, self.counterparty_claimable_outpoints.get(&txid));
27862786
} else {
27872787
debug_assert!(false);
27882788
log_error!(logger, "Detected counterparty commitment tx on-chain without tracking commitment number");
@@ -3230,7 +3230,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32303230
/// Attempts to claim a counterparty commitment transaction's outputs using the revocation key and
32313231
/// data in counterparty_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
32323232
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
3233-
/// HTLC-Success/HTLC-Timeout transactions.
3233+
/// HTLC-Success/HTLC-Timeout transactions. This function should only be called when claimable HTLC
3234+
/// data is available in `counterparty_claimable_outpoints`.
32343235
///
32353236
/// Returns packages to claim the revoked output(s), as well as additional outputs to watch and
32363237
/// general information about the output that is to the counterparty in the commitment
@@ -3279,8 +3280,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32793280
}
32803281

32813282
// Then, try to find revoked htlc outputs
3282-
if let Some(ref per_commitment_data) = per_commitment_option {
3283-
for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
3283+
if let Some(per_commitment_claimable_data) = per_commitment_option {
3284+
for (htlc, _) in per_commitment_claimable_data {
32843285
if let Some(transaction_output_index) = htlc.transaction_output_index {
32853286
if transaction_output_index as usize >= tx.output.len() ||
32863287
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
@@ -3304,9 +3305,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33043305
}
33053306
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
33063307

3307-
if let Some(per_commitment_data) = per_commitment_option {
3308+
if let Some(per_commitment_claimable_data) = per_commitment_option {
33083309
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, tx, height,
3309-
block_hash, per_commitment_data.iter().map(|(htlc, htlc_source)|
3310+
block_hash, per_commitment_claimable_data.iter().map(|(htlc, htlc_source)|
33103311
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
33113312
), logger);
33123313
} else {
@@ -3319,7 +3320,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33193320
block_hash, [].iter().map(|reference| *reference), logger);
33203321
}
33213322
}
3322-
} else if let Some(per_commitment_data) = per_commitment_option {
3323+
} else if let Some(per_commitment_claimable_data) = per_commitment_option {
33233324
// While this isn't useful yet, there is a potential race where if a counterparty
33243325
// revokes a state at the same time as the commitment transaction for that state is
33253326
// confirmed, and the watchtower receives the block before the user, the user could
@@ -3334,12 +3335,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33343335

33353336
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
33363337
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, tx, height, block_hash,
3337-
per_commitment_data.iter().map(|(htlc, htlc_source)|
3338+
per_commitment_claimable_data.iter().map(|(htlc, htlc_source)|
33383339
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
33393340
), logger);
3340-
33413341
let (htlc_claim_reqs, counterparty_output_info) =
3342-
self.get_counterparty_output_claim_info(commitment_number, commitment_txid, Some(tx));
3342+
self.get_counterparty_output_claim_info(commitment_number, commitment_txid, Some(tx), per_commitment_option);
33433343
to_counterparty_output_info = counterparty_output_info;
33443344
for req in htlc_claim_reqs {
33453345
claimable_outpoints.push(req);
@@ -3350,12 +3350,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33503350
}
33513351

33523352
/// Returns the HTLC claim package templates and the counterparty output info
3353-
fn get_counterparty_output_claim_info(&self, commitment_number: u64, commitment_txid: Txid, tx: Option<&Transaction>)
3353+
fn get_counterparty_output_claim_info(&self, commitment_number: u64, commitment_txid: Txid, tx: Option<&Transaction>, per_commitment_option: Option<&Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>)
33543354
-> (Vec<PackageTemplate>, CommitmentTxCounterpartyOutputInfo) {
33553355
let mut claimable_outpoints = Vec::new();
33563356
let mut to_counterparty_output_info: CommitmentTxCounterpartyOutputInfo = None;
33573357

3358-
let htlc_outputs = match self.counterparty_claimable_outpoints.get(&commitment_txid) {
3358+
let per_commitment_claimable_data = match per_commitment_option {
33593359
Some(outputs) => outputs,
33603360
None => return (claimable_outpoints, to_counterparty_output_info),
33613361
};
@@ -3394,7 +3394,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33943394
}
33953395
}
33963396

3397-
for (_, &(ref htlc, _)) in htlc_outputs.iter().enumerate() {
3397+
for &(ref htlc, _) in per_commitment_claimable_data.iter() {
33983398
if let Some(transaction_output_index) = htlc.transaction_output_index {
33993399
if let Some(transaction) = tx {
34003400
if transaction_output_index as usize >= transaction.output.len() ||
@@ -3584,6 +3584,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35843584
if counterparty_commitment_txid == confirmed_commitment_txid {
35853585
continue;
35863586
}
3587+
// If we have generated claims for counterparty_commitment_txid earlier, we can rely on always
3588+
// having claim related htlcs for counterparty_commitment_txid in counterparty_claimable_outpoints.
35873589
for (htlc, _) in self.counterparty_claimable_outpoints.get(counterparty_commitment_txid).unwrap_or(&vec![]) {
35883590
log_trace!(logger, "Canceling claims for previously confirmed counterparty commitment {}",
35893591
counterparty_commitment_txid);
@@ -4219,9 +4221,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42194221
}
42204222

42214223
macro_rules! check_htlc_valid_counterparty {
4222-
($counterparty_txid: expr, $htlc_output: expr) => {
4223-
if let Some(txid) = $counterparty_txid {
4224-
for &(ref pending_htlc, ref pending_source) in self.counterparty_claimable_outpoints.get(&txid).unwrap() {
4224+
($htlc_output: expr, $per_commitment_data: expr) => {
4225+
for &(ref pending_htlc, ref pending_source) in $per_commitment_data {
42254226
if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
42264227
if let &Some(ref source) = pending_source {
42274228
log_claim!("revoked counterparty commitment tx", false, pending_htlc, true);
@@ -4230,7 +4231,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42304231
}
42314232
}
42324233
}
4233-
}
42344234
}
42354235
}
42364236

@@ -4247,9 +4247,13 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42474247
// resolve the source HTLC with the original sender.
42484248
payment_data = Some(((*source).clone(), htlc_output.payment_hash, htlc_output.amount_msat));
42494249
} else if !$holder_tx {
4250-
check_htlc_valid_counterparty!(self.current_counterparty_commitment_txid, htlc_output);
4250+
if let Some(current_counterparty_commitment_txid) = &self.current_counterparty_commitment_txid {
4251+
check_htlc_valid_counterparty!(htlc_output, self.counterparty_claimable_outpoints.get(current_counterparty_commitment_txid).unwrap());
4252+
}
42514253
if payment_data.is_none() {
4252-
check_htlc_valid_counterparty!(self.prev_counterparty_commitment_txid, htlc_output);
4254+
if let Some(prev_counterparty_commitment_txid) = &self.prev_counterparty_commitment_txid {
4255+
check_htlc_valid_counterparty!(htlc_output, self.counterparty_claimable_outpoints.get(prev_counterparty_commitment_txid).unwrap());
4256+
}
42534257
}
42544258
}
42554259
if payment_data.is_none() {
@@ -4287,7 +4291,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42874291
}
42884292
}
42894293
if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(&input.previous_output.txid) {
4290-
scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
4294+
scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, b.as_ref().map(|boxed| &**boxed))),
42914295
"counterparty commitment tx", false);
42924296
}
42934297

0 commit comments

Comments
 (0)