Changeset View
Changeset View
Standalone View
Standalone View
src/miner.cpp
Show First 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | |||||
getExcessiveBlockSizeSig(const Config &config) { | getExcessiveBlockSizeSig(const Config &config) { | ||||
std::string cbmsg = "/EB" + getSubVersionEB(config.GetMaxBlockSize()) + "/"; | std::string cbmsg = "/EB" + getSubVersionEB(config.GetMaxBlockSize()) + "/"; | ||||
const char *cbcstr = cbmsg.c_str(); | const char *cbcstr = cbmsg.c_str(); | ||||
std::vector<uint8_t> vec(cbcstr, cbcstr + cbmsg.size()); | std::vector<uint8_t> vec(cbcstr, cbcstr + cbmsg.size()); | ||||
return vec; | return vec; | ||||
} | } | ||||
std::unique_ptr<CBlockTemplate> | std::unique_ptr<CBlockTemplate> | ||||
BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | BlockAssembler::CreateNewBlock(const Config &config, | ||||
const CScript &scriptPubKeyIn) { | |||||
int64_t nTimeStart = GetTimeMicros(); | int64_t nTimeStart = GetTimeMicros(); | ||||
resetBlock(); | resetBlock(); | ||||
pblocktemplate.reset(new CBlockTemplate()); | pblocktemplate.reset(new CBlockTemplate()); | ||||
if (!pblocktemplate.get()) { | if (!pblocktemplate.get()) { | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
// Pointer for convenience. | // Pointer for convenience. | ||||
pblock = &pblocktemplate->block; | pblock = &pblocktemplate->block; | ||||
// Add dummy coinbase tx as first transaction. It is updated at the end. | // Add dummy coinbase tx as first transaction. It is updated at the end. | ||||
pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, 0, -1); | pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, 0, -1); | ||||
LOCK2(cs_main, mempool->cs); | LOCK2(cs_main, mempool->cs); | ||||
CBlockIndex *pindexPrev = chainActive.Tip(); | CBlockIndex *pindexPrev = chainActive.Tip(); | ||||
assert(pindexPrev != nullptr); | assert(pindexPrev != nullptr); | ||||
nHeight = pindexPrev->nHeight + 1; | nHeight = pindexPrev->nHeight + 1; | ||||
const CChainParams &chainparams = config->GetChainParams(); | const CChainParams &chainparams = config.GetChainParams(); | ||||
pblock->nVersion = | pblock->nVersion = | ||||
ComputeBlockVersion(pindexPrev, chainparams.GetConsensus()); | ComputeBlockVersion(pindexPrev, chainparams.GetConsensus()); | ||||
// -regtest only: allow overriding block.nVersion with | // -regtest only: allow overriding block.nVersion with | ||||
// -blockversion=N to test forking scenarios | // -blockversion=N to test forking scenarios | ||||
if (chainparams.MineBlocksOnDemand()) { | if (chainparams.MineBlocksOnDemand()) { | ||||
pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion); | pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion); | ||||
} | } | ||||
pblock->nTime = GetAdjustedTime(); | pblock->nTime = GetAdjustedTime(); | ||||
nMaxGeneratedBlockSize = ComputeMaxGeneratedBlockSize(*config, pindexPrev); | nMaxGeneratedBlockSize = ComputeMaxGeneratedBlockSize(config, pindexPrev); | ||||
nMedianTimePast = pindexPrev->GetMedianTimePast(); | nMedianTimePast = pindexPrev->GetMedianTimePast(); | ||||
nLockTimeCutoff = | nLockTimeCutoff = | ||||
(STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST) | (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST) | ||||
? nMedianTimePast | ? nMedianTimePast | ||||
: pblock->GetBlockTime(); | : pblock->GetBlockTime(); | ||||
addPriorityTxs(); | addPriorityTxs(); | ||||
int nPackagesSelected = 0; | int nPackagesSelected = 0; | ||||
int nDescendantsUpdated = 0; | int nDescendantsUpdated = 0; | ||||
addPackageTxs(nPackagesSelected, nDescendantsUpdated); | addPackageTxs(nPackagesSelected, nDescendantsUpdated); | ||||
if (IsMagneticAnomalyEnabled(*config, pindexPrev)) { | if (IsMagneticAnomalyEnabled(config, pindexPrev)) { | ||||
// If magnetic anomaly is enabled, we make sure transaction are | // If magnetic anomaly is enabled, we make sure transaction are | ||||
// canonically ordered. | // canonically ordered. | ||||
// FIXME: Use a zipped list. See T479 | // FIXME: Use a zipped list. See T479 | ||||
std::sort(std::begin(pblocktemplate->entries) + 1, | std::sort(std::begin(pblocktemplate->entries) + 1, | ||||
std::end(pblocktemplate->entries), | std::end(pblocktemplate->entries), | ||||
[](const CBlockTemplateEntry &a, const CBlockTemplateEntry &b) | [](const CBlockTemplateEntry &a, const CBlockTemplateEntry &b) | ||||
-> bool { return a.tx->GetId() < b.tx->GetId(); }); | -> bool { return a.tx->GetId() < b.tx->GetId(); }); | ||||
} | } | ||||
Show All 31 Lines | BlockAssembler::CreateNewBlock(const Config &config, | ||||
uint64_t nSerializeSize = | uint64_t nSerializeSize = | ||||
GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION); | GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION); | ||||
LogPrintf("CreateNewBlock(): total size: %u txs: %u fees: %ld sigops %d\n", | LogPrintf("CreateNewBlock(): total size: %u txs: %u fees: %ld sigops %d\n", | ||||
nSerializeSize, nBlockTx, nFees, nBlockSigOps); | nSerializeSize, nBlockTx, nFees, nBlockSigOps); | ||||
// Fill in header. | // Fill in header. | ||||
pblock->hashPrevBlock = pindexPrev->GetBlockHash(); | pblock->hashPrevBlock = pindexPrev->GetBlockHash(); | ||||
UpdateTime(pblock, *config, pindexPrev); | UpdateTime(pblock, config, pindexPrev); | ||||
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, *config); | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, config); | ||||
pblock->nNonce = 0; | pblock->nNonce = 0; | ||||
pblocktemplate->entries[0].txSigOps = GetSigOpCountWithoutP2SH( | pblocktemplate->entries[0].txSigOps = GetSigOpCountWithoutP2SH( | ||||
*pblocktemplate->entries[0].tx, STANDARD_SCRIPT_VERIFY_FLAGS); | *pblocktemplate->entries[0].tx, STANDARD_SCRIPT_VERIFY_FLAGS); | ||||
// Copy all the transactions into the block | // Copy all the transactions into the block | ||||
// FIXME: This should be removed as it is significant overhead. | // FIXME: This should be removed as it is significant overhead. | ||||
// See T479 | // See T479 | ||||
for (const CBlockTemplateEntry &tx : pblocktemplate->entries) { | for (const CBlockTemplateEntry &tx : pblocktemplate->entries) { | ||||
pblock->vtx.push_back(tx.tx); | pblock->vtx.push_back(tx.tx); | ||||
} | } | ||||
CValidationState state; | CValidationState state; | ||||
BlockValidationOptions validationOptions(false, false); | BlockValidationOptions validationOptions(false, false); | ||||
if (!TestBlockValidity(*config, state, *pblock, pindexPrev, | if (!TestBlockValidity(config, state, *pblock, pindexPrev, | ||||
validationOptions)) { | validationOptions)) { | ||||
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", | throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", | ||||
__func__, | __func__, | ||||
FormatStateMessage(state))); | FormatStateMessage(state))); | ||||
} | } | ||||
int64_t nTime2 = GetTimeMicros(); | int64_t nTime2 = GetTimeMicros(); | ||||
LogPrint(BCLog::BENCH, | LogPrint(BCLog::BENCH, | ||||
▲ Show 20 Lines • Show All 476 Lines • Show Last 20 Lines |