Skip to content

Commit 386a6b6

Browse files
committed
Allow to optional specify the directory for the blocks storage
1 parent ed6ae80 commit 386a6b6

File tree

7 files changed

+50
-11
lines changed

7 files changed

+50
-11
lines changed

src/init.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ std::string HelpMessage(HelpMessageMode mode)
333333
strUsage += HelpMessageOpt("-version", _("Print version and exit"));
334334
strUsage += HelpMessageOpt("-alertnotify=<cmd>", _("Execute command when a relevant alert is received or we see a really long fork (%s in cmd is replaced by message)"));
335335
strUsage +=HelpMessageOpt("-assumevalid=<hex>", strprintf(_("If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet: %s)"), defaultChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnetChainParams->GetConsensus().defaultAssumeValid.GetHex()));
336+
strUsage += HelpMessageOpt("-blocksdir=<dir>", _("Specify blocks directory (default: <datadir>/blocks)"));
336337
strUsage += HelpMessageOpt("-blocknotify=<cmd>", _("Execute command when the best block changes (%s in cmd is replaced by block hash)"));
337338
strUsage += HelpMessageOpt("-blockreconstructionextratxn=<n>", strprintf(_("Extra transactions to keep in memory for compact block reconstructions (default: %u)"), DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN));
338339
if (showDebug)
@@ -596,7 +597,7 @@ void CleanupBlockRevFiles()
596597
// Remove the rev files immediately and insert the blk file paths into an
597598
// ordered map keyed by block file index.
598599
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
599-
fs::path blocksdir = GetDataDir() / "blocks";
600+
fs::path blocksdir = GetBlocksDir();
600601
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
601602
if (fs::is_regular_file(*it) &&
602603
it->path().filename().string().length() == 12 &&
@@ -908,6 +909,10 @@ bool AppInitParameterInteraction()
908909

909910
// also see: InitParameterInteraction()
910911

912+
if (!fs::is_directory(GetBlocksDir(false))) {
913+
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist.\n"), gArgs.GetArg("-blocksdir", "").c_str()));
914+
}
915+
911916
// if using block pruning, then disallow txindex
912917
if (gArgs.GetArg("-prune", 0)) {
913918
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX))
@@ -1630,7 +1635,7 @@ bool AppInitMain()
16301635

16311636
// ********************************************************* Step 10: import blocks
16321637

1633-
if (!CheckDiskSpace())
1638+
if (!CheckDiskSpace() && !CheckDiskSpace(0, true))
16341639
return false;
16351640

16361641
// Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ int main(int argc, char *argv[])
617617
if (!Intro::pickDataDirectory())
618618
return EXIT_SUCCESS;
619619

620-
/// 6. Determine availability of data directory and parse bitcoin.conf
620+
/// 6. Determine availability of data and blocks directory and parse bitcoin.conf
621621
/// - Do not call GetDataDir(true) before this step finishes
622622
if (!fs::is_directory(GetDataDir(false)))
623623
{

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ size_t CCoinsViewDB::EstimateSize() const
147147
return db.EstimateSize(DB_COIN, (char)(DB_COIN+1));
148148
}
149149

150-
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
150+
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetBlocksDir() / "index", nCacheSize, fMemory, fWipe) {
151151
}
152152

153153
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {

src/util.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,41 @@ fs::path GetDefaultDataDir()
598598
#endif
599599
}
600600

601+
static fs::path g_blocks_path_cached;
602+
static fs::path g_blocks_path_cache_net_specific;
601603
static fs::path pathCached;
602604
static fs::path pathCachedNetSpecific;
603605
static CCriticalSection csPathCached;
604606

