diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index f982b2bfd..68e146dd6 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -540,7 +540,7 @@ static struct wallet_channel *peer_channel_new(struct wallet *w, struct wallet_channel *wc = tal(peer, struct wallet_channel); wc->peer = peer; - /* TODO(cdecker) See if we already stored this peer in the DB and load if yes */ + wallet_peer_by_nodeid(w, &peer->id, peer); wc->id = 0; if (!wallet_channel_save(w, wc)) { @@ -590,6 +590,11 @@ void add_peer(struct lightningd *ld, u64 unique_id, peer->htlcs = tal_arr(peer, struct htlc_stub, 0); wallet_shachain_init(ld->wallet, &peer->their_shachain); + /* If we have the peer in the DB, this'll populate the fields, + * failure just indicates that the peer wasn't found in the + * DB */ + wallet_peer_by_nodeid(ld->wallet, id, peer); + /* peer->channel gets populated as soon as we start opening a channel */ peer->channel = NULL; diff --git a/wallet/db.c b/wallet/db.c index fc5a59fb6..0f8948f8a 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -79,7 +79,7 @@ char *dbmigrations[] = { ");", "CREATE TABLE peers (" " id INTEGER," - " node_id BLOB," /* pubkey */ + " node_id BLOB UNIQUE," /* pubkey */ " address TEXT," " PRIMARY KEY (id)" ");", diff --git a/wallet/wallet.c b/wallet/wallet.c index da9f52e59..cd3ae7169 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -401,6 +401,28 @@ static bool wallet_peer_load(struct wallet *w, const u64 id, struct peer *peer) return ok; } +bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, + struct peer *peer) +{ + bool ok; + tal_t *tmpctx = tal_tmpctx(w); + sqlite3_stmt *stmt = db_query( + __func__, w->db, "SELECT id, node_id FROM peers WHERE node_id='%s';", + pubkey_to_hexstr(tmpctx, nodeid)); + + ok = stmt != NULL && sqlite3_step(stmt) == SQLITE_ROW; + if (ok) { + peer->dbid = sqlite3_column_int64(stmt, 0); + ok &= sqlite3_column_pubkey(stmt, 1, &peer->id); + } else { + /* Make sure we mark this as a new peer */ + peer->dbid = 0; + } + sqlite3_finalize(stmt); + tal_free(tmpctx); + return ok; +} + /** * wallet_stmt2channel - Helper to populate a wallet_channel from a sqlite3_stmt * diff --git a/wallet/wallet.h b/wallet/wallet.h index 838338863..22510f40c 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -184,4 +184,17 @@ bool wallet_channel_config_save(struct wallet *w, struct channel_config *cc); */ bool wallet_channel_config_load(struct wallet *w, const u64 id, struct channel_config *cc); + +/** + * wallet_peer_by_nodeid -- Given a node_id/pubkey, load the peer from DB + * + * @w: the wallet to load from + * @nodeid: the node_id to search for + * @peer(out): the destination where to store the peer + * + * Returns true on success, or false if we were unable to find a peer + * with the given node_id. + */ +bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, + struct peer *peer); #endif /* WALLET_WALLET_H */