v29.4-roots.4 · Commit 7: [PATCH 07/37] release: establish Roots maintenance and assurance
src/node/miner.cpp
Before: src/node/miner.cpp · After: src/node/miner.cpp
File metadata
index aabe3fef4d97e3fb612f1a37d4e40ef3b3be6ea9..e039935a8165fe7185cdc28939485bd83aa0d3be 100644
Download original patch · Release notes
Diff rows 1–142 of 142. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.
src/node/miner.cpp
CHANGED
|
@@ -25,7 +25,10 @@
|
|
| 25 |
#include <validation.h>
|
| 26 |
|
| 27 |
#include <algorithm>
|
|
|
|
|
|
|
| 28 |
#include <utility>
|
|
|
|
| 29 |
|
| 30 |
namespace node {
|
| 31 |
|
|
@@ -93,6 +96,111 @@ BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool
|
|
| 93 |
fNeedSizeAccounting = m_options.nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE;
|
| 94 |
}
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
|
| 97 |
{
|
| 98 |
// Block resource limits
|
|
@@ -165,8 +273,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
|
|
| 165 |
if (m_mempool) {
|
| 166 |
LOCK(m_mempool->cs);
|
| 167 |
addPriorityTxs(*m_mempool, nPackagesSelected);
|
| 168 |
-
// addPackageTxs acquires the same recursive mempool lock.
|
| 169 |
-
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
|
| 170 |
}
|
| 171 |
|
| 172 |
const auto time_1{SteadyClock::now()};
|
|
@@ -325,10 +432,9 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
|
|
| 325 |
// Each time through the loop, we compare the best transaction in
|
| 326 |
// mapModifiedTxs with the next transaction in the mempool to decide what
|
| 327 |
// transaction package to work on next.
|
| 328 |
-
void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
|
| 329 |
{
|
| 330 |
-
const auto& mempool{*Assert(m_mempool)};
|
| 331 |
-
LOCK(mempool.cs);
|
| 332 |
|
| 333 |
// mapModifiedTx will store sorted packages after they are modified
|
| 334 |
// because some of their txs are already in the block
|
|
|
|
| 25 |
#include <validation.h>
|
| 26 |
|
| 27 |
#include <algorithm>
|
| 28 |
+
#include <cassert>
|
| 29 |
+
#include <map>
|
| 30 |
#include <utility>
|
| 31 |
+
#include <vector>
|
| 32 |
|
| 33 |
namespace node {
|
| 34 |
|
|
|
|
| 96 |
fNeedSizeAccounting = m_options.nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE;
|
| 97 |
}
|
| 98 |
|
| 99 |
+
namespace {
|
| 100 |
+
using TxCoinAgePriority = std::pair<double, CTxMemPool::txiter>;
|
| 101 |
+
|
| 102 |
+
struct TxCoinAgePriorityCompare {
|
| 103 |
+
bool operator()(const TxCoinAgePriority& a, const TxCoinAgePriority& b) const
|
| 104 |
+
{
|
| 105 |
+
if (a.first == b.first) {
|
| 106 |
+
return CompareTxMemPoolEntryByScore{}(*b.second, *a.second);
|
| 107 |
+
}
|
| 108 |
+
return a.first < b.first;
|
| 109 |
+
}
|
| 110 |
+
};
|
| 111 |
+
} // namespace
|
| 112 |
+
|
| 113 |
+
bool BlockAssembler::isStillDependent(const CTxMemPool& mempool, CTxMemPool::txiter iter)
|
| 114 |
+
{
|
| 115 |
+
assert(iter != mempool.mapTx.end());
|
| 116 |
+
for (const auto& parent : iter->GetMemPoolParentsConst()) {
|
| 117 |
+
const auto parent_it{mempool.mapTx.iterator_to(parent)};
|
| 118 |
+
if (!inBlock.count(parent_it)) return true;
|
| 119 |
+
}
|
| 120 |
+
return false;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)
|
| 124 |
+
{
|
| 125 |
+
const auto package_size{iter->GetSizeWithAncestors()};
|
| 126 |
+
const int64_t package_sigops{iter->GetSigOpCostWithAncestors()};
|
| 127 |
+
if (!TestPackage(package_size, package_sigops)) {
|
| 128 |
+
if (nBlockWeight > m_options.nBlockMaxWeight - 400 || nBlockSigOpsCost > MAX_BLOCK_SIGOPS_COST - 8 || lastFewTxs > 50) {
|
| 129 |
+
blockFinished = true;
|
| 130 |
+
return false;
|
| 131 |
+
}
|
| 132 |
+
if (nBlockWeight > m_options.nBlockMaxWeight - 4000) ++lastFewTxs;
|
| 133 |
+
return false;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
CTxMemPool::setEntries package;
|
| 137 |
+
package.insert(iter);
|
| 138 |
+
if (!TestPackageTransactions(package)) {
|
| 139 |
+
if (nBlockSize > m_options.nBlockMaxSize - 100 || lastFewTxs > 50) {
|
| 140 |
+
blockFinished = true;
|
| 141 |
+
return false;
|
| 142 |
+
}
|
| 143 |
+
if (nBlockSize > m_options.nBlockMaxSize - 1000) ++lastFewTxs;
|
| 144 |
+
return false;
|
| 145 |
+
}
|
| 146 |
+
return true;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
void BlockAssembler::addPriorityTxs(const CTxMemPool& mempool, int& packages_selected)
|
| 150 |
+
{
|
| 151 |
+
AssertLockHeld(mempool.cs);
|
| 152 |
+
|
| 153 |
+
uint64_t priority_size{static_cast<uint64_t>(gArgs.GetIntArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE))};
|
| 154 |
+
priority_size = std::min<uint64_t>(priority_size, m_options.nBlockMaxSize);
|
| 155 |
+
if (priority_size == 0) return;
|
| 156 |
+
|
| 157 |
+
const bool previous_size_accounting{fNeedSizeAccounting};
|
| 158 |
+
fNeedSizeAccounting = true;
|
| 159 |
+
|
| 160 |
+
std::vector<TxCoinAgePriority> priorities;
|
| 161 |
+
std::map<CTxMemPool::txiter, double, CompareIteratorByHash> waiting;
|
| 162 |
+
priorities.reserve(mempool.mapTx.size());
|
| 163 |
+
for (auto iter{mempool.mapTx.begin()}; iter != mempool.mapTx.end(); ++iter) {
|
| 164 |
+
double priority{iter->GetPriority(nHeight)};
|
| 165 |
+
CAmount fee_delta{0};
|
| 166 |
+
mempool.ApplyDeltas(iter->GetTx().GetHash(), priority, fee_delta);
|
| 167 |
+
priorities.emplace_back(priority, iter);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
TxCoinAgePriorityCompare compare;
|
| 171 |
+
std::make_heap(priorities.begin(), priorities.end(), compare);
|
| 172 |
+
while (!priorities.empty() && !blockFinished) {
|
| 173 |
+
const CTxMemPool::txiter iter{priorities.front().second};
|
| 174 |
+
const double priority{priorities.front().first};
|
| 175 |
+
std::pop_heap(priorities.begin(), priorities.end(), compare);
|
| 176 |
+
priorities.pop_back();
|
| 177 |
+
|
| 178 |
+
if (inBlock.count(iter)) {
|
| 179 |
+
assert(false);
|
| 180 |
+
continue;
|
| 181 |
+
}
|
| 182 |
+
if (isStillDependent(mempool, iter)) {
|
| 183 |
+
waiting.emplace(iter, priority);
|
| 184 |
+
continue;
|
| 185 |
+
}
|
| 186 |
+
if (!TestForBlock(iter)) continue;
|
| 187 |
+
|
| 188 |
+
AddToBlock(iter);
|
| 189 |
+
++packages_selected;
|
| 190 |
+
if (nBlockSize >= priority_size || priority <= MINIMUM_TX_PRIORITY) break;
|
| 191 |
+
|
| 192 |
+
for (const auto& child : iter->GetMemPoolChildrenConst()) {
|
| 193 |
+
const auto child_iter{mempool.mapTx.iterator_to(child)};
|
| 194 |
+
const auto waiting_iter{waiting.find(child_iter)};
|
| 195 |
+
if (waiting_iter == waiting.end()) continue;
|
| 196 |
+
priorities.emplace_back(waiting_iter->second, child_iter);
|
| 197 |
+
std::push_heap(priorities.begin(), priorities.end(), compare);
|
| 198 |
+
waiting.erase(waiting_iter);
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
fNeedSizeAccounting = previous_size_accounting;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
|
| 205 |
{
|
| 206 |
// Block resource limits
|
|
|
|
| 273 |
if (m_mempool) {
|
| 274 |
LOCK(m_mempool->cs);
|
| 275 |
addPriorityTxs(*m_mempool, nPackagesSelected);
|
| 276 |
+
addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
|
|
|
|
| 277 |
}
|
| 278 |
|
| 279 |
const auto time_1{SteadyClock::now()};
|
|
|
|
| 432 |
// Each time through the loop, we compare the best transaction in
|
| 433 |
// mapModifiedTxs with the next transaction in the mempool to decide what
|
| 434 |
// transaction package to work on next.
|
| 435 |
+
void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated)
|
| 436 |
{
|
| 437 |
+
AssertLockHeld(mempool.cs);
|
|
|
|
| 438 |
|
| 439 |
// mapModifiedTx will store sorted packages after they are modified
|
| 440 |
// because some of their txs are already in the block
|