Diff-minimise

This commit is contained in:
Luke Dashjr 2023-10-03 18:26:54 +00:00
parent 319e6c2bed
commit cf1bda10ff
2 changed files with 8 additions and 9 deletions

View File

@ -49,9 +49,8 @@ using node::UpdateTime;
/**
* Return average network hashes per second based on the last 'lookup' blocks,
* or from the last difficulty change if 'lookup' is -1.
* If 'height' is -1, compute the estimate from current chain tip.
* If 'height' is a valid block height, compute the estimate at the time when a given block was found.
* or from the last difficulty change if 'lookup' is nonpositive.
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
*/
static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) {
if (lookup < -1 || lookup == 0) {
@ -64,7 +63,7 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch
const CBlockIndex* pb = active_chain.Tip();
if (height >= 0) {
if (height >= 0 && height < active_chain.Height()) {
pb = active_chain[height];
}
@ -72,7 +71,7 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch
return 0;
// If lookup is -1, then use blocks since last difficulty change.
if (lookup == -1)
if (lookup <= 0)
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
// If lookup is larger than chain, then set it to chain length.

View File

@ -437,6 +437,7 @@ class BlockchainTest(BitcoinTestFramework):
def _test_getnetworkhashps(self):
self.log.info("Test getnetworkhashps")
hashes_per_second = self.nodes[0].getnetworkhashps()
assert_raises_rpc_error(
-3,
textwrap.dedent("""
@ -448,6 +449,9 @@ class BlockchainTest(BitcoinTestFramework):
""").strip(),
lambda: self.nodes[0].getnetworkhashps("a", []),
)
# This should be 2 hashes every 10 minutes or 1/300
assert abs(hashes_per_second * 300 - 1) < 0.0001
assert_raises_rpc_error(
-8,
"Block does not exist at specified height",
@ -473,10 +477,6 @@ class BlockchainTest(BitcoinTestFramework):
hashes_per_second = self.nodes[0].getnetworkhashps(100, 0)
assert_equal(hashes_per_second, 0)
# This should be 2 hashes every 10 minutes or 1/300
hashes_per_second = self.nodes[0].getnetworkhashps()
assert abs(hashes_per_second * 300 - 1) < 0.0001
# Ensure long lookups get truncated to chain length
hashes_per_second = self.nodes[0].getnetworkhashps(self.nodes[0].getblockcount() + 1000)
assert hashes_per_second > 0.003