mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
chaintopo: Record outpoint spends for owned outputs
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
2680e6d9ff
commit
ae30942e3f
4 changed files with 44 additions and 0 deletions
|
@ -348,6 +348,22 @@ static void updates_complete(struct chain_topology *topo)
|
||||||
next_topology_timer(topo);
|
next_topology_timer(topo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* topo_update_spends -- Tell the wallet about all spent outpoints
|
||||||
|
*/
|
||||||
|
static void topo_update_spends(struct chain_topology *topo, struct block *b)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void add_tip(struct chain_topology *topo, struct block *b)
|
static void add_tip(struct chain_topology *topo, struct block *b)
|
||||||
{
|
{
|
||||||
/* Attach to tip; b is now the tip. */
|
/* Attach to tip; b is now the tip. */
|
||||||
|
@ -357,6 +373,8 @@ static void add_tip(struct chain_topology *topo, struct block *b)
|
||||||
topo->tip = b;
|
topo->tip = b;
|
||||||
wallet_block_add(topo->wallet, b);
|
wallet_block_add(topo->wallet, b);
|
||||||
|
|
||||||
|
topo_update_spends(topo, b);
|
||||||
|
|
||||||
/* Only keep the transactions we care about. */
|
/* Only keep the transactions we care about. */
|
||||||
filter_block_txs(topo, b);
|
filter_block_txs(topo, b);
|
||||||
|
|
||||||
|
|
|
@ -258,6 +258,10 @@ struct json_result *null_response(const tal_t *ctx UNNEEDED)
|
||||||
void outpointfilter_add(struct outpointfilter *of UNNEEDED,
|
void outpointfilter_add(struct outpointfilter *of UNNEEDED,
|
||||||
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED)
|
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED)
|
||||||
{ fprintf(stderr, "outpointfilter_add called!\n"); abort(); }
|
{ fprintf(stderr, "outpointfilter_add called!\n"); abort(); }
|
||||||
|
/* Generated stub for outpointfilter_matches */
|
||||||
|
bool outpointfilter_matches(struct outpointfilter *of UNNEEDED,
|
||||||
|
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED)
|
||||||
|
{ fprintf(stderr, "outpointfilter_matches called!\n"); abort(); }
|
||||||
/* Generated stub for outpointfilter_new */
|
/* Generated stub for outpointfilter_new */
|
||||||
struct outpointfilter *outpointfilter_new(tal_t *ctx UNNEEDED)
|
struct outpointfilter *outpointfilter_new(tal_t *ctx UNNEEDED)
|
||||||
{ fprintf(stderr, "outpointfilter_new called!\n"); abort(); }
|
{ fprintf(stderr, "outpointfilter_new called!\n"); abort(); }
|
||||||
|
|
|
@ -1783,3 +1783,22 @@ void wallet_blocks_rollback(struct wallet *w, u32 height)
|
||||||
sqlite3_bind_int(stmt, 1, height);
|
sqlite3_bind_int(stmt, 1, height);
|
||||||
db_exec_prepared(w->db, stmt);
|
db_exec_prepared(w->db, stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wallet_outpoint_spend(struct wallet *w, const u32 blockheight,
|
||||||
|
const struct bitcoin_txid *txid, const u32 outnum)
|
||||||
|
{
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
if (outpointfilter_matches(w->owned_outpoints, txid, outnum)) {
|
||||||
|
stmt = db_prepare(w->db,
|
||||||
|
"UPDATE outputs "
|
||||||
|
"SET spend_height = ? "
|
||||||
|
"WHERE prev_out_tx = ?"
|
||||||
|
" AND prev_out_index = ?");
|
||||||
|
|
||||||
|
sqlite3_bind_int(stmt, 1, blockheight);
|
||||||
|
sqlite3_bind_sha256_double(stmt, 2, &txid->shad);
|
||||||
|
sqlite3_bind_int(stmt, 3, outnum);
|
||||||
|
|
||||||
|
db_exec_prepared(w->db, stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -704,4 +704,7 @@ void wallet_block_remove(struct wallet *w, struct block *b);
|
||||||
*/
|
*/
|
||||||
void wallet_blocks_rollback(struct wallet *w, u32 height);
|
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);
|
||||||
|
|
||||||
#endif /* WALLET_WALLET_H */
|
#endif /* WALLET_WALLET_H */
|
||||||
|
|
Loading…
Add table
Reference in a new issue