forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdip1fork_tests.cpp
235 lines (211 loc) · 12.1 KB
/
dip1fork_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
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
#include <chainparams.h>
#include <masternodes/masternodes.h>
#include <validation.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(dip1fork_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(blockreward_dfip1)
{
const CScript SCRIPT_PUB{CScript(OP_TRUE)};
const Consensus::Params & consensus = Params().GetConsensus();
auto height = consensus.AMKHeight;
CMutableTransaction coinbaseTx{};
coinbaseTx.vin.resize(1);
coinbaseTx.vin[0].prevout.SetNull();
coinbaseTx.vout.resize(1);
coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
coinbaseTx.vout[0].nValue = GetBlockSubsidy(height, consensus);
coinbaseTx.vin[0].scriptSig = CScript() << height << OP_0;
{ // check on pre-AMK height:
CCustomCSView mnview(*pcustomcsview.get());
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(coinbaseTx), height-1, 0, consensus);
BOOST_CHECK(res.ok);
}
{ // try to generate wrong tokens
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout[0].nTokenId.v = 1;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-wrong-tokens");
}
{ // do not pay foundation reward at all
CCustomCSView mnview(*pcustomcsview.get());
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(coinbaseTx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-foundation-reward");
}
{ // try to pay foundation reward slightly less than expected
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = GetBlockSubsidy(height, consensus) * consensus.foundationShareDFIP1 / COIN -1;
tx.vout[0].nValue -= tx.vout[1].nValue;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-foundation-reward");
}
{ // pay proper foundation reward, but not subtract consensus share
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = GetBlockSubsidy(height, consensus) * consensus.foundationShareDFIP1 / COIN;
tx.vout[0].nValue -= tx.vout[1].nValue;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-amount");
}
{ // at least, all is ok
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
CAmount const baseSubsidy = GetBlockSubsidy(height, consensus);
tx.vout[1].nValue = baseSubsidy * consensus.foundationShareDFIP1 / COIN;
tx.vout[0].nValue -= tx.vout[1].nValue;
tx.vout[0].nValue -= baseSubsidy * consensus.nonUtxoBlockSubsidies.at(CommunityAccountType::IncentiveFunding) / COIN;
tx.vout[0].nValue -= baseSubsidy * consensus.nonUtxoBlockSubsidies.at(CommunityAccountType::AnchorReward) / COIN;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(res.ok);
}
}
BOOST_AUTO_TEST_CASE(blockreward_dfip8)
{
const CScript SCRIPT_PUB{CScript(OP_TRUE)};
Consensus::Params consensus = Params().GetConsensus();
consensus.EunosHeight = 10000000;
auto height = consensus.EunosHeight;
CAmount blockReward = GetBlockSubsidy(height, consensus);
CMutableTransaction coinbaseTx{};
coinbaseTx.vin.resize(1);
coinbaseTx.vin[0].prevout.SetNull();
coinbaseTx.vout.resize(1);
coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
coinbaseTx.vout[0].nValue = GetBlockSubsidy(height, consensus);
coinbaseTx.vin[0].scriptSig = CScript() << height << OP_0;
{ // do not pay foundation reward at all
CCustomCSView mnview(*pcustomcsview.get());
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(coinbaseTx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-foundation-reward");
}
{ // try to pay foundation reward slightly less than expected
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[0].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.masternode);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.community) - 1;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-foundation-reward");
}
{ // try to pay foundation reward slightly more than expected
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[0].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.masternode);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.community) + 1;
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-amount");
}
{ // Try and pay staker too much
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[0].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.masternode) + 1;
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.community);
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(!res.ok && res.dbgMsg == "bad-cb-amount");
}
{ // Test everything is okay
CCustomCSView mnview(*pcustomcsview.get());
CMutableTransaction tx(coinbaseTx);
tx.vout.resize(2);
tx.vout[0].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.masternode);
tx.vout[1].scriptPubKey = consensus.foundationShareScript;
tx.vout[1].nValue = CalculateCoinbaseReward(blockReward, consensus.dist.community);
Res res = ApplyGeneralCoinbaseTx(mnview, CTransaction(tx), height, 0, consensus);
BOOST_CHECK(res.ok);
}
}
BOOST_AUTO_TEST_CASE(blockreward_dfip8_reductions)
{
Consensus::Params consensus = Params().GetConsensus();
consensus.EunosHeight = 10000000;
auto GetReductionsHeight = [consensus](const uint32_t reductions)
{
return consensus.EunosHeight + (reductions * Params().GetConsensus().emissionReductionPeriod);
};
// Test coinbase rewards reduction 0
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(0), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 40504000000);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 13499983200);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 1988746400);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 8100800);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 10308268000);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 9996387200);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 4001795200);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 700719200);
}
// Test coinbase rewards reduction 1
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(1), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 39832443680);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 13276153478);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 1955772984);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 7966488);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 10137356916);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 9830647100);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 3935445435);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 689101275);
}
// Test coinbase rewards reduction 100
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(100), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 7610296073);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 2536511681);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 373665537);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 1522059);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 1936820350);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 1878221070);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 751897252);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 131658122);
}
// Test coinbase rewards reduction 1000
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(1000), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 2250);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 749);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 110);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 572);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 555);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 222);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 38);
}
// Test coinbase rewards reduction 1251
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(1251), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 60);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 19);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 2);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 15);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 14);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 5);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 1);
}
// Test coinbase rewards reduction 1252
{
auto blockSubsidy = GetBlockSubsidy(GetReductionsHeight(1252), consensus);
BOOST_CHECK_EQUAL(blockSubsidy, 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.masternode), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.community), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.anchor), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.liquidity), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.loan), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.options), 0);
BOOST_CHECK_EQUAL(CalculateCoinbaseReward(blockSubsidy, consensus.dist.unallocated), 0);
}
}
BOOST_AUTO_TEST_SUITE_END()