@@ -1556,6 +1556,43 @@ void static FlushBlockFile(bool fFinalize = false)
1556
1556
1557
1557
static bool FindUndoPos (CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
1558
1558
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
+
1559
1596
static CCheckQueue<CScriptCheck> scriptcheckqueue (128 );
1560
1597
1561
1598
void ThreadScriptCheck () {
@@ -1783,9 +1820,6 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
1783
1820
CAmount nFees = 0 ;
1784
1821
int nInputs = 0 ;
1785
1822
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 ());
1789
1823
blockundo.vtxundo .reserve (block.vtx .size () - 1 );
1790
1824
std::vector<PrecomputedTransactionData> txdata;
1791
1825
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
1846
1880
blockundo.vtxundo .push_back (CTxUndo ());
1847
1881
}
1848
1882
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);
1852
1883
}
1853
1884
int64_t nTime3 = GetTimeMicros (); nTimeConnect += nTime3 - nTime2;
1854
1885
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
1868
1899
if (fJustCheck )
1869
1900
return true ;
1870
1901
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 ;
1885
1904
1905
+ if (!pindex->IsValid (BLOCK_VALID_SCRIPTS)) {
1886
1906
pindex->RaiseValidity (BLOCK_VALID_SCRIPTS);
1887
1907
setDirtyBlockIndex.insert (pindex);
1888
1908
}
1889
1909
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 ;
1893
1912
1894
1913
assert (pindex->phashBlock );
1895
1914
// add this block to the view's block chain
0 commit comments