Skip to content

Commit db80067

Browse files
committed
Avoid cloning RBF state when we just want to modify fields.
1 parent 9d74fff commit db80067

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use util::logger::Logger;
4141
use util::ser::{ReadableArgs, Readable, Writer, Writeable, WriterWriteAdaptor, U48};
4242
use util::{byte_utils, events};
4343

44-
use std::collections::{HashMap, hash_map};
44+
use std::collections::{HashMap, hash_map, HashSet};
4545
use std::sync::{Arc,Mutex};
4646
use std::{hash,cmp, mem};
4747

@@ -2330,7 +2330,7 @@ impl ChannelMonitor {
23302330
let mut watch_outputs = Vec::new();
23312331
let mut spendable_outputs = Vec::new();
23322332
let mut htlc_updated = Vec::new();
2333-
let mut bump_candidates = HashMap::new();
2333+
let mut bump_candidates = HashSet::new();
23342334
for tx in txn_matched {
23352335
if tx.input.len() == 1 {
23362336
// Assuming our keys were not leaked (in which case we're screwed no matter what),
@@ -2396,9 +2396,9 @@ impl ChannelMonitor {
23962396
// Scan all input to verify is one of the outpoint spent is of interest for us
23972397
let mut claimed_outputs_material = Vec::new();
23982398
for inp in &tx.input {
2399-
if let Some(ancestor_claimable_txid) = self.claimable_outpoints.get(&inp.previous_output) {
2399+
if let Some(first_claim_txid_height) = self.claimable_outpoints.get(&inp.previous_output) {
24002400
// If outpoint has claim request pending on it...
2401-
if let Some(claim_material) = self.pending_claim_requests.get_mut(&ancestor_claimable_txid.0) {
2401+
if let Some(claim_material) = self.pending_claim_requests.get_mut(&first_claim_txid_height.0) {
24022402
//... we need to verify equality between transaction outpoints and claim request
24032403
// outpoints to know if transaction is the original claim or a bumped one issued
24042404
// by us.
@@ -2417,7 +2417,7 @@ impl ChannelMonitor {
24172417
// before we could anyway), wait for ANTI_REORG_DELAY and clean the RBF
24182418
// tracking map.
24192419
if set_equality {
2420-
let new_event = OnchainEvent::Claim { claim_request: ancestor_claimable_txid.0.clone() };
2420+
let new_event = OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() };
24212421
match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
24222422
hash_map::Entry::Occupied(mut entry) => {
24232423
if !entry.get().contains(&new_event) {
@@ -2435,7 +2435,7 @@ impl ChannelMonitor {
24352435
}
24362436
}
24372437
//TODO: recompute soonest_timelock to avoid wasting a bit on fees
2438-
bump_candidates.insert(ancestor_claimable_txid.0.clone(), claim_material.clone());
2438+
bump_candidates.insert(first_claim_txid_height.0.clone());
24392439
}
24402440
break; //No need to iterate further, either tx is our or their
24412441
} else {
@@ -2509,23 +2509,26 @@ impl ChannelMonitor {
25092509
}
25102510
}
25112511
}
2512-
for (ancestor_claim_txid, ref mut cached_claim_datas) in self.pending_claim_requests.iter_mut() {
2512+
for (first_claim_txid, ref mut cached_claim_datas) in self.pending_claim_requests.iter_mut() {
25132513
if cached_claim_datas.height_timer == height {
2514-
if let hash_map::Entry::Vacant(entry) = bump_candidates.entry(ancestor_claim_txid.clone()) {
2515-
entry.insert(cached_claim_datas.clone());
2516-
}
2517-
}
2518-
}
2519-
for ref mut cached_claim_datas in bump_candidates.values_mut() {
2520-
if let Some((new_timer, new_feerate, bump_tx)) = self.bump_claim_tx(height, &cached_claim_datas, fee_estimator) {
2521-
cached_claim_datas.height_timer = new_timer;
2522-
cached_claim_datas.feerate_previous = new_feerate;
2523-
broadcaster.broadcast_transaction(&bump_tx);
2514+
bump_candidates.insert(first_claim_txid.clone());
2515+
}
2516+
}
2517+
for first_claim_txid in bump_candidates.iter() {
2518+
if let Some((new_timer, new_feerate)) = {
2519+
if let Some(claim_material) = self.pending_claim_requests.get(first_claim_txid) {
2520+
if let Some((new_timer, new_feerate, bump_tx)) = self.bump_claim_tx(height, &claim_material, fee_estimator) {
2521+
broadcaster.broadcast_transaction(&bump_tx);
2522+
Some((new_timer, new_feerate))
2523+
} else { None }
2524+
} else { unreachable!(); }
2525+
} {
2526+
if let Some(claim_material) = self.pending_claim_requests.get_mut(first_claim_txid) {
2527+
claim_material.height_timer = new_timer;
2528+
claim_material.feerate_previous = new_feerate;
2529+
} else { unreachable!(); }
25242530
}
25252531
}
2526-
for (ancestor_claim_txid, cached_claim_datas) in bump_candidates.drain() {
2527-
self.pending_claim_requests.insert(ancestor_claim_txid, cached_claim_datas);
2528-
}
25292532
self.last_block_hash = block_hash.clone();
25302533
(watch_outputs, spendable_outputs, htlc_updated)
25312534
}

0 commit comments

Comments
 (0)