common/json: add a helper for json to u16

As Rusty pointed out to me, the gossip protocol restricts cltvs to u16
so at least we'll use this helper for them.
This commit is contained in:
darosior
2019-11-06 14:46:55 +01:00
committed by Rusty Russell
parent 5766231d98
commit 16f5af00c7
3 changed files with 24 additions and 7 deletions

View File

@@ -90,6 +90,21 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok,
return true;
}
bool json_to_u16(const char *buffer, const jsmntok_t *tok,
short unsigned int *num)
{
uint64_t u64;
if (!json_to_u64(buffer, tok, &u64))
return false;
*num = u64;
/* Just in case it doesn't fit. */
if (*num != u64)
return false;
return true;
}
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
{
char *end;

View File

@@ -36,6 +36,10 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok,
bool json_to_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num);
/* Extract number from this (may be a string, or a number literal) */
bool json_to_u16(const char *buffer, const jsmntok_t *tok,
uint16_t *num);
/* Extract double from this (must be a number literal) */
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);

View File

@@ -820,8 +820,6 @@ static struct command_result *add_shadow_route(struct command *cmd,
const jsmntok_t *chan, *best = NULL;
size_t i;
u64 sample = 0;
/* FIXME: Use route_info's cltv_expiry_delta member instead */
u32 cltv_fuzz;
struct route_info *route = tal_arr(NULL, struct route_info, 1);
json_for_each_arr(i, chan, channels) {
@@ -835,9 +833,9 @@ static struct command_result *add_shadow_route(struct command *cmd,
continue;
/* Don't use if total would exceed 1/4 of our time allowance. */
json_to_number(buf, json_get_member(buf, chan, "delay"),
&cltv_fuzz);
if ((pc->final_cltv + cltv_fuzz) * 4 > pc->maxdelay)
json_to_u16(buf, json_get_member(buf, chan, "delay"),
&route[0].cltv_expiry_delta);
if ((pc->final_cltv + route[0].cltv_expiry_delta) * 4 > pc->maxdelay)
continue;
json_to_number(buf, json_get_member(buf, chan, "base_fee_millisatoshi"),
@@ -857,14 +855,14 @@ static struct command_result *add_shadow_route(struct command *cmd,
return start_pay_attempt(cmd, pc, "Initial attempt");
}
pc->final_cltv += cltv_fuzz;
pc->final_cltv += route[0].cltv_expiry_delta;
pc->shadow_dest = json_strdup(pc, buf,
json_get_member(buf, best, "destination"));
route_msatoshi(&pc->msat, pc->msat, route, 1);
tal_append_fmt(&pc->ps->shadow,
"Added %u cltv delay, %u base fee, and %u ppm fee "
"for shadow to %s.",
cltv_fuzz, route[0].fee_base_msat,
route[0].cltv_expiry_delta, route[0].fee_base_msat,
route[0].fee_proportional_millionths,
pc->shadow_dest);
tal_free(route);