Skip to content

Commit 639452a

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

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

@@ -285,7 +284,7 @@ static void channel_set_random_local_alias(struct channel *channel)
285284
{
286285
assert(channel->alias[LOCAL] == NULL);
287286
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
288-
randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));
287+
*channel->alias[LOCAL] = random_scid();
289288
/* We don't check for uniqueness. We would crash on a clash, but your machine is
290289
* probably broken beyond repair if it gets two equal 64 bit numbers */
291290
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
@@ -2021,13 +2021,11 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
20212021
tal_free(stmt);
20222022

20232023
for (size_t i = 0; i < tal_count(ids); i++) {
2024-
struct short_channel_id alias;
20252024
stmt = db_prepare_v2(db, SQL("UPDATE channels"
20262025
" SET alias_local = ?"
20272026
" WHERE id = ?;"));
20282027
/* We don't even check for clashes! */
2029-
randombytes_buf(&alias, sizeof(alias));
2030-
db_bind_short_channel_id(stmt, alias);
2028+
db_bind_short_channel_id(stmt, random_scid());
20312029
db_bind_u64(stmt, ids[i]);
20322030
db_exec_prepared_v2(stmt);
20332031
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

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

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

0 commit comments

Comments
 (0)