Commit Graph

614 Commits

Author SHA1 Message Date
Rusty Russell 7d1137c45e bitcoind_sendrawtx: tie the sending of the transaction to the particular peer.
Not important just yet, but it will be soon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-07 22:58:02 +10:30
Rusty Russell cc9ffe6c40 bitcoind: allow callbacks which are tied to an object.
We don't simply parent them on the object, we use a dummy object which tells
us not to call the callback if freed.

This would be better fixed by rewriting ccan/io to handle tal_free() of
a conn; then we could simply parent the entire thing and forget about it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-07 22:57:02 +10:30
Rusty Russell ef4f7c396b Merge remote-tracking branch 'origin/pr/72'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-07 22:56:02 +10:30
Glenn Willen 2b431b171b config: Allow overriding the transaction fee rate 2016-11-06 10:35:48 -08:00
Rusty Russell 04cff14ac8 peer: don't fret about fees too low on testnet.
Testnet fees are all over the place: don't close a connection due to
that.

Closes: #59
Reported-by: Thomas Daede <daede003@umn.edu>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-06 14:47:16 +10:30
Rusty Russell b49b90d5c3 peer: don't ever fail twice.
There are paths where this can happen (eg. db fail), but don't call
peer_breakdown() twice.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-06 14:46:16 +10:30
Rusty Russell 4855af8ba2 peer: don't worry about feechange if we can't do anything about it.
Triggering a commit will just do nothing anyway (same check at the
top of try_commit).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-06 14:45:16 +10:30
Rusty Russell ab11322b39 peer: don't close due to too low fees if we're already closed.
This fixes half of #59 (the part which crashes).  It doesn't fix the
fact that we should never be doing this for testnet.

Reported-by: Thomas Daede <daede003@umn.edu>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-06 14:44:16 +10:30
Rusty Russell 8949290794 Merge remote-tracking branch 'origin/pr/66'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-05 13:48:38 +10:30
Rusty Russell 5adaf46b19 Merge remote-tracking branch 'origin/pr/65'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-05 13:48:16 +10:30
Glenn Willen 2ce36a070b irc: Fix crash bug in node announcement parsing 2016-11-04 15:01:01 -07:00
Rusty Russell 25bb0f5248 peer: use correct enum in case statement.
These are the same, but we're using the ones from state.h instead of the
packet types directly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 12:12:38 +10:30
Rusty Russell feecabacce sphinx: fix marshalling/unmarshalling
Unfortunately, this fix will break compatibility.

Found by PVS Studio.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 12:09:31 +10:30
Rusty Russell d14a67addf db: fix leak for initial commit state.
It's only a single cstate, but it need not outlive the commit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:05 +10:30
Rusty Russell b40c4ae766 json_getroute: don't leak.
Allocate the route off the current command, not dstate.  And in the
case where the route is somehow not via a peer, don't leak memory.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell a902193874 sphinx: don't leak, especially on failed onion.
Generally, the pattern is: everything returned is allocated off the return
value, which is the only thing allocated off the context.  And it's always
freed.

Also, tal_free() returns NULL, so it's useful for one-line error
cleanups.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell de2ffd8985 accept_pkt_open_commit_sig: don't allocate sig.
Caller can allocate and free; this gets the context correct (that
commit).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell ac7d80bbeb peer: don't assign io_data and id twice when connecting out.
peer_first_connected does this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell 8a1559a343 peer: free init packet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell 6bda93f99a peer: don't leave initial cstate around.
Attach it to the commit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell 64809fb730 peer: free temporary transaction immediately
Don't leave it around until connect is finished.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell 8015ceadfd log: add netaddr
Also avoids allocating netaddr_name which otherwise lasts as long
as connection does.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell 4c7017f607 dns: simplify code by reaping dns child as soon as we have answers.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell e0368cc82d cryptopkt: don't leak negotiation state.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell 319eef266d commit_tx: free temporary wscript, rather than leaving attached to tx.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell b768de324c commit_tx: plug leak.
Use a tmpctx, and clean up afterwards.  Steal the script onto the tx,
however.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:16:14 +10:30
Rusty Russell 1d57fa60b8 invoice: print duplicate r value even if we choose it.
This shouldn't happen, but if our RNG is busted and get a duplicate,
r would be NULL.

