| 5789 | } |
| 5790 | |
| 5791 | bool 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) { |
no test coverage detected