Skip to content

Commit 0f9fc8a

Browse files
committed
f Split update_claims_view
1 parent 24c5dde commit 0f9fc8a

File tree

2 files changed

+73
-43
lines changed

2 files changed

+73
-43
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21752175
macro_rules! claim_htlcs {
21762176
($commitment_number: expr, $txid: expr) => {
21772177
let (htlc_claim_reqs, _) = self.get_counterparty_output_claim_info($commitment_number, $txid, None);
2178-
self.onchain_tx_handler.update_claims_view(&Vec::new(), htlc_claim_reqs, self.best_block.height(), self.best_block.block_hash(), self.best_block.height(), broadcaster, fee_estimator, logger);
2178+
self.onchain_tx_handler.update_claims_view_from_requests(htlc_claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
21792179
}
21802180
}
21812181
if let Some(txid) = self.current_counterparty_commitment_txid {
@@ -2201,10 +2201,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22012201
// block. Even if not, its a reasonable metric for the bump criteria on the HTLC
22022202
// transactions.
22032203
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, self.best_block.height());
2204-
self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), self.best_block.block_hash(), self.best_block.height(), broadcaster, fee_estimator, logger);
2204+
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
22052205
if let Some(ref tx) = self.prev_holder_signed_commitment_tx {
22062206
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&tx, self.best_block.height());
2207-
self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), self.best_block.block_hash(), self.best_block.height(), broadcaster, fee_estimator, logger);
2207+
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
22082208
}
22092209
}
22102210
}
@@ -2286,15 +2286,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22862286
self.onchain_tx_handler.opt_anchors(),
22872287
);
22882288
let best_block_height = self.best_block.height();
2289-
let best_block_hash = self.best_block.block_hash();
22902289
let commitment_package = PackageTemplate::build_package(
22912290
self.funding_info.0.txid.clone(), self.funding_info.0.index as u32,
22922291
PackageSolvingData::HolderFundingOutput(funding_output),
22932292
best_block_height, false, best_block_height,
22942293
);
2295-
self.onchain_tx_handler.update_claims_view(
2296-
&[], vec![commitment_package], best_block_height, best_block_hash,
2297-
best_block_height, broadcaster, &bounded_fee_estimator, logger,
2294+
self.onchain_tx_handler.update_claims_view_from_requests(
2295+
vec![commitment_package], best_block_height, best_block_height,
2296+
broadcaster, &bounded_fee_estimator, logger,
22982297
);
22992298
}
23002299
} else if !self.holder_tx_signed {
@@ -2931,7 +2930,8 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29312930
/// Update state for new block(s)/transaction(s) confirmed. Note that the caller must update
29322931
/// `self.best_block` before calling if a new best blockchain tip is available. More
29332932
/// concretely, `self.best_block` must never be at a lower height than `conf_height`, avoiding
2934-
/// complexity especially in `OnchainTx::update_claims_view`.
2933+
/// complexity especially in
2934+
/// `OnchainTx::update_claims_view_from_requests`/`OnchainTx::update_claims_view_from_matched_txn`.
29352935
///
29362936
/// `conf_height` should be set to the height at which any new transaction(s)/block(s) were
29372937
/// confirmed at, even if it is not the current best height.
@@ -3054,7 +3054,8 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30543054
}
30553055
}
30563056

3057-
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, conf_height, conf_hash, self.best_block.height(), broadcaster, fee_estimator, logger);
3057+
self.onchain_tx_handler.update_claims_view_from_requests(claimable_outpoints, conf_height, self.best_block.height(), broadcaster, fee_estimator, logger);
3058+
self.onchain_tx_handler.update_claims_view_from_matched_txn(&txn_matched, conf_height, conf_hash, self.best_block.height(), broadcaster, fee_estimator, logger);
30583059

30593060
// Determine new outputs to watch by comparing against previously known outputs to watch,
30603061
// updating the latter in the process.

lightning/src/chain/onchaintx.rs

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,21 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
547547

548548
/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
549549
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
550-
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
551-
/// if we receive a preimage after force-close.
550+
/// Together with `update_claims_view_from_matched_txn` this used to be named
551+
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
552+
/// preimage after force-close.
553+
///
552554
/// `conf_height` represents the height at which the transactions in `txn_matched` were
553555
/// confirmed. This does not need to equal the current blockchain tip height, which should be
554556
/// provided via `cur_height`, however it must never be higher than `cur_height`.
555-
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, conf_hash: BlockHash, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
557+
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(&mut self,
558+
requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B,
559+
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
556560
where B::Target: BroadcasterInterface,
557-
F::Target: FeeEstimator,
558-
L::Target: Logger,
561+
F::Target: FeeEstimator,
562+
L::Target: Logger,
559563
{
560-
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
564+
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
561565
let mut preprocessed_requests = Vec::with_capacity(requests.len());
562566
let mut aggregated_request = None;
563567

@@ -638,6 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
638642
}
639643
}
640644

