Commit Graph

585 Commits

Author SHA1 Message Date
Rusty Russell 08a910b06e BOLT: fee requirements update.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 3de6ca3d0b BOLT: update comment to indicate HTLC id must be completely unique.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell ffeab49ed3 htlc_add: only limit is on receiving side.
BOLT has been updated, so update code and comments.  The receiving
side check is sufficient, as the limit is per-offerer, and that's the
only way the HTLCs get back to the offerer's side.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fa7934dfe3 htlc: implement deadline as per BOLT.
Thus a node MUST estimate the deadline for successful redemption for
  each HTLC it offers.  A node MUST NOT offer a HTLC after this
  deadline, and MUST fail the connection if an HTLC which it offered is
  in either node's current commitment transaction past this deadline.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 45cac95331 log: logging for HTLCs.
The most complex thing we've logged yet, so we extract the core of the
log_struct_ function to call it multiple times.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell db72f33988 log: simplify union.
A well-placed cast avoids requirement for const and non-const members.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell e877febf43 daemon: allow fulfill command for onchain HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 25d99b91bd daemon: fail any HTLCs which we weren't committed to if we drop to chain.
There's a corner case where they had it in their commit tx, in which
case we can't fail the HTLC until our commit tx has won.  Again, we
use dstate->config.min_htlc_expiry.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell caf50c9ae4 peer: handle fulfull and fail onchain.
We now need to use bitcoin_witness_htlc with the r value, so that API
is updated to take 'struct rval' or 'struct sha256'.

We use the nc->delay amount (ie. dstate->config.min_htlc_expiry) to
wait for a timeout refund to be buried before "failing" upstream.
This should probably be made into a clearer parameter rather than
overloading this one.

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


Header from folded patch 'dont-use-peer-nc-in-onchain-code.patch':

peer: Don't use peer->nc->delay for onchain case.

Use the config var directly.  We should be freeing peer->nc when the
connection dies anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 4f1a1ec044 BOLT: update for latest onchain draft.
It now notes that node may already know the redemption preimage,
update to fix.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b8571c1ac8 watch: make it easier for them to self-delete.
Rather than keeping a pointer so they can free themselves, make it
explicit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 3ba25dd994 htlc: keep rval (if known).
This makes struct htlc a complete object, containing its own information.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 69a8ea2ad9 daemon: pay command.
This is the command an actual user would use: it figures out the fee
and route, and pays it if it can.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 21a29d9b4d daemon: fix bug when we close two peers simulatneously.
If a block triggers two peers to close, we ran io_break() on both of them; the
second overrode the first and we didn't end up freeing that one.

Rather than chase such bugs in future, simply iterate to see if any
peers need freeing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 31a5de644a daemon: route fulfill back.
As soon as an HTLC we offered is fulfilled, fulfill the HTLC which
caused it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 21fe4fbba7 daemon: route failures back.
When an we offered HTLC fails, we fail the HTLC which caused it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 2aa8fe8df1 daemon: route payments onwards.
If the fee is high enough, we offer it onwards.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f994a44827 daemon/peer: keep our own node connection information.
Note that the base fee is in millisatoshi, the proportional fee is
in microsatoshi per satoshi. ie. 1,000,000 means charge 1 satoshi for
every satoshi carried.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 37b269f53e daemon: link HTLCs together.
Most HTLCs we offer are triggered by an incoming HTLC from a different
peer.  Save this "source" htlc, so we can fail/fulfill it when we
fail/fulfill this one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell cc4fc4b668 daemon: use htlc pointers everywhere.
No more copies!

I tried changing the cstate->side[].htlcs to htlc_map rather than a
simple pointer array, but we rely on those array indices heavily for
permutation mapping, and it turned into a major rewrite (especially
for the steal case).

Eventually, we're going to want to reconstruct the commit info for
older commit txs rather than keeping all the permutation and
per-commit-info HTLC information in memory, so we can do the work
then.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 67ac2d2081 daemon: code for HTLC maps.
We currently keep copies of HTLCs in each commit_info structure, but
that's redundant.  Keep per-peer per-direction maps of HTLCs, then we can
just throw pointers around (next patch).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fecd91ab2a Move funding.[ch] to daemon/channel.[ch].
It's a more logical name, and a more logical place.  We change
"funding" to "channel" in the remaining exposed symbols, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b2fdc86740 daemon: check and use routing info in HTLC packet.
We only support being the end node for the moment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f4f0c1aa18 daemon: add dev-routefail command.
We're about to change the code so that if it can't route, it will fail
the HTLC.  The current low-level tests will hate this, so have a dev switch
to turn that off.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell bf3acfab62 daemon/htlc: including routing information.
This is the logical place for it to belong: with the HTLC.  For the manually-created
HTLCs, we create a simple one-hop route.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 4902907628 daemon: onion routing API
No meat; it doesn't encrypt as yet, but the API to wrap/unwrap routes is here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f06855ff6c daemon: extract newhtlc code into common function.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b1ac490bea daemon: add-route RPC command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 26a03acfd0 daemon: routing infrastructure.
Simple routing topology, and ability to ask about the cheapest route.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 27da8f77b5 daemon: expose find_peer(), rename other to find_peer_json().
This is the more normal case; find by ID.  The low-level json commands are
really just for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell a3375516e5 daemon: don't ever use timeouts in seconds, always blocks,
The protocol still supports both, but we now only support blocks.

