MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / LoadExternalBlockFile

Function LoadExternalBlockFile

src/validation.cpp:4617–4737  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4615}
4616
4617bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
4618{
4619 // Map of disk positions for blocks with unknown parent (only used for reindex)
4620 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
4621 int64_t nStart = GetTimeMillis();
4622
4623 int nLoaded = 0;
4624 try {
4625 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
4626 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
4627 uint64_t nRewind = blkdat.GetPos();
4628 while (!blkdat.eof()) {
4629 boost::this_thread::interruption_point();
4630
4631 blkdat.SetPos(nRewind);
4632 nRewind++; // start one byte further next time, in case of failure
4633 blkdat.SetLimit(); // remove former limit
4634 unsigned int nSize = 0;
4635 try {
4636 // locate a header
4637 unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4638 blkdat.FindByte(chainparams.MessageStart()[0]);
4639 nRewind = blkdat.GetPos()+1;
4640 blkdat >> buf;
4641 if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
4642 continue;
4643 // read size
4644 blkdat >> nSize;
4645 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
4646 continue;
4647 } catch (const std::exception&) {
4648 // no valid block header found; don't complain
4649 break;
4650 }
4651 try {
4652 // read block
4653 uint64_t nBlockPos = blkdat.GetPos();
4654 if (dbp)
4655 dbp->nPos = nBlockPos;
4656 blkdat.SetLimit(nBlockPos + nSize);
4657 blkdat.SetPos(nBlockPos);
4658 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4659 CBlock& block = *pblock;
4660 blkdat >> block;
4661 nRewind = blkdat.GetPos();
4662
4663 uint256 hash = block.GetHash();
4664 {
4665 LOCK(cs_main);
4666 // detect out of order blocks, and store them for later
4667 if (hash != chainparams.GetConsensus().hashGenesisBlock && !LookupBlockIndex(block.hashPrevBlock)) {
4668 LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
4669 block.hashPrevBlock.ToString());
4670 if (dbp)
4671 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
4672 continue;
4673 }
4674

Callers 1

ThreadImportFunction · 0.85

Calls 15

GetTimeMillisFunction · 0.85
LookupBlockIndexFunction · 0.85
ActivateBestChainFunction · 0.85
ReadBlockFromDiskFunction · 0.85
AbortNodeFunction · 0.85
GetPosMethod · 0.80
SetPosMethod · 0.80
SetLimitMethod · 0.80
FindByteMethod · 0.80
AcceptBlockMethod · 0.80
IsErrorMethod · 0.80
whatMethod · 0.80

Tested by

no test coverage detected