Skip to content

Commit 8c14ba5

Browse files
committed
Prepare BumpTransactionEventHandler for async conversion
Convert methods that need to be async when the wallet traits become async.
1 parent 4a4680f commit 8c14ba5

File tree

6 files changed

+122
-106
lines changed

6 files changed

+122
-106
lines changed

lightning/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1717
[features]
1818
# Internal test utilities exposed to other repo crates
1919
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "lightning-types/_test_utils"]
20-
_externalize_tests = ["inventory", "_test_utils"]
20+
_externalize_tests = ["inventory", "_test_utils", "tokio"]
2121
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2222
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2323
unsafe_revoked_tx_signing = []
@@ -48,12 +48,14 @@ backtrace = { version = "0.3", optional = true }
4848

4949
libm = { version = "0.2", default-features = false }
5050
inventory = { version = "0.3", optional = true }
51+
tokio = { version = "1.35", features = [ "macros", "rt" ], default-features = false, optional = true }
5152

5253
[dev-dependencies]
5354
regex = "1.5.6"
5455
lightning-types = { version = "0.3.0", path = "../lightning-types", features = ["_test_utils"] }
5556
lightning-macros = { path = "../lightning-macros" }
5657
parking_lot = { version = "0.12", default-features = false }
58+
tokio = { version = "1.35", features = [ "macros", "rt" ], default-features = false }
5759

5860
[dev-dependencies.bitcoin]
5961
version = "0.32.2"

