diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 5595cd6c8..4e3d28a72 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -741,7 +741,7 @@ log_fee: */ static void topo_update_spends(struct chain_topology *topo, struct block *b) { - const struct short_channel_id *scid; + const struct short_channel_id *spent_scids; for (size_t i = 0; i < tal_count(b->full_txs); i++) { const struct bitcoin_tx *tx = b->full_txs[i]; bool our_tx = true, includes_our_spend = false; @@ -757,15 +757,9 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b) bitcoin_tx_input_get_txid(tx, j, &outpoint_txid); - scid = wallet_outpoint_spend(topo->ld->wallet, tmpctx, - b->height, &outpoint_txid, - input->index, - &our_spend); - if (scid) { - gossipd_notify_spend(topo->bitcoind->ld, scid); - tal_free(scid); - } - + our_spend = wallet_outpoint_spend( + topo->ld->wallet, tmpctx, b->height, &outpoint_txid, + input->index); our_tx &= our_spend; includes_our_spend |= our_spend; if (our_spend) { @@ -783,6 +777,15 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b) record_tx_outs_and_fees(topo->ld, tx, &txid, b->height, inputs_total, our_tx); } + /* Retrieve all potential channel closes from the UTXO set and + * tell gossipd about them. */ + spent_scids = + wallet_utxoset_get_spent(tmpctx, topo->ld->wallet, b->height); + + for (size_t i=0; ibitcoind->ld, &spent_scids[i]); + } + tal_free(spent_scids); } static void topo_add_utxos(struct chain_topology *topo, struct block *b) diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index 9960466ed..418f10a8c 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -1466,12 +1466,6 @@ struct db_query db_postgres_queries[] = { .placeholders = 3, .readonly = false, }, - { - .name = "SELECT blockheight, txindex FROM utxoset WHERE txid = ? AND outnum = ?", - .query = "SELECT blockheight, txindex FROM utxoset WHERE txid = $1 AND outnum = $2", - .placeholders = 2, - .readonly = true, - }, { .name = "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);", .query = "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES($1, $2, $3, $4, $5, $6, $7);", @@ -1642,10 +1636,10 @@ struct db_query db_postgres_queries[] = { }, }; -#define DB_POSTGRES_QUERY_COUNT 272 +#define DB_POSTGRES_QUERY_COUNT 271 #endif /* HAVE_POSTGRES */ #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:9c0282186a89a37a232b8a4f34dceabaf5b53b7cc5c3bc24eb4e53967662cb6f +// SHA256STAMP:93c3f12a788012003366f01547f6059bc7894cddd9cf3b046b908f5458452da7 diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index 45a597d72..4ea9d086c 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -1466,12 +1466,6 @@ struct db_query db_sqlite3_queries[] = { .placeholders = 3, .readonly = false, }, - { - .name = "SELECT blockheight, txindex FROM utxoset WHERE txid = ? AND outnum = ?", - .query = "SELECT blockheight, txindex FROM utxoset WHERE txid = ? AND outnum = ?", - .placeholders = 2, - .readonly = true, - }, { .name = "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);", .query = "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);", @@ -1642,10 +1636,10 @@ struct db_query db_sqlite3_queries[] = { }, }; -#define DB_SQLITE3_QUERY_COUNT 272 +#define DB_SQLITE3_QUERY_COUNT 271 #endif /* HAVE_SQLITE3 */ #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:9c0282186a89a37a232b8a4f34dceabaf5b53b7cc5c3bc24eb4e53967662cb6f +// SHA256STAMP:93c3f12a788012003366f01547f6059bc7894cddd9cf3b046b908f5458452da7 diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index b1cf1769d..11eca0a97 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -946,7 +946,7 @@ msgstr "" msgid "DELETE FROM utxoset WHERE spendheight < ?" msgstr "" -#: wallet/wallet.c:2965 wallet/wallet.c:3111 +#: wallet/wallet.c:2965 wallet/wallet.c:3079 msgid "INSERT INTO blocks (height, hash, prev_hash) VALUES (?, ?, ?);" msgstr "" @@ -962,115 +962,111 @@ msgstr "" msgid "DELETE FROM blocks WHERE height > ?" msgstr "" -#: wallet/wallet.c:3015 +#: wallet/wallet.c:3011 msgid "UPDATE outputs SET spend_height = ?, status = ? WHERE prev_out_tx = ? AND prev_out_index = ?" msgstr "" -#: wallet/wallet.c:3033 +#: wallet/wallet.c:3029 msgid "UPDATE utxoset SET spendheight = ? WHERE txid = ? AND outnum = ?" msgstr "" -#: wallet/wallet.c:3052 -msgid "SELECT blockheight, txindex FROM utxoset WHERE txid = ? AND outnum = ?" -msgstr "" - -#: wallet/wallet.c:3084 wallet/wallet.c:3122 +#: wallet/wallet.c:3052 wallet/wallet.c:3090 msgid "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:3148 +#: wallet/wallet.c:3116 msgid "SELECT height FROM blocks WHERE height = ?" msgstr "" -#: wallet/wallet.c:3161 +#: wallet/wallet.c:3129 msgid "SELECT txid, spendheight, scriptpubkey, satoshis FROM utxoset WHERE blockheight = ? AND txindex = ? AND outnum = ? AND spendheight IS NULL" msgstr "" -#: wallet/wallet.c:3203 +#: wallet/wallet.c:3171 msgid "SELECT blockheight, txindex, outnum FROM utxoset WHERE spendheight = ?" msgstr "" -#: wallet/wallet.c:3234 wallet/wallet.c:3394 +#: wallet/wallet.c:3202 wallet/wallet.c:3362 msgid "SELECT blockheight FROM transactions WHERE id=?" msgstr "" -#: wallet/wallet.c:3244 +#: wallet/wallet.c:3212 msgid "INSERT INTO transactions ( id, blockheight, txindex, rawtx) VALUES (?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:3265 +#: wallet/wallet.c:3233 msgid "UPDATE transactions SET blockheight = ?, txindex = ? WHERE id = ?" msgstr "" -#: wallet/wallet.c:3282 +#: wallet/wallet.c:3250 msgid "INSERT INTO transaction_annotations (txid, idx, location, type, channel) VALUES (?, ?, ?, ?, ?) ON CONFLICT(txid,idx) DO NOTHING;" msgstr "" -#: wallet/wallet.c:3314 +#: wallet/wallet.c:3282 msgid "SELECT type, channel_id FROM transactions WHERE id=?" msgstr "" -#: wallet/wallet.c:3330 +#: wallet/wallet.c:3298 msgid "UPDATE transactions SET type = ?, channel_id = ? WHERE id = ?" msgstr "" -#: wallet/wallet.c:3349 +#: wallet/wallet.c:3317 msgid "SELECT type FROM transactions WHERE id=?" msgstr "" -#: wallet/wallet.c:3372 +#: wallet/wallet.c:3340 msgid "SELECT rawtx FROM transactions WHERE id=?" msgstr "" -#: wallet/wallet.c:3418 +#: wallet/wallet.c:3386 msgid "SELECT blockheight, txindex FROM transactions WHERE id=?" msgstr "" -#: wallet/wallet.c:3446 +#: wallet/wallet.c:3414 msgid "SELECT id FROM transactions WHERE blockheight=?" msgstr "" -#: wallet/wallet.c:3465 +#: wallet/wallet.c:3433 msgid "INSERT INTO channeltxs ( channel_id, type, transaction_id, input_num, blockheight) VALUES (?, ?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:3489 +#: wallet/wallet.c:3457 msgid "SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;" msgstr "" -#: wallet/wallet.c:3510 +#: wallet/wallet.c:3478 msgid "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;" msgstr "" -#: wallet/wallet.c:3555 +#: wallet/wallet.c:3523 msgid "UPDATE forwarded_payments SET in_msatoshi=?, out_msatoshi=?, state=?, resolved_time=?, failcode=? WHERE in_htlc_id=?" msgstr "" -#: wallet/wallet.c:3613 +#: wallet/wallet.c:3581 msgid "INSERT INTO forwarded_payments ( in_htlc_id, out_htlc_id, in_channel_scid, out_channel_scid, in_msatoshi, out_msatoshi, state, received_time, resolved_time, failcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:3672 +#: wallet/wallet.c:3640 msgid "SELECT CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)FROM forwarded_payments WHERE state = ?;" msgstr "" -#: wallet/wallet.c:3696 +#: wallet/wallet.c:3664 msgid "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)" msgstr "" -#: wallet/wallet.c:3784 +#: wallet/wallet.c:3752 msgid "SELECT t.id, t.rawtx, t.blockheight, t.txindex, t.type as txtype, c2.short_channel_id as txchan, a.location, a.idx as ann_idx, a.type as annotation_type, c.short_channel_id FROM transactions t LEFT JOIN transaction_annotations a ON (a.txid = t.id) LEFT JOIN channels c ON (a.channel = c.id) LEFT JOIN channels c2 ON (t.channel_id = c2.id) ORDER BY t.blockheight, t.txindex ASC" msgstr "" -#: wallet/wallet.c:3878 +#: wallet/wallet.c:3846 msgid "INSERT INTO penalty_bases ( channel_id, commitnum, txid, outnum, amount) VALUES (?, ?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:3903 +#: wallet/wallet.c:3871 msgid "SELECT commitnum, txid, outnum, amount FROM penalty_bases WHERE channel_id = ?" msgstr "" -#: wallet/wallet.c:3927 +#: wallet/wallet.c:3895 msgid "DELETE FROM penalty_bases WHERE channel_id = ? AND commitnum = ?" msgstr "" @@ -1085,4 +1081,4 @@ msgstr "" #: wallet/test/run-wallet.c:1345 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:6123671bea9e0a84e643ce453078dbdc4c8d12a3532e3c0354656eeaaff73f4b +# SHA256STAMP:1276c6c01e98d0dda121978e23a54023515df27ebf4a751e1887e9d5255f6670 diff --git a/wallet/wallet.c b/wallet/wallet.c index 297a80c0f..0f9af3d3d 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -3002,15 +3002,11 @@ void wallet_blocks_rollback(struct wallet *w, u32 height) db_exec_prepared_v2(take(stmt)); } -const struct short_channel_id * -wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight, - const struct bitcoin_txid *txid, const u32 outnum, - bool *our_spend) +bool wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight, + const struct bitcoin_txid *txid, const u32 outnum) { - struct short_channel_id *scid; struct db_stmt *stmt; - bool res; - int changes; + bool our_spend; if (outpointfilter_matches(w->owned_outpoints, txid, outnum)) { stmt = db_prepare_v2(w->db, SQL("UPDATE outputs " "SET spend_height = ?, " @@ -3025,9 +3021,9 @@ wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight, db_exec_prepared_v2(take(stmt)); - *our_spend = true; + our_spend = true; } else - *our_spend = false; + our_spend = false; if (outpointfilter_matches(w->utxoset_outpoints, txid, outnum)) { stmt = db_prepare_v2(w->db, SQL("UPDATE utxoset " @@ -3038,38 +3034,10 @@ wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight, db_bind_int(stmt, 0, blockheight); db_bind_sha256d(stmt, 1, &txid->shad); db_bind_int(stmt, 2, outnum); - db_exec_prepared_v2(stmt); - changes = db_count_changes(stmt); - tal_free(stmt); - - if (changes == 0) { - return NULL; - } - - /* Now look for the outpoint's short_channel_id */ - stmt = - db_prepare_v2(w->db, SQL("SELECT " - "blockheight, txindex " - "FROM utxoset " - "WHERE txid = ? AND outnum = ?")); - db_bind_sha256d(stmt, 0, &txid->shad); - db_bind_int(stmt, 1, outnum); - db_query_prepared(stmt); - - res = db_step(stmt); - assert(res); - - scid = tal(ctx, struct short_channel_id); - if (!mk_short_channel_id(scid, db_column_int(stmt, 0), - db_column_int(stmt, 1), outnum)) - fatal("wallet_outpoint_spend: invalid scid %u:%u:%u", - db_column_int(stmt, 0), - db_column_int(stmt, 1), outnum); tal_free(stmt); - return scid; } - return NULL; + return our_spend; } void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, diff --git a/wallet/wallet.h b/wallet/wallet.h index 635b7348c..c97e13203 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1093,14 +1093,11 @@ bool wallet_have_block(struct wallet *w, u32 blockheight); * Given the outpoint (txid, outnum), and the blockheight, mark the * corresponding DB entries as spent at the blockheight. * - * @our_spend - set to true if found in our wallet's output set, false otherwise - * @return scid The short_channel_id corresponding to the spent outpoint, if - * any. + * @return true if found in our wallet's output set, false otherwise */ -const struct short_channel_id * -wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight, - const struct bitcoin_txid *txid, const u32 outnum, - bool *our_spend); +bool wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, + const u32 blockheight, + const struct bitcoin_txid *txid, const u32 outnum); struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx, const struct short_channel_id *scid);