Commit Graph

617 Commits

Author SHA1 Message Date
Rusty Russell 06d2c19c41 test/test_protocol: make pretty(-ish) SVG diagrams.
Could make them much prettier, but they're functional.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 15:02:08 +09:30
Rusty Russell 126c91e1b1 test/test_protocol: don't allow multiple commits in flight by default.
Add "nocommitwait" for the two cases we test that, otherwise add
assertions that we never end up dealing with anything other than
the previous commit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 15:01:10 +09:30
Rusty Russell a77466aa21 test/test_protocol: Keep pointer to previous state.
Since our pre-change state is always the same as the previous step's
post-change state, we can simply keep a pointer, with a dummy empty
state for the initial one.

We could function-wrap it, but this change is even simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:58:06 +09:30
Rusty Russell 4c1b50baab test/test_protocol: don't bother keeping acked (outgoing) queue.
We can simply set a boolean to indicate there are changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:56:08 +09:30
Rusty Russell bc8cd0dce7 test/test_protocol: don't manually reapply changes.
This finished the previous patch by simply copying funding_next to funding
when we want to apply changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:55:58 +09:30
Rusty Russell 52db7ae0c4 test/test_protocol: keep cache of state with all changes applied.
This makes it easier to test for validity, though we still double-check
that a change doesn't overlap previous changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:51:25 +09:30
Rusty Russell c710a64ccf Makefile: support for suppressing valgrind.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:49:54 +09:30
Rusty Russell 388dfc355e test/test_protocol: add fee support.
We simply record how many fee changes there are, rather than supporting
a particular level.

Fees are tricky: it's a noop to apply them when incoming, but we apply them
when they've been acked.  Unlike HTLC modifications, which are symmetric,
fee updates only apply when returning to the originating node.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:49:32 +09:30
Rusty Russell 056c219bc1 test/test_protocol: add checksync command to assert nodes are in sync.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:48:11 +09:30
Rusty Russell 7f90d183da test/test_protocol: simulator for the updated wire-protocol BOLT.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:47:44 +09:30
Rusty Russell 35d1b13cde daemon: commit outstanding changes via timer.
While useful for testing, it doesn't make sense to have an explicit commit
command; we should commit whenever there are outstanding changes.

