From 3dbaae38e3b4774ba25f571d6660a278ed4494cc Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 5 Aug 2019 23:16:32 +0200 Subject: [PATCH] wallet: Add function to add filteredblocks from backfilling Signed-off-by: Christian Decker --- wallet/wallet.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- wallet/wallet.h | 13 +++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/wallet/wallet.c b/wallet/wallet.c index dda68a17e..e0bab2f59 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -2412,6 +2411,53 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, outpointfilter_add(w->utxoset_outpoints, &txid, outnum); } +void wallet_filteredblock_add(struct wallet *w, struct filteredblock *fb) +{ + if (wallet_have_block(w, fb->height)) + return; + sqlite3_stmt *stmt = db_prepare(w->db, "INSERT OR IGNORE INTO blocks " + "(height, hash, prev_hash) " + "VALUES (?, ?, ?);"); + sqlite3_bind_int(stmt, 1, fb->height); + sqlite3_bind_sha256_double(stmt, 2, &fb->id.shad); + sqlite3_bind_sha256_double(stmt, 3, &fb->prev_hash.shad); + db_exec_prepared(w->db, stmt); + + for (size_t i = 0; i < tal_count(fb->outpoints); i++) { + struct filteredblock_outpoint *o = fb->outpoints[i]; + stmt = db_prepare(w->db, "INSERT INTO utxoset (" + " txid," + " outnum," + " blockheight," + " spendheight," + " txindex," + " scriptpubkey," + " satoshis" + ") VALUES(?, ?, ?, ?, ?, ?, ?);"); + sqlite3_bind_sha256_double(stmt, 1, &o->txid.shad); + sqlite3_bind_int(stmt, 2, o->outnum); + sqlite3_bind_int(stmt, 3, fb->height); + sqlite3_bind_null(stmt, 4); + sqlite3_bind_int(stmt, 5, o->txindex); + sqlite3_bind_blob(stmt, 6, o->scriptPubKey, + tal_count(o->scriptPubKey), SQLITE_TRANSIENT); + sqlite3_bind_amount_sat(stmt, 7, o->satoshis); + db_exec_prepared(w->db, stmt); + + outpointfilter_add(w->utxoset_outpoints, &o->txid, o->outnum); + } +} + +bool wallet_have_block(struct wallet *w, u32 blockheight) +{ + bool result; + sqlite3_stmt *stmt = db_select_prepare(w->db, "height FROM blocks WHERE height = ?"); + sqlite3_bind_int(stmt, 1, blockheight); + result = sqlite3_step(stmt) == SQLITE_ROW; + db_stmt_done(stmt); + return result; +} + struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx, const struct short_channel_id *scid) { diff --git a/wallet/wallet.h b/wallet/wallet.h index bb6224644..09fa42c63 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1023,6 +1024,11 @@ void wallet_block_remove(struct wallet *w, struct block *b); */ void wallet_blocks_rollback(struct wallet *w, u32 height); +/** + * Return whether we have a block for the given height. + */ +bool wallet_have_block(struct wallet *w, u32 blockheight); + /** * Mark an outpoint as spent, both in the owned as well as the UTXO set * @@ -1159,4 +1165,11 @@ void free_unreleased_txs(struct wallet *w); */ struct wallet_transaction *wallet_transactions_get(struct wallet *w, const tal_t *ctx); +/** + * Add a filteredblock to the blocks and utxoset tables. + * + * This can be used to backfill the blocks and still unspent UTXOs that were before our wallet birth height. + */ +void wallet_filteredblock_add(struct wallet *w, struct filteredblock *fb); + #endif /* LIGHTNING_WALLET_WALLET_H */