Changeset View
Changeset View
Standalone View
Standalone View
src/miner.cpp
Show All 26 Lines | |||||
#include "utilmoneystr.h" | #include "utilmoneystr.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
#include "validationinterface.h" | #include "validationinterface.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <queue> | #include <queue> | ||||
#include <utility> | #include <utility> | ||||
#include <boost/thread.hpp> | |||||
#include <boost/tuple/tuple.hpp> | |||||
////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////// | ||||
// | // | ||||
// BitcoinMiner | // BitcoinMiner | ||||
// | // | ||||
// | // | ||||
// Unconfirmed transactions in the memory pool often depend on other | // Unconfirmed transactions in the memory pool often depend on other | ||||
// transactions in the memory pool. When we select transactions from the | // transactions in the memory pool. When we select transactions from the | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
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. | // Add dummy coinbase tx as first transaction. It is updated at the end. | ||||
pblock->vtx.emplace_back(); | pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, -1); | ||||
// updated at end | |||||
pblocktemplate->vTxFees.push_back(-SATOSHI); | |||||
// updated at end | |||||
pblocktemplate->vTxSigOpsCount.push_back(-1); | |||||
LOCK2(cs_main, mpool->cs); | LOCK2(cs_main, mpool->cs); | ||||
CBlockIndex *pindexPrev = chainActive.Tip(); | CBlockIndex *pindexPrev = chainActive.Tip(); | ||||
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()); | ||||
Show All 15 Lines | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
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. | ||||
std::sort(std::begin(pblock->vtx) + 1, std::end(pblock->vtx), | std::sort(std::begin(pblocktemplate->entries) + 1, | ||||
[](const std::shared_ptr<const CTransaction> &a, | std::end(pblocktemplate->entries), | ||||
const std::shared_ptr<const CTransaction> &b) -> bool { | [](const CBlockTemplateEntry &a, const CBlockTemplateEntry &b) | ||||
return a->GetId() < b->GetId(); | -> bool { return a.tx->GetId() < b.tx->GetId(); }); | ||||
}); | |||||
} | } | ||||
int64_t nTime1 = GetTimeMicros(); | int64_t nTime1 = GetTimeMicros(); | ||||
nLastBlockTx = nBlockTx; | nLastBlockTx = nBlockTx; | ||||
nLastBlockSize = nBlockSize; | nLastBlockSize = nBlockSize; | ||||
// Create coinbase transaction. | // Create coinbase transaction. | ||||
Show All 9 Lines | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
// Make sure the coinbase is big enough. | // Make sure the coinbase is big enough. | ||||
uint64_t coinbaseSize = | uint64_t coinbaseSize = | ||||
::GetSerializeSize(coinbaseTx, SER_NETWORK, PROTOCOL_VERSION); | ::GetSerializeSize(coinbaseTx, SER_NETWORK, PROTOCOL_VERSION); | ||||
if (coinbaseSize < MIN_TX_SIZE) { | if (coinbaseSize < MIN_TX_SIZE) { | ||||
coinbaseTx.vin[0].scriptSig | coinbaseTx.vin[0].scriptSig | ||||
<< std::vector<uint8_t>(MIN_TX_SIZE - coinbaseSize - 1); | << std::vector<uint8_t>(MIN_TX_SIZE - coinbaseSize - 1); | ||||
} | } | ||||
pblock->vtx[0] = MakeTransactionRef(coinbaseTx); | pblocktemplate->entries[0].tx = MakeTransactionRef(coinbaseTx); | ||||
pblocktemplate->vTxFees[0] = -1 * nFees; | pblocktemplate->entries[0].fees = -1 * nFees; | ||||
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->vTxSigOpsCount[0] = GetSigOpCountWithoutP2SH( | pblocktemplate->entries[0].sigOpCount = GetSigOpCountWithoutP2SH( | ||||
*pblock->vtx[0], STANDARD_CHECKDATASIG_VERIFY_FLAGS); | *pblocktemplate->entries[0].tx, STANDARD_CHECKDATASIG_VERIFY_FLAGS); | ||||
// Copy all the transactions into the block | |||||
for (const CBlockTemplateEntry &tx : pblocktemplate->entries) { | |||||
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))); | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | if (!ContextualCheckTransaction(*config, it->GetTx(), state, nHeight, | ||||
nLockTimeCutoff, nMedianTimePast)) { | nLockTimeCutoff, nMedianTimePast)) { | ||||
return TestForBlockResult::TXCantFit; | return TestForBlockResult::TXCantFit; | ||||
} | } | ||||
return TestForBlockResult::TXFits; | return TestForBlockResult::TXFits; | ||||
} | } | ||||
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { | void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { | ||||
pblock->vtx.emplace_back(iter->GetSharedTx()); | pblocktemplate->entries.push_back(CBlockTemplateEntry( | ||||
pblocktemplate->vTxFees.push_back(iter->GetFee()); | iter->GetSharedTx(), iter->GetFee(), iter->GetSigOpCount())); | ||||
pblocktemplate->vTxSigOpsCount.push_back(iter->GetSigOpCount()); | |||||
nBlockSize += iter->GetTxSize(); | nBlockSize += iter->GetTxSize(); | ||||
++nBlockTx; | ++nBlockTx; | ||||
nBlockSigOps += iter->GetSigOpCount(); | nBlockSigOps += iter->GetSigOpCount(); | ||||
nFees += iter->GetFee(); | nFees += iter->GetFee(); | ||||
inBlock.insert(iter); | inBlock.insert(iter); | ||||
bool fPrintPriority = | bool fPrintPriority = | ||||
gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY); | gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY); | ||||
▲ Show 20 Lines • Show All 339 Lines • Show Last 20 Lines |