(scan-build found this)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:58 +10:30
Rusty Russell 642a2f2d4e cryptopkt: free output packets after encryption.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:58 +10:30
Rusty Russell 95a01670ac cryptopkt: free incoming packet after decryption.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:57 +10:30
Rusty Russell 5f32d291bf jsonrpc: free old connections
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:57 +10:30
Rusty Russell a4ee683b3e tal_tmpctx: clear marker for temporary contexts.
This makes them stand out in memory dumps.  Also plug two existing
memory leaks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:27 +10:30
Rusty Russell 024e6a5855 feechange: fix db updates.
Found by PVS Studio.

Reported-by: Jon Griffiths <jgriffiths@blockstream.io>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:33:27 +10:30
Rusty Russell c1d6df58aa feechange: fix db updates.
Found by PVS Studio.

Reported-by: Jon Griffiths <jgriffiths@blockstream.io>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:33:27 +10:30
Rusty Russell 5349d2aaa7 routing: fix hash of pubkeys.
Found by PVS Studio.

Reported-by: Jon Griffiths <jgriffiths@blockstream.io>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:33:06 +10:30
Rusty Russell fa402aac31 routing: fix hash of pubkeys.
Found by PVS Studio.

Reported-by: Jon Griffiths <jgriffiths@blockstream.io>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:33:06 +10:30
Christian Decker f9a4af62e3 irc: Added alias handling for node announcements
Aliases seem to be popular among users wanting to show off their node,
so let's add them :-)
2016-10-28 16:47:01 +02:00
Christian Decker 94fd82dc81 irc: Announce the real expiry time
So far we've been announcing the locktime instead of the min_expiry the
node requires, this results in routes having wrong expiries.
2016-10-28 16:36:20 +02:00
Rusty Russell 48da22ab4b Merge remote-tracking branch 'origin/pr/56'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-25 11:23:34 +10:30
Rusty Russell dfad49bbd7 Merge remote-tracking branch 'origin/pr/55'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-25 11:19:15 +10:30
Rusty Russell 60e7e54f95 Merge remote-tracking branch 'origin/pr/53'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-25 11:16:52 +10:30
Christian Decker 3dd2e0c2b3 jsonrpc: Correctly handle numeric invoice labels
`listinvoice` was not handling numeric labels correctly if they are not
passed in as JSON strings (which was the behavior of
`lightning-cli`). So now we accept both string labels as well as numeric
labels.
2016-10-23 18:52:08 +02:00
Christian Decker de187ecaf7 jsonrpc: Accomodate some pedantic JSON-RPC impls
The JSON-RPC was non-standard compliant in that it omitted the required
`jsonrpc` entry and it was returning both `error` as well as
`result`. This fixes both of these issues.
2016-10-23 16:19:08 +02:00
Christian Decker a97d301155 routing: Refuse self-route
Fixes #50. Was causing a segfault because it was creating an empty route
and was trying to extract the first hop as next hop. Routes through self
can still be created manually, but `sendpay` would still refuse to act
on them due to the missing self-link.
2016-10-23 11:57:50 +02:00
Christian Decker 50807db7aa jsonrpc: Fixed `listinvoice` returning broken JSON
Closes #48.
2016-10-23 10:57:29 +02:00
Rusty Russell 144c40afd0 irc: get much less chatty.
We're going to wean off IRC, but as a quick fix, only announce 0-60 seconds
after we see a join, or every 6 hours.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-21 12:05:33 +10:30
Rusty Russell 7d20c4f75b feechange: disable sending of feechange messages.
We've seen intermittant failures on testnet, so disable sending feechanges
for now: we're completely changing it for 0.6 anyway, due to Milan Spec.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-19 12:00:27 +10:30
Rusty Russell af9ee44d52 sphinx: memset warning suppression workaround.
Milan summit finalized some changes, so this will be reworked
soon anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-17 12:44:09 +10:30
Rusty Russell 02cb651748 Merge remote-tracking branch 'origin/pr/46'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-17 12:37:20 +10:30
Rusty Russell d47d19ef2b Merge remote-tracking branch 'origin/pr/44'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-17 12:31:19 +10:30
Rusty Russell a9df84145e Merge remote-tracking branch 'origin/pr/43'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-17 12:20:03 +10:30
Christian Decker d30f3f1a40 sphinx: Remove obsolete onion implementation 2016-10-16 16:40:54 +02:00
Christian Decker 1d3737055a sphinx: Integrate sphinx OR with lightningd
Now replaces the old cleartext onion routing with the sphinx
implementation.
2016-10-16 16:40:50 +02:00
Christian Decker 7bb5c279a8 sphinx: Implemented sphinx onion routing
Implements a spec-compliant sphinx onion routing format. The format has
been cross-checked with the go implementation
cdecker/lightning-onion@b9e117e.
2016-10-16 16:40:43 +02:00
Rusty Russell 5797dc6496 init: add comments as per updated BOLT #2.
To match 8ad8041990dc "wire-protocol: rename reconnect_pkt to
init_pkt, add feature bits."

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 17:40:04 +10:30
Rusty Russell 5e078a8167 pkt_init: check feature bits.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 17:39:55 +10:30
Rusty Russell ec64e7756e protocol: pkt_reconnect becomes pkt_init, and always send.
Make this always the first packet after auth.  That means there's no
reliance on whether a node remembers an aborted connection.

