diff --git a/common/json_stream.c b/common/json_stream.c index 31a5367b1..60517b3fb 100644 --- a/common/json_stream.c +++ b/common/json_stream.c @@ -626,6 +626,14 @@ void json_add_amount_msat(struct json_stream *result, json_add_u64(result, msatfieldname, msat.millisatoshis); /* Raw: low-level helper */ } +void json_add_amount_sat(struct json_stream *result, + const char *satfieldname, + struct amount_sat sat) +{ + assert(strends(satfieldname, "_sat") || streq(satfieldname, "sat")); + json_add_u64(result, satfieldname, sat.satoshis); /* Raw: low-level helper */ +} + void json_add_amount_sat_msat(struct json_stream *result, const char *msatfieldname, struct amount_sat sat) diff --git a/common/json_stream.h b/common/json_stream.h index eeca272c3..9cc539e29 100644 --- a/common/json_stream.h +++ b/common/json_stream.h @@ -5,10 +5,10 @@ #define LIGHTNING_COMMON_JSON_STREAM_H #include "config.h" -#include #define JSMN_STRICT 1 # include +#include #include #include #include @@ -334,6 +334,11 @@ void json_add_amount_msat(struct json_stream *result, struct amount_msat msat) NO_NULL_ARGS; +void json_add_amount_sat(struct json_stream *result, + const char *satfieldname, + struct amount_sat sat) + NO_NULL_ARGS; + /* Adds an 'msat' field */ void json_add_amount_sat_msat(struct json_stream *result, const char *msatfieldname, diff --git a/common/route.c b/common/route.c index 69039c277..520db6e75 100644 --- a/common/route.c +++ b/common/route.c @@ -105,8 +105,7 @@ static bool dijkstra_to_hops(struct route_hop **hops, return false; gossmap_chan_get_capacity(gossmap, c, &total); - if (!amount_sat_to_msat(&((*hops[num_hops]).total_amount), total)) - abort(); + (*hops)[num_hops].capacity = total; (*hops)[num_hops].amount = *amount; (*hops)[num_hops].delay = *cltv; diff --git a/common/route.h b/common/route.h index 6d8936220..ce57cd875 100644 --- a/common/route.h +++ b/common/route.h @@ -18,7 +18,7 @@ struct gossmap_node; * @direction: 0 (dest node_id < src node_id), 1 (dest node_id > src). * @node_id: the node_id of the destination of this hop. * @amount: amount to send through this hop. - * @total_amount: The amount the channel was funded with. + * @capacity: The amount the channel was funded with. * @delay: total cltv delay at this hop. */ struct route_hop { @@ -26,7 +26,7 @@ struct route_hop { int direction; struct node_id node_id; struct amount_msat amount; - struct amount_msat total_amount; + struct amount_sat capacity; u32 delay; }; diff --git a/plugins/channel_hint.c b/plugins/channel_hint.c index 497a0bbba..3a6a57a28 100644 --- a/plugins/channel_hint.c +++ b/plugins/channel_hint.c @@ -9,8 +9,7 @@ void channel_hint_to_json(const char *name, const struct channel_hint *hint, json_add_short_channel_id_dir(dest, "scid", hint->scid); json_add_amount_msat(dest, "estimated_capacity_msat", hint->estimated_capacity); - json_add_amount_msat(dest, "capacity_msat", - hint->capacity); + json_add_amount_sat(dest, "capacity_sat", hint->capacity); json_add_bool(dest, "enabled", hint->enabled); json_object_end(dest); } @@ -37,8 +36,12 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint) * overall / refill_rate`. */ struct amount_msat refill; + struct amount_msat capacity; + if (!amount_sat_to_msat(&capacity, hint->capacity)) + abort(); + u64 seconds = now.ts.tv_sec - hint->timestamp; - if (!amount_msat_mul(&refill, hint->capacity, seconds)) + if (!amount_msat_mul(&refill, capacity, seconds)) abort(); refill = amount_msat_div(refill, PAY_REFILL_TIME); @@ -47,9 +50,8 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint) abort(); /* Clamp the value to the `overall_capacity` */ - if (amount_msat_greater(hint->estimated_capacity, - hint->capacity)) - hint->estimated_capacity = hint->capacity; + if (amount_msat_greater(hint->estimated_capacity, capacity)) + hint->estimated_capacity = capacity; /* TODO This is rather coarse. We could map the disabled flag to having 0msat capacity, and then relax from there. But it'd @@ -65,8 +67,8 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint) /* We report this hint as useless, if the hint does not * restrict the channel, i.e., if it is enabled and the * estimate is the same as the overall capacity. */ - return !hint->enabled || amount_msat_greater(hint->capacity, - hint->estimated_capacity); + return !hint->enabled || + amount_msat_greater(capacity, hint->estimated_capacity); } /** @@ -90,7 +92,7 @@ struct channel_hint *channel_hint_from_json(const tal_t *ctx, JSON_SCAN(json_to_u32, &hint->timestamp), JSON_SCAN(json_to_short_channel_id_dir, &hint->scid), JSON_SCAN(json_to_msat, &hint->estimated_capacity), - JSON_SCAN(json_to_msat, &hint->capacity), + JSON_SCAN(json_to_sat, &hint->capacity), JSON_SCAN(json_to_bool, &hint->enabled)); if (ret != NULL) diff --git a/plugins/channel_hint.h b/plugins/channel_hint.h index 771ae7650..5a2ffcbb1 100644 --- a/plugins/channel_hint.h +++ b/plugins/channel_hint.h @@ -40,10 +40,10 @@ struct channel_hint { /* Non-null if we are one endpoint of this channel */ struct local_hint *local; - /* The total `amount_msat` that were used to fund the + /* The total `amount_sat` that were used to fund the * channel. This is always smaller gte the - * estimated_capacity */ - struct amount_msat capacity; + * estimated_capacity (after normalization) */ + struct amount_sat capacity; }; /* A collection of channel_hint instances, allowing us to handle and diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index 9f07ade8b..b7bc1811c 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -409,7 +409,7 @@ static void channel_hints_update(struct payment *p, const struct short_channel_id scid, int direction, bool enabled, bool local, const struct amount_msat *estimated_capacity, - const struct amount_msat overall_capacity, + const struct amount_sat overall_capacity, u16 *htlc_budget) { struct payment *root = payment_root(p); @@ -476,8 +476,9 @@ static void channel_hints_update(struct payment *p, newhint.local = NULL; if (estimated_capacity != NULL) newhint.estimated_capacity = *estimated_capacity; - else - newhint.estimated_capacity = overall_capacity; + else if (!amount_sat_to_msat(&newhint.estimated_capacity, + overall_capacity)) + abort(); tal_arr_expand(&root->channel_hints, newhint); @@ -505,7 +506,7 @@ static void payment_exclude_most_expensive(struct payment *p) } } channel_hints_update(p, e->scid, e->direction, false, false, NULL, - e->total_amount, NULL); + e->capacity, NULL); } static void payment_exclude_longest_delay(struct payment *p) @@ -521,7 +522,7 @@ static void payment_exclude_longest_delay(struct payment *p) } } channel_hints_update(p, e->scid, e->direction, false, false, NULL, - e->total_amount, NULL); + e->capacity, NULL); } static struct amount_msat payment_route_fee(struct payment *p) @@ -1467,7 +1468,7 @@ handle_final_failure(struct command *cmd, case WIRE_TEMPORARY_NODE_FAILURE: case WIRE_REQUIRED_NODE_FEATURE_MISSING: case WIRE_INVALID_ONION_BLINDING: - case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: + case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: case WIRE_MPP_TIMEOUT: goto error; } @@ -1519,9 +1520,9 @@ handle_intermediate_failure(struct command *cmd, *... * - MUST return a `final_incorrect_htlc_amount` error. */ - case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: - case WIRE_FINAL_INCORRECT_CLTV_EXPIRY: - case WIRE_FINAL_INCORRECT_HTLC_AMOUNT: + case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: + case WIRE_FINAL_INCORRECT_CLTV_EXPIRY: + case WIRE_FINAL_INCORRECT_HTLC_AMOUNT: /* FIXME: Document in BOLT that intermediates must not return this! */ case WIRE_MPP_TIMEOUT: goto strange_error; @@ -1532,7 +1533,7 @@ handle_intermediate_failure(struct command *cmd, case WIRE_REQUIRED_CHANNEL_FEATURE_MISSING: /* All of these result in the channel being marked as disabled. */ channel_hints_update(root, errchan->scid, errchan->direction, - false, false, NULL, errchan->total_amount, + false, false, NULL, errchan->capacity, NULL); break; @@ -1551,7 +1552,7 @@ handle_intermediate_failure(struct command *cmd, * remember the amount we tried as an estimate. */ channel_hints_update(root, errchan->scid, errchan->direction, true, false, &estimated, - errchan->total_amount, NULL); + errchan->capacity, NULL); goto error; } @@ -1671,13 +1672,13 @@ static u8 *channel_update_from_onion_error(const tal_t *ctx, onion_message, &unused_msat, &channel_update) && !fromwire_fee_insufficient(ctx, - onion_message, &unused_msat, + onion_message, &unused_msat, &channel_update) && !fromwire_incorrect_cltv_expiry(ctx, - onion_message, &unused32, + onion_message, &unused32, &channel_update) && !fromwire_expiry_too_soon(ctx, - onion_message, + onion_message, &channel_update)) /* No channel update. */ return NULL; @@ -2631,6 +2632,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer, const jsmntok_t *toks, struct payment *p) { struct listpeers_channel **chans; + struct amount_sat capacity; chans = json_to_listpeers_channels(tmpctx, buffer, toks); @@ -2655,6 +2657,9 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer, else htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs; + if(!amount_msat_to_sat(&capacity, chans[i]->total_msat)) + abort(); + /* If we have both a scid and a local alias we want to * use the scid, and mark the alias as * unusable. Otherwise `getroute` might return the @@ -2667,19 +2672,19 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer, channel_hints_update( p, *chans[i]->scid, chans[i]->direction, enabled, true, &chans[i]->spendable_msat, - chans[i]->total_msat, &htlc_budget); + capacity, &htlc_budget); if (chans[i]->alias[LOCAL] != NULL) channel_hints_update(p, *chans[i]->alias[LOCAL], chans[i]->direction, false /* not enabled */, true, &AMOUNT_MSAT(0), - chans[i]->total_msat, + capacity, &htlc_budget); } else { channel_hints_update( p, *chans[i]->alias[LOCAL], chans[i]->direction, enabled, true, &chans[i]->spendable_msat, - chans[i]->total_msat, &htlc_budget); + capacity, &htlc_budget); } } @@ -3170,7 +3175,7 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p) hop.amount = dest_amount; hop.delay = route_cltv(d->final_cltv, routehint + i + 1, tal_count(routehint) - i - 1); - hop.total_amount = estimate; + hop.capacity = amount_msat_to_sat_round_down(estimate); /* Should we get a failure inside the routehint we'll * need the direction so we can exclude it. Luckily @@ -3539,7 +3544,7 @@ static void direct_pay_override(struct payment *p) { p->route[0].scid = hint->scid.scid; p->route[0].direction = hint->scid.dir; p->route[0].node_id = *p->route_destination; - p->route[0].total_amount = hint->capacity; + p->route[0].capacity = hint->capacity; paymod_log(p, LOG_DBG, "Found a direct channel (%s) with sufficient " "capacity, skipping route computation.", @@ -3947,7 +3952,7 @@ static void route_exclusions_step_cb(struct route_exclusions_data *d, struct route_exclusion *e = exclusions[i]; /* We don't need the details if we skip anyway. */ - struct amount_msat total = AMOUNT_MSAT(0); + struct amount_sat total = AMOUNT_SAT(0); if (e->type == EXCLUDE_CHANNEL) { channel_hints_update(p, e->u.chan_id.scid,