mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-22 08:34:20 +01:00
db: Make peers unique by node_id
We should only ever have a single entry for each node_id, enforcing this on the DB.
This commit is contained in:
committed by
Rusty Russell
parent
4bc0750882
commit
1a94e7282b
@@ -540,7 +540,7 @@ static struct wallet_channel *peer_channel_new(struct wallet *w,
|
|||||||
struct wallet_channel *wc = tal(peer, struct wallet_channel);
|
struct wallet_channel *wc = tal(peer, struct wallet_channel);
|
||||||
wc->peer = peer;
|
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;
|
wc->id = 0;
|
||||||
|
|
||||||
if (!wallet_channel_save(w, wc)) {
|
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);
|
peer->htlcs = tal_arr(peer, struct htlc_stub, 0);
|
||||||
wallet_shachain_init(ld->wallet, &peer->their_shachain);
|
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 gets populated as soon as we start opening a channel */
|
||||||
peer->channel = NULL;
|
peer->channel = NULL;
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ char *dbmigrations[] = {
|
|||||||
");",
|
");",
|
||||||
"CREATE TABLE peers ("
|
"CREATE TABLE peers ("
|
||||||
" id INTEGER,"
|
" id INTEGER,"
|
||||||
" node_id BLOB," /* pubkey */
|
" node_id BLOB UNIQUE," /* pubkey */
|
||||||
" address TEXT,"
|
" address TEXT,"
|
||||||
" PRIMARY KEY (id)"
|
" PRIMARY KEY (id)"
|
||||||
");",
|
");",
|
||||||
|
|||||||
@@ -401,6 +401,28 @@ static bool wallet_peer_load(struct wallet *w, const u64 id, struct peer *peer)
|
|||||||
return ok;
|
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
|
* wallet_stmt2channel - Helper to populate a wallet_channel from a sqlite3_stmt
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -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,
|
bool wallet_channel_config_load(struct wallet *w, const u64 id,
|
||||||
struct channel_config *cc);
|
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 */
|
#endif /* WALLET_WALLET_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user