Preview: headers-first synchronization

Here's a first (well, second) version of a fully-functional headers-first synchronization. It's not ready to be merged (it breaks reindexing, which cannot deal with out-of-order blocks on disk yet).

Many changes:
* Do not use 'getblocks', but 'getheaders', and use it to build a headers tree.
* Blocks are fetched in parallel from all available outbound peers, using a limited moving window. When one peer stalls the movement of the window, it is disconnected.
* No more orphan blocks. At all. We only ever request a block for which we have verified the headers, and store it to disk immediately. This means that a disk-fill attack would require PoW.
* Some new fields in getpeerinfo:
  * 'syncheight': the height up to which we've validated this peer's headers.
  * 'commonheight': the height up to which we have all blocks in common with this peer.
  * 'inflight': the heights we're currently requesting from this peer.
* Require protocol version 31800 for every peer (released in december 2010).
* No more syncnode (we sync from everyone we can, though limited to 1 during initial *headers* sync).
* Introduce some extra named constants and comments.
* Several bugfixes:
  * CChain::GetLocator() didn't work correctly across the CChain end.
  * FindMostWorkChain() failed to remove the actually failed block from setBlockIndexValid.
  * ProcessMessage() failed to act on ban/reject notices resulting from validation failures during the direct execution (CheckBlock, ...).

What about when a block is sent without a header or invite first?
On Sep 7, 2014 6:35 PM, "Pieter Wuille" <notifications@github.com> wrote:

> In src/main.cpp:
>
> >          if (pindex->nStatus & BLOCK_FAILED_MASK)
> >              return state.Invalid(error("AcceptBlock() : block is marked invalid"), 0, "duplicate");
> > -    }
> > -
> > -    CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint();
> > -    if (pcheckpoint && block.hashPrevBlock != (chainActive.Tip() ? chainActive.Tip()->GetBlockHash() : uint256(0)))
> > -    {
> > -        // Extra checks to prevent "fill up memory by spamming with bogus blocks"
>
> We certainly do, but every case this protects against is now caught in the
> proof of work check below. Previously, this check was useful, because we
> wanted to verify whether a block was viable without knowing its parent. In
> headers-first, we always know all headers before processing a block, so it
> doesn't add anything anymore.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/bitcoin/bitcoin/pull/4468/files#r17214249>.
>