-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpow_tests.cpp
105 lines (87 loc) · 3.88 KB
/
pow_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2015-2018 The Bitcoin Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chain.h>
#include <chainparams.h>
#include <pow.h>
#include <random.h>
#include <util.h>
#include <test/test_bitcoin.h>
#include <boost/test/unit_test.hpp>
using namespace std;
BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg,
int64_t nLastBlockTime, int64_t nFirstBlockTime,
const Consensus::Params& params)
{
return CalculateNextWorkRequired(bnAvg, nLastBlockTime, nFirstBlockTime, params, UintToArith256(params.powLimit), false);
}
/* Test calculation of next difficulty target with no constraints applying */
BOOST_AUTO_TEST_CASE(get_next_work)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
int64_t nLastRetargetTime = 1262149169; // NOTE: Not an actual block time
int64_t nThisTime = 1262152739; // Block #32255 of Bitcoin
arith_uint256 bnAvg;
bnAvg.SetCompact(0x1d00ffff);
BOOST_CHECK_EQUAL(0x1d011998,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
}
/* Test the constraint on the upper bound for next work */
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
int64_t nLastRetargetTime = 1231006505; // Block #0 of Bitcoin
int64_t nThisTime = 1233061996; // Block #2015 of Bitcoin
arith_uint256 bnAvg;
bnAvg.SetCompact(0x1f07ffff);
BOOST_CHECK_EQUAL(0x1f07ffff,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
}
/* Test the constraint on the lower bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
int64_t nLastRetargetTime = 1279296753; // NOTE: Not an actual block time
int64_t nThisTime = 1279297671; // Block #68543 of Bitcoin
arith_uint256 bnAvg;
bnAvg.SetCompact(0x1c05a3f4);
BOOST_CHECK_EQUAL(0x1c04bceb,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
}
/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
int64_t nLastRetargetTime = 1269205629; // NOTE: Not an actual block time
int64_t nThisTime = 1269211443; // Block #46367 of Bitcoin
arith_uint256 bnAvg;
bnAvg.SetCompact(0x1c387f6f);
BOOST_CHECK_EQUAL(0x1c4a93bb,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
}
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
std::vector<CBlockIndex> blocks(10000);
for (int i = 0; i < 10000; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : NULL;
blocks[i].nHeight = i;
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
}
for (int j = 0; j < 1000; j++) {
CBlockIndex *p1 = &blocks[GetRand(10000)];
CBlockIndex *p2 = &blocks[GetRand(10000)];
CBlockIndex *p3 = &blocks[GetRand(10000)];
int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
}
}
BOOST_AUTO_TEST_SUITE_END()