recv_block: log block height when we get error.

Usually it's UNKNOWN_PREV (ie. we're syncing); it's good to know what
block we're up to.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
peer: remove erroneous peers from the cache.

We kept reconnecting to a peer when a bug caused a bad welcome.  Rename
close_peer to close_bad_peer to make it really clear.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
peer: change peer port when they give is a listen port.

This way we match the cache; the actual port they connected from is in
the log prefix already.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: fix endian annotation on listen_port.

We treat it as le16 everywhere, the definition is wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
difficulty: more helper functions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: add multiple previous block hashes to block hash (BREAKS PROTOCOL)

Include the hashes of multiple previous blocks, not just the height-1
block.

The idea of such a structure (sometimes called an SPV skipchain) came
from bitcoin discussions, but it's particularly powerful for coins
with a horizon like pettycoin.

It allows discarding of some most blocks while maintaining proof that
work was done.  With a 10 second block time, we generate 3,153,600
blocks a year: 3.2GB with 1k blocks.  Using the skipchain we can weed
this down by a factor of 10 (more if we're lucky, a bit less if we're
unlucky).

I came across this idea in Mark Friedenbach's post on Compact SPV proofs:
  http://sourceforge.net/p/bitcoin/mailman/message/32111357/

But he traces the original idea to Andrew Miller's "The High-Value-Hash
Highway":
  https://bitcointalk.org/index.php?topic=98986.0

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: check prev field on new blocks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: fixed 8k interest bitmap (BREAKS PROTOCOL)

Might as well spell it out; it's only 8k.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Move detached block handing into its own file.

We'll want this for syncing, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
recv_block: handle blocks which aren't inside protocol_pkt_block.

We're going to change protocol_pkt_welcome to include a marshaled block,
but all the code assumes a protocol_pkt_block.  Remove this assumption,
though we still need to pass in the tal'ed ptr, so we can steal it.

This lets us fix a wart in the submitblock JSON API, too: it doesn't
need the protocol_pkt_block header any more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
num_prevs() function.

Helper to calculate how many (non-zero) previous hashes we have.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: Make welcome include "best" block (BREAKS PROTOCOL)

This approach is inspired by Gregory Maxwell's Reverse_header-fetching_sync:
https://en.bitcoin.it/wiki/User:Gmaxwell/Reverse_header-fetching_sync

It's made more powerful by the presence of multiple previous block
pointers, so we can get much more parallelism.

We don't currently compare results across different peers, so we could
be lead down the garden path by some peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pending: Remove incorrect comment, document weird behaviour.

I noticed that after a json sendrawtransaction injection, the node
was doing a PROTOCOL_PKT_GET_TX for transactions it already had.  This is
because add_pending_tx() returns ECODE_INPUT_UNKNOWN as it doesn't look
in the pending queue for inputs.

It's a wart, but it's not a fatal flaw.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
recv_block: get as many ancestors as we can at once.

This has the advantage that the queries can spread across multiple peers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: hash children for syncing (BREAKS PROTOCOL)

This makes sure we've got the correct tree, rather than just using a
counter.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
recv_block: calculate minimum possible difficulty.

Using 1/16 of last block's difficulty is wrong for very old blocks:
get more sophisticated by calculating minimum based on what we do
know.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
protocol: fix fee calculation (BREAKS PROTOCOL)

We should charge 0.3% of the actual transferred amount (ie. not
including change).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pettycoin-generate: don't need timestamp.o

Use ccan/time directly, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rename shard_nums to num_txs

Simple renaming: it really is the number of transactions within
each shard, so num_txs[] is clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
block_info: structure to contain block on the wire.

Since we're going to change it for merge mining, encapsulate it now to
avoid massive refactor confusing the block structure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>