Skip to content

Commit

Permalink
[Node] Do all block index writes in a batch
Browse files Browse the repository at this point in the history
Backport from bitcoin bitcoin#5367
  • Loading branch information
sipa authored and Warrows committed Jun 19, 2019
1 parent 7e8855d commit e515b1e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
12 changes: 6 additions & 6 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ class CBlockIndex

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
uint32_t nSequenceId;

//! zerocoin specific fields
std::map<libzerocoin::CoinDenomination, int64_t> mapZerocoinSupply;
std::vector<libzerocoin::CoinDenomination> vMintDenominationsInBlock;

void SetNull()
{
phashBlock = NULL;
Expand Down Expand Up @@ -251,7 +251,7 @@ class CBlockIndex
nStakeTime = 0;
}
}


CDiskBlockPos GetBlockPos() const
{
Expand Down Expand Up @@ -393,7 +393,7 @@ class CBlockIndex

/**
* Returns true if there are nRequired or more blocks of minVersion or above
* in the last Params().ToCheckBlockUpgradeMajority() blocks, starting at pstart
* in the last Params().ToCheckBlockUpgradeMajority() blocks, starting at pstart
* and going backwards.
*/
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired);
Expand Down Expand Up @@ -450,9 +450,9 @@ class CDiskBlockIndex : public CBlockIndex
hashNext = uint256();
}

explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex)
{
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
hashPrev = (pprev ? pprev->GetBlockHash() : uint256(0));
}

ADD_SERIALIZE_METHODS;
Expand Down
30 changes: 14 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3538,25 +3538,23 @@ bool static FlushStateToDisk(CValidationState& state, FlushStateMode mode)
// First make sure all block and undo data is flushed to disk.
FlushBlockFile();
// Then update all block file information (which may refer to block and undo files).
bool fileschanged = false;
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end();) {
if (!pblocktree->WriteBlockFileInfo(*it, vinfoBlockFile[*it])) {
return state.Abort("Failed to write to block index");
{
std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
vFiles.reserve(setDirtyFileInfo.size());
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
setDirtyFileInfo.erase(it++);
}
fileschanged = true;
setDirtyFileInfo.erase(it++);
}
if (fileschanged && !pblocktree->WriteLastBlockFile(nLastBlockFile)) {
return state.Abort("Failed to write to block index");
}
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end();) {
if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(*it))) {
return state.Abort("Failed to write to block index");
std::vector<const CBlockIndex*> vBlocks;
vBlocks.reserve(setDirtyBlockIndex.size());
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
vBlocks.push_back(*it);
setDirtyBlockIndex.erase(it++);
}
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
return state.Abort("Files to write to block index database");
}
setDirtyBlockIndex.erase(it++);
}

pblocktree->Sync();
// Finally flush the chainstate (which may refer to block index entries).
if (!pcoinsTip->Flush())
return state.Abort("Failed to write to coin database");
Expand Down
22 changes: 12 additions & 10 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,11 @@ bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
}

bool CBlockTreeDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo& info)
{
return Write(make_pair('f', nFile), info);
}

bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
{
return Read(make_pair('f', nFile), info);
}

bool CBlockTreeDB::WriteLastBlockFile(int nFile)
{
return Write('l', nFile);
}

bool CBlockTreeDB::WriteReindexing(bool fReindexing)
{
if (fReindexing)
Expand Down Expand Up @@ -171,6 +161,18 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
return true;
}

bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
CLevelDBBatch batch;
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
batch.Write(make_pair('f', it->first), *it->second);
}
batch.Write('l', nLastFile);
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
batch.Write(make_pair('b', (*it)->GetBlockHash()), CDiskBlockIndex(*it));
}
return WriteBatch(batch, true);
}

bool CBlockTreeDB::ReadTxIndex(const uint256& txid, CDiskTxPos& pos)
{
return Read(make_pair('t', txid), pos);
Expand Down
3 changes: 1 addition & 2 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ class CBlockTreeDB : public CLevelDBWrapper

public:
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo& fileinfo);
bool WriteBlockFileInfo(int nFile, const CBlockFileInfo& fileinfo);
bool ReadLastBlockFile(int& nFile);
bool WriteLastBlockFile(int nFile);
bool WriteReindexing(bool fReindex);
bool ReadReindexing(bool& fReindex);
bool ReadTxIndex(const uint256& txid, CDiskTxPos& pos);
Expand Down

0 comments on commit e515b1e

Please sign in to comment.