Commit Graph

1386 Commits

Author SHA1 Message Date
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
Christian Decker a99744e68e gitignore: Added channeld to ignore list. 2017-03-11 16:54:16 +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 df7b68eea9 wire/wire_io: support take() arg to io_write_wire().
This simplifies memory management, but means we have to keep the
original pointer.

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 de39752d05 bitcoin/script: add internal hash160_key helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 16:12:18 +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 e0d9b6efdf daemon/watch: fix macro clash.
'peer' vs 'struct peer' works iff the argument is actually called 'peer'.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:27: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 484c45a8a1 chain_topology: fix double-free when peer dies.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-07 11:25:48 +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
Christian Decker 4a995a42de gen-wire: Cleanup of the wire generator tool
The wiregen tool was a bit hard to maintain since it was printing all
over the place, mixing template and processing logic. This commit
tears the two apart, externalizes everything that is not a single code
line, and repackages it into templates. Specifically functions are now
their own template and header/implementation files are a template.

Furthermore this simplifies some of the boilerplate of mapping types
to sizes and back again, by extracting them into dicts.

All changes have been verified to produce identical results on the
current wire definitions, except a bit of whitespace changes.
2017-03-06 10:22:02 +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 e2c7925e0e daemon/chaintopology.c: remove last remaining lightningd_state references.
We put a topology pointer into struct outgoing_tx and struct block for now.

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 d34dade8bb daemon/chaintopology: don't leave outgoing txs in peer structure.
The peer structure is only for the old daemon; instead move the list
of all outgoing txs for rebroadcasting into struct topology (still
owned by peers, so they are removed when it exits).

One subtlety: on exit, struct topology is free before the peers,
so they end up removing from a freed list.  Thus we actually free
every outgoing tx manually on topology free.

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 9387609c7b daemon/pseudorand: be more paranoid with isaac64 output.
There's no reason to think that the seed isn't reproducable from the
output: we don't want to give away our siphash seed and allow hashbombing,
so seed isaac with the SHA of the seed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 293bebbe2d daemon/peer: handle narrow reconnect race on close.
Usually if we get a packet while closing (onchain event), we're going
through pkt_in which discards it.  However, if we're reconnecting, we
simply process the init packet and get upset because they've forgotten
us.

Hard to reproduce, but here's the log (in this case, test-routing --reconnect
and we have just done mutual close):

We reconnect in STATE_MUTUAL_CLOSING, send INIT pkt:

   +19.397025114 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Init with ack 1 opens + 9 sigs + 8 revokes + 1 shutdown + 1 closing

While waiting for response, we see the mutual close...
   +19.398732602 lightningd(4637):DEBUG: reaped 6370: bitcoin-cli -regtest=1 -datadir=/tmp/bitcoin-lightning2 getblock 2a63b209e17aedc5b1bcc6c2f9e044f97c9c3ca136fc64a719f704d2f632df5f false
   +19.401834422 lightningd(4637):DEBUG: Adding block 5fdf32f6d204f719a764fc36a13c9c7cf944e0f9c2c6bcb1c5ed7ae109b2632a
   +19.405167334 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Got UTXO spend for 8bb48a:0: 7f5e422f...

   +19.412543610 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: anchor_spent: STATE_MUTUAL_CLOSING => STATE_CLOSE_ONCHAIN_MUTUAL

And we also see it buried "forever" (10 blocks in test mode), so we forget peer:
   +19.423045014 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Anchor at depth 13
   +19.426775063 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: check_for_resolution: STATE_CLOSE_ONCHAIN_MUTUAL => STATE_CLOSED
   +19.427613109 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: db_forget_peer(023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898)
   +19.428130685 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: db_start_transaction(023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898)
   +19.501027511 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: db_commit_transaction(023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898)

Now, we get their reply, but they've forgotten us:
   +19.520208608 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Decrypted header len 5
   +19.520872035 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Received packet LEN=5, type=PKT__PKT_INIT
   +19.520999082 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Our order counter is 19, their ack 0
   +19.521078913 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: They acked 0, remote=16 local=15
   +19.521447174 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Queued pkt PKT__PKT_OPEN (order=19)
   +19.522563794 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Queued pkt PKT__PKT_OPEN_COMMIT_SIG (order=19)
   +19.523517319 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:BROKEN: Can't rexmit 2 when local commit 15 and remote 16
   +19.524613177 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:UNUSUAL: Sending PKT_ERROR: invalid ack
   +19.526638447 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: Queued pkt PKT__PKT_ERROR (order=19)
   +19.527508022 023ec94fb93c669154ba7b08907276e8c8661b2e65d80fc2c089215d5395574898:DEBUG: peer_comms_err: STATE_CLOSED => STATE_ERR_BREAKDOWN

We should never transition from STATE_CLOSED to STATE_ERR_BREAKDOWn,
and that's what this check prevents.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30