Skip to content

Commit

Permalink
gossipd: increase randomness in route selection.
Browse files Browse the repository at this point in the history
We have a seed, which is for (future!) unit testing consistency.  This
makes it change every time, so our pay_direct_test is more useful.

I tried restarting the noed around the loop, but it tended to fail
rebinding to the same port for some reason?

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 6, 2019
1 parent 38a2f6c commit 6a26b0c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions common/pseudorand.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ uint64_t pseudorand(uint64_t max)
return isaac64_next_uint(&isaac64, max);
}

uint64_t pseudorand_u64(void)
{
init_if_needed();

return isaac64_next_uint64(&isaac64);
}

const struct siphash_seed *siphash_seed(void)
{
init_if_needed();
Expand Down
5 changes: 5 additions & 0 deletions common/pseudorand.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/
uint64_t pseudorand(uint64_t max);

/**
* pseudorand - pseudo (guessable!) random number between 0 and UINT64_MAX.
*/
uint64_t pseudorand_u64(void);

/**
* Get the siphash seed for hash tables.
*/
Expand Down
2 changes: 1 addition & 1 deletion gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon,
/* routing.c does all the hard work; can return NULL. */
hops = get_route(tmpctx, daemon->rstate, &source, &destination,
msatoshi, riskfactor, final_cltv,
fuzz, siphash_seed(), excluded, max_hops);
fuzz, pseudorand_u64(), excluded, max_hops);

out = towire_gossip_getroute_reply(NULL, hops);
daemon_conn_send(daemon->master, take(out));
Expand Down
7 changes: 5 additions & 2 deletions gossipd/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ struct route_hop *get_route(const tal_t *ctx, struct routing_state *rstate,
const struct pubkey *destination,
const u64 msatoshi, double riskfactor,
u32 final_cltv,
double fuzz, const struct siphash_seed *base_seed,
double fuzz, u64 seed,
const struct short_channel_id_dir *excluded,
size_t max_hops)
{
Expand All @@ -1507,9 +1507,12 @@ struct route_hop *get_route(const tal_t *ctx, struct routing_state *rstate,
struct route_hop *hops;
struct node *n;
u64 *saved_capacity;
struct siphash_seed base_seed;

saved_capacity = tal_arr(tmpctx, u64, tal_count(excluded));

base_seed.u.u64[0] = base_seed.u.u64[1] = seed;

/* Temporarily set excluded channels' capacity to zero. */
for (size_t i = 0; i < tal_count(excluded); i++) {
struct chan *chan = get_channel(rstate, &excluded[i].scid);
Expand All @@ -1522,7 +1525,7 @@ struct route_hop *get_route(const tal_t *ctx, struct routing_state *rstate,

route = find_route(ctx, rstate, source, destination, msatoshi,
riskfactor / BLOCKS_PER_YEAR / 10000,
fuzz, base_seed, max_hops, &fee);
fuzz, &base_seed, max_hops, &fee);

/* Now restore the capacity. */
for (size_t i = 0; i < tal_count(excluded); i++) {
Expand Down
2 changes: 1 addition & 1 deletion gossipd/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ struct route_hop *get_route(const tal_t *ctx, struct routing_state *rstate,
const u64 msatoshi, double riskfactor,
u32 final_cltv,
double fuzz,
const struct siphash_seed *base_seed,
u64 seed,
const struct short_channel_id_dir *excluded,
size_t max_hops);
/* Disable channel(s) based on the given routing failure. */
Expand Down

0 comments on commit 6a26b0c

Please sign in to comment.