Skip to content

Commit 19a0a21

Browse files
presstabFuzzbawls
authored andcommitted
Add multithreading to RPC searchdzpiv.
1 parent 7323ecb commit 19a0a21

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

src/mintpool.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void CMintPool::Add(const pair<uint256, uint32_t>& pMint)
3131
insert(pMint);
3232
if (pMint.second > nCountLastGenerated)
3333
nCountLastGenerated = pMint.second;
34+
35+
LogPrintf("%s : add %s count %d to mint pool\n", __func__, pMint.first.GetHex().substr(0, 6), pMint.second);
3436
}
3537

3638
bool CMintPool::Has(const CBigNum& bnValue)

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
139139
{"generatemintlist", 1},
140140
{"searchdzpiv", 0},
141141
{"searchdzpiv", 1},
142+
{"searchdzpiv", 2},
142143
{"getfeeinfo", 0}
143144
};
144145

src/rpcwallet.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "spork.h"
2525
#include "primitives/deterministicmint.h"
2626
#include <boost/assign/list_of.hpp>
27+
#include <boost/thread/thread.hpp>
2728

2829
#include <univalue.h>
2930

@@ -3412,9 +3413,37 @@ UniValue dzpivstate(const UniValue& params, bool fHelp) {
34123413
return obj;
34133414
}
34143415

3416+
3417+
void static SearchThread(CzPIVWallet* zwallet, int nCountStart, int nCountEnd)
3418+
{
3419+
LogPrintf("%s: start=%d end=%d\n", __func__, nCountStart, nCountEnd);
3420+
try {
3421+
uint256 seedMaster = zwallet->GetMasterSeed();
3422+
for(int i = nCountStart; i < nCountEnd; i++) {
3423+
boost::this_thread::interruption_point();
3424+
CDataStream ss(SER_GETHASH, 0);
3425+
ss << seedMaster << i;
3426+
uint512 zerocoinSeed = Hash512(ss.begin(), ss.end());
3427+
3428+
CBigNum bnValue;
3429+
CBigNum bnSerial;
3430+
CBigNum bnRandomness;
3431+
CKey key;
3432+
zwallet->SeedToZPIV(zerocoinSeed, bnValue, bnSerial, bnRandomness, key);
3433+
3434+
uint256 hashPubcoin = GetPubCoinHash(bnValue);
3435+
zwallet->AddToMintPool(make_pair(hashPubcoin, i));
3436+
}
3437+
} catch (std::exception& e) {
3438+
LogPrintf("SearchThread() exception");
3439+
} catch (...) {
3440+
LogPrintf("SearchThread() exception");
3441+
}
3442+
}
3443+
34153444
UniValue searchdzpiv(const UniValue& params, bool fHelp)
34163445
{
3417-
if(fHelp || params.size() != 2)
3446+
if(fHelp || params.size() != 3)
34183447
throw runtime_error(
34193448
"searchdzpiv\n"
34203449
"\nMake an extended search for deterministically generated zPIV that have not yet been recognized by the wallet.\n" +
@@ -3423,9 +3452,10 @@ UniValue searchdzpiv(const UniValue& params, bool fHelp)
34233452
"\nArguments\n"
34243453
"1. \"count\" (numeric) Which sequential zPIV to start with.\n"
34253454
"2. \"range\" (numeric) How many zPIV to generate.\n"
3455+
"3. \"threads\" (numeric) How many threads should this operation consume.\n"
34263456

34273457
"\nExamples\n" +
3428-
HelpExampleCli("searchdzpiv", "1, 100") + HelpExampleRpc("searchdzpiv", "1, 100"));
3458+
HelpExampleCli("searchdzpiv", "1, 100, 2") + HelpExampleRpc("searchdzpiv", "1, 100, 2"));
34293459

34303460
EnsureWalletIsUnlocked();
34313461

@@ -3437,8 +3467,23 @@ UniValue searchdzpiv(const UniValue& params, bool fHelp)
34373467
if (nRange < 1)
34383468
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range has to be at least 1");
34393469

3470+
int nThreads = params[2].get_int();
3471+
34403472
CzPIVWallet* zwallet = pwalletMain->zwalletMain;
3441-
zwallet->GenerateMintPool(nCount, nRange);
3473+
3474+
boost::thread_group* dzpivThreads = new boost::thread_group();
3475+
int nRangePerThread = nRange / nThreads;
3476+
3477+
int nPrevThreadEnd = nCount - 1;
3478+
for (int i = 0; i < nThreads; i++) {
3479+
int nStart = nPrevThreadEnd + 1;;
3480+
int nEnd = nStart + nRangePerThread;
3481+
nPrevThreadEnd = nEnd;
3482+
dzpivThreads->create_thread(boost::bind(&SearchThread, zwallet, nStart, nEnd));
3483+
}
3484+
3485+
dzpivThreads->join_all();
3486+
34423487
CzPIVTracker* tracker = pwalletMain->zpivTracker;
34433488
zwallet->RemoveMintsFromPool(tracker->GetSerialHashes());
34443489
zwallet->SyncWithChain(false);

src/zpivwallet.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ void CzPIVWallet::Lock()
9696
seedMaster = 0;
9797
}
9898

99+
void CzPIVWallet::AddToMintPool(const std::pair<uint256, uint32_t>& pMint)
100+
{
101+
mintPool.Add(pMint);
102+
}
103+
99104
//Add the next 20 mints to the mint pool
100105
void CzPIVWallet::GenerateMintPool(uint32_t nCountStart, uint32_t nCountEnd)
101106
{
@@ -222,7 +227,7 @@ void CzPIVWallet::SyncWithChain(bool fGenerateMintPool)
222227
CoinDenomination denomination = CoinDenomination::ZQ_ERROR;
223228
bool fFoundMint = false;
224229
CBigNum bnValue = 0;
225-
for (const CTxOut out : tx.vout) {
230+
for (const CTxOut& out : tx.vout) {
226231
if (!out.scriptPubKey.IsZerocoinMint())
227232
continue;
228233

src/zpivwallet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class CzPIVWallet
2323

2424
public:
2525
CzPIVWallet(std::string strWalletFile);
26+
27+
void AddToMintPool(const std::pair<uint256, uint32_t>& pMint);
2628
bool SetMasterSeed(const uint256& seedMaster, bool fResetCount = false);
2729
uint256 GetMasterSeed() { return seedMaster; }
2830
void SyncWithChain(bool fGenerateMintPool = true);
@@ -37,10 +39,10 @@ class CzPIVWallet
3739
bool IsInMintPool(const CBigNum& bnValue) { return mintPool.Has(bnValue); }
3840
void UpdateCount();
3941
void Lock();
42+
void SeedToZPIV(const uint512& seed, CBigNum& bnValue, CBigNum& bnSerial, CBigNum& bnRandomness, CKey& key);
4043

4144
private:
4245
uint512 GetZerocoinSeed(uint32_t n);
43-
void SeedToZPIV(const uint512& seed, CBigNum& bnValue, CBigNum& bnSerial, CBigNum& bnRandomness, CKey& key);
4446
};
4547

4648
#endif //PIVX_ZPIVWALLET_H

0 commit comments

Comments
 (0)