From: Wilmer Paulino Date: Mon, 28 Nov 2022 15:47:44 +0000 (-0800) Subject: Introduce internal package ID to track pending claims X-Git-Tag: v0.0.113~12^2~5 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=68741263ae52c7d9032d72bd3840efac47b537eb;p=rust-lightning Introduce internal package ID to track pending claims Now that our txids will no longer be stable for package claims that require external funds to be allocated, we transition to a 32-byte array identifier to remain compatible with them. --- diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index a0850dfb2..8dcfea347 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -198,6 +198,9 @@ pub(crate) enum OnchainClaim { Event(ClaimEvent), } +/// An internal identifier to track pending package claims within the `OnchainTxHandler`. +type PackageID = [u8; 32]; + /// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and /// do RBF bumping if possible. pub struct OnchainTxHandler { @@ -225,11 +228,11 @@ pub struct OnchainTxHandler { // us and is immutable until all outpoint of the claimable set are post-anti-reorg-delay solved. // Entry is cache of elements need to generate a bumped claiming transaction (see ClaimTxBumpMaterial) #[cfg(test)] // Used in functional_test to verify sanitization - pub(crate) pending_claim_requests: HashMap, + pub(crate) pending_claim_requests: HashMap, #[cfg(not(test))] - pending_claim_requests: HashMap, + pending_claim_requests: HashMap, #[cfg(anchors)] - pending_claim_events: HashMap, + pending_claim_events: HashMap, // Used to link outpoints claimed in a connected block to a pending claim request. // Key is outpoint than monitor parsing has detected we have keys/scripts to claim @@ -238,9 +241,9 @@ pub struct OnchainTxHandler { // post-anti-reorg-delay solved, confirmaiton_block is used to erase entry if // block with output gets disconnected. #[cfg(test)] // Used in functional_test to verify sanitization - pub claimable_outpoints: HashMap, + pub claimable_outpoints: HashMap, #[cfg(not(test))] - claimable_outpoints: HashMap, + claimable_outpoints: HashMap, locktimed_packages: BTreeMap>, @@ -462,7 +465,7 @@ impl OnchainTxHandler { // since requests can have outpoints split off. if !self.onchain_events_awaiting_threshold_conf.iter() .any(|event_entry| if let OnchainEvent::Claim { claim_request } = event_entry.event { - first_claim_txid_height.0 == claim_request + first_claim_txid_height.0 == claim_request.into_inner() } else { // The onchain event is not a claim, keep seeking until we find one. false @@ -628,11 +631,11 @@ impl OnchainTxHandler { if let Some((new_timer, new_feerate, claim)) = self.generate_claim(cur_height, &req, &*fee_estimator, &*logger) { req.set_timer(new_timer); req.set_feerate(new_feerate); - let txid = match claim { + let package_id = match claim { OnchainClaim::Tx(tx) => { log_info!(logger, "Broadcasting onchain {}", log_tx!(tx)); broadcaster.broadcast_transaction(&tx); - tx.txid() + tx.txid().into_inner() }, #[cfg(anchors)] OnchainClaim::Event(claim_event) => { @@ -640,15 +643,16 @@ impl OnchainTxHandler { let txid = match claim_event { ClaimEvent::BumpCommitment { ref commitment_tx, .. } => commitment_tx.txid(), }; - self.pending_claim_events.insert(txid, claim_event); - txid + let package_id = txid.into_inner(); + self.pending_claim_events.insert(package_id, claim_event); + package_id }, }; for k in req.outpoints() { log_info!(logger, "Registering claiming request for {}:{}", k.txid, k.vout); - self.claimable_outpoints.insert(k.clone(), (txid, conf_height)); + self.claimable_outpoints.insert(k.clone(), (package_id, conf_height)); } - self.pending_claim_requests.insert(txid, req); + self.pending_claim_requests.insert(package_id, req); } } } @@ -698,7 +702,7 @@ impl OnchainTxHandler { txid: tx.txid(), height: conf_height, block_hash: Some(conf_hash), - event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() } + event: OnchainEvent::Claim { claim_request: Txid::from_inner(first_claim_txid_height.0) } }; if !self.onchain_events_awaiting_threshold_conf.contains(&entry) { self.onchain_events_awaiting_threshold_conf.push(entry); @@ -754,14 +758,15 @@ impl OnchainTxHandler { if entry.has_reached_confirmation_threshold(cur_height) { match entry.event { OnchainEvent::Claim { claim_request } => { + let package_id = claim_request.into_inner(); // We may remove a whole set of claim outpoints here, as these one may have // been aggregated in a single tx and claimed so atomically - if let Some(request) = self.pending_claim_requests.remove(&claim_request) { + if let Some(request) = self.pending_claim_requests.remove(&package_id) { for outpoint in request.outpoints() { log_debug!(logger, "Removing claim tracking for {} due to maturation of claim tx {}.", outpoint, claim_request); self.claimable_outpoints.remove(&outpoint); #[cfg(anchors)] - self.pending_claim_events.remove(&claim_request); + self.pending_claim_events.remove(&package_id); } } },