Skip to content

Commit e2edf95

Browse files
committed
Bugfix: make CreateNewBlock return pindexPrev
1 parent 6b04508 commit e2edf95

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

src/miner.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev)
8787
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
8888
}
8989

90-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
90+
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CBlockIndex*& pindexPrev)
9191
{
9292
// Create new block
9393
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
@@ -132,7 +132,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
132132

133133
{
134134
LOCK2(cs_main, mempool.cs);
135-
CBlockIndex* pindexPrev = chainActive.Tip();
135+
pindexPrev = chainActive.Tip();
136136
const int nHeight = pindexPrev->nHeight + 1;
137137
CCoinsViewCache view(pcoinsTip);
138138

@@ -385,14 +385,14 @@ bool static ScanHash(CBlockHeader *pblock, uint256 *phash)
385385
}
386386
}
387387

388-
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
388+
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CBlockIndex*& pindexPrev)
389389
{
390390
CPubKey pubkey;
391391
if (!reservekey.GetReservedKey(pubkey))
392392
return NULL;
393393

394394
CScript scriptPubKey = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
395-
return CreateNewBlock(scriptPubKey);
395+
return CreateNewBlock(scriptPubKey, pindexPrev);
396396
}
397397

398398
static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
@@ -452,9 +452,9 @@ bool MineBlock(CWallet *pwallet, uint256& hash)
452452
unsigned int nExtraNonce = 0;
453453

454454
while (true) {
455-
CBlockIndex *pindexPrev = chainActive.Tip(); // Actually needs cs_main...
455+
CBlockIndex *pindexPrev;
456456

457-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
457+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey, pindexPrev));
458458
if (!pblocktemplate.get()) {
459459
return false;
460460
}
@@ -497,9 +497,9 @@ void static BitcoinMiner(CWallet *pwallet)
497497
// Create new block
498498
//
499499
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
500-
CBlockIndex* pindexPrev = chainActive.Tip(); // Actually needs cs_main...
500+
CBlockIndex* pindexPrev;
501501

502-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
502+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey, pindexPrev));
503503
if (!pblocktemplate.get())
504504
{
505505
LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");

src/miner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads);
2727
/** Create a single block */
2828
bool MineBlock(CWallet *pwallet, uint256& hash);
2929
/** Generate a new block, without valid proof-of-work */
30-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
31-
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey);
30+
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CBlockIndex*& pindexPrev);
31+
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CBlockIndex*& pindexPrev);
3232
/** Modify the extranonce in a block */
3333
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
3434
void UpdateTime(CBlockHeader* block, const CBlockIndex* pindexPrev);

src/rpcmining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
475475

476476
// Store the pindexBest used before CreateNewBlock, to avoid races
477477
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
478-
CBlockIndex* pindexPrevNew = chainActive.Tip();
478+
CBlockIndex* pindexPrevNew;
479479
nStart = GetTime();
480480

481481
// Create new block
@@ -485,7 +485,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
485485
pblocktemplate = NULL;
486486
}
487487
CScript scriptDummy = CScript() << OP_TRUE;
488-
pblocktemplate = CreateNewBlock(scriptDummy);
488+
pblocktemplate = CreateNewBlock(scriptDummy, pindexPrevNew);
489489
if (!pblocktemplate)
490490
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
491491

src/test/miner_tests.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
6262
Checkpoints::fEnabled = false;
6363

6464
// Simple block creation, nothing special yet:
65-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
65+
CBlockIndex* pindexPrev;
66+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
6667

6768
// We can't make transactions until we have inputs
6869
// Therefore, load 100 blocks :)
@@ -90,7 +91,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
9091
delete pblocktemplate;
9192

9293
// Just to make sure we can still make simple blocks
93-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
94+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
9495
delete pblocktemplate;
9596

9697
// block sigops > limit: 1000 CHECKMULTISIG + 1
@@ -108,7 +109,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
108109
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
109110
tx.vin[0].prevout.hash = hash;
110111
}
111-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
112+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
112113
delete pblocktemplate;
113114
mempool.clear();
114115

@@ -128,14 +129,14 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
128129
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
129130
tx.vin[0].prevout.hash = hash;
130131
}
131-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
132+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
132133
delete pblocktemplate;
133134
mempool.clear();
134135

135136
// orphan in mempool
136137
hash = tx.GetHash();
137138
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
138-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
139+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
139140
delete pblocktemplate;
140141
mempool.clear();
141142

@@ -153,7 +154,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
153154
tx.vout[0].nValue = 5900000000LL;
154155
hash = tx.GetHash();
155156
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
156-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
157+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
157158
delete pblocktemplate;
158159
mempool.clear();
159160

@@ -164,7 +165,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
164165
tx.vout[0].nValue = 0;
165166
hash = tx.GetHash();
166167
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
167-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
168+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
168169
delete pblocktemplate;
169170
mempool.clear();
170171

@@ -182,7 +183,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
182183
tx.vout[0].nValue -= 1000000;
183184
hash = tx.GetHash();
184185
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
185-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
186+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
186187
delete pblocktemplate;
187188
mempool.clear();
188189

@@ -196,17 +197,17 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
196197
tx.vout[0].scriptPubKey = CScript() << OP_2;
197198
hash = tx.GetHash();
198199
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
199-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
200+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
200201
delete pblocktemplate;
201202
mempool.clear();
202203

203204
// subsidy changing
204205
int nHeight = chainActive.Height();
205206
chainActive.Tip()->nHeight = 209999;
206-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
207+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
207208
delete pblocktemplate;
208209
chainActive.Tip()->nHeight = 210000;
209-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
210+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
210211
delete pblocktemplate;
211212
chainActive.Tip()->nHeight = nHeight;
212213

@@ -238,7 +239,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
238239
mempool.addUnchecked(hash, CTxMemPoolEntry(tx2, 11, GetTime(), 111.0, 11));
239240
BOOST_CHECK(!IsFinalTx(tx2));
240241

241-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
242+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
242243

243244
// Neither tx should have make it into the template.
244245
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 1);
@@ -251,7 +252,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
251252
BOOST_CHECK(IsFinalTx(tx, chainActive.Tip()->nHeight + 1));
252253
BOOST_CHECK(IsFinalTx(tx2));
253254

254-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
255+
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey, pindexPrev));
255256
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
256257
delete pblocktemplate;
257258

0 commit comments

Comments
 (0)