Skip to content

Commit 50701ba

Browse files
committed
Move txindex/undo data disk location stuff out of ConnectBlock
1 parent 93a34cf commit 50701ba

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

src/validation.cpp

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,43 @@ void static FlushBlockFile(bool fFinalize = false)
15561556

15571557
static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
15581558

1559+
static bool WriteUndoDataForBlock(const CBlockUndo& blockundo, CValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
1560+
{
1561+
// Write undo information to disk
1562+
if (pindex->GetUndoPos().IsNull()) {
1563+
CDiskBlockPos _pos;
1564+
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1565+
return error("ConnectBlock(): FindUndoPos failed");
1566+
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
1567+
return AbortNode(state, "Failed to write undo data");
1568+
1569+
// update nUndoPos in block index
1570+
pindex->nUndoPos = _pos.nPos;
1571+
pindex->nStatus |= BLOCK_HAVE_UNDO;
1572+
setDirtyBlockIndex.insert(pindex);
1573+
}
1574+
1575+
return true;
1576+
}
1577+
1578+
static bool WriteTxIndexDataForBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex)
1579+
{
1580+
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1581+
std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1582+
vPos.reserve(block.vtx.size());
1583+
for (const CTransactionRef& tx : block.vtx)
1584+
{
1585+
vPos.push_back(std::make_pair(tx->GetHash(), pos));
1586+
pos.nTxOffset += ::GetSerializeSize(*tx, SER_DISK, CLIENT_VERSION);
1587+
}
1588+
1589+
if (fTxIndex)
1590+
if (!pblocktree->WriteTxIndex(vPos))
1591+
return AbortNode(state, "Failed to write transaction index");
1592+
1593+
return true;
1594+
}
1595+
15591596
static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
15601597

15611598
void ThreadScriptCheck() {
@@ -1783,9 +1820,6 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
17831820
CAmount nFees = 0;
17841821
int nInputs = 0;
17851822
int64_t nSigOpsCost = 0;
1786-
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1787-
std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1788-
vPos.reserve(block.vtx.size());
17891823
blockundo.vtxundo.reserve(block.vtx.size() - 1);
17901824
std::vector<PrecomputedTransactionData> txdata;
17911825
txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
@@ -1846,9 +1880,6 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
18461880
blockundo.vtxundo.push_back(CTxUndo());
18471881
}
18481882
UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
1849-
1850-
vPos.push_back(std::make_pair(tx.GetHash(), pos));
1851-
pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
18521883
}
18531884
int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
18541885
LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);
@@ -1868,28 +1899,16 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
18681899
if (fJustCheck)
18691900
return true;
18701901

1871-
// Write undo information to disk
1872-
if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
1873-
{
1874-
if (pindex->GetUndoPos().IsNull()) {
1875-
CDiskBlockPos _pos;
1876-
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1877-
return error("ConnectBlock(): FindUndoPos failed");
1878-
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
1879-
return AbortNode(state, "Failed to write undo data");
1880-
1881-
// update nUndoPos in block index
1882-
pindex->nUndoPos = _pos.nPos;
1883-
pindex->nStatus |= BLOCK_HAVE_UNDO;
1884-
}
1902+
if (!WriteUndoDataForBlock(blockundo, state, pindex, chainparams))
1903+
return false;
18851904

1905+
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
18861906
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
18871907
setDirtyBlockIndex.insert(pindex);
18881908
}
18891909

1890-
if (fTxIndex)
1891-
if (!pblocktree->WriteTxIndex(vPos))
1892-
return AbortNode(state, "Failed to write transaction index");
1910+
if (!WriteTxIndexDataForBlock(block, state, pindex))
1911+
return false;
18931912

18941913
assert(pindex->phashBlock);
18951914
// add this block to the view's block chain

0 commit comments

Comments
 (0)