It also gives us a place to put version bits.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 17:39:34 +10:30
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 d1c30c45ab getinfo: show block height.
My lightning node was *way* behind, and I couldn't figure out why it
wasn't seeing channel establishment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:01:18 +10:30
Rusty Russell f66358882a config: different defaults for mainnet and testnet.
In particular, remove fee ranges on testnet (too unreliable) and accept
a single confirm.

(Note that an earlier version of this had a bug when there was no
config file, this version includes the fix).

Closes: #40
Reported-by: Glenn Willen
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:18 +10:30
Rusty Russell c0d0606d66 lightningd: move testnet flag out of config.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:18 +10:30
Rusty Russell 1e6f7059d3 lightningd: allow --commit-fee-max=0 for "no limit"
For testnet, fee estimates are not reliable at all.

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell 44282eed16 test.sh: increase timeout, slow down bitcoind polling.
My temporary machine is incredibly slow.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell a1f1f1eda8 daemon: fix feechange logic.
Firstly, we need to update the staging fee amount when we queue a change.
Secondly we need to remove completed fee updates, otherwise we hit a
database constraint that peer & state are unique.

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell e2090f015d peer: don't spin when we want to commit and are not connected.
Instead, stop timer loop, and restart when we connect again.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell 25b3dc0038 channel: fix max feelevel calculation.
We were out by 1000, and also derived it from the previous, not current
state.

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell ab569c069f peer: add FIXME for the STATE_INIT case.
We store peers in the database for STATE_INIT, but they don't reconnect
properly.  We should not forget STATE_INIT dropped peers, but use some
timeout mechanism if we can't reconnect to clean up.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:17 +10:30
Rusty Russell fb99e0ccb0 peer: fail immediately if we detect double-connection.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell 0f6c387899 peer: delete from database if we haven't passed init.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell b45b4eaba6 bitcoind: explicit flag to bitcoin-cli for testnet/regtest.
Three days of on and off debugging, before I realized my server was talking
to a non-testnet bitcoind.  There was a bitcoind on that machine running
on testnet, but it uses the same dir and config, so the --bitcoin-datadir
option couldn't help.

