Skip to content

Commit e515b1e

Browse files
sipaWarrows
authored andcommitted
[Node] Do all block index writes in a batch
Backport from bitcoin bitcoin#5367
1 parent 7e8855d commit e515b1e

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/chain.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ class CBlockIndex
175175

176176
//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
177177
uint32_t nSequenceId;
178-
178+
179179
//! zerocoin specific fields
180180
std::map<libzerocoin::CoinDenomination, int64_t> mapZerocoinSupply;
181181
std::vector<libzerocoin::CoinDenomination> vMintDenominationsInBlock;
182-
182+
183183
void SetNull()
184184
{
185185
phashBlock = NULL;
@@ -251,7 +251,7 @@ class CBlockIndex
251251
nStakeTime = 0;
252252
}
253253
}
254-
254+
255255

256256
CDiskBlockPos GetBlockPos() const
257257
{
@@ -393,7 +393,7 @@ class CBlockIndex
393393

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

453-
explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
453+
explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex)
454454
{
455-
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
455+
hashPrev = (pprev ? pprev->GetBlockHash() : uint256(0));
456456
}
457457

458458
ADD_SERIALIZE_METHODS;

src/main.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,25 +3538,23 @@ bool static FlushStateToDisk(CValidationState& state, FlushStateMode mode)
35383538
// First make sure all block and undo data is flushed to disk.
35393539
FlushBlockFile();
35403540
// Then update all block file information (which may refer to block and undo files).
3541-
bool fileschanged = false;
3542-
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end();) {
3543-
if (!pblocktree->WriteBlockFileInfo(*it, vinfoBlockFile[*it])) {
3544-
return state.Abort("Failed to write to block index");
3541+
{
3542+
std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
3543+
vFiles.reserve(setDirtyFileInfo.size());
3544+
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
3545+
vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
3546+
setDirtyFileInfo.erase(it++);
35453547
}
3546-
fileschanged = true;
3547-
setDirtyFileInfo.erase(it++);
3548-
}
3549-
if (fileschanged && !pblocktree->WriteLastBlockFile(nLastBlockFile)) {
3550-
return state.Abort("Failed to write to block index");
3551-
}
3552-
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end();) {
3553-
if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(*it))) {
3554-
return state.Abort("Failed to write to block index");
3548+
std::vector<const CBlockIndex*> vBlocks;
3549+
vBlocks.reserve(setDirtyBlockIndex.size());
3550+
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
3551+
vBlocks.push_back(*it);
3552+
setDirtyBlockIndex.erase(it++);
3553+
}
3554+
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
3555+
return state.Abort("Files to write to block index database");
35553556
}
3556-
setDirtyBlockIndex.erase(it++);
35573557
}
3558-
3559-
pblocktree->Sync();
35603558
// Finally flush the chainstate (which may refer to block index entries).
35613559
if (!pcoinsTip->Flush())
35623560
return state.Abort("Failed to write to coin database");

src/txdb.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,11 @@ bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
8383
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
8484
}
8585

86-
bool CBlockTreeDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo& info)
87-
{
88-
return Write(make_pair('f', nFile), info);
89-
}
90-
9186
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
9287
{
9388
return Read(make_pair('f', nFile), info);
9489
}
9590

96-
bool CBlockTreeDB::WriteLastBlockFile(int nFile)
97-
{
98-
return Write('l', nFile);
99-
}
100-
10191
bool CBlockTreeDB::WriteReindexing(bool fReindexing)
10292
{
10393
if (fReindexing)
@@ -171,6 +161,18 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
171161
return true;
172162
}
173163

164+
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
165+
CLevelDBBatch batch;
166+
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
167+
batch.Write(make_pair('f', it->first), *it->second);
168+
}
169+
batch.Write('l', nLastFile);
170+
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
171+
batch.Write(make_pair('b', (*it)->GetBlockHash()), CDiskBlockIndex(*it));
172+
}
173+
return WriteBatch(batch, true);
174+
}
175+
174176
bool CBlockTreeDB::ReadTxIndex(const uint256& txid, CDiskTxPos& pos)
175177
{
176178
return Read(make_pair('t', txid), pos);

src/txdb.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ class CBlockTreeDB : public CLevelDBWrapper
5454

5555
public:
5656
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
57+
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
5758
bool ReadBlockFileInfo(int nFile, CBlockFileInfo& fileinfo);
58-
bool WriteBlockFileInfo(int nFile, const CBlockFileInfo& fileinfo);
5959
bool ReadLastBlockFile(int& nFile);
60-
bool WriteLastBlockFile(int nFile);
6160
bool WriteReindexing(bool fReindex);
6261
bool ReadReindexing(bool& fReindex);
6362
bool ReadTxIndex(const uint256& txid, CDiskTxPos& pos);

0 commit comments

Comments
 (0)