common: promote useful routines from renepay.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-08-07 11:19:53 +09:30
parent a29d135bee
commit 9b19eada30
6 changed files with 33 additions and 32 deletions

View file

@ -144,6 +144,19 @@ bool amount_msat_eq_sat(struct amount_msat msat, struct amount_sat sat);
/* a / b */
double amount_msat_ratio(struct amount_msat a, struct amount_msat b);
/* min(a,b) and max(a,b) */
static inline struct amount_msat amount_msat_min(struct amount_msat a,
struct amount_msat b)
{
return amount_msat_less(a, b) ? a : b;
}
static inline struct amount_msat amount_msat_max(struct amount_msat a,
struct amount_msat b)
{
return amount_msat_greater(a, b) ? a : b;
}
/* Check whether this asset is actually the main / fee-paying asset of the
* current chain. */
bool amount_asset_is_main(struct amount_asset *asset);

View file

@ -242,6 +242,19 @@ bool gossmap_chan_has_capacity(const struct gossmap_chan *chan,
int direction,
struct amount_msat amount);
/* Convenience routines to get htlc min/max as amount_msat */
static inline struct amount_msat
gossmap_chan_htlc_max(const struct gossmap_chan *chan, const int dir)
{
return amount_msat(fp16_to_u64(chan->half[dir].htlc_max));
}
static inline struct amount_msat
gossmap_chan_htlc_min(const struct gossmap_chan *chan, const int dir)
{
return amount_msat(fp16_to_u64(chan->half[dir].htlc_min));
}
/* Remove a channel from the map (warning! realloc can move gossmap_chan
* and gossmap_node ptrs!) */
void gossmap_remove_chan(struct gossmap *map, struct gossmap_chan *chan);

View file

@ -115,19 +115,6 @@ struct chan_extra *new_chan_extra(struct chan_extra_map *chan_extra_map,
const struct short_channel_id scid,
struct amount_msat capacity);
/* Helper to find the min of two amounts */
static inline struct amount_msat amount_msat_min(struct amount_msat a,
struct amount_msat b)
{
return amount_msat_less(a, b) ? a : b;
}
/* Helper to find the max of two amounts */
static inline struct amount_msat amount_msat_max(struct amount_msat a,
struct amount_msat b)
{
return amount_msat_greater(a, b) ? a : b;
}
/* Update the knowledge that this (channel,direction) can send x msat.*/
enum renepay_errorcode
chan_extra_can_send(struct chan_extra_map *chan_extra_map,
@ -190,18 +177,6 @@ enum renepay_errorcode channel_liquidity(struct amount_msat *liquidity,
const struct gossmap_chan *chan,
const int dir);
/* Helpers to get the htlc_max and htlc_min of a channel. */
static inline struct amount_msat
channel_htlc_max(const struct gossmap_chan *chan, const int dir)
{
return amount_msat(fp16_to_u64(chan->half[dir].htlc_max));
}
static inline struct amount_msat
channel_htlc_min(const struct gossmap_chan *chan, const int dir)
{
return amount_msat(fp16_to_u64(chan->half[dir].htlc_min));
}
/* inputs
* @chan: a channel
* @recv: how much can we send to this channels

View file

@ -93,7 +93,7 @@ flow_maximum_deliverable(struct amount_msat *max_deliverable,
if(bad_channel)*bad_channel = flow->path[0];
return err;
}
x = amount_msat_min(x, channel_htlc_max(flow->path[0], flow->dirs[0]));
x = amount_msat_min(x, gossmap_chan_htlc_max(flow->path[0], flow->dirs[0]));
if(amount_msat_zero(x))
{
@ -127,7 +127,7 @@ flow_maximum_deliverable(struct amount_msat *max_deliverable,
struct amount_msat x_new =
amount_msat_min(forward_cap, liquidity_cap);
x_new = amount_msat_min(
x_new, channel_htlc_max(flow->path[i], flow->dirs[i]));
x_new, gossmap_chan_htlc_max(flow->path[i], flow->dirs[i]));
/* safety check: amounts decrease along the route */
assert(amount_msat_less_eq(x_new, x));

View file

@ -497,7 +497,7 @@ static bool linearize_channel(const struct pay_parameters *params,
/* An extra bound on capacity, here we use it to reduce the flow such
* that it does not exceed htlcmax. */
s64 cap_on_capacity =
channel_htlc_max(c, dir).millisatoshis/1000; /* Raw: linearize_channel */
gossmap_chan_htlc_max(c, dir).millisatoshis/1000; /* Raw: linearize_channel */
capacity[0]=a;
cost[0]=0;
@ -1363,12 +1363,12 @@ get_flow_paths(const tal_t *ctx, const struct gossmap *gossmap,
/* obtain the supremum htlc_min along the route
*/
sup_htlc_min = amount_msat_max(
sup_htlc_min, channel_htlc_min(c, dir));
sup_htlc_min, gossmap_chan_htlc_min(c, dir));
/* obtain the infimum htlc_max along the route
*/
inf_htlc_max = amount_msat_min(
inf_htlc_max, channel_htlc_max(c, dir));
inf_htlc_max, gossmap_chan_htlc_max(c, dir));
}
s64 htlc_max=inf_htlc_max.millisatoshis/1000;/* Raw: need htlc_max in sats to do arithmetic operations.*/

View file

@ -86,9 +86,9 @@ route_check_constraints(struct route *route, struct gossmap *gossmap,
// check that we stay within the htlc max and min limits
if (amount_msat_greater(hop->amount,
channel_htlc_max(chan, dir)) ||
gossmap_chan_htlc_max(chan, dir)) ||
amount_msat_less(hop->amount,
channel_htlc_min(chan, dir))) {
gossmap_chan_htlc_min(chan, dir))) {
bitmap_set_bit(disabled_bitmap,
gossmap_chan_idx(gossmap, chan));
return RENEPAY_BAD_CHANNEL;