From 9ab28d1659359eb38c8e508de95c5473eb6d8227 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 5 Apr 2018 18:09:46 +0200 Subject: [PATCH] topology: Two off-by-one error when catching up with the blockchain There are two very hard problems in software engineering: 1. Off-by-one errors In this case we were rolling back further than needed and we were starting the catchup one block further than expected. Signed-off-by: Christian Decker --- lightningd/chaintopology.c | 2 +- wallet/wallet.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 7618fa0ac..5388fdb8c 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -573,7 +573,7 @@ static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount, /* Rollback to the given blockheight, so we start track * correctly again */ - wallet_blocks_rollback(topo->wallet, topo->first_blocknum - 1); + wallet_blocks_rollback(topo->wallet, topo->first_blocknum); /* Get up to speed with topology. */ bitcoind_getblockhash(bitcoind, topo->first_blocknum, diff --git a/wallet/wallet.c b/wallet/wallet.c index 329f24d00..a66324819 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2027,7 +2027,7 @@ void wallet_block_remove(struct wallet *w, struct block *b) void wallet_blocks_rollback(struct wallet *w, u32 height) { sqlite3_stmt *stmt = db_prepare(w->db, "DELETE FROM blocks " - "WHERE height >= ?"); + "WHERE height > ?"); sqlite3_bind_int(stmt, 1, height); db_exec_prepared(w->db, stmt); }