Commit Graph

650 Commits

Author SHA1 Message Date
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 207eeae1f7 lightningd: explictly split htlc_in and htlc_out.
They share some fields, but they're basically different, and it's clearest
to treat them differently in most places.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell ae62509b58 lightningd: remove dev-newhtlc command.
It added another path to the local-htlc handling, and we have full
invoice support now.

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 ad60d71c55 lightningd: fail for the moment if we fail in CHANNELD_NORMAL.
This makes testing easier for the moment, until we implement this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell b07b1c1690 channeld: dump htlc transitions.
Good for debugging.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell ad3ab53fa4 lightningd/htlc_wire: wire types for sending HTLCs to/from channel daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 7c9b66e67f lightningd: peer_htlcs.c
Move HTLC handling out of peer_control.c.

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 6e28412f8f lightningd: clean up HTLC error handling.
Some paths were still sending unencrypted failure messages; unify them
all.  We need to keep the fail_msg around for resubmission if the
channeld dies; similarly, we need to keep the htlc_end structure
itself after failure, in case the failed HTLC is committed: we can
move it to a minimal archive once it's flushed from both sides,
however.

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 e7297ffd5d lightningd/peer_control: remove redundant peer_by_pubkey().
We have peer_by_id() for exactly this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 212b46b1d9 lightningd/cryptomsg: fix use-after-free introduced by dev_disconnect.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 9327aa1df4 lightningd: fix logging of peer death.
We used level -1 to mean "append to log", but that doesn't actually
work, and results in an assert if we try to prune the logs:

	lightningd: daemon/pseudorand.c:36: pseudorand: Assertion `max' failed.

Expose logv_add and use that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 5b1d60839a lightning: fix reverse test causing crash when paying to no owner.
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 87ef7acbd6 lightningd/peer_control: join peer logs with master logs.
We use --log-level to control this, but we could add another switch.

It makes the test infrastructure simpler, since we can just look in the
main logs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-07 09:19:04 +09:30
Rusty Russell f7a22b699e lightningd/peer_control: embed id, don't use pointer.
We always know it now we have struct connection, so we don't need a pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-07 09:19:04 +09:30
Rusty Russell 3c105bd8db sphinx: Add BOLT#4 quotes.
In particular, it reassured me that the ammag obfuscation step occurs
even for the initial failmsg creator.

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
Rusty Russell 87fc3439c4 test_lightningd.py: fix obscure corner case in gossip.
I actually hit this very hard to reproduce race: if we haven't process
the channeld message when block #6 comes in, we won't send the gossip
message.  We wait for logs, but don't generate new blocks, and timeout
on l1.daemon.wait_for_log('peer_out WIRE_ANNOUNCEMENT_SIGNATURES').

The solution, which also tests that we don't send announcement signatures
immediately, is to generate a single block, wait for CHANNELD_NORMAL,
then (in gossip tests), generate 5 more.

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
Christian Decker 3404509928 wallet: Move UTXO tracking to DB
Since we have a simple way to query the database for UTXOs we can
simplify some of the coin selection logic. That gets rid of the
in-memory list of UTXOs.
2017-06-06 09:16:10 +09:30
Christian Decker 7e0b9bd1ab wallet: Always use the DB backed bip32_max_index
We were loading it on startup and updating as we went. Removing
caching to reduce chances of becoming desynchronized.
2017-06-06 09:16:10 +09:30
Christian Decker 257ecf6222 wallet: Helper to store/retrieve persistent vars to/from DB
Not the nicest code, but it allows us to store the bip32_max_index so
that we don't forget our addresses upon restart. We could have done
the same by retrieving the max index from our index, but then we'd
forget addresses that don't have an associated output. Conversion
to/from string is so that we can store arbitrary one off values in the
DB in the future, independent of type.
2017-06-06 09:16:10 +09:30
Christian Decker bdb6b1002f wallet: Reserve UTXOs used in build_utxos 2017-06-06 09:16:10 +09:30
Christian Decker ca30c8c0cc wallet: Track available outputs
We'd like to use the database as the unique source of truth, to
avoid having to synchronize multiple versions.
2017-06-06 09:16:10 +09:30
Christian Decker 9882a9fb29 wallet: Start the wallet interface and link it into lightningd
The database is hidden behind the wallet interface, which has all the
wallet specific functionality. First up is the tracking of outputs.
2017-06-06 09:16:10 +09:30
Christian Decker f1f9af4808 secrets: Fixed a typo in the per-peer secret generation 2017-06-01 18:52:40 +02:00
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 06bc035f59 Minor fixes: feedback from Christian
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 9b1d240c1f lightningd: --dev-disconnect support.
We use a file descriptor, so when we consume an entry, we move past it
(and everyone shares a file offset, so this works).

The file contains packet names prefixed by - (treat fd as closed when
we try to write this packet), + (write the packet then ensure the file
descriptor fails), or @ ("lose" the packet then ensure the file
descriptor fails).

The sync and async peer-write functions hook this in automatically.

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



Header from folded patch 'test-run-cryptomsg__fix_compilation.patch':

test/run-cryptomsg: fix compilation.

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 302ed0ca3b peer_control: always keep gossip_client_fd.
This is simpler than passing back and forth, for the moment at least.  That
means we don't need to ask for a new one on reconnect.

This partially reverts the gossip handling in openingd, since it no longer
passes the gossip fd back.  We also close it when peer is freed, so it
needs initializing to -1.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell d1fcc434c8 subd: use array of fd pointers, not fds, and use take().
This lets us specify that we want to keep some fds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell f504f0be47 gossip: handle release race.
We can go to release a gossip peer, and it can fail at the same time.
We work around the problem that the reply must be a gossipctl_release_peer_reply
with two fds, but it's not pretty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell b3a2a0623c lightningd: set up reconnect timer if we don't want to forget peer.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell f4722e93a0 peer_fail: handle incoming reconnections.
We kill the existing connection if possible; this may mean simply
forgetting the prior peer altogether if it's in an early state.

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
Rusty Russell 05875d7f35 opening: handle gossip.
We don't send out gossip, but we do relay it to the gossip daemon if we
get some.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 43ec37b865 openingd: fundee: don't send watch command to master.
Instead, send it the funding_signed message; it can watch, save to
database, and send it.

Now the openingd fundee path is a simple request and response, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell c2cfc3dd69 opening: funder: don't ask master for TXID, calculate it ourselves.
Simplifies state machine.  Master still has to calculate the tx to get
the signature and broadcast, but now the opening daemon funding path
is a simple request/response.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 4220362692 derive_basepoints: make arguments optional.
We want to use it in peer_control to generate the transaction, but we
really only need the funding_pubkey.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 7bfd282319 lightningd/utxo: helpers to translate from utxo * <-> utxo **
We need the former for marshalling, the latter for build_utxos and funding_tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 805228b939 lightningd/funding_tx: fix no-change-needed case.
We only allocate one output in that case, and changekey is undefined.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 3ab3281a4c lightningd/opening: rename functions and wire messages for clarity.
Each one is either funder or fundee now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 1e36a19164 lightningd/peer_control: keep cryptostate.
Like the fd, it's only useful when the peer is not in a daemon, so we
free & NULL it when that happens.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell ccad93edb3 gossipd: add fail_peer.
We use this if it reconnects via another fd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 5fb1f20898 gossip: make connection own peer.
We steal it when we're closing connection, but we normally want to forget
it if connection just dies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell aef745e37d peer_control: simplify code flow in depth callback.
I modified it to use states, and messed it up.  Rewrite it to be
far simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 780b3870ad lightningd: peer state cleanup.
1. We explicitly assert what state we're coming from, to make transitions
   clearer.
2. Every transition has a state, even between owners while waiting for HSM.
3. Explictly step though getting the HSM signature on the funding tx
   before starting channeld, rather than doing it in parallel: makes
   states clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 662dfef436 lightningd/gossip: Move INIT message handling to handshake daemon.
We need to do this on every connection, whether reconnecting or not,
so it makes sense for the handshake daemon to handle it and return
the feature fields.

Longer term I'm considering having the handshake daemon handle the
listening and connecting, and simply hand the fds back once the peers
are ready.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 0c8b24cf97 daemon/dns: hand netaddr we connected to through to callback.
That way it doesn't have to extract it from fd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell f6495d3310 lightningd/peer_control: don't create peer struct until we've connected.
We currently create a peer struct, then complete handshake to find out
who it is.  This means we have a half-formed peer, and worse: if it's
a reconnect we get two peers the same.

Add an explicit 'struct connection' for the handshake phase, and
construct a 'struct peer' once that's done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 61a2ed97e1 lightningd/peer_control: start of reconnect logic.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell fe1ff33419 lightningd/subd: don't take ownership of peer.
Use callback which fails the peer if subd dies: that will later allow
reconnect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 34e6e56471 lightningd: introduce peer_state enum.
The actual state names are place holders for now, really.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell 93849b1e02 lightningd/peer_control: save funder side in struct peer.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Rusty Russell be9bb5f9cb lightningd: peer_fail helper to fail/reconnect peer.
This will eventually hook into restart logic.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-25 14:24:47 +09:30
Christian Decker 3f4e43081c bitcoind: Respect testnet for bitcoin-cli 2017-05-20 19:59:16 +09:30
Rusty Russell f4d92813a0 lightningd: handle bad failure message.
We used to core dump if unwrap_onionreply() returned NULL!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-19 13:30:32 +02:00
Rusty Russell 811e14ff66 Update .gitignore files.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-12 12:59:09 +02:00
Rusty Russell 6e0e1c7067 Update to latest BOLT (hyphens changed to underscores).
Now in sync with 8ee57b97738b1e9467a1342ca8373d40f0c4aca5.

Our tool doesn't need to convert them any more, but we actually had a
mis-typed field in the HSM which needed fixing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-12 12:59:09 +02:00
Rusty Russell e97046f797 BOLT update: temporary_channel_failure with update.
Aka d140405a6f0d95e3ccf650e3560383768cbf3e03.

This doesn't make it work, just compile.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-12 12:59:09 +02:00
Christian Decker 2d76b066c2 routing: Cleaning up old hostname and port handling
The single string-based hostname and port has been retired in favor of
having multiple `struct ipaddr`s from the `node_announcement`. This
breaks the hostnames and ports from IRC, but I didn't bother to
backport ipaddr for it since it is only used in the legacy daemon.
2017-05-10 12:37:44 +09:30
Christian Decker e972b208c7 routing: Passing back all addresses on getnodes 2017-05-10 12:37:44 +09:30
Christian Decker 26892e79bb routing: Reading multiple addresses from node_announcements 2017-05-10 12:37:44 +09:30
Christian Decker daf8866eb5 gossip: Implement the basic node_announcement
Rather a big commit, but I couldn't figure out how to split it
nicely. It introduces a new message from the channel to the master
signaling that the channel has been announced, so that the master can
take care of announcing the node itself. A provisorial announcement is
created and passed to the HSM, which signs it and passes it back to
the master. Finally the master injects it into gossipd which will take
care of broadcasting it.
2017-05-10 12:37:44 +09:30
Rusty Russell b99c5620ef struct secret: use everywhere.
We alternated between using a sha256 and using a privkey, but there are
numerous places where we have a random 32 bytes which are neither.

This fixes many of them (plus, struct privkey is now defined in terms of
struct secret).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-09 11:43:35 +09:30
Christian Decker 126839886a routing: Use a pointer to a shared_secret htlc_end 2017-05-06 10:16:07 +09:30
Christian Decker 870b83f67f sphinx: Incrementally wrap replies in new onion layers 2017-05-06 10:16:07 +09:30
Christian Decker 9820abda7c sphinx: Store shared secrets on the origin node
We could recompute them once we receive a reply and need to decrypt
it, but why go through the trouble when we can just store them?
2017-05-06 10:16:07 +09:30
Christian Decker 79f848145c sphinx: Passing shared secret of the HTLC up to the master
This is needed so we can create error messages and wrap them on the
way back.
2017-05-06 10:16:07 +09:30
Christian Decker 79582ea415 sphinx: Update the HMAC in onionreply to full length 2017-05-06 10:10:55 +09:30
Rusty Russell 162dac1271 peer_control: don't complete fundchannel command until broadcast.
Under stress, the tests can mine blocks too soon, and the funding never
locks.  This gives more of a chance, at least.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-05 16:11:46 +09:30
Rusty Russell a12a670d85 opening: don't die if we get a gossip packet.
Using 'taskset -c 0' I managed to slow down pytest enough to trigger this
locally.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-05 16:11:45 +09:30
Rusty Russell cd90c8408c lightningd/gossip: clean up channel resolve failure handling.
We were getting an assert "!secp256k1_fe_is_zero(&ge->x)", because
an all-zero pubkey is invalid.  We allow marshal/unmarshal of NULL for
now, and clean up the error handling.

1. Use status_failed if master sends a bad message.
2. Similarly, kill the gossip daemon if it gives a bad reply.
3. Use an array for returned pubkeys: 0 or 2.
4. Use type_to_string(trc, struct short_channel_id, &scid) for tracing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-05 16:11:44 +09:30
Rusty Russell 103ac79c34 lightningd: expose channel in getpeers.
Lets us manually construct routes, for testing, and replaces the 'locked'
flag as well.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-03 11:47:09 +09:30
Rusty Russell 09e489030c channeld: implement malformed HTLC message.
I implemented this because a bug causes us to consider the HTLC malformed,
so I can trivially test it for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-02 12:19:35 +02:00
Christian Decker 39115717f6 channeld: Passing additional info to channeld
The CLTV delta was hardcoded until now, which was causing me some head
scratching. I guess I asked for it ^^
2017-05-02 11:49:14 +02:00
Christian Decker 25f1cba3cf routing: Ask gossipd to resolve channel_id and forward HTLCs
Since we now use the short_channel_id to identify the next hop we need
to resolve the channel_id to the pubkey of the next hop. This is done
by calling out to `gossipd` and stuffing the necessary information
into `htlc_end` and recovering it from there once we receive a reply.
2017-05-02 11:49:14 +02:00
Christian Decker b9dcb909b8 routing: Simplify code to read the route back in
This was overly complex since it was off-by-one and we were storing
some information elsewhere. Now this just loads the route as is into
structs, extracts some information for our outgoing HTLC, and then
shifts by the array of structs by one, and finally fills in the last
instruction, which is the terminal.
2017-05-02 11:47:52 +02:00
Christian Decker c32c17701a sphinx: Switching to new onion packet format 2017-05-02 11:47:52 +02:00
Christian Decker f700662a56 routing: Reading the channel_id from routes passed in from JSONRPC 2017-05-02 11:47:52 +02:00
Christian Decker d87ca4121d routing: Returning channel_id to getroute requests
The new onion uses the `channel_id` instead of the `node_id` of the
next hop to identify where to forward the payment. So we return the
exact channel chosen by the routing algo, to avoid having to look it
up again later.
2017-05-02 11:47:52 +02:00
Christian Decker f24aab1916 sphinx: Updating daemon to new sphinx implementation
Mainly switching from the old include to the new include and adjusting
the actual size of the onion packet. It also moves `channel.c` to use
`struct hop_data`.

It introduces a dummy next hop in `channel.c` that will be replaced in
the next commit.
2017-05-02 11:47:52 +02:00
Christian Decker b3af8d50a7 sphinx: Integrating with the new daemon 2017-05-02 11:47:19 +02:00
Christian Decker 4a468af378 sphinx: Parameterizing the HMAC size
Should have done this a long time ago...
2017-05-02 11:47:19 +02:00
Christian Decker 652d999dbd sphinx: Moving HMAC to the end of the packet
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-05-02 11:47:19 +02:00
Christian Decker 157c2da778 sphinx: Removing old nexthop based on public keys
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-05-02 11:47:19 +02:00
Christian Decker 83e89f0fe5 sphinx: Using hop_data to serialize per-hop information into onion
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-05-02 11:47:19 +02:00
Christian Decker 752f884c23 sphinx: Removed per-hop payloads, will be replaced by hop_data
This is in preparation for the migration to `hop_data` which contains
all fields, and uses the `short_channel_id` instead of the pubkey.
2017-05-02 11:47:19 +02:00
Rusty Russell 778b756369 channel: support HTLC forwarding.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-02 11:47:19 +02:00
Rusty Russell 8016dbbc91 lightningd: check amt_to_forward and outgoing_cltv_value
These must be checked whether we're the final hop or not.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-02 11:46:46 +02:00
Rusty Russell d35bf90009 lightningd/pay: Set outgoing_cltv_value for onion.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-02 11:46:46 +02:00
Rusty Russell 961987b046 lightningd: partial BOLT update.
Not the new onion stuff for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-05-02 11:46:46 +02:00
Christian Decker b4beab6537 gossip: Make the broadcast interval configurable
Adds a new command line flag `--dev-broadcast-interval=<ms>` that
allows us to specify how often the staggered broadcast should
trigger. The value is passed down to `gossipd` via an init message.

This is mainly useful for integration tests, since we do not want to
wait forever for gossip to propagate.
2017-05-02 11:59:24 +09:30
Christian Decker 4bc6ee1088 gossip: Fix two bugs in the forwarding of gossip
We were using an uninitialized `broadcast_index` on the peer which
would occasionally result in no forwardings at all, segmenting the
network. And during the `msg_queue` refactor, some wait targets were
not updated, resulting in the waits never to be woken up.
2017-05-02 11:59:24 +09:30
Rusty Russell f61da7eb64 tests/test_lightningd.py: incorporate everything from old test-basic shell test.
This moves all the non-legacy blackbox testing into python.

Before:
	real	10m18.385s

After:
	real	9m54.877s

Note that this doesn't valgrind the subdaemons: that patch seems to cause
some issues in the python framework which I am still chasing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-29 10:30:10 +02:00
Rusty Russell 056f93e2d2 ping: move test to python.
Faster and neater.

Before:
	real	0m11.200s

After:
	real	0m9.101s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-29 10:30:10 +02:00
Christian Decker bc0039e8c0 sphinx: Onion reply wrapping and unwrapping
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-04-28 13:40:43 +09:30
Christian Decker 09b31624f0 sphinx: Creating copy of sphinx for new daemon
Keeping both the legacy daemon and the new daemon happy with the
restructure is a lot of work, so we just don't do it :-)
2017-04-28 13:40:43 +09:30
Rusty Russell 6d55a17642 lightningd/dev_ping: expand to cover gossipd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell 8f358b7a91 lightningd: add dev_ping command for channeld.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell d5be8d26f2 lightningd/ping: ping support.
A spec update brings ping support.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell a436fa77fc lightningd/msg_queue: add msg_wake helper.
A cleaner wrapper than a raw io_wake.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell d967c2ba64 lightningd/gossip: interleave local and gossip messages.
Rather than dumping all gossip messages then handling local ones again.
This should help us give timely ping replies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell 4f0f2c0f4e lightningd/gossip: use msg_queue instead of open-coded queue.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-25 22:00:28 +02:00
Rusty Russell 52ee36c595 tests: run valgrind on children.
This fails on the old dev-restart tests, so we need to only enable it
for the new tests:

	rusty@rusty-XPS-13-9360:~/devel/cvs/lightning (guilt/ping-pong)$ daemon/test/test-basic --restart --verbose
	...
	{  }
	RESTARTING
	dev-restart failed!
	valgrind: mmap(0x38000000, 2265088) failed in UME with error 22 (Invalid argument).
	valgrind: this can be caused by executables with very large text, data or bss segments.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell ba12e316f1 lightningd: fix minimum depth.
Only the side *accepting* the connection gives a `minumum_depth`, but both
sides are supposed to wait that long:

BOLT #2:

	### The `funding_locked` message
...
	#### Requirements
	
	The sender MUST wait until the funding transaction has reached
	`minimum-depth` before sending this message.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell d27a5d3212 lightningd/lightningd: shutdown subdaemons on exit.
Especially under valgrind, we should give them some time to exit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell 3c5a70910a lightningd/hsm: shutdown when master conn is freed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell 3509f10e30 lightningd/gossip: shut down daemon when status fd closed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell 15519e5ddf lightningd/channel: set signatures to zero before sending to HSM.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Rusty Russell d06303c8a2 lightningd/channel: derive local channel_id.
We weren't setting it, but the peer wasn't checking it either.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-12 09:09:19 -07:00
Christian Decker f4cf485f00 channeld: Added tracing of incoming and outgoing messages
Makes it easier to search for messages being exchanged when testing.
2017-04-11 16:56:27 -07:00
Christian Decker 8e98e438e9 channeld: Collect signatures before sending announcements
We now have two partially overlapping state-machines: the channel
state and the announcement state. We need to request signatures from
the HSM to exchange them with the peer, and we need to have both sets
of signatures before we can proceed and send the actual announcements.
2017-04-11 16:56:27 -07:00
Rusty Russell 041accb114 channeld: Passing channel_update to HSM for signing, hand to gossipd.
Base-on-patch-by: Christian Decker <decker.christian@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-11 16:56:27 -07:00
Rusty Russell 21449650b0 lightningd/hsm: update_signature support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-11 16:56:27 -07:00
Christian Decker 9c07376dcf channeld: Use local secret to generate bitcoin signature
This key will eventually migrate to the HSM, but for now it's in the
channeld so we generate the signature there.
2017-04-11 16:56:27 -07:00
Rusty Russell 20716ad29b channeld: Ask HSM for channel announcement signature
This is fairly simple: we use synchronous I/O to the HSM.

Based-on: Christian Decker <decker.christian@gmail.com>
2017-04-11 16:56:27 -07:00
Christian Decker 30934eb2fb hsm: Creating signatures for incoming channel announcement sig reqs 2017-04-11 16:56:27 -07:00
Christian Decker b41c8e240a hsm: Added channel announcement sig request and reply
Note this only does the node signature; the channeld itself currently
holds the bitcoin privkey for the funding tx.
2017-04-11 16:56:27 -07:00
Rusty Russell fc98d797d2 hsm: new fd type for channeld.
Instead of reusing HSMFD_ECDH, we have an explicit channeld hsm fd,
which can do ECDH and will soon do channel announce signatures as well.

Based-on: Christian Decker <decker.christian@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-11 16:56:27 -07:00
Christian Decker b18eaf8e1c pay: Makefile was missing a header file 2017-04-04 12:27:07 +09:30
Rusty Russell 018c1d932d lightning/pay: fix missing include.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-03 06:07:56 +09:30
Rusty Russell 798c4b73c4 lightningd/htlc_end: fix key for stricter compiler.
We *should* split the struct into key and data, rather than only comparing
the key parts in the htlc_end_eq function.  But meanwhile, this fixes
the code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-02 09:36:29 +09:30
Rusty Russell e75309873b lightningd/derive_basepoints.h: one place for 2^48 shachain constant.
Suggested-by: Christian Decker <decker.christian@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell e2dc10f98d lightningd: pay support.
The previous code was very tied to the old daemon, so this copies a large
part of it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell f3dbc75eb3 lightningd: send message on HTLC failure, relay to peer.
We don't do the encryption wrapping we're supposed to do yet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 1c0b43f04d channel: tell master about the HTLC fulfillment.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 826fb0c2dc lightningd: track HTLC ends.
This lets us link HTLCs from one peer to another; but for the moment it
simply means we can adjust balance when an HTLC is fulfilled.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 86da7c3a4d lightningd/channel: send and receive htlc_fulfilled messages.
Includes a fix for the direction we fulfill.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 2d635a381b lightningd/channel: pass owner, not sender to channel_fulfill_htlc / channel_fail_htlc
I got this wrong when using them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 85fd8218e2 lightningd: track channel balance.
This is an approximate result (it's only our confirmed balance, not showing
outstanding HTLCs), but it gives an easy way to check HTLCs have been
resolved.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell be4af38d0c channel: unwrap and send incoming HTLCs to master.
So far it just looks it up, marks it resolved, then does nothing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell fed25cc540 lightningd/subd: add a context to requests.
If a peer dies, and then we get a reply, that can cause access after free.
The usual way to handle this is to make the request a child of the peer,
but in fact we still want to catch (and disard) it, so it's a little
more complex internally.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell a815500653 lightningd: hand HSM fd to channeld.
For the moment, it's just to do ECDH to read the onion.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell e36a65a189 lightningd/subd: msgcb return -1 to close channel.
They can't free it while we're using it, but they can return a value
to close it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell eb61446ec8 daemon/invoice: wean off dstate (a little), link into new daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell d3cbde4b46 lightningd/channel: send and receive revoke_and_ack packets.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 2ddc0b696c lightningd/channel: keep old commit points.
We need this, to validate that the secret they give us matches it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 27764b65f9 lightningd: fix shachain to be 48-bits, with hack for legacy.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell dd15361935 lightningd/derive_basepoints: helper to increment our per_commit_point.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 8f2c4348a9 lightningd/channel.c: tell if we're still awaiting revoke_and_ack.
And make sure we don't send another commit if we are.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 3041cd5915 lightningd/channel.c: clearer functions names, better return values.
We call channel_sent_commit *before* sending (so we know if we need
to), so the name is wrong.  Similarly channel_sent_revoke_and_ack.

We can usefully have them tell is if there is outstanding work to do,
too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell f83c04fdbe lightningd/channel.c: make callbacks clearly generic
Passing through 'struct peer *' was a layering violation.

Reported-by: Christian Decker <decker.christian@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell ada1eb5106 lightningd/channel.c: add callbacks for when HTLCs fully committed/removed.
The three cases we care about only happen on specific transitions:
1. They can no longer spend our failed HTLC: we can fail the source now.
2. They are fully committed to their new HTLC htlc: we can forward now.
3. They can no longer timeout their fulfilled HTLC: the funds are ours.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 82a467f131 lightningd/channel: send and receive update_commit
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 0197353bd9 lightningd/Makefile: put timeout into OLD_LIB_SRC for all daemons.
And remove the now-redundant mention in gossipd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 64b50e5cb6 lightningd/channel: handle adding HTLC, generalize handler.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 4839916038 lightningd/cryptomsg: discard unknown odd messages internally.
This saves all callers having to handle it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 47da80fdca lightningd: dev_newhtlc command.
For testing point-to-point HTLCs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 7919279367 lightningd/channel: inter-daemon messages for HTLC handling.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 185a649572 lightningd: include daemon/sphinx.
We're going to need it to make routes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 075092411e lightningd/channel: generate htlc txs and wscripts as well.
In practice, this is what we want, either to generate or check signatures.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell dd1a81d8bd lightningd/channel: implement channel_fail_htlc.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 3a9c559dc6 lightningd/channel: quote BOLT #2 for checking expiry.
https://github.com/lightningnetwork/lightning-rfc/pull/138:

	 BOLT 2: htlc-cltv must be in blocks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell 8cc7f31d44 Update wire from spec 9e0a0e893db389bfe392b2f4db8097949395fe28
Now we send genesis block in handshake.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Rusty Russell e45bc0ce47 Update wire from spec 9a572ff0a3a4d6bceba9c0a9b859692180ecf18f
Only changes commit_sig to commitment_signed, which we don't implement
yet anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-04-01 23:59:46 +10:30
Christian Decker 7793bd1b9d gossip: Consolidated direction bit computation
The direction bit was computed in several spots and was inconsistent
in some cases. Now we compute it just in routing, and once when
starting up `channeld`, this avoids recomputing it all over the place.
2017-03-28 14:06:48 +10:30
Rusty Russell 5e00beaeb4 lightningd/test: fix tests after 62ccf266fa
Now we correctly use the remote revocation basepoint, we need to set
it in run-channel (instead of the local revocation basepoint).

We also update all the comments, as per (pending) spec commit:
	https://github.com/lightningnetwork/lightning-rfc/pull/137

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-24 13:40:36 +10:30
Christian Decker 638594f3c6 jsonrpc: Implemented `getchannels` JSON-RPC call 2017-03-24 13:24:58 +10:30
Christian Decker 60a2227f0d channel: Disable channels that are lost via a channel_update
Before exiting, `channeld` constructs and sends a `channel_update`
marking the channel as disabled. This is the pro-active signalling
that the channel may no longer be used.
2017-03-23 13:34:03 +10:30
Christian Decker c8da420a9d routing: Fill in the `getroute` functionality
Copied the JSON-request parsing from `pay.c`, passing through to
`gossipd`, filling the reply with the `route_hop` serialization, and
serializing as JSON-RPC response.
2017-03-23 13:34:03 +10:30
Christian Decker 2d198e22d0 jsonrpc: Added nested messages for the getroute reply
The `route_hop` struct introduced in the previous refactoring is
reused when returning the reply to a `getroute` request. Since these
are nested messages I added the serialization and deserialization
methods.
2017-03-23 13:34:03 +10:30
Christian Decker 9273b615ab gossip: Added scheleton for `getroute` calls
This includes the request/reply passed from the main daemon to
`gossipd` and the basic handlers.
2017-03-23 13:34:03 +10:30
Christian Decker 23a1d7d475 gossip: Do not log anything, it breaks a daemon connection!
This came up while debugging the gossip daemon breaking upon calling
`getroute`. It turns out that log was still writing to stdout, but
stdout had been reused for an inter-daemon socket, which would
break...
2017-03-23 13:34:03 +10:30
sstone 62ccf266fa bolt 3: use our revocation basepoint when computing their revocation key 2017-03-23 13:19:34 +10:30
Christian Decker 21d7ed0cf6 subd: Do not close STDOUT in sub-daemons
The STDOUT fd being reused as communication sockets with other daemons
was causing some unexpected crashes if the sub-daemon wrote something,
e.g., using `log_*`. Not closing it should avoid that conflict.
2017-03-21 12:26:22 +01:00
Christian Decker d2c626820f channel: Wait for 6 confirmations before sending announcement sigs
The protocol specifies that in order for an announcement to be valid,
the channel has to have at least 6 confirmations.
2017-03-20 17:09:12 +01:00
Christian Decker b2ea4cfd66 wiregen: Passing ctx to array helpers that require it
Some of the struct array helpers need to allocate data when
deserializing their fields. The `getnodes` reply is one such example
that allocates the hostname. Since the change to calling array helpers
the getnodes call was broken because it was attempting to allocate off
of the entry, which did not have a tal header, thus failing.
2017-03-20 11:18:00 +10:30
Rusty Russell d2e19c3735 lightningd/status: reuse wire code for sending status.
Suggested-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 8be18ccfa1 lightningd/msg_queue: rename msg_is_fd to msg_extract_fd
Suggested-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell cf6d25cad6 lightningd/connection: rename to lightningd/daemon_conn
To match the structure name.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell d616dbbd44 lightningd/connection: remove conn_fd field.
io_conn_fd(conn) will give it to you anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell bdc41f00f3 lightningd/channel: use msg_queue properly for peer.
Use msg_enqueue's wake and msg_queue_wait, and don't clone packets since
msg_enqueue() respects take.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 5637564cd4 lightningd/status: support daemon_conn for status_trace and status_failed.
We remove the unused status_send_fd, and rename status_send_sync (it
should only be used for that case now).

We add a status_setup_async(), and wire things internally to use that
if it's set up: status_setup() is renamed status_setup_sync().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 7442cf7c3e lightningd/channel: use daemon_conn.
This is a little more awkward, as we used to do some work
synchronously (the init message), but it's still pretty clear.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 21498647e2 lightningd/hsm: remove status_send.
Since we use async IO, we can't use status_send.  We keep a pointer to the
master daemon_conn, and use that to send.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 6a0c9875a1 lightningd/hsm: use daemon_conn.
Nice simplification.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell c213ea482d channel: receive gossip fd on exec.
This simplifies things a little: hand the fd on exec rather than over the
request socket.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell f511012e29 lightningd/gossip: don't hand client fd until release.
The gossip subdaemon previously passed the fd after init: this is
unnecessary for peers which simply want to gossip (and not establish
channels).

Now we hand the gossip fd back with the peer fd.  This adds another
error message for when we fail to create the gossip fds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 7a9df37ef3 lightningd/subd: support multiple fds sent at once in normal messages.
Rather than returning SUBD_NEED_FD, callback returns how many fds it needs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 38bffc0f0c lightningd/subd: support multiple fds sent at once in request reply.
Instead of indicating where to place the fd, you say how many: the
fd array gets passed into the callback.

This is also clearer for the users.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 3938f40274 lightningd/gossip: use daemon_conn for status updates.
We're going to remove status_send (it's sync, and so doesn't play well with
async io).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 1f894b6234 lightningd/gossip: use daemon_conn for master daemon interaction.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 6dd07801d4 lightningd/connection: add fd support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 22d2392454 lightningd/subd: use msg_queue fd support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell bde872d34c lightningd/msg_queue: support queueing/dequeueing of fds.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 83466b2b32 ccan: update to get close option to io/fdpass.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 4bf398c4e7 status: move into lightningd/status.
It's really a lightningd-only thing, and we're about to do surgery on it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 1b0dc7414c lightningd/connection: use msg_queue_wait so it can wake us.
This measn that gossip (which also wants to wake it) needs to wake
the queue, not the daemon_conn.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell 38b68d478f lightningd/connection: daemon_conn_send should be const.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-20 07:50:53 +10:30
Rusty Russell de651bf8fb gossip: don't use hand-coded nested messages for getnodes array.
We can simply tell it 'nodes' is 'num_nodes*struct gossip_getnodes_entry'.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-16 14:35:26 +10:30
Rusty Russell e042198cf8 tools/generate-wire.py: allow typename instead of type sizes.
We use the fourth value (size) to determine the type, unless the fifth
value is suppled.  That's silly: allow the fourth value to be a typename,
since that's the only reason we care about the size at all!

Unfortunately there are places in the spec where we use a raw fieldname
without '*1' for a length, so we have to distingish this from the
typename case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-16 14:35:25 +10:30
Rusty Russell 9768e4c907 tools/generate-wire.py: open-code array handling for non-u8.
Except for the trivial case of u8 arrays, have the generator create
the loop code for the array iteration.

This removes some trivial helpers, and avoids us having to write more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-16 14:35:23 +10:30
Christian Decker d6918dc4ef gossip: Implemented `getnodes` JSON-RPC call 2017-03-15 21:32:55 +01:00
Christian Decker 74df755a29 gossip: Added nested message types for getnodes query
Added a struct that represents a single entry in a `getnodes` reply,
so that we can append one after the other and parse them again on the
other side.
2017-03-15 21:32:55 +01:00
Christian Decker 8d5591f110 channel: Create gossip announcement upon channel establishment 2017-03-15 12:45:11 +10:30
Christian Decker 5a1fbb7aaf channel: Passing through the endpoint node_ids
This is used to generate the announcements.
2017-03-15 12:45:11 +10:30
Rusty Russell 8a893df951 lightningd/subd: fix use-after-free, cleanup subd_send_msg unnecessary tal_dup.
subd_req() needs to get the type before it calls subd_send_msg, because
if it's take() then msg_enqueue() may reallocate.

Which also made me realize that subd_send_message() should not try to dup,
since msg_enqueue() handles that itself.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-14 10:57:48 +10:30
Christian Decker 8ae698d1dc Migrating daemon_conn to msg_queue and msg_queue takes over messages
We have some duplication in handling queues, so this is an attempt at
deduplicating some of that work. `daemon_conn` now uses the
`msg_queue` and `channeld` was also migrated to `msg_queue`. At the
same time I made `msg_queue` create a copy of the messages or takes
over messages marked with `take()`. This should make cleaning up
messages easier.
2017-03-13 17:32:03 +01:00
Christian Decker 05e64db6cd gossip: Refactored non-local peers to use `daemon_conn` 2017-03-13 11:26:48 +01:00
Christian Decker 1af6262822 connection: Added callback when the queue is cleared
This allows us to break out of the normal queue-based write loop and
handle things ourself for a while. Currently this is used to trigger
regular gossip dumps that do not proceed until the buffers have been
cleared in order to avoid memory-explosions.
2017-03-13 11:26:48 +01:00
Christian Decker fd1cbf9030 gossip: Refactoring message handling and removed redundant timers
We were firing off the wakeup timers all over the place, out of fear
that we would be triggering two concurrent broadcasts. This is not
really the case since the wakeup calls are idempotent. This also
allows us not to differentiate between triggering a broadcast on a
local peer or on a proxied peer.
2017-03-13 11:26:48 +01:00
Christian Decker ea1a891d3f channel: Forwarding incoming gossip messages to gossipd 2017-03-13 11:26:48 +01:00
Christian Decker 04f6070cb6 channel: Forwarding gossip messages to peer 2017-03-13 11:26:48 +01:00
Christian Decker 5721d7d194 gossip: Marking peer as non-local and forwarding broadcasts
This includes some code duplication, but since the two write targets
are fundamentally different we might need to refactor a bit more to
unify them again.
2017-03-13 11:26:48 +01:00
Christian Decker 408d2f5170 Async connection abstraction
This is an attempt at unifying all the async connection handling into
a reusable module.
2017-03-13 11:26:48 +01:00
Christian Decker 0596a6cc2c gossip: Initialize the secp256k1_context
We will be doing signature verifications and creation so we better
have the context to do so.
2017-03-13 11:26:48 +01:00
Christian Decker ab4f1f0550 gossip: Add missing log to gossip daemon
We will eventually ween off of the logging, or replace it with status
messages that log in `lightningd`, but for now we still have the
routing module that does some logging.
2017-03-13 11:26:48 +01:00
Christian Decker 11fc750383 gossip: Handle incoming client requests asynchronously 2017-03-13 11:26:48 +01:00
Christian Decker 34c96399b0 channel: Passing gossipd client fd to channeld 2017-03-13 11:26:48 +01:00
Christian Decker 3045e25624 gossip: Returning a gossip-client upon peer_ready
This fd is handed to the owner of the peer so that it can talk to the
gossip daemon.
2017-03-13 11:26:48 +01:00
Christian Decker 6319035033 lightningd: Add method to find a peer given its unique_id
Needed later
2017-03-13 11:26:48 +01:00
Christian Decker 477b537bad gossip: Do not clear peer in gossipd when it's handed off 2017-03-13 11:26:48 +01:00
Rusty Russell da5c7e0a08 daemon/subdaemon: remove in favor of daemon/subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:41 +10:30
Rusty Russell 14505594b5 lightningd/channel: convert to subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:41 +10:30
Rusty Russell 1781983be1 lightningd/opening: convert to subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:41 +10:30
Rusty Russell e467afce06 lightningd/handshake: convert to subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:41 +10:30
Rusty Russell 5dc8cb12be lightningd/gossip: convert to subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:40 +10:30
Rusty Russell 7f406ea80d lightningd/hsm: convert to subd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-11 07:19:32 +10:30
Rusty Russell 95f41287f0 lightningd/subd: new code for subdaemons.
This uses a single fd for both status and control.

To make this work, we enforce the convention that replies are the same
as requests + 100, and that their name ends in "_REPLY".

This also means that various daemons can simply exit when done; there's
no race between reading request and closing status fds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-10 21:45:55 +10:30
Rusty Russell cd62b74b1c lightningd/msg_queue: helper for queues of messages/
Since they're short, we use a simple array.  This can be replaced later.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-10 21:45:55 +10:30
Christian Decker 5277787db5 gossip: Do not cleanup broadcast messages
Freeing the messages results in a use-after-free when the next peer
attempts to synchronize.
2017-03-09 11:16:37 +01:00
Rusty Russell f0c838d250 lightningd: more updates to match latest BOLT2/3.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 16:22:34 +10:30
Rusty Russell fc198e1bac lightningd/test/run-commit_tx: generate test vectors for new scripts.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 16:19:57 +10:30
Rusty Russell 8d01eeeef5 lightningd/commit_tx: support for printing out actual tx fees.
This was included in the lightningnetwork/lightning-rfc#105 update
to the test vectors, and it's a good idea.  Takes a bit of work to
calculate (particularly, being aware of rounding issues).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 16:19:11 +10:30
Rusty Russell 29d6004efc bitcoin/script: update scripts to the lightningnetwork/lightning-rfc#123 version
aka "BOLT 3: Use revocation key hash rather than revocation key",
which builds on top of lightningnetwork/lightning-rfc#105 "BOLT 2,3,5:
Make htlc outputs of the commitment tx spendable with revocation key".

This affects callers, since they now need to hand us the revocation
pubkey, but commit_tx has that already anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 16:16:59 +10:30
Rusty Russell 9035d281c6 lightningd/lightningd: hook in channel daemon.
Doesn't do much yet except wait for funding confirmation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 12:01:43 +10:30
Rusty Russell 9014f2593e lightningd/channel: normal operation daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:56:12 +10:30
Rusty Russell c0b4bb1387 daemon/lightningd: store more state in peer.
We need to keep more information about the peer for handing off to the
channel daemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:39:53 +10:30
Rusty Russell 5a73380e64 lightningd/subdaemon: keep peer pointer for one-per-peer daemons.
This is really useful to map daemon back to peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:39:03 +10:30
Rusty Russell a845b07ada lightningd/cryptomsg: only free written messages if they're marked take().
This fixes a leak in gossip, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:38:20 +10:30
Rusty Russell c64447a929 lightningd/derive_basepoints: hoist derivation logic.
All the daemons will use a common seed for point derivation, so drag
it out of lightningd/opening.

This also provide a nice struct wrapper to reduce argument count.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:37:06 +10:30
Rusty Russell 39993f229d bitcoin/script: rename bitcoin_redeem_p2wpkh -> bitcoin_redeem_p2sh_p2wpkh
This is its full name, and less confusing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:35:48 +10:30
Rusty Russell 5f07e8405a lightningd/opening: fix theoretical race.
We should start watching for the transaction before we send the
signature; we might miss it otherwise.  In practice, we only see
transactions as they enter a block, so it won't happen, but be
thorough.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:35:03 +10:30
Rusty Russell 9eeb76e185 lightningd: broadcast transaction once we're told to.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:33:55 +10:30
Rusty Russell b937793555 lightningd/funding_tx: output number is 16 bit.
It's actually always 0 or 1, but the spec wants 16 bits, so use that here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:33:24 +10:30
Rusty Russell ccff3ac437 lightningd/funding_tx: fill in scriptsigs for p2sh UTXOs.
This is a bit tricky: for our signing code, we don't want scriptsigs,
but to calculate the txid, we need them.  For most transactions in lightning,
they're pure segwit so it doesn't matter, but funding transactions can
have P2SH-wrapped P2WPKH inputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:31:55 +10:30
Rusty Russell afc31cd68f lightningd: export bitcoin_pubkey as bip32_pubkey.
We're going to need it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:29:16 +10:30
Rusty Russell 2874e46c69 lightningd/hsm: use funding_tx helper to sign funding transaction.
DRY.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:28:42 +10:30
Rusty Russell 700d88dfa4 lightningd/hsm: sign funding transaction correctly.
We read in the localkey twice, leaving us to use an initializwed
remote_pubkey.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:28:20 +10:30
Rusty Russell d77e7963f9 funding_tx: permute inputs, don't re-calculate fees
built_utxos needs to calculate fees for figuring out how many utxos to
use, so fix that logic and rely on it.

We make build_utxos return a pointer array, so funding_tx can simply hand
that to permute_inputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:28:09 +10:30
Rusty Russell 6a14e24a82 lightningd: integrate bitcoind.
This allows us to broadcast transactions and watch for them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:26:38 +10:30
Rusty Russell 5514f4fadf lightningd/channel.h: fix comment typo.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-06 12:23:58 +10:30
Rusty Russell 7419fde9a0 Update to new spec: differentiate channel_id and short_channel_id.
The spec 4af8e1841151f0c6e8151979d6c89d11839b2f65 uses a 32-byte 'channel-id'
field, not to be confused with the 8-byte short ID used by gossip.  Rename
appropriately, and update to the new handshake protocol.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 2ce6a4bcca daemon/peer: move struct peer to internal header.
We use a different 'struct peer' in the new daemons, so make sure
the structure isn't assumed in any shared files.

This is a temporary shim.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 8d7acdc367 struct topology -> struct chain_topology
Topology also refers to the lightning network, be clear.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 38cc6c2f21 daemon/chaintopology: move dev_no_broadcast from lightningd_state to here.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 61e576ef12 daemon/chaintopology: use struct topology for more functions, not lightningd_state
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 923526baf3 daemon/watch.c: move list of watched txs/txouts into struct topology.
This weans daemon/watch.c off relying on struct lightningd_state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell e6efcdf5bd daemon/bitcoind: wean off struct lightningd_state.
We want to use this in the new daemon, so use 'struct bitcoind'
everywhere.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 5dc0402a44 Update to lightning-rfc to dc0b529161561c6be3ff53c5f8574b23c3305a04
Only minor changes, but I add some more spec text to
lightningd/test/run-commit_tx.c to be sure to catch if it changes
again.

One reference isn't upstream yet, so had to be commented out.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 9aebd5c797 lightningd: add opening to dependencies.
Without this, make check doesn't rebuild opening subdaemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell fd258fe495 lightningd: wire up opening daemon.
Now we can actually open a channel.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell d48f9faa19 lightningd/opening: fixes.
Now we've tested it:

1. open_channel needs to write response to REQ_FD not STATUS_FD.
2. recv_channel needs to send our next_per_commit, not echo theirs!
3. print the problematic signature if it's wrong, not our own.

Cleanups:

1. Return the message from open_channel/recv_channel for simplicity.
2. Trace signing information.
3. More tracing messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 391b15dbd7 lightningd/crypto_sync: trace all messages, especually failures.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell c69f43b5c0 subdaemon: always set incoming fds to blocking.
The gossip daemon didn't, but we shouldn't rely on it doing so anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell c8df820e58 lightning/peer_control: set logging level for peers to match global.
That way it's controlled by --log-level=

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell a737335da2 lightningd/commit_tx: don't segv on NULL htlcmap.
We don't care if we're just checking sigs, so allow NULL.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 5d0977df3a lightningd/funding_tx: use struct utxos, remove signing helper.
The signing helper was really just for testing, so remove it.  But
turn the funding_tx() function into a useful one by making it take the
utxo array.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 8edac22595 hsm: don't require privkey for signing off on funding.
I made it privkey to prove we owned one key, but without the HSM checking
we have a valid sig for the first commitment transaction, and that
we haven't revealed the revocation secret key, why bother?

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell edc30b12ea lightningd: --dev-debugger=<subdaemon>
Or for blackbox tests --gdb1=<subdaemon> / --gdb2=<subdaemon>.

This makes the subdaemon wait as soon as it's execed, so we can attach
the debugger.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell a72dd8d9de daemon/options: split option registration and parsing.
This allows us to add extra options before parsing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 952722a8cf lightningd/gossip: Fix return from gossip.
We should check that the peer it says it's returning is under its control,
we need to take back the peer fd, and use the correct conversion routine
for the packet it sends us.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 7525ed787a lightningd/hsm: create a peer-seed for peer secrets.
For the moment this is simply handed through to lightningd for
generating the per-peer secrets; eventually the HSM should keep it and
all peer secret key operations would be done via HSM-ops.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 90737371d0 lightningd/cryptomsg: split raw crypto_state vs peer_crypto_state
Raw crypto_state is what we send across the wire: the peer one is for
use in async crypto io routines (peer_read_message/peer_write_message).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell 6cf8a19881 lightningd/opening: receive details for remote config acceptance, not min/max.
The requirements for accepting the remote config are more complex than
a simple min/max value, as various parameters are related.  It turns
out that with a few assumptions, we can boil this down to:

1.  The valid feerate range.
2.  The minimum effective HTLC throughput we want
3.  The a maximum delay we'll accept for us to redeem.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell a8b9177e9e lightningd/opening: seed is a privkey not an sha256.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell b8472b65ba lightningd/build_utxos: don't lockin the UTXO reservation until confirmed.
Unless the transaction is confirmed, the UTXOs should be released if
something happens to the peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell be7c7b9732 lightningd/handshake: rename WIRE_BAD_COMMAND to WIRE_HANDSHAKE_BAD_COMMAND
So it doesn't clash with opening daemon, for example.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:56 +10:30
Rusty Russell b1403a764f lightningd/opening: use peer_failed to send PKT_ERR on problems.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:55 +10:30
Rusty Russell 8b279b7c14 opening: remove unused opening_watch_funding_req/resp.
This is now part of the responses: starting the normal peer daemon
is done once the tx is buried.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:55 +10:30
Rusty Russell 89af53267b lightningd/peer_failed: helper to send PKT_ERR and exit daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:55 +10:30
Rusty Russell 13ac472062 lightningd/crypto_sync: fix sync_crypto_write / sync_crypto_read
wire_sync_write() adds length, but we already have it, so use write_all.

sync_crypto_read() handed an on-stack buffer to cryptomsg_decrypt_header,
which expected a tal() pointer, so use the known length instead.

sync_crypto_read() also failed to read the tag; add that in (no
overflow possible as 16 is an int, len is a u16).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-24 16:22:35 +10:30
Christian Decker 8912272983 gossip: Prevent normal messages to trigger the staggered broadcast
Only call `peer_dump_gossip` if the broadcast timer actually
expired. previously it could get triggered by any normal message.
2017-02-21 17:59:34 +01:00
Christian Decker fccab6411d gossip: Add timer and normal queue for messages
The peer is woken up every 30 seconds to deliver the backlog of
messages. Additionally I added the normal message queue to be able to
send non-gossip message to the peer.
2017-02-21 17:59:34 +01:00
Christian Decker 83f51fe965 gossip: Add timer support to the io_loop 2017-02-21 17:59:34 +01:00
Christian Decker 597687d1f0 Fix CI build
Extraneous whitespace in cryptomsg.h
2017-02-21 17:56:35 +01:00
Rusty Russell 5475666b7e lightningd: simple wallet support.
This allows us to add funds via the P2SH-wrapped Segwit Transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:19:02 +10:30
Rusty Russell 065f11b42a lightningd/opening: opening daemon.
This conducts the conversation up until we have the txid to wait for
(or broadcast).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell e3f2d72d4d crypto_sync: synchronous routines for inter-peer crypto.
This is used by the opening daemon, for example.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell f66445c1d1 hsm: sign funding transactions.
The main daemon gives it to us to sign the inputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell 891a915e0f hsm: return BIP32 public seed on initialization.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell fddb38126d utxo: wire support for unspent transactions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell 19f3b68d28 hsm: remove shutdown command.
We don't use it, and should shutdown when control fd goes away anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell ac1890136c hsm: rename the hsmctl_hsmfd_ecdh_response to hsmctl_hsmfd_fd_response
We want to use it for other HSM fd requests.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell 08e95d59b5 permute_tx: generic pointer map.
Turns out we want to permute transactions for the wallet too, so we
use void ** rather than assume we're shuffling htlc ** (and do inputs,
too!).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:29 +10:30
Rusty Russell b431443266 hsm: BIP32 seed for bitcoin keys.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 4a7418e3db channel: object to track channel state.
This object is basically the embodyment of BOLT #2.  Each HTLC already
knows its own state; this moves them between states and keeps them
consistent.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell ec4db39a8e commit_tx: print HTLC id not amount when making test vectors.
For our test vectors there are two HTLCs with the same amount; this
is clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 9284819f68 commit_tx: expose more internal functions.
without having to build it, which is needed for limit enforcement.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell e8e96e67d0 commit_tx: make interface side-agnostic.
It's currently written to produce "local" commit-txs, but of course we
need to produce remote ones too, for signing.

Thus instead of using "remote" and "local" we use "other" and "self",
and indicate with a single "side" flag which we're generating (because
that changes how HTLCs are interpreted).

This also adds to the tests: generate the remote view of the commit_tx
and make sure it matches!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 068cdc825f htlc_tx: fix fee for HTLC-timeout tx.
Reported-by: Fabrice Drouin <fabrice.drouin@acinq.fr>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 726e7226c4 commit_tx: update HTLC-tx fees to match latest BOLT.
As per lightning-rfc BOLT #3 ec99f893f320e8c88f564c1c8566f3454f0f1f5f:
"fixed htlc weight calculation"

Reported-by: Fabrice Drouin <fabrice.drouin@acinq.fr>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 35909ba94a cryptomsg: wording fix with update to latest BOLT.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell f7cc079221 test/run-commit_tx: Fix derivation of BOLT #3 test vectors.
We were using the remote per_commitment_point instead of the local
per_commitment_point to generate the remotekey for the local transaction.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 20a07d860a check-source: include tests, libdir.
And fix the resulting issues.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:28 +10:30
Rusty Russell 9fd40da38c generate-wire: don't hand unknown structures specially.
It's awkward to handle them differently.  But this change means we
need to expose them to the generated code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-21 15:15:19 +10:30
Rusty Russell 66d122684d lightningd/test/run-commit_tx: Creation of BOLT 3 test vectors.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:22 +10:30
Rusty Russell 0fe53cc8e7 permute_tx: reintroduce permute map.
We used to have a permutation map; this reintroduces a variant which
uses the htlc pointers directly.

We need this because we have to send the htlc-tx signatures in output
order as part of the protocol: without two-stage HTLCs we only needed
to wire them up in the unilateral spend case so we simply brute-forced
the ordering.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:22 +10:30
Rusty Russell e11d9304ab lightningd/hbtlc_tx: creation of BOLT 3 htlc transactions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:22 +10:30
Rusty Russell f9189254e4 lightningd/commit_tx: creation of BOLT 3 commitment transaction.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:22 +10:30
Rusty Russell b290a96a4a lightningd: use daemon/htlc_state.c
We're about to need HTLC handling.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:22 +10:30
Rusty Russell be46fdf254 lightningd/funding_tx.c: helper to create a funding transaction (with change).
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:21 +10:30
Rusty Russell 75c6dd1981 channel_config: convenient structure for handling open/accept config info.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:21 +10:30
Rusty Russell dc3b27e1d3 key_derive: key derivation from basepoints as specified in BOLT 3
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:21 +10:30
Rusty Russell 4ed259c46c gen_peer_wire_csv: update for latest spec.
We have a separate announcement_signatures message now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-07 12:14:14 +10:30
Christian Decker fb814a7a9e gossip: Adding announcement handling to the gossip subdaemon.
We now have all the pieces to wire in the handling functionality to
the new gossip subdaemon.
2017-02-03 05:52:11 +10:30
Christian Decker 76e2c980e1 gossip: Moving to intmap-based broadcast for the legacy daemon
Moved the broadcast functionality to broadcast.[ch]. So far this
includes only the enqueuing side of broadcasts, the dequeuing and
actual push to the peer is daemon dependent. This also adds the
broadcast_state to the routing_state and the last broadcast index to
the peer for the legacy daemon.
2017-02-03 05:52:11 +10:30
Christian Decker 3cb576d69d refactor: Moving gossip/routing specific state into its own struct
This used to be part of `lightningd_state` which is being split up for
the various subdaemons. The main change is the addition of the `struct
routing_state` in `routing.h` and the addition of `rstate` in `struct
lightningd_state` for backwards compatibility.
2017-02-03 05:52:11 +10:30
Rusty Russell a600b1a30f handshake: hand in client fd on exec instead of gratuitous fd passing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-02 14:48:00 +10:30
Rusty Russell 13a30ff79c subdaemon: close unused fds when creating subdaemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-02 14:48:00 +10:30
Rusty Russell 2f9b04b0d9 subdaemon: start fds at 3 not 4.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-02 14:48:00 +10:30
Rusty Russell f9eea9041e handshake: update for BOLT 8 latest revision.
This now matches commit fc4846a27bf420c591ba0b024cc34972816913c7,
and fixes "make check-source".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-02 14:48:00 +10:30
Rusty Russell a02f1e10c4 make check: run all the tests.
We can't run them in parallel, but we can at least have 'make check'
run them all.

Developers should be running "make check-source && make check".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-02-02 14:48:00 +10:30
Rusty Russell 6bf3c30a10 lightningd/Makefile: fix check-source
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-13 10:51:57 +10:30
Christian Decker b41d71da30 Makefile: Making sure header vars are populated correctly
The problem with wire headers not being generated in time before stuff
depended on it turns out to be related with inclusion order of
sub-makefiles. The inclusions must preceed the use of
LIGHTNINGD_HEADERS since they append to that variable.
2017-01-13 10:50:07 +10:30
Rusty Russell 33748bf119 lightningd/Makefile: combine all headers (wire/gen was missing)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-11 10:15:27 +10:30
Rusty Russell 73d07ce441 libsodium: use our local submodule.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-11 10:04:26 +10:30
Rusty Russell 3af081aded Makefile: make all lightningd objects depend on all lightningd headers.
Christian reported that we weren't generating some gen_ files.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-11 09:16:29 +10:30
Rusty Russell 1e34e5344d lightningd/lightningd: add getpeers command.
This has an optional log level if you want to see logs for the peers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:34 +10:30
Rusty Russell dc8b174e3a lightningd/lightningd: maintain a per-peer log.
At the moment we just log each condition change.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 83b156517f lightningd/lightningd: maintain "condition" for each peer.
This lets us return better messages to the connect json command on failure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 7aaffda779 lightningd/lightningd: finish connect command once gossip started.
This is after the INIT message is received, so we know there are no
incompatible features.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell c536616bee lightningd/lightningd: wire up lightningd_gossip.
Now we hand peers off to the gossip daemon, to do the INIT handshake and
re-transmit/receive gossip.  They may stay there forever if neither we nor
them wants to open a channel.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 1800e84db7 subdaemon: callback to handle subdaemon status updates.
It's a bit messy, since some status messages are accompanied by an FD:
in this case, the handler returns STATUS_NEED_FD and we read that then
re-call the handler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 21a5c62ead lightningd_gossip: daemon to look after peers which don't a channel (yet)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 03758af4e1 lightningd/lightningd: create unique ID for each peer.
This distinguishes them before they have an ID, and also if the daemon
doesn't know the ID.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell bf118f1b86 lightningd/lightningd: add connect command.
Unlike the old daemon, this just connects; a separate command will be needed
to create a channel.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 211491f4d7 lightningd/lightningd: add lightning_handshake.
Now we do crypto handshake when peer comes in.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 78841456e0 lightningd/handshake: test handshake, produce BOLT 8 test vectors.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 32e1b5bb06 lightningd/handshake: simple daemon to do BOLT 8 crypto handshake.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 6f5fed17d8 lightningd/test: simple black-box tests.
These use the same infrastructure as the daemon/test blackbox tests,
so they're not currently wired into make check; use make
"lightningd-blackbox-tests".
2017-01-10 15:38:33 +10:30
Rusty Russell 0444d68197 lightningd/lightningd: add rpc interface.
daemon/lightning-cli works with this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 542e6844f7 lightningd/lightningd: start HSM at initialization time.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 10b8dc5950 lightningd/hsm: simple daemon to control the keys.
This provides APIs to access the keys.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 6a089ce112 cryptomsg: add towire/fromwire for crypto state.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 89a06734c4 lightningd/cryptomsg: test routines.
This creates output similar to the BOLT #8 test vectors.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell a7f682c66e lightningd/cryptomsg: routines to encrypt on the wire.
After the handshake, it's a simple matter of AEAD and key rotation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell faebb87d01 lightningd/subdaemon: routines to create daemons and get request/response.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:38:33 +10:30
Rusty Russell 8bf33c7839 lightningd/lightningd: daemon for controlling the other daemons.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-10 15:37:51 +10:30