From 6416fa5d58c8d4dffc1a57cca2172a6cfc07a989 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sun, 19 Jan 2014 00:53:15 -0200 Subject: [PATCH] another try to fix the infinite recursion triggered when the new (longer) chain contains an invalid transaction (due to older, buggy, twisterd or attacker). i'm not entirely convinced that original bitcoin couldn't be attacked the same way (51% is still needed, but crashing the client is pretty bad). --- src/main.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index a50eeb3f..9790e7ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1299,9 +1299,22 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew) // List of what to connect (typically only pindexNew) vector vConnect; - for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev) + for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev) { vConnect.push_back(pindex); + if( pindex->nStatus & BLOCK_FAILED_MASK ) { + BOOST_FOREACH(CBlockIndex *pindex, vConnect) { + pindex->nStatus |= BLOCK_FAILED_CHILD; + setBlockIndexValid.erase(pindex); + pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex)); + } + vConnect.clear(); + } + } reverse(vConnect.begin(), vConnect.end()); + + if (vConnect.size() < vDisconnect.size()) { + return error("SetBestChain() : Tried to connect to a shorter chain (invalid blocks)"); + } if (vDisconnect.size() > 0) { printf("REORGANIZE: Disconnect %"PRIszu" blocks; %s..\n", vDisconnect.size(), pfork->GetBlockHash().ToString().c_str());