Skip to content

Commit

Permalink
wallet: ensure all established channels have aliases.
Browse files Browse the repository at this point in the history
Commit dac8964 set aliases for channels at
creation time, but neglected to convert channels in the database.  Do that now!

Fixes: #7039
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 8, 2024
1 parent 34aaa9c commit b5bd907
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <lightningd/channel.h>
#include <lightningd/hsm_control.h>
#include <lightningd/plugin_hook.h>
#include <sodium/randombytes.h>
#include <stddef.h>
#include <wallet/db.h>
#include <wallet/psbt_fixup.h>
Expand Down Expand Up @@ -81,6 +82,8 @@ static void migrate_forwards_add_rowid(struct lightningd *ld,

static void migrate_initialize_forwards_wait_indexes(struct lightningd *ld,
struct db *db);
static void migrate_initialize_alias_local(struct lightningd *ld,
struct db *db);

/* Do not reorder or remove elements from this array, it is used to
* migrate existing databases from a previous state, based on the
Expand Down Expand Up @@ -1017,6 +1020,7 @@ static struct migration dbmigrations[] = {
{SQL("ALTER TABLE channels ADD remote_htlc_maximum_msat BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
{NULL, migrate_initialize_alias_local},
};

/**
Expand Down Expand Up @@ -1963,3 +1967,33 @@ static void migrate_normalize_invstr(struct lightningd *ld, struct db *db)
}
tal_free(stmt);
}

/* We required local aliases to be set on established channels,
* but we forgot about already-existing ones in the db! */
static void migrate_initialize_alias_local(struct lightningd *ld,
struct db *db)
{
struct db_stmt *stmt;
u64 *ids = tal_arr(tmpctx, u64, 0);

stmt = db_prepare_v2(db, SQL("SELECT id FROM channels"
" WHERE scid IS NOT NULL"
" AND alias_local IS NULL;"));
db_query_prepared(stmt);
while (db_step(stmt))
tal_arr_expand(&ids, db_col_u64(stmt, "id"));
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_u64(stmt, ids[i]);
db_exec_prepared_v2(stmt);
tal_free(stmt);
}
}

0 comments on commit b5bd907

Please sign in to comment.