From a8d587c418895dc92b4552f81df1660bf1a5ae6d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 28 Mar 2018 11:13:43 +0200 Subject: [PATCH] wallet: Return any eventual outpoint scid when marking it spent Just return the short_channel_id matching the outpoint that we just marked as spent. Signed-off-by: Christian Decker --- lightningd/chaintopology.c | 10 +++++++--- wallet/wallet.c | 29 +++++++++++++++++++++++++++-- wallet/wallet.h | 14 ++++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 727e2b36f..a58db2353 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -406,13 +406,17 @@ static void updates_complete(struct chain_topology *topo) */ static void topo_update_spends(struct chain_topology *topo, struct block *b) { + const struct short_channel_id *scid; for (size_t i = 0; i < tal_count(b->full_txs); i++) { const struct bitcoin_tx *tx = b->full_txs[i]; for (size_t j = 0; j < tal_count(tx->input); j++) { const struct bitcoin_tx_input *input = &tx->input[j]; - wallet_outpoint_spend(topo->wallet, b->height, - &input->txid, - input->index); + scid = wallet_outpoint_spend(topo->wallet, tmpctx, + b->height, &input->txid, + input->index); + if (scid) { + tal_free(scid); + } } } } diff --git a/wallet/wallet.c b/wallet/wallet.c index 460d03f1e..68d6e5217 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2016,10 +2016,13 @@ void wallet_blocks_rollback(struct wallet *w, u32 height) db_exec_prepared(w->db, stmt); } -void wallet_outpoint_spend(struct wallet *w, const u32 blockheight, - const struct bitcoin_txid *txid, const u32 outnum) +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) { + struct short_channel_id *scid; sqlite3_stmt *stmt; + int res; if (outpointfilter_matches(w->owned_outpoints, txid, outnum)) { stmt = db_prepare(w->db, "UPDATE outputs " @@ -2046,7 +2049,29 @@ void wallet_outpoint_spend(struct wallet *w, const u32 blockheight, sqlite3_bind_int(stmt, 3, outnum); db_exec_prepared(w->db, stmt); + + if (sqlite3_changes(w->db->sql) == 0) { + return NULL; + } + + /* Now look for the outpoint's short_channel_id */ + stmt = db_prepare(w->db, + "SELECT blockheight, txindex " + "FROM utxoset " + "WHERE txid = ? AND outnum = ?"); + sqlite3_bind_sha256_double(stmt, 1, &txid->shad); + sqlite3_bind_int(stmt, 2, outnum); + + res = sqlite3_step(stmt); + assert(res == SQLITE_ROW); + + scid = tal(ctx, struct short_channel_id); + mk_short_channel_id(scid, sqlite3_column_int(stmt, 0), + sqlite3_column_int(stmt, 1), outnum); + sqlite3_finalize(stmt); + return scid; } + return NULL; } void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, diff --git a/wallet/wallet.h b/wallet/wallet.h index bc162fc5f..4e70e143a 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -790,8 +790,18 @@ void wallet_block_remove(struct wallet *w, struct block *b); */ void wallet_blocks_rollback(struct wallet *w, u32 height); -void wallet_outpoint_spend(struct wallet *w, const u32 blockheight, - const struct bitcoin_txid *txid, const u32 outnum); +/** + * Mark an outpoint as spent, both in the owned as well as the UTXO set + * + * Given the outpoint (txid, outnum), and the blockheight, mark the + * corresponding DB entries as spent at the blockheight. + * + * @return scid The short_channel_id corresponding to the spent outpoint, if + * any. + */ +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); struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx, const struct short_channel_id *scid);