lightning/src/events/bump_transaction.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ where
625625
/// Handles a [`BumpTransactionEvent::ChannelClose`] event variant by producing a fully-signed
626626
/// transaction spending an anchor output of the commitment transaction to bump its fee and
627627
/// broadcasts them to the network as a package.
628-
fn handle_channel_close(
628+
async fn handle_channel_close(
629629
&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
630630
commitment_tx: &Transaction, commitment_tx_fee_sat: u64,
631631
anchor_descriptor: &AnchorDescriptor,
@@ -770,7 +770,7 @@ where
770770

771771
/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
772772
/// fully-signed, fee-bumped HTLC transaction that is broadcast to the network.
773-
fn handle_htlc_resolution(
773+
async fn handle_htlc_resolution(
774774
&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
775775
htlc_descriptors: &[HTLCDescriptor], tx_lock_time: LockTime,
776776
) -> Result<(), ()> {
@@ -899,7 +899,7 @@ where
899899
}
900900

901901
/// Handles all variants of [`BumpTransactionEvent`].
902-
pub fn handle_event(&self, event: &BumpTransactionEvent) {
902+
pub async fn handle_event(&self, event: &BumpTransactionEvent) {
903903
match event {
904904
BumpTransactionEvent::ChannelClose {
905905
claim_id,
@@ -915,19 +915,21 @@ where
915915
log_bytes!(claim_id.0),
916916
commitment_tx.compute_txid()
917917
);
918-
if let Err(_) = self.handle_channel_close(
918+
self.handle_channel_close(
919919
*claim_id,
920920
*package_target_feerate_sat_per_1000_weight,
921921
commitment_tx,
922922
*commitment_tx_fee_satoshis,
923923
anchor_descriptor,
924-
) {
924+
)
925+
.await
926+
.unwrap_or_else(|_| {
925927
log_error!(
926928
self.logger,
927929
"Failed bumping commitment transaction fee for {}",
928930
commitment_tx.compute_txid()
929931
);
930-
}
932+
});
931933
},
932934
BumpTransactionEvent::HTLCResolution {
933935
claim_id,
@@ -942,18 +944,20 @@ where
942944
log_bytes!(claim_id.0),
943945
log_iter!(htlc_descriptors.iter().map(|d| d.outpoint()))
944946
);
945-
if let Err(_) = self.handle_htlc_resolution(
947+
self.handle_htlc_resolution(
946948
*claim_id,
947949
*target_feerate_sat_per_1000_weight,
948950
htlc_descriptors,
949951
*tx_lock_time,
950-
) {
952+
)
953+
.await
954+
.unwrap_or_else(|_| {
951955
log_error!(
952956
self.logger,
953957
"Failed bumping HTLC transaction fee for commitment {}",
954958
htlc_descriptors[0].commitment_txid
955959
);
956-
}
960+
});
957961
},
958962
}
959963
}
@@ -1009,8 +1013,8 @@ mod tests {
10091013
}
10101014
}
10111015

1012-
#[test]
1013-
fn test_op_return_under_funds() {
1016+
#[tokio::test]
1017+
async fn test_op_return_under_funds() {
10141018
// Test what happens if we have to select coins but the anchor output value itself suffices
10151019
// to pay the required fee.
10161020
//
@@ -1069,7 +1073,7 @@ mod tests {
10691073
transaction_parameters.channel_type_features =
10701074
ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
10711075

1072-
handler.handle_event(&BumpTransactionEvent::ChannelClose {
1076+
let channel_close_event = &BumpTransactionEvent::ChannelClose {
10731077
channel_id: ChannelId([42; 32]),
10741078
counterparty_node_id: PublicKey::from_slice(&[2; 33]).unwrap(),
10751079
claim_id: ClaimId([42; 32]),
@@ -1085,6 +1089,7 @@ mod tests {
10851089
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
10861090
},
10871091
pending_htlcs: Vec::new(),
1088-
});
1092+
};
1093+
handler.handle_event(channel_close_event).await;
10891094
}
10901095
}

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
@@ -1846,15 +1846,15 @@ macro_rules! check_closed_event {
18461846
}
18471847
}
18481848

1849-
pub fn handle_bump_htlc_event(node: &Node, count: usize) {
1849+
pub async fn handle_bump_htlc_event<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, count: usize) {
18501850
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
18511851
assert_eq!(events.len(), count);
18521852
for event in events {
18531853
match event {
18541854
Event::BumpTransaction(bump_event) => {
18551855
if let BumpTransactionEvent::HTLCResolution { .. } = &bump_event {}
18561856
else { panic!(); }
1857-
node.bump_tx_handler.handle_event(&bump_event);
1857+
node.bump_tx_handler.handle_event(&bump_event).await;
18581858
},
18591859
_ => panic!(),
18601860
}

lightning/src/ln/functional_tests.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,19 @@ pub fn claim_htlc_outputs() {
14741474
// This is a regression test for https://github.com/lightningdevkit/rust-lightning/issues/3537.
14751475
#[xtest(feature = "_externalize_tests")]
14761476
pub fn test_multiple_package_conflicts() {
1477+
// #[tokio::test] can't be used here because it adds the #[test] attribute, which causes the external test runner to
1478+
// miss this test.
1479+
let body = Box::pin(async {
1480+
test_multiple_package_conflicts_internal().await;
1481+
});
1482+
1483+
return tokio::runtime::Builder::new_current_thread()
1484+
.build()
1485+
.expect("Failed building the Runtime")
1486+
.block_on(body);
1487+
}
1488+
1489+
async fn test_multiple_package_conflicts_internal() {
14771490
let chanmon_cfgs = create_chanmon_cfgs(3);
14781491
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
14791492
let mut user_cfg = test_default_channel_config();
@@ -1626,21 +1639,17 @@ pub fn test_multiple_package_conflicts() {
16261639
check_closed_broadcast!(nodes[2], true);
16271640
check_added_monitors(&nodes[2], 1);
16281641

1629-
let process_bump_event = |node: &Node| {
1630-
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
1631-
assert_eq!(events.len(), 1);
1632-
let bump_event = match &events[0] {
1633-
Event::BumpTransaction(bump_event) => bump_event,
1634-
_ => panic!("Unexepected event"),
1635-
};
1636-
node.bump_tx_handler.handle_event(bump_event);
1637-
1638-
let mut tx = node.tx_broadcaster.txn_broadcast();
1639-
assert_eq!(tx.len(), 1);
1640-
tx.pop().unwrap()
1642+
let events = nodes[2].chain_monitor.chain_monitor.get_and_clear_pending_events();
1643+
assert_eq!(events.len(), 1);
1644+
let bump_event = match &events[0] {
1645+
Event::BumpTransaction(bump_event) => bump_event,
1646+
_ => panic!("Unexpected event"),
16411647
};
1648+
nodes[2].bump_tx_handler.handle_event(bump_event).await;
16421649

1643-
let conflict_tx = process_bump_event(&nodes[2]);
1650+
let mut tx = nodes[2].tx_broadcaster.txn_broadcast();
1651+
assert_eq!(tx.len(), 1);
1652+
let conflict_tx = tx.pop().unwrap();
16441653
assert_eq!(conflict_tx.input.len(), 3);
16451654
assert_eq!(conflict_tx.input[0].previous_output.txid, node2_commit_tx.compute_txid());
16461655
assert_eq!(conflict_tx.input[1].previous_output.txid, node2_commit_tx.compute_txid());

0 commit comments

Comments
 (0)