Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Version 0.2.5.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg-Griffith authored and Greg Griffith committed May 2, 2018
2 parents d01079d + 93c853f commit 94c3cfa
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define CLIENT_VERSION_MAJOR 0
#define CLIENT_VERSION_MINOR 2
#define CLIENT_VERSION_REVISION 5
#define CLIENT_VERSION_BUILD 9
#define CLIENT_VERSION_BUILD 10

//! Set to true for release, false for prerelease or test build
#define CLIENT_VERSION_IS_RELEASE true
Expand Down
4 changes: 2 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void Shutdown()

if (pwalletMain)
pwalletMain->Flush(false);
ThreadScryptMiner(pwalletMain, true);
ThreadMiner(pwalletMain, true);
MapPort(false);
UnregisterValidationInterface(peerLogic.get());
peerLogic.reset();
Expand Down Expand Up @@ -1644,7 +1644,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// Generate coins in the background
if(gArgs.GetBoolArg("-staking", false))
{
ThreadScryptMiner(pwalletMain);
ThreadMiner(pwalletMain);
}

// ********************************************************* Step 12: finished
Expand Down
141 changes: 72 additions & 69 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "processblock.h"
#include "networks/netman.h"
#include "net.h"
#include "policy/policy.h"

#include <boost/thread.hpp>
#include <openssl/sha.h>
Expand All @@ -53,10 +54,6 @@ int64_t nLastCoinStakeSearchInterval = 0;
double dHashesPerSec;
int64_t nHPSTimerStart;

static std::string strMintMessage = "Info: Minting suspended due to locked wallet.";
static std::string strMintWarning;



//////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -172,21 +169,18 @@ class ScoreCompare
}
};


/** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */
static const unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS = LOCKTIME_VERIFY_SEQUENCE |
LOCKTIME_MEDIAN_TIME_PAST;

// CreateNewBlock:
// fProofOfStake: try (best effort) to make a proof-of-stake block
CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake)
std::unique_ptr<CBlockTemplate> CreateNewBlock(CWallet* pwallet, bool fProofOfStake)
{
CReserveKey reservekey(pwallet);

// Create new block
std::auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
if(!pblocktemplate.get())
return NULL;
{
return nullptr;
}
CBlock *pblock = &pblocktemplate->block; // pointer for convenience

// Create coinbase tx
Expand Down Expand Up @@ -433,7 +427,7 @@ CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake)
pblock->nNonce = 0;
}

return pblocktemplate.release();
return std::move(pblocktemplate);
}


Expand Down Expand Up @@ -539,20 +533,16 @@ bool CheckWork(const std::shared_ptr<const CBlock> pblock, CWallet& wallet, CRes
return true;
}

void ScryptMiner(CWallet *pwallet)
void EccMiner(CWallet *pwallet)
{
void *scratchbuf = scrypt_buffer_alloc();

LogPrintf("CPUMiner started for proof-of-%s\n", "stake");
SetThreadPriority(THREAD_PRIORITY_LOWEST);

// Make this thread recognisable as the mining thread
RenameThread("bitcoin-miner");

RenameThread("ecc-miner");
// Each thread has its own key and counter
CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0;

while (true)
{
if (fShutdown)
Expand All @@ -563,82 +553,44 @@ void ScryptMiner(CWallet *pwallet)
if (fShutdown)
return;
}
while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload())
while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload() || pwallet->IsLocked())
{
MilliSleep(1000);
if (fShutdown)
return;
}

while (pwallet->IsLocked())
{
strMintWarning = strMintMessage;
MilliSleep(1000);
}
strMintWarning = "";

//
// Create new block
//
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrev = pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip();

std::auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(pwallet, true));
std::unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(pwallet, true));
if (!pblocktemplate.get())
{
LogPrintf("Error in Miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
return;
}
CBlock *pblock = &pblocktemplate->block;
const std::shared_ptr<const CBlock> spblock(pblock);

IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);

{
// ppcoin: if proof-of-stake block found then process block
if (pblock->IsProofOfStake())
{
if (!pblock->SignScryptBlock(*pwalletMain))
{
strMintWarning = strMintMessage;
continue;
}
strMintWarning = "";
LogPrintf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str());
SetThreadPriority(THREAD_PRIORITY_NORMAL);
CheckWork(spblock, *pwalletMain, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST);
}
MilliSleep(1000); // 1 second delay
continue;
}

LogPrintf("Running Miner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));

//
// Pre-build hash buffers
//
char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);

FormatHashBuffers(pblock, pmidstate, pdata, phash1);

unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12);


//
// Search
//
int64_t nStart = GetTime();
arith_uint256 hashTarget = UintToArith256(CBigNum().SetCompact(pblock->nBits).getuint256());

unsigned int max_nonce = 0xffff0000;
CBlockHeader res_header;
arith_uint256 result;

while(true)
{
unsigned int nHashesDone = 0;
Expand All @@ -663,18 +615,15 @@ void ScryptMiner(CWallet *pwallet)
assert(result == UintToArith256(pblock->GetHash()));
if (!pblock->SignScryptBlock(*pwalletMain))
{
// strMintWarning = strMintMessage;
break;
}
strMintWarning = "";

SetThreadPriority(THREAD_PRIORITY_NORMAL);
const std::shared_ptr<const CBlock> spblock = std::make_shared<const CBlock>(*pblock);
CheckWork(spblock, *pwalletMain, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST);
break;
}
}