645+
self.process_onchain_events_awaiting_threshold_conf(cur_height, logger);
646+
}
647+
648+
/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
649+
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
650+
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
651+
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
652+
///
653+
/// `conf_height` represents the height at which the transactions in `txn_matched` were
654+
/// confirmed. This does not need to equal the current blockchain tip height, which should be
655+
/// provided via `cur_height`, however it must never be higher than `cur_height`.
656+
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(&mut self,
657+
txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash, cur_height: u32,
658+
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
659+
where B::Target: BroadcasterInterface,
660+
F::Target: FeeEstimator,
661+
L::Target: Logger,
662+
{
663+
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
641664
let mut bump_candidates = HashMap::new();
642665
for tx in txn_matched {
643666
// Scan all input to verify is one of the outpoint spent is of interest for us
@@ -715,34 +738,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
715738
}
716739
}
717740

718-
// After security delay, either our claim tx got enough confs or outpoint is definetely out of reach
719-
let onchain_events_awaiting_threshold_conf =
720-
self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
721-
for entry in onchain_events_awaiting_threshold_conf {
722-
if entry.has_reached_confirmation_threshold(cur_height) {
723-
match entry.event {
724-
OnchainEvent::Claim { claim_request } => {
725-
// We may remove a whole set of claim outpoints here, as these one may have
726-
// been aggregated in a single tx and claimed so atomically
727-
if let Some(request) = self.pending_claim_requests.remove(&claim_request) {
728-
for outpoint in request.outpoints() {
729-
log_debug!(logger, "Removing claim tracking for {} due to maturation of claim tx {}.", outpoint, claim_request);
730-
self.claimable_outpoints.remove(&outpoint);
731-
#[cfg(anchors)]
732-
self.pending_claim_events.remove(&claim_request);
733-
}
734-
}
735-
},
736-
OnchainEvent::ContentiousOutpoint { package } => {
737-
log_debug!(logger, "Removing claim tracking due to maturation of claim tx for outpoints:");
738-
log_debug!(logger, " {:?}", package.outpoints());
739-
self.claimable_outpoints.remove(&package.outpoints()[0]);
740-
}
741-
}
742-
} else {
743-
self.onchain_events_awaiting_threshold_conf.push(entry);
744-
}
745-
}
741+
self.process_onchain_events_awaiting_threshold_conf(cur_height, logger);
746742

747743
// Check if any pending claim request must be rescheduled
748744
for (first_claim_txid, ref request) in self.pending_claim_requests.iter() {
@@ -776,6 +772,39 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
776772
}
777773
}
778774

775+
fn process_onchain_events_awaiting_threshold_conf<L: Deref>(&mut self, cur_height: u32, logger: &L)
776+
where L::Target: Logger,
777+
{
778+
// After security delay, either our claim tx got enough confs or outpoint is definetely out of reach
779+
let onchain_events_awaiting_threshold_conf =
780+
self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
781+
for entry in onchain_events_awaiting_threshold_conf {
782+
if entry.has_reached_confirmation_threshold(cur_height) {
783+
match entry.event {
784+
OnchainEvent::Claim { claim_request } => {
785+
// We may remove a whole set of claim outpoints here, as these one may have
786+
// been aggregated in a single tx and claimed so atomically
787+
if let Some(request) = self.pending_claim_requests.remove(&claim_request) {
788+
for outpoint in request.outpoints() {
789+
log_debug!(logger, "Removing claim tracking for {} due to maturation of claim tx {}.", outpoint, claim_request);
790+
self.claimable_outpoints.remove(&outpoint);
791+
#[cfg(anchors)]
792+
self.pending_claim_events.remove(&claim_request);
793+
}
794+
}
795+
},
796+
OnchainEvent::ContentiousOutpoint { package } => {
797+
log_debug!(logger, "Removing claim tracking due to maturation of claim tx for outpoints:");
798+
log_debug!(logger, " {:?}", package.outpoints());
799+
self.claimable_outpoints.remove(&package.outpoints()[0]);
800+
}
801+
}
802+
} else {
803+
self.onchain_events_awaiting_threshold_conf.push(entry);
804+
}
805+
}
806+
}
807+
779808
pub(crate) fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
780809
&mut self,
781810
txid: &Txid,

0 commit comments

Comments
 (0)