mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-09 09:04:25 +01:00
renepay: add information decay linear in time
Signed-off-by: Lagrang3 <eduardo.quintana@pm.me>
This commit is contained in:
@@ -15,6 +15,9 @@
|
||||
#define SUPERVERBOSE_ENABLED 1
|
||||
#endif
|
||||
|
||||
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
bool chan_extra_is_busy(struct chan_extra const * const ce)
|
||||
{
|
||||
if(ce==NULL)return false;
|
||||
@@ -277,7 +280,8 @@ static void chan_extra_relax_(
|
||||
if(!amount_msat_sub(&new_a,ce->half[dir].known_min,down))
|
||||
new_a = AMOUNT_MSAT(0);
|
||||
if(!amount_msat_add(&new_b,ce->half[dir].known_max,up))
|
||||
new_b = amount_msat_min(new_b,ce->capacity);
|
||||
new_b = ce->capacity;
|
||||
new_b = amount_msat_min(new_b,ce->capacity);
|
||||
|
||||
ce->half[dir].known_min = new_a;
|
||||
ce->half[dir].known_max = new_b;
|
||||
@@ -301,6 +305,23 @@ void chan_extra_relax(
|
||||
chan_extra_relax_(ce,dir,x,y);
|
||||
}
|
||||
|
||||
/* Forget the channel information by a fraction of the capacity. */
|
||||
void chan_extra_relax_fraction(
|
||||
struct chan_extra* ce,
|
||||
double fraction)
|
||||
{
|
||||
fraction = fabs(fraction); // this number is always non-negative
|
||||
fraction = MIN(1.0,fraction); // this number cannot be greater than 1.
|
||||
struct amount_msat delta = amount_msat(ce->capacity.millisatoshis * fraction); /* Raw: get a fraction of the capacity */
|
||||
|
||||
/* The direction here is not important because the 'down' and the 'up'
|
||||
* limits are changed by the same amount.
|
||||
* Notice that if chan[0] with capacity C changes from (a,b) to (a-d,b+d)
|
||||
* then its counterpart chan[1] changes from (C-b,C-a) to (C-b-d,C-a+d),
|
||||
* hence both dirs are applied the same transformation. */
|
||||
chan_extra_relax_(ce,/*dir=*/0,delta,delta);
|
||||
}
|
||||
|
||||
|
||||
/* Returns either NULL, or an entry from the hash */
|
||||
struct chan_extra_half *
|
||||
|
||||
@@ -178,6 +178,11 @@ void chan_extra_relax(struct chan_extra_map *chan_extra_map,
|
||||
struct amount_msat down,
|
||||
struct amount_msat up);
|
||||
|
||||
/* Forget the channel information by a fraction of the capacity. */
|
||||
void chan_extra_relax_fraction(
|
||||
struct chan_extra* ce,
|
||||
double fraction);
|
||||
|
||||
|
||||
/* Returns either NULL, or an entry from the hash */
|
||||
struct chan_extra_half *get_chan_extra_half_by_scid(struct chan_extra_map *chan_extra_map,
|
||||
|
||||
@@ -93,6 +93,7 @@ static const char *init(struct plugin *p,
|
||||
pay_plugin->ctx = notleak_with_children(tal(p,tal_t));
|
||||
pay_plugin->plugin = p;
|
||||
pay_plugin->rexmit_timer=NULL;
|
||||
pay_plugin->last_time = 0;
|
||||
|
||||
rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
|
||||
"{id:%}", JSON_SCAN(json_to_node_id, &pay_plugin->my_id));
|
||||
@@ -731,6 +732,7 @@ static struct command_result *json_paystatus(struct command *cmd,
|
||||
return command_finished(cmd, ret);
|
||||
}
|
||||
|
||||
|
||||
/* Taken from ./plugins/pay.c
|
||||
*
|
||||
* We are interested in any prior attempts to pay this payment_hash /
|
||||
@@ -1177,10 +1179,10 @@ static struct command_result *json_pay(struct command *cmd,
|
||||
}
|
||||
tal_free(maxfee);
|
||||
|
||||
if (time_now().ts.tv_sec > invexpiry)
|
||||
const u64 now_sec = time_now().ts.tv_sec;
|
||||
if (now_sec > invexpiry)
|
||||
return renepay_fail(renepay, PAY_INVOICE_EXPIRED, "Invoice expired");
|
||||
|
||||
|
||||
/* To construct the uncertainty network we need to perform the following
|
||||
* steps:
|
||||
* 1. check that there is a 1-to-1 map between channels in gossmap
|
||||
@@ -1199,6 +1201,16 @@ static struct command_result *json_pay(struct command *cmd,
|
||||
pay_plugin->chan_extra_map);
|
||||
|
||||
|
||||
/* TODO(eduardo): We use a linear function to decide how to decay the
|
||||
* channel information. Other shapes could be used.
|
||||
* Also the choice of the proportional parameter TIMER_FORGET_SEC is
|
||||
* arbitrary.
|
||||
* Another idea is to measure time in blockheight. */
|
||||
const double fraction = (now_sec - pay_plugin->last_time)*1.0/TIMER_FORGET_SEC;
|
||||
uncertainty_network_relax_fraction(pay_plugin->chan_extra_map,
|
||||
fraction);
|
||||
pay_plugin->last_time = now_sec;
|
||||
|
||||
// TODO(eduardo): are there route hints for B12?
|
||||
// Add any extra hidden channel revealed by the routehints to the uncertainty network.
|
||||
if(invstr_is_b11)
|
||||
@@ -1218,12 +1230,6 @@ static struct command_result *json_pay(struct command *cmd,
|
||||
|
||||
json_add_sha256(req->js, "payment_hash", &p->payment_hash);
|
||||
return send_outreq(cmd->plugin, req);
|
||||
|
||||
// TODO(eduardo):
|
||||
// - get time since last payment,
|
||||
// - forget a portion of the bounds
|
||||
// - note that if sufficient time has passed, then we would forget
|
||||
// everything use TIMER_FORGET_SEC.
|
||||
}
|
||||
|
||||
static void handle_sendpay_failure_renepay(
|
||||
|
||||
@@ -94,6 +94,10 @@ struct pay_plugin {
|
||||
// sendpays.
|
||||
/* Timers. */
|
||||
struct plugin_timer *rexmit_timer;
|
||||
|
||||
/* It allows us to measure elapsed time
|
||||
* and forget channel information accordingly. */
|
||||
u64 last_time;
|
||||
};
|
||||
|
||||
/* Set in init */
|
||||
|
||||
@@ -346,3 +346,17 @@ bool uncertainty_network_update_from_listpeerchannels(
|
||||
malformed:
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Forget ALL channels information by a fraction of the capacity. */
|
||||
void uncertainty_network_relax_fraction(
|
||||
struct chan_extra_map* chan_extra_map,
|
||||
double fraction)
|
||||
{
|
||||
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))
|
||||
{
|
||||
chan_extra_relax_fraction(ce,fraction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,4 +44,9 @@ bool uncertainty_network_update_from_listpeerchannels(
|
||||
const char *buf,
|
||||
const jsmntok_t *toks);
|
||||
|
||||
/* Forget ALL channels information by a fraction of the capacity. */
|
||||
void uncertainty_network_relax_fraction(
|
||||
struct chan_extra_map* chan_extra_map,
|
||||
double fraction);
|
||||
|
||||
#endif /* LIGHTNING_PLUGINS_RENEPAY_UNCERTAINTY_NETWORK_H */
|
||||
|
||||
Reference in New Issue
Block a user