forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
test_pivx.cpp
238 lines (203 loc) · 8.28 KB
/
test_pivx.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
236
237
238
// Copyright (c) 2011-2013 The Bitcoin Core developers
// Copyright (c) 2017-2020 The PIVX developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#define BOOST_TEST_MODULE Pivx Test Suite
#include "test/test_pivx.h"
#include "blockassembler.h"
#include "consensus/merkle.h"
#include "guiinterface.h"
#include "evo/deterministicmns.h"
#include "evo/evodb.h"
#include "evo/evonotificationinterface.h"
#include "miner.h"
#include "net_processing.h"
#include "rpc/server.h"
#include "rpc/register.h"
#include "script/sigcache.h"
#include "sporkdb.h"
#include "txmempool.h"
#include "txdb.h"
#include "validation.h"
#include <boost/test/unit_test.hpp>
std::unique_ptr<CConnman> g_connman;
CClientUIInterface uiInterface; // Declared but not defined in guiinterface.h
FastRandomContext insecure_rand_ctx;
extern bool fPrintToConsole;
extern void noui_connect();
std::ostream& operator<<(std::ostream& os, const uint256& num)
{
os << num.ToString();
return os;
}
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
: m_path_root(fs::temp_directory_path() / "test_pivx" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
{
ECC_Start();
SetupEnvironment();
InitSignatureCache();
fCheckBlockIndex = true;
SelectParams(chainName);
evoDb.reset(new CEvoDB(1 << 20, true, true));
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
}
BasicTestingSetup::~BasicTestingSetup()
{
fs::remove_all(m_path_root);
ECC_Stop();
deterministicMNManager.reset();
evoDb.reset();
}
fs::path BasicTestingSetup::SetDataDir(const std::string& name)
{
fs::path ret = m_path_root / name;
fs::create_directories(ret);
gArgs.ForceSetArg("-datadir", ret.string());
return ret;
}
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
{
SetDataDir("tempdir");
ClearDatadirCache();
// Start the lightweight task scheduler thread
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler);
threadGroup.create_thread(std::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));
// Note that because we don't bother running a scheduler thread here,
// callbacks via CValidationInterface are unreliable, but that's OK,
// our unit tests aren't testing multiple parts of the code at once.
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
// Register EvoNotificationInterface
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
connman = g_connman.get();
pEvoNotificationInterface = new EvoNotificationInterface(*connman);
RegisterValidationInterface(pEvoNotificationInterface);
// Ideally we'd move all the RPC tests to the functional testing framework
// instead of unit tests, but for now we need these here.
RegisterAllCoreRPCCommands(tableRPC);
zerocoinDB.reset(new CZerocoinDB(0, true));
pSporkDB.reset(new CSporkDB(0, true));
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
if (!LoadGenesisBlock()) {
throw std::runtime_error("Error initializing block database");
}
{
CValidationState state;
bool ok = ActivateBestChain(state);
BOOST_CHECK(ok);
}
nScriptCheckThreads = 3;
for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck);
peerLogic.reset(new PeerLogicValidation(connman));
}
TestingSetup::~TestingSetup()
{
threadGroup.interrupt_all();
threadGroup.join_all();
GetMainSignals().FlushBackgroundCallbacks();
UnregisterAllValidationInterfaces();
GetMainSignals().UnregisterBackgroundSignalScheduler();
g_connman.reset();
peerLogic.reset();
UnloadBlockIndex();
delete pEvoNotificationInterface;
pcoinsTip.reset();
pcoinsdbview.reset();
pblocktree.reset();
zerocoinDB.reset();
pSporkDB.reset();
}
// Test chain only available on regtest
TestChainSetup::TestChainSetup(int blockCount) : TestingSetup(CBaseChainParams::REGTEST)
{
// if blockCount is over PoS start, delay it to 100 blocks after.
if (blockCount > Params().GetConsensus().vUpgrades[Consensus::UPGRADE_POS].nActivationHeight) {
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_POS, blockCount + 100);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V3_4, blockCount + 101);
}
// Generate a blockCount-block chain:
coinbaseKey.MakeNewKey(true);
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
for (int i = 0; i < blockCount; i++)
{
std::vector<CMutableTransaction> noTxns;
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
coinbaseTxns.push_back(*b.vtx[0]);
}
}
// Create a new block with coinbase paying to scriptPubKey, and try to add it to the current chain.
// Include given transactions, and, if fNoMempoolTx=true, remove transactions coming from the mempool.
CBlock TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool fNoMempoolTx)
{
CBlock block = CreateBlock(txns, scriptPubKey, fNoMempoolTx);
ProcessNewBlock(std::make_shared<const CBlock>(block), nullptr);
return block;
}
CBlock TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey)
{
CScript scriptPubKey = CScript() << ToByteVector(scriptKey.GetPubKey()) << OP_CHECKSIG;
return CreateAndProcessBlock(txns, scriptPubKey);
}
CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey,
bool fNoMempoolTx,
bool fTestBlockValidity)
{
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(
Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey,
nullptr, // wallet
false, // fProofOfStake
nullptr, // availableCoins
fNoMempoolTx,
fTestBlockValidity);
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(pblocktemplate->block);
// Add passed-in txns:
for (const CMutableTransaction& tx : txns) {
pblock->vtx.push_back(MakeTransactionRef(tx));
}
const int nHeight = WITH_LOCK(cs_main, return chainActive.Height()) + 1;
// Re-compute sapling root
pblock->hashFinalSaplingRoot = CalculateSaplingTreeRoot(pblock.get(), nHeight, Params());
// Find valid PoW
assert(SolveBlock(pblock, nHeight));
return *pblock;
}
CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey,
bool fTestBlockValidity)
{
CScript scriptPubKey = CScript() << ToByteVector(scriptKey.GetPubKey()) << OP_CHECKSIG;
return CreateBlock(txns, scriptPubKey, fTestBlockValidity);
}
std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
{
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits)) { ++(pblock->nNonce); }
return pblock;
}
TestChainSetup::~TestChainSetup()
{
}
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx)
{
CTransaction txn(tx);
return FromTx(txn);
}
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransaction& txn)
{
return CTxMemPoolEntry(MakeTransactionRef(txn), nFee, nTime, nHeight,
spendsCoinbaseOrCoinstake, sigOpCount);
}
[[noreturn]] void Shutdown(void* parg)
{
std::exit(0);
}
[[noreturn]] void StartShutdown()
{
std::exit(0);
}
bool ShutdownRequested()
{
return false;
}