@@ -3643,7 +3643,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
3643
3643
return true ;
3644
3644
}
3645
3645
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)
3647
3647
{
3648
3648
AssertLockHeld (cs_main);
3649
3649
// Check for duplicate
@@ -3692,6 +3692,21 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
3692
3692
return true ;
3693
3693
}
3694
3694
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
+
3695
3710
/* * Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
3696
3711
static bool AcceptBlock (const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested , const CDiskBlockPos* dbp, bool * fNewBlock )
3697
3712
{
@@ -5754,6 +5769,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
5754
5769
CBlockHeaderAndShortTxIDs cmpctblock;
5755
5770
vRecv >> cmpctblock;
5756
5771
5772
+ {
5757
5773
LOCK (cs_main);
5758
5774
5759
5775
if (mapBlockIndex.find (cmpctblock.header .hashPrevBlock ) == mapBlockIndex.end ()) {
@@ -5762,19 +5778,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
5762
5778
connman.PushMessage (pfrom, msgMaker.Make (NetMsgType::GETHEADERS, chainActive.GetLocator (pindexBestHeader), uint256 ()));
5763
5779
return true ;
5764
5780
}
5781
+ }
5765
5782
5766
5783
CBlockIndex *pindex = NULL ;
5767
5784
CValidationState state;
5768
- if (!AcceptBlockHeader ( cmpctblock.header , state, chainparams, &pindex)) {
5785
+ if (!ProcessNewBlockHeaders ({ cmpctblock.header } , state, chainparams, &pindex)) {
5769
5786
int nDoS;
5770
5787
if (state.IsInvalid (nDoS)) {
5771
- if (nDoS > 0 )
5788
+ if (nDoS > 0 ) {
5789
+ LOCK (cs_main);
5772
5790
Misbehaving (pfrom->GetId (), nDoS);
5791
+ }
5773
5792
LogPrintf (" Peer %d sent us invalid header via cmpctblock\n " , pfrom->id );
5774
5793
return true ;
5775
5794
}
5776
5795
}
5777
5796
5797
+ LOCK (cs_main);
5778
5798
// If AcceptBlockHeader returned true, it set pindex
5779
5799
assert (pindex);
5780
5800
UpdateBlockAvailability (pfrom->GetId (), pindex->GetBlockHash ());
@@ -5968,14 +5988,14 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
5968
5988
ReadCompactSize (vRecv); // ignore tx count; assume it is 0.
5969
5989
}
5970
5990
5971
- {
5972
- LOCK (cs_main);
5973
-
5974
5991
if (nCount == 0 ) {
5975
5992
// Nothing interesting. Stop asking this peers for more headers.
5976
5993
return true ;
5977
5994
}
5978
5995
5996
+ CBlockIndex *pindexLast = NULL ;
5997
+ {
5998
+ LOCK (cs_main);
5979
5999
CNodeState *nodestate = State (pfrom->GetId ());
5980
6000
5981
6001
// If this looks like it could be a block announcement (nCount <
@@ -6005,23 +6025,31 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
6005
6025
return true ;
6006
6026
}
6007
6027
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) {
6012
6031
Misbehaving (pfrom->GetId (), 20 );
6013
6032
return error (" non-continuous headers sequence" );
6014
6033
}
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);
6021
6045
}
6046
+ return error (" invalid header received" );
6022
6047
}
6023
6048
}
6024
6049
6050
+ {
6051
+ LOCK (cs_main);
6052
+ CNodeState *nodestate = State (pfrom->GetId ());
6025
6053
if (nodestate->nUnconnectingHeaders > 0 ) {
6026
6054
LogPrint (" net" , " peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n " , pfrom->id , nodestate->nUnconnectingHeaders );
6027
6055
}
@@ -6093,8 +6121,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
6093
6121
}
6094
6122
}
6095
6123
}
6096
-
6097
- NotifyHeaderTip ();
6098
6124
}
6099
6125
6100
6126
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex ) // Ignore blocks received while importing
0 commit comments