Skip to content

Commit af0bdec

Browse files
committed
Refactor generate_claim_tx to return OnchainClaim enum
1 parent 37949ce commit af0bdec

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

lightning/src/chain/onchaintx.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
162162
}
163163
}
164164

165+
pub(crate) enum OnchainClaim {
166+
Tx(Transaction),
167+
}
165168

166169
/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
167170
/// do RBF bumping if possible.
@@ -378,7 +381,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
378381
/// (CSV or CLTV following cases). In case of high-fee spikes, claim tx may stuck in the mempool, so you need to bump its feerate quickly using Replace-By-Fee or Child-Pay-For-Parent.
379382
/// Panics if there are signing errors, because signing operations in reaction to on-chain events
380383
/// are not expected to fail, and if they do, we may lose funds.
381-
fn generate_claim_tx<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(Option<u32>, u64, Transaction)>
384+
fn generate_claim<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(Option<u32>, u64, OnchainClaim)>
382385
where F::Target: FeeEstimator,
383386
L::Target: Logger,
384387
{
@@ -396,14 +399,14 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
396399
let transaction = cached_request.finalize_malleable_package(self, output_value, self.destination_script.clone(), logger).unwrap();
397400
log_trace!(logger, "...with timer {} and feerate {}", new_timer.unwrap(), new_feerate);
398401
assert!(predicted_weight >= transaction.weight());
399-
return Some((new_timer, new_feerate, transaction))
402+
return Some((new_timer, new_feerate, OnchainClaim::Tx(transaction)))
400403
}
401404
} else {
402405
// Note: Currently, amounts of holder outputs spending witnesses aren't used
403406
// as we can't malleate spending package to increase their feerate. This
404407
// should change with the remaining anchor output patchset.
405408
if let Some(transaction) = cached_request.finalize_untractable_package(self, logger) {
406-
return Some((None, 0, transaction));
409+
return Some((None, 0, OnchainClaim::Tx(transaction)));
407410
}
408411
}
409412
None
@@ -475,17 +478,21 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
475478
// Generate claim transactions and track them to bump if necessary at
476479
// height timer expiration (i.e in how many blocks we're going to take action).
477480
for mut req in preprocessed_requests {
478-
if let Some((new_timer, new_feerate, tx)) = self.generate_claim_tx(cur_height, &req, &*fee_estimator, &*logger) {
481+
if let Some((new_timer, new_feerate, claim)) = self.generate_claim(cur_height, &req, &*fee_estimator, &*logger) {
479482
req.set_timer(new_timer);
480483
req.set_feerate(new_feerate);
481-
let txid = tx.txid();
482-
for k in req.outpoints() {
483-
log_info!(logger, "Registering claiming request for {}:{}", k.txid, k.vout);
484-
self.claimable_outpoints.insert(k.clone(), (txid, conf_height));
484+
match claim {
485+
OnchainClaim::Tx(tx) => {
486+
let txid = tx.txid();
487+
for k in req.outpoints() {
488+
log_info!(logger, "Registering claiming request for {}:{}", k.txid, k.vout);
489+
self.claimable_outpoints.insert(k.clone(), (txid, conf_height));
490+
}
491+
self.pending_claim_requests.insert(txid, req);
492+
log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
493+
broadcaster.broadcast_transaction(&tx);
494+
},
485495
}
486-
self.pending_claim_requests.insert(txid, req);
487-
log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
488-
broadcaster.broadcast_transaction(&tx);
489496
}
490497
}
491498

@@ -603,9 +610,13 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
603610
// Build, bump and rebroadcast tx accordingly
604611
log_trace!(logger, "Bumping {} candidates", bump_candidates.len());
605612
for (first_claim_txid, request) in bump_candidates.iter() {
606-
if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(cur_height, &request, &*fee_estimator, &*logger) {
607-
log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
608-
broadcaster.broadcast_transaction(&bump_tx);
613+
if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(cur_height, &request, &*fee_estimator, &*logger) {
614+
match bump_claim {
615+
OnchainClaim::Tx(bump_tx) => {
616+
log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
617+
broadcaster.broadcast_transaction(&bump_tx);
618+
},
619+
}
609620
if let Some(request) = self.pending_claim_requests.get_mut(first_claim_txid) {
610621
request.set_timer(new_timer);
611622
request.set_feerate(new_feerate);
@@ -668,11 +679,15 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
668679
}
669680
}
670681
for (_, request) in bump_candidates.iter_mut() {
671-
if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(height, &request, fee_estimator, &&*logger) {
682+
if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(height, &request, fee_estimator, &&*logger) {
672683
request.set_timer(new_timer);
673684
request.set_feerate(new_feerate);
674-
log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
675-
broadcaster.broadcast_transaction(&bump_tx);
685+
match bump_claim {
686+
OnchainClaim::Tx(bump_tx) => {
687+
log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
688+
broadcaster.broadcast_transaction(&bump_tx);
689+
},
690+
}
676691
}
677692
}
678693
for (ancestor_claim_txid, request) in bump_candidates.drain() {

0 commit comments

Comments
 (0)