Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: Retrieve peer ID if it exists or create the peer if not #3801

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,22 +1535,47 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
tal_free(stmt);
}

void wallet_channel_insert(struct wallet *w, struct channel *chan)
static void wallet_peer_save(struct wallet *w, struct peer *peer)
{
struct db_stmt *stmt;
const char *addr =
type_to_string(tmpctx, struct wireaddr_internal, &peer->addr);
struct db_stmt *stmt =
db_prepare_v2(w->db, SQL("SELECT id FROM peers WHERE node_id = ?"));

db_bind_node_id(stmt, 0, &peer->id);
db_query_prepared(stmt);

if (chan->peer->dbid == 0) {
/* Need to create the peer first */
if (db_step(stmt)) {
/* So we already knew this peer, just return its dbid */
peer->dbid = db_column_u64(stmt, 0);
tal_free(stmt);

/* Since we're at it update the wireaddr */
stmt = db_prepare_v2(
w->db, SQL("UPDATE peers SET address = ? WHERE id = ?"));
db_bind_text(stmt, 0, addr);
db_bind_u64(stmt, 1, peer->dbid);
db_exec_prepared_v2(take(stmt));

} else {
/* Unknown peer, create it from scratch */
tal_free(stmt);
stmt = db_prepare_v2(w->db,
SQL("INSERT INTO peers (node_id, address) VALUES (?, ?);")
);
db_bind_node_id(stmt, 0, &chan->peer->id);
db_bind_text(stmt, 1,
type_to_string(tmpctx, struct wireaddr_internal,
&chan->peer->addr));
db_bind_node_id(stmt, 0, &peer->id);
db_bind_text(stmt, 1,addr);
db_exec_prepared_v2(stmt);
chan->peer->dbid = db_last_insert_id_v2(take(stmt));
peer->dbid = db_last_insert_id_v2(take(stmt));
}
}

void wallet_channel_insert(struct wallet *w, struct channel *chan)
{
struct db_stmt *stmt;

if (chan->peer->dbid == 0)
wallet_peer_save(w, chan->peer);

/* Insert a stub, that we update, unifies INSERT and UPDATE paths */
stmt = db_prepare_v2(
Expand Down