607+
const fs::path &GetBlocksDir(bool fNetSpecific)
608+
{
609+
610+
LOCK(csPathCached);
611+
612+
fs::path &path = fNetSpecific ? g_blocks_path_cache_net_specific : g_blocks_path_cached;
613+
614+
// This can be called during exceptions by LogPrintf(), so we cache the
615+
// value so we don't have to do memory allocations after that.
616+
if (!path.empty())
617+
return path;
618+
619+
if (gArgs.IsArgSet("-blocksdir")) {
620+
path = fs::system_complete(gArgs.GetArg("-blocksdir", ""));
621+
if (!fs::is_directory(path)) {
622+
path = "";
623+
return path;
624+
}
625+
} else {
626+
path = GetDataDir(false);
627+
}
628+
if (fNetSpecific)
629+
path /= BaseParams().DataDir();
630+
631+
path /= "blocks";
632+
fs::create_directories(path);
633+
return path;
634+
}
635+
605636
const fs::path &GetDataDir(bool fNetSpecific)
606637
{
607638

@@ -640,6 +671,8 @@ void ClearDatadirCache()
640671

641672
pathCached = fs::path();
642673
pathCachedNetSpecific = fs::path();
674+
g_blocks_path_cached = fs::path();
675+
g_blocks_path_cache_net_specific = fs::path();
643676
}
644677

645678
fs::path GetConfigFile(const std::string& confPath)

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ void ReleaseDirectoryLocks();
182182

183183
bool TryCreateDirectories(const fs::path& p);
184184
fs::path GetDefaultDataDir();
185+
const fs::path &GetBlocksDir(bool fNetSpecific = true);
185186
const fs::path &GetDataDir(bool fNetSpecific = true);
186187
void ClearDatadirCache();
187188
fs::path GetConfigFile(const std::string& confPath);

src/validation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
20562056
// Write blocks and block index to disk.
20572057
if (fDoFullFlush || fPeriodicWrite) {
20582058
// Depend on nMinDiskSpace to ensure we can write block index
2059-
if (!CheckDiskSpace(0))
2059+
if (!CheckDiskSpace(0, true))
20602060
return state.Error("out of disk space");
20612061
// First make sure all block and undo data is flushed to disk.
20622062
FlushBlockFile();
@@ -2895,7 +2895,7 @@ static bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int
28952895
if (nNewChunks > nOldChunks) {
28962896
if (fPruneMode)
28972897
fCheckForPruning = true;
2898-
if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
2898+
if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos, true)) {
28992899
FILE *file = OpenBlockFile(pos);
29002900
if (file) {
29012901
LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
@@ -2928,7 +2928,7 @@ static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos,
29282928
if (nNewChunks > nOldChunks) {
29292929
if (fPruneMode)
29302930
fCheckForPruning = true;
2931-
if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
2931+
if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos, true)) {
29322932
FILE *file = OpenUndoFile(pos);
29332933
if (file) {
29342934
LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
@@ -3604,9 +3604,9 @@ static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfte
36043604
nLastBlockWeCanPrune, count);
36053605
}
36063606

3607-
bool CheckDiskSpace(uint64_t nAdditionalBytes)
3607+
bool CheckDiskSpace(uint64_t nAdditionalBytes, bool blocks_dir)
36083608
{
3609-
uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available;
3609+
uint64_t nFreeBytesAvailable = fs::space(blocks_dir ? GetBlocksDir() : GetDataDir()).available;
36103610

36113611
// Check for nMinDiskSpace bytes (currently 50MB)
36123612
if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
@@ -3649,7 +3649,7 @@ static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
36493649

36503650
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
36513651
{
3652-
return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3652+
return GetBlocksDir() / strprintf("%s%05u.dat", prefix, pos.nFile);
36533653
}
36543654

36553655
CBlockIndex * CChainState::InsertBlockIndex(const uint256& hash)

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
254254
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex=nullptr, CBlockHeader *first_invalid=nullptr);
255255

256256
/** Check whether enough disk space is available for an incoming block */
257-
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
257+
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0, bool blocks_dir = false);
258258
/** Open a block file (blk?????.dat) */
259259
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
260260
/** Translation to a filesystem path */

0 commit comments

Comments
 (0)