Skip to content
Bitcoin Roots

v29.4-roots.2 · Commit 7: [PATCH 07/21] 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. The two line-number columns refer to before and after.

src/node/miner.cpp CHANGED
@@ -25,7 +25,10 @@
25
25
  #include <validation.h>
26
26
 
27
27
  #include <algorithm>
28
+ #include <cassert>
29
+ #include <map>
28
30
  #include <utility>
31
+ #include <vector>
29
32
 
30
33
  namespace node {
31
34
 
@@ -93,6 +96,111 @@ BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool
93
96
  fNeedSizeAccounting = m_options.nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE;
94
97
  }
95
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
+
96
204
  void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
97
205
  {
98
206
  // Block resource limits
@@ -165,8 +273,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
165
273
  if (m_mempool) {
166
274
  LOCK(m_mempool->cs);
167
275
  addPriorityTxs(*m_mempool, nPackagesSelected);
168
- // addPackageTxs acquires the same recursive mempool lock.
169
- addPackageTxs(nPackagesSelected, nDescendantsUpdated);
276
+ addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
170
277
  }
171
278
 
172
279
  const auto time_1{SteadyClock::now()};
@@ -325,10 +432,9 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
325
432
  // Each time through the loop, we compare the best transaction in
326
433
  // mapModifiedTxs with the next transaction in the mempool to decide what
327
434
  // transaction package to work on next.
328
- void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
435
+ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated)
329
436
  {
330
- const auto& mempool{*Assert(m_mempool)};
331
- LOCK(mempool.cs);
437
+ AssertLockHeld(mempool.cs);
332
438
 
333
439
  // mapModifiedTx will store sorted packages after they are modified
334
440
  // because some of their txs are already in the block