-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathstaking_tests.h
304 lines (270 loc) · 11.5 KB
/
staking_tests.h
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2019 The Blocknet developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BLOCKNET_TEST_STAKING_TESTS_H
#define BLOCKNET_TEST_STAKING_TESTS_H
#include <test/test_bitcoin.h>
#define protected public // for overridding protected fields in CChainParams
#include <chainparams.h>
#undef protected
#include <governance/governance.h>
#include <index/txindex.h>
#include <kernel.h>
#include <miner.h>
#include <policy/policy.h>
#include <pow.h>
#include <rpc/server.h>
#include <stakemgr.h>
#include <timedata.h>
#include <univalue.h>
#include <validation.h>
#include <wallet/wallet.h>
#include <boost/test/unit_test.hpp>
static UniValue CallRPC2(const std::string & strMethod, const UniValue & params) {
JSONRPCRequest request;
request.strMethod = strMethod;
request.params = params;
request.fHelp = false;
rpcfn_type method = tableRPC[strMethod]->actor;
try {
UniValue result = (*method)(request);
return result;
}
catch (const UniValue& objError) {
throw std::runtime_error(find_value(objError, "message").get_str());
}
}
static void AddKey(CWallet & wallet, const CKey & key) {
LOCK(wallet.cs_wallet);
wallet.AddKeyPubKey(key, key.GetPubKey());
}
static void rescanWallet(CWallet *w) {
WalletRescanReserver reserver(w);
reserver.reserve();
w->ScanForWalletTransactions(chainActive.Genesis()->GetBlockHash(), {}, reserver, true);
}
static void removeGovernanceDBFiles() {
try {
fs::remove_all(GetDataDir() / "indexes" / "governance");
} catch (...) {}
}
bool sendToAddress(CWallet *wallet, const CTxDestination & dest, const CAmount & amount, CTransactionRef & tx);
/**
* Proof-of-Stake test chain.
*/
struct TestChainPoSData {
std::string coinbase;
std::vector<CBlock> blocks;
int64_t mockTimeAfter;
explicit TestChainPoSData() {
CKey key;
key.MakeNewKey(true);
coinbase = EncodeSecret(key);
}
};
extern std::map<std::string, std::shared_ptr<TestChainPoSData>> g_CachedTestChainPoS;
struct TestChainPoS : public TestingSetup {
explicit TestChainPoS(const bool & init = true) : TestingSetup(CBaseChainParams::REGTEST) {
if (init)
Init("default"); // use default cached chain
}
/**
* A cached version of the named chain will be used if available. First time a name
* is used to create a sample chain, the chain is cached for use in other unit tests.
* Specifying an empty string will not cache the chain.
* @param name
* @return
*/
std::shared_ptr<TestChainPoSData> Init(const std::string & name="") {
gov::Governance::instance().reset();
if (!name.empty() && g_CachedTestChainPoS.count(name) && !g_CachedTestChainPoS[name]->blocks.empty()) {
auto cachedChain = g_CachedTestChainPoS[name];
coinbaseKey = DecodeSecret(cachedChain->coinbase);
for (const auto & block : cachedChain->blocks) {
auto blockptr = std::make_shared<const CBlock>(block);
SetMockTime(blockptr->GetBlockTime());
ProcessNewBlock(Params(), blockptr, true, nullptr);
m_coinbase_txns.push_back(blockptr->vtx[0]);
}
SetMockTime(cachedChain->mockTimeAfter);
ReloadWallet();
// Turn on index for staking
g_txindex = MakeUnique<TxIndex>(1 << 20, true);
g_txindex->Start();
g_txindex->Sync();
// Stake some blocks, use while loop to force fee calcs
StakeBlocks(5); SyncWithValidationInterfaceQueue();
return cachedChain;
}
auto cachedChain = std::make_shared<TestChainPoSData>();
// set coin maturity to something small to help staking tests
coinbaseKey.MakeNewKey(true);
cachedChain->coinbase = EncodeSecret(coinbaseKey);
CBasicKeyStore keystore; // temp used to spend coinbases
keystore.AddKey(coinbaseKey);
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
for (int i = 0; i < Params().GetConsensus().lastPOWBlock; ++i) {
SetMockTime(GetAdjustedTime() + Params().GetConsensus().nPowTargetSpacing);
std::vector<CMutableTransaction> txs;
if (i > Params().GetConsensus().coinMaturity) {
int j = i - Params().GetConsensus().coinMaturity;
auto tx = m_coinbase_txns[j];
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0] = CTxIn(COutPoint(tx->GetHash(), 0));
mtx.vout.resize(2);
for (int k = 0; k < (int)mtx.vout.size(); ++k) {
mtx.vout[k].scriptPubKey = scriptPubKey;
mtx.vout[k].nValue = (tx->GetValueOut()/mtx.vout.size()) - CENT;
}
const CTransaction txConst(mtx);
SignatureData sigdata = DataFromTransaction(mtx, 0, tx->vout[0]);
ProduceSignature(keystore, MutableTransactionSignatureCreator(&mtx, 0, mtx.vout[0].nValue, SIGHASH_ALL), tx->vout[0].scriptPubKey, sigdata);
UpdateInput(mtx.vin[0], sigdata);
txs.push_back(mtx);
}
CBlock b = CreateAndProcessBlock(txs, scriptPubKey);
cachedChain->blocks.push_back(b);
m_coinbase_txns.push_back(b.vtx[0]);
}
cachedChain->mockTimeAfter = GetAdjustedTime();
if (!name.empty())
g_CachedTestChainPoS[name] = cachedChain;
ReloadWallet();
// Turn on index for staking
g_txindex = MakeUnique<TxIndex>(1 << 20, true);
g_txindex->Start();
g_txindex->Sync();
// Stake some blocks
StakeBlocks(5);
return cachedChain;
}
void ReloadWallet() {
if (wallet) {
UnregisterValidationInterface(wallet.get());
RemoveWallet(wallet);
wallet.reset();
}
bool firstRun;
wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateMock());
AddWallet(wallet); // add wallet to global mgr
wallet->LoadWallet(firstRun);
AddKey(*wallet, coinbaseKey);
{
WalletRescanReserver reserver(wallet.get());
reserver.reserve();
wallet->ScanForWalletTransactions(chainActive.Genesis()->GetBlockHash(), {}, reserver, true);
}
wallet->SetBroadcastTransactions(true);
RegisterValidationInterface(wallet.get());
}
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) {
const CChainParams& chainparams = Params();
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
CBlock& block = pblocktemplate->block;
// Replace mempool-selected txns with just coinbase plus passed-in txns:
block.vtx.resize(1);
for (const CMutableTransaction& tx : txns)
block.vtx.push_back(MakeTransactionRef(tx));
// IncrementExtraNonce creates a valid coinbase and merkleRoot
{
LOCK(cs_main);
unsigned int extraNonce = 0;
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
}
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
return block;
}
void StakeBlocks(const int blockCount) {
int tries{0};
const int currentBlockHeight = chainActive.Height();
while (chainActive.Height() < currentBlockHeight + blockCount) {
CBlockIndex *pindex = nullptr;
try {
{
LOCK(cs_main);
pindex = chainActive.Tip();
}
std::vector<std::shared_ptr<CWallet>> wallets{wallet};
if (pindex && staker.Update(wallets, pindex, Params().GetConsensus(), true) && staker.TryStake(pindex, Params())) {
LOCK(cs_main);
auto lc = wallet->chain().lock();
WalletRescanReserver reserver(wallet.get());
reserver.reserve();
wallet->ScanForWalletTransactions(lc->getBlockHash(chainActive.Height()), {}, reserver, true);
}
} catch (std::exception & e) {
LogPrintf("Staker ran into an exception: %s\n", e.what());
throw e;
} catch (...) {
throw std::runtime_error("Staker unknown error");
}
if (++tries > 1000)
throw std::runtime_error("Staker failed to find stake");
auto stime = staker.LastUpdateTime();
if (stime == 0)
stime = GetAdjustedTime();
SetMockTime(stime + Params().GetConsensus().PoSFutureBlockTimeLimit(pindex->GetBlockTime()));
}
}
StakeMgr::StakeCoin FindStake() {
const int currentBlockHeight = chainActive.Height();
CBlockIndex *tip = nullptr;
{
LOCK(cs_main);
tip = chainActive.Tip();
}
int tries{0};
const CChainParams & params = Params();
while (true) {
try {
std::vector<std::shared_ptr<CWallet>> wallets{wallet};
if (staker.Update(wallets, tip, params.GetConsensus(), true)) {
std::vector<StakeMgr::StakeCoin> nextStakes;
if (!staker.NextStake(nextStakes, tip, params))
continue;
for (const auto & ns : nextStakes) {
auto blocktemplate = BlockAssembler(params).CreateNewBlockPoS(*ns.coin, ns.hashBlock, ns.time, ns.blockTime, ns.wallet.get(), true);
uint256 haspos;
if (CheckProofOfStake(blocktemplate->block, tip, haspos, params.GetConsensus()))
return ns;
}
}
} catch (std::exception & e) {
LogPrintf("Staker ran into an exception: %s\n", e.what());
} catch (...) { }
if (++tries > 1000)
throw std::runtime_error("Staker failed to find stake");
auto stime = staker.LastUpdateTime();
if (stime == 0)
stime = GetAdjustedTime();
SetMockTime(stime + params.GetConsensus().PoSFutureBlockTimeLimit(tip->GetBlockTime()));
}
}
~TestChainPoS() {
if (g_txindex) {
g_txindex->Stop();
g_txindex.reset();
}
if (wallet) {
UnregisterValidationInterface(wallet.get());
RemoveWallet(wallet);
wallet.reset();
}
m_coinbase_txns.clear();
locked_chain.reset();
chain.reset();
// clean governance db
removeGovernanceDBFiles();
gov::Governance::instance().reset();
};
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain();
std::unique_ptr<interfaces::Chain::Lock> locked_chain = chain->assumeLocked(); // Temporary. Removed in upcoming lock cleanup
std::shared_ptr<CWallet> wallet;
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
StakeMgr staker;
};
#endif //BLOCKNET_TEST_STAKING_TESTS_H