It's hard to do risk management with timeouts in seconds, given block
variance.  This is also signficantly simpler, as HTLC timeouts are
always fired in response to blocks, not wall-clock times.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell c1e1299249 daemon: always log error packets we create.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 5296b7f9a0 log: add structure logging.
Uses a gcc extension (cast to union) for typechecking, but that can be
removed for compilers which don't support it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 04b3e8f91d daemon: use siphash for hashes.
Remove ccan/hash (aka Jenkins lookup3) altogether.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell ba9df99770 daemon: wire in payment.
This actually uses the accept-payment data to make payments.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 5027410ab1 daemon: payment infrastructure.
A new 'accept-payment' command tells the node to fulfill HTLCs using
the R value if the amount is correct.  It's not wired in yet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell f1af56fcee daemon: save acked changes, so we can process them when confirmed on both sides.
We need to know when changes are fully committed by both sides:
1) For their HTLC_ADDs, this is when we can fulfill/fail/route.
2) For their HTLC_FAILs, this is when we can fail incoming.

For HTLC_FULFULL we don't need to wait: as soon as we know the preimage
we can propogate it.

For the moment, we simply log and assert; acting on it comes later.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell eeb9b9de84 funding: make funding_htlc_by_id() return pointer, not offset.
While the pointer is only valid until the funding changes, that's also
true of the offset; and a pointer has a convenient "not found"
sentinal value.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 156d1be9ed daemon: struct rval to represent r values.
We've been stuffing these into sha256s, but they're actually nonces.
Create a new structure for that for clarity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 1abc676c4f daemon: always take packets into pkt_in, then have it demultiplex.
Cleanly separates packet handling functions, and the weird transition cases.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell b4f0d32b09 daemon: always terminate waiting manual update command on failure.
We missed some cases, resulting in hanging commands.  Just check whenever
we fail.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 1444d407f3 daemon: remove normal operation loop from state.c
It's now in its own little state machine, which is more typesafe.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell bc5800b1c1 state: remove unused fields from union input
And make the add/fail/fulfill arg a pointer to a union htlc_staging
directly, removing struct htlc_progress.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 50bffd67c2 daemon: don't save invalid commit signatures.
We don't want anyone to think the commitment tx is signed, so only
assign ci->sig after validation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 43db90391a daemon: fix case where commit has nothing to do.
Prevents assert() triggering in queue_pkt_commit().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell b820f5a0dd Makefile: fix submodule.
Christian noted that missing daemon/jsmn/jsmn.h didn't trigger the git
submodule weirdness.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 9046597344 daemon: tighten check for invalid IDs.
We don't have to, but helps debugging.  Language in latest rev of BOLT#2
has been tightened too (aa2e1919de0826beaf92e0b3b441a6ab9fce6261)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 3928cbe30e daemon: fail connection if preimage isn't valid for shachain.
This means they generated it wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-06-25 14:20:15 +09:30
Rusty Russell 6bbd05c001 Merge remote-tracking branch 'origin/pr/25' 2016-06-25 14:19:57 +09:30
Rusty Russell 2436207a7a test: detect segwit correctly, assume master branch.
Segwit was merged, but the strings changed between there and segwit4
(also, my BIP9 patch changed the output).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-06-25 14:13:08 +09:30
sstone be1a230ae8 fix formatting issues 2016-06-23 17:11:10 +02:00
sstone 07e6ed0c16 save received revocation preimages in shachain.
make sure that preimages are generated in reverse order.
2016-06-23 16:38:35 +02:00
Rusty Russell 49ebed737d daemon/test: test differential fees.
This would have revealed the previous breakage (and I tested that!),
plus now we test negotiate on closing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-30 11:18:46 +09:30
sstone 385c2a5905 setup_first_commit: initialize their remote commit with their commit fee rate 2016-05-27 16:53:01 +02:00
Rusty Russell 40b550a9d2 daemon: don't log bogus warning on failed anchors.
We no longer get bitcoind to manage our transactions for us, so we don't
need to -zapwallettxs when an anchor fails.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 773a6088e4 daemon: reorder and collapse functions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 400d415172 daemon: remove pending input and command queues.
we don't use them any more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 889db659c5 test: test commands during commit phase.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 4110376e87 daemon: allow commands during commit.
There's no real reason to avoid commands for the next commit; this has
the benefit that we can remove the infrastructure to queue commands.
The only exceptions are the commit command and the opening phase.

We still only allow one commit at a time, but that's mainly run off a
timer which can try again later.  For the JSONRPC API used for
testing, we can simply fail the commit if one is in progress.

For opening we add an explicit peer_open_complete() call in place of
using the command infrastructure.

Commands are now outside the state machine altogether: we simply have
it return the new state instead of the command status.  The JSONRPC
functions can also now run commands directly.

This removes the idea of "peercond" as well: you can simply examine
the states to determine whether an input is valid.  There are
fine-grained helpers for this now, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell d4862938c8 daemon: move unacked queue into commit_info struct.
We're about to allow changes while we're waiting for a commit ack.
This means we can't have a single "unacked changes" queue; when we
receive the revocation reply, we need to apply the unacked changes
known at the time we sent the commit, not any we've created since
then.

Note that we still only have a single staged_commit; we never have two
outstanding commits, since for simplicity we will still block
following update_commit pending the reply to the current one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 36fc62ab81 test: add --crash option to cause nodes to crash dump on test failure.
This causes full logs to be dropped in "crash.log".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell ca9d775f9a lightning-cli: don't print junk on error.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell f662424b7b protocol: don't sign initial commitment for non-funder.
As per lightning-rfc commit b8469aa758a1a7ebbd73c987be3e5207b778241b
("re-protocol: don't hand signature to non-funding side initially.")

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5188b14c7c daemon: fix unwatch anchor depth.
We still need to watch the anchor output in this case: that's what
makes us handle the commit transcction we broadcast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell b9d4f7c0ab daemon: dev-output command.
Useful for controlling conversations between two nodes, by
blocking one's output.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 97bc4ed0cb daemon/test: test mutual close with outstanding HTLCS.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5aed0e12f8 daemon: remove closing states from state machine.
We already removed the on-chain states, now we remove the "clearing" state
(which wasn't fully implemented anyway).

This turns into two smaller state machines: one for clearing, which
still allows HTLCs to be failed and fulfilled, and one for mutual
close negotiation which only allows close_signature messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 4c63845969 daemon: make funding directions local-centric.
Previous to this, we kept the remote side's 'struct channel_state'
backwards: peer->remote.commit->cstate.side[OURS] was their HTLCs,
and [THEIRS] was our HTLCs.  This made some things easier, but was
horrible for readability.

This inverts things so we keep track of the remote side's state from
our point of view: [OURS] is ours, [THEIRS] is theirs.  Which makes
much more sense.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 148bd793cd daemon/test: test overlapping commits.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 45a6f81c3c protocol: remove ack fields.
As per lightning-rfc commit 8ee09e749990a11fa53bea03d5961cfde4be4616,
we remove the acks from the protocol now they're no longer needed (and
all the infrastructure).

