Skip to content

Commit

Permalink
Backport Bitcoin PR#9183: Final Preparation for main.cpp Split (#1561)
Browse files Browse the repository at this point in the history
* Expose AcceptBlockHeader through main.h

* Split ::HEADERS processing into two separate cs_main locks

This will allow NotifyHeaderTip to be called from an
AcceptBlockHeader wrapper function without holding cs_main.

* Use exposed ProcessNewBlockHeaders from ProcessMessages
  • Loading branch information
OlegGirko authored and UdjinM6 committed Aug 2, 2017
1 parent b4b3431 commit 415085c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3835,7 +3835,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
return true;
}

static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL)
static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
{
AssertLockHeld(cs_main);
// Check for duplicate
Expand Down Expand Up @@ -3886,6 +3886,21 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
return true;
}

// Exposed wrapper for AcceptBlockHeader
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
{
{
LOCK(cs_main);
for (const CBlockHeader& header : headers) {
if (!AcceptBlockHeader(header, state, chainparams, ppindex)) {
return false;
}
}
}
NotifyHeaderTip();
return true;
}

/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
{
Expand Down Expand Up @@ -6020,32 +6035,38 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
}

{
LOCK(cs_main);

if (nCount == 0) {
// Nothing interesting. Stop asking this peers for more headers.
return true;
}

CBlockIndex *pindexLast = NULL;
BOOST_FOREACH(const CBlockHeader& header, headers) {
CValidationState state;
if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
{
LOCK(cs_main);
uint256 hashLastBlock;
for (const CBlockHeader& header : headers) {
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
Misbehaving(pfrom->GetId(), 20);
return error("non-continuous headers sequence");
}
if (!AcceptBlockHeader(header, state, chainparams, &pindexLast)) {
int nDoS;
if (state.IsInvalid(nDoS)) {
if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
std::string strError = "invalid header received " + header.GetHash().ToString();
return error(strError.c_str());
hashLastBlock = header.GetHash();
}
}

CValidationState state;
if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
int nDoS;
if (state.IsInvalid(nDoS)) {
if (nDoS > 0) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), nDoS);
}
return error("invalid header received");
}
}

{
LOCK(cs_main);
if (pindexLast)
UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());

Expand Down Expand Up @@ -6104,8 +6125,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
}
}

NotifyHeaderTip();
}

else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
Expand Down
11 changes: 11 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
* @return True if state.IsValid()
*/
bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool* fNewBlock);

/**
* Process incoming block headers.
*
* @param[in] block The block headers themselves
* @param[out] state This may be set to an Error state if any error occurred processing them
* @param[in] chainparams The params for the chain we want to connect to
* @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
*/
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL);

/** Check whether enough disk space is available for an incoming block */
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
/** Open a block file (blk?????.dat) */
Expand Down

0 comments on commit 415085c

Please sign in to comment.