Commit Graph

110 Commits

Author SHA1 Message Date
Rusty Russell e5994ad8c1 json_connect: don't return until we're in state normal.
This gives much better errors, and allows us to return the peer id.

Closes: #37
Reported-by: Glenn Willen
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:01:18 +10:30
Rusty Russell 5bcc9047b0 db: save error, return it when we commit transaction.
This saves a lot of error handling, and puts it in the place we care about.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell 68632e6020 Use "msatoshi" not "msatoshis" everywhere.
Including in JSON API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell b47fbfead0 db: Always fail HTLC inside a transaction.
This is important when we put payments in the database: they need to be
updated atomically as the HTLC is.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-02 12:02:18 +09:30
Rusty Russell 23049f09a9 db: Always fulfill HTLC inside a transaction.
This is important when we put payments in the database: they need to be
updated atomically as the HTLC is.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-02 12:01:18 +09:30
Rusty Russell 1ed4dbde05 peer: add peer_fail helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-02 12:00:18 +09:30
Rusty Russell 42cf0ef543 peer: do logging before crypto is on.
We create a logging object when we connect, then carry it through.  If
it comes from the database, we just use the peerid as the log prefix.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:31 +09:30
Rusty Russell 012574790d pay: make interface idempotent.
We stopped automatically retransmitting locally-generated add/removes
after a reconnect, but this breaks the "pay" interface as it stands.

The correct solution to this is to make the pay interface idempotent:
you can trigger it as many times as you want and it will only succeed
once.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:04:59 +09:30
Rusty Russell d4ddebd55a htlc: save fail message in HTLC.
It's not currently encrypted, but at least you get some idea now why
an HTLC failed.  We (ab)use HTTP error codes for the moment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 14:51:41 +09:30
Rusty Russell e7b003b499 daemon: handle feechange requests.
I originally overloaded struct htlc for this, as they go through the
same states, but separating them turned out to be clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-26 15:31:19 +09:30
Rusty Russell 319c2ec5fc peer: keep addresses separately from peers.
This makes more sense eventually: we may know the network addresses of
many peers, not just those we're connecting to.  So keep a mapping, and
update it when we successfully connect outwards.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:14 +09:30
Rusty Russell 5f368f1c95 peer: save/load results in database.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:14 +09:30
Rusty Russell be38d3f507 Minor cleanups: things found while debugging the database code.
1. Fix #ifdef DEBUG code in signature.c so it compiles.
2. Don't set peer->closing.our_script in queue_pkt_close_shutdown: it's
   assigned in caller already.
3. Wrap setting of htlc's rval in set_htlc_rval() function.
4. Log where we were when unexpected packet comes in.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 0bb183e028 peer: split and expose new_peer function.
More of a pure allocator, for when we load peers from db.  Also moves
shachain_init out of secrets and into new_peer where it logically
belongs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell ab2fac3714 peer: add flag to indicate whether we created anchor.
Useful for database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 5f4b4525b3 peer: use signed values for order.
This gives us a clear way to indicate "invalid", and also sqlite3 stores
signed 64-bit numbers, so it's clearer this way.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 15e8bd5a45 peer: save minimum possible depth for anchor.
We'll save this in the database so we know where to start the chain
from when we reload.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 795601dfcf daemon: reconnect with timeout, try from both sides.
This is dumb, since one side will never succeed.  But in future when
there is a method for nodes to broadcast their public address (or send
their address inline to connected nodes), either side should try to
connect.

Importantly though, there are places which will queue packets at
various times (eg. HTLC timeout), so we need to clear the queue just
before re-transmitting, not when disconnecting.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 3866d7605c daemon: reconnect support.
To do this we keep an order counter so we know how to retransmit.  We
could simply keep old packets, but this is a little clearer for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:08 +09:30
Rusty Russell bb28bbd470 peer: always initialize commit_info commit number, other fields.
We used to use talz, but that prevents valgrind from noticing when we use
uninitialized fields.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell ab38fd7542 peer: rename closing_onchain to onchain.
The "closing" is implied.  Plus, it's too long.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell e19d5751fe peer: remove commit_info's prev pointer.
This is the final step before removing old commit_infos entirely.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 23f9c7c209 permute_tx: don't save permutation map.
We no longer need it anywhere.  This simplifies things to the point where
we might as well just not include dust outputs as we go, rather than
explicitly removing them, which gets rid of remove_dust.c as well.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell dca6c8efc1 peer: don't use permutation map for their unilateral spends.
Similar to the way we derive which outputs are which for old transactions
we steal, we derive them even for their current transaction.

We keep track of this information in peer->closing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 4319f3ac70 peer: explicitly store the previous revocation hash when sending new update.
We want to stop keeping old commitment information (except the minimal
txid to commitment-number mapping).  One place we currently use it is
after sending a commitment signature, and before we've received the
revocation for the old commitment.  For this duration, there are two
valid commitment transactions.

So we store "their_prev_revocation_hash" explicitly for this duration.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 2aaf0cb817 peer: remove unacked_changes and acked_changes queues.
These are now implied by the htlc state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 7709eb9b4a protocol: use separate ack packet.
It's a data-leak to send ack before we have verified identity of peer.
Plus, we can't send it until we know which peer it is, anyway!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell ec3344ce6e daemon/output_to_htlc: routines to map outputs for HTLCs for a given commit_num.
And use this to resolve old transactions by comparing outputs with
HTLCs.

Rather than remembering the output ordering for every one of their
previous commitment transactions, we just remember the commitment
number for each commitment txid, and when we see it, derive all the
HTLC scriptpubkeys and the to-us and to-them scriptpubkeys, and figure
out which is which.

This avoids us having to save information on disk, except for the
txid->commitment-number mapping (and the shachain).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 7c2165f5b4 peer: save txid -> commit_num mapping.
This is in preparation for placing it in a database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 9b2fd3a969 peer: record depth at which anchor tx is considered deep enough.
This makes it explicit, which is better for storing in a database (before
it was just what watch callback, plus peer->local.mindepth).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 08f7ade80f peer.c, packets.c: make more functions static.
This also has to re-order functions, so it looks worse than it is. 

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 6615db32c0 packets.c: queue_pkt_* only creates and sends packets.
Move other logic into caller: it grew this way because we used to have
a centralized "state" machine which knew nothing of these internal
details.  But now we want to re-queue packets on reconnect, we really
want these routines to be idempotent.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell abf4182ef5 peer: cache txid for commitment_tx.
Minor efficiency and simplification.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 1af3428c6c peer: keep a single HTLC map for all htlcs.
Not separate "locally-offered" and "remotely-offered" ones; we can
distinguish them by htlc->state now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 4b5ec85c25 daemon: keep enum htlc_state within struct htlc.
And update the state as HTLCs get moved around.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 22976bdd32 daemon: use HTLC states.
Since we only care about the latest commits, we can simply associate a
state with each HTLC, rather than using queues of HTLCs associated
with each commitment transaction.

This works far better in the context of a database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 2a03af4486 Misc minor cleanups.
From doing a code walkthrough with Christian Decker; unnecessary const in
bitcoin/tx.c, an erroneous FIXME, a missing comment, and an unused struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell a613d8d1fb peer: make id a pointer, NULL until we know peer's ID.
Much better than undefined, and testing for NULL is better than
testing for STATE_INIT.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fbe15bdce2 peer: remove unused struct members.
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 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 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 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 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 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