From ca4730b5fff0b7c3453c0a6cf2f383ca009824be Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Sep 2020 13:19:35 +0930 Subject: [PATCH] lightningd: use proper receivable calc for routehint selection. Signed-off-by: Rusty Russell --- lightningd/invoice.c | 41 +-------------------- lightningd/test/run-invoice-select-inchan.c | 9 +++++ 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 0bb5a24a0..7c1a46598 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -436,39 +436,6 @@ static struct command_result *parse_fallback(struct command *cmd, return NULL; } -/** incoming_capacity - * - * @brief Determine the ability of the peer to pay us. - * - * @param ld - the lightningd. - * @param c - the channel to check. - * @param capacity_to_pay_us - out; if this returns true, - * the pointed-to `struct amount_msat` will contain how - * much the peer can pay us at maximum. - * - * @return false if the peer cannot pay to us, true if - * the peer can pay us and `capacity_to_pay_us` is set. - */ -static bool incoming_capacity(struct lightningd *ld, - struct channel *c, - struct amount_msat *capacity_to_pay_us) -{ - struct amount_msat their_msat; - if (!amount_sat_sub_msat(&their_msat, c->funding, c->our_msat)) { - log_broken(ld->log, - "underflow: funding %s - our_msat %s", - type_to_string(tmpctx, struct amount_sat, - &c->funding), - type_to_string(tmpctx, struct amount_msat, - &c->our_msat)); - return false; - } - if (!amount_msat_sub_sat(capacity_to_pay_us, their_msat, - c->our_config.channel_reserve)) - return false; - return true; -} - /* * From array of incoming channels [inchan], find suitable ones for * a payment-to-us of [amount_needed], using criteria: @@ -530,9 +497,7 @@ static struct route_info **select_inchan(const tal_t *ctx, 0 ^ ^ ^ funding our_reserve our_msat */ - /* Can the peer pay to us, and if so how much? */ - if (!incoming_capacity(ld, c, &capacity_to_pay_us)) - continue; + capacity_to_pay_us = channel_amount_receivable(c); /* Does the peer have sufficient balance to pay us, * even after having taken into account their reserve? */ @@ -628,9 +593,7 @@ static struct route_info **select_inchan_mpp(const tal_t *ctx, continue; } - /* Can the peer pay to us? */ - if (!incoming_capacity(ld, c, &capacity_to_pay_us)) - continue; + capacity_to_pay_us = channel_amount_receivable(c); /* Is the channel in the inchans input? */ found = NULL; diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index b0dc9ab1c..767ddc513 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -142,6 +142,9 @@ u32 get_feerate(const struct fee_states *fee_states UNNEEDED, enum side opener UNNEEDED, enum side side UNNEEDED) { fprintf(stderr, "get_feerate called!\n"); abort(); } +/* Generated stub for hash_htlc_key */ +size_t hash_htlc_key(const struct htlc_key *htlc_key UNNEEDED) +{ fprintf(stderr, "hash_htlc_key called!\n"); abort(); } /* Generated stub for htlc_is_trimmed */ bool htlc_is_trimmed(enum side htlc_owner UNNEEDED, struct amount_msat htlc_amount UNNEEDED, @@ -679,13 +682,17 @@ static void add_peer(struct lightningd *ld, int n, enum channel_state state, memset(&peer->id, n, sizeof(peer->id)); list_head_init(&peer->channels); list_add_tail(&ld->peers, &peer->list); + peer->ld = ld; c->state = state; c->owner = connected ? (void *)peer : NULL; + c->opener = LOCAL; + c->peer = peer; /* Channel has incoming capacity n*1000 - 1 millisatoshi */ c->funding.satoshis = n+1; c->our_msat = AMOUNT_MSAT(1); c->our_config.channel_reserve = AMOUNT_SAT(1); + c->our_config.htlc_minimum = AMOUNT_MSAT(0); c->channel_info.their_config.channel_reserve = AMOUNT_SAT(0); list_add_tail(&peer->channels, &c->list); } @@ -715,6 +722,8 @@ int main(void) ld = tal(tmpctx, struct lightningd); list_head_init(&ld->peers); + htlc_in_map_init(&ld->htlcs_in); + chainparams = chainparams_for_network("regtest"); inchans = tal_arr(tmpctx, struct route_info, 0); deadends = tal_arrz(tmpctx, bool, 100);