Skip to content

Add ChannelPending event emitted upon funding_signed - #2098

Merged
TheBlueMatt merged 1 commit into
lightningdevkit:mainfrom
tnull:2023-03-add-channel-pending-event
Apr 3, 2023
Merged

Add ChannelPending event emitted upon funding_signed#2098
TheBlueMatt merged 1 commit into
lightningdevkit:mainfrom
tnull:2023-03-add-channel-pending-event

Conversation

@tnull

@tnulltnull commented Mar 10, 2023

Copy link
Copy Markdown
Contributor

Users currently don't have good way of being notified when channel open negotiations have succeeded and new channels are pending confirmation on chain.

To this end, we add a new ChannelPending event that is emitted when send or receive a funding_signed message, i.e., at the last moment before waiting for the confirmation period.

@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 84ea88f to 226dfdeCompareMarch 10, 2023 16:34
@codecov-commenter

codecov-commenter commented Mar 10, 2023

Copy link
Copy Markdown

Codecov Report

Patch coverage: 80.95% and project coverage change: -0.03⚠️

Comparison is base (0e28bcb) 91.38% compared to head (3acc6f8) 91.36%.

❗ Current head 3acc6f8 differs from pull request most recent head 9873c7d. Consider uploading reports for the commit 9873c7d to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@ Coverage Diff @@## main #2098 +/- ##
==========================================
- Coverage 91.38% 91.36% -0.03% 
==========================================
Files 102 102 Lines 49767 49844 +77 Branches 49767 49844 +77 ==========================================
+ Hits 45482 45540 +58 - Misses 4285 4304 +19 
Impacted FilesCoverage Δ
lightning-background-processor/src/lib.rs77.80% <ø> (ø)
lightning/src/events/mod.rs30.85% <0.00%> (-1.29%)⬇️
lightning/src/ln/functional_test_utils.rs92.59% <93.75%> (+<0.01%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.59% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs90.08% <100.00%> (+0.04%)⬆️
lightning/src/ln/channelmanager.rs89.13% <100.00%> (+0.03%)⬆️
lightning/src/ln/functional_tests.rs98.22% <100.00%> (+<0.01%)⬆️
lightning/src/ln/priv_short_conf_tests.rs97.56% <100.00%> (+0.03%)⬆️
lightning/src/ln/reload_tests.rs95.57% <100.00%> (+0.01%)⬆️

... and 3 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment threadlightning/src/util/events.rs
Comment threadlightning/src/ln/functional_test_utils.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/util/events.rs Outdated
Comment threadlightning/src/util/events.rs
@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 5bd01c1 to 37ebcdaCompareMarch 13, 2023 20:49
@tnull

Copy link
Copy Markdown
ContributorAuthor

Also rebased on main to take advantage of the CI fix.

@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 37ebcda to 970fa70CompareMarch 13, 2023 21:20
@tnull

tnull commented Mar 13, 2023

Copy link
Copy Markdown
ContributorAuthor

... and kicked CI. Having tests that rely on bitcoind is really a gift that keeps on giving:

---- test_esplora_syncs stdout ----
thread 'test_esplora_syncs' panicked at 'called `Result::unwrap()` on an `Err` value: JsonRpc(Rpc(RpcError { code: -4, message: "Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?\n", data: None }))', tests/integration_tests.rs:35:65
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 970fa70 to d2af89bCompareMarch 14, 2023 11:21
Comment threadlightning/src/util/events.rs
Comment threadlightning/src/ln/channel.rs Outdated
@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from d2af89b to 58ea755CompareMarch 14, 2023 15:25
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/util/events.rs Outdated
@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Note that you'll need to squash all the commits before we land - we should avoid having commits that individually fail tests as it makes future bisection hard.

@RCasatta

RCasatta commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

... and kicked CI. Having tests that rely on bitcoind is really a gift that keeps on giving:

---- test_esplora_syncs stdout ----
thread 'test_esplora_syncs' panicked at 'called `Result::unwrap()` on an `Err` value: JsonRpc(Rpc(RpcError { code: -4, message: "Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?\n", data: None }))', tests/integration_tests.rs:35:65
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Can't reproduce locally by trying in a while loop.

But I stopped early because the test implementation is leaky, I think the drop on ElectrsD and BitcoinD is not called because in OnceCell? Since it is not called the process cannot be stopped/killed and unpleasantly stick around.

The main point of bitcoind and electrsd is to have isolated process, I suggest to create one instance in every test using it instead of having a global &'static

@tnull

tnull commented Mar 16, 2023

Copy link
Copy Markdown
ContributorAuthor

Can't reproduce locally by trying in a while loop.

But I stopped early because the test implementation is leaky, I think the drop on ElectrsD and BitcoinD is not called because in OnceCell? Since it is not called the process cannot be stopped/killed and unpleasantly stick around.

The main point of bitcoind and electrsd is to have isolated process, I suggest to create one instance in every test using it instead of having a global &'static

I think spinning up new dedicated BitcoinD/EsploraD instance per test case is infeasible, especially when we need to premine 101 blocks every time before we can get started. However, it seems you're right and once_cell indeed won't call drop and hence doesn't allow us to clean up properly upon shutdown, which is kind of unfortunate. Will have to further look into how to mitigate this.

@RCasatta

Copy link
Copy Markdown
Contributor

I think spinning up new dedicated BitcoinD/EsploraD instance per test case is infeasible, especially when we need to premine 101 blocks every time before we can get started

How many tests with electrs do you have? It takes a couple of seconds to spin up bitcoind and electrs synced with 101 blocks, and by default N tests are launched in parallel where N is the number of your cores.

@tnull

Copy link
Copy Markdown
ContributorAuthor

How many tests with electrs do you have? It takes a couple of seconds to spin up bitcoind and electrs synced with 101 blocks, and by default N tests are launched in parallel where N is the number of your cores.

You're right, should be fine for now, although we might want to reconsider if the number of tests grows. Fixed in #2132

@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch 2 times, most recently from 5526228 to 6a36d31CompareMarch 29, 2023 14:11
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
/// confirmation on-chain.
///
/// This event is emitted when the funding transaction has been signed and is broadcast to the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we have a note that, in rare cases, this event may be missed if a channel is ultimately confirmed quickly prior to the event being processed and then we restart? Its a stupid rare case but its possible I think?

@tnulltnullMar 30, 2023

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I'm confused, which scenario are you describing exactly? Wouldn't rather be an edge case that we emit the event twice when we decide to rebroadcast on restart?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we can: (a) fund the channel, with an async monitor update, (b) not have that complete, but persist the manager, then (c) complete the monitor update and broadcast the funding tx (d) then restart, replay the chain and see the funding tx confirm, then (e) re-complete the monitor update completed in c, then we wont get the event again but the user will never have used it.

I think with "full" async monitor updates this becomes more realistic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we have a note that, in rare cases, this event may be missed if a channel is ultimately confirmed quickly prior to the event being processed and then we restart?

Since we plan to leverage it for #2137, seems we should always regenerate it in cases like this? The channel will actually be ready, but as long as it's queued before the ChannelReady event, it should be fine.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yea, that's my biggest concern here.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I think we can: (a) fund the channel, with an async monitor update, (b) not have that complete, but persist the manager, then (c) complete the monitor update and broadcast the funding tx (d) then restart, replay the chain and see the funding tx confirm, then (e) re-complete the monitor update completed in c, then we wont get the event again but the user will never have used it.

Ugh., I see. In that scenario it would persist the event queue when the event hasn't been emitted yet and wouldn't re-emit it after restart. Mh, instead of leaving a note I went ahead and am now tracking the emission of ChannelPending in the same way as ChannelReady and removed it from internal_funding_created entirely.
This means that we're now only emitting it after CMU completion or reestablish, i.e., would re-emit it if the "emitted" state wasn't persisted before.

@tnull

Copy link
Copy Markdown
ContributorAuthor

Rebased on main to fix failing CI.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

LGTM, I think, feel free to squash the fixups.

@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 3acc6f8 to 8475376CompareApril 1, 2023 10:07
@tnull

tnull commented Apr 1, 2023

Copy link
Copy Markdown
ContributorAuthor

LGTM, I think, feel free to squash the fixups.

Squashed without further changes.

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, just two minor comments

} else { panic!(); }

