This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cs_blockstorage to guard disk access for block files
- Loading branch information
1 parent
32bbb04
commit 851c4e6
Showing
17 changed files
with
230 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include "blockstorage.h" | ||
|
||
#include "clientversion.h" | ||
#include "pow.h" | ||
#include "streams.h" | ||
#include "util/logger.h" | ||
|
||
CCriticalSection cs_blockstorage; | ||
|
||
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix) | ||
{ | ||
return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile); | ||
} | ||
|
||
FILE *OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) | ||
{ | ||
if (pos.IsNull()) | ||
return NULL; | ||
fs::path path = GetBlockPosFilename(pos, prefix); | ||
fs::create_directories(path.parent_path()); | ||
FILE *file = fopen(path.string().c_str(), fReadOnly ? "rb" : "rb+"); | ||
if (!file && !fReadOnly) | ||
file = fopen(path.string().c_str(), "wb+"); | ||
if (!file) | ||
{ | ||
LogPrintf("Unable to open file %s\n", path.string()); | ||
return NULL; | ||
} | ||
if (pos.nPos) | ||
{ | ||
if (fseek(file, pos.nPos, SEEK_SET)) | ||
{ | ||
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string()); | ||
fclose(file); | ||
return NULL; | ||
} | ||
} | ||
return file; | ||
} | ||
|
||
FILE *OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) { return OpenDiskFile(pos, "blk", fReadOnly); } | ||
FILE *OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) { return OpenDiskFile(pos, "rev", fReadOnly); } | ||
bool WriteBlockToDisk(const CBlock &block, CDiskBlockPos &pos, const CMessageHeader::MessageMagic &messageStart) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage) | ||
{ | ||
// Open history file to append | ||
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); | ||
if (fileout.IsNull()) | ||
return error("WriteBlockToDisk: OpenBlockFile failed"); | ||
|
||
// Write index header | ||
unsigned int nSize = GetSerializeSize(fileout, block); | ||
fileout << FLATDATA(messageStart) << nSize; | ||
|
||
// Write block | ||
long fileOutPos = ftell(fileout.Get()); | ||
if (fileOutPos < 0) | ||
return error("WriteBlockToDisk: ftell failed"); | ||
pos.nPos = (unsigned int)fileOutPos; | ||
fileout << block; | ||
|
||
return true; | ||
} | ||
|
||
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage) | ||
{ | ||
block.SetNull(); | ||
|
||
// Open history file to read | ||
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); | ||
if (filein.IsNull()) | ||
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString()); | ||
|
||
// Read block | ||
try | ||
{ | ||
filein >> block; | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString()); | ||
} | ||
|
||
// Check the header | ||
if (block.IsProofOfWork()) | ||
{ | ||
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) | ||
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); | ||
} | ||
return true; | ||
} | ||
|
||
bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, const Consensus::Params &consensusParams) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage) | ||
{ | ||
if (!pindex) | ||
{ | ||
return false; | ||
} | ||
if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams)) | ||
{ | ||
return false; | ||
} | ||
if (block.GetHash() != pindex->GetBlockHash()) | ||
{ | ||
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s", | ||
pindex->ToString(), pindex->GetBlockPos().ToString()); | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
#ifndef BITCOIN_BLOCK_STORAGE_H | ||
#define BITCOIN_BLOCK_STORAGE_H | ||
|
||
#include "chain/blockindex.h" | ||
#include "consensus/params.h" | ||
#include "fs.h" | ||
#include "net/protocol.h" | ||
#include "sync.h" | ||
|
||
extern CCriticalSection cs_blockstorage; | ||
|
||
/** Translation to a filesystem path */ | ||
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix); | ||
/** Open a block file (blk?????.dat) */ | ||
FILE *OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false); | ||
/** Open an undo file (rev?????.dat) */ | ||
FILE *OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false); | ||
/** Functions for disk access for blocks */ | ||
bool WriteBlockToDisk(const CBlock &block, CDiskBlockPos &pos, const CMessageHeader::MessageMagic &messageStart) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage); | ||
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage); | ||
bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, const Consensus::Params &consensusParams) | ||
EXCLUSIVE_LOCKS_REQUIRED(cs_blockstorage); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.