This is more certain: specify whether we're testnet on every single query.
Now we can skip the attempt to parse bitcoin.conf, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell 8c8fa2cecd config: make sure that anchor-confirms is non-zero.
We don't actually look into mempool, so setting zero is misleading.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell a19762b55c open: increase verbosity of errors
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell 1f1bd56e2a peer: add more logging for anchor events.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell 0460f0caca getroute: fix help message to get arg order correct, include riskfactor.
Closes: #38
Reported-by: Glenn Willen
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 14:00:16 +10:30
Rusty Russell 7c9884d292 build: fix parallel initial build.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 13:58:57 +10:30
Rusty Russell 4fcdf6de5c irc: fix check-source.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 13:56:57 +10:30
Rusty Russell 93b72332bd Handle anchor in a run of blocks.
Fix dff50c3a5f2224592d135ad9cc6c11be89e26def: if multiple blocks come between
polling, we'll get called once for the new tip.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-10-07 13:53:18 +10:30
Christian Decker 594eb8109c jsonrpc: Added 'getnodes' to list known nodes.
getnodes returns an object containing a single array of 'nodes'. Each
element contains the node's ID, its hostname and its port. If
unknown (because we haven't seen a node announcement yet) then the port
is 0 and the hostname is null.
2016-10-02 14:52:24 +02:00
Christian Decker 2a5a114f27 irc: Handle node announcements
lightningd now uses a WHOIS query on itself to learn its external IP
address and announces that on the channel with the NODE message. It also
tracks other nodes in the routing table.

Refactored the signature verification to reuse it for both CHAN and NODE
messages.
2016-10-02 14:52:14 +02:00
Christian Decker b2126375e0 irc: Add contact information to nodes
The routing table now includes hostnames and ports for the node as well
as a helper to add/update the nodes we learn about.
2016-10-02 14:30:25 +02:00
Christian Decker 8e2abfcc70 jsonrpc: Added 'getchannels' RPC method.
'getchannels' returns a 'channels' array containing an object for each
known channel. Each channel object represents one direction of a
bidirectional channel, with a from and a to node ID along with the fees
for that direction. This matched the internal storage of channels and
allows unbalanced fees for each direction.
2016-10-02 12:22:28 +02:00
Christian Decker 356bb73fe9 log/jsonrpc: getlog was returning invalid JSON.
A mixup between object and array notation in the getlog response was
resulting in invalid JSON.
2016-09-19 14:10:44 +02:00
Rusty Russell 5b3bde715a getinfo: add version information
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-14 05:28:51 +09:30
Rusty Russell e5b2cacd3f daemon: fix up getinfo command, add it to tests.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-14 05:15:53 +09:30
Rusty Russell dff50c3a5f peer: do not send anchor depth input twice.
We *should* be in a state which accepts it (could happen with reorg),
and there's no reason to test for greater than depth since we must process
blocks in order.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-13 15:26:44 +09:30
Rusty Russell 7b5806fef2 peer: give more sensible error if anchor is too large.
Currently we get the odd message "Own anchor has insufficient funds".

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-13 12:40:32 +09:30
Rusty Russell 13593d4cbf peer: don't try to change fees while we're still setting up channel.
And add a FIXME about doing feechanges properly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-13 12:40:28 +09:30
Rusty Russell 221a96cdeb getinfo: new RPC command
Useful for getting ID, what port (if not set in config file).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-13 03:37:07 +09:30
Rusty Russell 831558d884 test.sh: remove scary-looking output from 'make check'
NO_VALGRIND= daemon/test/test.sh --normal --restart
lightning-cli: Connecting to 'lightning-rpc': Connection refused
lightning-cli: Connecting to 'lightning-rpc': Connection refused
lightning-cli: Connecting to 'lightning-rpc': Connection refused
lightning-cli: Connecting to 'lightning-rpc': Connection refused
lightning-cli: Connecting to 'lightning-rpc': Connection refused

This is expected: it happens when node3 is restarting.  Redirect
errors to /dev/null.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-08 15:56:33 +09:30
Rusty Russell 453c768b12 test: disable IRC connections for test.sh
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-08 15:47:05 +09:30
Christian Decker 2f6eefc49b routing: Wiring in the IRC peer discovery
Added a flag to disable the IRC discovery and calling the entrypoint to
start the IRC client loop.
2016-09-07 23:49:39 +02:00
Christian Decker f4568e5c1c routing: Added IRC announcement glue
Added channel announcement serialization and parsing, as well as the
entrypoints for the IRC peer discovery. Announcements are signed by the
sending endpoint and signatures are verified before adding the channels
to the local view of the topology. We do not yet verify the existence of
the anchor transaction.
2016-09-07 23:49:39 +02:00
Rusty Russell fce9ee29e3 db: always call db_update_our_closing in a transaction.
It's not in a transaction in one caller, so wrap that.
This removes some more error handling code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09: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 ea41386ed3 daemon: rename all the low-level commands to dev-
Rename the structs to match (and remove dev-echo).

This makes it clear that they're not the normal API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell c83fb1a2dd waitinvoice: RPC call for processing incoming invoices.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell 27715f7732 invoice: order by when they were paid.
We need some ordering to deliver them to the JSON "waitinvoice" command;
we use a counter where 0 means "unpaid".

We keep two lists now, one for unpaid and one for paid invoices.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell 0761c12381 delinvoice: routine to remove an unpaid invoice.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell f6a0ea0b66 listinvoices: RPC command to show one or all invoices.
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 060ff29b45 Rename payment to invoice.
And rename JSON's accept-payment command to invoice.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:49 +09:30
Rusty Russell ca80fc0286 getroute: add a risk factor.
We need some way to reflect the tradeoff between the possible delay if
a payment gets stuck, and the fees charged by nodes.  This adds a risk
factor which reflects the probability that a node goes down, and the
cost associated with losing access to our funds for a given time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 16:47:48 +09:30
Rusty Russell ab125f709b db: save and restore accepted payments.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 13:47:33 +09:30
Rusty Russell 79b5663db7 routing: don't coredump on routes to neighbors.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 13:47:33 +09:30
Rusty Russell fcdb13093e db: use wrappers around boolean values.
TRUE and FALSE according to sql2003 standard, 1 and 0 according to sqlite3.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 13:47:33 +09:30
Rusty Russell 9d9ff00508 db: use macros to create tables.
I had some nonsensical columns, eg "bool ours", but sqlite3 pretty much
ignores them.  Use macros so mistakes are harder to make.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-06 13:47:33 +09:30
Rusty Russell 04a07fd90e db: save and restore "sendpay" commands.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-05 13:29:48 +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 6fe5ecb2ac update-mocks: make mocks compile with -Wunused
Do this by scattering UNNEEDED everywhere, except in the case of (void).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-09-01 11:50:32 +09:30
Rusty Russell 15b42b89db peer: fix comparison bug if our connection_fee is negative.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 17:19:41 +09:30
Rusty Russell a4346b56f6 json: remove unused json_get_arr parameter.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 17:19:40 +09:30
Rusty Russell 7bebfe265c pay: remove route when a payment fails partway.
It's a bit harsh, but I'm assuming they'll get refreshed eventually.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:10:17 +09:30
Rusty Russell d8af789bbb channel: remove enum channel_side, rename htlc_side to side.
We had enum channel_side (OURS, THEIRS) for which end of a channel we
had, and htlc_side (LOCAL, REMOTE) for who proposed the HTLC.

Combine these both into simply "enum side".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell f90fb4934f close_shutdown: make sure script_pubkey is standard.
As per BOLT update 9c3f150d2a44af6ee2c3be03acd6ef80ea184f4e.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell 2804a4de7a test: test case where we can't afford fees.
In particular, make sure B can just afford it, then have the A add a
HTLC which means B can no longer afford the fees, and A should cover
it.

We do this by modifying the previous overlapping-fail test, but we
need to have B offer it the htlc before A does: racy in the normal
autocommit case.  So we do a manual commit here, always.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell f8fa8c8dff peer: handle case correctly where they can't handle fees.
When they propose an HTLC to us, they need to be able to cover both it,
and the associated fees.  When it gets acked and applied to them, however,
they may no longer be able to afford the fees; this is OK and expected.

So add a flag to say whether they can dig into fees or not: without
this patch the code calls fatal() on the next patch which tests it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell 1518f4424e log: add channel_state logging ability.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell 89235e0904 handle_pkt_commit: don't abort on database error.
Simply mark an error with this peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell bb0bf2b5ad log: add easy way to log hex blobs.
Use it in one case, and also fix another logging FIXME.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell 87af51a422 daemon: remove three fixed FIXMEs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:31 +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 9bd6a10f49 peer: fix close before their sig has been received in corner case.
If we haven't received their closing signature yet, we might try to
send the closing packet anyway (and segfault).  Make sure we have
their signature before trying that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:31 +09:30
Rusty Russell 2610799bda pay: split into getroute and sendpay
This is less convenient to use, but makes far more sense for a real
user (like a wallet).  It can ask about the route, then decide whether
to use it or not.

This will make even more sense once we add a parameter to control how
long we let the HTLC be delayed for, so a client can query for high,
medium and low tolerances and compare results.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:08 +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 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 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 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