forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathhdwallet_test_fixture.cpp
159 lines (134 loc) · 5.25 KB
/
hdwallet_test_fixture.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
// Copyright (c) 2017-2023 The Particl 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/hdwallet_test_fixture.h>
#include <blind.h>
#include <node/miner.h>
#include <pos/miner.h>
#include <rpc/server.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/db.h>
#include <wallet/hdwallet.h>
#include <wallet/rpc/wallet.h>
#include <wallet/test/util.h>
#include <boost/test/unit_test.hpp>
HDWalletTestingSetup::HDWalletTestingSetup(const ChainType chain_type):
TestingSetup(chain_type, { "-balancesindex" }, true, true, true /* fParticlMode */),
m_wallet_loader{interfaces::MakeWalletLoader(*m_node.chain, *Assert(m_node.args))}
{
pwalletMain = std::make_shared<CHDWallet>(m_node.chain.get(), "", CreateMockWalletDatabaseBDB());
pwalletMain->m_warn_no_active_acc = false;
WalletContext& wallet_context = *m_wallet_loader->context();
AddWallet(wallet_context, pwalletMain);
pwalletMain->LoadWallet();
m_chain_notifications_handler = m_node.chain->handleNotifications({ pwalletMain.get(), [](CHDWallet*) {} });
m_wallet_loader->registerRpcs();
}
HDWalletTestingSetup::~HDWalletTestingSetup()
{
WalletContext& wallet_context = *m_wallet_loader->context();
RemoveWallet(wallet_context, pwalletMain, std::nullopt);
pwalletMain->Finalise();
pwalletMain.reset();
particl::mapStakeSeen.clear();
particl::listStakeSeen.clear();
}
void StakeNBlocks(CHDWallet *pwallet, size_t nBlocks)
{
ChainstateManager *pchainman{nullptr};
if (pwallet->HaveChain()) {
pchainman = pwallet->chain().getChainman();
}
if (!pchainman) {
LogPrintf("Error: Chainstate manager not found.\n");
return;
}
size_t nStaked = 0;
size_t k, nTries = 10000;
for (k = 0; k < nTries; ++k) {
int nBestHeight = WITH_LOCK(cs_main, return pchainman->ActiveChain().Height());
int64_t nSearchTime = GetTime() & ~Params().GetStakeTimestampMask(nBestHeight+1);
if (nSearchTime <= pwallet->nLastCoinStakeSearchTime) {
std::this_thread::sleep_for(std::chrono::milliseconds(250));
continue;
}
std::unique_ptr<node::CBlockTemplate> pblocktemplate = pwallet->CreateNewBlock();
BOOST_REQUIRE(pblocktemplate.get());
if (pwallet->SignBlock(pblocktemplate.get(), nBestHeight+1, nSearchTime)) {
CBlock *pblock = &pblocktemplate->block;
if (CheckStake(*pchainman, pblock)) {
nStaked++;
}
}
if (nStaked >= nBlocks) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
BOOST_REQUIRE(k < nTries);
if (pchainman->m_options.signals) {
pchainman->m_options.signals->SyncWithValidationInterfaceQueue();
}
}
bool CreateValidBlock(CHDWallet *pwallet, CBlock &block_out)
{
ChainstateManager *pchainman{nullptr};
if (pwallet->HaveChain()) {
pchainman = pwallet->chain().getChainman();
}
if (!pchainman) {
LogPrintf("Error: Chainstate manager not found.\n");
return false;
}
size_t k, nTries = 10000;
for (k = 0; k < nTries; ++k) {
int nBestHeight = WITH_LOCK(cs_main, return pchainman->ActiveChain().Height());
int64_t nSearchTime = GetTime() & ~Params().GetStakeTimestampMask(nBestHeight+1);
if (nSearchTime <= pwallet->nLastCoinStakeSearchTime) {
std::this_thread::sleep_for(std::chrono::milliseconds(250));
continue;
}
std::unique_ptr<node::CBlockTemplate> pblocktemplate = pwallet->CreateNewBlock();
BOOST_REQUIRE(pblocktemplate.get());
if (pwallet->SignBlock(pblocktemplate.get(), nBestHeight + 1, nSearchTime)) {
block_out = pblocktemplate->block;
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
return false;
}
uint256 AddTxn(CHDWallet *pwallet, CTxDestination &dest, OutputTypes input_type, OutputTypes output_type, CAmount amount, CAmount exploit_amount, std::string expect_error)
{
uint256 txid;
BOOST_REQUIRE(IsValidDestination(dest));
{
LOCK(pwallet->cs_wallet);
std::string sError;
std::vector<CTempRecipient> vecSend;
vecSend.emplace_back(output_type, amount, dest);
CTransactionRef tx_new;
CWalletTx wtx(tx_new, TxStateInactive{});
CTransactionRecord rtx;
CAmount nFee;
CCoinControl coinControl;
coinControl.m_debug_exploit_anon = exploit_amount;
int rv = input_type == OUTPUT_RINGCT ?
pwallet->AddAnonInputs(wtx, rtx, vecSend, true, 3, 1, nFee, &coinControl, sError) :
input_type == OUTPUT_CT ?
pwallet->AddBlindedInputs(wtx, rtx, vecSend, true, nFee, &coinControl, sError) :
pwallet->AddStandardInputs(wtx, rtx, vecSend, true, nFee, &coinControl, sError);
BOOST_REQUIRE(rv == 0);
rv = pwallet->SubmitTxMemoryPoolAndRelay(wtx, sError, true);
if (expect_error.empty()) {
BOOST_REQUIRE(rv == 1);
} else {
BOOST_CHECK(sError == expect_error);
BOOST_REQUIRE(rv == 0);
}
txid = wtx.GetHash();
} // cs_wallet
pwallet->SyncWithValidationInterfaceQueue();
return txid;
}