We have a 10ms timer to allow limited batching, however.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:30:11 +09:30
Rusty Russell fe1ba96332 daemon: time options use opt_time.
Currently this mean --bitcoin-poll; we're going to change the other time
options to block heights anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:29:12 +09:30
Rusty Russell 6c7facfd2d daemon: time option support.
No need to have all times in seconds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:28:17 +09:30
Rusty Russell 82c2325467 timeout: make all timers one-shot.
It's closer to what we want, and simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:26:09 +09:30
Rusty Russell 4beaedfa49 daemon/test: clean up Makefile.
This means mkae tells us directly what failed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:47 +09:30
Rusty Russell 604122e787 daemon: permute input in steal transaction.
This is just generally good practice.  All our other txs are single-input,
so we've not needed to permute inputs before.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:11 +09:30
Rusty Russell 082eaf406e daemon/test: fix spending check.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 15:56:47 +09:30
Rusty Russell 09de557c69 daemon: update limit to reflect latest BOLT#2.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 14:42:52 +09:30
Rusty Russell db3faf0f52 TODO: update to reflect progress
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 14:07:44 +09:30
Rusty Russell d6603adc2f daemon/test: test stealing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 12:00:30 +09:30
Rusty Russell 5e40b264dd daemon: handle cheating.
As per onchain.md.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:55 +09:30
Rusty Russell 4cb6cd1f90 daemon: handle information leak.
As per onchain.md; log that an unknown spend occurred, and weep.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:32 +09:30
Rusty Russell 1be98d860f daemon: handle receipt of error packets.
If it's all printable, print it, otherwise dump hex.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:55:52 +09:30
Rusty Russell e1c6f2d630 daemon: dev-signcommit command to sign the current commit tx.
Do not use this!  We use it to test stealing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:54:49 +09:30
Rusty Russell effcb73a48 daemon/test: speed up and clean up tests.
They would sometimes fail under load, if using valgrind.  Retry
properly rather than relying on random sleeps.  Also, takes "make
check" running time here from 1m31.864s to 1m16.872s.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:36 +09:30
Rusty Russell 7ae15401dd daemon: log all state changes the same way.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:34 +09:30
Rusty Russell 623eec4068 daemon/test: test unilateral close.
We use dev-disconnect to convince one node the other has disconnected
(but not vice versa), to get deterministic behaviour.  We do this with
one HTLC outstanding, to test the HTLC timeout path.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell 0f9889f2c6 state: trim unused states.
Now we never enter the state machine if we're dealing with on-chain
transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f29a6043d2 daemon: open-code handling of on-chain states.
Once we see an on-chain tx, we ignore the state machine and handle it
as per the onchain.md draft.  This specifies a *resolution* for each
output, and we're done when they're irrevocable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f6b36b9be3 Revert: 064309df1a "peer: signature in commit_info is always valid."
It's not quite true: if we offer the anchor, we have a commitinfo
without their signature yet.  So make it a pointer again.  Since we
always allocate struct commit_info with talz, it starts as a NULL
pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:13:50 +09:30
Rusty Russell c94f77fd14 forever-confirms: warn if less than 100.
BOLT #onchain considers 100 the minimum depth to be "irrevocable".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:50 +09:30
Rusty Russell 5eb50345ae daemon: implement bitcoin_htlc_timeout()
This is called when an HTLC times out, and we need to send it back to
ourselves.  We also adjust the locktime, since in practice we should
refuse an HTLC less than our locktime.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:47 +09:30
Rusty Russell a4125313ce peer: helpers to extract a given HTLC from commit_info.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:47 +09:30
Rusty Russell 1b49d2afa6 chaintopology: always track txs we broadcast ourselves.
This is inefficient, but it means we always know the tx depth.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:16 +09:30
Rusty Russell 7d4d2977b6 watch: depth callback is always >= 0
We don't report conflicts, just depths.  So we report 0 if it's in a
main chain which loses to another, otherwise it's always positive.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:39 +09:30
Rusty Russell 4e102ccfcf chaintopology: simply track txids, not watches.
This is less efficient, but simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:37 +09:30
Rusty Russell 57ec0397ad chaintopology: only deal with the main chain.
Since bitcoind doesn't propagate non-main chains, there's little point
trying to be smart when we see them.  This simplifies things immensely.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:06:19 +09:30
Rusty Russell 17167704a6 daemon: handle bitcoin transaction re-broadcasting.
It's primitive, but we re-broadcast any txs not included in the main
chain every time the tip moves.  We only track transactions we are
watching, but that turns out to cover every transaction we generate
anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell c1dc0e54fa daemon/bitcoind: bitcoind_sendrawtx to send hex string.
This can fail.  Real cases include both sides dumping their commitment
txs in testing (only one can succeed).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 8fe2ba0ab3 daemon/bitcoind: don't eliminate output on non-zero exit status.
Abort if caller wasn't expecting it, otherwise save the result.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 29db78ea43 daemon: always call state machine through state_single.
That logs transitions, and we're about to patch it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 3f374d8d8b check-bolt: handle references to early-drafts too.
Particularly for the onchain.md draft.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:04 +09:30
Rusty Russell 1245ffaae3 script: add check for 32-byte preimage.
We need to enforce this onchain as we do in the protocol off-chain,
otherwise we can have an onchain redemption we can't redeem upstream
via the protocol.  While Laolu points out there's a 520 byte limit on
witness stack element, that can still make for a larger tx and make
problems for the steal tx case.

The downside is that even the timeout transaction, which used to spend
the HTLC with an empty 'secret', now needs a 32-byte secret, making it
a little larger.  We create a 'bitcoin_witness_htlc' helper for this
case.

See: http://lists.linuxfoundation.org/pipermail/lightning-dev/2016-May/000529.html

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell 02593059cd script: rename scriptpubkey_htlc_send to/recv bitcoin_redeem_htlc_send/recv
They could be scriptpubkeys, but they're actually used inside p2wsh,
so they're really witness scripts.  We use the term "redeem" elsewhere
from when we were using p2sh, though.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell e18aea8d71 daemon: simplify fee calculation for spends of our own commit tx.
It's not exact, but faking a sig, measuring length, then resigning was
neither exact nor pretty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell 12b37d5f80 daemon: fix logic which determines how anchor output was spent.
We watch the anchor output, and separate it into different cases.
This is simpler with segwit (txids are known before sigs), but we also
had missed the case of our own commit transaction spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell eb5d832963 state: don't spend the "to-us" output from their commit tx.
There's no reason to, it's a simple p2wpkh to our key.

We still spend the "to-us" from our commit tx, since it could be
theoretically be stolen by the revocation value, and it's a complex
p2wsh which a normal wallet won't have the information to spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell c4713a6ed5 state: use htlc_onchain structure
Turns out that we want to pass information about the commit info, the
HTLC number and (sometimes) the R value, so create a struct for that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell bccd10c6d8 daemon: don't code check for cleared commits in the main loop, use pending queue.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 33a477d65d daemon: add pending input queue.
You can't re-enter the state machine from a callback, so this allows you
to queue an input for when it returns.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30