mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
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:
parent
0faa8397c3
commit
6a95d3a25e
4 changed files with 22 additions and 26 deletions
|
@ -3,6 +3,9 @@
|
||||||
#define LIGHTNING_COMMON_NODE_ID_H
|
#define LIGHTNING_COMMON_NODE_ID_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <bitcoin/pubkey.h>
|
#include <bitcoin/pubkey.h>
|
||||||
|
#include <ccan/crypto/siphash24/siphash24.h>
|
||||||
|
#include <ccan/htable/htable_type.h>
|
||||||
|
#include <common/pseudorand.h>
|
||||||
|
|
||||||
struct node_id {
|
struct node_id {
|
||||||
u8 k[PUBKEY_CMPR_LEN];
|
u8 k[PUBKEY_CMPR_LEN];
|
||||||
|
@ -43,4 +46,21 @@ static inline int node_id_idx(const struct node_id *id1,
|
||||||
/* marshal/unmarshal functions */
|
/* marshal/unmarshal functions */
|
||||||
void towire_node_id(u8 **pptr, const struct node_id *id);
|
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);
|
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 */
|
#endif /* LIGHTNING_COMMON_NODE_ID_H */
|
||||||
|
|
|
@ -100,16 +100,8 @@ static const struct node_id *peer_keyof(const struct peer *peer)
|
||||||
return &peer->id;
|
return &peer->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*~ We also need to define a hashing function. siphash24 is a fast yet
|
/*~ We reuse node_id_hash from common/node_id.h, which uses siphash
|
||||||
* cryptographic hash in ccan/crypto/siphash24; we might be able to get away
|
* and a per-run seed. */
|
||||||
* 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 also define an equality function: is this element equal to this key? */
|
/*~ We also define an equality function: is this element equal to this key? */
|
||||||
static bool peer_eq_node_id(const struct peer *peer,
|
static bool peer_eq_node_id(const struct peer *peer,
|
||||||
|
|
|
@ -98,11 +98,6 @@ static const struct node_id *node_cache_id(const struct node_id_cache *nc)
|
||||||
return &nc->node_id;
|
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,
|
static bool node_id_cache_eq(const struct node_id_cache *nc,
|
||||||
const struct node_id *node_id)
|
const struct node_id *node_id)
|
||||||
{
|
{
|
||||||
|
|
|
@ -192,17 +192,6 @@ static struct command_result *json_getroute(struct command *cmd,
|
||||||
return command_finished(cmd, js);
|
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,
|
HTABLE_DEFINE_TYPE(struct node_id, node_id_keyof, node_id_hash, node_id_eq,
|
||||||
node_map);
|
node_map);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue