Changeset View
Changeset View
Standalone View
Standalone View
src/miner.cpp
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | return GetVirtualTransactionSize(nSizeWithAncestors, | ||||
nSigOpCountWithAncestors); | nSigOpCountWithAncestors); | ||||
} | } | ||||
BlockAssembler::Options::Options() | BlockAssembler::Options::Options() | ||||
: nExcessiveBlockSize(DEFAULT_MAX_BLOCK_SIZE), | : nExcessiveBlockSize(DEFAULT_MAX_BLOCK_SIZE), | ||||
nMaxGeneratedBlockSize(DEFAULT_MAX_GENERATED_BLOCK_SIZE), | nMaxGeneratedBlockSize(DEFAULT_MAX_GENERATED_BLOCK_SIZE), | ||||
blockMinFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB) {} | blockMinFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB) {} | ||||
BlockAssembler::BlockAssembler(const CChainParams ¶ms, | BlockAssembler::BlockAssembler(CChainState &chainstate, | ||||
const CChainParams ¶ms, | |||||
const CTxMemPool &mempool, | const CTxMemPool &mempool, | ||||
const Options &options) | const Options &options) | ||||
: chainParams(params), m_mempool(mempool) { | : chainParams(params), m_mempool(mempool), m_chainstate(chainstate) { | ||||
blockMinFeeRate = options.blockMinFeeRate; | blockMinFeeRate = options.blockMinFeeRate; | ||||
// Limit size to between 1K and options.nExcessiveBlockSize -1K for sanity: | // Limit size to between 1K and options.nExcessiveBlockSize -1K for sanity: | ||||
nMaxGeneratedBlockSize = std::max<uint64_t>( | nMaxGeneratedBlockSize = std::max<uint64_t>( | ||||
1000, std::min<uint64_t>(options.nExcessiveBlockSize - 1000, | 1000, std::min<uint64_t>(options.nExcessiveBlockSize - 1000, | ||||
options.nMaxGeneratedBlockSize)); | options.nMaxGeneratedBlockSize)); | ||||
// Calculate the max consensus sigchecks for this block. | // Calculate the max consensus sigchecks for this block. | ||||
auto nMaxBlockSigChecks = GetMaxBlockSigChecksCount(nMaxGeneratedBlockSize); | auto nMaxBlockSigChecks = GetMaxBlockSigChecksCount(nMaxGeneratedBlockSize); | ||||
// Allow the full amount of signature check operations in lieu of a separate | // Allow the full amount of signature check operations in lieu of a separate | ||||
Show All 21 Lines | static BlockAssembler::Options DefaultOptions(const Config &config) { | ||||
if (gArgs.IsArgSet("-blockmintxfee") && | if (gArgs.IsArgSet("-blockmintxfee") && | ||||
ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) { | ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) { | ||||
options.blockMinFeeRate = CFeeRate(n); | options.blockMinFeeRate = CFeeRate(n); | ||||
} | } | ||||
return options; | return options; | ||||
} | } | ||||
BlockAssembler::BlockAssembler(const Config &config, const CTxMemPool &mempool) | BlockAssembler::BlockAssembler(const Config &config, CChainState &chainstate, | ||||
: BlockAssembler(config.GetChainParams(), mempool, DefaultOptions(config)) { | const CTxMemPool &mempool) | ||||
} | : BlockAssembler(chainstate, config.GetChainParams(), mempool, | ||||
DefaultOptions(config)) {} | |||||
void BlockAssembler::resetBlock() { | void BlockAssembler::resetBlock() { | ||||
inBlock.clear(); | inBlock.clear(); | ||||
// Reserve space for coinbase tx. | // Reserve space for coinbase tx. | ||||
nBlockSize = 1000; | nBlockSize = 1000; | ||||
nBlockSigOps = 100; | nBlockSigOps = 100; | ||||
// These counters do not include coinbase tx. | // These counters do not include coinbase tx. | ||||
nBlockTx = 0; | nBlockTx = 0; | ||||
nFees = Amount::zero(); | nFees = Amount::zero(); | ||||
} | } | ||||
std::optional<int64_t> BlockAssembler::m_last_block_num_txs{std::nullopt}; | std::optional<int64_t> BlockAssembler::m_last_block_num_txs{std::nullopt}; | ||||
std::optional<int64_t> BlockAssembler::m_last_block_size{std::nullopt}; | std::optional<int64_t> BlockAssembler::m_last_block_size{std::nullopt}; | ||||
std::unique_ptr<CBlockTemplate> | std::unique_ptr<CBlockTemplate> | ||||
BlockAssembler::CreateNewBlock(CChainState &chainstate, | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
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. | ||||
CBlock *const pblock = &pblocktemplate->block; | CBlock *const 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, -1); | pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, -1); | ||||
LOCK2(cs_main, m_mempool.cs); | LOCK2(cs_main, m_mempool.cs); | ||||
assert(std::addressof(*::ChainActive().Tip()) == | assert(std::addressof(*::ChainActive().Tip()) == | ||||
std::addressof(*chainstate.m_chain.Tip())); | std::addressof(*m_chainstate.m_chain.Tip())); | ||||
CBlockIndex *pindexPrev = chainstate.m_chain.Tip(); | CBlockIndex *pindexPrev = m_chainstate.m_chain.Tip(); | ||||
assert(pindexPrev != nullptr); | assert(pindexPrev != nullptr); | ||||
nHeight = pindexPrev->nHeight + 1; | nHeight = pindexPrev->nHeight + 1; | ||||
const Consensus::Params &consensusParams = chainParams.GetConsensus(); | const Consensus::Params &consensusParams = chainParams.GetConsensus(); | ||||
pblock->nVersion = ComputeBlockVersion(pindexPrev, consensusParams); | pblock->nVersion = ComputeBlockVersion(pindexPrev, consensusParams); | ||||
// -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 | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
// Fill in header. | // Fill in header. | ||||
pblock->hashPrevBlock = pindexPrev->GetBlockHash(); | pblock->hashPrevBlock = pindexPrev->GetBlockHash(); | ||||
UpdateTime(pblock, chainParams, pindexPrev); | UpdateTime(pblock, chainParams, pindexPrev); | ||||
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainParams); | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainParams); | ||||
pblock->nNonce = 0; | pblock->nNonce = 0; | ||||
pblocktemplate->entries[0].sigOpCount = 0; | pblocktemplate->entries[0].sigOpCount = 0; | ||||
BlockValidationState state; | BlockValidationState state; | ||||
assert(std::addressof(::ChainstateActive()) == std::addressof(chainstate)); | assert(std::addressof(::ChainstateActive()) == | ||||
if (!TestBlockValidity(state, chainParams, chainstate, *pblock, pindexPrev, | std::addressof(m_chainstate)); | ||||
if (!TestBlockValidity(state, chainParams, m_chainstate, *pblock, | |||||
pindexPrev, | |||||
BlockValidationOptions(nMaxGeneratedBlockSize) | BlockValidationOptions(nMaxGeneratedBlockSize) | ||||
.withCheckPoW(false) | .withCheckPoW(false) | ||||
.withCheckMerkleRoot(false))) { | .withCheckMerkleRoot(false))) { | ||||
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", | throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", | ||||
__func__, state.ToString())); | __func__, state.ToString())); | ||||
} | } | ||||
int64_t nTime2 = GetTimeMicros(); | int64_t nTime2 = GetTimeMicros(); | ||||
▲ Show 20 Lines • Show All 343 Lines • Show Last 20 Lines |