Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starting miner when blockchain is not synced kills meritd #219

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,6 @@ void static MeritMiner(const CChainParams& chainparams, uint8_t nThreads)
unsigned int nExtraNonce = 0;

std::shared_ptr<CReserveScript> coinbaseScript;
GetMainSignals().ScriptForMining(coinbaseScript);

ctpl::thread_pool pool{nThreads};

Expand All @@ -915,9 +914,8 @@ void static MeritMiner(const CChainParams& chainparams, uint8_t nThreads)
// due to some internal error but also if the keypool is empty.
// In the latter case, already the pointer is NULL.
if (!coinbaseScript || coinbaseScript->reserveScript.empty()) {
throw std::runtime_error(
"No coinbase script available "
"(mining requires confirmed wallet)");
LogPrintf("No coinbase script available (mining requires confirmed wallet)."
" Blockchain might be not fully synced.\n");
}

while (true) {
Expand All @@ -940,6 +938,24 @@ void static MeritMiner(const CChainParams& chainparams, uint8_t nThreads)
} while (true);
}

while (IsInitialBlockDownload()) {
LogPrintf("Initial blockchain download is active.\n");
MilliSleep(1000);
}

if (!coinbaseScript || coinbaseScript->reserveScript.empty()) {
LogPrintf("No coinbase script found, generating new.\n");
GetMainSignals().ScriptForMining(coinbaseScript);
}


if (!coinbaseScript || coinbaseScript->reserveScript.empty()) {
throw std::runtime_error(
"No coinbase script available"
" (mining requires confirmed wallet)."
" Miner will be stopped.\n");
}

//
// Create new block
//
Expand Down Expand Up @@ -1052,9 +1068,11 @@ void static MeritMiner(const CChainParams& chainparams, uint8_t nThreads)
}
} catch (const boost::thread_interrupted&) {
LogPrintf("MeritMiner terminated\n");
gArgs.ForceSetArg("-mine", 0);
throw;
} catch (const std::runtime_error& e) {
LogPrintf("MeritMiner runtime error: %s\n", e.what());
gArgs.ForceSetArg("-mine", 0);
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
static std::unique_ptr<CBlockTemplate> pblocktemplate;
if (pindexPrev != chainActive.Tip() || (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
{
// Clear pindexPrev so future calls make a new block, despite any failures from here on
// Clear pindexPrev so future calls make a new block, despite any failures from here on
pindexPrev = nullptr;

// Store the pindexBest used before CreateNewBlock, to avoid races
Expand Down Expand Up @@ -1030,7 +1030,7 @@ UniValue setmining(const JSONRPCRequest& request)

GenerateMerit(mine, nThreads, Params());

return NullUniValue;
return gArgs.GetBoolArg("-mine", DEFAULT_MINING);
}

UniValue getmining(const JSONRPCRequest& request)
Expand Down
32 changes: 22 additions & 10 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,24 +2138,36 @@ bool AreExpectedInvitesRewarded(const pog::InviteRewards& expected_invites, cons
bool IsInitialBlockDownload()
{
// Once this function has returned false, it must remain false.
static std::atomic<bool> latchToFalse{false};
// static std::atomic<bool> latchToFalse{false};
// Optimization: pre-test latch before taking the lock.
if (latchToFalse.load(std::memory_order_relaxed))
return false;
// if (latchToFalse.load(std::memory_order_relaxed)) {
// LogPrintf("Pre: Latched to false\n");
// return false;
// }

LOCK(cs_main);
if (latchToFalse.load(std::memory_order_relaxed))
return false;
if (fImporting || fReindex)
// if (latchToFalse.load(std::memory_order_relaxed)) {
// LogPrintf("Latched to false\n");
// return false;
// }
if (fImporting || fReindex) {
LogPrintf("Importing or reindex\n");
return true;
if (chainActive.Tip() == nullptr)
}
if (chainActive.Tip() == nullptr) {
LogPrintf("Tip is null\n");
return true;
if (chainActive.Tip()->nChainWork < nMinimumChainWork)
}
if (chainActive.Tip()->nChainWork < nMinimumChainWork) {
LogPrintf("Tip chainwork is less than minimum chainwork\n");
return true;
if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
}
if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge)) {
LogPrintf("Time check triggered\n");
return true;
}
LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
latchToFalse.store(true, std::memory_order_relaxed);
// latchToFalse.store(true, std::memory_order_relaxed);
return false;
}

Expand Down
10 changes: 7 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,8 +1499,8 @@ isminetype CWallet::IsMine(const referral::Referral& ref) const
{
LOCK(cs_wallet);
const auto root = GetRootReferral();
return
mapWalletRTx.count(ref.GetHash()) ||
return
mapWalletRTx.count(ref.GetHash()) ||
(root && ref.parentAddress == root->GetAddress()) ? ISMINE_ALL : ISMINE_NO;
}

Expand Down Expand Up @@ -4110,7 +4110,7 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)

CWalletDB walletdb(*dbw);


auto it = setKeyPool.begin();
while(it != setKeyPool.end()) {
nIndex = *it;
Expand All @@ -4133,6 +4133,10 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
it++;
}

// if (it == setKeyPool.end()) {
// throw std::runtime_error(std::string(__func__) + ": beconed key was not found");
// }

// do not remove key from pool
if (Daedalus()) {
return;
Expand Down