wallet: Add primitives to store blockchain internally

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2018-02-17 16:45:34 +01:00
parent fea03dbca9
commit 504202973f
2 changed files with 41 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -10,6 +10,7 @@
#include <ccan/tal/tal.h>
#include <common/channel_config.h>
#include <common/utxo.h>
#include <lightningd/chaintopology.h>
#include <lightningd/htlc_end.h>
#include <lightningd/invoice.h>
#include <onchaind/onchain_wire.h>
@@ -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 */