{
"subject": "Re: [PATCH] Automatic block validation",
"content": {
"format": "html",
"body": "<div class=\"post\">That's a difficult approach.<br/><br/>We need to cause a reorg, which will disconnect the invalid chain.<br/><br/>This is code that will rarely ever get tested, and is fairly intricate, so something simple and safe is best.<br/><br/>Here's what I was thinking of. (I haven't tested this yet) It checks all the blocks in the main chain. If it finds a bad one, it sets all that chain's bnChainWork to 0 so it can't win best chain again, and it reduces best chain work to the fork level so any new block after the fork will cause a reorg. (It can't change pindexBest without actually doing a reorg)<br/><br/>This isn't perfect yet. It still needs to receive one valid block to trigger the reorg. <br/><br/>It would probably be possible to initiate an AddToBlockIndex or Reorganize after the check, but it would require a lot more careful attention. I probably should break out part of AddToBlockIndex that sets the new best block. I'll probably end up doing that instead of the code below.<br/><br/><div class=\"codeheader\">Code:</div><div class=\"code\">bool CTxDB::LoadBlockIndex()<br/>{<br/> ...<br/><br/> // Verify blocks in the main chain<br/> vector<CBlockIndex*> vChain;<br/> for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)<br/> {<br/> vChain.push_back(pindex);<br/> CBlock block;<br/> if (!block.ReadFromDisk(pindex))<br/> return error(\"LoadBlockIndex() : block.ReadFromDisk failed\");<br/> if (!block.CheckBlock())<br/> {<br/> bnBestChainWork = pindex->pprev->bnChainWork;<br/> foreach(CBlockIndex* pindex2, vChain)<br/> pindex2->bnChainWork = 0;<br/> }<br/> }<br/><br/> return true;<br/>}<br/></div></div>"
},
"source": {
"name": "Bitcoin Forum",
"url": "https://bitcointalk.org/index.php?topic=832.msg9754#msg9754"
},
"date": "2010-08-16T15:25:54Z"
}