We also place the commit number in the commit_info where it logically
belongs, removing it from the peer struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 2bf43f1ebd daemon: handle HTLC as per BOLT #2 algorithm.
From BOLT#2 (rev 8ee09e749990a11fa53bea03d5961cfde4be4616):

   Thus each node (conceptually) tracks:
...
   3. Two *unacked changesets*: one for the local commitment (their proposals) and one for the remote (our proposals)
   4. Two *acked changesets*: one for the local commitment (our proposals, acknowledged) and one for the remote (their proposals, acknowledged).

   (Note that an implementation MAY optimize this internally, for
   example, pre-applying the changesets in some cases).

In our case, we apply the unacked changes immediately into
staging_cstate, and save them in an unacked_changes array.  That array
gets applied to staging_cstate as soon as it's acked (we only allow
one outstanding update_commit, so we only need one array).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell cf7a7a7273 funding: use sides[OURS/THEIRS] instead of a and b.
This is a little clearer, and handling arrays is easier than separate
variables.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 84f5a82eea daemon: use "local" and "remote" instead of "us" and "them".
This is the language used in BOLT#2; be consistent.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 311ae9b4d8 daemon: remove unused functions.
These were left over from when the state machine handled onchain
transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 0ceee86098 daemon: don't allow fulfill/fail on uncommitted HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell f43cc72d6a Makefile: add generated packet names.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell fabdcaf62b daemon: close correctly when guest fails.
Otherwise we can receive another packet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5a2a9126c3 daemon: don't allow new RPC commands when peer closing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:24:39 +09:30
Rusty Russell b6339af195 daemon: remove unused CMD_REQUEUE.
This was a remnant of the old code which used to alternate priority
for making changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:23:39 +09:30
Rusty Russell c710a64ccf Makefile: support for suppressing valgrind.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:49:54 +09:30
Rusty Russell 35d1b13cde daemon: commit outstanding changes via timer.
While useful for testing, it doesn't make sense to have an explicit commit
command; we should commit whenever there are outstanding changes.

We have a 10ms timer to allow limited batching, however.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:30:11 +09:30
Rusty Russell fe1ba96332 daemon: time options use opt_time.
Currently this mean --bitcoin-poll; we're going to change the other time
options to block heights anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:29:12 +09:30
Rusty Russell 6c7facfd2d daemon: time option support.
No need to have all times in seconds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:28:17 +09:30
Rusty Russell 82c2325467 timeout: make all timers one-shot.
It's closer to what we want, and simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:26:09 +09:30
Rusty Russell 4beaedfa49 daemon/test: clean up Makefile.
This means mkae tells us directly what failed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:47 +09:30
Rusty Russell 604122e787 daemon: permute input in steal transaction.
This is just generally good practice.  All our other txs are single-input,
so we've not needed to permute inputs before.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:11 +09:30
Rusty Russell 082eaf406e daemon/test: fix spending check.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 15:56:47 +09:30
Rusty Russell 09de557c69 daemon: update limit to reflect latest BOLT#2.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 14:42:52 +09:30
Rusty Russell d6603adc2f daemon/test: test stealing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 12:00:30 +09:30
Rusty Russell 5e40b264dd daemon: handle cheating.
As per onchain.md.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:55 +09:30
Rusty Russell 4cb6cd1f90 daemon: handle information leak.
As per onchain.md; log that an unknown spend occurred, and weep.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:32 +09:30
Rusty Russell 1be98d860f daemon: handle receipt of error packets.
If it's all printable, print it, otherwise dump hex.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:55:52 +09:30
Rusty Russell e1c6f2d630 daemon: dev-signcommit command to sign the current commit tx.
Do not use this!  We use it to test stealing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:54:49 +09:30
Rusty Russell effcb73a48 daemon/test: speed up and clean up tests.
They would sometimes fail under load, if using valgrind.  Retry
properly rather than relying on random sleeps.  Also, takes "make
check" running time here from 1m31.864s to 1m16.872s.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:36 +09:30
Rusty Russell 7ae15401dd daemon: log all state changes the same way.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:34 +09:30
Rusty Russell 623eec4068 daemon/test: test unilateral close.
We use dev-disconnect to convince one node the other has disconnected
(but not vice versa), to get deterministic behaviour.  We do this with
one HTLC outstanding, to test the HTLC timeout path.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell 0f9889f2c6 state: trim unused states.
Now we never enter the state machine if we're dealing with on-chain
transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f29a6043d2 daemon: open-code handling of on-chain states.
Once we see an on-chain tx, we ignore the state machine and handle it
as per the onchain.md draft.  This specifies a *resolution* for each
output, and we're done when they're irrevocable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f6b36b9be3 Revert: 064309df1a "peer: signature in commit_info is always valid."
It's not quite true: if we offer the anchor, we have a commitinfo
without their signature yet.  So make it a pointer again.  Since we
always allocate struct commit_info with talz, it starts as a NULL
pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:13:50 +09:30
Rusty Russell c94f77fd14 forever-confirms: warn if less than 100.
BOLT #onchain considers 100 the minimum depth to be "irrevocable".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:50 +09:30
Rusty Russell 5eb50345ae daemon: implement bitcoin_htlc_timeout()
This is called when an HTLC times out, and we need to send it back to
ourselves.  We also adjust the locktime, since in practice we should
refuse an HTLC less than our locktime.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:47 +09:30
Rusty Russell a4125313ce peer: helpers to extract a given HTLC from commit_info.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:47 +09:30
Rusty Russell 1b49d2afa6 chaintopology: always track txs we broadcast ourselves.
This is inefficient, but it means we always know the tx depth.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:16 +09:30
Rusty Russell 7d4d2977b6 watch: depth callback is always >= 0
We don't report conflicts, just depths.  So we report 0 if it's in a
main chain which loses to another, otherwise it's always positive.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:39 +09:30
Rusty Russell 4e102ccfcf chaintopology: simply track txids, not watches.
This is less efficient, but simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:37 +09:30
Rusty Russell 57ec0397ad chaintopology: only deal with the main chain.
Since bitcoind doesn't propagate non-main chains, there's little point
trying to be smart when we see them.  This simplifies things immensely.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:06:19 +09:30
Rusty Russell 17167704a6 daemon: handle bitcoin transaction re-broadcasting.
It's primitive, but we re-broadcast any txs not included in the main
chain every time the tip moves.  We only track transactions we are
watching, but that turns out to cover every transaction we generate
anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell c1dc0e54fa daemon/bitcoind: bitcoind_sendrawtx to send hex string.
This can fail.  Real cases include both sides dumping their commitment
txs in testing (only one can succeed).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 8fe2ba0ab3 daemon/bitcoind: don't eliminate output on non-zero exit status.
Abort if caller wasn't expecting it, otherwise save the result.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 29db78ea43 daemon: always call state machine through state_single.
That logs transitions, and we're about to patch it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell e18aea8d71 daemon: simplify fee calculation for spends of our own commit tx.
It's not exact, but faking a sig, measuring length, then resigning was
neither exact nor pretty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell 12b37d5f80 daemon: fix logic which determines how anchor output was spent.
We watch the anchor output, and separate it into different cases.
This is simpler with segwit (txids are known before sigs), but we also
had missed the case of our own commit transaction spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell eb5d832963 state: don't spend the "to-us" output from their commit tx.
There's no reason to, it's a simple p2wpkh to our key.

