| 4016 | } |
| 4017 | |
| 4018 | bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp) |
| 4019 | { |
| 4020 | // Map of disk positions for blocks with unknown parent (only used for reindex) |
| 4021 | static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent; |
| 4022 | int64_t nStart = GetTimeMillis(); |
| 4023 | |
| 4024 | int nLoaded = 0; |
| 4025 | try { |
| 4026 | // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor |
| 4027 | CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION); |
| 4028 | uint64_t nRewind = blkdat.GetPos(); |
| 4029 | while (!blkdat.eof()) { |
| 4030 | boost::this_thread::interruption_point(); |
| 4031 | |
| 4032 | blkdat.SetPos(nRewind); |
| 4033 | nRewind++; // start one byte further next time, in case of failure |
| 4034 | blkdat.SetLimit(); // remove former limit |
| 4035 | unsigned int nSize = 0; |
| 4036 | try { |
| 4037 | // locate a header |
| 4038 | unsigned char buf[CMessageHeader::MESSAGE_START_SIZE]; |
| 4039 | blkdat.FindByte(chainparams.MessageStart()[0]); |
| 4040 | nRewind = blkdat.GetPos()+1; |
| 4041 | blkdat >> FLATDATA(buf); |
| 4042 | if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) |
| 4043 | continue; |
| 4044 | // read size |
| 4045 | blkdat >> nSize; |
| 4046 | if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE) |
| 4047 | continue; |
| 4048 | } catch (const std::exception&) { |
| 4049 | // no valid block header found; don't complain |
| 4050 | break; |
| 4051 | } |
| 4052 | try { |
| 4053 | // read block |
| 4054 | uint64_t nBlockPos = blkdat.GetPos(); |
| 4055 | if (dbp) |
| 4056 | dbp->nPos = nBlockPos; |
| 4057 | blkdat.SetLimit(nBlockPos + nSize); |
| 4058 | blkdat.SetPos(nBlockPos); |
| 4059 | std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(); |
| 4060 | CBlock& block = *pblock; |
| 4061 | blkdat >> block; |
| 4062 | nRewind = blkdat.GetPos(); |
| 4063 | |
| 4064 | // detect out of order blocks, and store them for later |
| 4065 | uint256 hash = block.GetHash(); |
| 4066 | if (hash != chainparams.GetConsensus(0).hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) { |
| 4067 | LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(), |
| 4068 | block.hashPrevBlock.ToString()); |
| 4069 | if (dbp) |
| 4070 | mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp)); |
| 4071 | continue; |
| 4072 | } |
| 4073 | |
| 4074 | // process in case the block isn't known yet |
| 4075 | if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) { |
no test coverage detected