From 3a8029f0335dbdb039eccb7bddc88fb0cd1ff971 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 11 May 2011 16:50:14 -0400 Subject: [PATCH 1/5] Update nTime after nExtraNonce to avoid potential race (extraNonce being reset due to just-occurred time change after nTime is set) --- rpc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rpc.cpp b/rpc.cpp index 90e7f15a..2175dddb 100644 --- a/rpc.cpp +++ b/rpc.cpp @@ -1330,15 +1330,15 @@ Value getwork(const Array& params, bool fHelp) vNewBlock.push_back(pblock); } - // Update nTime - pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - pblock->nNonce = 0; - // Update nExtraNonce static unsigned int nExtraNonce = 0; static int64 nPrevTime = 0; IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, nPrevTime); + // Update nTime + pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + pblock->nNonce = 0; + // Save mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, nExtraNonce); From aa4a9c5250f94c82a9e4077a1b56ba634a9841fa Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 11 May 2011 17:12:47 -0400 Subject: [PATCH 2/5] Reset extraNonce only every 15 seconds, just in case some miner is updating time himself and stuff --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 4c8f20c1..bf7c9934 100644 --- a/main.cpp +++ b/main.cpp @@ -3448,7 +3448,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& { // Update nExtraNonce int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1) + if (++nExtraNonce >= 0x7f && nNow > nPrevTime+15) { nExtraNonce = 1; nPrevTime = nNow; From 02d87b3aa375810bcd63dcf72637e788a75fc7d4 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Sun, 12 Jun 2011 20:16:54 -0400 Subject: [PATCH 3/5] Reset extraNonce only when prevBlock changes, so miners can continue updating the time on their work until it's stale --- main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index bf7c9934..d71d4116 100644 --- a/main.cpp +++ b/main.cpp @@ -3447,12 +3447,13 @@ CBlock* CreateNewBlock(CReserveKey& reservekey) void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime) { // Update nExtraNonce - int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - if (++nExtraNonce >= 0x7f && nNow > nPrevTime+15) + static uint256 hashPrevBlock; + if (hashPrevBlock != pblock->hashPrevBlock) { - nExtraNonce = 1; - nPrevTime = nNow; + nExtraNonce = 0; + hashPrevBlock = pblock->hashPrevBlock; } + ++nExtraNonce; pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); pblock->hashMerkleRoot = pblock->BuildMerkleTree(); } From 49c8e53ee2c2705fd0a8731458cb776dad7e1f6e Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Mon, 25 Jul 2011 11:59:41 -0400 Subject: [PATCH 4/5] Save coinbase, not just extraNonce --- src/rpc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rpc.cpp b/src/rpc.cpp index 9e7cfc5b..2a458423 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1581,7 +1581,8 @@ Value getwork(const Array& params, bool fHelp) if (IsInitialBlockDownload()) throw JSONRPCError(-10, "Bitcoin is downloading blocks..."); - static map > mapNewBlock; + typedef map > mapNewBlock_t; + static mapNewBlock_t mapNewBlock; static vector vNewBlock; static CReserveKey reservekey(pwalletMain); @@ -1624,7 +1625,7 @@ Value getwork(const Array& params, bool fHelp) pblock->nNonce = 0; // Save - mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, nExtraNonce); + mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig); // Prebuild hash buffers char pmidstate[32]; @@ -1657,11 +1658,10 @@ Value getwork(const Array& params, bool fHelp) if (!mapNewBlock.count(pdata->hashMerkleRoot)) return false; CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first; - unsigned int nExtraNonce = mapNewBlock[pdata->hashMerkleRoot].second; pblock->nTime = pdata->nTime; pblock->nNonce = pdata->nNonce; - pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); + pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second; pblock->hashMerkleRoot = pblock->BuildMerkleTree(); return CheckWork(pblock, *pwalletMain, reservekey); From 83f4cd156e9d52bd7c4351336dfa4806a43ee4e4 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 6 Sep 2011 16:39:05 -0400 Subject: [PATCH 5/5] Bugfix: Use timestamp in coinbase rather than "bits", needed to ensure coinbase txn is unique even if address is the same --- src/main.cpp | 7 +++---- src/main.h | 2 +- src/rpc.cpp | 9 ++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 480d2d8b..10308bc9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2794,7 +2794,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey) } -void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime) +void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) { // Update nExtraNonce static uint256 hashPrevBlock; @@ -2804,7 +2804,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& hashPrevBlock = pblock->hashPrevBlock; } ++nExtraNonce; - pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); + pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nTime << CBigNum(nExtraNonce); pblock->hashMerkleRoot = pblock->BuildMerkleTree(); } @@ -2902,7 +2902,6 @@ void static BitcoinMiner(CWallet *pwallet) // Each thread has its own key and counter CReserveKey reservekey(pwallet); unsigned int nExtraNonce = 0; - int64 nPrevTime = 0; while (fGenerateBitcoins) { @@ -2929,7 +2928,7 @@ void static BitcoinMiner(CWallet *pwallet) auto_ptr pblock(CreateNewBlock(reservekey)); if (!pblock.get()) return; - IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce, nPrevTime); + IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce); printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size()); diff --git a/src/main.h b/src/main.h index 97a40d3e..0b05febb 100644 --- a/src/main.h +++ b/src/main.h @@ -96,7 +96,7 @@ bool ProcessMessages(CNode* pfrom); bool SendMessages(CNode* pto, bool fSendTrickle); void GenerateBitcoins(bool fGenerate, CWallet* pwallet); CBlock* CreateNewBlock(CReserveKey& reservekey); -void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime); +void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1); bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey); bool CheckProofOfWork(uint256 hash, unsigned int nBits); diff --git a/src/rpc.cpp b/src/rpc.cpp index 2a458423..33cfa77b 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1615,15 +1615,14 @@ Value getwork(const Array& params, bool fHelp) vNewBlock.push_back(pblock); } - // Update nExtraNonce - static unsigned int nExtraNonce = 0; - static int64 nPrevTime = 0; - IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, nPrevTime); - // Update nTime pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); pblock->nNonce = 0; + // Update nExtraNonce + static unsigned int nExtraNonce = 0; + IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); + // Save mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);