From 4a6b1f36b773001f6c5a8e1d3d196833e4fb872d Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 2 Oct 2016 12:22:05 -0400 Subject: Expose AcceptBlockHeader through main.h --- src/main.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 05442057e..4a2682bc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3649,7 +3649,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co 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 @@ -3698,6 +3698,21 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state return true; } +// Exposed wrapper for AcceptBlockHeader +bool ProcessNewBlockHeaders(const std::vector& 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) { -- cgit v1.2.3 From 63fd101c529cf6e2bc5ecab244fd56e2c5be521f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 3 Nov 2016 16:34:31 -0400 Subject: 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. --- src/main.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 4a2682bc7..4cd5bebb7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5982,14 +5982,14 @@ 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; + { + LOCK(cs_main); CNodeState *nodestate = State(pfrom->GetId()); // If this looks like it could be a block announcement (nCount < @@ -6019,7 +6019,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return true; } - CBlockIndex *pindexLast = NULL; BOOST_FOREACH(const CBlockHeader& header, headers) { CValidationState state; if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) { @@ -6035,7 +6034,11 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, } } } + } + { + LOCK(cs_main); + CNodeState *nodestate = State(pfrom->GetId()); if (nodestate->nUnconnectingHeaders > 0) { LogPrint("net", "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->id, nodestate->nUnconnectingHeaders); } -- cgit v1.2.3 From a8b936df2053e68208d106cddec06227ef563e41 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 3 Nov 2016 16:56:33 -0400 Subject: Use exposed ProcessNewBlockHeaders from ProcessMessages --- src/main.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 4cd5bebb7..f88efe7b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6019,22 +6019,27 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return true; } - BOOST_FOREACH(const CBlockHeader& header, headers) { - CValidationState state; - if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) { + 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); - return error("invalid header received"); + 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); @@ -6110,8 +6115,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, } } } - - NotifyHeaderTip(); } else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing -- cgit v1.2.3 From 58a215ce8c13b900cf982c39f8ee4879290d1a95 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 3 Nov 2016 17:01:39 -0400 Subject: Use ProcessNewBlockHeaders in CMPCTBLOCK processing --- src/main.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index f88efe7b5..04a55b3dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5768,6 +5768,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, CBlockHeaderAndShortTxIDs cmpctblock; vRecv >> cmpctblock; + { LOCK(cs_main); if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) { @@ -5776,19 +5777,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()); return true; } + } CBlockIndex *pindex = NULL; CValidationState state; - if (!AcceptBlockHeader(cmpctblock.header, state, chainparams, &pindex)) { + if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) { int nDoS; if (state.IsInvalid(nDoS)) { - if (nDoS > 0) + if (nDoS > 0) { + LOCK(cs_main); Misbehaving(pfrom->GetId(), nDoS); + } LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->id); return true; } } + LOCK(cs_main); // If AcceptBlockHeader returned true, it set pindex assert(pindex); UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash()); -- cgit v1.2.3