From 91aadbdacf9a3111da76eb831ea36d6827e1d6fc Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 16 Feb 2012 10:22:31 -0500 Subject: [PATCH] Fix issue #848 : broken mining on testnet --- src/main.cpp | 20 ++++++++++++++++++-- src/main.h | 2 ++ src/rpc.cpp | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cd02652..ebf51ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -807,6 +807,15 @@ void static InvalidChainFound(CBlockIndex* pindexNew) printf("InvalidChainFound: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n"); } +void CBlock::UpdateTime(const CBlockIndex* pindexPrev) +{ + nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + + // Updating time can change work required on testnet: + if (fTestNet) + nBits = GetNextWorkRequired(pindexPrev, this); +} + @@ -2896,7 +2905,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey) // Fill in header pblock->hashPrevBlock = pindexPrev->GetBlockHash(); pblock->hashMerkleRoot = pblock->BuildMerkleTree(); - pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + pblock->UpdateTime(pindexPrev); pblock->nBits = GetNextWorkRequired(pindexPrev, pblock.get()); pblock->nNonce = 0; @@ -3053,6 +3062,7 @@ void static BitcoinMiner(CWallet *pwallet) FormatHashBuffers(pblock.get(), pmidstate, pdata, phash1); unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4); + unsigned int& nBlockBits = *(unsigned int*)(pdata + 64 + 8); unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12); @@ -3140,8 +3150,14 @@ void static BitcoinMiner(CWallet *pwallet) break; // Update nTime every few seconds - pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + pblock->UpdateTime(pindexPrev); nBlockTime = ByteReverse(pblock->nTime); + if (fTestNet) + { + // Changing pblock->nTime can change work required on testnet: + nBlockBits = ByteReverse(pblock->nBits); + hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); + } } } } diff --git a/src/main.h b/src/main.h index 3e381b0..278f4f0 100644 --- a/src/main.h +++ b/src/main.h @@ -846,6 +846,8 @@ public: return n; } + void UpdateTime(const CBlockIndex* pindexPrev); + uint256 BuildMerkleTree() const { diff --git a/src/rpc.cpp b/src/rpc.cpp index 8967b2c..aade32a 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1548,7 +1548,7 @@ Value getwork(const Array& params, bool fHelp) } // Update nTime - pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + pblock->UpdateTime(pindexPrev); pblock->nNonce = 0; // Update nExtraNonce -- 1.7.1