node_b.handle_funding_created(&node_a.get_our_node_id(), &get_event_msg!(node_a_holder, MessageSendEvent::SendFundingCreated, node_b.get_our_node_id()));
let events_b = node_b.get_and_clear_pending_events();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Use expect_channel_pending_event here and below

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Mh, yeah, unfortunatly I can't easily as expected_channel_pending_event takes a functional_test_utils::Node as parameter, and this sets up the ChannelManager manually. I'd rather not rewrite this entire test or all other tests just to avoid writing the match statement out once.

Comment threadlightning/src/events/mod.rs Outdated
Currently, users don't have good way of being notified when channel open
negotiations have succeeded and new channels are pending confirmation on
chain. To this end, we add a new `ChannelPending` event that is emitted
when send or receive a `funding_signed` message, i.e., at the last
moment before waiting for the confirmation period.
We track whether the event had previously been emitted in `Channel` and
remove it from `internal_funding_created` entirely. Hence, we now
only emit the event after ChannelMonitorUpdate completion, or upon
channel reestablish. This mitigates a race condition where where we
wouldn't persist the event *and* wouldn't regenerate it on restart,
therefore potentially losing it, if async CMU wouldn't complete before
ChannelManager persistence.
@tnull
tnullforce-pushed the 2023-03-add-channel-pending-event branch from 8475376 to 9873c7dCompareApril 3, 2023 17:04
@tnull

ghost commented Apr 3, 2023

Copy link
Copy Markdown
ContributorAuthor

Squashed with the fixup:

diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs
index f20d6667..2d576d33 100644
--- a/lightning/src/events/mod.rs+++ b/lightning/src/events/mod.rs@@ -631,5 +631,5 @@ pub enum Event {
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
///
- /// May be `None` for channels created prior to LDK version 0.0.115.+ /// Will be `None` for channels created prior to LDK version 0.0.115.
former_temporary_channel_id: Option<[u8; 32]>,
/// The `node_id` of the channel counterparty.

write_tlv_fields!(writer, {
(0, channel_id, required),
(2, user_channel_id, required),
(4, former_temporary_channel_id, required),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: option is a bit more straightforward/saves a few bytes on the wire. It doesn't matter, though, just for future reference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, it's a tiny bit more efficient but to be honest I don't liking mixing implementation type and serialization type semantics if it can be avoided. In my mind option -> field is optional, not field is Option.

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. I believe the CI failure is unrelated but can't actually see the issue.

@TheBlueMatt

ghost commented Apr 3, 2023

Copy link
Copy Markdown
Collaborator

Tracked CI failure as #2144

@TheBlueMatt
TheBlueMatt merged commit 7ca3709 into lightningdevkit:mainApr 3, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@tnull@codecov-commenter@TheBlueMatt@RCasatta@dunxen@wpaulino@alecchendev