Skip to content

Commit

Permalink
Merge pull request bitcoin#3 from bitcoin-unlimited/dev_parallel
Browse files Browse the repository at this point in the history
Unsolved block's height is improperly updated by chain tip height
  • Loading branch information
ptschip committed Mar 1, 2017
2 parents 4de0c3d + 8109bb8 commit c9e06dc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3888,9 +3888,13 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
{
CScript expect = CScript() << nHeight;
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
!std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
return state.DoS(100, error("%s: block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
}
!std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin()))
{
int blockCoinbaseHeight = block.GetHeight();
uint256 hashp = block.hashPrevBlock;
uint256 hash = block.GetHash();
return state.DoS(100, error("%s: block height mismatch in coinbase, expected %d, got %d, block is %s, parent block is %s, pprev is %s", __func__,nHeight,blockCoinbaseHeight,hash.ToString(),hashp.ToString(), pindexPrev->phashBlock->ToString()), REJECT_INVALID, "bad-cb-height");
}
}

return true;
Expand Down
13 changes: 6 additions & 7 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s
LOCK2(cs_main, mempool.cs);
CBlockIndex* pindexPrev = chainActive.Tip();
const int nHeight = pindexPrev->nHeight + 1;
pblock->nTime = GetAdjustedTime();
// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
pblock->nTime = GetAdjustedTime();
const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();

pblock->nVersion = UnlimitedComputeBlockVersion(pindexPrev, chainparams.GetConsensus(),pblock->nTime);
Expand Down Expand Up @@ -361,11 +364,7 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s

pblock->vtx[0] = txNew;
pblocktemplate->vTxFees[0] = -nFees;

// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
pblock->nNonce = 0;
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);

Expand All @@ -392,7 +391,7 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s
return pblocktemplate.release();
}

void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
void IncrementExtraNonce(CBlock* pblock, unsigned int& nExtraNonce)
{
// Update nExtraNonce
static uint256 hashPrevBlock;
Expand All @@ -402,7 +401,7 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
hashPrevBlock = pblock->hashPrevBlock;
}
++nExtraNonce;
unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
unsigned int nHeight = pblock->GetHeight(); // Height first in coinbase required for block.version=2
CMutableTransaction txCoinbase(pblock->vtx[0]);

CScript script = (CScript() << nHeight << CScriptNum(nExtraNonce));
Expand Down
2 changes: 1 addition & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct CBlockTemplate
/** Generate a new block, without valid proof-of-work */
CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn);
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void IncrementExtraNonce(CBlock* pblock, unsigned int& nExtraNonce);
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

#endif // BITCOIN_MINER_H
12 changes: 12 additions & 0 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ class CBlock : public CBlockHeader
READWRITE(vtx);
}

uint64_t GetHeight() const // Returns the block's height as specified in its coinbase transaction
{
const CScript& sig = vtx[0].vin[0].scriptSig;
int numlen = sig[0];
if (numlen == OP_0) return 0;
if ((numlen >= OP_1) && (numlen <= OP_16)) return numlen-OP_1+1;
std::vector<unsigned char> heightScript(numlen);
copy(sig.begin()+1, sig.begin()+1+numlen,heightScript.begin());
CScriptNum coinbaseHeight(heightScript, false,numlen);
return coinbaseHeight.getint();
}

void SetNull()
{
CBlockHeader::SetNull();
Expand Down
12 changes: 6 additions & 6 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript, int nG
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
CBlock *pblock = &pblocktemplate->block;
{
LOCK(cs_main);
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
// LOCK(cs_main);
IncrementExtraNonce(pblock, nExtraNonce);
}
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
++pblock->nNonce;
Expand Down Expand Up @@ -525,9 +525,9 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
}

// Update block
static CBlockIndex* pindexPrev;
static int64_t nStart;
static CBlockTemplate* pblocktemplate;
static CBlockIndex* pindexPrev = NULL;
static int64_t nStart = 0;
static CBlockTemplate* pblocktemplate = NULL;
if (pindexPrev != chainActive.Tip() ||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
{
Expand Down Expand Up @@ -674,7 +674,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
result.push_back(Pair("sizelimit", (int64_t)maxGeneratedBlock));
result.push_back(Pair("curtime", pblock->GetBlockTime()));
result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
result.push_back(Pair("height", (int64_t)(pblock->GetHeight()))); // BU get the height directly from the block because pindexPrev could change if another block has come in.

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
block.vtx.push_back(tx);
// IncrementExtraNonce creates a valid coinbase and merkleRoot
unsigned int extraNonce = 0;
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
IncrementExtraNonce(&block, extraNonce);

while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;

Expand Down
2 changes: 1 addition & 1 deletion src/unlimited.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ void static BitcoinMiner(const CChainParams& chainparams)
return;
}
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
IncrementExtraNonce(pblock, nExtraNonce);

LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
Expand Down

0 comments on commit c9e06dc

Please sign in to comment.