Skip to content

Commit 64f275d

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 caa18e6 commit 64f275d

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
@@ -2208,8 +2208,8 @@ static enum watch_result funding_spent(struct channel *channel,
22082208
}
22092209
}
22102210

2211-
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
2212-
WIRE_ONCHAIND_INIT, &txid, 0, block->height);
2211+
wallet_insert_funding_spend(channel->peer->ld->wallet, channel,
2212+
&txid, 0, block->height);
22132213

22142214
return onchaind_funding_spent(channel, tx, block->height);
22152215
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,6 @@ const char *version(void)
10251025
/* Generated stub for wallet_channel_save */
10261026
void wallet_channel_save(struct wallet *w UNNEEDED, struct channel *chan UNNEEDED)
10271027
{ fprintf(stderr, "wallet_channel_save called!\n"); abort(); }
1028-
/* Generated stub for wallet_channeltxs_add */
1029-
void wallet_channeltxs_add(struct wallet *w UNNEEDED, struct channel *chan UNNEEDED,
1030-
const int type UNNEEDED, const struct bitcoin_txid *txid UNNEEDED,
1031-
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
1032-
{ fprintf(stderr, "wallet_channeltxs_add called!\n"); abort(); }
10331028
/* Generated stub for wallet_delete_peer_if_unused */
10341029
void wallet_delete_peer_if_unused(struct wallet *w UNNEEDED, u64 peer_dbid UNNEEDED)
10351030
{ 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,
10531048
/* Generated stub for wallet_init_channels */
10541049
bool wallet_init_channels(struct wallet *w UNNEEDED)
10551050
{ fprintf(stderr, "wallet_init_channels called!\n"); abort(); }
1051+
/* Generated stub for wallet_insert_funding_spend */
1052+
void wallet_insert_funding_spend(struct wallet *w UNNEEDED,
1053+
const struct channel *chan UNNEEDED,
1054+
const struct bitcoin_txid *txid UNNEEDED,
1055+
const u32 input_num UNNEEDED, const u32 blockheight UNNEEDED)
1056+
{ fprintf(stderr, "wallet_insert_funding_spend called!\n"); abort(); }
10561057
/* Generated stub for wallet_offer_find */
10571058
char *wallet_offer_find(const tal_t *ctx UNNEEDED,
10581059
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
@@ -4659,9 +4659,10 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
46594659
return txids;
46604660
}
46614661

4662-
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
4663-
const int type, const struct bitcoin_txid *txid,
4664-
const u32 input_num, const u32 blockheight)
4662+
void wallet_insert_funding_spend(struct wallet *w,
4663+
const struct channel *chan,
4664+
const struct bitcoin_txid *txid,
4665+
const u32 input_num, const u32 blockheight)
46654666
{
46664667
struct db_stmt *stmt;
46674668
stmt = db_prepare_v2(w->db, SQL("INSERT INTO channeltxs ("
@@ -4672,72 +4673,15 @@ void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
46724673
", blockheight"
46734674
") VALUES (?, ?, ?, ?, ?);"));
46744675
db_bind_int(stmt, chan->dbid);
4675-
db_bind_int(stmt, type);
4676-
db_bind_sha256(stmt, &txid->shad.sha);
4676+
/* FIXME: This is WIRE_ONCHAIND_INIT, accidentally leaked into db! */
4677+
db_bind_int(stmt, 5001);
4678+
db_bind_txid(stmt, txid);
46774679
db_bind_int(stmt, input_num);
46784680
db_bind_int(stmt, blockheight);
46794681

46804682
db_exec_prepared_v2(take(stmt));
46814683
}
46824684

4683-
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w)
4684-
{
4685-
struct db_stmt *stmt;
4686-
size_t count = 0;
4687-
u32 *channel_ids = tal_arr(ctx, u32, 0);
4688-
stmt = db_prepare_v2(
4689-
w->db,
4690-
SQL("SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"));
4691-
db_bind_int(stmt, WIRE_ONCHAIND_INIT);
4692-
db_query_prepared(stmt);
4693-
4694-
while (db_step(stmt)) {
4695-
count++;
4696-
tal_resize(&channel_ids, count);
4697-
channel_ids[count-1] = db_col_u64(stmt, "DISTINCT(channel_id)");
4698-
}
4699-
tal_free(stmt);
4700-
4701-
return channel_ids;
4702-
}
4703-
4704-
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
4705-
u32 channel_id)
4706-
{
4707-
struct db_stmt *stmt;
4708-
size_t count = 0;
4709-
struct channeltx *res = tal_arr(ctx, struct channeltx, 0);
4710-
stmt = db_prepare_v2(
4711-
w->db, SQL("SELECT"
4712-
" c.type"
4713-
", c.blockheight"
4714-
", t.rawtx"
4715-
", c.input_num"
4716-
", c.blockheight - t.blockheight + 1 AS depth"
4717-
", t.id as txid "
4718-
"FROM channeltxs c "
4719-
"JOIN transactions t ON t.id = c.transaction_id "
4720-
"WHERE c.channel_id = ? "
4721-
"ORDER BY c.id ASC;"));
4722-
db_bind_int(stmt, channel_id);
4723-
db_query_prepared(stmt);
4724-
4725-
while (db_step(stmt)) {
4726-
count++;
4727-
tal_resize(&res, count);
4728-
4729-
res[count-1].channel_id = channel_id;
4730-
res[count-1].type = db_col_int(stmt, "c.type");
4731-
res[count-1].blockheight = db_col_int(stmt, "c.blockheight");
4732-
res[count-1].tx = db_col_tx(ctx, stmt, "t.rawtx");
4733-
res[count-1].input_num = db_col_int(stmt, "c.input_num");
4734-
res[count-1].depth = db_col_int(stmt, "depth");
4735-
db_col_txid(stmt, "txid", &res[count-1].txid);
4736-
}
4737-
tal_free(stmt);
4738-
return res;
4739-
}
4740-
47414685
struct bitcoin_tx *wallet_get_funding_spend(const tal_t *ctx,
47424686
struct wallet *w,
47434687
u64 channel_id,

wallet/wallet.h

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

11831183
/**
1184-
* Store transactions of interest in the database to replay on restart
1184+
* Store funding txid spend to start replay on restart
1185+
* Note that tx should already be saved by wallet_transaction_add!
11851186
*/
1186-
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
1187-
const int type, const struct bitcoin_txid *txid,
1188-
const u32 input_num, const u32 blockheight);
1189-
1190-
/**
1191-
* List channels for which we had an onchaind running
1192-
*/
1193-
u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w);
1194-
1195-
/**
1196-
* Get transactions that we'd like to replay for a channel.
1197-
*/
1198-
struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w,
1199-
u32 channel_id);
1187+
void wallet_insert_funding_spend(struct wallet *w,
1188+
const struct channel *chan,
1189+
const struct bitcoin_txid *txid,
1190+
const u32 input_num, const u32 blockheight);
12001191

12011192
/**
12021193
* Get the transaction which spend funding for this channel, if any.

0 commit comments

Comments
 (0)