| 4155 | |
| 4156 | |
| 4157 | bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp) |
| 4158 | { |
| 4159 | const CChainParams& chainparams = Params(); |
| 4160 | // Map of disk positions for blocks with unknown parent (only used for reindex) |
| 4161 | static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent; |
| 4162 | int64_t nStart = GetTimeMillis(); |
| 4163 | |
| 4164 | int nLoaded = 0; |
| 4165 | try { |
| 4166 | // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor |
| 4167 | CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION); |
| 4168 | uint64_t nRewind = blkdat.GetPos(); |
| 4169 | while (!blkdat.eof()) { |
| 4170 | boost::this_thread::interruption_point(); |
| 4171 | |
| 4172 | blkdat.SetPos(nRewind); |
| 4173 | nRewind++; // start one byte further next time, in case of failure |
| 4174 | blkdat.SetLimit(); // remove former limit |
| 4175 | unsigned int nSize = 0; |
| 4176 | try { |
| 4177 | // locate a header |
| 4178 | unsigned char buf[MESSAGE_START_SIZE]; |
| 4179 | blkdat.FindByte(Params().MessageStart()[0]); |
| 4180 | nRewind = blkdat.GetPos()+1; |
| 4181 | blkdat >> FLATDATA(buf); |
| 4182 | if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE)) |
| 4183 | continue; |
| 4184 | // read size |
| 4185 | blkdat >> nSize; |
| 4186 | if (nSize < 80) |
| 4187 | continue; |
| 4188 | } catch (const std::exception&) { |
| 4189 | // no valid block header found; don't complain |
| 4190 | break; |
| 4191 | } |
| 4192 | try { |
| 4193 | // read block |
| 4194 | uint64_t nBlockPos = blkdat.GetPos(); |
| 4195 | if (dbp) |
| 4196 | dbp->nPos = nBlockPos; |
| 4197 | blkdat.SetLimit(nBlockPos + nSize); |
| 4198 | blkdat.SetPos(nBlockPos); |
| 4199 | CBlock block; |
| 4200 | blkdat >> block; |
| 4201 | nRewind = blkdat.GetPos(); |
| 4202 | |
| 4203 | // detect out of order blocks, and store them for later |
| 4204 | uint256 hash = block.GetHash(); |
| 4205 | if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) { |
| 4206 | LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(), |
| 4207 | block.hashPrevBlock.ToString()); |
| 4208 | if (dbp) |
| 4209 | mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp)); |
| 4210 | continue; |
| 4211 | } |
| 4212 | |
| 4213 | // process in case the block isn't known yet |
| 4214 | if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) { |
no test coverage detected