From faf6304bdfdf228354b4072b72f4c0ef90fdaade Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 16 May 2025 13:06:03 +0200 Subject: [PATCH] test: Use mockable time in GuessVerificationProgress Also add a test. --- src/kernel/chainparams.cpp | 6 +++--- src/validation.cpp | 2 +- test/functional/rpc_blockchain.py | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 0a359e6620..f81758879b 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -613,9 +613,9 @@ public: }; chainTxData = ChainTxData{ - 0, - 0, - 0 + .nTime = 0, + .tx_count = 0, + .dTxRate = 0.001, // Set a non-zero rate to make it testable }; base58Prefixes[PUBKEY_ADDRESS] = std::vector(1,111); diff --git a/src/validation.cpp b/src/validation.cpp index 13dbe83f80..a2e1b8c195 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5572,7 +5572,7 @@ double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) c return 0.0; } - int64_t nNow = time(nullptr); + const int64_t nNow{TicksSinceEpoch(NodeClock::now())}; double fTxTotal; diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 2976f9188a..a323d5eebc 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -101,6 +101,7 @@ class BlockchainTest(BitcoinTestFramework): self._test_waitforblockheight() self._test_getblock() self._test_getdeploymentinfo() + self._test_verificationprogress() self._test_y2106() assert self.nodes[0].verifychain(4, 0) @@ -280,6 +281,14 @@ class BlockchainTest(BitcoinTestFramework): # calling with an explicit hash works self.check_signalling_deploymentinfo_result(self.nodes[0].getdeploymentinfo(gbci207["bestblockhash"]), gbci207["blocks"], gbci207["bestblockhash"], "started") + def _test_verificationprogress(self): + self.log.info("Check that verificationprogress is less than 1 when the block tip is old") + future = 2 * 60 * 60 + self.nodes[0].setmocktime(self.nodes[0].getblockchaininfo()["time"] + future + 1) + assert_greater_than(1, self.nodes[0].getblockchaininfo()["verificationprogress"]) + self.nodes[0].setmocktime(self.nodes[0].getblockchaininfo()["time"] + future) + assert_greater_than(1, self.nodes[0].getblockchaininfo()["verificationprogress"]) + def _test_y2106(self): self.log.info("Check that block timestamps work until year 2106") self.generate(self.nodes[0], 8)[-1]