Skip to content

Commit 37ea542

Browse files
authored
Merge pull request #147 from instagibbs/watchdoglock
rpc watchdog should take cs_main to protect the reconsiderblock queue
2 parents 593969c + 3af9906 commit 37ea542

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

src/main.cpp

+34-38
Original file line numberDiff line numberDiff line change
@@ -2929,22 +2929,7 @@ void ThreadScriptCheck() {
29292929

29302930
bool BitcoindRPCCheck(const bool init)
29312931
{
2932-
//First, we can clear out any blocks thatsomehow are now deemed valid
2933-
//eg reconsiderblock rpc call manually
2934-
std::vector<uint256> vblocksToReconsider;
2935-
pblocktree->ReadInvalidBlockQueue(vblocksToReconsider);
2936-
std::vector<uint256> vblocksToReconsiderAgain;
2937-
BOOST_FOREACH(uint256& blockhash, vblocksToReconsider) {
2938-
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
2939-
if ((pblockindex->nStatus & BLOCK_FAILED_MASK)) {
2940-
vblocksToReconsiderAgain.push_back(blockhash);
2941-
}
2942-
}
2943-
vblocksToReconsider = vblocksToReconsiderAgain;
2944-
vblocksToReconsiderAgain.clear();
2945-
pblocktree->WriteInvalidBlockQueue(vblocksToReconsider);
2946-
2947-
//Next, check for working rpc
2932+
// Check for working rpc
29482933
if (GetBoolArg("-validatepegin", DEFAULT_VALIDATE_PEGIN)) {
29492934
// During init try until a non-RPC_IN_WARMUP result
29502935
while (true) {
@@ -2979,35 +2964,46 @@ bool BitcoindRPCCheck(const bool init)
29792964
}
29802965
}
29812966

2967+
29822968
//Sanity startup check won't reconsider queued blocks
29832969
if (init) {
29842970
return true;
29852971
}
29862972

2987-
/* Getting this far means we either aren't validating pegins(so let's make sure that's why
2988-
it failed previously) or we successfully connected to bitcoind
2989-
Time to reconsider blocks
2990-
*/
2991-
if (vblocksToReconsider.size() > 0) {
2992-
CValidationState state;
2993-
BOOST_FOREACH(const uint256& blockhash, vblocksToReconsider) {
2994-
{
2995-
LOCK(cs_main);
2996-
if (mapBlockIndex.count(blockhash) == 0)
2997-
continue;
2998-
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
2999-
ResetBlockFailureFlags(pblockindex);
3000-
}
3001-
}
2973+
//First, we can clear out any blocks thatsomehow are now deemed valid
2974+
//eg reconsiderblock rpc call manually
2975+
std::vector<uint256> vblocksToReconsider;
2976+
pblocktree->ReadInvalidBlockQueue(vblocksToReconsider);
30022977

3003-
//All blocks are now being reconsidered
3004-
ActivateBestChain(state, Params());
3005-
//This simply checks for DB errors
3006-
if (!state.IsValid()) {
3007-
//Something scary?
2978+
BOOST_FOREACH(uint256& blockhash, vblocksToReconsider) {
2979+
// Skip blocks that have been validated in meantime
2980+
if (mapBlockIndex.count(blockhash) == 0) {
2981+
continue;
2982+
}
2983+
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
2984+
if (!(pblockindex->nStatus & BLOCK_FAILED_MASK)) {
2985+
continue;
30082986
}
2987+
{
2988+
LOCK(cs_main);
2989+
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
2990+
ResetBlockFailureFlags(pblockindex);
2991+
}
2992+
}
2993+
2994+
CValidationState state;
2995+
//All blocks are now being reconsidered
2996+
ActivateBestChain(state, Params());
2997+
//This simply checks for DB errors
2998+
if (!state.IsValid()) {
2999+
//Something scary?
3000+
}
30093001

3010-
//Now to clear out now-valid blocks
3002+
{
3003+
// Clear out now-valid blocks in list
3004+
LOCK(cs_main);
3005+
std::vector<uint256> vblocksToReconsiderAgain;
3006+
pblocktree->ReadInvalidBlockQueue(vblocksToReconsider);
30113007
BOOST_FOREACH(const uint256& blockhash, vblocksToReconsider) {
30123008
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
30133009

@@ -3019,7 +3015,7 @@ bool BitcoindRPCCheck(const bool init)
30193015

30203016
//Write back remaining blocks
30213017
pblocktree->WriteInvalidBlockQueue(vblocksToReconsiderAgain);
3022-
}
3018+
}
30233019
return true;
30243020
}
30253021

0 commit comments

Comments
 (0)