Skip to content

Commit f64039a

Browse files
committed
Convert BumpTransactionEventHandler to async
1 parent ea25b4b commit f64039a

File tree

6 files changed

+152
-142
lines changed

6 files changed

+152
-142
lines changed

lightning/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ regex = "1.5.6"
5454
lightning-types = { version = "0.3.0", path = "../lightning-types", features = ["_test_utils"] }
5555
lightning-macros = { path = "../lightning-macros" }
5656
parking_lot = { version = "0.12", default-features = false }
57+
tokio = { version = "1.35", features = [ "macros", "rt" ] }
5758

5859
[dev-dependencies.bitcoin]
5960
version = "0.32.2"

lightning/src/events/bump_transaction.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ where
603603
/// Handles a [`BumpTransactionEvent::ChannelClose`] event variant by producing a fully-signed
604604
/// transaction spending an anchor output of the commitment transaction to bump its fee and
605605
/// broadcasts them to the network as a package.
606-
fn handle_channel_close(
606+
async fn handle_channel_close(
607607
&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
608608
commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor,
609609
) -> Result<(), ()> {
@@ -714,7 +714,7 @@ where
714714

715715
/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
716716
/// fully-signed, fee-bumped HTLC transaction that is broadcast to the network.
717-
fn handle_htlc_resolution(
717+
async fn handle_htlc_resolution(
718718
&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
719719
htlc_descriptors: &[HTLCDescriptor], tx_lock_time: LockTime,
720720
) -> Result<(), ()> {
@@ -818,7 +818,7 @@ where
818818
}
819819

820820
/// Handles all variants of [`BumpTransactionEvent`].
821-
pub fn handle_event(&self, event: &BumpTransactionEvent) {
821+
pub async fn handle_event(&self, event: &BumpTransactionEvent) {
822822
match event {
823823
BumpTransactionEvent::ChannelClose {
824824
claim_id, package_target_feerate_sat_per_1000_weight, commitment_tx,
@@ -829,7 +829,7 @@ where
829829
if let Err(_) = self.handle_channel_close(
830830
*claim_id, *package_target_feerate_sat_per_1000_weight, commitment_tx,
831831
*commitment_tx_fee_satoshis, anchor_descriptor,
832-
) {
832+
).await {
833833
log_error!(self.logger, "Failed bumping commitment transaction fee for {}",
834834
commitment_tx.compute_txid());
835835
}
@@ -841,7 +841,7 @@ where
841841
log_bytes!(claim_id.0), log_iter!(htlc_descriptors.iter().map(|d| d.outpoint())));
842842
if let Err(_) = self.handle_htlc_resolution(
843843
*claim_id, *target_feerate_sat_per_1000_weight, htlc_descriptors, *tx_lock_time,
844-
) {
844+
).await {
845845
log_error!(self.logger, "Failed bumping HTLC transaction fee for commitment {}",
846846
htlc_descriptors[0].commitment_txid);
847847
}
@@ -903,8 +903,8 @@ mod tests {
903903
}
904904
}
905905

906-
#[test]
907-
fn test_op_return_under_funds() {
906+
#[tokio::test]
907+
async fn test_op_return_under_funds() {
908908
// Test what happens if we have to select coins but the anchor output value itself suffices
909909
// to pay the required fee.
910910
//
@@ -964,6 +964,6 @@ mod tests {
964964
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
965965
},
966966
pending_htlcs: Vec::new(),
967-
});
967+
}).await;
968968
}
969969
}

lightning/src/ln/async_signer_tests.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool) {
805805
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
806806
}
807807

808-
fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
808+
async fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
809809
// Ensures that we can obtain holder signatures for commitment and HTLC transactions
810810
// asynchronously by allowing their retrieval to fail and retrying via
811811
// `ChannelMonitor::signer_unblocked`.
@@ -909,7 +909,7 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
909909

910910
// No HTLC transaction should be broadcast as the signer is not available yet.
911911
if anchors && !remote_commitment {
912-
handle_bump_htlc_event(&nodes[0], 1);
912+
handle_bump_htlc_event(&nodes[0], 1).await;
913913
}
914914
let txn = nodes[0].tx_broadcaster.txn_broadcast();
915915
assert!(txn.is_empty(), "expected no transaction to be broadcast, got {:?}", txn);
@@ -920,7 +920,7 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
920920
get_monitor!(nodes[0], chan_id).signer_unblocked(nodes[0].tx_broadcaster, nodes[0].fee_estimator, &nodes[0].logger);
921921

922922
if anchors && !remote_commitment {
923-
handle_bump_htlc_event(&nodes[0], 1);
923+
handle_bump_htlc_event(&nodes[0], 1).await;
924924
}
925925
{
926926
let txn = nodes[0].tx_broadcaster.txn_broadcast();
@@ -929,24 +929,24 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
929929
}
930930
}
931931