We still spend the "to-us" from our commit tx, since it could be
theoretically be stolen by the revocation value, and it's a complex
p2wsh which a normal wallet won't have the information to spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell c4713a6ed5 state: use htlc_onchain structure
Turns out that we want to pass information about the commit info, the
HTLC number and (sometimes) the R value, so create a struct for that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell bccd10c6d8 daemon: don't code check for cleared commits in the main loop, use pending queue.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 33a477d65d daemon: add pending input queue.
You can't re-enter the state machine from a callback, so this allows you
to queue an input for when it returns.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell c94c495257 daemon: allow multiple watches on the same tx.
This turns out to make life easier for watching HTLC timeouts (we just
place a new watch for each HTLC).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 77a89bcf2b watch: indicate which input of tx is spend the watch txo.
If we generate a tx which spends a heap of TXOs (eg. steal
transaction), we'll need this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 9eabab78ab utils: tal_hexstr() helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 6f2cb72c27 daemon/test: don't generate tiny dust HTLCs in testing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 7614e513bd commit_tx: keep permutation map.
This lets us map the HTLCs back to outputs when txs occur onchain.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 76cb195ea1 daemon: dev-disconnect command.
This lets one end experience a disconnect without the other noticing.
Good for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell faed0ef736 daemon/test: use config file rather than long cli args.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 4bbb86ae30 daemon: clean up test dirs.
Move final helpers out of test-cli/

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 064309df1a peer: signature in commit_info is always valid.
It's given in the packet which creates the new commit_info, so no need to
make it a pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 03a538ca02 check-bolt: check that comments in code match the specs.
And fix the mistakes!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 4fe90ee3f5 peer: fix mutual close detection.
Since 43729c6856 (protocol: add output script to close_clearing message.)
the close scripts are not p2sh, but arbitrary.  Fix the close tx matching.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 43729c6856 protocol: add output script to close_clearing message.
We just use a p2sh to a single address for the moment, but that's simply for
non-segwit wallets; we'll pay to whatever the other side specifies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 20:01:52 +09:30
Rusty Russell f24b73124a Remove txid normalization.
Since any transaction with all segregated-witness inputs is non-malleable,
and all our transactions are that, we can remove normalized txids.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 20:01:52 +09:30
Rusty Russell b1700b1a91 protocol: non-HTLC commit tx outputs are p2wpkh
This is changes the payments to either party to be p2wpkh.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:57:35 +09:30
Rusty Russell bd081d219d protocol: anchor output is now witness 2of2.
Rather than p2sh of a 2of2, it's now a version 0 witness program.
This means that the commit transaction input and mutual close
transaction input are both different.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:55:35 +09:30
Rusty Russell 8bd334380e peer: use tip mediantime for CSV timeout.
Using wallclock is gauche (and I saw it fail once in tests), so fix that
FIXME now it's easy.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:52:35 +09:30
Rusty Russell 85554761c8 bitcoind: eliminate wallet routines.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:51:35 +09:30
Rusty Russell b5a6ac26c7 watch: don't hand blockhash, have commit_tx_depth() use get_last_mediantime()
There isn't a single blockhash; we may be on multiple forks.  But the one
caller which cares is commit_tx_depth(), which wants to know if the tx is
spendable yet.  So that uses get_last_mediantime().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:50:35 +09:30
Rusty Russell e3868b11d2 peer: use watch_txo to watch commit_tx outputs.
We really want to do this for HTLCs; we don't do anything useful yet, but
this code replaces the direct call to bitcoind_watch_addr().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:49:35 +09:30
Rusty Russell 7b4de8e445 watch: use chaintopology
Rather than polling for interesting bitcoin txs via importaddress, we use
the chain topology to register our interest directly.x 

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:48:35 +09:30
Rusty Russell 6e39b0a642 chaintopology: get_last_mediantime()
This gets the median time of the block the tx is in.  If there is more
than one (different tips), it gets the last median time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:46:32 +09:30
Rusty Russell e09795d24e chaintopology: get full tx information for each block.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:42:18 +09:30
Rusty Russell e60b1b4efa bitcoind: use correct endianness for block hashes.
Like txids, we need to reverse them.  We didn't, but then we only used them
to pass to/from bitcoind.  We're about to get them from the block header,
so we need to fix that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:41:20 +09:30
Rusty Russell e7b9a913a6 bitcoind: getrawblock helper.
It turns out using the verbose=false parameter, you can get the raw block
from getblock.  Do that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:40:29 +09:30
Rusty Russell 3d9cb81215 watch: express everything in terms of watch_tx and watch_txo.
With segregated witness, we can (in advance!) specify the txid or tx
output we want to watch, so convert to that now.  For the moment it's
done by pretending we have normalized txids; that goes away after the
conversion.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:39:21 +09:30
Rusty Russell 14d722d48d bitcoind: pretend normalized txids are in the block.
This lets us live in a segwit world, before segwit.  It's a shim which we
can remove once we've changed all our outputs.

