diff --git a/common/node_id.h b/common/node_id.h index 1f23c9d33..1e2f8d063 100644 --- a/common/node_id.h +++ b/common/node_id.h @@ -3,6 +3,9 @@ #define LIGHTNING_COMMON_NODE_ID_H #include "config.h" #include +#include +#include +#include struct node_id { u8 k[PUBKEY_CMPR_LEN]; @@ -43,4 +46,21 @@ static inline int node_id_idx(const struct node_id *id1, /* marshal/unmarshal functions */ void towire_node_id(u8 **pptr, const struct node_id *id); void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id); + +/* Hash table functions for node ids */ +static inline const struct node_id *node_id_keyof(const struct node_id *id) +{ + return id; +} + +/* We need to define a hashing function. siphash24 is a fast yet + * cryptographic hash in ccan/crypto/siphash24; we might be able to get away + * with a slightly faster hash with fewer guarantees, but it's good hygiene to + * use this unless it's a proven bottleneck. siphash_seed() is a function in + * common/pseudorand which sets up a seed for our hashing; it's different + * every time the program is run. */ +static inline size_t node_id_hash(const struct node_id *id) +{ + return siphash24(siphash_seed(), id->k, sizeof(id->k)); +} #endif /* LIGHTNING_COMMON_NODE_ID_H */ diff --git a/connectd/connectd.h b/connectd/connectd.h index c94088745..8a35a02b8 100644 --- a/connectd/connectd.h +++ b/connectd/connectd.h @@ -100,16 +100,8 @@ static const struct node_id *peer_keyof(const struct peer *peer) return &peer->id; } -/*~ We also need to define a hashing function. siphash24 is a fast yet - * cryptographic hash in ccan/crypto/siphash24; we might be able to get away - * with a slightly faster hash with fewer guarantees, but it's good hygiene to - * use this unless it's a proven bottleneck. siphash_seed() is a function in - * common/pseudorand which sets up a seed for our hashing; it's different - * every time the program is run. */ -static size_t node_id_hash(const struct node_id *id) -{ - return siphash24(siphash_seed(), id->k, sizeof(id->k)); -} +/*~ We reuse node_id_hash from common/node_id.h, which uses siphash + * and a per-run seed. */ /*~ We also define an equality function: is this element equal to this key? */ static bool peer_eq_node_id(const struct peer *peer, diff --git a/lightningd/log.c b/lightningd/log.c index ff7f1602e..85b450a42 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -98,11 +98,6 @@ static const struct node_id *node_cache_id(const struct node_id_cache *nc) return &nc->node_id; } -static size_t node_id_hash(const struct node_id *id) -{ - return siphash24(siphash_seed(), id->k, sizeof(id->k)); -} - static bool node_id_cache_eq(const struct node_id_cache *nc, const struct node_id *node_id) { diff --git a/plugins/topology.c b/plugins/topology.c index 510a7901a..943dcd54d 100644 --- a/plugins/topology.c +++ b/plugins/topology.c @@ -192,17 +192,6 @@ static struct command_result *json_getroute(struct command *cmd, return command_finished(cmd, js); } -static const struct node_id *node_id_keyof(const struct node_id *id) -{ - return id; -} - -static size_t node_id_hash(const struct node_id *id) -{ - return siphash24(siphash_seed(), id->k, sizeof(id->k)); -} - - HTABLE_DEFINE_TYPE(struct node_id, node_id_keyof, node_id_hash, node_id_eq, node_map);