932-
#[test]
933-
fn test_async_holder_signatures_no_anchors() {
934-
do_test_async_holder_signatures(false, false);
932+
#[tokio::test]
933+
async fn test_async_holder_signatures_no_anchors() {
934+
do_test_async_holder_signatures(false, false).await;
935935
}
936936

937-
#[test]
938-
fn test_async_holder_signatures_remote_commitment_no_anchors() {
939-
do_test_async_holder_signatures(false, true);
937+
#[tokio::test]
938+
async fn test_async_holder_signatures_remote_commitment_no_anchors() {
939+
do_test_async_holder_signatures(false, true).await;
940940
}
941941

942-
#[test]
943-
fn test_async_holder_signatures_anchors() {
944-
do_test_async_holder_signatures(true, false);
942+
#[tokio::test]
943+
async fn test_async_holder_signatures_anchors() {
944+
do_test_async_holder_signatures(true, false).await;
945945
}
946946

947-
#[test]
948-
fn test_async_holder_signatures_remote_commitment_anchors() {
949-
do_test_async_holder_signatures(true, true);
947+
#[tokio::test]
948+
async fn test_async_holder_signatures_remote_commitment_anchors() {
949+
do_test_async_holder_signatures(true, true).await;
950950
}
951951

952952
#[test]

lightning/src/ln/functional_test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1840,15 +1840,15 @@ macro_rules! check_closed_event {
18401840
}
18411841
}
18421842

1843-
pub fn handle_bump_htlc_event(node: &Node, count: usize) {
1843+
pub async fn handle_bump_htlc_event<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, count: usize) {
18441844
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
18451845
assert_eq!(events.len(), count);
18461846
for event in events {
18471847
match event {
18481848
Event::BumpTransaction(bump_event) => {
18491849
if let BumpTransactionEvent::HTLCResolution { .. } = &bump_event {}
18501850
else { panic!(); }
1851-
node.bump_tx_handler.handle_event(&bump_event);
1851+
node.bump_tx_handler.handle_event(&bump_event).await;
18521852
},
18531853
_ => panic!(),
18541854
}

lightning/src/ln/functional_tests.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -2956,8 +2956,9 @@ pub fn claim_htlc_outputs() {
29562956
// transaction.
29572957
//
29582958
// This is a regression test for https://github.com/lightningdevkit/rust-lightning/issues/3537.
2959+
#[tokio::test]
29592960
#[xtest(feature = "_externalize_tests")]
2960-
pub fn test_multiple_package_conflicts() {
2961+
pub async fn test_multiple_package_conflicts() {
29612962
let chanmon_cfgs = create_chanmon_cfgs(3);
29622963
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
29632964
let mut user_cfg = test_default_channel_config();
@@ -3106,21 +3107,18 @@ pub fn test_multiple_package_conflicts() {
31063107
check_closed_broadcast!(nodes[2], true);
31073108
check_added_monitors(&nodes[2], 1);
31083109

3109-
let process_bump_event = |node: &Node| {
3110-
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
3111-
assert_eq!(events.len(), 1);
3112-
let bump_event = match &events[0] {
3113-
Event::BumpTransaction(bump_event) => bump_event,
3114-
_ => panic!("Unexepected event"),
3115-
};
3116-
node.bump_tx_handler.handle_event(bump_event);
3117-
3118-
let mut tx = node.tx_broadcaster.txn_broadcast();
3119-
assert_eq!(tx.len(), 1);
3120-
tx.pop().unwrap()
3110+
let events = nodes[2].chain_monitor.chain_monitor.get_and_clear_pending_events();
3111+
assert_eq!(events.len(), 1);
3112+
let bump_event = match &events[0] {
3113+
Event::BumpTransaction(bump_event) => bump_event,
3114+
_ => panic!("Unexpected event"),
31213115
};
3116+
nodes[2].bump_tx_handler.handle_event(bump_event).await;
3117+
3118+
let mut tx = nodes[2].tx_broadcaster.txn_broadcast();
3119+
assert_eq!(tx.len(), 1);
3120+
let conflict_tx = tx.pop().unwrap();
31223121

3123-
let conflict_tx = process_bump_event(&nodes[2]);
31243122
assert_eq!(conflict_tx.input.len(), 3);
31253123
assert_eq!(conflict_tx.input[0].previous_output.txid, node2_commit_tx.compute_txid());
31263124
assert_eq!(conflict_tx.input[1].previous_output.txid, node2_commit_tx.compute_txid());

0 commit comments

Comments
 (0)