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

Function to check if an ID is occupied by any channel inside the DB. #7216

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 4 additions & 7 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1480,13 +1480,10 @@ static struct channel *stub_chan(struct command *cmd,
144);

/* If the channel is already stored, return NULL. */
peer = peer_by_id(cmd->ld, &nodeid);
if (peer) {
if (find_channel_by_id(peer, &cid)) {
log_debug(cmd->ld->log, "channel %s already exists!",
fmt_channel_id(tmpctx, &cid));
return NULL;
}
if (channel_exists_by_id(cmd->ld->wallet, id)) {
log_debug(cmd->ld->log, "channel %s already exists!",
fmt_channel_id(tmpctx, &cid));
return NULL;
} else {
struct wireaddr_internal wint;

Expand Down
20 changes: 20 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,26 @@ static void wallet_peer_save(struct wallet *w, struct peer *peer)
}
}

bool channel_exists_by_id(struct wallet *w, u64 dbid) {
struct db_stmt *stmt;
stmt = db_prepare_v2(w->db, SQL("SELECT *"
" FROM channels"
" WHERE id = ?"));

db_bind_u64(stmt, dbid);
db_query_prepared(stmt);

/* If we found a result it means channel exists at that place. */
if (db_step(stmt)) {
db_col_ignore(stmt, "*");
tal_free(stmt);
return true;
}

tal_free(stmt);
return false;
}

void wallet_channel_insert(struct wallet *w, struct channel *chan)
{
struct db_stmt *stmt;
Expand Down
10 changes: 10 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,16 @@ void wallet_htlcsigs_confirm_inflight(struct wallet *w, struct channel *chan,
*/
void wallet_channel_save(struct wallet *w, struct channel *chan);

/**
* wallet_channels_by_dbid -- Check if an ID is preoccupied inside the `channels` table
*
* @wallet: the wallet
* @dbid: the ID at which we want to check the value.
*
* Returns `true` if it is occupied, false otherwise.
*/
bool channel_exists_by_id(struct wallet *w, u64 dbid);

/**
* wallet_channel_insert -- Insert the initial channel into the database
*
Expand Down
Loading