Browse Source

wallet: Add primitives to store blockchain internally

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
504202973f
  1. 29
      wallet/wallet.c
  2. 12
      wallet/wallet.h

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

12
wallet/wallet.h

@ -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 */

Loading…
Cancel
Save