Skip to content

Commit c5fba12

Browse files
committed
lightningd: only store channel funding spend txs into db.
Now we do replay, we don't need the others. Signed-off-by: Rusty Russell <[email protected]>
1 parent af5f9ae commit c5fba12

File tree

6 files changed

+24
-96
lines changed

6 files changed

+24
-96
lines changed

lightningd/onchain_control.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,8 @@ static enum watch_result onchain_tx_watched(struct lightningd *ld,
208208
return KEEP_WATCHING;
209209
}
210210

211-
/* Store the channeltx so we can replay later */
212-
wallet_channeltxs_add(ld->wallet, channel,
213-
WIRE_ONCHAIND_DEPTH, txid, 0, blockheight);
211+
/* Store so we remember if we crash, and can replay later */
212+
wallet_insert_funding_spend(ld->wallet, channel, txid, 0, blockheight);
214213

215214
onchain_tx_depth(channel, txid, depth);
216215
return KEEP_WATCHING;
@@ -245,14 +244,6 @@ static enum watch_result onchain_txo_watched(struct channel *channel,
245244
size_t input_num,
246245
const struct block *block)
247246
{
248-
struct bitcoin_txid txid;
249-
bitcoin_txid(tx, &txid);
250-
251-
/* Store the channeltx so we can replay later */
252-
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
253-
WIRE_ONCHAIND_SPENT, &txid, input_num,
254-
block->height);
255-
256247
onchain_txo_spent(channel, tx, input_num, block->height);
257248

