Commit Graph

38 Commits

Author SHA1 Message Date
Rusty Russell 57689390fb state: queue packets directly.
Rather than creating packets then queueing them, call out to functions
which do both.  This moves us towards doing more work in those functions
where we send out a request, which is sometimes clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-30 16:57:18 +10:30
Rusty Russell b423b33236 protocol: implement close as per BOLT #2.
We don't actually implement closing when we have HTLCs (we should
allow it, as that's what the clearing phase is for), since soon we'll
rewrite HTLC to match the async HTLC protocol of BOLT #2.

Note that this folds the close paths, using a simple check if we have
a close transaction.  That's a slight state layer violation, but
reduces code duplication.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:09:41 +10:30
Rusty Russell cb0cc80984 patch remove-timeout.patch 2016-03-15 17:07:12 +10:30
Rusty Russell 983000428f daemon: rename CMD_SEND_HTLC_UPDATE to CMD_SEND_HTLC_ADD
There are other updates than just adding an HTLC; make this explicit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:42:15 +10:30
Rusty Russell 4f67b59c26 protocol: rename routefail to fail.
It's a generic "I couldn't complete this" failure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:41:15 +10:30
Rusty Russell 40361e3dd8 state: explicit callback to check pkt_open_complete.
For now, this always succeeds, but include the hook for completeness.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:39:15 +10:30
Rusty Russell a2f4000d22 state: cleanup after anchor has been transmitted.
If something goes wrong after we've broadcast the anchor tx, we need to use
the commit tx to spend it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:38:15 +10:30
Rusty Russell 65cc6bbd50 pkt_err: make it variadic.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 79e1c9bb62 state: make bitcoin_commit() take a non-const peer.
We have to sign the commit at this stage, so easiest if peer isn't const
so we can sign it in-place.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 6a7a046f60 state: add peer_unwatch_close_timeout() call.
This tell us to disarm the INPUT_CLOSE_COMPLETE_TIMEOUT: either we hit
an error and are going to unilateral close, or we received their signature
successfully.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 144ab3bef4 state: make tx arguments const.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell ebf2bc57d8 state: add async anchor creation.
Actually generating the anchor transaction in my implementation
requires interaction with bitcoind, which we want to be async.  So add
a callback and a new state to wait for it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 0db3c03ed1 state: fix BITCOIN_HTLC_TOTHEM_SPENT idata.
We use both union fields idata->btc and idata->htlc, which is clearly
wrong.  Have peer_tx_revealed_r_value return the HTLC it's talking
about.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 2b3d5b4049 state: remove all traces of struct state_effect.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 63ea6bfd7a state: add callbacks for adding/removing watches.
This lets us eliminate struct state_effect altogether (the next patch
removes the now-unused arguments).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 1a20e8094f state: return tx to be queued directly.
Instead of effect->broadcast_tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell e984df486d state: return Pkt to be queued directly.
Instead of effect->send_pkt.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 7383da5f87 state: remove update_theirsig effect.
They get this from accept_pkt_update_accept() or accept_pkt_update_signature().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 63cb0667f6 state: use callbacks for htlc management.
We only have one htlc in flight at a time, but sometimes it changes:
particularly when we are lowpriority and a highpriority request comes
in.  Handle this using a set of callbacks for htlc handling.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell c1dc7137ba names: generate names for command_status and state_peercond.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell f48adb097e state: use peer_unexpected_pkt() for an unexpected packet.
Instead of effect->in_error.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 9013a7d872 state: set peer->state directly.
Instead of new_state effect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 4c9a9f8982 state: return status of current command.
We temporarily move effect to the end of the arg list: we'll get rid
of it eventually.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 03268014b4 state: use PEER_BUSY when doing a command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 14ede84d87 state: set peercond in peer directly.
Instead of stop_packets or stop_commands effects.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell bbd9d4aa01 state: inline input_is_pkt().
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:46 +10:30
Rusty Russell 15c5fca876 state: take struct peer instead of struct state_data.
Just a name change for the test code, but this is what we'll be using
for the daemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:45 +10:30
Rusty Russell 85f4a7cf14 state: simplify effect.
Make it a linked list of effects, rather than one big union.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:45 +10:30
Rusty Russell 4d22b4e3eb pkt_open: use flag to indicate whether packet will offer anchor.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:45 +10:30
Rusty Russell a38d0c985e Makefile: more fascist warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:37 +10:30
Rusty Russell d733e82352 header cleanup: sort include lines into alpha order, after config.h
This makes merging easier in future.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:38:08 +10:30
Rusty Russell 4d74fd165f state: Allow CMD_CLOSE at any time.
As suggested by Anthony Towns.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:19 +09:30
Rusty Russell 188d0b76a4 state: use an enum for the completion.
Instead of overloading complete_data.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:19 +09:30
Rusty Russell 6821b4f31c state: handle on-chain HTLCs.
When a unilateral close occurs, we have to watch on-chain ("live")
HTLCs.  If the other side spends their HTLC output, we need to grab
the rvalue.  If it times out, we need to spend it back to ourselves.
If we get an R value, we need to spend our own HTLC output back to
ourselves.

Because there are multiple HTLCs, this doesn't fit very neatly into a
state machine.  We divide into "have htlcs" and "don't have htlcs",
and use a INPUT_NO_MORE_HTLCS once all htlcs are resolved to transition.

Our test harness now tracks individual HTLCs, so we refined some
inputs (in particular, it won't try to complete/timeout an HTLC before
we have any).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30
Rusty Russell 31459d6cd2 protocol: rename update_complete_htlc to update_fulfill_htlc.
Complete was an overloaded word.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30
Rusty Russell ca68c5c47f state: remove non-HTLC updates.
They're still in the base protocol (good for testing), just not here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30
Rusty Russell 42bf766d64 state: hand tx explicitly to bitcoin_watch / bitcoin_watch_delayed
Neater than assuming it's effect->broadcast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30
Rusty Russell 847ef21c07 state: Core state machine for lightning.
It's written in a repetitive and stylized form, for easier testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30