Commit Graph

103 Commits

Author SHA1 Message Date
Rusty Russell 217df2d2e8 channeld: fix shutdown when we have to complete HTLCs.
We need to check if we exit after sending a revoke_and_ack, otherwise
channeld ends up getting the closing_signed packet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 40895e4550 Update to match spec: "BOLT7: Reorder feature bitmaps in order to allow future changes"
aka. a257554456cda98afd1532c302c0e5e84de0455e

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-03 14:26:41 +02:00
Christian Decker 35847206b2 fix: Allow pings while waiting for the channel to lock
This was causing failures on testnet where confirmations are not
immediate.

Reported-by: Fabrice Drouin @sstone
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-07-22 09:52:14 +09:30
Rusty Russell 6fa90c926a channeld: exit after shutdown when no more HTLCs.
Ready for the introduction of closingd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 94a33a7d15 channeld: use an enum side not a bool for funder/
This predated our handling of enums.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 9ec804341c channeld: fix intermittant failure on reading init msg from master.
As tracked down by Christian; by setting up the master conn first,
we make the master fd async.  This means that the synchronous read
(in init_channel) can fail with -EAGAIN, and indeed, Christian
saw this when not running under valgrind.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-03 19:43:34 +09:30
Rusty Russell 249085ac10 channeld: reorder functions more logically.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-03 19:43:34 +09:30
Christian Decker f9a2f73072 Addressing feedback from PR #187 2017-06-30 13:42:00 +02:00
Christian Decker 70f4484ba8 channeld: Pass through the agreed feerate_per_kw to channeld
We were erroneously using the fee_base which refers to the fees we
require for forwarding transactions instead of the on-chain fees.
2017-06-30 19:55:17 +09:30
Christian Decker 5c80963577 bug: channel_update parameters slipped by one 2017-06-30 19:55:17 +09:30
Christian Decker 02aba77698 channel: Passing channel_flags through to channeld 2017-06-30 19:55:17 +09:30
Rusty Russell ee9ebbc184 channeld: fix `funding_locked` retransmission using latest revision of BOLT2.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 42c9cb58c7 channeld: rename commit_index to next_index.
This is what it actually is, and makes it clearer when we refer to the
spec.  It's the commitment we're currently updating, which is the next
commitment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell d411b07ba2 channeld: fix make_revocation_msg() to take index of revoke.
It currently takes index + 1 (ie. current commit), which is confusing
in this context.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 1679f2da06 channeld: update to send next commitment number on connection reestablish.
As per latest BOLT revision.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 05b9e0d538 channeld: do connection reestablishment synchronously.
We currently have the problem that the master can send new HTLCs before
we've processed the incoming reestablish message.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell d492f3872c wire/peer_wire: rename gossip_msg / unknown_msg / unknown_msg_discardable
The next patch includes wire/peer_wire.h and causes a compile error
as lightningd/gossip_control.c defined its own gossip_msg function.

