common: expose node_id_hash functions.

They're used in several places, and we're about to add more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-01-16 13:55:48 +10:30 committed by Alex Myers
parent 0faa8397c3
commit 6a95d3a25e
4 changed files with 22 additions and 26 deletions

View file

@ -3,6 +3,9 @@
#define LIGHTNING_COMMON_NODE_ID_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/htable/htable_type.h>
#include <common/pseudorand.h>
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 */

View file

@ -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,

View file

@ -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)
{

View file

@ -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);