bitcoin: have random_scid() function.
What changed, and why it matters
This commit is a small code cleanup: it creates a shared helper function called random_scid() that generates a random channel identifier, and replaces two places that previously did this directly with calls to the new helper. It also adds a couple of test setup lines so wallet tests don't reuse stale channel maps. There is no security fix here—just refactoring and test hygiene.
No security action required; treat as routine refactoring and test maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff introduces random_scid() in bitcoin/short_channel_id.c/h using libsodium’s randombytes_buf(), then refactors lightningd/channel.c’s channel_set_random_local_alias() and wallet/db.c’s migrate_initialize_alias_local() to use it. wallet/test/run-wallet.c gets additional test initialization (creating a fresh channels_by_scid map) and a chanmap_remove call in test_channel_inflight_crud to avoid alias clashes during tests. No vulnerability is patched; behavior is functionally equivalent.
Changed components
bitcoin/short_channel_id.cbitcoin/short_channel_id.hlightningd/channel.cwallet/db.cwallet/test/run-wallet.cInspect captured patch +18 / −5
diff --git a/bitcoin/short_channel_id.c b/bitcoin/short_channel_id.c
index 80e7d28a..cc1de68c 100644
--- a/bitcoin/short_channel_id.c
+++ b/bitcoin/short_channel_id.c
@@ -1,6 +1,7 @@
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <ccan/tal/str/str.h>
+#include <sodium/randombytes.h>
#include <stdio.h>
#include <wire/wire.h>
@@ -99,3 +100,10 @@ struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max
scid.u64 = fromwire_u64(cursor, max);
return scid;
}
+
+struct short_channel_id random_scid(void)
+{
+ struct short_channel_id scid;
+ randombytes_buf(&scid, sizeof(scid));
+ return scid;
+}
diff --git a/bitcoin/short_channel_id.h b/bitcoin/short_channel_id.h
index cf382d69..1cd13f9a 100644
--- a/bitcoin/short_channel_id.h
+++ b/bitcoin/short_channel_id.h
@@ -98,4 +98,6 @@ void towire_short_channel_id(u8 **pptr,
struct short_channel_id short_channel_id);
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max);
+/* Set to random bytes */
+struct short_channel_id random_scid(void);
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */
diff --git a/lightningd/channel.c b/lightningd/channel.c
index ff673df1..589860e1 100644
--- a/lightningd/channel.c
+++ b/lightningd/channel.c
@@ -17,7 +17,6 @@
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
-#include <sodium/randombytes.h>
#include <wallet/txfilter.h>
#include <wire/peer_wire.h>
@@ -285,7 +284,7 @@ static void channel_set_random_local_alias(struct channel *channel)
{
assert(channel->alias[LOCAL] == NULL);
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
- randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));
+ *channel->alias[LOCAL] = random_scid();
/* We don't check for uniqueness. We would crash on a clash, but your machine is
* probably broken beyond repair if it gets two equal 64 bit numbers */
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
diff --git a/wallet/db.c b/wallet/db.c
index 15c54eb0..bc7b0256 100644
--- a/wallet/db.c
+++ b/wallet/db.c
@@ -2021,13 +2021,11 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
tal_free(stmt);
for (size_t i = 0; i < tal_count(ids); i++) {
- struct short_channel_id alias;
stmt = db_prepare_v2(db, SQL("UPDATE channels"
" SET alias_local = ?"
" WHERE id = ?;"));
/* We don't even check for clashes! */
- randombytes_buf(&alias, sizeof(alias));
- db_bind_short_channel_id(stmt, alias);
+ db_bind_short_channel_id(stmt, random_scid());
db_bind_u64(stmt, ids[i]);
db_exec_prepared_v2(stmt);
tal_free(stmt);
diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c
index 6cfe7f83..0e96b9d1 100644
--- a/wallet/test/run-wallet.c
+++ b/wallet/test/run-wallet.c
@@ -1366,6 +1366,9 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx
CHECK_MSG(!wallet_err, wallet_err);
w->max_channel_dbid = 0;
+ /* Create fresh channels map */
+ ld->channels_by_scid = tal(ld, struct channel_scid_map);
+ channel_scid_map_init(ld->channels_by_scid);
return w;
}
@@ -2092,6 +2095,9 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
/* do inflights get correctly added to the channel? */
wallet_inflight_add(w, inflight);
+ /* Hack to remove scids from htable so we don't clash! */
+ chanmap_remove(ld, chan, *chan->alias[LOCAL]);
+
/* do inflights get correctly loaded from the database? */
CHECK_MSG(c2 = wallet_channel_load(w, chan->dbid),
tal_fmt(w, "Load from DB"));
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.