Skip to content

Async BumpTransactionEventHandler #3752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

joostjager
Copy link
Contributor

@joostjager joostjager commented Apr 28, 2025

Converts BumpTransactionEventHandler to async.

Changes the CoinSelectionSource and WalletSource traits to be async and provides WalletSourceSyncWrapper as a helper for users that want to implement a sync wallet source.

TestWalletSource is kept sync, to prevent a cascade of async conversions in tests.

Fixes #3540

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 28, 2025

👋 Thanks for assigning @TheBlueMatt as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@joostjager joostjager force-pushed the async-wallet-bump branch 3 times, most recently from d9b1c75 to 873516b Compare May 1, 2025 11:08
Copy link

codecov bot commented May 1, 2025

Codecov Report

Attention: Patch coverage is 94.13681% with 18 lines in your changes missing coverage. Please review.

Project coverage is 89.78%. Comparing base (98297a6) to head (6fd5e5b).

Files with missing lines Patch % Lines
lightning/src/events/bump_transaction.rs 90.85% 6 Missing and 9 partials ⚠️
lightning/src/ln/functional_tests.rs 96.29% 1 Missing ⚠️
lightning/src/ln/monitor_tests.rs 98.94% 1 Missing ⚠️
lightning/src/util/test_utils.rs 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #3752    +/-   ##
========================================
  Coverage   89.78%   89.78%            
========================================
  Files         159      159            
  Lines      128672   128778   +106     
  Branches   128672   128778   +106     
========================================
+ Hits       115522   115624   +102     
+ Misses      10476    10470     -6     
- Partials     2674     2684    +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joostjager joostjager force-pushed the async-wallet-bump branch 6 times, most recently from 4b4b898 to 3816c66 Compare May 5, 2025 11:37
@joostjager joostjager force-pushed the async-wallet-bump branch 13 times, most recently from 1a86521 to e1509f1 Compare May 14, 2025 09:55
@joostjager joostjager changed the title Async wallet bump Async BumpTransactionEventHandler May 14, 2025
@joostjager joostjager marked this pull request as ready for review May 14, 2025 11:06
@joostjager joostjager requested a review from tnull May 14, 2025 11:06
@joostjager joostjager added the weekly goal Someone wants to land this this week label May 14, 2025
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a first / quickish pass. Generally looks good so far.

@@ -48,12 +48,14 @@ backtrace = { version = "0.3", optional = true }

libm = { version = "0.2", default-features = false }
inventory = { version = "0.3", optional = true }
tokio = { version = "1.35", features = [ "macros", "rt" ], optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set default-features = false here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Why is this? I looked at the tokio crate and it seems there are no default features?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 'best practice' and will also make sure we'd not add arbitrary dependencies if tokio decided to add default features in some release.

*commitment_tx_fee_satoshis,
anchor_descriptor,
) {
if let Err(_) = self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO these would be a bit more readable if we did the the whole call including .await on a separate line and then simply did if res.is_err() { .. }

Copy link
Contributor Author

@joostjager joostjager May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it, but I don't think it is an improvement with that temp var.

let res = self
	.handle_channel_close(
		*claim_id,
		*package_target_feerate_sat_per_1000_weight,
		commitment_tx,
		*commitment_tx_fee_satoshis,
		anchor_descriptor,
	)
	.await;
if res.is_err() {
	log_error!(
		self.logger,
		"Failed bumping commitment transaction fee for {}",
		commitment_tx.compute_txid()
	);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also tried with map_err, but that doesn't work all that well with the parent fn without a return result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also tried with map_err, but that doesn't work all that well with the parent fn without a return result.

How about:

				self.handle_channel_close(
					*claim_id,
					*package_target_feerate_sat_per_1000_weight,
					commitment_tx,
					*commitment_tx_fee_satoshis,
					anchor_descriptor,
				)
				.await
				.unwrap_or_else(|_| {
					log_error!(
						self.logger,
						"Failed bumping commitment transaction fee for {}",
						commitment_tx.compute_txid()
					);
				});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is nicer. Wasn't aware of the unwrap_or_else on result. Updated.

#[allow(clippy::expect_used, clippy::diverging_sub_expression)]
{
return tokio::runtime::Builder::new_current_thread()
.enable_all()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I don't think we need to enable_all here, could just actually enable features we need. Same goes for all the tests that use [tokio::test], although not sure if it makes a big difference in practice.

Copy link
Contributor Author

@joostjager joostjager May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just expanded the macro. Can make customizations, but maybe it is better to keep to the original expansion? Also if it doesn't matter in practice...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no strong opinion. If we increase the number of tests using tokio, we might want to some benchmarks though, as the runtime of cargo test accumulates over time, so every second we can squeeze out of it would be appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed then. If we get more of these xtests, we might need to duplicate the macro or something like that.

let configs = [(false, false), (false, true), (true, false), (true, true)];
let mut last_err = None;
for (force_conflicting_utxo_spend, tolerate_high_network_feerates) in configs {
log_debug!(
self.logger,
"Attempting coin selection targeting {} sat/kW (force_conflicting_utxo_spend = {}, tolerate_high_network_feerates = {})",
target_feerate_sat_per_1000_weight,
force_conflicting_utxo_spend,
tolerate_high_network_feerates
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Indent off here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the indent off. The closing parenthesis?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the parenthesis and all arguments of log_debug too (they need to be indented by one tab).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the problem anymore in the diff now, but github still shows it on the old code fragment? In my IDE it all looks good with tabs, so I think it is solved.

@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@joostjager joostjager force-pushed the async-wallet-bump branch from e1509f1 to 991d505 Compare May 15, 2025 14:56
@joostjager joostjager requested a review from tnull May 15, 2025 14:57
@joostjager joostjager force-pushed the async-wallet-bump branch from 991d505 to 7f67bb0 Compare May 16, 2025 08:20
Async closures are complicated. Preparatory commit.
Convert methods that need to be async when the wallet traits become
async.
It seems that the compiler doesn't recognize the drop and complains
that the mutex crosses an await (introduced in later commit), even
though it doesn't.
Async closures are complicated. Preparatory commit.
Changes the CoinSelectionSource and WalletSource traits to be async and
provides WalletSourceSyncWrapper to as a helper for users that want to
implement a sync wallet source.

TestWalletSource is kept sync, to prevent a cascade of async conversions
in tests.
@joostjager
Copy link
Contributor Author

@tnull latest changes here

Will now push the rebase

@joostjager joostjager force-pushed the async-wallet-bump branch from 7f67bb0 to 6fd5e5b Compare May 16, 2025 08:25
@joostjager joostjager requested a review from TheBlueMatt May 16, 2025 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make BumpTransactionEventHandler async-optional
3 participants