From 504202973f2cf9af97dd38d115035ec8cbbfbaa6 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 17 Feb 2018 16:45:34 +0100 Subject: [PATCH] wallet: Add primitives to store blockchain internally Signed-off-by: Christian Decker --- wallet/wallet.c | 29 +++++++++++++++++++++++++++++ wallet/wallet.h | 12 ++++++++++++ 2 files changed, 41 insertions(+) diff --git a/wallet/wallet.c b/wallet/wallet.c index bc562e224..89e3346b6 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1660,3 +1660,32 @@ bool wallet_network_check(struct wallet *w, } return true; } + +void wallet_block_add(struct wallet *w, struct block *b) +{ + sqlite3_stmt *stmt = db_prepare(w->db, + "INSERT INTO blocks " + "(height, hash, prev_hash) " + "VALUES (?, ?, ?);"); + sqlite3_bind_int(stmt, 1, b->height); + sqlite3_bind_sha256_double(stmt, 2, &b->blkid.shad); + if (b->prev) { + sqlite3_bind_sha256_double(stmt, 3, &b->prev->blkid.shad); + }else { + sqlite3_bind_null(stmt, 3); + } + db_exec_prepared(w->db, stmt); +} + +void wallet_block_remove(struct wallet *w, struct block *b) +{ + sqlite3_stmt *stmt = db_prepare(w->db, + "DELETE FROM blocks WHERE hash = ?"); + sqlite3_bind_sha256_double(stmt, 1, &b->blkid.shad); + db_exec_prepared(w->db, stmt); + + stmt = db_prepare(w->db, "SELECT * FROM blocks WHERE height >= ?;"); + sqlite3_bind_int(stmt, 1, b->height); + assert(sqlite3_step(stmt) == SQLITE_DONE); + sqlite3_finalize(stmt); +} diff --git a/wallet/wallet.h b/wallet/wallet.h index 68f2090be..a3f66fd7a 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -684,4 +685,15 @@ void wallet_htlc_sigs_save(struct wallet *w, u64 channel_id, bool wallet_network_check(struct wallet *w, const struct chainparams *chainparams); +/** + * wallet_block_add - Add a block to the blockchain tracked by this wallet + */ +void wallet_block_add(struct wallet *w, struct block *b); + +/** + * wallet_block_remove - Remove a block (and all its descendants) from the tracked blockchain + */ +void wallet_block_remove(struct wallet *w, struct block *b); + + #endif /* WALLET_WALLET_H */