Skip to content

Commit dc6dee4

Browse files
committed
Merge #9183: Final Preparation for main.cpp Split
2c8c57e Document cs_main status when calling into PNB or PNBH (Matt Corallo) 58a215c Use ProcessNewBlockHeaders in CMPCTBLOCK processing (Matt Corallo) a8b936d Use exposed ProcessNewBlockHeaders from ProcessMessages (Matt Corallo) 63fd101 Split ::HEADERS processing into two separate cs_main locks (Matt Corallo) 4a6b1f3 Expose AcceptBlockHeader through main.h (Matt Corallo)
2 parents ad826b3 + 2c8c57e commit dc6dee4

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/main.cpp

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,7 +3643,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
36433643
return true;
36443644
}
36453645

3646-
static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL)
3646+
static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
36473647
{
36483648
AssertLockHeld(cs_main);
36493649
// Check for duplicate
@@ -3692,6 +3692,21 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
36923692
return true;
36933693
}
36943694

3695+
// Exposed wrapper for AcceptBlockHeader
3696+
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3697+
{
3698+
{
3699+
LOCK(cs_main);
3700+
for (const CBlockHeader& header : headers) {
3701+
if (!AcceptBlockHeader(header, state, chainparams, ppindex)) {
3702+
return false;
3703+
}
3704+
}
3705+
}
3706+
NotifyHeaderTip();
3707+
return true;
3708+
}
3709+
36953710
/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
36963711
static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
36973712
{
@@ -5754,6 +5769,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
57545769
CBlockHeaderAndShortTxIDs cmpctblock;
57555770
vRecv >> cmpctblock;
57565771

5772+
{
57575773
LOCK(cs_main);
57585774

57595775
if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
@@ -5762,19 +5778,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
57625778
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
57635779
return true;
57645780
}
5781+
}
57655782

57665783
CBlockIndex *pindex = NULL;
57675784
CValidationState state;
5768-
if (!AcceptBlockHeader(cmpctblock.header, state, chainparams, &pindex)) {
5785+
if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
57695786
int nDoS;
57705787
if (state.IsInvalid(nDoS)) {
5771-
if (nDoS > 0)
5788+
if (nDoS > 0) {
5789+
LOCK(cs_main);
57725790
Misbehaving(pfrom->GetId(), nDoS);
5791+
}
57735792
LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->id);
57745793
return true;
57755794
}
57765795
}
57775796

5797+
LOCK(cs_main);
57785798
// If AcceptBlockHeader returned true, it set pindex
57795799
assert(pindex);
57805800
UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
@@ -5968,14 +5988,14 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
59685988
ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
59695989
}
59705990

5971-
{
5972-
LOCK(cs_main);
5973-
59745991
if (nCount == 0) {
59755992
// Nothing interesting. Stop asking this peers for more headers.
59765993
return true;
59775994
}
59785995

5996+
CBlockIndex *pindexLast = NULL;
5997+
{
5998+
LOCK(cs_main);
59795999
CNodeState *nodestate = State(pfrom->GetId());
59806000

59816001
// If this looks like it could be a block announcement (nCount <
@@ -6005,23 +6025,31 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
60056025
return true;
60066026
}
60076027

6008-
CBlockIndex *pindexLast = NULL;
6009-
BOOST_FOREACH(const CBlockHeader& header, headers) {
6010-
CValidationState state;
6011-
if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
6028+
uint256 hashLastBlock;
6029+
for (const CBlockHeader& header : headers) {
6030+
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
60126031
Misbehaving(pfrom->GetId(), 20);
60136032
return error("non-continuous headers sequence");
60146033
}
6015-
if (!AcceptBlockHeader(header, state, chainparams, &pindexLast)) {
6016-
int nDoS;
6017-
if (state.IsInvalid(nDoS)) {
6018-
if (nDoS > 0)
6019-
Misbehaving(pfrom->GetId(), nDoS);
6020-
return error("invalid header received");
6034+
hashLastBlock = header.GetHash();
6035+
}
6036+
}
6037+
6038+
CValidationState state;
6039+
if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
6040+
int nDoS;
6041+
if (state.IsInvalid(nDoS)) {
6042+
if (nDoS > 0) {
6043+
LOCK(cs_main);
6044+
Misbehaving(pfrom->GetId(), nDoS);
60216045
}
6046+
return error("invalid header received");
60226047
}
60236048
}
60246049

6050+
{
6051+
LOCK(cs_main);
6052+
CNodeState *nodestate = State(pfrom->GetId());
60256053
if (nodestate->nUnconnectingHeaders > 0) {
60266054
LogPrint("net", "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->id, nodestate->nUnconnectingHeaders);
60276055
}
@@ -6093,8 +6121,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
60936121
}
60946122
}
60956123
}
6096-
6097-
NotifyHeaderTip();
60986124
}
60996125

61006126
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing

src/main.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,28 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
223223
* Note that we guarantee that either the proof-of-work is valid on pblock, or
224224
* (and possibly also) BlockChecked will have been called.
225225
*
226+
* Call without cs_main held.
227+
*
226228
* @param[in] pblock The block we want to process.
227229
* @param[in] fForceProcessing Process this block even if unrequested; used for non-network block sources and whitelisted peers.
228230
* @param[out] dbp The already known disk position of pblock, or NULL if not yet stored.
229231
* @param[out] fNewBlock A boolean which is set to indicate if the block was first received via this call
230232
* @return True if state.IsValid()
231233
*/
232234
bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool* fNewBlock);
235+
236+
/**
237+
* Process incoming block headers.
238+
*
239+
* Call without cs_main held.
240+
*
241+
* @param[in] block The block headers themselves
242+
* @param[out] state This may be set to an Error state if any error occurred processing them
243+
* @param[in] chainparams The params for the chain we want to connect to
244+
* @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
245+
*/
246+
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL);
247+
233248
/** Check whether enough disk space is available for an incoming block */
234249
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
235250
/** Open a block file (blk?????.dat) */

0 commit comments

Comments
 (0)