We need a few more sleeps in our test script, since we've slowed
things down by doing these calls for every tx in every block.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:38:13 +09:30
Rusty Russell 521d3d53ed chaintopology: keep track of the bitcoin block topology.
This allows us to track precise transaction depth ourselves,
particularly in the case of branching.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:37:13 +09:30
Rusty Russell 01e46d154e bitcoind: getchaintips support.
This allows us to sae the block topology in detail.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:36:13 +09:30
Rusty Russell 1d27428aab log: use real time for logs, even if dev-mocktime is used.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:35:13 +09:30
Rusty Russell 45fa89e134 daemon/test/test.sh: neaten state checks.
Better debugging when things go wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:34:13 +09:30
Rusty Russell 1568774e62 daemon: assert that all anchor inputs are witness.
Otherwise, they're malleable.  We only care about our own anchor:
their anchor is their problem (and they'll probably get away with it).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:47 +09:30
Rusty Russell eb14111cd0 daemon/wallet: supply a p2wpkh address (as P2SH address).
This is an address that bitcoind will happily pay to, but we know it's
a witness output so our inputs to the anchor are immalleable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:46 +09:30
Rusty Russell c25ac68538 bitcoind: better logging for failures.
In particular, print out the entire command line we used.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:46 +09:30
Rusty Russell edcec2ba4e daemon/test: activate segwit.
You need to be running a bitcoind modified with segregated witness:

	https://github.com/sipa/bitcoin/tree/segwit4

It needs 432 blocks to activate it!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:45 +09:30
Rusty Russell f7d86da1b5 daemon: have user supply UTXO for enchor input.
This lets us ensure that anchor tx has witness scripts for inputs, and thus
is immalleable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 13:07:04 +09:30
Rusty Russell 772a960c41 tx: add measure_tx_len() helper.
We currently linearize and then measure the string; this is better since
we're about to do it in a second place.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 13:07:04 +09:30
Rusty Russell e84bf16bf8 bitcoind: fee estimation support.
This can be used for proper support for fee negotiation; for the moment
it will be used for our anchor transaction creation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 13:07:03 +09:30
Rusty Russell 51de503096 daemon: keep track of simple addresses for injecting funds.
We need to control the *inputs* to the anchor tx, to make sure they
pay to witness scripts (thus the anchor is immalleable).  The easiest
way to do this is to hand out P2SH addresses for the user, and have
them pay into those.  Then they hand us that tx and we use it to
create the anchor.

This is not a long-term solution!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 13:07:03 +09:30
Rusty Russell df5d4e3c10 bitcoin/signature: BIP143 signature support.
We hand in the witness_script: if non-NULL, we use BIP143-style
signature hash creation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 13:05:51 +09:30
Rusty Russell 58b14292ad bitcoin/tx: (optional) input amount.
We need this for signing segwitness txs.  Unfortunately, we don't have it
for transactions we received as hex, only ones we created; to make this safe
we use a pointer which is NULL if we don't know, and those will crash if
we try to sign or check their sigs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:43:53 +09:30
Rusty Russell 66084271c3 bitcoind: work around fundrawtransaction segwit issue.
See https://github.com/sipa/bitcaoin/issues/67

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:41:53 +09:30
Rusty Russell 95340aa03f bitcoind: use fundrawtransaction rather than sendtoaddress and -nowalletbroadcast
Luke-Jr points out this is the Right Way to do these things.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:40:53 +09:30
Rusty Russell 40b14981fd daemon: fix BIP68 support.
We got the -> second translation wrong by a factor of 512, and also we
need to move the median time in our tests otherwise bitcoind won't let
us spend the tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:34:29 +09:30
Rusty Russell 8104886503 Remove Alpha support.
I had already disabled it, and this clears the decks for Segregated Witness
which gives us everything we want.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:32:43 +09:30
Rusty Russell 0f35441a29 protocol: move `ack` out of header into specific packets.
This reflects the BOLT #1/#2 protocol change, as suggeted by Pierre.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:31:43 +09:30
Rusty Russell 0e07cc7a36 daemon: fix close fee negotiation.
We always set *matches to false (outside the branch, oops).  We also
distinguish the case where we ack from the case where they acked,
which removes a FIXME and makes it work.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:30:43 +09:30
Rusty Russell 4c136dde98 peer: don't free unclosed connection.
We need to close it first, otherwise use after free in
peer_disconnect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:29:43 +09:30
Rusty Russell 15f7ec436c Fix "make check-source"
Obviously, haven't run it in a while.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:28:43 +09:30
Rusty Russell cc7cb0163e daemon: make logic in bitcoind interface more explicit.
In particular, we expect not to have a block hash if (and only if!) confirmations == 0.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-01 14:08:55 +10:30
zhibiao.pan e8a83c3f76 fix "missing braces around initializer"
Fixes #20
2016-04-01 13:13:56 +10:30
Rusty Russell 5e7b3d02a1 daemon: batching of changes as per BOLT #2.
We now keep a list of commitment transaction states for "us" and
"them", as well as a "struct channel_state" for staged changes.

We manipulate these structures as we send out packets, receive
packets, or receive acknowledgement of packets.  In particular, we
update the other nodes' staging_cstate as we send out our requests,
and update our own staging_cstate are we receive acks.  When we
receive a request, we update both (as we immediately send out our
ack).

The RPC output is changed; rather than expose the complexity, we
expose our last committed state: what would happen if we have to drop
to the blockchain now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-31 17:13:20 +10:30
Rusty Russell b7a7234717 packets: remember callbacks for acks on queued packets.
Not used yet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-30 16:57:41 +10:30
Rusty Russell 57689390fb state: queue packets directly.
Rather than creating packets then queueing them, call out to functions
which do both.  This moves us towards doing more work in those functions
where we send out a request, which is sometimes clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-30 16:57:18 +10:30
Rusty Russell 8ed68179a5 funding: add 64-bit id to HTLCs.
This gives us a reliable way to distinguish HTLCs, even in the face of
duplicate R values.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-30 16:55:03 +10:30
Rusty Russell b6943b9198 protocol: remove support for uncompressed pubkeys.
There's no good reason to support them, and this way every key is 33 bytes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-30 16:54:16 +10:30
Rusty Russell 8c468c1e15 daemon: use fee rates rather than absolute fees (BOLT #2)
And divide fees as specified there.

We still use fixed values rather than floating, and we don't send or
handle update_fee messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:12:43 +10:30
Rusty Russell ba2854e835 peer: don't segv if closing before anchor established.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:11:56 +10:30
Rusty Russell 04b4eb2f59 daemon/test: Fix printing when getblock doesn't show tx.
Ran into this when machine was under massive load.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:10:42 +10:30
Rusty Russell 994addadce state: INPUT_CONNECTION_LOST
We used to have a hacky close timeout which would immediately fire
when we'd closed because the connection was down.  Far better to have
a specific "connection lost" input, and have it respond like CMD_CLOSE.

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

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

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:09:41 +10:30
Rusty Russell afedf0e8ac cryptopkt: implement ack callbacks.
For the change to asynchronous updates as specified by BOLT #2, we
need to know when the other side acknowledged a packet.  This creates
a simple callback mechanism for it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:03:44 +10:30
Rusty Russell 8131ff57a8 cryptopkt: don't create clever io routine, embed callback in io_data.
We have a structure, let's use it to store the callback and avoid
lots of complex code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:02:25 +10:30
Rusty Russell 18a9e5bda2 daemon: Implement encryption as per BOLT #1.
This uses libsodium (we could use openssl, but the required primitives
are only in 1.1.0 which is still in alpha).

It doesn't handle reconnections yet, either.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-16 16:06:17 +10:30
Rusty Russell 923313e3d3 crypto: add length prefix to handshake.
As per BOLT#1.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:10:42 +10:30
Rusty Russell 6a062d8527 daemon: check protobuf-c version, report failure.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:09:42 +10:30
Rusty Russell 6410b0ac9c test: don't reply on specific bitcoin.conf settings.
I changed mine off regtest, and "make check" broke.  Fix that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:42 +10:30
Rusty Russell 8f2e66089b daemon: add --bitcoin-datadir for bitcoin-cli.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:40 +10:30
Rusty Russell f8a96ba11f daemon: remove try_command call after queue_cmd.
queue_cmd already calls try_command; this is reduandant.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:36 +10:30
Rusty Russell b017ca1240 protocol: include next revocation hash in open packet.
This means we send the first two revocation hashes; this is important
once we move to a commit model as we need to send (unsolicited) the
signature for the *next* commit tx so we need its commit hash.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:35 +10:30
Rusty Russell 53a8aef95c daemon: use dynamic array for outgoing queue.
Coming changes to the protocol allow theoretically infinite outstanding
packets, so remove [5].

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:31 +10:30
Rusty Russell cc16f46621 daemon: introduce union htlc_staging for proposed changes to HTLCs.
This encapsulates proposals more cleanly, and is important when we change
the protocol to have more than one outstanding at a time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:07:31 +10:30
Rusty Russell b8911cde98 daemon: don't allocate htlc inside struct newhtlc.
It's overkill.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:07:31 +10:30
Rusty Russell b218e79206 daemon: make find_peer take the raw token.
Cuts duplicate code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:07:31 +10:30
Rusty Russell 71c02c7b4c daemon: make json_get_param() understand which args are compulsory.
So far only one isn't, so this saves us some checks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:07:30 +10:30
Rusty Russell 85147347e2 funding: explicitly mark which side offered the anchor.
The channel funding code needs to know who offered the anchor, as they
are responsible for paying fees until the other side is able to.  This
is actually a hack, but at least now it's internal to funding and not
passed in at every funding_delta() call.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:07:30 +10:30
Rusty Russell cb0cc80984 patch remove-timeout.patch 2016-03-15 17:07:12 +10:30
Rusty Russell 1f9103c9d3 daemon: rename num_htlcs to commit_tx_counter.
Much clearer name.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:43:15 +10:30
Rusty Russell 983000428f daemon: rename CMD_SEND_HTLC_UPDATE to CMD_SEND_HTLC_ADD
There are other updates than just adding an HTLC; make this explicit.

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

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

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

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:38:15 +10:30
Rusty Russell 35ab923163 peer: fix dangling peer->current_htlc->htlc pointer.
It currently points into freed memory once we've make_commit_txs; we
don't currently dereference it after that, but I did in some test code
and got a surprise.  Make a copy in all cases where we set it, so
there can't be lifetime problems.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:36:15 +10:30
Rusty Russell 3fbee72f3a daemon/test: make --verbose flag less verbose.
Just print out the commands we do, not -x which gives TMI.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:35:15 +10:30
Rusty Russell ae82c0a12c daemon/test: Make --gdb1/--gdb2 actually work, clean up params.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:34:15 +10:30
Rusty Russell 16a7e3dc97 daemon/test: always test with valgrind, don't print errors unless a problem.
Unfortunately this seems to require longer sleeps.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:33:15 +10:30
Rusty Russell b280cab6b0 Makefile: clean daemon files with make clean.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:32:15 +10:30
Rusty Russell c1d1b360bf daemon: add memcheck() in various places where we expect things to be initialized.
This just causes valgrind to warn sooner.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:31:15 +10:30
Rusty Russell ab1176d218 jsonrpc: rename "id" to "peerid" everywhere.
To be distinct from HTLC ids.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:30:15 +10:30
Rusty Russell 48a4ec88dd daemon/test/test.sh: remove -x flag, add --verbose option.
--verbose also turns on grep outputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:29:15 +10:30
Rusty Russell 862509637b daemon: implement unilateral commit.
This is only for the simple case where there are no HTLCs.

We group the current commit information together in the struct;
this involves a trivial transform from peer->cur_commit_theirsig to
peer->cur_commit.theirsig.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 27eedc9f51 patch bitcoind_get_mediantime.patch 2016-01-22 06:45:28 +10:30
Rusty Russell 6afe3f718d daemon: bitcoind callback gives the blockhash the tx was included in.
This is required for transactions which use OP_CSV to lock outputs for
a given amount of time: we need to know the mediantime of the block
they were included into.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell b70c18a40e daemon: implement anchor watch timeout.
We abort when this happens, but still worth testing.

This involves a refactor so we can allocate watches off a specific context,
for easy freeing when they're no longer wanted.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 4e5ced4de2 daemon: don't crash on pkt_err
We're about to test it, for anchor timeout.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 168ed96b12 daemon: close command.
This performs a mutual close.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 45c5c83d6f daemon: exit main loop to free dead peers.
When a peer is finally to be freed (ie. STATE_CLOSED), doing this
inside the state logic is a bit fraught.  We're better off exiting the
io loop and freeing it there.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell f690be1e83 daemon: dev-crash command.
Causes us to dump in crash.log.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 212f8ee022 daemon: fail if we enter an error state.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell b76858c1a1 daemon: implement HTLC expiry.
We do the simplest thing: a timer goes off, and we check all HTLCs for
one which has expired more than 30 seconds ago.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 1018823f97 daemon: HTLC expiry limits.
Don't accept an HTLC which is about to expire, nor one which will take
too long to expire.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell a3e3f83d9f daemon: dev-mocktime command
Useful for precise timing control for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell f3c5aa7634 daemon: don't close conn until we've sent all the output packets.
Otherwise we won't finish the conversation.  In fact, only the writer
side should ever close: we wake it if we want to close and it tests
peer->cond.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 8b666ea449 daemon: queue commands rather than executing them immediately.
When the only commands are via JSON, you might argue that we should
simply insist the user not operate on the same peer in parallel.  That
would suck, and also we need to handle the case of a command from
a timer (eg. HTLC expiry!) or a bitcoin event.

So, we need a queue for commands, but also we need to do some of the
command checking just before the command runs: the HTLC we're dealing
with might have vanished for example.

The current command is wrapped in an anonymous "curr_cmd" struct
for extra clarity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 2346f6bf14 daemon: routefail command.
This should be renamed: it's actually any kind of after-the-fact failure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 86f4bd772c daemon/test/test.sh: check_status() helper for HTLC status checks.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 1e82799852 daemon: fulfillhtlc command
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 17359279b2 daemon: getpeers: list HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell e1f772a443 peer: implement committed_to_htlcs().
Simply count how many HTLCs are in our current funding state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:28 +10:30
Rusty Russell 9efdbbb21b peer: use funding.h's struct channel_htlc.
Instead of our own fields for the current htlc.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell fc4c94cb06 daemon: simple close support for the case of one side closing transaction.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 6ba5c3cc3b timeout: oneshot timer support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 9f560a9494 daemon: --closing-fee
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 6bdaa5d1ca daemon: newhtlc command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 645958920e peer: make_commit_txs() helper.
We need to call it in several places, so unify it into a single function.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 5acb3c9848 daemon: dev-rhash command.
Useful for creating SHA256(r) from scripts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 8f0e10309a daemon: --bitcoind-poll=<seconds>
Speeds up testing significantly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 6443629f6a daemon/test/test.sh: add --valgrind and --gdb options.
We add a second to the sleep, as valgrind can be a little slow.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell f5538bd1d2 daemon: test scripts.
We comment out the peer_create_close_tx() abort for now, so we
can test.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:27 +10:30
Rusty Russell 3c9fd4fbe6 daemon: code to open channel and watch anchor.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:45:05 +10:30
Rusty Russell ecbe671688 peer: keep current commit txs, anchor state, channel funding and their sig.
This lets us implement accept_pkt_anchor().

Also had to predeclare sha256 in commit_tx.h, revealed by the new
includes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:27 +10:30
Rusty Russell 871e4d5172 secrets: peer_sign_theircommit() helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:26 +10:30
Rusty Russell 90e381f750 bitcoind: try gettransaction as well as getrawtransaction.
Because with walletbroadcast=0, getrawtransaction doesn't see them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:26 +10:30
Rusty Russell ae04116883 daemon: send open_pkt on initialization.
This gets us to the creation of the anchor transaction, where we stop.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:25 +10:30
Rusty Russell 12b9d39b76 daemon: store revocation hashes in the peer_visible_state structure.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:24 +10:30
Rusty Russell abc002ff15 daemon: add state.c.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:44:13 +10:30
Rusty Russell a5e08e7f79 lightning-cli: give error message when lightningd produces non-object response.
Nicer than assert() failing without showing the response.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 0dbbd81430 bitcoind: tell bitcoind_poll_transactions's callback if tx is a coinbase.
In this case, the inputs aren't valid transactions, so don't try to
find them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 5b9f8d8bbd bitcoind: serialize requests.
bitcoind has a limit of 16 requests at once, by default, so our simplest
solution is to serialize them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 980d0d2bf1 bitcoind: warn if their bitcoin config not on testnet/regtest.
Otherwise you get weird errors as bitcoind refuses our testnet addresses.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 34e32978b8 bitcoind: warn if their bitcoin config doesn't have walletbroadcast=0.
Because we use the bitcoin wallet to create the anchor transaction, we
need to make sure it doesn't broadcast it; safest to check their config
for the option.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell c51a8d804f bitcoind: routine to send to a specific address.
We use this to create our anchor payment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell c0766061fa dns: don't allow non-stream sockets.
It was "connecting" to "localhost 30000" successfully, because it was
using UDP.  Oops.

This reveals another issue: init_conn() uses the wrong address to
connect to, as we'd already incremented the pointer in try_connect_one().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell d5328c7577 bitcoind: routine to broadcast a transaction.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell fc49e3fd74 daemon: rename 'state' to 'dstate' everywhere.
This is the daemon state, not the state machine state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell b04392609a daemon: encapsulate each side's state in a struct.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 0376e08fea daemon: peer needs to know who offered the anchor.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell e9237f94b1 daemon/watch: API to watch various bitcoin transactions.
This uses the functions in bitcoind to provide callbacks when various
things happen.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 5ec8ff91e4 daemon/bitcoind: communication with bitcoind.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 24602459b1 daemon/config: add testnet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell d303393d67 daemon/peer: save their commit key too.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell a5aa7b9abd daemon: config file support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell bf3080ca09 secrets: handle per-peer secrets as well.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 08ccb4b6f0 getpeers: new command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell d8959b3117 peer: make connect command an async command.
So it can return failure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 366f8a5f3f dns: add failure callback.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell d68ae0b612 jsonrpc: adapt it to be async.
This allows for JSON commands which aren't instantaneous.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 74f294e36c daemon: encrypted communication (version 3)
After useful feedback from Anthony Towns and Mats Jerratsch (of
thunder.network fame), this is the third version of inter-node crypto.

1) First, each side sends a 33-byte session pubkey.  This is a
   bitcoin-style compressed EC key, unique for each session.
  
2) ECDH is used to derive a shared secret.  From this we generate
   the following transmission encoding parameters for each side:
   Session AES-128 key: SHA256(shared-secret || my-sessionpubkey || 0)
   Session HMAC key: SHA256(shared-secret || my-sessionpubkey || 1)
   IV for AES: SHA256(shared-secret || my-sessionpubkey || 2)

