Browse Source

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 <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
4e6f9787b6
  1. 3
      wallet/db.c
  2. 47
      wallet/wallet.c

3
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,"

47
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,

Loading…
Cancel
Save