Skip to content

Commit fa0c7d9

Browse files
author
MarcoFalke
committed
move-only: Move *Disk functions to blockstorage
Can be reviewed with the git options --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent fa91b2b commit fa0c7d9

14 files changed

+181
-152
lines changed

src/index/base.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
#include <chainparams.h>
66
#include <index/base.h>
7+
#include <node/blockstorage.h>
78
#include <node/ui_interface.h>
89
#include <shutdown.h>
910
#include <tinyformat.h>
1011
#include <util/system.h>
1112
#include <util/translation.h>
12-
#include <validation.h>
13+
#include <validation.h> // For g_chainman
1314
#include <warnings.h>
1415

1516
constexpr char DB_BEST_BLOCK = 'B';

src/index/blockfilterindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include <dbwrapper.h>
88
#include <index/blockfilterindex.h>
9+
#include <node/blockstorage.h>
910
#include <util/system.h>
10-
#include <validation.h>
1111

1212
/* The index database stores three items for each block: the disk location of the encoded filter,
1313
* its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by

src/net_processing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <merkleblock.h>
1717
#include <netbase.h>
1818
#include <netmessagemaker.h>
19+
#include <node/blockstorage.h>
1920
#include <policy/fees.h>
2021
#include <policy/policy.h>
2122
#include <primitives/block.h>

src/node/blockstorage.cpp

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,154 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <blockstorage.h>
5+
#include <node/blockstorage.h>
66

7+
#include <chain.h>
78
#include <chainparams.h>
9+
#include <flatfile.h>
810
#include <fs.h>
11+
#include <pow.h>
912
#include <shutdown.h>
13+
#include <signet.h>
14+
#include <streams.h>
1015
#include <util/system.h>
1116
#include <validation.h>
1217

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+
13153
struct CImportingNow {
14154
CImportingNow()
15155
{

src/node/blockstorage.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,36 @@
55
#ifndef BITCOIN_NODE_BLOCKSTORAGE_H
66
#define BITCOIN_NODE_BLOCKSTORAGE_H
77

8+
#include <cstdint>
89
#include <vector>
910

1011
#include <fs.h>
12+
#include <protocol.h> // For CMessageHeader::MessageStartChars
1113

1214
class ArgsManager;
15+
class CBlock;
16+
class CBlockIndex;
17+
class CBlockUndo;
18+
class CChain;
19+
class CChainParams;
1320
class ChainstateManager;
21+
struct FlatFilePos;
22+
namespace Consensus {
23+
struct Params;
24+
}
1425

1526
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
1627

28+
/** Functions for disk access for blocks */
29+
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
30+
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
31+
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
32+
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
33+
34+
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
35+
36+
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
37+
1738
void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
1839

1940
#endif // BITCOIN_NODE_BLOCKSTORAGE_H

src/node/interfaces.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <addrdb.h>
66
#include <banman.h>
7-
#include <boost/signals2/signal.hpp>
87
#include <chain.h>
98
#include <chainparams.h>
109
#include <init.h>
@@ -17,6 +16,7 @@
1716
#include <net_processing.h>
1817
#include <netaddress.h>
1918
#include <netbase.h>
19+
#include <node/blockstorage.h>
2020
#include <node/coin.h>
2121
#include <node/context.h>
2222
#include <node/transaction.h>
@@ -53,6 +53,8 @@
5353
#include <optional>
5454
#include <utility>
5555

56+
#include <boost/signals2/signal.hpp>
57+
5658
using interfaces::BlockTip;
5759
using interfaces::Chain;
5860
using interfaces::FoundBlock;

src/rest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <core_io.h>
99
#include <httpserver.h>
1010
#include <index/txindex.h>
11+
#include <node/blockstorage.h>
1112
#include <node/context.h>
1213
#include <primitives/block.h>
1314
#include <primitives/transaction.h>

src/rpc/blockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <core_io.h>
1515
#include <hash.h>
1616
#include <index/blockfilterindex.h>
17+
#include <node/blockstorage.h>
1718
#include <node/coinstats.h>
1819
#include <node/context.h>
1920
#include <node/utxo_snapshot.h>

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <index/txindex.h>
1111
#include <key_io.h>
1212
#include <merkleblock.h>
13+
#include <node/blockstorage.h>
1314
#include <node/coin.h>
1415
#include <node/context.h>
1516
#include <node/psbt.h>

src/test/util/blockfilter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/util/blockfilter.h>
66

77
#include <chainparams.h>
8+
#include <node/blockstorage.h>
89
#include <validation.h>
910

1011

0 commit comments

Comments
 (0)