Skip to content

Commit c27788f

Browse files
committed
bitcoin: have random_scid() function.
Signed-off-by: Rusty Russell <[email protected]>
1 parent 0fb91f8 commit c27788f

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

bitcoin/short_channel_id.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22
#include <bitcoin/short_channel_id.h>
33
#include <ccan/tal/str/str.h>
4+
#include <sodium/randombytes.h>
45
#include <stdio.h>
56
#include <wire/wire.h>
67

@@ -99,3 +100,10 @@ struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max
99100
scid.u64 = fromwire_u64(cursor, max);
100101
return scid;
101102
}
103+
104+
struct short_channel_id random_scid(void)
105+
{
106+
struct short_channel_id scid;
107+
randombytes_buf(&scid, sizeof(scid));
108+
return scid;
109+
}

bitcoin/short_channel_id.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ void towire_short_channel_id(u8 **pptr,
9898
struct short_channel_id short_channel_id);
9999
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max);
100100

101+
/* Set to random bytes */
102+
struct short_channel_id random_scid(void);
101103
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */

lightningd/channel.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <lightningd/opening_common.h>
1818
#include <lightningd/peer_control.h>
1919
#include <lightningd/subd.h>
20-
#include <sodium/randombytes.h>
2120
#include <wallet/txfilter.h>
2221
#include <wire/peer_wire.h>
2322

@@ -273,7 +272,7 @@ static void channel_set_random_local_alias(struct channel *channel)
273272
{
274273
assert(channel->alias[LOCAL] == NULL);
275274
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
276-
randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));
275+
*channel->alias[LOCAL] = random_scid();
277276
/* We don't check for uniqueness. We would crash on a clash, but your machine is
278277
* probably broken beyond repair if it gets two equal 64 bit numbers */
279278
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);

wallet/db.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,13 +2020,11 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
20202020
tal_free(stmt);
20212021

20222022
for (size_t i = 0; i < tal_count(ids); i++) {
2023-
struct short_channel_id alias;
20242023
stmt = db_prepare_v2(db, SQL("UPDATE channels"
20252024
" SET alias_local = ?"
20262025
" WHERE id = ?;"));
20272026
/* We don't even check for clashes! */
2028-
randombytes_buf(&alias, sizeof(alias));
2029-
db_bind_short_channel_id(stmt, alias);
2027+
db_bind_short_channel_id(stmt, random_scid());
20302028
db_bind_u64(stmt, ids[i]);
20312029
db_exec_prepared_v2(stmt);
20322030
tal_free(stmt);

wallet/test/run-wallet.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,9 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx
13661366
CHECK_MSG(!wallet_err, wallet_err);
13671367
w->max_channel_dbid = 0;
13681368

1369+
/* Create fresh channels map */
1370+
ld->channels_by_scid = tal(ld, struct channel_scid_map);
1371+
channel_scid_map_init(ld->channels_by_scid);
13691372
return w;
13701373
}
13711374

@@ -2091,6 +2094,9 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
20912094
/* do inflights get correctly added to the channel? */
20922095
wallet_inflight_add(w, inflight);
20932096

2097+
/* Hack to remove scids from htable so we don't clash! */
2098+
chanmap_remove(ld, chan, *chan->alias[LOCAL]);
2099+
20942100
/* do inflights get correctly loaded from the database? */
20952101
CHECK_MSG(c2 = wallet_channel_load(w, chan->dbid),
20962102
tal_fmt(w, "Load from DB"));

0 commit comments

Comments
 (0)