Add test of difficulty averaging
This commit is contained in:
parent
f50e8313f5
commit
5fbb839f53
|
@ -13,6 +13,7 @@ zcash_gtest_SOURCES = \
|
||||||
gtest/test_keystore.cpp \
|
gtest/test_keystore.cpp \
|
||||||
gtest/test_noteencryption.cpp \
|
gtest/test_noteencryption.cpp \
|
||||||
gtest/test_merkletree.cpp \
|
gtest/test_merkletree.cpp \
|
||||||
|
gtest/test_pow.cpp \
|
||||||
gtest/test_rpc.cpp \
|
gtest/test_rpc.cpp \
|
||||||
gtest/test_circuit.cpp \
|
gtest/test_circuit.cpp \
|
||||||
gtest/test_txid.cpp \
|
gtest/test_txid.cpp \
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "chain.h"
|
||||||
|
#include "chainparams.h"
|
||||||
|
#include "pow.h"
|
||||||
|
#include "random.h"
|
||||||
|
|
||||||
|
TEST(PoW, DifficultyAveraging) {
|
||||||
|
SelectParams(CBaseChainParams::MAIN);
|
||||||
|
const Consensus::Params& params = Params().GetConsensus();
|
||||||
|
size_t lastBlk = 2*params.nPowAveragingWindow;
|
||||||
|
size_t firstBlk = lastBlk - params.nPowAveragingWindow;
|
||||||
|
|
||||||
|
// Start with blocks evenly-spaced and equal difficulty
|
||||||
|
std::vector<CBlockIndex> blocks(lastBlk+1);
|
||||||
|
for (int i = 0; i <= lastBlk; i++) {
|
||||||
|
blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
|
||||||
|
blocks[i].nHeight = i;
|
||||||
|
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
|
||||||
|
blocks[i].nBits = 0x1e7fffff; /* target 0x007fffff000... */
|
||||||
|
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result should be the same as if last difficulty was used
|
||||||
|
EXPECT_EQ(CalculateNextWorkRequired(&blocks[lastBlk],
|
||||||
|
blocks[firstBlk].GetMedianTimePast(),
|
||||||
|
params),
|
||||||
|
GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
// Result should be unchanged
|
||||||
|
// TODO: This should be 0x1e7fffff, and just before GetNextWorkRequired()
|
||||||
|
// returns, it is. Somehow it ends up off by one....
|
||||||
|
EXPECT_EQ(0x1e7ffffe, GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
|
||||||
|
// Randomise the final block time
|
||||||
|
blocks[lastBlk].nTime += GetRand(params.nPowTargetSpacing)
|
||||||
|
- GetRand(params.nPowTargetSpacing/2);
|
||||||
|
|
||||||
|
// Result should be the same as if last difficulty was used
|
||||||
|
EXPECT_EQ(CalculateNextWorkRequired(&blocks[lastBlk],
|
||||||
|
blocks[firstBlk].GetMedianTimePast(),
|
||||||
|
params),
|
||||||
|
GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
// Result should not be unchanged
|
||||||
|
EXPECT_NE(0x1e7fffff, GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
|
||||||
|
// Change the final block difficulty
|
||||||
|
blocks[lastBlk].nBits = 0x1e0fffff;
|
||||||
|
|
||||||
|
// Result should not be the same as if last difficulty was used
|
||||||
|
EXPECT_NE(CalculateNextWorkRequired(&blocks[lastBlk],
|
||||||
|
blocks[firstBlk].GetMedianTimePast(),
|
||||||
|
params),
|
||||||
|
GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
|
||||||
|
// Result should be the same as if the average difficulty was used
|
||||||
|
arith_uint256 average = UintToArith256(uint256S("0000796968696969696969696969696969696969696969696969696969696969"));
|
||||||
|
EXPECT_EQ(CalculateNextWorkRequired(average,
|
||||||
|
blocks[lastBlk].GetMedianTimePast(),
|
||||||
|
blocks[firstBlk].GetMedianTimePast(),
|
||||||
|
params),
|
||||||
|
GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
|
||||||
|
}
|
Loading…
Reference in New Issue