@@ -27,23 +27,6 @@ bool fHavePruned = false;
27
27
bool fPruneMode = false ;
28
28
uint64_t nPruneTarget = 0 ;
29
29
30
- // TODO make namespace {
31
- RecursiveMutex cs_LastBlockFile;
32
- std::vector<CBlockFileInfo> vinfoBlockFile;
33
- int nLastBlockFile = 0 ;
34
- /* * Global flag to indicate we should check to see if there are
35
- * block/undo files that should be deleted. Set on startup
36
- * or if we allocate more file space when we're in prune mode
37
- */
38
- bool fCheckForPruning = false ;
39
-
40
- /* * Dirty block index entries. */
41
- std::set<CBlockIndex*> setDirtyBlockIndex;
42
-
43
- /* * Dirty block file entries. */
44
- std::set<int > setDirtyFileInfo;
45
- // } // namespace
46
-
47
30
static FILE* OpenUndoFile (const FlatFilePos& pos, bool fReadOnly = false );
48
31
static FlatFileSeq BlockFileSeq ();
49
32
static FlatFileSeq UndoFileSeq ();
@@ -86,7 +69,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block)
86
69
if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork )
87
70
pindexBestHeader = pindexNew;
88
71
89
- setDirtyBlockIndex .insert (pindexNew);
72
+ m_dirty_blockindex .insert (pindexNew);
90
73
91
74
return pindexNew;
92
75
}
@@ -104,7 +87,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
104
87
pindex->nFile = 0 ;
105
88
pindex->nDataPos = 0 ;
106
89
pindex->nUndoPos = 0 ;
107
- setDirtyBlockIndex .insert (pindex);
90
+ m_dirty_blockindex .insert (pindex);
108
91
109
92
// Prune from m_blocks_unlinked -- any block we prune would have
110
93
// to be downloaded again in order to consider its chain, at which
@@ -121,8 +104,8 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
121
104
}
122
105
}
123
106
124
- vinfoBlockFile [fileNumber].SetNull ();
125
- setDirtyFileInfo .insert (fileNumber);
107
+ m_blockfile_info [fileNumber].SetNull ();
108
+ m_dirty_fileinfo .insert (fileNumber);
126
109
}
127
110
128
111
void BlockManager::FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
@@ -137,8 +120,8 @@ void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nM
137
120
// last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
138
121
unsigned int nLastBlockWeCanPrune = std::min ((unsigned )nManualPruneHeight, chain_tip_height - MIN_BLOCKS_TO_KEEP);
139
122
int count = 0 ;
140
- for (int fileNumber = 0 ; fileNumber < nLastBlockFile ; fileNumber++) {
141
- if (vinfoBlockFile [fileNumber].nSize == 0 || vinfoBlockFile [fileNumber].nHeightLast > nLastBlockWeCanPrune) {
123
+ for (int fileNumber = 0 ; fileNumber < m_last_blockfile ; fileNumber++) {
124
+ if (m_blockfile_info [fileNumber].nSize == 0 || m_blockfile_info [fileNumber].nHeightLast > nLastBlockWeCanPrune) {
142
125
continue ;
143
126
}
144
127
PruneOneBlockFile (fileNumber);
@@ -177,10 +160,10 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
177
160
nBuffer += nPruneTarget / 10 ;
178
161
}
179
162
180
- for (int fileNumber = 0 ; fileNumber < nLastBlockFile ; fileNumber++) {
181
- nBytesToPrune = vinfoBlockFile [fileNumber].nSize + vinfoBlockFile [fileNumber].nUndoSize ;
163
+ for (int fileNumber = 0 ; fileNumber < m_last_blockfile ; fileNumber++) {
164
+ nBytesToPrune = m_blockfile_info [fileNumber].nSize + m_blockfile_info [fileNumber].nUndoSize ;
182
165
183
- if (vinfoBlockFile [fileNumber].nSize == 0 ) {
166
+ if (m_blockfile_info [fileNumber].nSize == 0 ) {
184
167
continue ;
185
168
}
186
169
@@ -189,7 +172,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
189
172
}
190
173
191
174
// don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
192
- if (vinfoBlockFile [fileNumber].nHeightLast > nLastBlockWeCanPrune) {
175
+ if (m_blockfile_info [fileNumber].nHeightLast > nLastBlockWeCanPrune) {
193
176
continue ;
194
177
}
195
178
@@ -290,7 +273,7 @@ bool BlockManager::LoadBlockIndex(
290
273
}
291
274
if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev ->nStatus & BLOCK_FAILED_MASK)) {
292
275
pindex->nStatus |= BLOCK_FAILED_CHILD;
293
- setDirtyBlockIndex .insert (pindex);
276
+ m_dirty_blockindex .insert (pindex);
294
277
}
295
278
if (pindex->IsAssumedValid () ||
296
279
(pindex->IsValid (BLOCK_VALID_TRANSACTIONS) &&
@@ -348,6 +331,31 @@ void BlockManager::Unload()
348
331
}
349
332
350
333
m_block_index.clear ();
334
+
335
+ m_blockfile_info.clear ();
336
+ m_last_blockfile = 0 ;
337
+ m_dirty_blockindex.clear ();
338
+ m_dirty_fileinfo.clear ();
339
+ }
340
+
341
+ bool BlockManager::WriteBlockIndexDB ()
342
+ {
343
+ std::vector<std::pair<int , const CBlockFileInfo*>> vFiles;
344
+ vFiles.reserve (m_dirty_fileinfo.size ());
345
+ for (std::set<int >::iterator it = m_dirty_fileinfo.begin (); it != m_dirty_fileinfo.end ();) {
346
+ vFiles.push_back (std::make_pair (*it, &m_blockfile_info[*it]));
347
+ m_dirty_fileinfo.erase (it++);
348
+ }
349
+ std::vector<const CBlockIndex*> vBlocks;
350
+ vBlocks.reserve (m_dirty_blockindex.size ());
351
+ for (std::set<CBlockIndex*>::iterator it = m_dirty_blockindex.begin (); it != m_dirty_blockindex.end ();) {
352
+ vBlocks.push_back (*it);
353
+ m_dirty_blockindex.erase (it++);
354
+ }
355
+ if (!m_block_tree_db->WriteBatchSync (vFiles, m_last_blockfile, vBlocks)) {
356
+ return false ;
357
+ }
358
+ return true ;
351
359
}
352
360
353
361
bool BlockManager::LoadBlockIndexDB (ChainstateManager& chainman)
@@ -357,17 +365,17 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
357
365
}
358
366
359
367
// Load block file info
360
- m_block_tree_db->ReadLastBlockFile (nLastBlockFile );
361
- vinfoBlockFile .resize (nLastBlockFile + 1 );
362
- LogPrintf (" %s: last block file = %i\n " , __func__, nLastBlockFile );
363
- for (int nFile = 0 ; nFile <= nLastBlockFile ; nFile++) {
364
- m_block_tree_db->ReadBlockFileInfo (nFile, vinfoBlockFile [nFile]);
365
- }
366
- LogPrintf (" %s: last block file info: %s\n " , __func__, vinfoBlockFile[nLastBlockFile ].ToString ());
367
- for (int nFile = nLastBlockFile + 1 ; true ; nFile++) {
368
+ m_block_tree_db->ReadLastBlockFile (m_last_blockfile );
369
+ m_blockfile_info .resize (m_last_blockfile + 1 );
370
+ LogPrintf (" %s: last block file = %i\n " , __func__, m_last_blockfile );
371
+ for (int nFile = 0 ; nFile <= m_last_blockfile ; nFile++) {
372
+ m_block_tree_db->ReadBlockFileInfo (nFile, m_blockfile_info [nFile]);
373
+ }
374
+ LogPrintf (" %s: last block file info: %s\n " , __func__, m_blockfile_info[m_last_blockfile ].ToString ());
375
+ for (int nFile = m_last_blockfile + 1 ; true ; nFile++) {
368
376
CBlockFileInfo info;
369
377
if (m_block_tree_db->ReadBlockFileInfo (nFile, info)) {
370
- vinfoBlockFile .push_back (info);
378
+ m_blockfile_info .push_back (info);
371
379
} else {
372
380
break ;
373
381
}
@@ -425,7 +433,7 @@ bool IsBlockPruned(const CBlockIndex* pblockindex)
425
433
// If we're using -prune with -reindex, then delete block files that will be ignored by the
426
434
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
427
435
// is missing, do the same here to delete any later block files after a gap. Also delete all
428
- // rev files since they'll be rewritten by the reindex anyway. This ensures that vinfoBlockFile
436
+ // rev files since they'll be rewritten by the reindex anyway. This ensures that m_blockfile_info
429
437
// is in sync with what's actually on disk by the time we start downloading, so that pruning
430
438
// works correctly.
431
439
void CleanupBlockRevFiles ()
@@ -470,11 +478,11 @@ std::string CBlockFileInfo::ToString() const
470
478
return strprintf (" CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)" , nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date (nTimeFirst), FormatISO8601Date (nTimeLast));
471
479
}
472
480
473
- CBlockFileInfo* GetBlockFileInfo (size_t n)
481
+ CBlockFileInfo* BlockManager:: GetBlockFileInfo (size_t n)
474
482
{
475
483
LOCK (cs_LastBlockFile);
476
484
477
- return &vinfoBlockFile .at (n);
485
+ return &m_blockfile_info .at (n);
478
486
}
479
487
480
488
static bool UndoWriteToDisk (const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
@@ -538,32 +546,32 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
538
546
return true ;
539
547
}
540
548
541
- static void FlushUndoFile (int block_file, bool finalize = false )
549
+ void BlockManager:: FlushUndoFile (int block_file, bool finalize)
542
550
{
543
- FlatFilePos undo_pos_old (block_file, vinfoBlockFile [block_file].nUndoSize );
551
+ FlatFilePos undo_pos_old (block_file, m_blockfile_info [block_file].nUndoSize );
544
552
if (!UndoFileSeq ().Flush (undo_pos_old, finalize)) {
545
553
AbortNode (" Flushing undo file to disk failed. This is likely the result of an I/O error." );
546
554
}
547
555
}
548
556
549
- void FlushBlockFile (bool fFinalize = false , bool finalize_undo = false )
557
+ void BlockManager:: FlushBlockFile (bool fFinalize , bool finalize_undo)
550
558
{
551
559
LOCK (cs_LastBlockFile);
552
- FlatFilePos block_pos_old (nLastBlockFile, vinfoBlockFile[nLastBlockFile ].nSize );
560
+ FlatFilePos block_pos_old (m_last_blockfile, m_blockfile_info[m_last_blockfile ].nSize );
553
561
if (!BlockFileSeq ().Flush (block_pos_old, fFinalize )) {
554
562
AbortNode (" Flushing block file to disk failed. This is likely the result of an I/O error." );
555
563
}
556
564
// we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
557
565
// e.g. during IBD or a sync after a node going offline
558
- if (!fFinalize || finalize_undo) FlushUndoFile (nLastBlockFile , finalize_undo);
566
+ if (!fFinalize || finalize_undo) FlushUndoFile (m_last_blockfile , finalize_undo);
559
567
}
560
568
561
- uint64_t CalculateCurrentUsage ()
569
+ uint64_t BlockManager:: CalculateCurrentUsage ()
562
570
{
563
571
LOCK (cs_LastBlockFile);
564
572
565
573
uint64_t retval = 0 ;
566
- for (const CBlockFileInfo& file : vinfoBlockFile ) {
574
+ for (const CBlockFileInfo& file : m_blockfile_info ) {
567
575
retval += file.nSize + file.nUndoSize ;
568
576
}
569
577
return retval;
@@ -605,44 +613,44 @@ fs::path GetBlockPosFilename(const FlatFilePos& pos)
605
613
return BlockFileSeq ().FileName (pos);
606
614
}
607
615
608
- bool FindBlockPos (FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false )
616
+ bool BlockManager:: FindBlockPos (FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown )
609
617
{
610
618
LOCK (cs_LastBlockFile);
611
619
612
- unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile ;
613
- if (vinfoBlockFile .size () <= nFile) {
614
- vinfoBlockFile .resize (nFile + 1 );
620
+ unsigned int nFile = fKnown ? pos.nFile : m_last_blockfile ;
621
+ if (m_blockfile_info .size () <= nFile) {
622
+ m_blockfile_info .resize (nFile + 1 );
615
623
}
616
624
617
625
bool finalize_undo = false ;
618
626
if (!fKnown ) {
619
- while (vinfoBlockFile [nFile].nSize + nAddSize >= (gArgs .GetBoolArg (" -fastprune" , false ) ? 0x10000 /* 64kb */ : MAX_BLOCKFILE_SIZE)) {
627
+ while (m_blockfile_info [nFile].nSize + nAddSize >= (gArgs .GetBoolArg (" -fastprune" , false ) ? 0x10000 /* 64kb */ : MAX_BLOCKFILE_SIZE)) {
620
628
// when the undo file is keeping up with the block file, we want to flush it explicitly
621
629
// when it is lagging behind (more blocks arrive than are being connected), we let the
622
630
// undo block write case handle it
623
- finalize_undo = (vinfoBlockFile [nFile].nHeightLast == (unsigned int )active_chain.Tip ()->nHeight );
631
+ finalize_undo = (m_blockfile_info [nFile].nHeightLast == (unsigned int )active_chain.Tip ()->nHeight );
624
632
nFile++;
625
- if (vinfoBlockFile .size () <= nFile) {
626
- vinfoBlockFile .resize (nFile + 1 );
633
+ if (m_blockfile_info .size () <= nFile) {
634
+ m_blockfile_info .resize (nFile + 1 );
627
635
}
628
636
}
629
637
pos.nFile = nFile;
630
- pos.nPos = vinfoBlockFile [nFile].nSize ;
638
+ pos.nPos = m_blockfile_info [nFile].nSize ;
631
639
}
632
640
633
- if ((int )nFile != nLastBlockFile ) {
641
+ if ((int )nFile != m_last_blockfile ) {
634
642
if (!fKnown ) {
635
- LogPrint (BCLog::BLOCKSTORE, " Leaving block file %i: %s\n " , nLastBlockFile, vinfoBlockFile[nLastBlockFile ].ToString ());
643
+ LogPrint (BCLog::BLOCKSTORE, " Leaving block file %i: %s\n " , m_last_blockfile, m_blockfile_info[m_last_blockfile ].ToString ());
636
644
}
637
645
FlushBlockFile (!fKnown , finalize_undo);
638
- nLastBlockFile = nFile;
646
+ m_last_blockfile = nFile;
639
647
}
640
648
641
- vinfoBlockFile [nFile].AddBlock (nHeight, nTime);
649
+ m_blockfile_info [nFile].AddBlock (nHeight, nTime);
642
650
if (fKnown ) {
643
- vinfoBlockFile [nFile].nSize = std::max (pos.nPos + nAddSize, vinfoBlockFile [nFile].nSize );
651
+ m_blockfile_info [nFile].nSize = std::max (pos.nPos + nAddSize, m_blockfile_info [nFile].nSize );
644
652
} else {
645
- vinfoBlockFile [nFile].nSize += nAddSize;
653
+ m_blockfile_info [nFile].nSize += nAddSize;
646
654
}
647
655
648
656
if (!fKnown ) {
@@ -652,31 +660,31 @@ bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight,
652
660
return AbortNode (" Disk space is too low!" , _ (" Disk space is too low!" ));
653
661
}
654
662
if (bytes_allocated != 0 && fPruneMode ) {
655
- fCheckForPruning = true ;
663
+ m_check_for_pruning = true ;
656
664
}
657
665
}
658
666
659
- setDirtyFileInfo .insert (nFile);
667
+ m_dirty_fileinfo .insert (nFile);
660
668
return true ;
661
669
}
662
670
663
- static bool FindUndoPos (BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
671
+ bool BlockManager:: FindUndoPos (BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
664
672
{
665
673
pos.nFile = nFile;
666
674
667
675
LOCK (cs_LastBlockFile);
668
676
669
- pos.nPos = vinfoBlockFile [nFile].nUndoSize ;
670
- vinfoBlockFile [nFile].nUndoSize += nAddSize;
671
- setDirtyFileInfo .insert (nFile);
677
+ pos.nPos = m_blockfile_info [nFile].nUndoSize ;
678
+ m_blockfile_info [nFile].nUndoSize += nAddSize;
679
+ m_dirty_fileinfo .insert (nFile);
672
680
673
681
bool out_of_space;
674
682
size_t bytes_allocated = UndoFileSeq ().Allocate (pos, nAddSize, out_of_space);
675
683
if (out_of_space) {
676
684
return AbortNode (state, " Disk space is too low!" , _ (" Disk space is too low!" ));
677
685
}
678
686
if (bytes_allocated != 0 && fPruneMode ) {
679
- fCheckForPruning = true ;
687
+ m_check_for_pruning = true ;
680
688
}
681
689
682
690
return true ;
@@ -705,7 +713,7 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
705
713
return true ;
706
714
}
707
715
708
- bool WriteUndoDataForBlock (const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
716
+ bool BlockManager:: WriteUndoDataForBlock (const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
709
717
{
710
718
// Write undo information to disk
711
719
if (pindex->GetUndoPos ().IsNull ()) {
@@ -721,14 +729,14 @@ bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& st
721
729
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up
722
730
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
723
731
// the FindBlockPos function
724
- if (_pos.nFile < nLastBlockFile && static_cast <uint32_t >(pindex->nHeight ) == vinfoBlockFile [_pos.nFile ].nHeightLast ) {
732
+ if (_pos.nFile < m_last_blockfile && static_cast <uint32_t >(pindex->nHeight ) == m_blockfile_info [_pos.nFile ].nHeightLast ) {
725
733
FlushUndoFile (_pos.nFile , true );
726
734
}
727
735
728
736
// update nUndoPos in block index
729
737
pindex->nUndoPos = _pos.nPos ;
730
738
pindex->nStatus |= BLOCK_HAVE_UNDO;
731
- setDirtyBlockIndex .insert (pindex);
739
+ m_dirty_blockindex .insert (pindex);
732
740
}
733
741
734
742
return true ;
@@ -825,7 +833,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex
825
833
}
826
834
827
835
/* * Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
828
- FlatFilePos SaveBlockToDisk (const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
836
+ FlatFilePos BlockManager:: SaveBlockToDisk (const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
829
837
{
830
838
unsigned int nBlockSize = ::GetSerializeSize (block, CLIENT_VERSION);
831
839
FlatFilePos blockPos;
0 commit comments