Commit Graph

838 Commits

Author SHA1 Message Date
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 43c0cdb5e4 test: check overlapping HTLC fails.
This triggered an assert until the last patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 14:51:44 +09:30
Rusty Russell 74107ff1ea peer: resend failures on restart/reconnect.
If we've not relayed a failure yet (ie. we relayed it instantly, but it
wasn't confirmed), we need to redo it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 14:51:43 +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 169c6b53cb protobuf_convert: expose helpers for unwrapping protobufs into tal heirarchies
It's still ugly, but at least it's encapsulated.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:15:57 +09:30
Rusty Russell 192d5402ff peer: remove unused arg from our_htlc_failed().
And fix one case where we didn't set htlc r value (onchain redemption).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:14:57 +09:30
Rusty Russell 7e95637cb5 peer: on restart, resend any fulfilled HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:13:57 +09:30
Rusty Russell 1bd2a28caa peer: on restart, only resubmit added HTLCs which weren't already routed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:12:57 +09:30
Rusty Russell d964ad2d94 daemon: don't restart newhtlc/failhtlc/fulfill htlc commands on reconnect,
These low level commands we restarted on reconnect for ease of
testing.  Don't do that, and check that we're connected when those
commands occur.

This introduces subtle issues with --manual-commit --reconnect: restarting
node1 also forgets uncommitted things from node2, requiring reordering for
some tests.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:11:57 +09:30
Rusty Russell 92187ae5e7 test: print output of commands which fail.
We capture the output in case we need to resubmit the command after restarting,
but we weren't printing it out on failure (set -e means we'd stop immediately).

As a side-effect of this change, we don't restart after failed
commands, which caused another bug: we were writing the 2->3 route to
the config file, but not restarting again, so we lost the route.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:10:57 +09:30
Rusty Russell 7550ec23ec daemon: new command gethtlcs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-30 20:09:57 +09:30
Rusty Russell b0278d7152 peer: fix maximum htlc value when we disconnect.
If we send an HTLC #1, then get disconnected before a confirm, we will
forget it.  But we've incremented peer->htlc_id_counter, so when we offer
it again we'll make it HTLC #2, which is non-consecutive.

To make this clear, we always start htlc ids at 0 now.  That revealed
the bugs handled in the previous patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-28 12:51:57 +09:30
Rusty Russell 555a753564 db: don't assume HTLCs in order when reconstructing channel state.
We don't have an ordering of HTLCs between peers: we don't know
whether your HTLC 0 or my HTLC 0 occurred first.  This matters,
as we play them all back to reconstruct state (probably overkill anyway).

So we add force_* operators which don't do bounds checks, and do
bounds checks at the end.  We also note that we don't need to apply
fee changes: that should be in the database already.

Also relax db constraints: IDs are not unique, they are unique per
side (we can both have HTLC #0).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-28 09:34:47 +09:30
Rusty Russell b22bdfcbe8 test: use random port/rpcport for bitcoind.
This means we don't get confused if a testnet bitcoind already running.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-26 15:44:04 +09:30
Christian Decker 754616c45a routing: Fix for single hop routes segfaulting.
The first hop is being stripped from computed routes, however the
first channel of the route is being used to get our peer address. This
results in segfaults if the route is just one hop, i.e., has no first
channel to get the peer's address from. Fixed by simply using an
existing pointer to our peer.
2016-08-26 15:39:35 +09:30
Rusty Russell 72a9cfbbe2 test: test feerate changes.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-26 15:33:47 +09:30
Rusty Russell 1305df6417 daemon: send feechange requests.
This is a bit overzealous, but good for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-26 15:33:40 +09:30
Rusty Russell 31bdf384cb daemon: accept feechange packets.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-26 15:33:30 +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 2c7256ac69 protocol: add feechange packet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-25 11:39:46 +09:30
Rusty Russell c11c81a920 daemon: first unit test, infrastructure.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-25 11:39:46 +09:30
Rusty Russell 24298a4278 channel: approx_max_feerate and can_afford_feerate
Routines for getting maximum feerate we should offer, and checking if their
offer is valid.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-25 11:39:46 +09:30
Rusty Russell c7f8ce9d3b Merge pull request #32 from braydonf/master
README.md: include libmysql-dev
2016-08-19 11:50:41 +09:30
Braydon Fuller 607922f2b8 README.md: include libmysql-dev 2016-08-18 22:07:35 -04:00
Rusty Russell 8d2776376a 0.4: Wright's Cryptographic Proof
Christian Decker moved us to libbase58 and off openssl; he got to name
this release.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-19 11:13:10 +09:30
Rusty Russell f1c34c2ec5 INSTALL: update requirements.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-19 11:05:32 +09:30
Rusty Russell e40a070014 json_commit: document usage correctly.
It's pretty horrible to hand the entire tx, but at least document it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-19 10:34:12 +09:30
Rusty Russell 830a65de46 daemon: --add-route option.
This allows hardcoded routes in the config file, which is required until
we get route advertisements.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:14 +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 f68607d10b db: forget peer properly.
Otherwise, if they reconnect, we get a database error.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:14 +09:30
Rusty Russell 9f512f6540 peer: restart from the other side.
Testing this revealed that we can't just reconnect when we have something to
send, as we might be NATed; we should try to reconnect anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:14 +09:30
Rusty Russell 190b30e958 daemon: test restarting.
We add a "dev-restart" command which causes the daemon to close fds
and exec itself.  Then we do it after every command, with the caveat
that we always send a commit before newhtlc, because if not committed,
that is forgotten.  Fulfillhtlc and failhtlc get resent, since they're
idempotent.

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 71b8a07c56 db.c: database API.
These tables could use a rework, as they largely reflect our internal
state.  But it's a start.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +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 f4fe42e977 commit_tx: more debugging.
Sometimes you need a bigger hammer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 9b869a80a1 peer: set up peer->nc only when in state NORMAL.
This ensures we don't try to route before we're set up, or once we're
shutting down.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 87da9634bc routing: use ids, not nodes in interface.
Turns out to be more convenient.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:25:13 +09:30
Rusty Russell 8a3c9908ce protocol: no signature in update_commit if receiver has no outputs.
So if there are no HTLCs, and the receiver can't spend anyway, don't
sign.  This has the added benefit that no two signed commitment
transactions will ever be identical (the revocation preimage changes).

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 7275aa9c32 channel: use u64 for fee_rate everywhere.
It fits in a u32, but we mix it with other values which could cause
overflow, so let's just use u64 everywhere.

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 fd650ba79d protocol: rename clearing to shutdown.
As per lightning-rfc e277023be40f0dcc7ff7e818cef1e0d23547cb8c.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +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 9448358cfd chaintopology: wait for full blockchain load before start.
Caught because we generated an HTLCs which had already expired, since
we didn't know the latest block.  Other errors are certainly possible,
so it's safest to load the entire thing before going live.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 02cb7abd9d bitcoind: keep running fee estimate.
This avoids us having to query it when we create anchor transaction, and
lets us always use dynamic fee information.

The config options for max and min are now percentages, rather than absolute.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30