Commit graph

174 commits

Author SHA1 Message Date
Rusty Russell
c797b6fb20 libplugin: add method string to jsonrpc callbacks, implement generic helpers.
Without knowing what method was called, we can't have useful general logging
methods, so go through the pain of adding "const char *method" everywhere,
and add:

1. ignore_and_complete - we're done when jsonrpc returned
2. log_broken_and_complete - we're done, but emit BROKEN log.
3. plugin_broken_cb - if this happens, fail the plugin.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-11-07 17:04:35 +10:30
Rusty Russell
4f4ec9aefd askrene: make sure we depend on libplugin.h
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-11-07 17:04:35 +10:30
Rusty Russell
c5099b1647 libplugin: clean up API.
When we used to allow cmd to be NULL, we had to hand the plugin
everywhere.  We no longer do.

1. Various jsonrpc_ functions no longer need the plugin arg.
2. send_outreq no longer needs a plugin arg.
3. The init function takes a command, not a plugin.
4. Remove command_deprecated_in_nocmd_ok.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-11-07 17:04:35 +10:30
Rusty Russell
7ce30e2873 libplugin: make timers have a "command" context.
This is cleaner: everything can now be associated with a command
context.

You're supposed to eventually dispose of it using timer_complete().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-11-07 17:04:35 +10:30
Lagrang3
ba905f7e20 renepay: add test for description interface
Change-log: renepay: add test for description interface

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-11-06 09:59:27 +01:00
Lagrang3
2d9277a9a1 renepay: pay BOLT11 invoices with description_hash
This remove an unnecessary check for existing description field if the
description_hash is provided in the invoice. The bolt11_decode function
already checks the description against the hash if both are provided.

Changelog-Fix: renepay: allow to pay BOLT11 invoices with description_hash, the description field is made optional

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-11-06 09:59:27 +01:00
Rusty Russell
f944e03fca BOLT update: remove INVALID_REALM error.
This is obsolete (since modern onions) and so removed from spec.
We should not set it, and don't need to handle it specially.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-10-16 07:14:32 +10:30
Rusty Russell
5052f0763f gossmap: keep capacity for locally-generated channels as well.
It was weird not to have a capacity associated with localmods channels, and
fixing it has some very nice side effects.

Now the gossmap_chan_get_capacity() call never fails (we prevented reading
of channels from gossmap in the partially-written case already), so we
make it return the capacity.  We do this in msat, because that's what
all the callers want.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-10-04 11:27:53 +09:30
Rusty Russell
a60063e763 common/gossmods_listpeerchannels: include channel capacity in callback.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-10-04 11:27:53 +09:30
Rusty Russell
a65e325b13 gossmap: implement partial updates.
This is actually what we want in several places: to only override one or
two fields in a channel_update.

We add a gossmap_local_setchan() with a similar API to the old
gossmap_local_updatechan(), for the case where we want to set every
field.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-10-04 11:27:53 +09:30
Rusty Russell
3253623785 gossmods_from_listpeerchannels: use correct type for cltv_delta.
Doesn't matter now, but will with the next change where we want to
pass a pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-10-04 11:27:53 +09:30
Rusty Russell
679f46f733 common/amount: rename amount_sat_zero/amount_msat_zerp -> amount_sat_is_zero/amount_msat_is_zero.
I used `amount_msat_eq(x, AMOUNT_MSAT(0))` because I forgot this
function existed.  I probably missed it because the name is surprising,
so add "is" in there to make it clear it's a boolean function.

You'll note almost all the places which did use it are Eduardo's and
Lisa's code, so maybe it's just me.

Fix up a few places which I could use it, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-09-19 12:16:53 +09:30
Rusty Russell
f05f871c92 common/amount: add amount_msat_accumulate()
Saves some typing, and is clearer than checking if both args really
are the same!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-09-19 12:16:53 +09:30
Lagrang3
5400989552 renepay: bugfix: apply channel filter globally
Channel filter must apply to the modified gossmap+localmods,
otherwise we disable local channels with htlcmax=0.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
4fdc74f100 renepay: take it easy with local channels
Add a little bit of uncertainty to the local channels to avoid
consuming precisely all spendable_msat on our side, which leads to
temporary channel failure if the spendable_msat changes during the
course of the payment.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
e41625fb29 renepay: bugfix refreshgossmap
Refresh gossmap once before we read the routehints.
Make sure that if channels in the routehints are public,
we retrieve their capacities from gossip.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
d39b0092ec renepay: bugfix reserved htlc in MCF
Reserved HTLCs were underestimated by floor (mathematical function)
use ceil instead.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
bb68e65b7a renepay: add precondition check + error msg
- add more checks
- add more error messages
- compute probabilities without fees during MCF
- compute probabilities with fees during get_routes

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
684d6dff50 renepay: patch to assertion htlc_total<=known_max
In theory we should not have htlc_total<=known_max.
But for some strange race condition we do sometimes.
Until we find a solution to ensure the correct state
of the uncertainty network we remove the assertion.
Thanks to signed arithmetic and MIN guards, the rest
of the code in linearize_channel can handle the
weird cases with known_max<htlc_total.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-19 10:11:35 -07:00
Lagrang3
7056018b15 renepay: add const probability cost
The probability for a success forward of x amount on a channel is now
computed as:

	P_success(x) = b * P_rene(x)

