From c90344ea47c58f705934abb838258740dd083e33 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Oct 2024 21:52:33 +0200 Subject: [PATCH] an attempt to fix incorrect PoW block template creation in case when there are lots of PoS blocks --- src/currency_core/blockchain_storage.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index e41c9ada..4911c3c2 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -1553,8 +1553,21 @@ bool blockchain_storage::create_block_template(const create_block_template_param } diffic = get_next_diff_conditional(pos); + CHECK_AND_ASSERT_MES(diffic, false, "get_next_diff_conditional failed"); + + // check PoW block timestamp against the current blockchain timestamp median -- if it's not okay, don't create a new block + // TODO (performance) both get_next_diff_conditional and get_last_n_blocks_timestamps obtains last N blocks, consider data reusing -- sowle + if (!pos) + { + std::vector timestamps = get_last_n_blocks_timestamps(BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW); + uint64_t median_ts = epee::misc_utils::median(timestamps); + if(b.timestamp < median_ts) + { + LOG_PRINT_YELLOW("Block template construction failed because current core timestamp, " << b.timestamp << ", is less than median of last " << BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW << " blocks, " << median_ts, LOG_LEVEL_2); + return false; + } + } - CHECK_AND_ASSERT_MES(diffic, false, "difficulty owverhead.");