// Meter hashes/sec
static int64_t nHashCounter;
if (nHPSTimerStart == 0)
Expand All @@ -697,7 +646,6 @@ void ScryptMiner(CWallet *pwallet)
}
}
}

// Check for stop or if block needs to be rebuilt
if (fShutdown)
return;
Expand All @@ -709,24 +657,79 @@ void ScryptMiner(CWallet *pwallet)
break;
if (pindexPrev != pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip())
break;

// Update nTime every few seconds
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime());
pblock->nTime = std::max(pblock->GetBlockTime(), pindexPrev->GetBlockTime() - nMaxClockDrift);
pblock->UpdateTime();
nBlockTime = ByteReverse(pblock->nTime);

if (pblock->GetBlockTime() >= (int64_t)pblock->vtx[0]->nTime + nMaxClockDrift)
break; // need to update coinbase timestamp
}
}

scrypt_buffer_free(scratchbuf);
}

void EccMinter(CWallet *pwallet)
{
LogPrintf("CPUMiner started for proof-of-%s\n", "stake");
SetThreadPriority(THREAD_PRIORITY_LOWEST);
// Make this thread recognisable as the mining thread
RenameThread("ecc-minter");
// Each thread has its own key and counter
CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0;
while (true)
{
if (fShutdown)
return;
if(!g_connman)
{
MilliSleep(1000);
if (fShutdown)
return;
}
while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload() || pwallet->IsLocked())
{
MilliSleep(1000);
if (fShutdown)
return;
}
//
// Create new block
//
CBlockIndex* pindexPrev = pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip();
std::unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(pwallet, true));
if (!pblocktemplate.get())
{
LogPrintf("Error in Miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
return;
}
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
// ppcoin: if proof-of-stake block found then process block
if (pblock->IsProofOfStake())
{
if (!pblock->SignScryptBlock(*pwalletMain))
{
continue;
}
LogPrintf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str());
SetThreadPriority(THREAD_PRIORITY_NORMAL);
const std::shared_ptr<const CBlock> spblock = std::make_shared<const CBlock>(*pblock);
CheckWork(spblock, *pwalletMain, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST);
}
MilliSleep(1000); // 1 second delay
continue;
}
}




boost::thread_group* minerThreads = nullptr;

void ThreadScryptMiner(void* parg, bool shutdownOnly)
void ThreadMiner(void* parg, bool shutdownOnly)
{

if (minerThreads != nullptr)
Expand All @@ -748,7 +751,7 @@ void ThreadScryptMiner(void* parg, bool shutdownOnly)
CWallet* pwallet = (CWallet*)parg;
try
{
minerThreads->create_thread(boost::bind(&ScryptMiner, pwallet));
minerThreads->create_thread(boost::bind(&EccMinter, pwallet));
}
catch (std::exception& e) {
PrintException(&e, "ThreadECCMinter()");
Expand Down
10 changes: 5 additions & 5 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ static const bool DEFAULT_GENERATE = false;
static const bool DEFAULT_PRINTPRIORITY = false;


/** Check mined proof-of-stake block */
bool CheckStake(CBlock* pblock, CWallet& wallet);

/** Base sha256 mining transform */
void SHA256Transform(void* pstate, void* pinput, const void* pinit);

void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
bool CheckWork(const std::shared_ptr<const CBlock> pblock, CWallet& wallet, CReserveKey& reservekey);

void ThreadScryptMiner(void* parg, bool shutdownOnly=false);
/** Check mined proof-of-stake block */
bool CheckStake(CBlock* pblock, CWallet& wallet);

void ThreadMiner(void* parg, bool shutdownOnly=false);

struct CBlockTemplate
{
Expand All @@ -49,7 +49,7 @@ struct CBlockTemplate
std::vector<int64_t> vTxSigOps;
};

CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake);
std::unique_ptr<CBlockTemplate> CreateNewBlock(CWallet* pwallet, bool fProofOfStake);

extern boost::thread_group* minerThreads;

Expand Down
14 changes: 8 additions & 6 deletions src/rpc/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ UniValue generate(const UniValue& params, bool fHelp)
UniValue blockHashes(UniValue::VARR);
while (nHeight < nHeightEnd)
{
std::auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(pwalletMain, true));
std::unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(pwalletMain, true));
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
CBlock *pblock = &pblocktemplate->block;
Expand Down Expand Up @@ -238,7 +238,7 @@ UniValue setgenerate(const UniValue& params, bool fHelp)
if (pnetMan->getActivePaymentNetwork()->MineBlocksOnDemand())
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Use the generate method instead of setgenerate on this network");

ThreadScryptMiner(pwalletMain, false);
ThreadMiner(pwalletMain, false);

return NullUniValue;
}
Expand Down Expand Up @@ -532,7 +532,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
// Update block
static CBlockIndex* pindexPrev;
static int64_t nStart;
static CBlockTemplate* pblocktemplate;
static std::unique_ptr<CBlockTemplate> pblocktemplate;
if (pindexPrev != pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip() ||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
{
Expand All @@ -547,12 +547,14 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
// Create new block
if(pblocktemplate)
{
delete pblocktemplate;
pblocktemplate = NULL;
pblocktemplate.reset();
pblocktemplate = nullptr;
}
pblocktemplate = CreateNewBlock(pwalletMain, true);
pblocktemplate.reset(CreateNewBlock(pwalletMain, true).get());
if (!pblocktemplate)
{
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
}

// Need to update only after we know CreateNewBlock succeeded
pindexPrev = pindexPrevNew;
Expand Down
Loading

0 comments on commit 94c3cfa

Please sign in to comment.