where P_rene(x) is the probability distribution proposed by Rene
Pickhardt with a uniform distribution on the known liquidity of the
channel and 'b' is a new parameter we add in this PR, by the name of
"base probability". The "base probability" represents the probability
for a channel in the network choosen at random to be able to forward at
least 1msat, ie. of being alive, non-depleted and with at least one HTLC
slot. We don't know the value of 'b', but for the moment we assume that
it is 0.98 and use that as default.

As a consequence the probability cost becomes non-linear and non-convex
because of the additional constant term:

	Cost(x) = - log P_success(x)
	        = - log b  -  - log P_rene(x)
		= - log b  +  Cost_rene(x)

We currently don't handle well base fees and neither this additional
"base probability" but we can as a first approximation linearize the
cost function in this way:

	Cost_linear(x) = (- log b)*x  +  Cost_rene_linear(x)

Changelog-Added: renepay: Add a dev parameter to renepay that represents
a constant probability of availability for all channels in the network.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-15 16:46:41 +09:30
Lagrang3
9afc10bf99 renepay: add cli option exclude
msggen cannot handle the complex type in renepay-exclude,
therefore I added a rule override for it, just like pay-exclude.

```
msggen cln-grpc/proto/node.proto
Traceback (most recent call last):
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/__main__.py", line 131, in <module>
    main()
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/__main__.py", line 115, in main
    run()
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/__main__.py", line 72, in run
    service = load_jsonrpc_service(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/utils/utils.py", line 241, in load_jsonrpc_service
    methods = [load_jsonrpc_method(name) for name in grpc_method_names]
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/utils/utils.py", line 209, in load_jsonrpc_method
    request = CompositeField.from_js(schema["methods"][rpc_name]['request'], path=name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/model.py", line 297, in from_js
    field = ArrayField.from_js(fpath, ftype)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/model.py", line 464, in from_js
    itemtype = UnionField.from_js(child_js, path)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rusty/devel/cvs/lightning/contrib/msggen/msggen/model.py", line 393, in from_js
    itemtype = PrimitiveField(
               ^^^^^^^^^^^^^^^
TypeError: PrimitiveField.__init__() missing 2 required positional arguments: 'added' and 'deprecated'
```

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
Changelog-EXPERIMENTAL: renepay: add cli option "exclude" to manually disable channels and nodes.
2024-08-12 22:44:58 -07:00
Lagrang3
44d53dc473 renepay: add a channel filtering paymod
We add a channel filtering paymod that disables channels that have very
low max_htlc. It can be expanded to consider other properties as well,
for instance high base fee, low capacity or high latency.

Changelog-Added: renepay: prune the network by disabling channels we
don't like, eg. very low max_htlc.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 22:44:58 -07:00
Lagrang3
c0dd3cd826 renepay: disabled channels in a map
Expecting to have more than just a bunch of disabled channels if we
prune the lightning network heavily I am changing the internal data
structure of disabledmap from a simple array to a hashtable.

Have a finer control over disabled channels by targeting
short_channel_id_dir instead of short_channel_id,
ie. we can disable a single direction of a channel.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 22:44:58 -07:00
Lagrang3
3f32f9ea10 renepay: fix strange gossmap_refresh log
Often I find this in the logs:
DEBUG   plugin-cln-renepay: gossmap ignored 94692259263600 channel updates

Apparently gossmap_refresh does not initialize the input variable num_channel_updates_rejected.
Fix this by explicitely initializing it before calling gossmap_refresh.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
5e4eb7bdc3 renepay: fix double free of tal object
routefail object allocation was linked to route,
we had a crash of the plugin with the following error:

0x561f424fc07a send_backtrace
	common/daemon.c:33
0x561f424fc102 crashdump
	common/daemon.c:75
0x7f5b0e7dc04f ???
	???:0
0x7f5b0e82ae2c ???
	???:0
0x7f5b0e7dbfb1 ???
	???:0
0x7f5b0e7c6471 ???
	???:0
0x561f4252581f call_error
	ccan/ccan/tal/tal.c:95
0x561f425258c8 check_bounds
	ccan/ccan/tal/tal.c:169
0x561f425258f9 to_tal_hdr
	ccan/ccan/tal/tal.c:179
0x561f42526283 tal_free
	ccan/ccan/tal/tal.c:525
0x561f424e5379 routefail_end
	plugins/renepay/routefail.c:52
0x561f424e557b handle_failure
	plugins/renepay/routefail.c:431

