Skip to content

Commit 4815164

Browse files
committed
Convert BumpTransactionEventHandler to async
1 parent 493b686 commit 4815164

File tree

8 files changed

+157
-142
lines changed

8 files changed

+157
-142
lines changed

lightning/Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::sign::{
3030
};
3131
use crate::sign::ecdsa::EcdsaChannelSigner;
3232
use crate::sync::Mutex;
33+
use crate::util::async_poll::AsyncResult;
3334
use crate::util::logger::Logger;
3435

3536
use bitcoin::{OutPoint, Psbt, PubkeyHash, Sequence, ScriptBuf, Transaction, TxIn, TxOut, Witness, WPubkeyHash};
@@ -603,7 +604,7 @@ where
603604
/// Handles a [`BumpTransactionEvent::ChannelClose`] event variant by producing a fully-signed
604605
/// transaction spending an anchor output of the commitment transaction to bump its fee and
605606
/// broadcasts them to the network as a package.
606-
fn handle_channel_close(
607+
async fn handle_channel_close(
607608
&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
608609
commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor,
609610
) -> Result<(), ()> {
@@ -714,7 +715,7 @@ where
714715

715716
/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
716717
/// fully-signed, fee-bumped HTLC transaction that is broadcast to the network.
717-
fn handle_htlc_resolution(
718+
async fn handle_htlc_resolution(
718719
&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
719720
htlc_descriptors: &[HTLCDescriptor], tx_lock_time: LockTime,
720721
) -> Result<(), ()> {
@@ -818,7 +819,7 @@ where
818819
}
819820

820821
/// Handles all variants of [`BumpTransactionEvent`].
821-
pub fn handle_event(&self, event: &BumpTransactionEvent) {
822+
pub async fn handle_event(&self, event: &BumpTransactionEvent) {
822823
match event {
823824
BumpTransactionEvent::ChannelClose {
824825
claim_id, package_target_feerate_sat_per_1000_weight, commitment_tx,
@@ -829,7 +830,7 @@ where
829830
if let Err(_) = self.handle_channel_close(
830831
*claim_id, *package_target_feerate_sat_per_1000_weight, commitment_tx,
831832
*commitment_tx_fee_satoshis, anchor_descriptor,
832-
) {
833+
).await {
833834
log_error!(self.logger, "Failed bumping commitment transaction fee for {}",
834835
commitment_tx.compute_txid());
835836
}
@@ -841,7 +842,7 @@ where
841842
log_bytes!(claim_id.0), log_iter!(htlc_descriptors.iter().map(|d| d.outpoint())));
842843
if let Err(_) = self.handle_htlc_resolution(
843844
*claim_id, *target_feerate_sat_per_1000_weight, htlc_descriptors, *tx_lock_time,
844-
) {
845+
).await {
845846
log_error!(self.logger, "Failed bumping HTLC transaction fee for commitment {}",
846847
htlc_descriptors[0].commitment_txid);
847848
}
@@ -903,8 +904,8 @@ mod tests {
903904
}
904905
}
905906

906-
#[test]
907-
fn test_op_return_under_funds() {
907+
#[tokio::test]
908+
async fn test_op_return_under_funds() {
908909
// Test what happens if we have to select coins but the anchor output value itself suffices
909910
// to pay the required fee.
910911
//
@@ -964,6 +965,6 @@ mod tests {
964965
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
965966
},
966967
pending_htlcs: Vec::new(),
967-
});
968+
}).await;
968969
}
969970
}

lightning/src/ln/async_signer_tests.rs

Lines changed: 15 additions & 15 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 12 additions & 14 deletions
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)