lightningd: save short_channel_id in inflight struct as soon as it gets mined.
What changed, and why it matters
This is a small internal cleanup change in Core Lightning. It stores a channel identifier (short_channel_id) in memory as soon as a splicing transaction is mined, instead of looking it up from the database every time it is needed. It also removes an unused database helper function. There is no obvious security vulnerability here; it is primarily a code-quality and efficiency improvement.
No security action required. Treat as normal code review/merge for maintainability. If auditing, verify that `inflight->scid` is always set before use in `splice_depth_cb()` and that the reorg/free path cannot lead to a use-after-free or stale pointer.
Security signals we found
No direct security signal: change is a refactoring/caching improvement.
Removal of a database lookup path reduces attack surface slightly, but no vulnerability is identified in the removed code.
No input validation changes, no memory safety defects visible in diff, no privilege changes.
Evidence from the diff
The commit adds a scid pointer to struct channel_inflight, initializes it to NULL, sets it in splice_found() when the funding transaction is located, frees it on reorg, and uses the cached value in splice_depth_cb() instead of calling wallet_transaction_locate() and re-deriving the short_channel_id. Because this removes the last caller, wallet_transaction_locate() and its declaration are deleted. The change reduces database lookups and removes a FIXME comment about re-derivation.
Changed components
lightningd/channel.clightningd/channel.hlightningd/channel_control.clightningd/channel_control.hwallet/wallet.cwallet/wallet.hInspect captured patch +12 / −46
diff --git a/lightningd/channel.c b/lightningd/channel.c
index db05cf57..b15fdb2c 100644
--- a/lightningd/channel.c
+++ b/lightningd/channel.c
@@ -204,6 +204,7 @@ new_inflight(struct channel *channel,
inflight->funding_psbt = tal_steal(inflight, psbt);
inflight->last_tx = NULL;
inflight->tx_broadcast = false;
+ inflight->scid = NULL;
/* Channel lease infos */
inflight->lease_blockheight_start = lease_blockheight_start;
diff --git a/lightningd/channel.h b/lightningd/channel.h
index 9661e4b7..b96666e0 100644
--- a/lightningd/channel.h
+++ b/lightningd/channel.h
@@ -53,6 +53,9 @@ struct channel_inflight {
/* Channel context */
struct channel *channel;
+ /* SCID once we're mined, otherwise NULL */
+ struct short_channel_id *scid;
+
/* Funding info */
const struct funding_info *funding;
struct wally_psbt *funding_psbt;
diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c
index 0da795ee..9735e72a 100644
--- a/lightningd/channel_control.c
+++ b/lightningd/channel_control.c
@@ -700,18 +700,11 @@ static enum watch_result splice_depth_cb(struct lightningd *ld,
}
if (inflight->channel->owner) {
- /* FIXME: Save this in inflight so we don't neeed to re-derive. */
- struct short_channel_id scid;
- struct txlocator *loc = wallet_transaction_locate(tmpctx, ld->wallet, &inflight->funding->outpoint.txid);
- if (!mk_short_channel_id(&scid,
- loc->blkheight, loc->index,
- inflight->funding->outpoint.n))
- abort();
log_debug(inflight->channel->log, "splice_depth_cb: sending funding depth scid: %s",
- fmt_short_channel_id(tmpctx, scid));
+ fmt_short_channel_id(tmpctx, *inflight->scid));
subd_send_msg(inflight->channel->owner,
take(towire_channeld_funding_depth(
- NULL, &scid,
+ NULL, inflight->scid,
depth, true,
&inflight->funding->outpoint.txid)));
}
@@ -726,6 +719,7 @@ static enum watch_result splice_reorged_cb(struct lightningd *ld, struct channel
{
log_unusual(inflight->channel->log, "Splice inflight txid %s reorged out",
fmt_bitcoin_txid(tmpctx, &inflight->funding->outpoint.txid));
+ inflight->scid = tal_free(inflight->scid);
return DELETE_WATCH;
}
@@ -736,11 +730,13 @@ static void splice_found(struct lightningd *ld,
const struct txlocator *loc,
struct channel_inflight *inflight)
{
- struct short_channel_id scid;
+ assert(!inflight->scid);
+ inflight->scid = tal(inflight, struct short_channel_id);
- if (!mk_short_channel_id(&scid,
+ if (!mk_short_channel_id(inflight->scid,
loc->blkheight, loc->index,
inflight->funding->outpoint.n)) {
+ inflight->scid = tal_free(inflight->scid);
channel_fail_permanent(inflight->channel,
REASON_LOCAL,
"Invalid funding scid %u:%u:%u",
diff --git a/lightningd/channel_control.h b/lightningd/channel_control.h
index 8eb1e118..b5d78381 100644
--- a/lightningd/channel_control.h
+++ b/lightningd/channel_control.h
@@ -9,6 +9,7 @@ struct crypto_state;
struct lightningd;
struct peer_fd;
struct peer;
+struct txlocator;
bool peer_start_channeld(struct channel *channel,
struct peer_fd *peer_fd,
diff --git a/wallet/wallet.c b/wallet/wallet.c
index 47bf6e0f..2b080247 100644
--- a/wallet/wallet.c
+++ b/wallet/wallet.c
@@ -5263,34 +5263,6 @@ u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid)
return blockheight;
}
-struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w,
- const struct bitcoin_txid *txid)
-{
- struct txlocator *loc;
- struct db_stmt *stmt;
-
- stmt = db_prepare_v2(
- w->db, SQL("SELECT blockheight, txindex FROM transactions WHERE id=?"));
- db_bind_txid(stmt, txid);
- db_query_prepared(stmt);
-
- if (!db_step(stmt)) {
- tal_free(stmt);
- return NULL;
- }
-
- if (db_col_is_null(stmt, "blockheight")) {
- db_col_ignore(stmt, "txindex");
- loc = NULL;
- } else {
- loc = tal(ctx, struct txlocator);
- loc->blkheight = db_col_int(stmt, "blockheight");
- loc->index = db_col_int(stmt, "txindex");
- }
- tal_free(stmt);
- return loc;
-}
-
struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
struct wallet *w,
const u32 blockheight)
diff --git a/wallet/wallet.h b/wallet/wallet.h
index d7a13132..589b4888 100644
--- a/wallet/wallet.h
+++ b/wallet/wallet.h
@@ -1280,13 +1280,6 @@ struct bitcoin_tx *wallet_transaction_get(const tal_t *ctx, struct wallet *w,
*/
u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid);
-/**
- * Locate a transaction in the blockchain, returns NULL if the transaction is
- * not tracked or is not yet confirmed.
- */
-struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w,
- const struct bitcoin_txid *txid);
-
/**
* Get transaction IDs for transactions that we are tracking.
*/
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.