MCPcopy Create free account
hub / github.com/LUX-Core/lux / LoadExternalBlockFile

Function LoadExternalBlockFile

src/main.cpp:5791–5902  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5789}
5790
5791bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos* dbp)
5792{
5793 // Map of disk positions for blocks with unknown parent (only used for reindex)
5794 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
5795 int64_t nStart = GetTimeMillis();
5796
5797 int nLoaded = 0;
5798 try {
5799 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
5800 CBufferedFile blkdat(fileIn, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8, SER_DISK, CLIENT_VERSION);
5801 uint64_t nRewind = blkdat.GetPos();
5802 while (!blkdat.eof()) {
5803 boost::this_thread::interruption_point();
5804
5805 blkdat.SetPos(nRewind);
5806 nRewind++; // start one byte further next time, in case of failure
5807 blkdat.SetLimit(); // remove former limit
5808 unsigned int nSize = 0;
5809 try {
5810 // locate a header
5811 unsigned char buf[MESSAGE_START_SIZE];
5812 blkdat.FindByte(chainparams.MessageStart()[0]);
5813 nRewind = blkdat.GetPos() + 1;
5814 blkdat >> FLATDATA(buf);
5815 if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
5816 continue;
5817 // read size
5818 blkdat >> nSize;
5819 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
5820 continue;
5821 } catch (const std::exception&) {
5822 // no valid block header found; don't complain
5823 break;
5824 }
5825 try {
5826 // read block
5827 uint64_t nBlockPos = blkdat.GetPos();
5828 if (dbp)
5829 dbp->nPos = nBlockPos;
5830 blkdat.SetLimit(nBlockPos + nSize);
5831 blkdat.SetPos(nBlockPos);
5832 CBlock block;
5833 blkdat >> block;
5834 nRewind = blkdat.GetPos();
5835
5836 // detect out of order blocks, and store them for later
5837 uint256 hash = block.GetHash();
5838 CBlockIndex* pindexPrev = LookupBlockIndex(block.hashPrevBlock);
5839 if (hash != chainparams.GetConsensus().hashGenesisBlock && pindexPrev == NULL) {
5840 LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
5841 block.hashPrevBlock.ToString());
5842 if (dbp)
5843 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
5844 continue;
5845 }
5846
5847 int TheHeight = pindexPrev ? pindexPrev->nHeight + 1 : 0;
5848 if (pindexPrev) {

Callers 1

ThreadImportFunction · 0.85

Calls 15

GetTimeMillisFunction · 0.85
LookupBlockIndexFunction · 0.85
LogPrintFunction · 0.85
ProcessNewBlockFunction · 0.85
NotifyHeaderTipFunction · 0.85
ReadBlockFromDiskFunction · 0.85
AbortNodeFunction · 0.85
GetPosMethod · 0.80
SetPosMethod · 0.80
SetLimitMethod · 0.80
FindByteMethod · 0.80
IsErrorMethod · 0.80

Tested by

no test coverage detected