From af2456c624d32ddc5145cc930b6e230a06e86fc0 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 07:53:24 +0200 Subject: [PATCH] csnode: give BlockChain's consensus-settings members defined defaults blockRewardIntegral_/Fraction_, miningCoefficientIntegral_/Fraction_, stakingOn_, miningOn_ and TimeMinStage1_ had no initializer and are not set by the BlockChain constructor. They are written into every quick-start checkpoint (BlockChain_Serializer::save), read back on the --set-bc-top path (Node::init) and emitted by successfullQuickStartEvent. Before any consensus-settings change, or a cache load, has assigned them, that is undefined behaviour. In particular, a checkpoint saved by a slow-starting node before the order-37 activation can carry arbitrary reward settings. Use the same defaults as BlockChain_Serializer::clear(): zero amounts, both flags off, TimeMinStage1 = 500. Nothing changes for members that are assigned before use. Test: BlockChain.ConsensusSettingsHaveDefinedDefaults. It documents the defaults; the old code may pass it by chance, since reading uninitialised memory often yields zero. nodetests: 121/121 pass. Co-Authored-By: Claude Opus 5.5 --- csnode/include/csnode/blockchain.hpp | 15 ++++++++------- tests/csnode/blockchain_tests.cpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/csnode/include/csnode/blockchain.hpp b/csnode/include/csnode/blockchain.hpp index a40ef8fb8..348aea307 100644 --- a/csnode/include/csnode/blockchain.hpp +++ b/csnode/include/csnode/blockchain.hpp @@ -553,12 +553,13 @@ public slots: static constexpr size_t kQuickStartSaveCachesInterval = 500'000; std::chrono::steady_clock::time_point lastCheckpointWallClock_ = std::chrono::steady_clock::now(); - int32_t blockRewardIntegral_; - uint64_t blockRewardFraction_ ; - int32_t miningCoefficientIntegral_; - uint64_t miningCoefficientFraction_; - bool stakingOn_; - bool miningOn_; - uint32_t TimeMinStage1_; + // consensus settings persisted in quick-start caches; same defaults as BlockChain_Serializer::clear() + int32_t blockRewardIntegral_ = 0; + uint64_t blockRewardFraction_ = 0; + int32_t miningCoefficientIntegral_ = 0; + uint64_t miningCoefficientFraction_ = 0; + bool stakingOn_ = false; + bool miningOn_ = false; + uint32_t TimeMinStage1_ = 500; }; #endif // BLOCKCHAIN_HPP diff --git a/tests/csnode/blockchain_tests.cpp b/tests/csnode/blockchain_tests.cpp index dcaa2406e..66c9a1833 100644 --- a/tests/csnode/blockchain_tests.cpp +++ b/tests/csnode/blockchain_tests.cpp @@ -61,3 +61,17 @@ TEST(BlockChain, ContentEqualRequiresSameTrustedMask) { canonical.add_real_trusted(cs::Utils::maskToBits(cs::Bytes{ 0, 255, 1 })); ASSERT_FALSE(BlockChain::testContentEqual(own, canonical)); } + +// these members are saved into quick-start caches and read back on the --set-bc-top path, +// possibly before any consensus-settings change has set them +TEST(BlockChain, ConsensusSettingsHaveDefinedDefaults) { + const csdb::Address genesis = csdb::Address::from_string("0000000000000000000000000000000000000000000000000000000000000001"); + const csdb::Address start = csdb::Address::from_string("0000000000000000000000000000000000000000000000000000000000000002"); + BlockChain blockChain(genesis, start); + + ASSERT_TRUE(blockChain.getBlockReward() == csdb::Amount{ 0 }); + ASSERT_TRUE(blockChain.getMiningCoefficient() == csdb::Amount{ 0 }); + ASSERT_FALSE(blockChain.getMiningOn()); + ASSERT_FALSE(blockChain.getStakingOn()); + ASSERT_EQ(blockChain.getTimeMinStage1(), 500u); +}