Refactor message handling for lower latency

- ThreadMessageHandler woken up by events that require it
- TRY_LOCKs replaced with LOCKs to limit worst-case delays
- Trickle behaviour split into separate thread
- SendMessages() split in two: original now only handles cs_main-dependent work
Add CaughtUp() function (needed by later commits)
Rearrange ProcessMessages + add nLastDataPos variable for partial message tracking.
Don't request blocks when block invs received unless it's the syncnode, or we've CaughtUp()
Force selection of a new syncnode since the current syncnode is about to stop sending blocks.
Replacement stalled block logic. Depending on the situation, it will either disconnect
the peer (as previous behaviour), or it will reselect a syncnode (e.g. when 500 blocks
have been received from the syncnode).
Add commandline option for sync timeout
Rename MarkBlockAsReceived to MarkBlockAsRequested to reduce confusion.
Show downloaded block size and time taken to download.
Debug when a block is incoming and show how many blocks still to come.
prevent peer flooding request queue for an inv

mapAlreadyAskedFor does not keep track of which peer has a request queued for a
particular tx. As a result, a peer can blind a node to a tx indefinitely by
sending many invs for the same tx, and then never replying to getdatas for it.
Each inv received will be placed 2 minutes farther back in mapAlreadyAskedFor,
so a short message containing 10 invs would render that tx unavailable for 20
minutes.

This is fixed by disallowing a peer from having more than one entry for a
particular inv in mapAlreadyAskedFor at a time.
Improve getblocks (reception) logging
Only respond to getblocks requests that are asking for substantially newer block invs.
Cache signature hashes during evaluation
addrman: Do not propagate obviously poor addresses onto the network
Include undo data to startup checks and refactor them onto a function.

The check is now simpler, somewhat more efficient, and
covers more data, to be sure the node can reorganize.
Add a switch to allow the node to run in a pruned state

These are the main functional changes on this state:

* The node is prevented to run with a wallet.
* Check for data at startup is limited to last 288 blocks.
* NODE_NETWORK flag is unset.
* Requests for pruned blocks from other peers are ignored.
Changed LevelDB cursors to use scoped pointers to ensure destruction when going out of scope.
This corrects a bug where an exception thrown reading from the database causes the cursor to
be left open, which causes an assertion error to occur when the database is deleted (around
line 938 of init.cpp).
Merge remote-tracking branch 'bitcoin/pr/4656'
Merge remote-tracking branch 'bitcoin/pr/4632'