3) All packets from then on are encrypted of form:
	/* HMAC, covering totlen and data */
	struct sha256 hmac;
	/* Total data transmitted (including this). */
	le64 totlen;
	/* Encrypted contents, rounded up to 16 byte boundary. */
	u8 data[];

4) The first packet is an Authenticate protobuf, containing this node's
   pubkey, and a bitcoin-style EC signature of the other side's session
   pubkey.

5) Unknown protobuf fields are handled in the protocol as follows
   (including in the initial Authenticate packet):

   1) Odd numbered fields are optional, and backwards compatible.
   2) Even numbered fields are required; abort if you get one.

Currently both sides just send an error packet "hello" after the
handshake, and make sure they receive the same.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 9449f387ac daemon: primitive privkey handling.
Eventually this will be in a separate process, etc.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 29c8611e53 daemon: add helper to log hex.
Good for keys; eventually we'll want to log structures by type though.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 14cbcd467f daemon: add global secp256k1 context for easy access.
This caches the tables, so you're not supposed to regenerate it all
the time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 2df28021ac daemon: command to connect
Now we can connect two daemons to each other.  Who both say Hello! and
close.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 39120b3ac6 daemon: async DNS lookups.
Also based on pettycoin code.

(With embarrassing bug fixed where it didn't increment the address used,
 thus using 100% CPU if that connect failed!)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell e4224f72d4 daemon: netaddr
