Skip to content

Commit 8246198

Browse files
committed
bitcoin: hash_scid and hash_scidd public functions.
We reimplemented this redundantly: hash_scid was called short_channel_id_hash, so I obviously missed it. Rename, and implement hash_scidd helper too. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent f7c0ac7 commit 8246198

File tree

11 files changed

+20
-58
lines changed

11 files changed

+20
-58
lines changed

bitcoin/short_channel_id.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static inline bool short_channel_id_eq(struct short_channel_id a,
1717
return a.u64 == b.u64;
1818
}
1919

20-
static inline size_t short_channel_id_hash(struct short_channel_id scid)
20+
static inline size_t hash_scid(struct short_channel_id scid)
2121
{
2222
/* scids cost money to generate, so simple hash works here */
2323
return (scid.u64 >> 32) ^ (scid.u64 >> 16) ^ scid.u64;
@@ -46,6 +46,12 @@ static inline bool short_channel_id_dir_eq(const struct short_channel_id_dir *a,
4646
return short_channel_id_eq(a->scid, b->scid) && a->dir == b->dir;
4747
}
4848

49+
static inline size_t hash_scidd(const struct short_channel_id_dir *scidd)
50+
{
51+
/* Bottom bit is common, so use bit 4 for direction */
52+
return hash_scid(scidd->scid) | (scidd->dir << 4);
53+
}
54+
4955
static inline u32 short_channel_id_blocknum(struct short_channel_id scid)
5056
{
5157
return scid.u64 >> 40;

common/gossmap.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ static bool chanidx_eq_id(const ptrint_t *pidx,
2929
struct short_channel_id pidxid = chanidx_id(pidx);
3030
return short_channel_id_eq(pidxid, scid);
3131
}
32-
static size_t scid_hash(const struct short_channel_id scid)
33-
{
34-
return siphash24(siphash_seed(), &scid, sizeof(scid));
35-
}
36-
HTABLE_DEFINE_NODUPS_TYPE(ptrint_t, chanidx_id, scid_hash, chanidx_eq_id,
32+
HTABLE_DEFINE_NODUPS_TYPE(ptrint_t, chanidx_id, hash_scid, chanidx_eq_id,
3733
chanidx_htable);
3834

3935
static struct node_id nodeidx_id(const ptrint_t *pidx);

connectd/connectd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static bool scid_to_node_id_eq_scid(const struct scid_to_node_id *scid_to_node_i
260260
* we use this to forward onion messages which specify the next hop by scid/dir. */
261261
HTABLE_DEFINE_NODUPS_TYPE(struct scid_to_node_id,
262262
scid_to_node_id_keyof,
263-
short_channel_id_hash,
263+
hash_scid,
264264
scid_to_node_id_eq_scid,
265265
scid_htable);
266266

lightningd/channel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ static inline bool scid_to_channel_eq_scid(const struct scid_to_channel *scidcha
891891
/* Define channel_scid_map */
892892
HTABLE_DEFINE_NODUPS_TYPE(struct scid_to_channel,
893893
scid_to_channel_key,
894-
short_channel_id_hash,
894+
hash_scid,
895895
scid_to_channel_eq_scid,
896896
channel_scid_map);
897897

plugins/askrene/askrene.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,4 @@ static inline struct askrene *get_askrene(struct plugin *plugin)
9595
{
9696
return plugin_get_data(plugin, struct askrene);
9797
}
98-
99-
/* Convenience routine for hash tables */
100-
static inline size_t hash_scidd(const struct short_channel_id_dir *scidd)
101-
{
102-
/* scids cost money to generate, so simple hash works here */
103-
return (scidd->scid.u64 >> 32) ^ (scidd->scid.u64 >> 16) ^ (scidd->scid.u64 << 1) ^ scidd->dir;
104-
}
105-
10698
#endif /* LIGHTNING_PLUGINS_ASKRENE_ASKRENE_H */

plugins/askrene/layer.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ local_channel_scid(const struct local_channel *lc)
8181
return lc->scid;
8282
}
8383

84-
static size_t hash_scid(const struct short_channel_id scid)
85-
{
86-
/* scids cost money to generate, so simple hash works here */
87-
return (scid.u64 >> 32) ^ (scid.u64 >> 16) ^ scid.u64;
88-
}
89-
9084
static inline bool local_channel_eq_scid(const struct local_channel *lc,
9185
const struct short_channel_id scid)
9286
{

plugins/channel_hint.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33
#include <common/memleak.h>
44
#include <plugins/channel_hint.h>
55

6-
size_t channel_hint_hash(const struct short_channel_id_dir *out)
7-
{
8-
struct siphash24_ctx ctx;
9-
siphash24_init(&ctx, siphash_seed());
10-
siphash24_update(&ctx, &out->scid.u64, sizeof(u64));
11-
siphash24_update(&ctx, &out->dir, sizeof(int));
12-
return siphash24_done(&ctx);
13-
}
14-
156
const struct short_channel_id_dir *channel_hint_keyof(const struct channel_hint *out)
167
{
178
return &out->scid;
@@ -20,8 +11,7 @@ const struct short_channel_id_dir *channel_hint_keyof(const struct channel_hint
2011
bool channel_hint_eq(const struct channel_hint *a,
2112
const struct short_channel_id_dir *b)
2213
{
23-
return short_channel_id_eq(a->scid.scid, b->scid) &&
24-
a->scid.dir == b->dir;
14+
return short_channel_id_dir_eq(&a->scid, b);
2515
}
2616

2717
void channel_hint_to_json(const char *name, const struct channel_hint *hint,

plugins/channel_hint.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ struct channel_hint {
4040
struct amount_msat capacity;
4141
};
4242

43-
size_t channel_hint_hash(const struct short_channel_id_dir *out);
44-
4543
const struct short_channel_id_dir *channel_hint_keyof(const struct channel_hint *out);
4644

4745
bool channel_hint_eq(const struct channel_hint *a,
4846
const struct short_channel_id_dir *b);
4947

5048
HTABLE_DEFINE_NODUPS_TYPE(struct channel_hint, channel_hint_keyof,
51-
channel_hint_hash, channel_hint_eq, channel_hint_map)
49+
hash_scidd, channel_hint_eq, channel_hint_map)
5250

5351
/* A collection of channel_hint instances, allowing us to handle and
5452
* update them more easily. */

plugins/renepay/chan_extra.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ static inline bool chan_extra_eq_scid(const struct chan_extra *cd,
5050
return short_channel_id_eq(scid, cd->scid);
5151
}
5252

53-
HTABLE_DEFINE_NODUPS_TYPE(struct chan_extra, chan_extra_scid, short_channel_id_hash,
53+
HTABLE_DEFINE_NODUPS_TYPE(struct chan_extra, chan_extra_scid,
54+
hash_scid,
5455
chan_extra_eq_scid,
5556
chan_extra_map);
5657

plugins/renepay/disabledmap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void disabledmap_add_channel(struct disabledmap *p,
4747
struct short_channel_id_dir scidd)
4848
{
4949
struct short_channel_id_dir *ptr_scidd =
50-
scidd_map_get(p->disabled_map, scidd);
50+
scidd_map_get(p->disabled_map, &scidd);
5151
if (ptr_scidd) {
5252
/* htable allows for duplicates, but we don't want duplicates.
5353
*/
@@ -64,7 +64,7 @@ void disabledmap_warn_channel(struct disabledmap *p,
6464
struct short_channel_id_dir scidd)
6565
{
6666
struct short_channel_id_dir *ptr_scidd =
67-
scidd_map_get(p->warned_map, scidd);
67+
scidd_map_get(p->warned_map, &scidd);
6868
if (ptr_scidd) {
6969
/* htable allows for duplicates, but we don't want duplicates.
7070
*/
@@ -84,7 +84,7 @@ void disabledmap_add_node(struct disabledmap *p, struct node_id node)
8484
bool disabledmap_channel_is_warned(struct disabledmap *p,
8585
struct short_channel_id_dir scidd)
8686
{
87-
return scidd_map_get(p->warned_map, scidd) != NULL;
87+
return scidd_map_get(p->warned_map, &scidd) != NULL;
8888
}
8989

9090
bitmap *tal_disabledmap_get_bitmap(const tal_t *ctx, struct disabledmap *p,

0 commit comments

Comments
 (0)