Skip to content

Commit c141c14

Browse files
committed
Merge #7942: locking for Misbehave() and other cs_main locking fixes
719de56 lock cs_main for chainActive (Kaz Wesley) efb54ba lock cs_main for State/Misbehaving (Kaz Wesley)
2 parents ae5575b + 719de56 commit c141c14

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,14 +2922,15 @@ static void NotifyHeaderTip() {
29222922
*/
29232923
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
29242924
CBlockIndex *pindexMostWork = NULL;
2925+
CBlockIndex *pindexNewTip = NULL;
29252926
do {
29262927
boost::this_thread::interruption_point();
29272928
if (ShutdownRequested())
29282929
break;
29292930

2930-
CBlockIndex *pindexNewTip = NULL;
29312931
const CBlockIndex *pindexFork;
29322932
bool fInitialDownload;
2933+
int nNewHeight;
29332934
{
29342935
LOCK(cs_main);
29352936
CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2952,6 +2953,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29522953
pindexNewTip = chainActive.Tip();
29532954
pindexFork = chainActive.FindFork(pindexOldTip);
29542955
fInitialDownload = IsInitialBlockDownload();
2956+
nNewHeight = chainActive.Height();
29552957
}
29562958
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
29572959

@@ -2980,7 +2982,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29802982
{
29812983
LOCK(cs_vNodes);
29822984
BOOST_FOREACH(CNode* pnode, vNodes) {
2983-
if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate)) {
2985+
if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate)) {
29842986
BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
29852987
pnode->PushBlockHash(hash);
29862988
}
@@ -2993,7 +2995,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29932995
}
29942996
}
29952997
}
2996-
} while(pindexMostWork != chainActive.Tip());
2998+
} while (pindexNewTip != pindexMostWork);
29972999
CheckBlockIndex(chainparams.GetConsensus());
29983000

29993001
// Write changes periodically to disk, after relay.
@@ -4569,6 +4571,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45694571
strCommand == NetMsgType::FILTERCLEAR))
45704572
{
45714573
if (pfrom->nVersion >= NO_BLOOM_VERSION) {
4574+
LOCK(cs_main);
45724575
Misbehaving(pfrom->GetId(), 100);
45734576
return false;
45744577
} else {
@@ -4584,6 +4587,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45844587
if (pfrom->nVersion != 0)
45854588
{
45864589
pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
4590+
LOCK(cs_main);
45874591
Misbehaving(pfrom->GetId(), 1);
45884592
return false;
45894593
}
@@ -4643,7 +4647,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46434647
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
46444648

46454649
// Potentially mark this peer as a preferred download peer.
4650+
{
4651+
LOCK(cs_main);
46464652
UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
4653+
}
46474654

46484655
// Change version
46494656
pfrom->PushMessage(NetMsgType::VERACK);
@@ -4701,6 +4708,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
47014708
else if (pfrom->nVersion == 0)
47024709
{
47034710
// Must have a version message before anything else
4711+
LOCK(cs_main);
47044712
Misbehaving(pfrom->GetId(), 1);
47054713
return false;
47064714
}
@@ -4736,6 +4744,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
47364744
return true;
47374745
if (vAddr.size() > 1000)
47384746
{
4747+
LOCK(cs_main);
47394748
Misbehaving(pfrom->GetId(), 20);
47404749
return error("message addr size() = %u", vAddr.size());
47414750
}
@@ -4803,6 +4812,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
48034812
vRecv >> vInv;
48044813
if (vInv.size() > MAX_INV_SZ)
48054814
{
4815+
LOCK(cs_main);
48064816
Misbehaving(pfrom->GetId(), 20);
48074817
return error("message inv size() = %u", vInv.size());
48084818
}
@@ -4878,6 +4888,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
48784888
vRecv >> vInv;
48794889
if (vInv.size() > MAX_INV_SZ)
48804890
{
4891+
LOCK(cs_main);
48814892
Misbehaving(pfrom->GetId(), 20);
48824893
return error("message getdata size() = %u", vInv.size());
48834894
}
@@ -5129,6 +5140,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
51295140
// Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
51305141
unsigned int nCount = ReadCompactSize(vRecv);
51315142
if (nCount > MAX_HEADERS_RESULTS) {
5143+
LOCK(cs_main);
51325144
Misbehaving(pfrom->GetId(), 20);
51335145
return error("headers message size = %u", nCount);
51345146
}
@@ -5390,8 +5402,11 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
53905402
LOCK(pfrom->cs_filter);
53915403

53925404
if (!filter.IsWithinSizeConstraints())
5405+
{
53935406
// There is no excuse for sending a too-large filter
5407+
LOCK(cs_main);
53945408
Misbehaving(pfrom->GetId(), 100);
5409+
}
53955410
else
53965411
{
53975412
delete pfrom->pfilter;
@@ -5411,13 +5426,17 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
54115426
// and thus, the maximum size any matched object can have) in a filteradd message
54125427
if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
54135428
{
5429+
LOCK(cs_main);
54145430
Misbehaving(pfrom->GetId(), 100);
54155431
} else {
54165432
LOCK(pfrom->cs_filter);
54175433
if (pfrom->pfilter)
54185434
pfrom->pfilter->insert(vData);
54195435
else
5436+
{
5437+
LOCK(cs_main);
54205438
Misbehaving(pfrom->GetId(), 100);
5439+
}
54215440
}
54225441
}
54235442

0 commit comments

Comments
 (0)