Structure for a net address.  We can expand it later to cover exotic
address types (Tor?).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 469401610f daemon: socket code.
At the moment, if you connect it just says Hello! and closes the socket.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell c28a9ce6c7 daemon: lightning-cli
Minimal possible implementation; not real parsing or pretty printing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 9ccb51c93f daemon: UNIX domain socket for JSON-based control.
Also taken from pettycoin. 

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 6ccfcf4477 daemon: timeout structure for IO.
For better or worse, the ccan/timer structure is completely minimal,
and designed to be wrapped inside a container structure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell aed857a30c json: routine to create a uint64_t field.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 725512fb03 json: parse bitcoind-style bitcoin amount.
Always of form d*.dddddddd; we turn that into satoshis, because we're
sane.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 06a25887da json: routine to parse a uint64_t.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell ac4491909c daemon/json: helpers for using jsmn.
Also taken from pettycoin.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 0c81b0918a daemon/jsmn: build the module.
Can't build inside the submodule, as that makes it dirty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 378dadc371 daemon: configuration directory (default ~/.lightning).
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 7b766f4bc8 daemon: fatal() function.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 89f1f1548e daemon: dump logs on crash.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 117cf6b02d daemon: add logging options.
Particularly logging to file.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell 3e77c55a3b Makefile: add daemon source sanity checks.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell dd35260ce8 daemon: first code.
Logging code based heavily on pettycoin's.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:47 +10:30
Rusty Russell deb2e7b654 daemon/jsmn: Add submodule for jsmn.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:37:13 +10:30