| 50 | |
| 51 | namespace { |
| 52 | std::vector<BlockInfo<FileTag>> readBlocksImpl(SafeMemReader &reader, int fileNum, const ChainDiskConfiguration &config) { |
| 53 | try { |
| 54 | std::vector<BlockInfo<FileTag>> blocks; |
| 55 | // Read blocks in loop while we can... |
| 56 | while (reader.has(sizeof(uint32_t))) { |
| 57 | auto magic = reader.readNext<uint32_t>(); |
| 58 | if (magic != config.blockMagic) { |
| 59 | break; |
| 60 | } |
| 61 | auto length = reader.readNext<uint32_t>(); |
| 62 | auto blockStartOffset = reader.offset(); |
| 63 | assert(reader.has(length)); |
| 64 | while (reader.peakNext<uint32_t>() == config.blockMagic) { |
| 65 | // The previous block must have been cut off |
| 66 | // See https://github.com/bitcoin/bitcoin/issues/8614 |
| 67 | reader.advance(sizeof(uint32_t)); |
| 68 | length = reader.readNext<uint32_t>(); |
| 69 | blockStartOffset = reader.offset(); |
| 70 | } |
| 71 | auto header = reader.readNext<CBlockHeader>(); |
| 72 | auto numTxes = reader.readVariableLengthInteger(); |
| 73 | uint32_t inputCount = 0; |
| 74 | uint32_t outputCount = 0; |
| 75 | for (size_t i = 0; i < numTxes; i++) { |
| 76 | TransactionHeader h(reader); |
| 77 | inputCount += h.inputCount; |
| 78 | outputCount += h.outputCount; |
| 79 | } |
| 80 | // The next two lines bring the reader to the end of this block |
| 81 | reader.reset(blockStartOffset); |
| 82 | reader.advance(length); |
| 83 | inputCount--; |
| 84 | blocks.emplace_back(header, length, numTxes, inputCount, outputCount, config, fileNum, blockStartOffset); |
| 85 | } |
| 86 | return blocks; |
| 87 | } catch (const std::out_of_range &e) { |
| 88 | std::cerr << "Failed to read block header information" |
| 89 | << " from " << reader.getPath() |
| 90 | << " at offset " << reader.offset() |
| 91 | << ": " << e.what() << "\n"; |
| 92 | throw; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | std::vector<BlockInfo<FileTag>> readBlocksInfo(int fileNum, const ParserConfiguration<FileTag> &config) { |