Skip to content

Commit

Permalink
wallet: recoverchannel RPC only stubs a channel if the id is vacant i…
Browse files Browse the repository at this point in the history
…n the DB as well. Closed channels are not loaded inside the peer struct, hence find_channel_by_id fails when the id is occupied by a closed channel. So we need to create a function to directly check if the ID is occupied or not.
  • Loading branch information
adi2011 authored and endothermicdev committed May 16, 2024
1 parent 934b85a commit 0d5ba47
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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

0 comments on commit 0d5ba47

Please sign in to comment.