Skip to content

Commit 91adcd7

Browse files
furszyFuzzbawls
authored andcommitted
masternode: CalculateScore and GetBlockHash accessing to chainActive without cs_main fix.
Github-Pull: #1791 Rebased-From: a14aba3
1 parent eeb112f commit 91adcd7

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/masternode.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,31 @@ std::map<int64_t, uint256> mapCacheBlockHashes;
2323
//Get the last hash that matches the modulus given. Processed in reverse order
2424
bool GetBlockHash(uint256& hash, int nBlockHeight)
2525
{
26-
if (chainActive.Tip() == NULL) return false;
26+
int tipHeight;
27+
const CBlockIndex* tipIndex;
28+
{
29+
LOCK(cs_main);
30+
CBlockIndex *pindex = chainActive.Tip();
31+
if (!pindex) return false;
32+
tipHeight = pindex->nHeight;
33+
tipIndex = mapBlockIndex[pindex->GetBlockHash()];
34+
}
2735

2836
if (nBlockHeight == 0)
29-
nBlockHeight = chainActive.Tip()->nHeight;
37+
nBlockHeight = tipHeight;
3038

3139
if (mapCacheBlockHashes.count(nBlockHeight)) {
3240
hash = mapCacheBlockHashes[nBlockHeight];
3341
return true;
3442
}
3543

36-
const CBlockIndex* BlockLastSolved = chainActive.Tip();
37-
const CBlockIndex* BlockReading = chainActive.Tip();
44+
const CBlockIndex* BlockLastSolved = tipIndex;
45+
const CBlockIndex* BlockReading = tipIndex;
3846

39-
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || chainActive.Tip()->nHeight + 1 < nBlockHeight) return false;
47+
if (BlockLastSolved == nullptr || BlockLastSolved->nHeight == 0 || tipHeight + 1 < nBlockHeight) return false;
4048

4149
int nBlocksAgo = 0;
42-
if (nBlockHeight > 0) nBlocksAgo = (chainActive.Tip()->nHeight + 1) - nBlockHeight;
50+
if (nBlockHeight > 0) nBlocksAgo = (tipHeight + 1) - nBlockHeight;
4351
assert(nBlocksAgo >= 0);
4452

4553
int n = 0;
@@ -157,7 +165,10 @@ bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb)
157165
//
158166
uint256 CMasternode::CalculateScore(int mod, int64_t nBlockHeight)
159167
{
160-
if (chainActive.Tip() == NULL) return UINT256_ZERO;
168+
{
169+
LOCK(cs_main);
170+
if (chainActive.Tip() == NULL) return UINT256_ZERO;
171+
}
161172

162173
uint256 hash;
163174
uint256 aux = vin.prevout.hash + vin.prevout.n;

0 commit comments

Comments
 (0)