Skip to content

Commit 6fdd43b

Browse files
committed
Add struct to track block-connect-time-generated info for callbacks
1 parent 2efcfa5 commit 6fdd43b

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/validation.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,11 +2152,20 @@ static int64_t nTimeFlush = 0;
21522152
static int64_t nTimeChainState = 0;
21532153
static int64_t nTimePostConnect = 0;
21542154

2155+
/**
2156+
* Used to track conflicted transactions removed from mempool and transactions
2157+
* applied to the UTXO state as a part of a single ActivateBestChainStep call.
2158+
*/
2159+
struct ConnectTrace {
2160+
std::vector<CTransactionRef> txConflicted;
2161+
std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>> txChanged;
2162+
};
2163+
21552164
/**
21562165
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
21572166
* corresponding to pindexNew, to bypass loading it again from disk.
21582167
*/
2159-
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::vector<CTransactionRef> &txConflicted, std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>> &txChanged)
2168+
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, ConnectTrace& connectTrace)
21602169
{
21612170
assert(pindexNew->pprev == chainActive.Tip());
21622171
// Read block from disk.
@@ -2192,12 +2201,12 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
21922201
int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
21932202
LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
21942203
// Remove conflicting transactions from the mempool.;
2195-
mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, &txConflicted, !IsInitialBlockDownload());
2204+
mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, &connectTrace.txConflicted, !IsInitialBlockDownload());
21962205
// Update chainActive & related variables.
21972206
UpdateTip(pindexNew, chainparams);
21982207

21992208
for (unsigned int i=0; i < pblock->vtx.size(); i++)
2200-
txChanged.emplace_back(pblock->vtx[i], pindexNew, i);
2209+
connectTrace.txChanged.emplace_back(pblock->vtx[i], pindexNew, i);
22012210

22022211
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
22032212
LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
@@ -2279,7 +2288,7 @@ static void PruneBlockIndexCandidates() {
22792288
* Try to make some progress towards making pindexMostWork the active block.
22802289
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
22812290
*/
2282-
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::vector<CTransactionRef>& txConflicted, std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>>& txChanged)
2291+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
22832292
{
22842293
AssertLockHeld(cs_main);
22852294
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2312,7 +2321,7 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
23122321

23132322
// Connect new blocks.
23142323
BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2315-
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL, txConflicted, txChanged)) {
2324+
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL, connectTrace)) {
23162325
if (state.IsInvalid()) {
23172326
// The block violates a consensus rule.
23182327
if (!state.CorruptionPossible())
@@ -2380,17 +2389,13 @@ static void NotifyHeaderTip() {
23802389
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
23812390
CBlockIndex *pindexMostWork = NULL;
23822391
CBlockIndex *pindexNewTip = NULL;
2383-
std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>> txChanged;
2384-
if (pblock)
2385-
txChanged.reserve(pblock->vtx.size());
23862392
do {
2387-
txChanged.clear();
23882393
boost::this_thread::interruption_point();
23892394
if (ShutdownRequested())
23902395
break;
23912396

23922397
const CBlockIndex *pindexFork;
2393-
std::vector<CTransactionRef> txConflicted;
2398+
ConnectTrace connectTrace;
23942399
bool fInitialDownload;
23952400
{
23962401
LOCK(cs_main);
@@ -2404,7 +2409,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
24042409
return true;
24052410

24062411
bool fInvalidFound = false;
2407-
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound, txConflicted, txChanged))
2412+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound, connectTrace))
24082413
return false;
24092414

24102415
if (fInvalidFound) {
@@ -2421,13 +2426,13 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
24212426

24222427
// throw all transactions though the signal-interface
24232428
// while _not_ holding the cs_main lock
2424-
for (const auto& tx : txConflicted)
2429+
for (const auto& tx : connectTrace.txConflicted)
24252430
{
24262431
GetMainSignals().SyncTransaction(*tx, pindexNewTip, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
24272432
}
24282433
// ... and about transactions that got confirmed:
2429-
for (unsigned int i = 0; i < txChanged.size(); i++)
2430-
GetMainSignals().SyncTransaction(*std::get<0>(txChanged[i]), std::get<1>(txChanged[i]), std::get<2>(txChanged[i]));
2434+
for (unsigned int i = 0; i < connectTrace.txChanged.size(); i++)
2435+
GetMainSignals().SyncTransaction(*std::get<0>(connectTrace.txChanged[i]), std::get<1>(connectTrace.txChanged[i]), std::get<2>(connectTrace.txChanged[i]));
24312436

24322437
// Notify external listeners about the new tip.
24332438
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);

0 commit comments

Comments
 (0)