1
0
Commit Graph

230 Commits

Author SHA1 Message Date
SomberNight
2c57c78ebe asyncio: stop using get_event_loop(). introduce ~singleton loop.
asyncio.get_event_loop() became deprecated in python3.10. (see https://github.com/python/cpython/issues/83710)
```
.../electrum/electrum/daemon.py:470: DeprecationWarning: There is no current event loop
  self.asyncio_loop = asyncio.get_event_loop()
.../electrum/electrum/network.py:276: DeprecationWarning: There is no current event loop
  self.asyncio_loop = asyncio.get_event_loop()
```
Also, according to that thread, "set_event_loop() [... is] not deprecated by oversight".
So, we stop using get_event_loop() and set_event_loop() in our own code.
Note that libraries we use (such as the stdlib for python <3.10), might call get_event_loop,
which then relies on us having called set_event_loop e.g. for the GUI thread. To work around
this, a custom event loop policy providing a get_event_loop implementation is used.

Previously, we have been using a single asyncio event loop, created with
util.create_and_start_event_loop, and code in many places got a reference to this loop
using asyncio.get_event_loop().
Now, we still use a single asyncio event loop, but it is now stored as a global in
util._asyncio_event_loop (access with util.get_asyncio_loop()).

I believe these changes also fix https://github.com/spesmilo/electrum/issues/5376
2022-04-29 18:49:07 +02:00
SomberNight
d7c02c4f7f network.switch_to_interface: catch 'task group terminated'
closes https://github.com/spesmilo/electrum/issues/7677
see https://github.com/spesmilo/electrum/issues/7677#issuecomment-1065170369
2022-03-11 15:39:03 +01:00
Federico
fcf54432f7 network: make request timeout configurable (#7696) 2022-03-04 12:25:56 +00:00
SomberNight
d7af868ed8 network: test if interface is alive before iface.taskgroup.spawn
closes https://github.com/spesmilo/electrum/issues/7677

```
 E/n | network | taskgroup died.
 Traceback (most recent call last):
   File "/opt/electrum/electrum/network.py", line 1204, in main
     [await group.spawn(job) for job in self._jobs]
   File "/home/voegtlin/.local/lib/python3.8/site-packages/aiorpcx/curio.py", line 297, in __aexit__
     await self.join()
   File "/opt/electrum/electrum/util.py", line 1255, in join
     task.result()
   File "/opt/electrum/electrum/network.py", line 1277, in _maintain_sessions
     await maintain_main_interface()
   File "/opt/electrum/electrum/network.py", line 1268, in maintain_main_interface
     await self._ensure_there_is_a_main_interface()
   File "/opt/electrum/electrum/network.py", line 1245, in _ensure_there_is_a_main_interface
     await self._switch_to_random_interface()
   File "/opt/electrum/electrum/network.py", line 648, in _switch_to_random_interface
     await self.switch_to_interface(random.choice(servers))
   File "/opt/electrum/electrum/network.py", line 714, in switch_to_interface
     await i.taskgroup.spawn(self._request_server_info(i))
   File "/home/voegtlin/.local/lib/python3.8/site-packages/aiorpcx/curio.py", line 204, in spawn
     self._add_task(task)
   File "/home/voegtlin/.local/lib/python3.8/site-packages/aiorpcx/curio.py", line 150, in _add_task
     raise RuntimeError('task group terminated')
 RuntimeError: task group terminated
```

I believe the "suppress spurious cancellations" block was added as SilentTaskGroup raised
CancelledError instead of RuntimeError for this scenario.
2022-02-21 20:09:26 +01:00
SomberNight
c9c094cfab requirements: bump min aiorpcx to 0.22.0
aiorpcx 0.20 changed the behaviour/API of TaskGroups.
When used as a context manager, TaskGroups no longer propagate
exceptions raised by their tasks. Instead, the calling code has
to explicitly check the results of tasks and decide whether to re-raise
any exceptions.
This is a significant change, and so this commit introduces "OldTaskGroup",
which should behave as the TaskGroup class of old aiorpcx. All existing
usages of TaskGroup are replaced with OldTaskGroup.

closes https://github.com/spesmilo/electrum/issues/7446
2022-02-15 18:22:44 +01:00
SomberNight
c131831373 util: rm SilentTaskGroup. this does not seem to be needed anymore
I think this was originally needed due to incorrect management of group lifecycles,
which our current code is doing better.

also note that if we needed this, in newer aiorpcx, the name of
the field was ~changed from `_closed` to `joined`:
239002689a
2022-02-15 18:22:40 +01:00
SomberNight
3f3212e94d some clean-ups now that we require python 3.8
In particular, asyncio.CancelledError no longer inherits Exception (it inherits BaseException directly)
2022-02-15 18:22:36 +01:00
SomberNight
3851ce5dd1 scripts: add "update_default_servers.py" 2022-01-18 19:50:46 +01:00
SomberNight
07bd2fe97d network.py: reorder bitcoin core error msg whitelist
test policy.cpp strings last as some of them are short and too generic

fixes https://github.com/spesmilo/electrum/issues/7483
2021-09-08 18:18:08 +02:00
MrNaif2018
aafa74ed08 Improve _mythread checks (#7403)
* Improve _mythread checks

* Create get_running_loop util
2021-07-15 14:52:25 +00:00
ghost43
de716ab07c Merge pull request #7247 from bitromortac/2104-fix-liquidity-hints
lnworker: fix path_finder access
2021-04-26 15:17:40 +00:00
SomberNight
8bba3b9ef5 network: also clear network.path_finder in stop_gossip
stop_gossip should undo start_gossip

related: #7242
2021-04-26 17:06:52 +02:00
bitromortac
4cb0b99996 lnworker: fix path_finder access 2021-04-26 08:43:50 +02:00
SomberNight
720d60c391 (trivial) rm dead code
follow-up #7152
2021-04-22 17:57:50 +02:00
ghost43
0f83270053 Merge pull request #7152 from bitromortac/2103-liquidity-hints
Liquidity hints for pathfinding
2021-04-22 14:45:41 +00:00
SomberNight
7ffb2c3cb0 config: (trivial) add some type hints and rm unused variable 2021-04-15 19:00:46 +02:00
bitromortac
4df67a4f78 lnrouter+lnworker: use liquidity hints
Adds liquidity hints for the sending capabilities of routing channels in the
graph. The channel blacklist is incorporated into liquidity hints.
Liquidity hints are updated when a payment fails with a temporary
channel failure or when it succeeds. Liquidity hints are used to give a
penalty in the _edge_cost heuristics used by the pathfinding algorithm.
The base penalty in (_edge_cost) is removed because it is now part of the
liquidity penalty. We don't return early from get_distances, as we want
to explore all channels.
2021-04-06 20:13:42 +02:00
ThomasV
a4210ce5e7 Qt: remove the 'local watchtower' option from GUI.
It is not realistic to expect Electrum to be used as a watchtower
in GUI mode, and possibly counter-productive (may set wrong
expectations).

A proper watchtower should be configured as a daemon. The
documentation will be updated to reflect this change.
2021-03-28 10:44:03 +02:00
SomberNight
5beadaab95 commands: clean-up inject_fees cmd 2021-03-25 19:33:40 +01:00
Benoit Verret
f731c38293 Minor style changes 2021-03-21 00:36:23 -04:00
SomberNight
16f874b58e network: trigger 'network_updated' as soon as set_parameters finishes
e.g. kivy GUI refreshes some fields based on this callback;
this should result in faster refreshes
(otherwise e.g. when switching servers, a callback might only come if
we successfully connect to the new server; or if an unrelated event
triggers it)
2021-03-17 13:40:27 +01:00
SomberNight
24e4aa3ab9 network.best_effort_reliable: use curio APIs instead of asyncio 2021-03-12 17:53:13 +01:00
SomberNight
5339e0054c network.stop: clean-up 2021-03-10 19:01:30 +01:00
SomberNight
3c019c2f9c daemon/wallet/network: make stop() methods async 2021-03-09 17:52:36 +01:00
SomberNight
ec6baa12f8 follow-up prev
oops, that was just for local testing
2021-03-04 17:50:52 +01:00
SomberNight
064670bd75 network: close interfaces more aggressively (abort after 2 seconds)
fixes #7083
2021-03-04 17:47:49 +01:00
SomberNight
d85e910262 logging: eliminate "fee_estimates" log spam
only log fee estimates if they changed, instead of ~10 times per minute
2021-02-27 12:27:26 +01:00
SomberNight
8bdbcf73d9 network.py: update bitcoin core error msg whitelist
Also add descriptive text for some of the common messages people ask
for help with.

closes #6760
related #5851
related #6985
2021-02-21 04:48:36 +01:00
ThomasV
ded449233e Trampoline routing.
- trampoline is enabled by default in config, to prevent download of `gossip_db`.
   (if disabled, `gossip_db` will be downloaded, regardless of the existence of channels)
 - if trampoline is enabled:
    - the wallet can only open channels with trampoline nodes
    - already-existing channels with non-trampoline nodes are frozen for sending.
 - there are two types of trampoline payments: legacy and end-to-end (e2e).
 - we decide to perform legacy or e2e based on the invoice:
    - we use trampoline_routing_opt in features to detect Eclair and Phoenix invoices
    - we use trampoline_routing_hints to detect Electrum invoices
 - when trying a legacy payment, we add a second trampoline to the path to preserve privacy.
   (we fall back to a single trampoline if the payment fails for all trampolines)
 - the trampoline list is hardcoded, it will remain so until `trampoline_routing_opt` feature flag is in INIT.
 - there are currently only two nodes in the hardcoded list, it would be nice to have more.
 - similar to Phoenix, we find the fee/cltv by trial-and-error.
    - if there is a second trampoline in the path, we use the same fee for both.
    - the final spec should add fee info in error messages, so we will be able to fine-tune fees
2021-02-17 17:28:13 +01:00
SomberNight
60792dc808 network: fix not retrying main interface in some cases
This was a regression from e83f0dd3fc,
introduced by the change in connection_down() there.
(if the initial connection to the main interface is not successful, the
network status will not be set to 'disconnected' - but it should be)
That change is now reverted here.
That change was somewhat independent of the rest of that commit,
except the rest highlighted that something of the sort was needed:
as it might sometimes take many seconds for an interface to close
and we might launch another interface for the same server while the
first one is still closing, the "server == self.default_server" test
is highly problematic.
To fix this second - original - issue, we now introduce a "closing" state
for interfaces (in the form of network._closing_ifaces).
2021-01-21 04:02:11 +01:00
SomberNight
b1449a0a39 interface.got_disconnected: change from Future to Event
Events are far easier to reason about.
2021-01-21 03:11:15 +01:00
ThomasV
ad91257729 pass blacklist to lnrouter.find_route, so that lnrouter is stateless (see #6778) 2021-01-11 15:19:50 +01:00
SomberNight
e83f0dd3fc network: when switching servers, don't wait for old interface to close
The GUI blocks until network.set_parameters returns when switching servers,
which waits for switch_to_interface, which used to wait until interface.close()
returns. interface.close() tries to flush buffered writes to the wire, with a
30 sec timeout.

If the server or the network connection is slow, flushing the buffer can take
several seconds. In particular, servers running Fulcrum always seem to
timeout in this case, freezing the GUI for 30 seconds (when switching away).
2020-12-13 18:08:36 +01:00
SomberNight
dbb7d7ce4d network: set _loop_thread again as it helps debugging
related: #6825
2020-12-08 11:48:01 +01:00
ThomasV
0b183444b4 Rename maybe_init_lightning(). Call load_data() from there 2020-11-20 08:35:57 +01:00
ThomasV
f155f3aff5 start lngossip if instantiated; this saves a boolean 2020-10-22 18:48:27 +02:00
bitromortac
4efcb53d24 network: load gossip db early
The gossip db is loaded early when the network is started to save
time when the gui is locked and a wallet not yet loaded. Side effects
of the LNWallet to start peering when a channel db is loaded is
circumvented.
2020-10-22 18:05:51 +02:00
bitromortac
6045de759b lightning: enable by default but without gossip
Enables lightning by creating a node private key and storing it in
the wallet. The gossiper is not launched at start up, only if there
are existing channels.
2020-10-22 18:05:51 +02:00
SomberNight
c5da22a9dd network: tighten checks of server responses for type/sanity 2020-10-16 19:30:42 +02:00
SomberNight
bde415cae7 wallet: don't try to get_input_tx from network when offline
related: https://github.com/spesmilo/electrum/issues/6648#issuecomment-708499893

Trying to fetch the prev tx from the network is a blocking operation with
a 10 sec timeout - we should not hang for 10 seconds if there is no network connection.
2020-10-14 19:30:10 +02:00
SomberNight
d19ff43266 interface: check server response for some methods
some basic sanity checks

Previously if the server sent back a malformed response, it could partially corrupt a wallet file.
(as sometimes the response would get persisted, and issues would only arise later when the values were used)
2020-07-02 15:41:39 +02:00
SomberNight
2eec7e1600 network: smarter switch_unwanted_fork_interface
Previously this function would not switch to a different chain if the
current chain contained the preferred block. This was not the intended
behaviour: if there is a *stronger* chain that *also* contains the
preferred block, we should jump to that.

Note that with this commit there will now always be a preferred block
(defaults to genesis). Previously, it might seem that often there was none,
but actually in practice if the user used the GUI context menu to switch
servers even once, there was one (usually genesis).

Hence, with the old code, if an attacker mined a single header which
then got reorged, auto_connect clients which were connected to the
attacker's server would never switch servers (jump chains) even
without the user explicitly configuring preference for the stale branch.
2020-06-21 11:31:54 +02:00
SomberNight
3766420a0b network: clarify local_height/server_height 2020-06-21 08:20:56 +02:00
SomberNight
21e637f543 network: validate server peers sent by main server
Data returned by the main server for request "server.peers.subscribe"
is of course untrusted input. Previously if it contained e.g. invalid port numbers
or IP addresses, it could kill the whole network taskgroup.
(this might have only affected master and not released versions,
which would only raise exceptions once the client actually tried to connect to an invalid host/port)
2020-05-13 19:28:35 +02:00
ThomasV
11aaa0b66f Simplify services (watchtower, payserver):
- Do not expose services settings in GUI
 - Use a single netaddress configuration variable.
2020-05-10 14:52:50 +02:00
SomberNight
9ac41be1e8 network: set .oneserver in __init__
fixes #6135
2020-05-01 03:27:31 +02:00
Luke Childs
cd199390e2 Use non-standard localhost port for server-string fallback (#6087)
* Use non-standard localhost port for server-string fallback

Co-authored-by: Luke Childs <lukechilds123@gmail.com>
2020-04-16 19:39:05 +00:00
SomberNight
9e57ae630b network/gui: unify host/port input fields to single server str
This allows optionally specifying the protocol for the main server.

fixes #6095
fixes #5278
2020-04-16 21:19:48 +02:00
SomberNight
b2cfaddff2 network.NetworkParameters: merge host+port+protocol into "server" field 2020-04-16 21:19:45 +02:00
SomberNight
adc3784bc2 network: allow mixed protocols among interfaces
Previously all the interfaces used either "t" or "s".
Now the network only tries to use "s" for all interfaces, except for
the main interface, which the user can manually specify to use "t".
(so e.g. if you run with "--server localhost:50002:t", the main server will use "t",
but all the rest will use "s")
2020-04-16 21:19:42 +02:00