From 428b23c9752c9d2646d35aee06cbae8177e4ad28 Mon Sep 17 00:00:00 2001 From: Lagrang3 Date: Wed, 22 May 2024 16:32:49 +0100 Subject: [PATCH] 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 --- plugins/renepay/mods.c | 49 +++++++++++++++++++++++++---------- plugins/renepay/uncertainty.c | 20 ++++++++++++++ plugins/renepay/uncertainty.h | 10 +++++++ 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/plugins/renepay/mods.c b/plugins/renepay/mods.c index 9000e259754d..fd3554ac7fdf 100644 --- a/plugins/renepay/mods.c +++ b/plugins/renepay/mods.c @@ -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 * @@ -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, ¬haveresults_pay_cond, (void *)16, + /*24*/ OP_IF, ¬haveresults_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}; diff --git a/plugins/renepay/uncertainty.c b/plugins/renepay/uncertainty.c index c2c437970ab1..f3f397e13909 100644 --- a/plugins/renepay/uncertainty.c +++ b/plugins/renepay/uncertainty.c @@ -1,4 +1,5 @@ #include "config.h" +#include #include void uncertainty_route_success(struct uncertainty *uncertainty, @@ -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; +} diff --git a/plugins/renepay/uncertainty.h b/plugins/renepay/uncertainty.h index a591ece4773e..b8f9e29c9a9b 100644 --- a/plugins/renepay/uncertainty.h +++ b/plugins/renepay/uncertainty.h @@ -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 */