From 4e6f9787b679e3724f3b2701a1d31ec3d23743bb Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 17 Aug 2017 13:52:47 +0200 Subject: [PATCH] wallet: unique_id is not the same as dbid They happen to advance at the same pace but mixing them may have unforeseen consequences, and I have done so a few times already so this explicitly separates them. Signed-off-by: Christian Decker --- wallet/db.c | 3 ++- wallet/wallet.c | 47 ++++++++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index 0f8948f8a..8213a6e49 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -38,7 +38,8 @@ char *dbmigrations[] = { hash BLOB, \ PRIMARY KEY (shachain_id, pos));", "CREATE TABLE channels (" - " id INTEGER," /* unique_id */ + " id INTEGER," /* chan->id */ + " unique_id INTEGER," " peer_id INTEGER REFERENCES peers(id) ON DELETE CASCADE," " short_channel_id BLOB," " channel_config_local INTEGER," diff --git a/wallet/wallet.c b/wallet/wallet.c index cd3ae7169..96cdef60e 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -441,6 +441,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, if (!chan->peer) { chan->peer = talz(chan, struct peer); } + chan->id = sqlite3_column_int64(stmt, col++); chan->peer->unique_id = sqlite3_column_int64(stmt, col++); chan->peer->dbid = sqlite3_column_int64(stmt, col++); wallet_peer_load(w, chan->peer->dbid, chan->peer); @@ -452,7 +453,6 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, chan->peer->scid = NULL; } - /* TODO(cdecker) Load channel configs into chan */ chan->peer->our_config.id = sqlite3_column_int64(stmt, col++); wallet_channel_config_load(w, chan->peer->our_config.id, &chan->peer->our_config); remote_config_id = sqlite3_column_int64(stmt, col++); @@ -548,41 +548,44 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, col += 2; } - assert(col == 33); + assert(col == 34); return ok; } +/* List of fields to retrieve from the channels DB table, in the order + * that wallet_stmt2channel understands and will parse correctly */ +const char *channel_fields = + "id, unique_id, peer_id, short_channel_id, channel_config_local, " + "channel_config_remote, state, funder, channel_flags, " + "minimum_depth, " + "next_index_local, next_index_remote, num_revocations_received, " + "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " + "funding_locked_remote, push_msatoshi, msatoshi_local, " + "fundingkey_remote, revocation_basepoint_remote, " + "payment_basepoint_remote, " + "delayed_payment_basepoint_remote, per_commit_remote, " + "old_per_commit_remote, feerate_per_kw, shachain_remote_id, " + "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " + "last_sent_commit_state, last_sent_commit_id, " + "last_tx, last_sig"; + bool wallet_channel_load(struct wallet *w, const u64 id, struct wallet_channel *chan) { bool ok; /* The explicit query that matches the columns and their order in * wallet_stmt2channel. */ - const char *channel_query = - "SELECT id, peer_id, short_channel_id, channel_config_local, " - "channel_config_remote, state, funder, channel_flags, " - "minimum_depth, " - "next_index_local, next_index_remote, num_revocations_received, " - "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " - "funding_locked_remote, push_msatoshi, msatoshi_local, " - "fundingkey_remote, revocation_basepoint_remote, " - "payment_basepoint_remote, " - "delayed_payment_basepoint_remote, per_commit_remote, " - "old_per_commit_remote, feerate_per_kw, shachain_remote_id, " - "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " - "last_sent_commit_state, last_sent_commit_id, " - "last_tx, last_sig " - "FROM channels WHERE " - "id=%" PRIu64 ";"; - - sqlite3_stmt *stmt = db_query(__func__, w->db, channel_query, id); + sqlite3_stmt *stmt = db_query( + __func__, w->db, "SELECT %s FROM channels WHERE id=%" PRIu64 ";", + channel_fields, id); + if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); return false; } + ok = wallet_stmt2channel(w, stmt, chan); - chan->id = id; sqlite3_finalize(stmt); return ok; @@ -696,6 +699,7 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ /* Now do the real update */ ok &= db_exec(__func__, w->db, "UPDATE channels SET" + " unique_id=%"PRIu64"," " shachain_remote_id=%"PRIu64"," " short_channel_id=%s," " state=%d," @@ -717,6 +721,7 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ " channel_config_local=%"PRIu64"," " last_tx=%s, last_sig=%s" " WHERE id=%"PRIu64, + p->unique_id, p->their_shachain.id, p->scid?tal_fmt(tmpctx,"'%s'", short_channel_id_to_str(tmpctx, p->scid)):"null", p->state,