Skip to content

Commit

Permalink
batched write of headers
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegralTeam committed Feb 13, 2020
1 parent 7829321 commit 04996bb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ class CMasternodesView

virtual CMasternode const * ExistMasternode(uint256 const & id) const;

virtual void WriteMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool fIsFakeNet = true) { assert(false); }
virtual void CommitHeaders() { assert(false); }
virtual void WriteMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool isBatched, bool fIsFakeNet = true) { assert(false); }
virtual bool FindMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, std::map<uint256, CBlockHeader> & blockHeaders, bool fIsFakeNet = true) { assert(false); }
virtual void EraseMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, uint256 const & hash) { assert(false); }

Expand Down
20 changes: 18 additions & 2 deletions src/masternodes/mn_txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ void CMasternodesViewDB::CommitBatch()
}
}

void CMasternodesViewDB::CommitHeaders()
{
if (headersBatch)
{
db->WriteBatch(*headersBatch);
headersBatch.reset();
}
}

bool CMasternodesViewDB::ReadHeight(int & h)
{
// it's a hack, cause we don't know active chain tip at the loading time
Expand All @@ -118,13 +127,20 @@ void CMasternodesViewDB::EraseMasternode(uint256 const & txid)
BatchErase(make_pair(DB_MASTERNODES, txid));
}

void CMasternodesViewDB::WriteMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool fIsFakeNet)
void CMasternodesViewDB::WriteMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool isBatched, bool fIsFakeNet)
{
if (fIsFakeNet) {
return;
}

db->Write(DBMNBlockHeadersKey{DB_MN_BLOCK_HEADERS, DBMNBlockHeadersSearchKey{txid, mintedBlocks}, hash}, blockHeader);
if (!headersBatch)
{
headersBatch.reset(new CDBBatch(*db));
}
headersBatch->Write(DBMNBlockHeadersKey{DB_MN_BLOCK_HEADERS, DBMNBlockHeadersSearchKey{txid, mintedBlocks}, hash}, blockHeader);
if (!isBatched) {
CommitHeaders();
}
}

bool CMasternodesViewDB::FindMintedBlockHeader(uint256 const & txid, uint64_t const mintedBlocks, std::map<uint256, CBlockHeader> & blockHeaders, bool fIsFakeNet)
Expand Down
22 changes: 12 additions & 10 deletions src/masternodes/mn_txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CMasternodesViewDB : public CMasternodesView
private:
boost::shared_ptr<CDBWrapper> db;
boost::scoped_ptr<CDBBatch> batch;
boost::scoped_ptr<CDBBatch> headersBatch;

public:
CMasternodesViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
Expand Down Expand Up @@ -88,26 +89,27 @@ class CMasternodesViewDB : public CMasternodesView

protected:
void CommitBatch();
void CommitHeaders() override;

bool ReadHeight(int & h);
void WriteHeight(int h);

void WriteMasternode(uint256 const & txid, CMasternode const & node);
void EraseMasternode(uint256 const & txid);

void WriteMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool fIsFakeNet = true);
bool FindMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, std::map<uint256, CBlockHeader> & blockHeaders, bool fIsFakeNet = true);
void EraseMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, uint256 const & hash);
void WriteMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, uint256 const & hash, CBlockHeader const & blockHeader, bool isBatched, bool fIsFakeNet = true) override;
bool FindMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, std::map<uint256, CBlockHeader> & blockHeaders, bool fIsFakeNet = true) override;
void EraseMintedBlockHeader(uint256 const & txid, uint64_t mintedBlocks, uint256 const & hash) override;

void WriteCriminal(uint256 const & mnId, CDoubleSignFact const & doubleSignFact);
void EraseCriminal(uint256 const & mnId);
void WriteCriminal(uint256 const & mnId, CDoubleSignFact const & doubleSignFact) override;
void EraseCriminal(uint256 const & mnId) override;

