Skip to content

Commit 019d26a

Browse files
Don't assert if we were beaten to the block
A timing window exists where a wallet could be creating a new block from within the miner thread when a new block is received to the wallet. This window will create a situation where TestBlockValidity() fails because the chain tip has changed between the time it created the new block and the time it tested the validity of the block. This situation would result in the wallet being asserted; however this is a little overkill. rather than asserting if the tip has changed, it is better to throw the block away. This problem was revealed during a testnet test of an altcoin, and very prevalent when multiple wallet existed with the exact same number of staking coins received in the same transaction; or when multiple wallets were staking the same coins via import private key. The problem happens significantly less in more normal circumstances, but was still observed in a testing environment with fast blocks. It is likely that this scenario has been encountered but never determined to be root cause, as a crashed wallet could be restarted, re-indexed and never investigated further.
1 parent 68c81c4 commit 019d26a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5176,7 +5176,11 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, CBlock* pblock, CDis
51765176
bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex* const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
51775177
{
51785178
AssertLockHeld(cs_main);
5179-
assert(pindexPrev && pindexPrev == chainActive.Tip());
5179+
assert(pindexPrev);
5180+
if (pindexPrev != chainActive.Tip()) {
5181+
LogPrintf("TestBlockValidity(): No longer working on chain tip\n");
5182+
return false;
5183+
}
51805184

51815185
CCoinsViewCache viewNew(pcoinsTip);
51825186
CBlockIndex indexDummy(block);

0 commit comments

Comments
 (0)