From b354251f1aa2e98585de0942d1fb4f1ec599a39b Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 9 May 2012 17:02:07 +0000 Subject: [PATCH 1/2] Bugfix: getwork: NULL pindexPrev across CreateNewBlock, in case it fails --- rpc.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/rpc.cpp b/rpc.cpp index 113de85a9db..a579a5d905a 100644 --- a/rpc.cpp +++ b/rpc.cpp @@ -967,8 +967,13 @@ Value getwork(const Array& params, bool fHelp) delete pblock; vNewBlock.clear(); } + + // Clear pindexPrev so future getworks make a new block, despite any failures from here on + pindexPrev = NULL; + + // Store the pindexBest used before CreateNewBlock, to avoid races nTransactionsUpdatedLast = nTransactionsUpdated; - pindexPrev = pindexBest; + CBlockIndex* pindexPrevNew = pindexBest; nStart = GetTime(); // Create new block @@ -976,6 +981,9 @@ Value getwork(const Array& params, bool fHelp) if (!pblock) throw JSONRPCError(-7, "Out of memory"); vNewBlock.push_back(pblock); + + // Need to update only after we know CreateNewBlock succeeded + pindexPrev = pindexPrevNew; } // Update nTime From 5d53f48acbbba0ac80e8a26545a3ecaf8d139356 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 9 May 2012 17:08:39 +0000 Subject: [PATCH 2/2] Bugfix: getmemorypool: NULL pindexPrev across CreateNewBlock, in case it fails --- src/bitcoinrpc.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 05913cd0ffb..d8d65e7f426 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1760,16 +1760,26 @@ Value getmemorypool(const Array& params, bool fHelp) if (pindexPrev != pindexBest || (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 5)) { + // Clear pindexPrev so future calls make a new block, despite any failures from here on + pindexPrev = NULL; + + // Store the pindexBest used before CreateNewBlock, to avoid races nTransactionsUpdatedLast = nTransactionsUpdated; - pindexPrev = pindexBest; + CBlockIndex* pindexPrevNew = pindexBest; nStart = GetTime(); // Create new block if(pblock) + { delete pblock; + pblock = NULL; + } pblock = CreateNewBlock(reservekey); if (!pblock) throw JSONRPCError(-7, "Out of memory"); + + // Need to update only after we know CreateNewBlock succeeded + pindexPrev = pindexPrevNew; } // Update nTime