void WriteCurrentTeam(std::set<CKeyID> const & currentTeam);
bool LoadCurrentTeam(std::set<CKeyID> & newTeam);
bool EraseCurrentTeam();
void WriteCurrentTeam(std::set<CKeyID> const & currentTeam) override;
bool LoadCurrentTeam(std::set<CKeyID> & newTeam) override;
bool EraseCurrentTeam() override;

void WriteFoundationsDebt(CAmount const foundationsDebt);
bool LoadFoundationsDebt();
void WriteFoundationsDebt(CAmount const foundationsDebt) override;
bool LoadFoundationsDebt() override;

// void WriteDeadIndex(int height, uint256 const & txid, char type);
// void EraseDeadIndex(int height, uint256 const & txid);
Expand Down
4 changes: 2 additions & 2 deletions src/test/double_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ BOOST_AUTO_TEST_CASE(check_doublesign)
uint64_t mintedBlocks = 0;
std::vector<CBlockHeader> criminalsBlockHeaders = GenerateTwoCriminalsHeaders(minterKey, mintedBlocks, masternodeID);

pmasternodesview->WriteMintedBlockHeader(masternodeID, mintedBlocks, criminalsBlockHeaders[0].GetHash(), criminalsBlockHeaders[0], false);
pmasternodesview->WriteMintedBlockHeader(masternodeID, mintedBlocks, criminalsBlockHeaders[1].GetHash(), criminalsBlockHeaders[1], false);
pmasternodesview->WriteMintedBlockHeader(masternodeID, mintedBlocks, criminalsBlockHeaders[0].GetHash(), criminalsBlockHeaders[0], false, false);
pmasternodesview->WriteMintedBlockHeader(masternodeID, mintedBlocks, criminalsBlockHeaders[1].GetHash(), criminalsBlockHeaders[1], false, false);
BOOST_CHECK(!pmasternodesview->CheckDoubleSign(criminalsBlockHeaders[0], criminalsBlockHeaders[1]));

std::map<uint256, CBlockHeader> blockHeaders;
Expand Down
8 changes: 5 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3688,7 +3688,7 @@ static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, c
return true;
}

bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool isBatched)
{
AssertLockHeld(cs_main);
// Check for duplicate
Expand Down Expand Up @@ -3728,7 +3728,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, CValidationState

auto existingBlockHeader = blockHeaders.find(hash);
if (!blockHeaders.size() || existingBlockHeader == blockHeaders.end()) {
pmasternodesview->WriteMintedBlockHeader(nodeId, block.mintedBlocks, hash, block, fIsFakeNet);
pmasternodesview->WriteMintedBlockHeader(nodeId, block.mintedBlocks, hash, block, isBatched, fIsFakeNet);
}

for (std::pair <uint256, CBlockHeader> blockHeader : blockHeaders) {
Expand Down Expand Up @@ -3811,17 +3811,19 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidatio
CMasternodesViewHistory history(pmasternodesview.get());
for (const CBlockHeader& header : headers) {
CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
bool accepted = g_blockman.AcceptBlockHeader(header, state, chainparams, &pindex/*, &history.GetState(header.height)*/);
bool accepted = g_blockman.AcceptBlockHeader(header, state, chainparams, &pindex, true/*, &history.GetState(header.height)*/);
::ChainstateActive().CheckBlockIndex(chainparams.GetConsensus());

if (!accepted) {
if (first_invalid) *first_invalid = header;
pmasternodesview->CommitHeaders(); // commit headers anyway
return false;
}
if (ppindex) {
*ppindex = pindex;
}
}
pmasternodesview->CommitHeaders();
}
NotifyHeaderTip();
{
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class BlockManager {
const CBlockHeader& block,
CValidationState& state,
const CChainParams& chainparams,
CBlockIndex** ppindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
CBlockIndex** ppindex, bool isBatched = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
};

/**
Expand Down

0 comments on commit 04996bb

Please sign in to comment.