Skip to content

Commit

Permalink
renepay: relax knowledge mod
Browse files Browse the repository at this point in the history
Add a payment mod that relaxes the knowledge of the uncertainty network
as a function of time.

Signed-off-by: Lagrang3 <[email protected]>
  • Loading branch information
Lagrang3 authored and endothermicdev committed May 23, 2024
1 parent 2664fae commit 428b23c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
49 changes: 35 additions & 14 deletions plugins/renepay/mods.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,27 @@ static struct command_result *checktimeout_cb(struct payment *payment)

REGISTER_PAYMENT_MODIFIER(checktimeout, checktimeout_cb);

/*****************************************************************************
* knowledgerelax
*
* Reduce the knowledge of the network as time goes by.
*/

static struct command_result *knowledgerelax_cb(struct payment *payment)
{
const u64 now_sec = time_now().ts.tv_sec;
enum renepay_errorcode err = uncertainty_relax(
pay_plugin->uncertainty, now_sec - pay_plugin->last_time);
if (err)
plugin_err(pay_plugin->plugin,
"uncertainty_relax failed with error %s",
renepay_errorcode_name(err));
pay_plugin->last_time = now_sec;
return payment_continue(payment);
}

REGISTER_PAYMENT_MODIFIER(knowledgerelax, knowledgerelax_cb);

/*****************************************************************************
* alwaystrue
*
Expand Down Expand Up @@ -1068,29 +1089,29 @@ REGISTER_PAYMENT_CONDITION(retry, retry_cb);
*/
// TODO
// add shadow route
// add knowledge decay
// add check pre-approved invoice
void *payment_virtual_program[] = {
/*0*/ OP_CALL, &previous_sendpays_pay_mod,
/*2*/ OP_CALL, &selfpay_pay_mod,
/*4*/ OP_CALL, &getmychannels_pay_mod,
/*6*/ OP_CALL, &routehints_pay_mod,
/*4*/ OP_CALL, &knowledgerelax_pay_mod,
/*6*/ OP_CALL, &getmychannels_pay_mod,
/*8*/ OP_CALL, &routehints_pay_mod,
// TODO: add a channel filter, for example disable channels that have
// htlcmax < 0.1% of payment amount, or base fee > 100msat, or
// proportional_fee > 10%, or capacity < 10% payment amount
// TODO shadow_additions
/* do */
/*8*/ OP_CALL, &refreshgossmap_pay_mod,
/*10*/ OP_CALL, &checktimeout_pay_mod,
/*12*/ OP_CALL, &compute_routes_pay_mod,
/*14*/ OP_CALL, &send_routes_pay_mod,
/*10*/ OP_CALL, &refreshgossmap_pay_mod,
/*12*/ OP_CALL, &checktimeout_pay_mod,
/*14*/ OP_CALL, &compute_routes_pay_mod,
/*16*/ OP_CALL, &send_routes_pay_mod,
/*do*/
/*16*/ OP_CALL, &checktimeout_pay_mod,
/*18*/ OP_CALL, &sleep_pay_mod,
/*20*/ OP_CALL, &collect_results_pay_mod,
/*18*/ OP_CALL, &checktimeout_pay_mod,
/*20*/ OP_CALL, &sleep_pay_mod,
/*22*/ OP_CALL, &collect_results_pay_mod,
/*while*/
/*22*/ OP_IF, &nothaveresults_pay_cond, (void *)16,
/*24*/ OP_IF, &nothaveresults_pay_cond, (void *)18,
/* while */
/*25*/ OP_IF, &retry_pay_cond, (void *)8,
/*28*/ OP_CALL, &end_pay_mod, /* safety net, default failure if reached */
/*20*/ NULL};
/*27*/ OP_IF, &retry_pay_cond, (void *)10,
/*30*/ OP_CALL, &end_pay_mod, /* safety net, default failure if reached */
/*32*/ NULL};
20 changes: 20 additions & 0 deletions plugins/renepay/uncertainty.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <plugins/renepay/renepayconfig.h>
#include <plugins/renepay/uncertainty.h>

void uncertainty_route_success(struct uncertainty *uncertainty,
Expand Down Expand Up @@ -177,3 +178,22 @@ struct chan_extra *uncertainty_find_channel(struct uncertainty *uncertainty,
{
return chan_extra_map_get(uncertainty->chan_extra_map, scid);
}

enum renepay_errorcode uncertainty_relax(struct uncertainty *uncertainty,
double seconds)
{
assert(seconds >= 0);
const double fraction = MIN(seconds / TIMER_FORGET_SEC, 1.0);
struct chan_extra_map *chan_extra_map =
uncertainty_get_chan_extra_map(uncertainty);
struct chan_extra_map_iter it;
for (struct chan_extra *ce = chan_extra_map_first(chan_extra_map, &it);
ce; ce = chan_extra_map_next(chan_extra_map, &it)) {
enum renepay_errorcode err =
chan_extra_relax_fraction(ce, fraction);

if (err)
return err;
}
return RENEPAY_NOERROR;
}
10 changes: 10 additions & 0 deletions plugins/renepay/uncertainty.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ bool uncertainty_set_liquidity(struct uncertainty *uncertainty,
struct chan_extra *uncertainty_find_channel(struct uncertainty *uncertainty,
const struct short_channel_id scid);

/* Adds randomness to the current state simulating the natural evolution of the
* liquidity in the network. It should be a markovian process with the minimum
* requirement that the transition operator T satisfies:
* T(t1) T(t2) = T(t1+t2)
* i.e. the transition operator is a one parameter semigroup.
* For the moment we omit the continuous and linear aspects of the problem for a
* lack for formulation. */
enum renepay_errorcode uncertainty_relax(struct uncertainty *uncertainty,
double seconds);

#endif /* LIGHTNING_PLUGINS_RENEPAY_UNCERTAINTY_H */

0 comments on commit 428b23c

Please sign in to comment.