From 589cb673ce37665b163b74e25e7693d10908e3ea Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 18 Dec 2017 14:45:38 +1030 Subject: [PATCH] routing: remove negative fee support. We can't get them; channel_update doesn't support it. # 1M nodes: $ /gossipd/test/run-bench-find_route 1000000 1 > /tmp/out => 47677 msec Signed-off-by: Rusty Russell --- gossipd/routing.c | 22 +++++++++------------- gossipd/routing.h | 4 ++-- gossipd/test/run-bench-find_route.c | 2 +- gossipd/test/run-find_route-specific.c | 2 +- gossipd/test/run-find_route.c | 2 +- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/gossipd/routing.c b/gossipd/routing.c index 9c49b96de..02bb760ef 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -246,11 +246,11 @@ static void clear_bfg(struct node_map *nodes) } } -static s64 connection_fee(const struct node_connection *c, u64 msatoshi) +static u64 connection_fee(const struct node_connection *c, u64 msatoshi) { - s64 fee; + u64 fee; - if (mul_overflows_s64(c->proportional_fee, msatoshi)) + if (mul_overflows_u64(c->proportional_fee, msatoshi)) return INFINITE; fee = (c->proportional_fee * msatoshi) / 1000000; /* This can't overflow: c->base_fee is a u32 */ @@ -259,12 +259,8 @@ static s64 connection_fee(const struct node_connection *c, u64 msatoshi) /* Risk of passing through this channel. We insert a tiny constant here * in order to prefer shorter routes, all things equal. */ -static u64 risk_fee(s64 amount, u32 delay, double riskfactor) +static u64 risk_fee(u64 amount, u32 delay, double riskfactor) { - /* If fees are so negative we're making money, ignore risk. */ - if (amount < 0) - return 1; - return 1 + amount * delay * riskfactor / BLOCKS_PER_YEAR / 10000; } @@ -278,7 +274,7 @@ static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor) assert(c->dst == node); for (h = 0; h < ROUTING_MAX_HOPS; h++) { /* FIXME: Bias against smaller channels. */ - s64 fee; + u64 fee; u64 risk; if (node->bfg[h].total == INFINITE) @@ -287,8 +283,8 @@ static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor) fee = connection_fee(c, node->bfg[h].total); risk = node->bfg[h].risk + risk_fee(node->bfg[h].total + fee, c->delay, riskfactor); - if (node->bfg[h].total + (s64)fee + (s64)risk - < c->src->bfg[h+1].total + (s64)c->src->bfg[h+1].risk) { + if (node->bfg[h].total + fee + risk + < c->src->bfg[h+1].total + c->src->bfg[h+1].risk) { SUPERVERBOSE("...%s can reach here in hoplen %zu total %"PRIu64, type_to_string(trc, struct pubkey, &c->src->id), @@ -303,7 +299,7 @@ static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor) static struct node_connection * find_route(const tal_t *ctx, struct routing_state *rstate, const struct pubkey *from, const struct pubkey *to, u64 msatoshi, - double riskfactor, s64 *fee, struct node_connection ***route) + double riskfactor, u64 *fee, struct node_connection ***route) { struct node *n, *src, *dst; struct node_map_iter it; @@ -770,7 +766,7 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate, struct node_connection **route; u64 total_amount; unsigned int total_delay; - s64 fee; + u64 fee; struct route_hop *hops; int i; struct node_connection *first_conn; diff --git a/gossipd/routing.h b/gossipd/routing.h index 2edb7da83..da91a1c64 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -14,7 +14,7 @@ struct node_connection { /* millisatoshi. */ u32 base_fee; /* millionths */ - s32 proportional_fee; + u32 proportional_fee; /* Delay for HTLC in blocks.*/ u32 delay; @@ -54,7 +54,7 @@ struct node { /* Temporary data for routefinding. */ struct { /* Total to get to here from target. */ - s64 total; + u64 total; /* Total risk premium of this route. */ u64 risk; /* Where that came from. */ diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index d277cf3f3..cab13a9ad 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -187,7 +187,7 @@ int main(int argc, char *argv[]) for (size_t i = 0; i < num_runs; i++) { struct pubkey from = nodeid(pseudorand(num_nodes)); struct pubkey to = nodeid(pseudorand(num_nodes)); - s64 fee; + u64 fee; struct node_connection **route, *nc; nc = find_route(ctx, rstate, &from, &to, diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 908bfa6b6..db1f3b575 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -61,7 +61,7 @@ int main(void) struct node_connection *nc; struct routing_state *rstate; struct pubkey a, b, c; - s64 fee; + u64 fee; struct node_connection **route; secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 21785cee0..bfa1a6526 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -72,7 +72,7 @@ int main(void) struct routing_state *rstate; struct pubkey a, b, c, d; struct privkey tmp; - s64 fee; + u64 fee; struct node_connection **route; secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY