forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet_test_fixture.cpp
More file actions
117 lines (99 loc) · 3.87 KB
/
Copy pathwallet_test_fixture.cpp
File metadata and controls
117 lines (99 loc) · 3.87 KB
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
// Copyright (c) 2016-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <wallet/test/wallet_test_fixture.h>
#include <injector.h>
#include <rpc/server.h>
#include <validation.h>
#include <wallet/db.h>
#include <wallet/rpcvalidator.h>
#include <wallet/rpcwalletext.h>
#include <consensus/merkle.h>
#include <test/test_unite_mocks.h>
WalletTestingSetup::WalletTestingSetup(const std::string& chainName)
: WalletTestingSetup([](Settings& s){}, chainName) {}
WalletTestingSetup::WalletTestingSetup(std::function<void(Settings&)> f, const std::string& chainName)
: TestingSetup(chainName)
{
bitdb.MakeMock();
bool fFirstRun;
g_address_type = OUTPUT_TYPE_DEFAULT;
g_change_type = OUTPUT_TYPE_DEFAULT;
std::unique_ptr<CWalletDBWrapper> dbw(new CWalletDBWrapper(&bitdb, "wallet_test.dat"));
f(settings);
esperanza::WalletExtensionDeps deps(&settings, &stake_validator_mock);
pwalletMain = MakeUnique<CWallet>(deps, std::move(dbw));
pwalletMain->LoadWallet(fFirstRun);
vpwallets.insert(vpwallets.begin(), &*pwalletMain);
RegisterValidationInterface(pwalletMain.get());
RegisterWalletRPCCommands(tableRPC);
RegisterValidatorRPCCommands(tableRPC);
RegisterWalletextRPCCommands(tableRPC);
}
WalletTestingSetup::~WalletTestingSetup()
{
UnregisterValidationInterface(pwalletMain.get());
vpwallets.clear();
bitdb.Flush(true);
bitdb.Reset();
}
TestChain100Setup::TestChain100Setup() : WalletTestingSetup(CBaseChainParams::REGTEST)
{
CUnitESecret vchSecret;
bool fGood = vchSecret.SetString("cQTjnbHifWGuMhm9cRgQ23ip5KntTMfj3zwo6iQyxMVxSfJyptqL");
assert(fGood);
coinbaseKey = vchSecret.GetKey();
{
LOCK(pwalletMain->cs_wallet);
assert(pwalletMain->AddKey(coinbaseKey));
}
WalletRescanReserver reserver(pwalletMain.get());
reserver.reserve();
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver);
// Generate a 100-block chain:
GetComponent<Settings>()->stake_split_threshold = 0; // reset to 0
CScript script_pubkey = GetScriptForDestination(WitnessV0KeyHash(coinbaseKey.GetPubKey().GetID()));
for (int i = 0; i < COINBASE_MATURITY; i++)
{
std::vector<CMutableTransaction> noTxns;
CBlock b = CreateAndProcessBlock(noTxns, script_pubkey);
coinbaseTxns.push_back(*b.vtx[0]);
auto x = b.GetHash().ToString();
assert(!x.empty());
}
}
//
// Create a new block with just given transactions, coinbase paying to
// scriptPubKey, and try to add it to the current chain.
//
CBlock
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool *processed)
{
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
unsigned int extraNonce = 0;
{
LOCK(cs_main);
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
}
// Regenerate the merkle roots cause we possibly changed the txs included
block.ComputeMerkleTrees();
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
const bool was_processed = ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
assert(processed != nullptr || was_processed);
if (processed != nullptr) {
*processed = was_processed;
}
SyncWithValidationInterfaceQueue(); // To prevent Wallet::ConnectBlock from running concurrently
CBlock result = block;
return result;
}
TestChain100Setup::~TestChain100Setup()
{
}