New names are clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 866ac79850 channeld: shutdown support.
We keep the scriptpubkey to send until after a commitment_signed (or,
in the corner case, if there's no pending commitment).  When we
receive a shutdown from the peer, we pass it up to the master.

It's up to the master not to add any more HTLCs, which works because
we move from CHANNELD_NORMAL to CHANNELD_SHUTTING_DOWN.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 773d2c09e1 lightningd: push incoming HTLCs into SENT_REMOVE_HTLC upon outgoing resolution.
When we get a fail/fulfill on an outgoing HTLC, we tell the correspoding
incoming HTLC about it.  But if that peer is disconnected, we don't.

The better solution is to copy the preimage/malformed/failmessage and mark
the incoming HTLC as resolved.  This is done most simply by marking it
SENT_REMOVE_HTLC, which will work in the database case as well.

channeld now re-transmits appropriately when it gets started with an HTLC
in that state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 9e941e26ed channeld: increment remote commit_index before receiving revoke.
This matches what the master does: increments commit index when we send
commit_sig.  Thus if we restart at that point, we match.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell d95adf7f33 subd: hand through fatal messages as well to callback.
This matters in one case: channeld receiving a bad message is a
permenant failure, whereas losing a connection is transient.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 15405f95e1 lightningd: simply store commit index, rather than count sent/received.
This is what channeld wants to know, so just do that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell d236e724a9 channeld: save old remote_per_commit and return it in init.
We need the old remote per_commitment_point so we can validate the
per_commitment_secret when we get it.

We unify this housekeeping in the master daemon using
update_per_commit_point().

This patch also saves whether remote funding is locked, and disallows
doing that twice (channeld should ignore it).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 6b61e9ab0a channeld: exchange channel_reestablish.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell b600e6118c lightningd/channel: hand commit_index in when creating txs only.
Don't store it persistently, as we want to be explicit anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 90284af249 channeld: make per_commit_point and per_commit_secret handling clearer.
All the +1 and -1 in the existing combination routine were confusing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 9497cbe73b lightningd/channel: tell master about the commit sigs.
There are two ways we can do retransmission on reconnect: re-derive
what we would have sent, or remember it and simply re-send.  The
rederivation is difficult: unwinding state depends on whether we sent
a revoke_and_ack before or after the commitment_signed, and unwinding
a revoke_and_ack would require us to remember HTLCs we would have
normally forgotten at this point.

So we simply tell the master to remember the old signatures for us,
and hand them back in case we need to re-send.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 3f65f0f000 lightningd: fix up malformed onion handling.
In the case where we can't decrypt the onion, we can't fail it in the
normal way (which is encrypted using the onion shared secret), we need
to respond with a update_fail_malformed_htlc message.

Moreover, we need to remember this for persistence.  This means that
we really have three conclusions for an HTLC: fulfilled, failed,
malformed.  Fix up the logic everywhere which assumed failed or
fulfilled.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 4097351f16 channeld: get handed existing HTLCs in init message.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 7e820f07e7 channeld: get commit indices from master.
Note that this will change a little in following commits.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell c1d5e41dfe channeld: include last sent commit and last revoke in init message.
It's easiest to have the master keep the last commit we sent, for
re-transmission.  We could recalculate it, but it's made more difficult
by the before/after revoke case.

And because revoke_and_ack changes the channel state, we need to
remember which order we sent them in for re-transmission.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 4151135be2 channel: change initialization to have explicit local amount, and commot indices.
This is useful for restoration.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell a6b03dec15 channeld: do init as sync IO.
Saves a special case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 060a440548 channeld: tell master about next_per_commit_point.
It needs to save them to the db in case of restart; this means we tell
it about funding_locked, as well as the next_per_commit_point given
in revoke_and_ack.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 46efb37152 channeld: send shared secrets with initial got_commitsig message.
The channel daemon gets the shared secrets from the HSM to save
the master daemon some work.  It used to hand these over at
revoke_and_ack receive, which is when the master daemon needs them.

However, it's a bit simpler to hand them over when we first tell
the master about the incoming HTLC (the first commitsig).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 61906ea415 channeld: don't keep shachain.
The master daemon is the one which stores it, have it do the check.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 4a161b710d channeld: pass htlcs to master daemon in batches.
When adding their HTLCs, it needs all the information.  When failing,
it needs the id as key and the failure reason.  When fulfilling, it
needs the id and payment preimage.

It also needs to know when we have received an revoke_and_ack or a
commitment_signed, to place in the database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell dcb3af7d7c channeld: allow waiting for a specific reply from master.
We're about to change to a batch interface, where we tell the master
before we send certain packets (eg. commit, revoke).  We need to wait
for it to respond before doing anything else, but it might cross-over
and be sending us commands at the same time.

This queues those requests until we're ready.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 42ede14081 channeld: return io_plan from handlers.
This prepares us for handlers turning off peer I/O, rather than assuming
we always want to handle the next incoming message.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 1ca97a7913 channeld: don't unpack union, leave that to master.
We still get the shared secret, since that requires a round trip to the HSM
(why waste the master daemon's time?) but it does the processing, which
simplifies the message passing and things like realm handling which
have nothing to do with this particular channeld.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 7105085801 lightningd/channel: hand back changed htlcs, not callbacks.
Means caller has to do some more work, but this is closer to what we want:
we're going to want to send them to the master daemon for atomic commit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 996567c250 lightningd: update BOLT to add channel_reestablish message.
We don't handle it yet though.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell d4618fa199 subdaemons: handle master or gossipd failing.
We should simply exit in this case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-07 09:19:04 +09:30
Rusty Russell 6f181e0dc1 BOLT update for 8-byte satoshi values, and other updates.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-07 09:19:04 +09:30
Rusty Russell 7389aae26a Massive BOLT text underscore and formatting updates.
This brings us up to 61b5b3f7b4145c9d6d66973b6bfbf28e6c0a0791.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-07 09:19:04 +09:30
Christian Decker 605438f213 channel: Die on unexpected gossipd messages 2017-06-07 06:41:39 +09:30
Rusty Russell 693457a580 lightningd: remove unused offset field from CSV files.
The format we use to generate marshal/unmarshal code is from
the spec's tools/extract-formats.py which includes the offset:
we don't use it at all, so rather than having manually-calculated
(and thus probably wrong) values, or 0, emit it altogther.

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell ed16bb3134 channeld: send optional init message.
We use this to make it send the funding_signed message, rather than having
the master daemon do it (which was even more hacky).  It also means it
can handle the crypto, so no need for the packet to be handed up encrypted,
and also make --dev-disconnect "just work" for this packet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 8f190c673c channel: don't send disable update to gossipd if we haven't announced channel yet.
Valgrind error file: /tmp/lightning-8k06jbb3/test_disconnect/lightning-7/valgrind-errors
==32307== Uninitialised byte(s) found during client check request
==32307==    at 0x11EBAD: memcheck_ (mem.h:247)
==32307==    by 0x11EC18: towire (towire.c:14)
==32307==    by 0x11EF19: towire_short_channel_id (towire.c:92)
==32307==    by 0x12203E: towire_channel_update (gen_peer_wire.c:918)
==32307==    by 0x1148D4: send_channel_update (channel.c:185)
==32307==    by 0x1175C5: peer_conn_broken (channel.c:1010)
==32307==    by 0x13186F: destroy_conn (poll.c:173)
==32307==    by 0x13188F: destroy_conn_close_fd (poll.c:179)
==32307==    by 0x13B279: notify (tal.c:235)
==32307==    by 0x13B721: del_tree (tal.c:395)
==32307==    by 0x13BB3A: tal_free (tal.c:504)
==32307==    by 0x130522: io_close (io.c:415)
==32307==  Address 0xffefff87d is on thread 1's stack
==32307==  in frame #2, created by towire_short_channel_id (towire.c:88)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 3126eed4de patch peer_control-keep-init-information.patch 2017-05-25 14:24:47 +09:30