258249
/* We don't need to keep watching: If this output is double-spent

lightningd/peer_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,8 @@ static enum watch_result funding_spent(struct channel *channel,
21672167
}
21682168
}
21692169

2170-
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
2171-
WIRE_ONCHAIND_INIT, &txid, 0, block->height);
2170+
wallet_insert_funding_spend(channel->peer->ld->wallet, channel,
2171+
&txid, 0, block->height);
21722172

21732173
return onchaind_funding_spent(channel, tx, block->height);
21742174
}

lightningd/test/run-invoice-select-inchan.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,6 @@ void wallet_channel_save(struct wallet *w UNNEEDED, struct channel *chan UNNEEDE
10001000
/* Generated stub for wallet_channel_stats_load */
10011001
void wallet_channel_stats_load(struct wallet *w UNNEEDED, u64 cdbid UNNEEDED, struct channel_stats *stats UNNEEDED)
10021002
{ fprintf(stderr, "wallet_channel_stats_load called!\n"); abort(); }
1003-
/* Generated stub for wallet_channeltxs_add */
1004-
void wallet_channeltxs_add(struct wallet *w UNNEEDED, struct channel *chan UNNEEDED,
1005-
const int type UNNEEDED, const struct bitcoin_txid *txid UNNEEDED,
1006-
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
1007-
{ fprintf(stderr, "wallet_channeltxs_add called!\n"); abort(); }
10081003
/* Generated stub for wallet_delete_peer_if_unused */
10091004
void wallet_delete_peer_if_unused(struct wallet *w UNNEEDED, u64 peer_dbid UNNEEDED)
10101005
{ fprintf(stderr, "wallet_delete_peer_if_unused called!\n"); abort(); }
@@ -1028,6 +1023,12 @@ bool wallet_htlcs_load_out_for_channel(struct wallet *wallet UNNEEDED,
10281023
/* Generated stub for wallet_init_channels */
10291024
bool wallet_init_channels(struct wallet *w UNNEEDED)
10301025
{ fprintf(stderr, "wallet_init_channels called!\n"); abort(); }
1026+
/* Generated stub for wallet_insert_funding_spend */
1027+
void wallet_insert_funding_spend(struct wallet *w UNNEEDED,
1028+
const struct channel *chan UNNEEDED,
1029+
const struct bitcoin_txid *txid UNNEEDED,
1030+
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
1031+
{ fprintf(stderr, "wallet_insert_funding_spend called!\n"); abort(); }
10311032
/* Generated stub for wallet_offer_find */
10321033
char *wallet_offer_find(const tal_t *ctx UNNEEDED,
10331034
struct wallet *w UNNEEDED,

wallet/db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ static struct migration dbmigrations[] = {
10211021
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
10221022
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
10231023
{NULL, migrate_initialize_alias_local},
1024+
/* FIXME: Remove now-unused type column from channeltxs */
10241025
};
10251026

10261027
/**

wallet/wallet.c

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,9 +4722,10 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
47224722
return txids;
47234723
}
47244724

4725-
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
4726-
const int type, const struct bitcoin_txid *txid,
4727-
const u32 input_num, const u32 blockheight)
4725+
void wallet_insert_funding_spend(struct wallet *w,
4726+
const struct channel *chan,
4727+
const struct bitcoin_txid *txid,
4728+
const u32 input_num, const u32 blockheight)
47284729
{
47294730
struct db_stmt *stmt;
47304731
stmt = db_prepare_v2(w->db, SQL("INSERT INTO channeltxs ("
@@ -4735,72 +4736,15 @@ void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
47354736
", blockheight"
47364737
") VALUES (?, ?, ?, ?, ?);"));
47374738
db_bind_int(stmt, chan->dbid);
4738-
db_bind_int(stmt, type);
4739-
db_bind_sha256(stmt, &txid->shad.sha);
4739+
/* FIXME: This is WIRE_ONCHAIND_INIT, accidentally leaked into db! */
4740+
db_bind_int(stmt, 5001);
4741+
db_bind_txid(stmt, txid);
47404742
db_bind_int(stmt, input_num);
47414743
db_bind_int(stmt, blockheight);
47424744

47434745
db_exec_prepared_v2(take(stmt));
47444746
}
47454747

4746-
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w)
4747-
{
4748-
struct db_stmt *stmt;
4749-
size_t count = 0;
4750-
u32 *channel_ids = tal_arr(ctx, u32, 0);
4751-
stmt = db_prepare_v2(
4752-
w->db,
4753-
SQL("SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"));
4754-
db_bind_int(stmt, WIRE_ONCHAIND_INIT);
4755-
db_query_prepared(stmt);
4756-
4757-
while (db_step(stmt)) {
4758-
count++;
4759-
tal_resize(&channel_ids, count);
4760-
channel_ids[count-1] = db_col_u64(stmt, "DISTINCT(channel_id)");
4761-
}
4762-
tal_free(stmt);
4763-
4764-
return channel_ids;
4765-
}
4766-
4767-
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
4768-
u32 channel_id)
4769-
{
4770-
struct db_stmt *stmt;
4771-
size_t count = 0;
4772-
struct channeltx *res = tal_arr(ctx, struct channeltx, 0);
4773-
stmt = db_prepare_v2(
4774-
w->db, SQL("SELECT"
4775-
" c.type"
4776-
", c.blockheight"
4777-
", t.rawtx"
4778-
", c.input_num"
4779-
", c.blockheight - t.blockheight + 1 AS depth"
4780-
", t.id as txid "
4781-
"FROM channeltxs c "
4782-
"JOIN transactions t ON t.id = c.transaction_id "
4783-
"WHERE c.channel_id = ? "
4784-
"ORDER BY c.id ASC;"));
4785-
db_bind_int(stmt, channel_id);
4786-
db_query_prepared(stmt);
4787-
4788-
while (db_step(stmt)) {
4789-
count++;
4790-
tal_resize(&res, count);
4791-
4792-
res[count-1].channel_id = channel_id;
4793-
res[count-1].type = db_col_int(stmt, "c.type");
4794-
res[count-1].blockheight = db_col_int(stmt, "c.blockheight");
4795-
res[count-1].tx = db_col_tx(ctx, stmt, "t.rawtx");
4796-
res[count-1].input_num = db_col_int(stmt, "c.input_num");
4797-
res[count-1].depth = db_col_int(stmt, "depth");
4798-
db_col_txid(stmt, "txid", &res[count-1].txid);
4799-
}
4800-
tal_free(stmt);
4801-
return res;
4802-
}
4803-
48044748
struct bitcoin_tx *wallet_get_funding_spend(const tal_t *ctx,
48054749
struct wallet *w,
48064750
u64 channel_id,

wallet/wallet.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,22 +1221,13 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
12211221
const u32 blockheight);
12221222

12231223
/**
1224-
* Store transactions of interest in the database to replay on restart
1224+
* Store funding txid spend to start replay on restart
1225+
* Note that tx should already be saved by wallet_transaction_add!
12251226
*/
1226-
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
1227-
const int type, const struct bitcoin_txid *txid,
1228-
const u32 input_num, const u32 blockheight);
1229-
1230-
/**
1231-
* List channels for which we had an onchaind running
1232-
*/
1233-
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w);
1234-
1235-
/**
1236-
* Get transactions that we'd like to replay for a channel.
1237-
*/
1238-
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
1239-
u32 channel_id);
1227+
void wallet_insert_funding_spend(struct wallet *w,
1228+
const struct channel *chan,
1229+
const struct bitcoin_txid *txid,
1230+
const u32 input_num, const u32 blockheight);
12401231

12411232
/**
12421233
* Get the transaction which spend funding for this channel, if any.

0 commit comments

Comments
 (0)