Commit Graph

1550 Commits

Author SHA1 Message Date
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
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