Skip to content

Commit 04396a8

Browse files
committed
Prepare BumpTransactionEventHandler for async conversion
Convert methods that need to be async when the wallet traits become async.
1 parent 1080696 commit 04396a8

File tree

6 files changed

+138
-113
lines changed

6 files changed

+138
-113
lines changed

lightning/Cargo.toml

+3-1
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

+27-20
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,13 +915,16 @@ where
915915
log_bytes!(claim_id.0),
916916
commitment_tx.compute_txid()
917917
);
918-
if let Err(_) = self.handle_channel_close(
919-
*claim_id,
920-
*package_target_feerate_sat_per_1000_weight,
921-
commitment_tx,
922-
*commitment_tx_fee_satoshis,
923-
anchor_descriptor,
924-
) {
918+
if let Err(_) = self
919+
.handle_channel_close(
920+
*claim_id,
921+
*package_target_feerate_sat_per_1000_weight,
922+
commitment_tx,
923+
*commitment_tx_fee_satoshis,
924+
anchor_descriptor,
925+
)
926+
.await
927+
{
925928
log_error!(
926929
self.logger,
927930
"Failed bumping commitment transaction fee for {}",
@@ -942,12 +945,15 @@ where
942945
log_bytes!(claim_id.0),
943946
log_iter!(htlc_descriptors.iter().map(|d| d.outpoint()))
944947
);
945-
if let Err(_) = self.handle_htlc_resolution(
946-
*claim_id,
947-
*target_feerate_sat_per_1000_weight,
948-
htlc_descriptors,
949-
*tx_lock_time,
950-
) {
948+
if let Err(_) = self
949+
.handle_htlc_resolution(
950+
*claim_id,
951+
*target_feerate_sat_per_1000_weight,
952+
htlc_descriptors,
953+
*tx_lock_time,
954+
)
955+
.await
956+
{
951957
log_error!(
952958
self.logger,
953959
"Failed bumping HTLC transaction fee for commitment {}",
@@ -1009,8 +1015,8 @@ mod tests {
10091015
}
10101016
}
10111017

1012-
#[test]
1013-
fn test_op_return_under_funds() {
1018+
#[tokio::test]
1019+
async fn test_op_return_under_funds() {
10141020
// Test what happens if we have to select coins but the anchor output value itself suffices
10151021
// to pay the required fee.
10161022
//
@@ -1069,7 +1075,7 @@ mod tests {
10691075
transaction_parameters.channel_type_features =
10701076
ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
10711077

1072-
handler.handle_event(&BumpTransactionEvent::ChannelClose {
1078+
let channel_close_event = &BumpTransactionEvent::ChannelClose {
10731079
channel_id: ChannelId([42; 32]),
10741080
counterparty_node_id: PublicKey::from_slice(&[2; 33]).unwrap(),
10751081
claim_id: ClaimId([42; 32]),
@@ -1085,6 +1091,7 @@ mod tests {
10851091
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
10861092
},
10871093
pending_htlcs: Vec::new(),
1088-
});
1094+
};
1095+
handler.handle_event(channel_close_event).await;
10891096
}
10901097
}

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

+29-13
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,26 @@ pub fn claim_htlc_outputs() {
29592959
// This is a regression test for https://github.com/lightningdevkit/rust-lightning/issues/3537.
29602960
#[xtest(feature = "_externalize_tests")]
29612961
pub fn test_multiple_package_conflicts() {
2962+
// Wanted to use #[tokio::test] for this, but that macro adds a test attribute and then the external test runner
2963+
// can't find the test anymore.
2964+
let body = async {
2965+
test_multiple_package_conflicts_internal().await;
2966+
};
2967+
let mut body = body;
2968+
#[allow(unused_mut)]
2969+
let mut body = unsafe { tokio::macros::support::Pin::new_unchecked(&mut body) };
2970+
let body: ::core::pin::Pin<&mut dyn ::core::future::Future<Output = ()>> = body;
2971+
#[allow(clippy::expect_used, clippy::diverging_sub_expression)]
2972+
{
2973+
return tokio::runtime::Builder::new_current_thread()
2974+
.enable_all()
2975+
.build()
2976+
.expect("Failed building the Runtime")
2977+
.block_on(body);
2978+
}
2979+
}
2980+
2981+
async fn test_multiple_package_conflicts_internal() {
29622982
let chanmon_cfgs = create_chanmon_cfgs(3);
29632983
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
29642984
let mut user_cfg = test_default_channel_config();
@@ -3107,21 +3127,17 @@ pub fn test_multiple_package_conflicts() {
31073127
check_closed_broadcast!(nodes[2], true);
31083128
check_added_monitors(&nodes[2], 1);
31093129

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

3124-
let conflict_tx = process_bump_event(&nodes[2]);
3138+
let mut tx = nodes[2].tx_broadcaster.txn_broadcast();
3139+
assert_eq!(tx.len(), 1);
3140+
let conflict_tx = tx.pop().unwrap();
31253141
assert_eq!(conflict_tx.input.len(), 3);
31263142
assert_eq!(conflict_tx.input[0].previous_output.txid, node2_commit_tx.compute_txid());
31273143
assert_eq!(conflict_tx.input[1].previous_output.txid, node2_commit_tx.compute_txid());

0 commit comments

Comments
 (0)