-
Notifications
You must be signed in to change notification settings - Fork 24
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
Rework and simplify electrum client, miniwallet and watcher #411
Conversation
This looks like a nice cleanup! I tested it in branch test-pr-411 and I ran into one issue. The problem we're having on the Phoenix side is that ElectrumClient's The issue is that, Phoenix has a better understanding of the network state than lightning-kmp. Specifically, it has a NetworkMonitor, which knows whether or not the device has an Intenet connection. This is obviously OS-specific (for example, here's the iOS version). And since Phoenix receives notifications from the OS about the network state, it can better determine when to attempt a re-connect. And there are other complications. On iOS, when the app is sent into the background, the OS will kill any open network connections (after a brief delay). The Phoenix layer knows when the app is going to be sent to the background, and prefers to perform a clean disconnect on its own. Unless of course an outgoing payment is in progress when the user background's the app. In which case Phoenix asks the OS for permission to perform a "long-lived background task". If granted, it then keeps the connection(s) open until the payment completes, or we run out of granted time. There's also the WatchTower task, which runs every few days while the app is in the background... Long story short, there are a bunch of complications surrounding connect/disconnect/reconnect. All this logic is implemented in AppConnectionsDaemon. (See |
Sorry I missed that, I'll restore |
0b11d32
to
cb32535
Compare
Rebased and updated to be 100% compatible with the current client and watcher (tested on Android but not on iOS). |
cb32535
to
38935b6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on Phoenix Android and iOS, it works fine. There are issues with Tor in edge cases but I think they should be investigated separately.
src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumWatcher.kt
Outdated
Show resolved
Hide resolved
src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumClient.kt
Outdated
Show resolved
Hide resolved
I have added a few nits on branch electrum-client-rework-pm. Why is this PR draft, what's left to work on? |
CompletableDeferred<> is used to handle multiple concurrent calls and provide suspend methods which are much easier to use. For example, to retrieve a transaction you can simply call `client.getTx(txid)` and you can chain such calls in your message handling loops: this eliminate the need to send messages to yourself and create intermediate states that are just waiting for a response from the electrum client. We also fix a bug where Phoenix would crash after being disconnected because it would try to use a closed socket to send Ping messages.
f66fd82
to
39136e6
Compare
Apply suggestions from https://github.com/ACINQ/lightning-kmp/compare/electrum-client-rework-pm and add a new ElectrumSubscriptionResponse type for subscription events.
Thanks, I used these changes and also updated ElectrumClient's notification flow to a
The only thing that I'm unsure of is the handling of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM, but this requires an ACK from the Phoenix team.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@dpad85 with or without |
So the removal of |
It is used by a watch-tower module written in Swift in the iOS app. Code has bee simplified and there are now 2 separate flows: - one for watch events - one for "up-to-date" events (which are just the number of milliseconds since the watcher is ready and idle)
3e16024
to
6ad2580
Compare
I've restored |
In the initial implementation of the wallet, we were asynchronously retrieving the parent transaction of utxos. When a new utxo was detected, there would be a period of time where `WalletState` would contain the utxo, but not the parent tx, hence the need for the `consistent` boolean. But since #411 the Electrum interface has become "synchronous" with the use of `suspend` functions. Note that if for some reason we cannot retrieve the transaction from Electrum, then we ignore the utxo, which is what we were doing previously with a `walletStateFlow.filter { it.consistent }` in Peer.
In the initial implementation of the wallet, we were asynchronously retrieving the parent transaction of utxos. When a new utxo was detected, there would be a period of time where `WalletState` would contain the utxo, but not the parent tx, hence the need for the `consistent` boolean. But since #411 the Electrum interface has become "synchronous" with the use of `suspend` functions. Note that if for some reason we cannot retrieve the transaction from Electrum, then we ignore the utxo, which is what we were doing previously with a `walletStateFlow.filter { it.consistent }` in Peer.
CompletableDeferred<> is used to handle multiple concurrent calls and provide suspend methods which are much easier to use. For example, to retrieve a transaction you can simply call
client.getTx(txid)
and you can chain such calls in your message handling loops: this eliminate the need to send messages to yourself and create intermediate states that are just waiting for a response from the electrum client.I've also remove the
NotifyTxEvent
event which I believe is not used anywhere.