From 4bc07508820df15aec126f6b3f836a412f826b39 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 14 Aug 2017 22:06:59 +0200 Subject: [PATCH] wallet: Moving wallet_channel->peer_id into the peer struct This was supposed to be a temporary solution anyway, and I had a rather annoying mixup between peer_id and unique_id, the latter of which is actually a connection identifier. --- lightningd/peer_control.c | 1 - lightningd/peer_control.h | 3 +++ wallet/wallet.c | 12 ++++++------ wallet/wallet.h | 1 - wallet/wallet_tests.c | 6 +++--- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index ce5ab8fbb..f982b2bfd 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -541,7 +541,6 @@ static struct wallet_channel *peer_channel_new(struct wallet *w, wc->peer = peer; /* TODO(cdecker) See if we already stored this peer in the DB and load if yes */ - wc->peer_id = 0; wc->id = 0; if (!wallet_channel_save(w, wc)) { diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index fbd5c2a4e..443afcb33 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -20,6 +20,9 @@ struct crypto_state; struct peer { struct lightningd *ld; + /* Database ID of the peer */ + u64 dbid; + /* Unique ID of connection (works even if we have multiple to same id) */ u64 unique_id; diff --git a/wallet/wallet.c b/wallet/wallet.c index 2b839168f..da9f52e59 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -395,7 +395,7 @@ static bool wallet_peer_load(struct wallet *w, const u64 id, struct peer *peer) sqlite3_finalize(stmt); return false; } - peer->unique_id = sqlite3_column_int64(stmt, 0); + peer->dbid = sqlite3_column_int64(stmt, 0); ok &= sqlite3_column_pubkey(stmt, 1, &peer->id); sqlite3_finalize(stmt); return ok; @@ -420,8 +420,8 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, chan->peer = talz(chan, struct peer); } chan->peer->unique_id = sqlite3_column_int64(stmt, col++); - chan->peer_id = sqlite3_column_int64(stmt, col++); - wallet_peer_load(w, chan->peer_id, chan->peer); + chan->peer->dbid = sqlite3_column_int64(stmt, col++); + wallet_peer_load(w, chan->peer->dbid, chan->peer); if (sqlite3_column_short_channel_id(stmt, col++, &scid)) { chan->peer->scid = tal(chan->peer, struct short_channel_id); @@ -649,19 +649,19 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ struct peer *p = chan->peer; tal_t *tmpctx = tal_tmpctx(w); - if (chan->peer_id == 0) { + if (p->dbid == 0) { /* Need to store the peer first */ ok &= db_exec(__func__, w->db, "INSERT INTO peers (node_id) VALUES ('%s');", db_serialize_pubkey(tmpctx, &chan->peer->id)); - chan->peer_id = sqlite3_last_insert_rowid(w->db->sql); + p->dbid = sqlite3_last_insert_rowid(w->db->sql); } db_begin_transaction(w->db); /* Insert a stub, that we can update, unifies INSERT and UPDATE paths */ if (chan->id == 0) { - ok &= db_exec(__func__, w->db, "INSERT INTO channels (peer_id) VALUES (%"PRIu64");", chan->peer_id); + ok &= db_exec(__func__, w->db, "INSERT INTO channels (peer_id) VALUES (%"PRIu64");", p->dbid); chan->id = sqlite3_last_insert_rowid(w->db->sql); } diff --git a/wallet/wallet.h b/wallet/wallet.h index 2b0e110f3..838338863 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -52,7 +52,6 @@ struct wallet_shachain { /* TODO(cdecker) Separate peer from channel */ struct wallet_channel { u64 id; - u64 peer_id; struct peer *peer; }; diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index 387271327..40bda648a 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -132,7 +132,7 @@ static bool channelseq(struct wallet_channel *c1, struct wallet_channel *c2) struct channel_info *ci1 = p1->channel_info, *ci2 = p2->channel_info; struct changed_htlc *lc1 = p1->last_sent_commit, *lc2 = p2->last_sent_commit; CHECK(c1->id == c2->id); - CHECK(c1->peer_id == c2->peer_id); + CHECK(c1->peer->dbid == c2->peer->dbid); CHECK(p1->their_shachain.id == p2->their_shachain.id); CHECK_MSG(pubkey_eq(&p1->id, &p2->id), "NodeIDs do not match"); CHECK((p1->scid == NULL && p2->scid == NULL) || short_channel_id_eq(p1->scid, p2->scid)); @@ -221,7 +221,7 @@ static bool test_channel_crud(const tal_t *ctx) /* We just inserted them into an empty DB so this must be 1 */ CHECK(c1.id == 1); - CHECK(c1.peer_id == 1); + CHECK(c1.peer->dbid == 1); CHECK(c1.peer->their_shachain.id == 1); /* Variant 2: update with scid set */ @@ -232,7 +232,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Updates should not result in new ids */ CHECK(c1.id == 1); - CHECK(c1.peer_id == 1); + CHECK(c1.peer->dbid == 1); CHECK(c1.peer->their_shachain.id == 1); /* Variant 3: update with our_satoshi set */