lightningd: only store channel funding spend txs into db.

Now we do replay, we don't need the others.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-11-13 13:24:28 +10:30
parent 5c81b0c3dc
commit 20a41fd798
6 changed files with 24 additions and 96 deletions

View File

@ -208,9 +208,8 @@ static enum watch_result onchain_tx_watched(struct lightningd *ld,
return KEEP_WATCHING;
}
/* Store the channeltx so we can replay later */
wallet_channeltxs_add(ld->wallet, channel,
WIRE_ONCHAIND_DEPTH, txid, 0, blockheight);
/* Store so we remember if we crash, and can replay later */
wallet_insert_funding_spend(ld->wallet, channel, txid, 0, blockheight);
onchain_tx_depth(channel, txid, depth);
return KEEP_WATCHING;
@ -245,14 +244,6 @@ static enum watch_result onchain_txo_watched(struct channel *channel,
size_t input_num,
const struct block *block)
{
struct bitcoin_txid txid;
bitcoin_txid(tx, &txid);
/* Store the channeltx so we can replay later */
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
WIRE_ONCHAIND_SPENT, &txid, input_num,
block->height);
onchain_txo_spent(channel, tx, input_num, block->height);
/* We don't need to keep watching: If this output is double-spent

View File

@ -2208,8 +2208,8 @@ static enum watch_result funding_spent(struct channel *channel,
}
}
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
WIRE_ONCHAIND_INIT, &txid, 0, block->height);
wallet_insert_funding_spend(channel->peer->ld->wallet, channel,
&txid, 0, block->height);
return onchaind_funding_spent(channel, tx, block->height);
}

View File

@ -1025,11 +1025,6 @@ const char *version(void)
/* Generated stub for wallet_channel_save */
void wallet_channel_save(struct wallet *w UNNEEDED, struct channel *chan UNNEEDED)
{ fprintf(stderr, "wallet_channel_save called!\n"); abort(); }
/* Generated stub for wallet_channeltxs_add */
void wallet_channeltxs_add(struct wallet *w UNNEEDED, struct channel *chan UNNEEDED,
const int type UNNEEDED, const struct bitcoin_txid *txid UNNEEDED,
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
{ fprintf(stderr, "wallet_channeltxs_add called!\n"); abort(); }
/* Generated stub for wallet_delete_peer_if_unused */
void wallet_delete_peer_if_unused(struct wallet *w UNNEEDED, u64 peer_dbid UNNEEDED)
{ fprintf(stderr, "wallet_delete_peer_if_unused called!\n"); abort(); }
@ -1053,6 +1048,12 @@ bool wallet_htlcs_load_out_for_channel(struct wallet *wallet UNNEEDED,
/* Generated stub for wallet_init_channels */
bool wallet_init_channels(struct wallet *w UNNEEDED)
{ fprintf(stderr, "wallet_init_channels called!\n"); abort(); }
/* Generated stub for wallet_insert_funding_spend */
void wallet_insert_funding_spend(struct wallet *w UNNEEDED,
const struct channel *chan UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED,
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
{ fprintf(stderr, "wallet_insert_funding_spend called!\n"); abort(); }
/* Generated stub for wallet_offer_find */
char *wallet_offer_find(const tal_t *ctx UNNEEDED,
struct wallet *w UNNEEDED,

View File

@ -1021,6 +1021,7 @@ static struct migration dbmigrations[] = {
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
{NULL, migrate_initialize_alias_local},
/* FIXME: Remove now-unused type column from channeltxs */
};
/**

View File

@ -4659,9 +4659,10 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
return txids;
}
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
const int type, const struct bitcoin_txid *txid,
const u32 input_num, const u32 blockheight)
void wallet_insert_funding_spend(struct wallet *w,
const struct channel *chan,
const struct bitcoin_txid *txid,
const u32 input_num, const u32 blockheight)
{
struct db_stmt *stmt;
stmt = db_prepare_v2(w->db, SQL("INSERT INTO channeltxs ("
@ -4672,72 +4673,15 @@ void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
", blockheight"
") VALUES (?, ?, ?, ?, ?);"));
db_bind_int(stmt, chan->dbid);
db_bind_int(stmt, type);
db_bind_sha256(stmt, &txid->shad.sha);
/* FIXME: This is WIRE_ONCHAIND_INIT, accidentally leaked into db! */
db_bind_int(stmt, 5001);
db_bind_txid(stmt, txid);
db_bind_int(stmt, input_num);
db_bind_int(stmt, blockheight);
db_exec_prepared_v2(take(stmt));
}
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w)
{
struct db_stmt *stmt;
size_t count = 0;
u32 *channel_ids = tal_arr(ctx, u32, 0);
stmt = db_prepare_v2(
w->db,
SQL("SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"));
db_bind_int(stmt, WIRE_ONCHAIND_INIT);
db_query_prepared(stmt);
while (db_step(stmt)) {
count++;
tal_resize(&channel_ids, count);
channel_ids[count-1] = db_col_u64(stmt, "DISTINCT(channel_id)");
}
tal_free(stmt);
return channel_ids;
}
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
u32 channel_id)
{
struct db_stmt *stmt;
size_t count = 0;
struct channeltx *res = tal_arr(ctx, struct channeltx, 0);
stmt = db_prepare_v2(
w->db, SQL("SELECT"
" c.type"
", c.blockheight"
", t.rawtx"
", c.input_num"
", c.blockheight - t.blockheight + 1 AS depth"
", t.id as txid "
"FROM channeltxs c "
"JOIN transactions t ON t.id = c.transaction_id "
"WHERE c.channel_id = ? "
"ORDER BY c.id ASC;"));
db_bind_int(stmt, channel_id);
db_query_prepared(stmt);
while (db_step(stmt)) {
count++;
tal_resize(&res, count);
res[count-1].channel_id = channel_id;
res[count-1].type = db_col_int(stmt, "c.type");
res[count-1].blockheight = db_col_int(stmt, "c.blockheight");
res[count-1].tx = db_col_tx(ctx, stmt, "t.rawtx");
res[count-1].input_num = db_col_int(stmt, "c.input_num");
res[count-1].depth = db_col_int(stmt, "depth");
db_col_txid(stmt, "txid", &res[count-1].txid);
}
tal_free(stmt);
return res;
}
struct bitcoin_tx *wallet_get_funding_spend(const tal_t *ctx,
struct wallet *w,
u64 channel_id,

View File

@ -1181,22 +1181,13 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
const u32 blockheight);
/**
* Store transactions of interest in the database to replay on restart
* Store funding txid spend to start replay on restart
* Note that tx should already be saved by wallet_transaction_add!
*/
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
const int type, const struct bitcoin_txid *txid,
const u32 input_num, const u32 blockheight);
/**
* List channels for which we had an onchaind running
*/
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w);
/**
* Get transactions that we'd like to replay for a channel.
*/
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
u32 channel_id);
void wallet_insert_funding_spend(struct wallet *w,
const struct channel *chan,
const struct bitcoin_txid *txid,
const u32 input_num, const u32 blockheight);
/**
* Get the transaction which spend funding for this channel, if any.