# Re: [PATCH] Automatic block validation

Post by: satoshi on August 16, 2010, 03:25:54 PM<br>
Last edit: August 16, 2010, 08:06:58 PM by satoshi

That's a difficult approach.

We need to cause a reorg, which will disconnect the invalid chain.

This is code that will rarely ever get tested, and is fairly intricate, so something simple and safe is best.

Here's what I was thinking of. &nbsp;(I haven't tested this yet) &nbsp;It checks all the blocks in the main chain. &nbsp;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. &nbsp;(It can't change pindexBest without actually doing a reorg)

This isn't perfect yet. &nbsp;It still needs to receive one valid block to trigger the reorg.

It would probably be possible to initiate an AddToBlockIndex or Reorganize after the check, but it would require a lot more careful attention. &nbsp;I probably should break out part of AddToBlockIndex that sets the new best block. &nbsp;I'll probably end up doing that instead of the code below.

Code:

```
bool CTxDB::LoadBlockIndex()
{
    ...

    // Verify blocks in the main chain
    vector<CBlockIndex*> vChain;
    for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
    {
        vChain.push_back(pindex);
        CBlock block;
        if (!block.ReadFromDisk(pindex))
            return error("LoadBlockIndex() : block.ReadFromDisk failed");
        if (!block.CheckBlock())
        {
            bnBestChainWork = pindex->pprev->bnChainWork;
            foreach(CBlockIndex* pindex2, vChain)
                pindex2->bnChainWork = 0;
        }
    }

    return true;
}
```

---

Source file: bitcoin-forum-satoshi-nakamoto.tgz

External link: https://bitcointalk.org/index.php?topic=832.msg9754#msg9754
