From 64f5d4a9ab2c24b2b616070a1b1af8be59849b11 Mon Sep 17 00:00:00 2001 From: Lee *!* Clagett Date: Fri, 10 Feb 2023 07:18:50 -0500 Subject: [PATCH] Fix rescan to invalid blockchain height bug (#48) --- src/db/storage.cpp | 18 ++++++++++++++++++ src/error.cpp | 2 ++ src/error.h | 1 + 3 files changed, 21 insertions(+) diff --git a/src/db/storage.cpp b/src/db/storage.cpp index 97a13ef..5bc3ac7 100644 --- a/src/db/storage.cpp +++ b/src/db/storage.cpp @@ -1352,6 +1352,24 @@ namespace db MONERO_PRECOND(db != nullptr); return db->try_write([this, height, addresses] (MDB_txn& txn) -> expect> { + { + cursor::blocks blocks_cur; + MONERO_CHECK(check_cursor(txn, this->db->tables.blocks, blocks_cur)); + + MDB_val key = lmdb::to_val(blocks_version); + MDB_val value{}; + + MONERO_LMDB_CHECK(mdb_cursor_get(blocks_cur.get(), &key, &value, MDB_SET)); + MONERO_LMDB_CHECK(mdb_cursor_get(blocks_cur.get(), &key, &value, MDB_LAST_DUP)); + + const expect current_height = + blocks.get_value(value); + if (!current_height) + return current_height.error(); + if (*current_height < height) + return {error::bad_height}; + } + std::vector updated{}; updated.reserve(addresses.size()); diff --git a/src/error.cpp b/src/error.cpp index 4ea69d0..9b63d14 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -57,6 +57,8 @@ namespace lws return "Received invalid transaction from REST client"; case error::bad_daemon_response: return "Response from monerod daemon was bad/unexpected"; + case error::bad_height: + return "Invalid blockchain height"; case error::blockchain_reorg: return "A blockchain reorg has been detected"; case error::configuration: diff --git a/src/error.h b/src/error.h index ca1fe58..0a847e7 100644 --- a/src/error.h +++ b/src/error.h @@ -42,6 +42,7 @@ namespace lws bad_blockchain, //!< Blockchain is invalid or wrong network type bad_client_tx, //!< REST client submitted invalid transaction bad_daemon_response, //!< RPC Response from daemon was invalid + bad_height, //!< Invalid blockchain height blockchain_reorg, //!< Blockchain reorg after fetching/scanning block(s) configuration, //!< Process configuration invalid crypto_failure, //!< Cryptographic function failed