Commit Graph

666 Commits

Author SHA1 Message Date
ThomasV
a5965933d2 Fix CTNs in should_be_closed_due_to_expiring_htlcs (fixes #7906).
Also fix sending too many fee updates.
Rename lnworker.on_channel_update, that name was misleading.
2022-08-02 18:00:39 +02:00
ThomasV
b5617ec0fd lnworker: get_channel_objects method to be used by GUI 2022-07-20 21:07:13 +02:00
ThomasV
af1fb1ae9f remove_channel_backup: a channel backup might be in both lists 2022-07-20 17:00:57 +02:00
ThomasV
f5abd4f1d1 add command line for rebalancing channels 2022-07-13 10:14:34 +02:00
ThomasV
9b075ba187 get_node_alias: allow node_id prefixes for channel backups 2022-07-11 11:30:02 +02:00
ThomasV
36a6fd6311 request_force_close_from_backup:
- for an onchain backups, if the channel is with a
   hardcoded trampoline, try first without gossip DB.
 - for imported backups, fallback to gossip DB if we
   fail to connect with the provided network address.
2022-07-11 11:16:43 +02:00
SomberNight
1b6706bed6 replace some erroneous usages of IntFlag with IntEnum 2022-06-29 18:07:03 +02:00
SomberNight
9f1da8422b qt LightningTxDialog: (fix regression) show fee for ln payments
follow-up e1d34300e5
2022-06-29 18:02:55 +02:00
SomberNight
2511d8118c lnworker.get_lightning_history(&get_payment_value): rm failing assert
follow-up e1d34300e5
follow-up dc15d59fcf

```
Traceback (most recent call last):
  File ".../electrum/gui/qt/main_window.py", line 910, in timer_actions
    self.update_wallet()
  File ".../electrum/gui/qt/main_window.py", line 1068, in update_wallet
    self.update_tabs()
  File ".../electrum/gui/qt/main_window.py", line 1075, in update_tabs
    self.history_model.refresh('update_tabs')
  File ".../electrum/util.py", line 439, in <lambda>
    return lambda *args, **kw_args: do_profile(args, kw_args)
  File ".../electrum/util.py", line 435, in do_profile
    o = func(*args, **kw_args)
  File ".../electrum/gui/qt/history_list.py", line 275, in refresh
    transactions = wallet.get_full_history(
  File ".../electrum/util.py", line 439, in <lambda>
    return lambda *args, **kw_args: do_profile(args, kw_args)
  File ".../electrum/util.py", line 435, in do_profile
    o = func(*args, **kw_args)
  File ".../electrum/wallet.py", line 1109, in get_full_history
    lightning_history = self.lnworker.get_lightning_history() if self.lnworker and include_lightning else {}
  File ".../electrum/lnworker.py", line 839, in get_lightning_history
    assert direction == PaymentDirection.FORWARDING
AssertionError
```
2022-06-29 16:12:47 +02:00
ThomasV
dc15d59fcf get_payment_value: rm failing assert 2022-06-29 13:33:04 +02:00
SomberNight
cafd5c4af0 lnworker.add_request: force keyword args
and change type-hint to reflect that fallback_address can be None
2022-06-22 02:26:54 +02:00
ThomasV
dbf055de9a EventListener class to handle callbacks
and QtEventListener for Qt
2022-06-22 02:07:46 +02:00
ThomasV
e1d34300e5 lnworker: get_payment_value
- the detection of self-payments was using the length
   of the htlc list, incorrectly categorizing all MPPs.
 - the fee for self-payments was not correctly computed.
2022-06-16 21:02:34 +02:00
ThomasV
cfb6ab6822 Trampoline: always increase fees, to ensure we do not get stuck in a loop.
Also handle TEMPORARY_CHANNEL_FAILURE, which is sometimes returned by Eclair.
2022-06-15 19:18:04 +02:00
ThomasV
9fe93524b7 Index lightning requests with rhash instead of onchain address.
get_unused_addresses() has been broken since #7730, because
addresses are considered as permanently used if they are in
the list of keys of receive_requests. This is true even if
an address is used as fallback for a lightning payment. This
means that the number of lightning payments we can receive
is constrained by the gap limit.

If a payment succeeds off-chain, we want to be able to reuse
its fallback address in other requests (this does not reduce
privacy, because invoices already share the same public key).

This implies that we should not use the onchain address as key
for lightning-enabled requests in wallet.receive_requests. If
we did, paid invoices would be overwritten when the address is
reused. That is the reason for the wallet_db upgrade.

Related: a3faf85e3c
2022-06-15 18:44:52 +02:00
ThomasV
adb5b6213b follow-up a3faf85e3c 2022-06-14 20:33:19 +02:00
ThomasV
a3faf85e3c wallet:
- add new index: requests_rhash_to_key (fixes #7845)
 - when creating a request, do not save its description in labels.
   Instead, return it as default value in wallet.get_label_by_rhash
lnworker:
  - rename 'payments' to 'payment_info'
  - add note to delete_payment_info
commands: rename 'rmrequest' to 'delete_request'
2022-06-14 13:39:18 +02:00
ThomasV
c316d406db unconfirmed swaps: revert label change (follow-up 599ad1c017) 2022-06-13 10:37:25 +02:00
ThomasV
599ad1c017 lnworker: get_onchain_history does not need to be online anymore 2022-06-13 10:26:07 +02:00
SomberNight
1613736b45 lnpeer: rename trigger_force_close to request_force_close
for more consistent naming with rest of the code
2022-06-10 17:13:11 +02:00
ThomasV
121d8732f1 Persist LNWatcher transactions in wallet file:
- separate AddressSynchronizer from Wallet and LNWatcher
 - the AddressSynchronizer class is referred to as 'adb' (address database)
 - Use callbacks to replace overloaded methods
2022-06-10 13:07:53 +02:00
SomberNight
8674fd96d5 lnworker: also check expiring_htlcs in ChannelState.SHUTDOWN
otherwise the remote could intentionally send "shutdown" during an attack
2022-06-07 22:37:25 +02:00
ThomasV
0ed4fea899 Revert "num_sats_can_receive: bucket channels that we have with the same node"
This reverts commit c06a9ccb9c.
2022-06-05 09:10:32 +02:00
ThomasV
c06a9ccb9c num_sats_can_receive: bucket channels that we have with the same node 2022-06-04 11:17:21 +02:00
ThomasV
8750936679 Rebalance dialog:
- move dialog code to own submodule and class
 - disable ok button if amount is out of bounds
 - add max button
 - add Rebalance button to channels tab
2022-05-29 18:40:45 +02:00
ThomasV
ce96d1f97b lnworker: create_routes_for_payment: try random order
without this, we keep retrying with the same trampoline when payment fails
2022-05-29 12:38:16 +02:00
ThomasV
cd54244956 lnworker: in sugest_channels_for_rebalance, delta depends on the channel
Also, safety margin can be reduced.
2022-05-29 11:58:49 +02:00
ThomasV
5d659cda0e improve channel_funding_sat suggestion. add min_amount parameter to new_channel_dialog 2022-05-22 17:18:28 +02:00
ThomasV
0656e6d44e suggest_rebalance: special case for MPP heuristics 2022-05-22 09:22:16 +02:00
ThomasV
53151244e2 LNWorker: Add suggest_rebalance methods for sending and receiving.
These methods return a list of channels that can be rebalanced,
in order to receive or send a given amount.

Also add 'channels' parameter to submarine swaps.
Previously, swaps were not considering which channel to use.

When we do not have liquidity to pay an invoice:
 - add 'rebalance' option in order to pay an invoice
 - use the suggested channel in the 'swap' option

When we do not have the liquidity to receive an invoice:
 - add 'Rebalance' and 'Swap' buttons to the receive tab
2022-05-21 20:25:44 +02:00
ThomasV
917f256e33 remove scheduled invoices: bad UX. better expect the user to retry later. 2022-05-21 12:24:26 +02:00
ThomasV
bc9cc51800 Add 'channels' parameter to create invoice and pay.
Add rebalance dialog to GUI
2022-05-21 11:35:44 +02:00
ThomasV
dd3481c9e1 Merge pull request #7821 from SomberNight/202205_lnworker_num_sats_can_receive
lnworker: rework num_sats_can_receive and routing_hints_for_invoice
2022-05-20 10:01:59 +02:00
SomberNight
2ec9e869b3 invoice.get_amount_sat: handle None in more places
I believe lightning requests created before https://github.com/spesmilo/electrum/pull/7730
can have an amount of None - ones created after have amount 0 instead.
We could do a wallet db upgrade potentially.
Regardless, the type hint is `get_amount_sat(self) -> Union[int, str, None]`,
so None should be handled. (well, arguably "!" should be handled too...)

```
E | gui.qt.exception_window.Exception_Hook | exception caught by crash reporter
Traceback (most recent call last):
  File "...\electrum\electrum\gui\qt\request_list.py", line 101, in item_changed
    self.parent.show_receive_request(req)
  File "...\electrum\electrum\gui\qt\main_window.py", line 1279, in show_receive_request
    URI = req.get_bip21_URI(lightning=bip21_lightning)
  File "...\electrum\electrum\invoices.py", line 164, in get_bip21_URI
    amount = int(self.get_amount_sat())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
```

```
E | gui.qt.exception_window.Exception_Hook | exception caught by crash reporter
Traceback (most recent call last):
  File "...\electrum\electrum\gui\qt\request_list.py", line 101, in item_changed
    self.parent.show_receive_request(req)
  File "...\electrum\electrum\gui\qt\main_window.py", line 1281, in show_receive_request
    can_receive_lightning = self.wallet.lnworker and req.get_amount_sat() <= self.wallet.lnworker.num_sats_can_receive()
TypeError: '<=' not supported between instances of 'NoneType' and 'decimal.Decimal'
```
2022-05-19 19:43:07 +02:00
SomberNight
dd5cb2a5c1 lnworker: rework num_sats_can_receive and routing_hints_for_invoice
follow-up https://github.com/spesmilo/electrum/pull/7818

- note it matters whether a sender pays us end-to-end-trampoline or just via legacy
  - consider: Alice has 0.1 BTC recv cap in chan1 and 1 BTC recv cap in chan2, both with border-node T1
    - if sender is paying e2e trampoline, it can realistically pay even ~1.1 BTC, as T1 can resplit the HTLCs
    - if sender is paying legacy, it will have a hard time trying to pay more than 1 BTC, in practice
      - although note if T1 has implemented non-strict-forwarding (see BOLT-04), achieving 1 BTC is easy,
        as T1 can redirect HTLCs (but cannot split them, in this case)
  - to make num_sats_can_receive realistic, it assumes the legacy case
- To calc num_sats_can_receive, we sort our channels in decreasing order of receive-capacities, iterate over them
  and calculate a running sum - we stop adding channels when the next chan's recv cap is small compared to
  the running total.
- When putting routing hints in an invoice, we do the same, with the added condition that we keep adding channels
  if their recv cap is larger than the invoice amount.
  - consider: Alice has 0.1 BTC recv cap in chan1 with Bob, and 1 BTC recv cap in chan2 with Carol
    - if Alice wants to recv 100 sats, it is useful to add hints for both channels into the invoice, for redundancy
    - if Alice wants to recv 0.9 BTC, it is questionable whether adding the smaller chan is useful - the code here won't add it
2022-05-19 18:28:04 +02:00
ThomasV
f90a08bbe2 Filter nodes for receiving:
- increase MPP_RECEIVE_CUTOFF from 5 to 20 percent
 - filter channels by node_id, not channel_id
 - make num_sats_can_receive consistent with routing hints
2022-05-18 18:11:40 +02:00
ThomasV
0a6495c490 lnworker: if trampoline is active, make num_sats_can_send
return the max value allowed for legacy payments.
(that is, do not assume we can do MPP involving two trampolines)
This reverts fb6047ec46

Reason: num_sats_can_send may be used to decide the amount
we put in a new channel.
2022-05-18 09:48:22 +02:00
ThomasV
1274ec7655 Qt balance piechart: show lightning funds that are frozen 2022-05-10 20:07:30 +02:00
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
ThomasV
f4e902e907 LNWorker: give up payment after timeout, not number of attempts.
Limiting attempts may interrupt a MPP before we receive a MPP_timeout
The attempts parameter is still used in unit tests.
2022-04-29 12:17:38 +02:00
SomberNight
f10752b523 lnworker: LNWallet.start_network to call super().start_network
The call to super was removed in 4efcb53d24 ,
so that LNWallet's taskgroup would not run _maintain_connectivity, AFAICT.
The way it was done meant that "main_loop" itself would not run for LNWallet, only for LNGossip - very confusing.
It is only due to a quirk in the behaviour of TaskGroups that the group "started" at all.
2022-04-26 20:24:08 +02:00
ThomasV
2117118047 set_request_status: pass the right key to request_status callback. 2022-04-23 19:24:31 +02:00
ThomasV
8354dd006b fix receiving lightning requests without amount 2022-04-23 12:06:24 +02:00
SomberNight
5f7388a475 lnworker: fix get_onchain_history if running with --offline
```
Traceback (most recent call last):
  File "...\electrum\electrum\gui\qt\main_window.py", line 898, in timer_actions
    self.update_wallet()
  File "...\electrum\electrum\gui\qt\main_window.py", line 1040, in update_wallet
    self.update_tabs()
  File "...\electrum\electrum\gui\qt\main_window.py", line 1047, in update_tabs
    self.history_model.refresh('update_tabs')
  File "...\electrum\electrum\util.py", line 439, in <lambda>
    return lambda *args, **kw_args: do_profile(args, kw_args)
  File "...\electrum\electrum\util.py", line 435, in do_profile
    o = func(*args, **kw_args)
  File "...\electrum\electrum\gui\qt\history_list.py", line 275, in refresh
    transactions = wallet.get_full_history(
  File "...\electrum\electrum\util.py", line 439, in <lambda>
    return lambda *args, **kw_args: do_profile(args, kw_args)
  File "...\electrum\electrum\util.py", line 435, in do_profile
    o = func(*args, **kw_args)
  File "...\electrum\electrum\wallet.py", line 947, in get_full_history
    lnworker_history = self.lnworker.get_onchain_history() if self.lnworker and include_lightning else {}
  File "...\electrum\electrum\lnworker.py", line 911, in get_onchain_history
    tx_height = self.lnwatcher.get_tx_height(swap.funding_txid)
AttributeError: 'NoneType' object has no attribute 'get_tx_height'
```
2022-04-22 19:18:07 +02:00
ThomasV
fb6047ec46 lnworker: fix can_pay_invoice for trampoline MPP
Call it from the GUI
2022-04-22 15:13:12 +02:00
ThomasV
60865f3902 Show options if we do not have the liquidity to pay a lightning invoice:
pay onchain, open channel, rebalance.

If we do a swap or open a channel, the payment will be scheduled.
2022-04-20 12:48:22 +02:00
ThomasV
2c6e36e89d Fallback addresses
- add fallback address to BOLT-11 LN invoices
   - Qt: if LN payment fails, propose onchain fallback
2022-04-20 12:48:22 +02:00
ThomasV
7102fb732e follow-up prev:
- detect payment of requests both onchain or LN
 - create single type of requests in GUI
2022-04-20 12:48:22 +02:00
ThomasV
e392197ab9 wallet_db upgrade:
- unify lightning and onchain invoices, with optional fields for bip70 and lightning
 - add receive_address fields to submarine swaps
2022-04-20 12:48:22 +02:00
ThomasV
877a9f553b Merge pull request #7748 from spesmilo/confirm_reverse_swaps
Confirm reverse swaps
2022-04-09 10:41:02 +02:00