|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
5 |
| -#include <blockstorage.h> |
| 5 | +#include <node/blockstorage.h> |
6 | 6 |
|
| 7 | +#include <chain.h> |
7 | 8 | #include <chainparams.h>
|
| 9 | +#include <flatfile.h> |
8 | 10 | #include <fs.h>
|
| 11 | +#include <pow.h> |
9 | 12 | #include <shutdown.h>
|
| 13 | +#include <signet.h> |
| 14 | +#include <streams.h> |
10 | 15 | #include <util/system.h>
|
11 | 16 | #include <validation.h>
|
12 | 17 |
|
| 18 | +// From validation. TODO move here |
| 19 | +bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false); |
| 20 | + |
| 21 | +static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart) |
| 22 | +{ |
| 23 | + // Open history file to append |
| 24 | + CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); |
| 25 | + if (fileout.IsNull()) |
| 26 | + return error("WriteBlockToDisk: OpenBlockFile failed"); |
| 27 | + |
| 28 | + // Write index header |
| 29 | + unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); |
| 30 | + fileout << messageStart << nSize; |
| 31 | + |
| 32 | + // Write block |
| 33 | + long fileOutPos = ftell(fileout.Get()); |
| 34 | + if (fileOutPos < 0) |
| 35 | + return error("WriteBlockToDisk: ftell failed"); |
| 36 | + pos.nPos = (unsigned int)fileOutPos; |
| 37 | + fileout << block; |
| 38 | + |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams) |
| 43 | +{ |
| 44 | + block.SetNull(); |
| 45 | + |
| 46 | + // Open history file to read |
| 47 | + CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); |
| 48 | + if (filein.IsNull()) |
| 49 | + return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString()); |
| 50 | + |
| 51 | + // Read block |
| 52 | + try { |
| 53 | + filein >> block; |
| 54 | + } |
| 55 | + catch (const std::exception& e) { |
| 56 | + return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString()); |
| 57 | + } |
| 58 | + |
| 59 | + // Check the header |
| 60 | + if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) |
| 61 | + return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); |
| 62 | + |
| 63 | + // Signet only: check block solution |
| 64 | + if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) { |
| 65 | + return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString()); |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams) |
| 72 | +{ |
| 73 | + FlatFilePos blockPos; |
| 74 | + { |
| 75 | + LOCK(cs_main); |
| 76 | + blockPos = pindex->GetBlockPos(); |
| 77 | + } |
| 78 | + |
| 79 | + if (!ReadBlockFromDisk(block, blockPos, consensusParams)) |
| 80 | + return false; |
| 81 | + if (block.GetHash() != pindex->GetBlockHash()) |
| 82 | + return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s", |
| 83 | + pindex->ToString(), pindex->GetBlockPos().ToString()); |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start) |
| 88 | +{ |
| 89 | + FlatFilePos hpos = pos; |
| 90 | + hpos.nPos -= 8; // Seek back 8 bytes for meta header |
| 91 | + CAutoFile filein(OpenBlockFile(hpos, true), SER_DISK, CLIENT_VERSION); |
| 92 | + if (filein.IsNull()) { |
| 93 | + return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString()); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + CMessageHeader::MessageStartChars blk_start; |
| 98 | + unsigned int blk_size; |
| 99 | + |
| 100 | + filein >> blk_start >> blk_size; |
| 101 | + |
| 102 | + if (memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) { |
| 103 | + return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(), |
| 104 | + HexStr(blk_start), |
| 105 | + HexStr(message_start)); |
| 106 | + } |
| 107 | + |
| 108 | + if (blk_size > MAX_SIZE) { |
| 109 | + return error("%s: Block data is larger than maximum deserialization size for %s: %s versus %s", __func__, pos.ToString(), |
| 110 | + blk_size, MAX_SIZE); |
| 111 | + } |
| 112 | + |
| 113 | + block.resize(blk_size); // Zeroing of memory is intentional here |
| 114 | + filein.read((char*)block.data(), blk_size); |
| 115 | + } catch (const std::exception& e) { |
| 116 | + return error("%s: Read from block file failed: %s for %s", __func__, e.what(), pos.ToString()); |
| 117 | + } |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start) |
| 123 | +{ |
| 124 | + FlatFilePos block_pos; |
| 125 | + { |
| 126 | + LOCK(cs_main); |
| 127 | + block_pos = pindex->GetBlockPos(); |
| 128 | + } |
| 129 | + |
| 130 | + return ReadRawBlockFromDisk(block, block_pos, message_start); |
| 131 | +} |
| 132 | + |
| 133 | +/** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */ |
| 134 | +FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp) |
| 135 | +{ |
| 136 | + unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); |
| 137 | + FlatFilePos blockPos; |
| 138 | + if (dbp != nullptr) |
| 139 | + blockPos = *dbp; |
| 140 | + if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) { |
| 141 | + error("%s: FindBlockPos failed", __func__); |
| 142 | + return FlatFilePos(); |
| 143 | + } |
| 144 | + if (dbp == nullptr) { |
| 145 | + if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) { |
| 146 | + AbortNode("Failed to write block"); |
| 147 | + return FlatFilePos(); |
| 148 | + } |
| 149 | + } |
| 150 | + return blockPos; |
| 151 | +} |
| 152 | + |
13 | 153 | struct CImportingNow {
|
14 | 154 | CImportingNow()
|
15 | 155 | {
|
|
0 commit comments