Skip to content

Commit d2827d9

Browse files
Add multi wallet multi threaded staking
1 parent e3da5a0 commit d2827d9

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,8 +2031,10 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
20312031
}, DUMP_BANS_INTERVAL);
20322032

20332033
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
2034-
if (wallets.size() && wallets[0])
2035-
MintStake(threadGroup, wallets[0], node.chainman, node.connman.get(), node.mempool.get());
2034+
for (unsigned int i = 0; i < wallets.size(); i++) {
2035+
if (wallets[i])
2036+
MintStake(threadGroup, wallets[i], i+1, node.chainman, node.connman.get(), node.mempool.get());
2037+
}
20362038

20372039
#if HAVE_SYSTEM
20382040
StartupNotify(args);

src/miner.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,8 @@ static inline bool ProcessBlockFound(const CBlock* pblock, const CChainParams& c
717717
//LogPrintf("generated %s\n", FormatMoney(pblock->IsProofOfStake() ? pblock->vtx[1]->GetValueOut() : pblock->vtx[0]->GetValueOut()));
718718

719719
// Found a solution
720-
{
721-
LOCK(cs_main);
722-
if (pblock->hashPrevBlock != ::ChainActive().Tip()->GetBlockHash())
723-
return error("XEPMiner: generated block is stale");
724-
}
720+
if (pblock->hashPrevBlock != ::ChainActive().Tip()->GetBlockHash())
721+
return error("XEPMiner: generated block is stale");
725722

726723
// Process this block the same as if we had received it from another node
727724
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
@@ -734,7 +731,6 @@ static inline bool ProcessBlockFound(const CBlock* pblock, const CChainParams& c
734731
static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool)
735732
{
736733
LogPrintf("CPUMiner started for proof-of-stake\n");
737-
util::ThreadRename("xep-stake-minter");
738734

739735
unsigned int nExtraNonce = 0;
740736

@@ -750,13 +746,13 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
750746
LogPrintf("Set proof-of-stake timeout: %ums for %u UTXOs\n", pos_timio, vCoins.size());
751747
}
752748

753-
std::string strMintMessage = _("Info: Minting suspended due to locked wallet.").translated;
754-
std::string strMintSyncMessage = _("Info: Minting suspended while synchronizing wallet.").translated;
755-
std::string strMintDisabledMessage = _("Info: Minting disabled by 'nostaking' option.").translated;
756-
std::string strMintBlockMessage = _("Info: Minting suspended due to block creation failure.").translated;
757-
std::string strMintEmpty = "";
749+
const std::string strMintMessage = _("Info: Minting suspended due to locked wallet.").translated;
750+
const std::string strMintSyncMessage = _("Info: Minting suspended while synchronizing wallet.").translated;
751+
const std::string strMintDisabledMessage = _("Info: Minting disabled by 'nostaking' option.").translated;
752+
const std::string strMintBlockMessage = _("Info: Minting suspended due to block creation failure.").translated;
753+
const std::string strMintEmpty = "";
758754
if (!gArgs.GetBoolArg("-staking", true)) {
759-
strMintWarning = strMintDisabledMessage;
755+
SetMintWarning(strMintDisabledMessage);
760756
LogPrintf("proof-of-stake minter disabled\n");
761757
return;
762758
}
@@ -765,8 +761,8 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
765761
bool fNeedToClear = false;
766762
while (true) {
767763
while (pwallet->IsLocked()) {
768-
if (strMintWarning != strMintMessage) {
769-
strMintWarning = strMintMessage;
764+
if (GetMintWarning() != strMintMessage) {
765+
SetMintWarning(strMintMessage);
770766
uiInterface.NotifyAlertChanged();
771767
}
772768
fNeedToClear = true;
@@ -778,8 +774,8 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
778774
// Busy-wait for the network to come online so we don't waste time mining
779775
// on an obsolete chain. In regtest mode we expect to fly solo.
780776
while (connman == nullptr || connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 || ::ChainstateActive().IsInitialBlockDownload()) {
781-
if (strMintWarning != strMintSyncMessage) {
782-
strMintWarning = strMintSyncMessage;
777+
if (GetMintWarning() != strMintSyncMessage) {
778+
SetMintWarning(strMintSyncMessage);
783779
uiInterface.NotifyAlertChanged();
784780
}
785781
fNeedToClear = true;
@@ -790,16 +786,16 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
790786

791787
while (GuessVerificationProgress(Params().TxData(), ::ChainActive().Tip()) < 0.996) {
792788
LogPrintf("Minter thread sleeps while sync at %f\n", GuessVerificationProgress(Params().TxData(), ::ChainActive().Tip()));
793-
if (strMintWarning != strMintSyncMessage) {
794-
strMintWarning = strMintSyncMessage;
789+
if (GetMintWarning() != strMintSyncMessage) {
790+
SetMintWarning(strMintSyncMessage);
795791
uiInterface.NotifyAlertChanged();
796792
}
797793
fNeedToClear = true;
798794
if (!connman->interruptNet.sleep_for(std::chrono::seconds(10)))
799795
return;
800796
}
801797
if (fNeedToClear) {
802-
strMintWarning = strMintEmpty;
798+
SetMintWarning(strMintEmpty);
803799
uiInterface.NotifyAlertChanged();
804800
fNeedToClear = false;
805801
}
@@ -824,7 +820,7 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
824820
return;
825821
continue;
826822
}
827-
strMintWarning = strMintBlockMessage;
823+
SetMintWarning(strMintBlockMessage);
828824
uiInterface.NotifyAlertChanged();
829825
LogPrintf("Error in XEPMiner: Keypool ran out, please call keypoolrefill before restarting the staking thread\n");
830826
if (!connman->interruptNet.sleep_for(std::chrono::seconds(10)))
@@ -837,6 +833,7 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
837833
bool pubkeyInSig = true;
838834
{
839835
LOCK(pwallet->cs_wallet);
836+
840837
if (!pwallet->GetBlockSigningPubKey(*pblock, signingPubKey, pubkeyInSig)) {
841838
LogPrintf("PoSMiner(): failed to get signing pubkey for PoS block\n");
842839
continue;
@@ -847,6 +844,7 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
847844
// peercoin: if proof-of-stake block found then process block
848845
{
849846
LOCK(pwallet->cs_wallet);
847+
850848
if (!pwallet->SignBlock(*pblock, signingPubKey)) {
851849
LogPrintf("PoSMiner(): failed to sign PoS block\n");
852850
continue;
@@ -873,22 +871,23 @@ static inline void PoSMiner(std::shared_ptr<CWallet> pwallet, ChainstateManager*
873871
}
874872

875873
// peercoin: stake minter thread
876-
static void ThreadStakeMinter(std::shared_ptr<CWallet> pwallet, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool)
874+
static void ThreadStakeMinter(std::shared_ptr<CWallet> pwallet, const unsigned int walletNum, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool)
877875
{
878-
LogPrintf("ThreadStakeMinter started\n");
876+
util::ThreadRename("xep-stake-minter-" + std::to_string(walletNum));
877+
LogPrintf("ThreadStakeMinter #%u started\n", walletNum);
879878
try {
880879
PoSMiner(pwallet, chainman, connman, mempool);
881880
} catch (std::exception& e) {
882881
PrintExceptionContinue(&e, "ThreadStakeMinter()");
883882
} catch (...) {
884883
PrintExceptionContinue(NULL, "ThreadStakeMinter()");
885884
}
886-
LogPrintf("ThreadStakeMinter exiting\n");
885+
LogPrintf("ThreadStakeMinter #%u exiting\n", walletNum);
887886
}
888887

889888
// peercoin: stake minter
890-
void MintStake(boost::thread_group& threadGroup, std::shared_ptr<CWallet> pwallet, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool)
889+
void MintStake(boost::thread_group& threadGroup, std::shared_ptr<CWallet> pwallet, const unsigned int walletNum, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool)
891890
{
892891
// peercoin: mint proof-of-stake blocks in the background
893-
threadGroup.create_thread(boost::bind(&ThreadStakeMinter, pwallet, chainman, connman, mempool));
892+
threadGroup.create_thread(boost::bind(&ThreadStakeMinter, pwallet, walletNum, chainman, connman, mempool));
894893
}

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,6 @@ namespace boost {
222222
} // namespace boost
223223

224224
bool CreateCoinStake(CMutableTransaction& coinstakeTx, CBlock* pblock, std::shared_ptr<CWallet> pwallet, const int& nHeight, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);
225-
void MintStake(boost::thread_group& threadGroup, std::shared_ptr<CWallet> pwallet, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool);
225+
void MintStake(boost::thread_group& threadGroup, std::shared_ptr<CWallet> pwallet, const unsigned int walletNum, ChainstateManager* chainman, CConnman* connman, CTxMemPool* mempool);
226226

227227
#endif // BITCOIN_MINER_H

src/warnings.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static Mutex g_warnings_mutex;
1616
static bilingual_str g_misc_warnings GUARDED_BY(g_warnings_mutex);
1717
static bool fLargeWorkForkFound GUARDED_BY(g_warnings_mutex) = false;
1818
static bool fLargeWorkInvalidChainFound GUARDED_BY(g_warnings_mutex) = false;
19-
std::string strMintWarning;
19+
static std::string strMintWarning GUARDED_BY(g_warnings_mutex);
2020

2121
void SetMiscWarning(const bilingual_str& warning)
2222
{
@@ -42,6 +42,18 @@ void SetfLargeWorkInvalidChainFound(bool flag)
4242
fLargeWorkInvalidChainFound = flag;
4343
}
4444

45+
void SetMintWarning(const std::string& warning)
46+
{
47+
LOCK(g_warnings_mutex);
48+
strMintWarning = warning;
49+
}
50+
51+
std::string GetMintWarning()
52+
{
53+
LOCK(g_warnings_mutex);
54+
return strMintWarning;
55+
}
56+
4557
bilingual_str GetWarnings(bool verbose)
4658
{
4759
bilingual_str warnings_concise;

src/warnings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ void SetMiscWarning(const bilingual_str& warning);
1414
void SetfLargeWorkForkFound(bool flag);
1515
bool GetfLargeWorkForkFound();
1616
void SetfLargeWorkInvalidChainFound(bool flag);
17+
void SetMintWarning(const std::string& warning);
18+
std::string GetMintWarning();
1719
/** Format a string that describes several potential problems detected by the core.
1820
* @param[in] verbose bool
1921
* - if true, get all warnings separated by <hr />
@@ -22,5 +24,4 @@ void SetfLargeWorkInvalidChainFound(bool flag);
2224
*/
2325
bilingual_str GetWarnings(bool verbose);
2426

25-
extern std::string strMintWarning;
2627
#endif // BITCOIN_WARNINGS_H

0 commit comments

Comments
 (0)