From 5e207f4ea5c6a46fea76897e3507569614cd70b8 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 16 Jun 2016 10:45:19 +1200 Subject: [PATCH] Improve comments per review --- src/rpcmining.cpp | 11 ++++------- src/test/miner_tests.cpp | 6 +++++- src/test/pow_tests.cpp | 10 +++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 49bb6387b..b424512b5 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -45,16 +45,13 @@ Value GetNetworkHashPS(int lookup, int height) { if (pb == NULL || !pb->nHeight) return 0; - // If lookup is -1, then use difficulty averaging window. + // If lookup is nonpositive, then use difficulty averaging window. if (lookup <= 0) lookup = pb->nHeight - Params().GetConsensus().nPowAveragingWindow; - // If lookup is still negative, then use blocks since genesis. - if (lookup <= 0) - lookup = pb->nHeight; - - // If lookup is larger than chain, then set it to chain length. - if (lookup > pb->nHeight) + // If lookup is still nonpositive, or is larger than chain, then set it to + // chain length. + if (lookup <= 0 || lookup > pb->nHeight) lookup = pb->nHeight; CBlockIndex *pb0 = pb; diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index f790769c9..5e4fd639a 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -158,7 +158,11 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) CBlock *pblock = &pblocktemplate->block; // pointer for convenience pblock->nVersion = 4; - // Fake the blocks taking at least nPowTargetSpacing + // Fake the blocks taking at least nPowTargetSpacing to be mined. + // GetMedianTimePast() returns the median of 11 blocks, so the timestamp + // of the next block must be six spacings ahead of that to be at least + // one spacing ahead of the tip. Within 11 blocks of genesis, the median + // will be closer to the tip, and blocks will appear slower. pblock->nTime = chainActive.Tip()->GetMedianTimePast()+6*Params().GetConsensus().nPowTargetSpacing; CMutableTransaction txCoinbase(pblock->vtx[0]); txCoinbase.vin[0].scriptSig = CScript() << (chainActive.Height()+1) << OP_0; diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 6815959bb..4bc499c41 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -22,7 +22,7 @@ BOOST_AUTO_TEST_CASE(get_next_work) int64_t nLastRetargetTime = 1262149169; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 32255; - pindexLast.nTime = 1262152739; // Block #32255 + pindexLast.nTime = 1262152739; // Block #32255 of Bitcoin pindexLast.nBits = 0x1d00ffff; BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d011998); } @@ -33,10 +33,10 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) SelectParams(CBaseChainParams::MAIN); const Consensus::Params& params = Params().GetConsensus(); - int64_t nLastRetargetTime = 1231006505; // Block #0 + int64_t nLastRetargetTime = 1231006505; // Block #0 of Bitcoin CBlockIndex pindexLast; pindexLast.nHeight = 2015; - pindexLast.nTime = 1233061996; // Block #2015 + pindexLast.nTime = 1233061996; // Block #2015 of Bitcoin // TODO change once the harder genesis block is generated pindexLast.nBits = 0x207fffff; BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x207fffff); @@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) int64_t nLastRetargetTime = 1279295950; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 68543; - pindexLast.nTime = 1279297671; // Block #68543 + pindexLast.nTime = 1279297671; // Block #68543 of Bitcoin pindexLast.nBits = 0x1c05a3f4; BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c05306f); } @@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) int64_t nLastRetargetTime = 1269207250; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 46367; - pindexLast.nTime = 1269211443; // Block #46367 + pindexLast.nTime = 1269211443; // Block #46367 of Bitcoin pindexLast.nBits = 0x1c387f6f; BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c418995); }