Skip to content

Commit

Permalink
Fix for multiple machines doing the same work if wallet is shared.
Browse files Browse the repository at this point in the history
Initialize the extra nonce using the current value of a nanosecond-precision clock.
Boost.Chrono library is now required for compiling.
  • Loading branch information
Mikael Hirki committed Dec 26, 2013
1 parent 8a3a382 commit a1c3f58
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
4 changes: 1 addition & 3 deletions bitcoin-qt-win64.pro
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,7 @@ LIBS += $$join(BOOST_LIB_PATH,,-L,) $$join(BDB_LIB_PATH,,-L,) $$join(OPENSSL_LIB
LIBS += -lssl -lcrypto -ldb_cxx$$BDB_LIB_SUFFIX
# -lgdi32 has to happen after -lcrypto (see #681)
win32:LIBS += -lws2_32 -lshlwapi -lmswsock -lole32 -loleaut32 -luuid -lgdi32
LIBS += -lboost_system$$BOOST_LIB_SUFFIX -lboost_filesystem$$BOOST_LIB_SUFFIX -lboost_program_options$$BOOST_LIB_SUFFIX -lboost_thread$$BOOST_THREAD_LIB_SUFFIX
win32:LIBS += -lboost_chrono$$BOOST_LIB_SUFFIX
macx:LIBS += -lboost_chrono$$BOOST_LIB_SUFFIX
LIBS += -lboost_system$$BOOST_LIB_SUFFIX -lboost_filesystem$$BOOST_LIB_SUFFIX -lboost_program_options$$BOOST_LIB_SUFFIX -lboost_thread$$BOOST_THREAD_LIB_SUFFIX -lboost_chrono$$BOOST_LIB_SUFFIX
# Link dynamically against GMP
LIBS += -Wl,-Bdynamic -lgmp

Expand Down
4 changes: 1 addition & 3 deletions bitcoin-qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ LIBS += $$join(BOOST_LIB_PATH,,-L,) $$join(BDB_LIB_PATH,,-L,) $$join(OPENSSL_LIB
LIBS += -lssl -lcrypto -ldb_cxx$$BDB_LIB_SUFFIX
# -lgdi32 has to happen after -lcrypto (see #681)
win32:LIBS += -lws2_32 -lshlwapi -lmswsock -lole32 -loleaut32 -luuid -lgdi32
LIBS += -lboost_system$$BOOST_LIB_SUFFIX -lboost_filesystem$$BOOST_LIB_SUFFIX -lboost_program_options$$BOOST_LIB_SUFFIX -lboost_thread$$BOOST_THREAD_LIB_SUFFIX
win32:LIBS += -lboost_chrono$$BOOST_LIB_SUFFIX
macx:LIBS += -lboost_chrono$$BOOST_LIB_SUFFIX
LIBS += -lboost_system$$BOOST_LIB_SUFFIX -lboost_filesystem$$BOOST_LIB_SUFFIX -lboost_program_options$$BOOST_LIB_SUFFIX -lboost_thread$$BOOST_THREAD_LIB_SUFFIX -lboost_chrono$$BOOST_LIB_SUFFIX
# Link dynamically against GMP
LIBS += -Wl,-Bdynamic -lgmp

Expand Down
16 changes: 13 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4455,11 +4455,11 @@ CBlockTemplate* CreateNewBlock(CReserveKey& reservekey)
}


void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, bool fNoReset)
{
// Update nExtraNonce
static uint256 hashPrevBlock;
if (hashPrevBlock != pblock->hashPrevBlock)
if (!fNoReset && hashPrevBlock != pblock->hashPrevBlock)
{
nExtraNonce = 0;
hashPrevBlock = pblock->hashPrevBlock;
Expand Down Expand Up @@ -4572,6 +4572,16 @@ void static BitcoinMiner(CWallet *pwallet)
int64 nSieveGenTime = 0; // how many milliseconds sieve generation took
bool fIncrementPrimorial = true; // increase or decrease primorial factor

// Many machines may be using the same key if they are sharing the same wallet
// Make extra nonce unique by setting it to a modulo of the high resolution clock's value
const unsigned int nExtraNonceModulo = 10000000;
boost::chrono::high_resolution_clock::time_point time_now = boost::chrono::high_resolution_clock::now();
boost::chrono::nanoseconds ns_now = boost::chrono::duration_cast<boost::chrono::nanoseconds>(time_now.time_since_epoch());
nExtraNonce = ns_now.count() % nExtraNonceModulo;

// Print the chosen extra nonce for debugging
printf("BitcoinMiner() : Setting initial extra nonce to %u\n", nExtraNonce);

try { loop {
while (vNodes.empty())
MilliSleep(1000);
Expand All @@ -4592,7 +4602,7 @@ void static BitcoinMiner(CWallet *pwallet)
if (!pblocktemplate.get())
return;
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, true);

if (fDebug && GetBoolArg("-printmining"))
printf("Running PrimecoinMiner with %"PRIszu" transactions in block (%u bytes)\n", pblock->vtx.size(),
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
/** Generate a new block, without valid proof-of-work */
CBlockTemplate* CreateNewBlock(CReserveKey& reservekey);
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, bool fNoReset = false);
/** Do mining precalculation */
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
/** Check mined block */
Expand Down
1 change: 1 addition & 0 deletions src/makefile.solaris
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ LIBS += \
-l boost_filesystem$(BOOST_LIB_SUFFIX) \
-l boost_program_options$(BOOST_LIB_SUFFIX) \
-l boost_thread$(BOOST_LIB_SUFFIX) \
-l boost_chrono$(BOOST_LIB_SUFFIX) \
-l db_cxx$(BDB_LIB_SUFFIX) \
-l ssl \
-l crypto \
Expand Down
1 change: 1 addition & 0 deletions src/makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ LIBS += \
-l boost_filesystem$(BOOST_LIB_SUFFIX) \
-l boost_program_options$(BOOST_LIB_SUFFIX) \
-l boost_thread$(BOOST_LIB_SUFFIX) \
-l boost_chrono$(BOOST_LIB_SUFFIX) \
-l db_cxx$(BDB_LIB_SUFFIX) \
-l ssl \
-l crypto \
Expand Down

0 comments on commit a1c3f58

Please sign in to comment.