apparently there was a race condition for which the route was first
freed before we arrived to routefail_end where we manually free
routefail. I don't see how this could have happened, but anyways
this subtle bug can be avoided by linking the routefail to the payment.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
c4d0447773 renepay: remove __PRETTY_FUNCTION__
Remove __PRETTY_FUNCTION__ in favor of __func__

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
cc9b8b0a19 renepay: bugfix finalized routes
Changelog-Fixed: renepay: finalized routes have to be processed and
determine the payment status even after the payment goes into the
background (no current command active). Not doing so leads to finalized
routes getting stuck in the payment internal data structure and their
associated HTLCs in the uncertainty network don't get released.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
04bf0ae29d renepay: remove unnecessary payment list
We rather have a payment hash table to look up payments based on the
payment hash than having a list.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
6a1776d312 renepay: rm HTLCs from uncertainty on route free
Add remove_htlc in the route destructor.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
d6667e305b renepay: review routetracking
Add missing uncertainty update after payment success.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
ccb70de4eb renepay: minor fix
Use success_data_from_listsendpays to check if there are "complete"
sendpays instead of imposing the presence of "complete" sendpays as a
precondition for success_data_from_listsendpays.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
0c685f541e renepay: rethink the computed routes storage
Simply move the "computed routes" array from the payment to the
routetracker. It makes sense to put all temporary stages of routing into
a single data structure: the routetracker.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
1052945ac0 renepay: remove unnecessary rpc calls
There were some dummy rpc calls to waitblockheight in the payment workflow
to allow the function stack to clear. But it is better if some steps in
the payment are executed "atomically" to avoid strange race conditions.
For example: all steps between getting pending sendpays, computing new
routes and sending those routes.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
a339e553e8 renepay: listsendpay before computing routes
It feels unsafe to rely on the internal state of the plugin's database
to tell how many pending sendpays there are for the current payment.
The safest way is to assume lightningd knows and thus use listsendpay
before computing routes.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
ec431276ac renepay: global record of computed routes
Use a single global map of computed routes instead of one for each
payment.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
8b2d45e52e renepay: simpler routetracker
- remove payment pointer from routetracker, fetch payment if necessary
  from payment_hash;
- "have results" condition as a function call to routetracker.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:27:34 -07:00
Lagrang3
f16363b86d renepay: small tweak to decay time
The time of decay "TIMER_FORGET_SEC" was set to 1 hour,
which is very low, it would make the plugin try depleted channels after
just a couple of seconds for very small amounts.
I set it to 1 week ~ 6e5 seconds.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-12 13:17:21 -07:00
Lagrang3
db4a26d94a renepay: remove unused declaration
Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-08-09 14:38:17 +09:30
Lagrang3
07d4451a40 renepay: bugfix reading pending sendpays
When renepay starts, one of the first operations it does is to check for
pending sendpays for the same invoice. Those are added up to an internal
database to keep track of. Also the amount they deliver to the
destination is computed so that the current payment rpc call would try
to complete the payment for whatever amount remains to pay.
For an incomplete payment after calling the RPC renepay I found this in
the logs:

2024-05-22T19:44:19.853Z DEBUG   plugin-cln-renepay: There are pending sendpays to this invoice. groupid = 6 delivering = 0msat, last_partid = 10

Where delivering should be the sum of the amounts delivered by pending routes.
2024-08-09 14:38:17 +09:30
Rusty Russell
9b19eada30 common: promote useful routines from renepay.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-08-07 20:35:30 +09:30
Rusty Russell
11bfbf5deb libplugin: add data pointer for plugin convenience.
This avoids globals (and means memleak traverses the variables!): we
only change over the test plugin though, to avoid unnecessary churn.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-08-07 20:35:30 +09:30
ShahanaFarooqui
2ff3e55f08 plugin: Removing category, description and long_description from plugin_command struct
Changelog-None.
2024-07-31 14:42:58 +09:30
Rusty Russell
b29b96aae8 common: hoist scidd->pubkey conversion function into gossmap.
We will want to use it in the pay plugin too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-07-18 10:53:55 +09:30
Rusty Russell
34052f9960 bitcoin: hoist scid hash into common code.
A second user is coming.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-07-09 15:09:29 +02:00
Lagrang3
428b23c975 renepay: relax knowledge mod
Add a payment mod that relaxes the knowledge of the uncertainty network
as a function of time.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-05-23 15:15:57 -05:00
Lagrang3
2664faea38 renepay: put global variables into renepayconfig.h
Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-05-23 15:15:57 -05:00
Lagrang3
abfcc9bc5d renepay: clean unit tests
Delete temporary gossmap files after unit tests completion.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-05-20 16:58:49 -05:00
Lagrang3
638fff8526 renepay: fix memleak in CI
Keep a double-entry record of pending routes so that we can flag them as
not memory leaks.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-05-20 16:58:49 -05:00
Lagrang3
4b8d9116de renepay: test channel capacity unavailable
Add a test that checks `get_route` and `uncertainty_update` behaviour's
handling of missing channel capacities.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-05-20 16:58:49 -05:00
Rusty Russell
7040d49242 renepay: fix 32-bit compilation.
```
plugins/renepay/mods.c: In function 'payment_continue':
plugins/renepay/mods.c:63:7: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
   63 |       (u64)payment_virtual_program[payment->exec_state++];
      |       ^
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2024-05-17 14:06:36 -05:00