Skip to content

Commit 5656bec

Browse files
committed
wallet: we can assume local_alias field is non-null.
We have a migration which ensures this, but then I discovered that did *not* address channels without an SCID yet. So fixed the migration, and simpligied the code. Signed-off-by: Rusty Russell <[email protected]>
1 parent c27788f commit 5656bec

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

wallet/db.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ static struct migration dbmigrations[] = {
10261026
{SQL("ALTER TABLE channels ADD remote_htlc_maximum_msat BIGINT DEFAULT NULL;"), NULL},
10271027
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
10281028
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
1029-
{NULL, migrate_initialize_alias_local},
1029+
{NULL, NULL}, /* old migrate_initialize_alias_local */
10301030
{SQL("CREATE TABLE addresses ("
10311031
" keyidx BIGINT,"
10321032
" addrtype INTEGER)"), NULL},
@@ -1043,6 +1043,7 @@ static struct migration dbmigrations[] = {
10431043
{SQL("ALTER TABLE channel_funding_inflights ADD locked_scid BIGINT DEFAULT 0;"), NULL},
10441044
{NULL, migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards},
10451045
{SQL("ALTER TABLE channels ADD old_scids BLOB DEFAULT NULL;"), NULL},
1046+
{NULL, migrate_initialize_alias_local},
10461047
};
10471048

10481049
/**
@@ -2012,8 +2013,7 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
20122013
u64 *ids = tal_arr(tmpctx, u64, 0);
20132014

20142015
stmt = db_prepare_v2(db, SQL("SELECT id FROM channels"
2015-
" WHERE scid IS NOT NULL"
2016-
" AND alias_local IS NULL;"));
2016+
" WHERE alias_local IS NULL;"));
20172017
db_query_prepared(stmt);
20182018
while (db_step(stmt))
20192019
tal_arr_expand(&ids, db_col_u64(stmt, "id"));

wallet/test/run-wallet.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
17941794
struct pubkey pk;
17951795
struct node_id id;
17961796
struct changed_htlc *last_commit;
1797+
struct short_channel_id local_alias;
17971798
secp256k1_ecdsa_signature *sig = tal(w, secp256k1_ecdsa_signature);
17981799
u8 *scriptpubkey = tal_arr(ctx, u8, 100);
17991800
secp256k1_ecdsa_signature *node_sig1 = tal(w, secp256k1_ecdsa_signature);
@@ -1850,6 +1851,8 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
18501851
/* Init channel inflights */
18511852
list_head_init(&c1.inflights);
18521853
c1.type = type;
1854+
local_alias = random_scid();
1855+
c1.alias[LOCAL] = &local_alias;
18531856

18541857
db_begin_transaction(w->db);
18551858
CHECK(!wallet_err);

wallet/wallet.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
18141814

18151815
scid = db_col_optional_scid(tmpctx, stmt, "scid");
18161816
old_scids = db_col_short_channel_id_arr(tmpctx, stmt, "old_scids");
1817-
alias[LOCAL] = db_col_optional_scid(tmpctx, stmt, "alias_local");
1817+
alias[LOCAL] = tal(tmpctx, struct short_channel_id);
1818+
*alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
18181819
alias[REMOTE] = db_col_optional_scid(tmpctx, stmt, "alias_remote");
18191820

18201821
ok &= wallet_shachain_load(w, db_col_u64(stmt, "shachain_remote_id"),
@@ -2094,7 +2095,8 @@ static struct closed_channel *wallet_stmt2closed_channel(const tal_t *ctx,
20942095
cc->peer_id = db_col_optional(cc, stmt, "p.node_id", node_id);
20952096
db_col_channel_id(stmt, "full_channel_id", &cc->cid);
20962097
cc->scid = db_col_optional_scid(cc, stmt, "scid");
2097-
cc->alias[LOCAL] = db_col_optional_scid(cc, stmt, "alias_local");
2098+
cc->alias[LOCAL] = tal(cc, struct short_channel_id);
2099+
*cc->alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
20982100
cc->alias[REMOTE] = db_col_optional_scid(cc, stmt, "alias_remote");
20992101
cc->opener = db_col_int(stmt, "funder");
21002102
cc->closer = db_col_int(stmt, "closer");
@@ -2649,11 +2651,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
26492651
db_bind_amount_msat(stmt, &chan->htlc_minimum_msat);
26502652
db_bind_amount_msat(stmt, &chan->htlc_maximum_msat);
26512653

2652-
if (chan->alias[LOCAL] != NULL)
2653-
db_bind_short_channel_id(stmt, *chan->alias[LOCAL]);
2654-
else
2655-
db_bind_null(stmt);
2656-
2654+
db_bind_short_channel_id(stmt, *chan->alias[LOCAL]);
26572655
if (chan->alias[REMOTE] != NULL)
26582656
db_bind_short_channel_id(stmt, *chan->alias[REMOTE]);
26592657
else

0 commit comments

Comments
 (0)