Skip to content

Commit bb4dda6

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 <rusty@rustcorp.com.au>
1 parent 639452a commit bb4dda6

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},
@@ -1044,6 +1044,7 @@ static struct migration dbmigrations[] = {
10441044
{NULL, migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards},
10451045
{SQL("ALTER TABLE channel_funding_inflights ADD i_sent_sigs INTEGER DEFAULT 0"), NULL},
10461046
{SQL("ALTER TABLE channels ADD old_scids BLOB DEFAULT NULL;"), NULL},
1047+
{NULL, migrate_initialize_alias_local},
10471048
};
10481049

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

20152016
stmt = db_prepare_v2(db, SQL("SELECT id FROM channels"
2016-
" WHERE scid IS NOT NULL"
2017-
" AND alias_local IS NULL;"));
2017+
" WHERE alias_local IS NULL;"));
20182018
db_query_prepared(stmt);
20192019
while (db_step(stmt))
20202020
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
@@ -1819,7 +1819,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
18191819

18201820
scid = db_col_optional_scid(tmpctx, stmt, "scid");
18211821
old_scids = db_col_short_channel_id_arr(tmpctx, stmt, "old_scids");
1822-
alias[LOCAL] = db_col_optional_scid(tmpctx, stmt, "alias_local");
1822+
alias[LOCAL] = tal(tmpctx, struct short_channel_id);
1823+
*alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
18231824
alias[REMOTE] = db_col_optional_scid(tmpctx, stmt, "alias_remote");
18241825

18251826
ok &= wallet_shachain_load(w, db_col_u64(stmt, "shachain_remote_id"),
@@ -2099,7 +2100,8 @@ static struct closed_channel *wallet_stmt2closed_channel(const tal_t *ctx,
20992100
cc->peer_id = db_col_optional(cc, stmt, "p.node_id", node_id);
21002101
db_col_channel_id(stmt, "full_channel_id", &cc->cid);
21012102
cc->scid = db_col_optional_scid(cc, stmt, "scid");
2102-
cc->alias[LOCAL] = db_col_optional_scid(cc, stmt, "alias_local");
2103+
cc->alias[LOCAL] = tal(cc, struct short_channel_id);
2104+
*cc->alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
21032105
cc->alias[REMOTE] = db_col_optional_scid(cc, stmt, "alias_remote");
21042106
cc->opener = db_col_int(stmt, "funder");
21052107
cc->closer = db_col_int(stmt, "closer");
@@ -2654,11 +2656,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
26542656
db_bind_amount_msat(stmt, &chan->htlc_minimum_msat);
26552657
db_bind_amount_msat(stmt, &chan->htlc_maximum_msat);
26562658

2657-
if (chan->alias[LOCAL] != NULL)
2658-
db_bind_short_channel_id(stmt, *chan->alias[LOCAL]);
2659-
else
2660-
db_bind_null(stmt);
2661-
2659+
db_bind_short_channel_id(stmt, *chan->alias[LOCAL]);
26622660
if (chan->alias[REMOTE] != NULL)
26632661
db_bind_short_channel_id(stmt, *chan->alias[REMOTE]);
26642662
else

0 commit comments

Comments
 (0)