Pad input to 32-bit alignment (this makes transactions aligned too).

Also add missing le16_to_cpu().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fix debug checks in block_add().

It doesn't need to return bool any more, and we should only check
chains after all pointers updated.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Use complete transaction for tx hash (including signature)

This means that two otherwise-identical gateway injections can
be differentiated.  It's also simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
More careful checking of batches.

We used not to check the transactions themselves; now we're going to
add backrefs for the inputs, we can properly report any errors in
a transaction, so we do that.

In addition, after checking the transactions are well-formed, we
check the merkle before other checks.  The difference is that if
the merkle matches, then any errors are actually errors in the block
which (in the next patch) we will use to taint the block and tell
all our peers about.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Don't include extra information on transaction problems.

We really need to send that report in a separate packet (since
we need such a packet anyway to report to all nodes if we discover
a transaction is now invalid).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Add intrastructure for complaints.

Given we have only partial knowledge, we might later discover that
a block or transaction is faulty.  Add a list of "complaints" to each
peer, for telling them about these.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Complain when a transaction has bad inputs.

We don't actually handle receiving such complaints: it shouldn't happen
with the client as it stands, since we insist on complete knowledge.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Extract core functions to create/check merkle proofs.

We will use these to generate the complaint proofs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Restore Makefile accidentally erased in c788d39d6fa39837d817665e54f1f43ca6ac14f3

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Add packet creation helpers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Generate messages about bad blocks.

When we discover a block contains an invalid transaction, we need to
taint it, and tell everyone.  Those messages contain enough information
to prove the problem (we never assume any peer has full knowledge).

Obviously a block which contains an invalid transaction is invalid:
we neither aim to find out more about it nor mine on any block below it.
We do remember the block and the proof: if anyone mentions it (or
an descendent) we send them the complaint message.

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

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Have check_transaction return all inputs.

Slightly nicer API, and easy to do.

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

And split helpers into chain.c, so generate can use them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Add backrefs to transaction inputs in blocks.

This allows a node to prove that an input is invalid, by showing
the transaction referred to by the input reference.  Otherwise
a node with partial knowledge has no way of knowing if a transaction
simply references a transaction it doesn't know about (OK) or one
which doesn't exist (not OK).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Standardize type for proving a transaction is in a block.

Simplifies the protocol and the code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Add a horizon (1 month).

Pettycoin is not a store of value!

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

I talked about doing it, but didn't actually fix it.

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

Features get voted in by having a whole difficulty block voted by 75% of
the blocks.  Then after another difficulty block, we expect the version
to increment to indicate an incompatible change.

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