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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
// Copyright (c) 2015 The Discoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "arith_uint256.h"
#include "chainparams.h"
#include "discoin.h"
#include "test/test_bitcoin.h"
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(discoin_tests, TestingSetup)
/**
* the maximum block reward at a given height for a block without fees
*/
uint64_t expectedMaxSubsidy(int height) {
if (height < 100000) {
return 1000000 * COIN;
} else if (height < 145000) {
return 500000 * COIN;
} else if (height < 200000) {
return 250000 * COIN;
} else if (height < 300000) {
return 125000 * COIN;
} else if (height < 400000) {
return 62500 * COIN;
} else if (height < 500000) {
return 31250 * COIN;
} else if (height < 600000) {
return 15625 * COIN;
} else {
return 10000 * COIN;
}
}
/**
* the minimum possible value for the maximum block reward at a given height
* for a block without fees
*/
uint64_t expectedMinSubsidy(int height) {
if (height < 100000) {
return 0;
} else if (height < 145000) {
return 0;
} else if (height < 200000) {
return 250000 * COIN;
} else if (height < 300000) {
return 125000 * COIN;
} else if (height < 400000) {
return 62500 * COIN;
} else if (height < 500000) {
return 31250 * COIN;
} else if (height < 600000) {
return 15625 * COIN;
} else {
return 10000 * COIN;
}
}
BOOST_AUTO_TEST_CASE(subsidy_first_100k_test)
{
const CChainParams& mainParams = Params(CBaseChainParams::MAIN);
CAmount nSum = 0;
arith_uint256 prevHash = UintToArith256(uint256S("0"));
for (int nHeight = 0; nHeight <= 100000; nHeight++) {
const Consensus::Params& params = mainParams.GetConsensus(nHeight);
CAmount nSubsidy = GetDiscoinBlockSubsidy(nHeight, params, ArithToUint256(prevHash));
BOOST_CHECK(MoneyRange(nSubsidy));
BOOST_CHECK(nSubsidy <= 1000000 * COIN);
nSum += nSubsidy;
// Use nSubsidy to give us some variation in previous block hash, without requiring full block templates
prevHash += nSubsidy;
}
const CAmount expected = 54894174438 * COIN;
BOOST_CHECK_EQUAL(expected, nSum);
}
BOOST_AUTO_TEST_CASE(subsidy_100k_145k_test)
{
const CChainParams& mainParams = Params(CBaseChainParams::MAIN);
CAmount nSum = 0;
arith_uint256 prevHash = UintToArith256(uint256S("0"));
for (int nHeight = 100000; nHeight <= 145000; nHeight++) {
const Consensus::Params& params = mainParams.GetConsensus(nHeight);
CAmount nSubsidy = GetDiscoinBlockSubsidy(nHeight, params, ArithToUint256(prevHash));
BOOST_CHECK(MoneyRange(nSubsidy));
BOOST_CHECK(nSubsidy <= 500000 * COIN);
nSum += nSubsidy;
// Use nSubsidy to give us some variation in previous block hash, without requiring full block templates
prevHash += nSubsidy;
}
const CAmount expected = 12349960000 * COIN;
BOOST_CHECK_EQUAL(expected, nSum);
}
// Check the simplified rewards after block 145,000
BOOST_AUTO_TEST_CASE(subsidy_post_145k_test)
{
const CChainParams& mainParams = Params(CBaseChainParams::MAIN);
const uint256 prevHash = uint256S("0");
for (int nHeight = 145000; nHeight < 600000; nHeight++) {
const Consensus::Params& params = mainParams.GetConsensus(nHeight);
CAmount nSubsidy = GetDiscoinBlockSubsidy(nHeight, params, prevHash);
CAmount nExpectedSubsidy = (500000 >> (nHeight / 100000)) * COIN;
BOOST_CHECK(MoneyRange(nSubsidy));
BOOST_CHECK_EQUAL(nSubsidy, nExpectedSubsidy);
}
// Test reward at 600k+ is constant
CAmount nConstantSubsidy = GetDiscoinBlockSubsidy(600000, mainParams.GetConsensus(600000), prevHash);
BOOST_CHECK_EQUAL(nConstantSubsidy, 10000 * COIN);
nConstantSubsidy = GetDiscoinBlockSubsidy(700000, mainParams.GetConsensus(700000), prevHash);
BOOST_CHECK_EQUAL(nConstantSubsidy, 10000 * COIN);
}
BOOST_AUTO_TEST_CASE(get_next_work_difficulty_limit)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(0);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1386474927; // Block # 1
pindexLast.nHeight = 239;
pindexLast.nTime = 1386475638; // Block #239
pindexLast.nBits = 0x1e0ffff0;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1e00ffff);
}
BOOST_AUTO_TEST_CASE(get_next_work_pre_digishield)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(0);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1386942008; // Block 9359
pindexLast.nHeight = 9599;
pindexLast.nTime = 1386954113;
pindexLast.nBits = 0x1c1a1206;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c15ea59);
}
BOOST_AUTO_TEST_CASE(get_next_work_digishield)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(145000);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1395094427;
// First hard-fork at 145,000, which applies to block 145,001 onwards
pindexLast.nHeight = 145000;
pindexLast.nTime = 1395094679;
pindexLast.nBits = 0x1b499dfd;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1b671062);
}
BOOST_AUTO_TEST_CASE(get_next_work_digishield_modulated_upper)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(145000);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1395100835;
// Test the upper bound on modulated time using mainnet block #145,107
pindexLast.nHeight = 145107;
pindexLast.nTime = 1395101360;
pindexLast.nBits = 0x1b3439cd;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1b4e56b3);
}
BOOST_AUTO_TEST_CASE(get_next_work_digishield_modulated_lower)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(145000);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1395380517;
// Test the lower bound on modulated time using mainnet block #149,423
pindexLast.nHeight = 149423;
pindexLast.nTime = 1395380447;
pindexLast.nBits = 0x1b446f21;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1b335358);
}
BOOST_AUTO_TEST_CASE(get_next_work_digishield_rounding)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus(145000);
CBlockIndex pindexLast;
int64_t nLastRetargetTime = 1395094679;
// Test case for correct rounding of modulated time - this depends on
// handling of integer division, and is not obvious from the code
pindexLast.nHeight = 145001;
pindexLast.nTime = 1395094727;
pindexLast.nBits = 0x1b671062;
BOOST_CHECK_EQUAL(CalculateDiscoinNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1b6558a4);
}
BOOST_AUTO_TEST_CASE(hardfork_parameters)
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& initialParams = Params().GetConsensus(0);
BOOST_CHECK_EQUAL(initialParams.nPowTargetTimespan, 14400);
BOOST_CHECK_EQUAL(initialParams.fAllowLegacyBlocks, true);
BOOST_CHECK_EQUAL(initialParams.fDigishieldDifficultyCalculation, false);
const Consensus::Params& initialParamsEnd = Params().GetConsensus(144999);
BOOST_CHECK_EQUAL(initialParamsEnd.nPowTargetTimespan, 14400);
BOOST_CHECK_EQUAL(initialParamsEnd.fAllowLegacyBlocks, true);
BOOST_CHECK_EQUAL(initialParamsEnd.fDigishieldDifficultyCalculation, false);
const Consensus::Params& digishieldParams = Params().GetConsensus(145000);
BOOST_CHECK_EQUAL(digishieldParams.nPowTargetTimespan, 60);
BOOST_CHECK_EQUAL(digishieldParams.fAllowLegacyBlocks, true);
BOOST_CHECK_EQUAL(digishieldParams.fDigishieldDifficultyCalculation, true);
const Consensus::Params& digishieldParamsEnd = Params().GetConsensus(371336);
BOOST_CHECK_EQUAL(digishieldParamsEnd.nPowTargetTimespan, 60);
BOOST_CHECK_EQUAL(digishieldParamsEnd.fAllowLegacyBlocks, true);
BOOST_CHECK_EQUAL(digishieldParamsEnd.fDigishieldDifficultyCalculation, true);
const Consensus::Params& auxpowParams = Params().GetConsensus(371337);
BOOST_CHECK_EQUAL(auxpowParams.nHeightEffective, 371337);
BOOST_CHECK_EQUAL(auxpowParams.nPowTargetTimespan, 60);
BOOST_CHECK_EQUAL(auxpowParams.fAllowLegacyBlocks, false);
BOOST_CHECK_EQUAL(auxpowParams.fDigishieldDifficultyCalculation, true);
const Consensus::Params& auxpowHighParams = Params().GetConsensus(700000); // Arbitrary point after last hard-fork
BOOST_CHECK_EQUAL(auxpowHighParams.nPowTargetTimespan, 60);
BOOST_CHECK_EQUAL(auxpowHighParams.fAllowLegacyBlocks, false);
BOOST_CHECK_EQUAL(auxpowHighParams.fDigishieldDifficultyCalculation, true);
}
BOOST_AUTO_TEST_SUITE_END()
|