Skip to content

Commit 4275b77

Browse files
committed
Avoid cloning RBF state when we just want to modify fields.
1 parent 85c03c1 commit 4275b77

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

@@ -2337,7 +2337,7 @@ impl ChannelMonitor {
23372337
let mut watch_outputs = Vec::new();
23382338
let mut spendable_outputs = Vec::new();
23392339
let mut htlc_updated = Vec::new();
2340-
let mut bump_candidates = HashMap::new();
2340+
let mut bump_candidates = HashSet::new();
23412341
for tx in txn_matched {
23422342
if tx.input.len() == 1 {
23432343
// Assuming our keys were not leaked (in which case we're screwed no matter what),
@@ -2403,9 +2403,9 @@ impl ChannelMonitor {
24032403
// Scan all input to verify is one of the outpoint spent is of interest for us
24042404
let mut claimed_outputs_material = Vec::new();
24052405
for inp in &tx.input {
2406-
if let Some(ancestor_claimable_txid) = self.claimable_outpoints.get(&inp.previous_output) {
2406+
if let Some(first_claim_txid_height) = self.claimable_outpoints.get(&inp.previous_output) {
24072407
// If outpoint has claim request pending on it...
2408-
if let Some(claim_material) = self.pending_claim_requests.get_mut(&ancestor_claimable_txid.0) {
2408+
if let Some(claim_material) = self.pending_claim_requests.get_mut(&first_claim_txid_height.0) {
24092409
//... we need to verify equality between transaction outpoints and claim request
24102410
// outpoints to know if transaction is the original claim or a bumped one issued
24112411
// by us.
@@ -2422,7 +2422,7 @@ impl ChannelMonitor {
24222422

24232423
macro_rules! clean_claim_request_after_safety_delay {
24242424
() => {
2425-
let new_event = OnchainEvent::Claim { claim_request: ancestor_claimable_txid.0.clone() };
2425+
let new_event = OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() };
24262426
match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
24272427
hash_map::Entry::Occupied(mut entry) => {
24282428
if !entry.get().contains(&new_event) {
@@ -2452,7 +2452,7 @@ impl ChannelMonitor {
24522452
}
24532453
}
24542454
//TODO: recompute soonest_timelock to avoid wasting a bit on fees
2455-
bump_candidates.insert(ancestor_claimable_txid.0.clone(), claim_material.clone());
2455+
bump_candidates.insert(first_claim_txid_height.0.clone());
24562456
}
24572457
break; //No need to iterate further, either tx is our or their
24582458
} else {
@@ -2526,23 +2526,26 @@ impl ChannelMonitor {
25262526
}
25272527
}
25282528
}
2529-
for (ancestor_claim_txid, ref mut cached_claim_datas) in self.pending_claim_requests.iter_mut() {
2529+
for (first_claim_txid, ref mut cached_claim_datas) in self.pending_claim_requests.iter_mut() {
25302530
if cached_claim_datas.height_timer == height {
2531-
if let hash_map::Entry::Vacant(entry) = bump_candidates.entry(ancestor_claim_txid.clone()) {
2532-
entry.insert(cached_claim_datas.clone());
2533-
}
2534-
}
2535-
}
2536-
for ref mut cached_claim_datas in bump_candidates.values_mut() {
2537-
if let Some((new_timer, new_feerate, bump_tx)) = self.bump_claim_tx(height, &cached_claim_datas, fee_estimator) {
2538-
cached_claim_datas.height_timer = new_timer;
2539-
cached_claim_datas.feerate_previous = new_feerate;
2540-
broadcaster.broadcast_transaction(&bump_tx);
2531+
bump_candidates.insert(first_claim_txid.clone());
2532+
}
2533+
}
2534+
for first_claim_txid in bump_candidates.iter() {
2535+
if let Some((new_timer, new_feerate)) = {
2536+
if let Some(claim_material) = self.pending_claim_requests.get(first_claim_txid) {
2537+
if let Some((new_timer, new_feerate, bump_tx)) = self.bump_claim_tx(height, &claim_material, fee_estimator) {
2538+
broadcaster.broadcast_transaction(&bump_tx);
2539+
Some((new_timer, new_feerate))
2540+
} else { None }
2541+
} else { unreachable!(); }
2542+
} {
2543+
if let Some(claim_material) = self.pending_claim_requests.get_mut(first_claim_txid) {
2544+
claim_material.height_timer = new_timer;
2545+
claim_material.feerate_previous = new_feerate;
2546+
} else { unreachable!(); }
25412547
}
25422548
}
2543-
for (ancestor_claim_txid, cached_claim_datas) in bump_candidates.drain() {
2544-
self.pending_claim_requests.insert(ancestor_claim_txid, cached_claim_datas);
2545-
}
25462549
self.last_block_hash = block_hash.clone();
25472550
(watch_outputs, spendable_outputs, htlc_updated)
25482551
}

0 commit comments

Comments
 (0)