Skip to content

Commit a616206

Browse files
committed
Give peer time-adjustment data an own lock
Instead of relying on cs_main (defined in a different module) to prevent concurrent access to it.
1 parent 0d09b3e commit a616206

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int64 CTransaction::nMinTxFee = 10000; // Override with -mintxfee
4949
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying) */
5050
int64 CTransaction::nMinRelayTxFee = 10000;
5151

52-
CMedianFilter<int> cPeerBlockCounts(8, 0); // Amount of blocks that other nodes claim to have
52+
static CMedianFilter<int> cPeerBlockCounts(8, 0); // Amount of blocks that other nodes claim to have
5353

5454
map<uint256, CBlock*> mapOrphanBlocks;
5555
multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
@@ -3460,8 +3460,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
34603460

34613461
LogPrintf("receive version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str());
34623462

3463-
LOCK(cs_main);
34643463
AddTimeData(pfrom->addr, nTime);
3464+
3465+
LOCK(cs_main);
34653466
cPeerBlockCounts.input(pfrom->nStartingHeight);
34663467
}
34673468

src/util.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ bool fServer = false;
8181
string strMiscWarning;
8282
bool fNoListen = false;
8383
bool fLogTimestamps = false;
84-
CMedianFilter<int64> vTimeOffsets(200,0);
8584
volatile bool fReopenDebugLog = false;
8685

8786
// Init OpenSSL library multithreading support
@@ -1296,10 +1295,12 @@ void SetMockTime(int64 nMockTimeIn)
12961295
nMockTime = nMockTimeIn;
12971296
}
12981297

1298+
static CCriticalSection cs_nTimeOffset;
12991299
static int64 nTimeOffset = 0;
13001300

13011301
int64 GetTimeOffset()
13021302
{
1303+
LOCK(cs_nTimeOffset);
13031304
return nTimeOffset;
13041305
}
13051306

@@ -1312,12 +1313,14 @@ void AddTimeData(const CNetAddr& ip, int64 nTime)
13121313
{
13131314
int64 nOffsetSample = nTime - GetTime();
13141315

1316+
LOCK(cs_nTimeOffset);
13151317
// Ignore duplicates
13161318
static set<CNetAddr> setKnown;
13171319
if (!setKnown.insert(ip).second)
13181320
return;
13191321

13201322
// Add data
1323+
static CMedianFilter<int64> vTimeOffsets(200,0);
13211324
vTimeOffsets.input(nOffsetSample);
13221325
LogPrintf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
13231326
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)

0 commit comments

Comments
 (0)