Commit Graph

1391 Commits

Author SHA1 Message Date
practicalswift 7e9750ffee Reduce variable scopes 2018-03-26 01:31:21 +00:00
practicalswift 94ca824cf5 Use correct format strings for signed integers 2018-03-26 01:20:22 +00:00
ZmnSCPxj 74f3662a3b lightningd/subd.h: Add missing wire/wire.h.
If not included, a source file containing only
`#include<lightningd/subd.h>` will file compilation.
2018-03-26 01:09:59 +00:00
ZmnSCPxj 0bb9bcc0f1 wallet: Track some channel usage statistics.
Fixes: #1049
2018-03-26 01:08:52 +00:00
conanoc e7c8b95be3 Fix include order 2018-03-26 00:30:39 +00:00
conanoc c4700a13a7 Add guard for BACKTRACE_SUPPORTED 2018-03-26 00:30:39 +00:00
Rusty Russell 9f7d4312ff bolt11: undo json encoding for description bytes.
We don't handle \u, since we assume everyone sane is using UTF-8.  We'd
still have to reject '\u0000' and maybe other weird cases if we did.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-26 00:20:53 +00:00
Rusty Russell 54431d2b08 lightningd: escape our own alias when we print it in logs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-26 00:20:53 +00:00
Rusty Russell 4139f06780 json_getnodes: escape the alias when we print it out.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-26 00:20:53 +00:00
Rusty Russell 76e8a11380 wallet: use json_escaped for invoice label.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-26 00:20:53 +00:00
Rusty Russell d92579f627 common/json_escaped: new type which explicitly notes a string is already JSON.
Trivial to use as a string, but it still means you should be careful
around it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-26 00:20:53 +00:00
Christian Decker 0a5ea76d77 gossip: Add message types to store gossip msgs and associate data
Since we may want to extend the on-disk format by adding custom information we
may as well just go the extra mile and reuse the serialization primitives we
already have.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-25 23:56:59 +00:00
practicalswift a4059ef83e Use expected LIGHTNING_DIR_FILE_H define 2018-03-25 23:54:21 +00:00
practicalswift 98f49c0837 Remove include in file foo.c that is already included in foo.h 2018-03-25 23:54:21 +00:00
Igor Cota 8c00e4f98d Add --bitcoin-rpcport option to pass to bitcoin-rpc 2018-03-25 23:17:36 +02:00
practicalswift 0bf1b01425 Fix typos 2018-03-25 15:53:01 +02:00
Mark Beckwith 489d8d04a6 Fund all (#1249)
* Funding channel with "all"
* Added test for fund all
2018-03-21 00:06:04 +01:00
ZmnSCPxj 5737853123 options: Add --autocleaninvoice-* options. 2018-03-20 17:25:51 +01:00
ZmnSCPxj 159e736fc1 invoices: Implement autocleaninvoice. 2018-03-20 17:25:51 +01:00
ZmnSCPxj 3b0081aebb invoices: Add delexpiredinvoice command. 2018-03-20 17:25:51 +01:00
Rusty Russell 8966a175ee chaintopology: add debug message when we're adjusting due to feerate floor.
But only if we're actually going to change the feerate, otherwise we'd
log every time.

Suggested-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-20 16:08:45 +01:00
Rusty Russell 2e687b9b35 chaintopology: set a fee floor to avoid creating unrelayable txs.
Naively, this would be 250 satoshi per sipa, but it's not since bitcoind's
fee calculation was not rewritten to deal with weight, but instead bolted
on using vbytes.

The resulting calculations made me cry; I dried my tears on the thorns
of BUILD_ASSERT (I know that makes no sense, but bear with me here as I'm
trying not to swear at my bitcoind colleagues right now).

Fixes: #1194
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-20 16:08:45 +01:00
ZmnSCPxj, ZmnSCPxj jxPCSmnZ 044705a547 peer_control: Fix a use-after-free bug. (#1237)
This bug is a classic case of being lazy:
1. peer_accept_channel() allocated its return off the input message,
   rather than taking an explicit allocation context.  This concealed the
   lifetime nature of the return.
2. The context for sanitize_error was the error itself, rather than the
   more obvious tmpctx (connect_failed does not take).

The global tmpctx removes the "efficiency" excuse for grabbing a random
object to use as context, and is also nice and explicit.

All-the-hard-work-by: @ZmnSCPxj
2018-03-19 09:27:09 +00:00
practicalswift e56eee50c8 Make sure we never pass a negative value to dup2(...) 2018-03-19 09:25:39 +00:00
ZmnSCPxj 9efe123a0d lightningd/json: Move json helpers specific for lightningd to new module. 2018-03-19 00:27:55 +00:00
ZmnSCPxj 881eb80f6a payalgo: Make 'pay' return similar to 'listpayments' 2018-03-19 00:27:55 +00:00
ZmnSCPxj 3c39bcc0f7 pay: Make json_add_payment_fields accessible to other parts. 2018-03-19 00:27:55 +00:00
ZmnSCPxj d181ecbeea payalgo: Implement retry_for for pay command. 2018-03-19 00:27:55 +00:00
ZmnSCPxj 981af478f1 payalgo: Keep and report route and route failures. 2018-03-19 00:27:55 +00:00
ZmnSCPxj 234d67da34 channel_fail_permanent: Use a channel-level error, not an all-channels error.
Fixes: #1229
2018-03-17 13:26:58 +01:00
ZmnSCPxj d49915cdd2 peer_control: Have listpeers also return channel ID. 2018-03-17 13:26:58 +01:00
ZmnSCPxj 099acb2a66 opening_control: have fundchannel also return channel_id.
For voyeurism.
2018-03-17 13:26:58 +01:00
Rusty Russell 4ffd5e213b pay: work around bug #1212 for existing databases.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 04:01:06 +00:00
Rusty Russell 1b9791f0f5 pay: delete HTLC when we delete payment.
This fixes the root cause of https://github.com/ElementsProject/lightning/issues/1212
where we deleted the payment because we wanted to retry, then retry failed
so we had an (old) HTLC without a matching payment.  We then fed that
HTLC to onchaind, which tells us it's missing, and we try to fail the
payment and deref a NULL pointer.

Fixes: #1212
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 04:01:06 +00:00
ZmnSCPxj 7bbc1948f7 chaintopology: Add missing `block_map_del`.
We would `block_map_add` inside `add_tip`, but we never
`block_map_del` inside `remove_tip`, which is dangerous as
we actually `tal_free` the block inside `remove_tip`.

Our CI did not reliably trap this problem since block
hashes are random and rerunning the `test_blockchaintrack`
often passed spuriously.
2018-03-16 01:45:08 +00:00
ZmnSCPxj 022cfccddd pay: sendpay and waitsendpay print similar to listpayments 2018-03-16 01:24:43 +00:00
ZmnSCPxj fa281d32ea pay: sendpay_result has payment on success or in-progress. 2018-03-16 01:24:43 +00:00
practicalswift f9bd43f266 Limit scope of variable failcode. Remove dead code. 2018-03-16 01:17:40 +00:00
practicalswift b557e7b0fc Fix misleading indentation (space before tab) 2018-03-16 01:17:40 +00:00
practicalswift 7bd1ae6b2e Fix misleading indentation (in the case of DEVELOPER) 2018-03-16 01:17:40 +00:00
practicalswift b206aed371 Remove redundant cast to same type 2018-03-16 01:17:40 +00:00
practicalswift 03e2e404f2 Mark unused parameters as such (using "UNUSED") 2018-03-16 01:17:40 +00:00
Rusty Russell e63b7bb539 take: allocate temporary variables off NULL.
If we're going to simply take() a pointer, don't allocate it off a random
object.  Using NULL makes our intent clear, particularly with allocating
packets we're going to take() onto a queue.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 00:16:10 +00:00
Rusty Russell 0a6e3d1e13 utils: remove tal_tmpctx altogether, use global.
In particular, we now only free tmpctx at the end of main().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 00:16:10 +00:00
Rusty Russell de5d84097e lightningd: remove ltmp context now we have tmpctx.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 00:16:10 +00:00
Rusty Russell ef2a063169 utils: add a global tmpctx.
I did a brief audit of tmpctx uses, and we do leak them in various
corner cases.  Fortunely, all our daemons are based on some kind of
I/O loop, so it's fairly easy to clean a global tmpctx at that point.

This makes things a bit neater, and slightly more efficient, but also
clearer: I avoided creating a tmpctx in a few places because I didn't
want to add another allocation.  With that penalty removed, I can use
it more freely and hopefully write clearer code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 00:16:10 +00:00
Rusty Russell 41ef42ee94 memdump: fix overzealous assertions from b857b2e843
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-16 00:16:10 +00:00
ZmnSCPxj a0c2686ebd pay: Have sendpay wait for payment to be saved.
The payment should be stored in a "timely" manner, i.e.
within 10ms.
2018-03-14 05:33:09 +00:00
ZmnSCPxj a7a18b96cf pay: Rename some sendpay functions and fields to waitsendpay
Nearer to their actual purposes at this point.  Also in preparation
for next change.
2018-03-14 05:33:09 +00:00
ZmnSCPxj 61882ed9df pay: Add hook for triggering storage of payments. 2018-03-14 05:33:09 +00:00
ZmnSCPxj 24ee5d0437 pay: Store payment on failure. 2018-03-14 05:33:09 +00:00
ZmnSCPxj 411c9583a7 pay: Save and recover failure information.
Needed for particular race condition: client calls `sendpay` with
intent to call `waitsendpay` later to get information, but the
payment fails after `sendpay` returns but before client can invoke
`waitsendpay`.
This lets client know of information even if it manages to invoke
`waitsendpay` "late".
2018-03-14 05:33:09 +00:00
ZmnSCPxj 1e4adb0359 pay: Make sendpay nonblocking. 2018-03-14 05:33:09 +00:00
ZmnSCPxj bb4661f008 pay: Factor out construction of sendpay_result 2018-03-14 05:33:09 +00:00
Rusty Russell 46cc7c281e features: more general accessor functions.
As we add more features, the current code is insufficient.

1. Keep an array of single feature bits, for easy switching on and off.
2. Create feature_offered() which checks for both compulsory and optional
   variants.
3. Invert requires_unsupported_features() and unsupported_features()
   which tend to be double-negative, all_supported_features() and
   features_supported().
4. Move single feature definition from wire/peer_wire.h to common/features.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-14 02:19:37 +00:00
Christian Decker 74a444eb7a jsonrpc: Return the channel capacity for listchannels
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-12 22:34:51 +00:00
Christian Decker 5690783148 gossip: Pass output value to gossipd
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-12 22:34:51 +00:00
Christian Decker a8e553098a gossip: Pass output value to gossipd
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-12 22:34:51 +00:00
Christian Decker cb4ba9bf4f gossip: Use the utxoset to shortcircuit the txout lookup
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-12 22:34:51 +00:00
ZmnSCPxj 1de124277b peer_control: Print both `to_self_delay` in listpeers. 2018-03-12 14:54:29 +01:00
John Barboza b4f7ce61b4 Append to crash log instead of creating new one
Multiple crashes accumulate in case you have some kind
of auto-restart for daemons.
2018-03-11 14:30:34 +01:00
Isidoro Ghezzi f24a7b9356 help: listnodes and listchannels,have optional arg 2018-03-11 14:27:09 +01:00
practicalswift 161ed320f8 Improve onboarding experience by handling common failure scenarios for new users more gracefully
Improve usability in these scenarios:
* bitcoin-cli not available in PATH and/or bitcoind not running
* bitcoin-cli available in PATH but bitcoind is not running
2018-03-08 23:51:45 +00:00
Rusty Russell 6c1233de44 channel: reserve a bip32 index as soon as channel is opened.
This simplifies things, and means it's always in the database.  Our
previous approach to creating it on the fly had holes when it was
created for onchaind, causing us to use another every time we
restarted.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-07 18:55:51 +01:00
Rusty Russell e76f863329 txfilter: clean up prototypes.
Use const, add TAKES to declaration.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-07 18:55:51 +01:00
Rusty Russell c9e271df90 pay: wrap missing route_channels handling in COMPAT_V052.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-07 18:55:51 +01:00
Rusty Russell 9d9c1c3db6 wallet: wrap missing last_processed_block handling in COMPAT_V052.
Our testing also reveals a bug: we start lightningd and shut it down
before fully processing the blockchain, so we don't set
last_processed_block.  Fix that by setting it immediately once we have
a block: worst case it goes backwards a little.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-07 18:55:51 +01:00
Rusty Russell 5d5c1a5da5 lightningd: don't discard const in get_chainparams().
In general, it is true that accessors should take const and discard it,
but chainparams is *always* const.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-07 18:55:51 +01:00
John Barboza 30b290cb8f Explicit error message when disconnect fails
Whether the peer is not connected or not gossiping.
2018-03-07 16:14:01 +01:00
John Barboza 0a1351fd51 gossipd: command to disconnect from a gossiping peer
lightning-cli disconnect <peer id>
2018-03-07 16:14:01 +01:00
Corné Plooy b2ce619bcd Fix order of includes 2018-03-06 19:26:21 +01:00
Corné Plooy 71c39e96f3 Instead of passing the line number behind the '\0' of an artificial command-line argument, store it in a global variable.
While it is still a bit of a hack, this makes the code much easier to read.
2018-03-06 19:26:21 +01:00
Corné Plooy b857b2e843 Add assertions in various places to ensure tal_fmt doesn't receive NULL as argument for strings. 2018-03-06 19:26:21 +01:00
Christian Decker e44ea0b363 topology: Add new P2WSH outpoints to the wallet utxoset
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
Christian Decker ae30942e3f chaintopo: Record outpoint spends for owned outputs
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
Christian Decker 2680e6d9ff wallet: Move txfilter into wallet
Transaction filters are strongly related to the wallet, this move just
makes it a bit more explicit.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
Christian Decker 5fd19483a9 txfilter: Add an outpoint filter
This can be used both for our own outputs as well as the utxos we are
tracking.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
Christian Decker 227dc36146 utxo: Add blockheight and spendheight to outputs to track state
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
Christian Decker 1aa21498c3 topology: Track blockchain changes in the DB
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-06 18:59:27 +01:00
ZmnSCPxj 8e8d7c2aba pay/sendpay: Use spec names for rhash and r. 2018-03-05 20:21:37 +00:00
John Barboza 31b9b6bc1c clear onchaind subd in channel on error
When the subdaemon closes unexpectedly, clear the reference and
update the channel billboard with the description
2018-03-04 16:29:44 +01:00
ZmnSCPxj 1f6008689d payalgo: Throttle pay command if failure is due to blockheight disagreement. 2018-03-03 18:19:53 +01:00
John Barboza b50912d8b1 Print node alias when listing peers 2018-03-02 02:25:30 +00:00
Rusty Russell c5d41a23d7 short_channel_id: just use structeq.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-01 23:33:56 +01:00
Rusty Russell 042d5d13f5 short_channel_id: don't use bitfields.
I leave all the now-unnecessary accessors in place to avoid churn, but
the use of bitfields has been more pain than help.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-03-01 23:33:56 +01:00
Igor Cota cb6820d445 Do preimage, hash and consequentially bolt11 generation before we store the invoice in db.
This way we store bolt11 to the table as well, in case a client needs it later
2018-03-01 03:55:48 +00:00
Christian Decker a90502d917 jsonrpc: Refuse to forget a channel with an open HTLC
... instead provide useful suggestions as to how to close it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-01 03:50:50 +00:00
Christian Decker aef5780f36 htlc: Extracted htlc detection from the channel destructor
Let's have a simple function that allows us to check whether a channel
still has an HTLC open.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-03-01 03:50:50 +00:00
Corné Plooy f32ebb7dca Indentation fix. 2018-02-28 21:21:25 +01:00
Corné Plooy 1e879de89e json-rpc: connection_complete_ok and json_command_malformed should never receive NULL arguments. Pass "null" instead, where needed. 2018-02-28 21:21:25 +01:00
Corné Plooy d86ad15b53 json-rpc: translate NULL into "null" instead of "(null)". 2018-02-28 21:21:25 +01:00
practicalswift dbb7ea1e41 Adjust log level for logging disconnected RPC users ("Command failed after jcon close")
This is a follow-up to #1121
2018-02-28 11:38:22 +01:00
ZmnSCPxj 978e5c67d8 invoices: Remove persistent in-memory invoice structures. 2018-02-28 11:17:08 +01:00
ZmnSCPxj f05c86618c invoices: Change iteration interface to be more abstract. 2018-02-28 11:17:08 +01:00
ZmnSCPxj 9b4c6699f9 invoices: Semantically separate invoice details from invoice.
In preparation for removing in-memory invoice structures.
Invoice details are requested rarely anyway.
2018-02-28 11:17:08 +01:00
practicalswift a20cf5463d Adjust log level for logging disconnected RPC users ("Abandoning command" + "Command returned result after jcon close")
The following command can be used to trigger these messages:

```
$ timeout 0.01 cli/lightning-cli connect [insert-syntactically-valid-peer-id-here] 123.123.123.123 # where 123.123.123.123 is unreachable
```
2018-02-28 01:53:52 +00:00
ZmnSCPxj 73cda2f2ae payalgo: Report route, and result of trying route. 2018-02-27 13:38:32 +01:00
ZmnSCPxj 21cfec816d payalgo: Report number of tries on failure. 2018-02-27 13:38:32 +01:00
ZmnSCPxj 6c9d81ef42 payalgo: Remove reporting PAY_TRY_ANOTHER_ROUTE and PAY_UNPARSEABLE_ONION.
These error codes will cause `pay` to retry, so `pay` will never
actually report those error codes.
Those error codes will only get reported at the `sendpay` level.
2018-02-27 13:38:32 +01:00
Rusty Russell b7ed5670d5 lightningd: close and reopen db across fork for daemonize
Fixes: #1092
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-27 00:09:34 +01:00
Rusty Russell cf3f19524e gossip: formalize passing of siphash_seed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-26 06:35:02 +00:00
ZmnSCPxj f7f55edcdb wireaddr: Do hostname resolution in parse_wireaddr.
Fixes: #834
2018-02-26 03:13:55 +00:00
ZmnSCPxj bd07a91782 jsonrpc: Let Base58 '2..' address work on both regtest and testnet. 2018-02-26 03:09:15 +00:00
John Barboza b028a0a439 invoice: add fallback address to invoice command
* Modifies invoice command to have the following format
  invoice <msatoshi> <label> <desc> <?expiry> <?fallbackaddr>
* Adds support for Segwit bcrt1 addresses for withdraw
* Add test case for fallback address in invoice creation
* Create a common json_tok_address_scriptpubkey to be used
  by invoice and withdraw commands.
2018-02-26 03:09:15 +00:00
ZmnSCPxj d23650d2ed Explain fuzz initial values. 2018-02-26 02:36:27 +00:00
ZmnSCPxj 2db6ff9186 gossip_control: Change defult fuzz of getroute to 75%. 2018-02-26 02:36:27 +00:00
ZmnSCPxj 6767434ea9 routing: Use siphash24 for route randomization
Primary idea by @rustyrussell
2018-02-26 02:36:27 +00:00
ZmnSCPxj 61d0b50c54 payalgo: Randomize paths as long as we respect maxfeepercent. 2018-02-26 02:36:27 +00:00
ZmnSCPxj 9693843141 payalgo: Change sendpay_parent to try_parent.
Now try_parent is used as the parent for all allocations needed
  for a try.
2018-02-26 02:36:27 +00:00
ZmnSCPxj 216c52940d gossipd: Add fuzz and seed to getroute request. 2018-02-26 02:36:27 +00:00
ZmnSCPxj 2b782ee436 gossip_control: Add fuzz and seed parameters.
In preparation for improved route randomization.
2018-02-26 02:36:27 +00:00
ZmnSCPxj e34be575aa lightning/peer_control.h: Minor cleanup of nonexistent functions. 2018-02-25 20:41:50 +00:00
Rusty Russell 50171d3e1a lightningd: add --bitcoin-cli arg for testing.
And remove unused bitcoin_datadir and BITCOIN_CLI.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-24 10:37:28 +01:00
Rusty Russell f0a12c5c23 bitcoind: retry after one second if a call fails.
There are two recurring calls: the estimatefee call and the
getblockcount call.  Currently we simply discard them on error, the
timer isn't rearmed.

This should fix a number of cases where bitcoind has an intermittant
failure and lightningd simply stops collecting blocks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-24 10:37:28 +01:00
Rusty Russell 0204c44243 bitcoind: allow processing callbacks to indicate that a failure is spurious.
In particular, process_getblockhash() exits with status 8 when the block
number is out of range, which is expected.  Any other exit status should
be treated as a spurious error.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-24 10:37:28 +01:00
Rusty Russell cccdb53bec channel_states: fold all the ONCHAIND states into one.
The billboard is now far more useful to tell what's going on, and this
gets us closer to a state == owner mapping.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Rusty Russell 8aa7a48138 listpeers: display billboard as 'status' array.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Rusty Russell cca0a5412e subd: clear transient billboard on start and shutdown.
Use NULL on the callback to mean "clear the slot", and call it.

We have do this in two places: the old daemon might die, or the new
daemon might start first.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Rusty Russell 26b004e5af subd: handle status_peer_billboard messages from subdaemons.
We use a callback which updates the appropriate slot.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Rusty Russell db4de95033 lightningd: single transient billboard for opening peers.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Rusty Russell 86a04c59d4 lightningd: maintain a status billboard for each channel.
Each state (effectively, each daemon) has two slots: a permanent slot
if something permanent happens (usually, a failure), and a transient
slot which summarizes what's happening right now.

Uncommitted channels only have a transient slot, by their very nature.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-23 18:02:00 +01:00
Christian Decker 6f6176a91e options: Add `--offline` to disable listening and auto-reconnection 2018-02-23 06:30:15 +00:00
Rusty Russell 85f1a9b825 lightningd: append (as much as we can) version into default name if DEVELOPER=1
This helps for bug reporting, where we may need to diagnose the other
end.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-22 12:24:26 +01:00
practicalswift 1b51b5ae0f Remove unused parameter topo in txw_fire(struct chain_topology *topo, ...) 2018-02-22 10:46:30 +01:00
practicalswift 0046ace318 Remove unused parameter topo in txowatch_fire(struct chain_topology *topo, ...) 2018-02-22 10:46:30 +01:00
practicalswift 74841ef567 Remove unused parameter payment_hash in forward_htlc(..., const struct sha256 *payment_hash, ...) 2018-02-22 10:46:30 +01:00
practicalswift d873bf60bf Remove unused parameter fieldname in json_add_log(..., const char *fieldname, ...) 2018-02-22 10:46:30 +01:00
practicalswift 2ab274595e Remove unused parameter ptr in corrupt(const void *ptr, ...) 2018-02-22 10:46:30 +01:00
practicalswift f24eae60b7 Remove unused parameter why in delete_channel(..., const char *why) 2018-02-22 10:46:30 +01:00
practicalswift f15868a94d Remove unused function json_dev_broadcast(...) 2018-02-22 09:45:22 +00:00
Rusty Russell b536e97df2 dev_ping: don't crash with silly values.
It's a dev command, but still.

Fixes: #985
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-22 01:10:09 +00:00
practicalswift 91a9c2923f Mark intentionally unused parameters as such (with "UNUSED") 2018-02-22 01:09:12 +00:00
practicalswift 3dbace3421 Remove redundant casts to same type 2018-02-21 13:07:40 +01:00
Rusty Russell 369d3ab46e fixup! Close memory leak, make error exposure DEVELOPER=1 only. 2018-02-21 12:59:26 +01:00
Rusty Russell 9842e32e06 lightningd: expose internal error information.
In #1018 we got no information, except "Internal error".  At least
if we tell the other side what went wrong, we're more likely to get
an answer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-21 12:59:26 +01:00
Rusty Russell 1192f16733 lightningd: fix crash when htlc failed and source is onchain.
If the source channel is onchain, we try to send a message to onchaind
which (1) doesn't care, (2) doesn't take a channel_fail_htlc msg, and
(3) causes us to crash in subd.c:

	assert(!strstarts(sd->msgname(fromwire_peektype(msg_out)), "INVALID"));

Fixes: #821
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:58:48 +01:00
Rusty Russell e92b710406 tools/generate-wire.py: remove length argument from fromwire_ routines.
We always hand in "NULL" (which means use tal_len on the msg), except
for two places which do that manually for no good reason.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell 719290a4c4 txwatch: remove unused callback arg, hide struct definitions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell ae8fb96d06 lightningd/channel_control: routines to control channeld (move from peer_control.c)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell 36652a3df3 lightningd/peer_control: reorder, move more functions out.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell 372040bbd5 lightningd/onchain_control: routines to control onchaind (move from peer_control.c)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell 9176ee628c lightningd/closing_control: routines to control closingd (move from peer_control.c)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell a83ff83328 lightningd/connect_control: routines to control connecting (move from peer_control.c)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell 4cf274b1c0 lightningd/opening_control: routines to control openingd (move from peer_control.c)
We also fold opening_got_hsm_funding_sig() into the caller; it was
previously a callback before we decided to always use the HSM
synchronously.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 22:36:21 +01:00
Rusty Russell eca55cee3c subd: handle stdin being closed (eg. --daemon).
We need to do a more complex dance if stdin was important.

Fixes: #1016
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 21:42:53 +01:00
practicalswift 6bacab5e87 Fix typos 2018-02-20 13:05:51 +01:00
Rusty Russell 4aeebe88f2 log: fix ltmp crash.
When we clear and recreate ltmp, we attach it to whatever logbook it's on.
This, of course, is fraught, since it may be freed.

We could make it NULL-parented, but that makes YA special-case to free
when we exit (we try to keep valgrind happy by freeing everything).  So
since the first log_book is the permanent one attached to lightningd,
just keep that parent when we re-build it after use.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 12:58:13 +01:00
libbitc c360cb7b1f Add option to specify pid file
Closes #969
2018-02-20 03:24:23 +00:00
Rusty Russell 1dc6f7575a lightningd: fix crash when we get onchain after old db.
lightningd(25923): lightning_onchaind(03cbf298b068300be33f06c947b9d3f00a0f0e8089da3233f5db37e81d3a596fe1): UPDATE WIRE_ONCHAIN_MISSING_HTLC_OUTPUT
lightningd(25923): FATAL SIGNAL 11 RECEIVED
lightningd(25923): backtrace: lightningd/log.c:498 (log_crash) 0x410ae8
lightningd(25923): backtrace: (null):0 ((null)) 0x7ff2ddb8e0df
lightningd(25923): backtrace: lightningd/pay.c:231 (local_routing_failure) 0x415b33
lightningd(25923): backtrace: lightningd/pay.c:383 (payment_failed) 0x41604c
lightningd(25923): backtrace: lightningd/peer_htlcs.c:783 (onchain_failed_our_htlc) 0x4225e5
lightningd(25923): backtrace: lightningd/peer_control.c:1209 (handle_missing_htlc_output) 0x41b31c
lightningd(25923): backtrace: lightningd/peer_control.c:1323 (onchain_msg) 0x41b759
lightningd(25923): backtrace: lightningd/subd.c:498 (sd_msg_read) 0x425b40

Fixes: #878
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-20 01:38:32 +00:00
practicalswift f351417905 Add assertions to clarify our assumptions about msg != NULL 2018-02-19 12:17:55 +01:00
ZmnSCPxj ea3ee01215 peer_control: Support xxx@yyy:zzz form for connecting. 2018-02-19 11:59:11 +01:00
Rusty Russell cfa50d393a openingd: use peer_failed like normal instead of boutique negotiation_failed.
Because peer_failed would previously drop the connection, we had a
special 'negotiation_failed' message which made the master hand it
back to gossipd.  We don't need that any more.

This also meant we no longer need a special hook in read_peer_msg
for openingd to send this message.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell 02d469b3d4 peer_failed: hand fds back to master when we fail.
master now hands it back to gossipd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell f76ff90485 status: split off error messages into a new 'peer_status' type.
Several daemons (onchaind, hsm) want to use the status messages, but
don't communicate with peers.  The coming changes made them drag in
more code they didn't need, so instead we have a different
non-overlapping type.

We combine the status_received_errmsg and status_sent_errmsg
into a single status_peer_error, with the presence or not of the
'error_for_them' field indicating direction. 

We also rename status_fatal_connection_lost() to
peer_failed_connection_lost() to fit in.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell 611ecc60ae lightningd: rename peer_state -> channel_state, remove OPENINGD.
And now we can finally do the db upgrade to remove any OPENINGD
channels once, since we never put them back.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell cc460095ca lightningd: make new_channel a proper constructor.
It's giant, but it's encapsulating at least.  It is called from the wallet
code when loading channels, or from the opening code when converting
an uncommitted_channel.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell e0603d7221 channel: now we're always complete, fields don't have to be optional.
Now any struct channel is a genuine channel, the following fields are
always valid:

1. funding_txid: doesn't need to be a pointer.
2. our_msatoshi: doesn't need to be a pointer.
3. last_sig: doesn't need to be a pointer.
4. channel_info: doesn't need to be a pointer.

In addition, 'last_tx' is always valid.

The main effect is to remove a whole heap of branches from the wallet code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell 8db8c51201 lightningd: struct uncommitted_channel for opening channels.
Each peer can have one 'uncommitted' channel, which is in the process
of opening.  This is used for openingd, and then on return we convert
it into a full-fledged struct channel and commit it into the database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell d2f691b288 subd: make functions more generic, don't assume 'struct channel'.
This means the caller needs to supply an explicit log to base the
subd log on, and also a callback for error handling.

The callback is kind of ugly, but it gets reworked towards the end
of this series.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell 1a78efaee5 peer_control: don't refer to peer in copy_to_parent_log
Once we rely on the logbook outlasting the peer, we can't refer to the
peer from the logbook function:

Valgrind error file: valgrind-errors.26567
==26567== Invalid read of size 8
==26567==    at 0x126297: copy_to_parent_log (peer_control.c:690)
==26567==    by 0x11C06B: maybe_print (log.c:253)
==26567==    by 0x11C145: logv (log.c:270)
==26567==    by 0x11C448: log_ (log.c:319)
==26567==    by 0x132951: destroy_subd (subd.c:537)
==26567==    by 0x179C19: notify (tal.c:240)
==26567==    by 0x17A0CE: del_tree (tal.c:400)
==26567==    by 0x17A120: del_tree (tal.c:410)
==26567==    by 0x17A4ED: tal_free (tal.c:509)
==26567==    by 0x16DEB5: io_close (io.c:443)
==26567==    by 0x1328BC: sd_msg_read (subd.c:516)
==26567==    by 0x1320AC: read_fds (subd.c:328)
==26567==  Address 0x6cf9ca0 is 48 bytes inside a block of size 216 free'd
==26567==    at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26567==    by 0x17A1A9: del_tree (tal.c:421)
==26567==    by 0x17A4ED: tal_free (tal.c:509)
==26567==    by 0x124B6C: delete_peer (peer_control.c:180)
==26567==    by 0x12B369: destroy_uncommitted_channel (peer_control.c:2505)
==26567==    by 0x179C19: notify (tal.c:240)
==26567==    by 0x17A0CE: del_tree (tal.c:400)
==26567==    by 0x17A4ED: tal_free (tal.c:509)
==26567==    by 0x12B31E: opening_channel_errmsg (peer_control.c:2496)
==26567==    by 0x13243A: handle_peer_error (subd.c:407)
==26567==    by 0x1326E4: sd_msg_read (subd.c:472)
==26567==    by 0x1320AC: read_fds (subd.c:328)
==26567==  Block was alloc'd at
==26567==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26567==    by 0x179C83: allocate (tal.c:250)
==26567==    by 0x17A250: tal_alloc_ (tal.c:448)
==26567==    by 0x124950: new_peer (peer_control.c:151)
==26567==    by 0x12B3EC: new_uncommitted_channel (peer_control.c:2521)
==26567==    by 0x12B5C5: peer_accept_channel (peer_control.c:2569)
==26567==    by 0x126099: peer_sent_nongossip (peer_control.c:641)
==26567==    by 0x113B28: peer_nongossip (gossip_control.c:55)
==26567==    by 0x113D9D: gossip_msg (gossip_control.c:144)
==26567==    by 0x132783: sd_msg_read (subd.c:487)
==26567==    by 0x1320AC: read_fds (subd.c:328)
==26567==    by 0x16D1FE: next_plan (io.c:59)
==26567==

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell a2c6ec6c9b lightningd: use tal_link for log_book.
BackgroundL Each log has a log_book: many logs can share the same one,
as each one can have a separate prefix.

Testing tickled a bug at the end of this series, where subd was
logging to the peer's log_book on shutdown, but the peer was already
freed.  We've already had issues with logging while lightningd is
shutting down.

There are times when reference counting really is the right answer,
this seems to be one of them: the 'struct log' share the 'struct
log_book' and the last 'struct log' cleans it up.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
Rusty Russell 72108f0cb9 wallet: don't use rowid for the channel's DBID.
We derive the seed from this, so it needs to be unique, but using
rowid forced us to put the channel into the db early, before it
was ready.

Instead, use a counter to ensure uniqueness, initialized when we load
existing peers.  This doesn't need to touch the database at all.

As we now have only two places where the channel is committed (the
funder and fundee paths), so we create a new explicit
'wallet_channel_insert()' function: 'wallet_channel_save()' now just
updates.

Note that this also fixes some weirdness in
wallet_channels_load_active: we strangely avoided loading channels in
CLOSINGD_COMPLETE (which fortunately was a transient state, so
unlikely anyone hit this).  Note that since the lines above already
delete all the OPENINGD channels, we now simply load them all.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-19 02:56:51 +00:00
ZmnSCPxj 9be4e159ae invoice: Disable overlong description.
See: https://github.com/ElementsProject/lightning/pull/1020#pullrequestreview-97372207

Fixes: #1014
2018-02-19 02:42:11 +00:00
ZmnSCPxj 38535fc36c payalgo: Create a new failure for paying expired invoice. 2018-02-18 13:51:37 +01:00
Christian Decker b2819f9f97 wallet: Add check that wallet matches the network on startup
Adds a simple check that compares genesis-blockhashes from the
chainparams against the blockhash that the wallet was created
with. The wallet is network specific, so mixing is always a bad idea.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-17 11:36:17 +00:00
Rusty Russell 43ec3f0761 jsonrpc: allow multiple commands in-flight from single JSON connection.
We now keep a list of commands for the jcon instead of a simple
'current' pointer: the assertions become a bit more complex, but
the rest is fairly mechanical.

Fixes: #1007
Reported-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 17:51:04 +01:00
ZmnSCPxj 4ad1021c2c payalgo: Repeat pay command if possible.
Fixes: #863
2018-02-16 13:08:29 +01:00
ZmnSCPxj fda26bdcda payalgo: New file for pay command. 2018-02-16 13:08:29 +01:00
ZmnSCPxj 7ee6ccfbd7 pay: Generalize internal interface of sendpay. 2018-02-16 13:08:29 +01:00
ZmnSCPxj 700dda7e60 pay: Rename `pay_command` to `sendpay_command`
In preparation for separating `pay` algorithm from
`sendpay`.
2018-02-16 13:08:29 +01:00
Rusty Russell bdd11e07fe chaintopology: fix 100 block subtraction.
We do a complicated dance because we don't know the current block
height before setting up the topology.

If we're starting at a particular block, we want to go back 100 blocks
before that to cover any reorgs.

If we're not (fresh startup), we still want to go back 100 blocks
because we don't bother handling a reorg which removes all the blocks
we know.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:07:12 +01:00
Rusty Russell 6620305606 wallet: use last_processed_block to determine scan start.
With fallback depending on chainparams: this means the first upgrade
will be slow, but after that it'll be fast.

Fixes: #990
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:07:12 +01:00
Rusty Russell 21849329dd wallet: store last block number we searched for UTXOs.
We already go back 100 from this in case of reorgs, so the block number
itself is sufficient.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:07:12 +01:00
Rusty Russell 256bdc12ff lightningd: activate crashlog later.
We error out for all kinds of reasons early on (eg. bitcoind down),
and printing a backtrace for them is pretty confusing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:02:41 +01:00
Rusty Russell eb17d6af71 lightningd: implement --daemon.
Includes closing off stdout and stderr.  We don't do it directly in the
arg parser, as we want to interact normally (eg with other errors) before
we turn off stdout/stderr.

Fixes: #986
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:02:41 +01:00
Rusty Russell 65593d4a98 chaintopology: don't start fee estimation loop until we're fully active.
This interacts badly with --daemon (next patch) which then tries to
reap a child it didn't create, which took me a couple of hours to
figure out.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:02:41 +01:00
Rusty Russell ccd0e5db54 ccan: update so we get exposed path constants.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 13:02:41 +01:00
Rusty Russell a08bcfdbd3 jsonrpc: don't crash on multiple commands at once.
Once we read a command, we are supposed to io_wait until it finishes.
However, we are actually woken in two places: when it's complete
(which is correct), and when it's written out (which is wrong).

We don't care when it's written out, only when it's finished:
refactor to make json_done() free and NULL the old ->current,
rather than have the callers do it.  Now it's clear that it's
ready for both new output and new input.

Fixes: #934
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-16 12:56:48 +01:00
Rusty Russell 8f7a19d1a3 onchain: handle case where multiple HTLCs exist for same payment_hash.
We will have probably failed the others, but either way, don't try to
fulfill an HTLC we've already failed.

Fixes: #394
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:57:42 +01:00
Rusty Russell 55d962046b Rename (almost) all destructors to destroy_<type>.
We usually did this, but sometimes they were named after what they did,
rather than what they cleaned up.

There are still a few exceptions:
1. I didn't bother creating destroy_xxx wrappers for htable routines
   which already existed.
2. Sometimes destructors really are used for side-effects (eg. to simply
   mark that something was freed): these are clearer with boutique names.
3. Generally destructors are static, but they don't need to be: in some
   cases we attach a destructor then remove it later, or only attach
   to *some* cases.  These are best with qualifiers in the destroy_<type>
   name.

Suggested-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 6a3ccafaf9 wallet: don't implicitly remove peers, but do it explicitly.
This provides a sanity check that we are in sync, and also keeps the
logic in the program and out of the SQL.

Since the destructor now doesn't clean up the peer, there are some
wider changes to be made when cleaning up.  Most notably we create
lots of channels in run-wallet.c and they previously freed the peer:
now we need free the peer explicitly, so we need to free them first.

Suggested-by: @cdecker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 98de10b842 channel: rename free_channel to delete_channel.
free_channel() sounds like a destructor.

Suggested-by: @cdecker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell d822ba1eee lightningd: allow a new channel open from peer if no *active* channels.
And return the correct error message for the channel they give, if
they try to re-establish on an error channel.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 8f48a72d74 lightningd: remove peer->log in favor of channel->log.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell e20fff9340 lightningd: remove almost all other peer2channel / channel2peer shims.
This final sweep only keepl peer2channel within peer_control.c for
the reconnect case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 0e93fb932a lightningd: bitcoind and topology routines take channel, not peer.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 329e31bbe7 lightningd/peer_htlcs: remove remaining peer_ shims.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell cf7c399cc5 htlc: keep channel pointer, not peer pointer.
And move the no-remaining-htlcs check from the peer destructor to the
channel destructor.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 409fef582d subd: keep pointer to channel, not peer.
This rolls through many other functions, making them take channel not peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell b7680412e3 lightningd: rename peer_fail functions to channel_fail.
And move them into channel.c.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 8c084d57ff lightningd: channels own the peer.
Channels are within the peer structure, but the peer is freed only
when the last channel is freed.

We also implement channel_set_owner() and make peer_set_owner() a temporary
wrapper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 32411de90e lightningd: split struct peer into struct peer and struct channel.
Much like the database; peer contains id, address, channel contains
per-channel information.  Where we create a channel, we always create
the peer too.

For the moment, peer->log and channel->log coexist side-by-side, to
reduce some of the churn.

Note that this changes the API to dev-forget-channel: if we have more
than one channel, we insist they specify the short-channel-id.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 6b71654351 lightningd: create new structure `channel` to hold per-channel info.
This is not connected yet; during the transition, there will be a 1:1
mapping from channel to peer, so we can use channel2peer and peer2channel
to shim between them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 81ca1db347 wallet: delete peers with no channels.
ON DELETE CASCADE goes the other way: we should clean up peers with no
channels from db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 38a313af0d wallet: delete channels in state OPENINGD.
Both when we forget about an opening peer, and at startup.  We're
going to be relying on this, and the next patch, as we refactor
peer/channel handling to mirror the db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell 15eaf56d79 wallet: add ld pointer.
This will be required to give it direct access to the ld->peers list.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00
Rusty Russell fd04fca771 jsonrpc: make lifetimes in error path clearer.
Saving one allocation isn't worth the confusion.

Reported-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-13 12:01:27 +01:00
Rusty Russell dec86e0115 jsonrpc: split error and success cases, use json_add_string_escape.
Combining the two was just awkward, so it's clearer to have separate
functions.  And we make the lower-level functions do the escaping.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-13 12:01:27 +01:00
Christian Decker b4ce4d228d JSON-RPC: Fix unquoted error string when parser fails
Fixes #963

Reported-by: @shesek
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-13 12:01:27 +01:00
Christian Decker cb97dfec4e gossip: Fix up some comments on PR #955
Just some minor things that were suggested during review.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-11 11:40:59 +01:00
Christian Decker 9f8f0ccec9 peer: Store htlc_sigs when they are updated
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-11 01:13:07 +01:00
root 73022d91c6 Fix indentation 2018-02-10 21:30:46 +01:00
root 8b0ed27c43 Add optional from id parameter to getroute call. 2018-02-10 21:30:46 +01:00
Christian Decker 5a133d2d70 json-rpc: Return a standard compliant error when parsing fails
The JSON-RPC spec specifies that if the request is unparseable we
should return an error with a NULL id. This is a bit more friendly
than slamming the door in the face.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-10 15:44:14 +01:00
Christian Decker e92e26f84d json-rpc: Check for unprintable characters in JSON-RPC commands
As reported by @practicalswift in #945 it is possible to inject
non-printable, or shell escape, characters in a json command, that
will fail to parse and then clear the shell.

Reported-by: @practicalswift
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-10 15:44:14 +01:00
ZmnSCPxj 354fafbf60 pay: Add a limit to the fee.
Fixes: #717
2018-02-09 12:44:33 +01:00
practicalswift 2a979a2d5c Fix typos 2018-02-08 23:33:49 +01:00
practicalswift 4f4756bd20 Fix a-vs-an typos 2018-02-08 22:49:34 +01:00
practicalswift 5474512c31 Add assertion to clarify assumption made in order to avoid UB 2018-02-08 19:31:18 +01:00
Rusty Russell cc9ca82821 status: separate types for peer failure vs "impossible" failures.
Ideally we'd rename status_failed() to status_fatal(), but that's
too much churn for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Rusty Russell fd498be7ca status: generate messages rather than marshal/unmarshal manually.
Now we have wirestring, this is much more natural.  And with the
24M length limit, we needn't be so concerned about dumping 64k peer
messages in hex.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Rusty Russell 780b620cb1 openingd: use wirestring instead of u8 array for why negotiation failed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Rusty Russell 674a2c7554 tools/generate_wire.py: make bitcoin_tx a varlen structs
Now it allocates naturally.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Rusty Russell 526d3a232e tools/generate_wire.py: generate varlen arrays properly.
These are now logically arrays of pointers.  This is much more natural,
and gets rid of the horrible utxo array converters.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Rusty Russell ad8dfaca1c tools/generate_wire.py: make varlen structs self-allocate.
If we tell it a struct is variable length, make fromwire() allocate
and return it off ctx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-08 19:07:12 +01:00
Christian Decker e04619457b master: Don't crash if peer reconnects before we activate it
`activate_peer` does little more than wiring up some txwatches and
asking `gossipd` to reconnect to the peer. If the peer manages to
reconnect before we activate then we would crash.

This just changes the `assert` causing the crash into a conditional
whether we need to reconnect or not.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-08 01:07:59 +00:00
ZmnSCPxj d666857aed pay: Recover from unparseable routing failures by random disabling of channels.
Fixes: #868

Not pretty, but workable.
2018-02-08 00:51:30 +00:00
ZmnSCPxj 0d0ef2c733 gossipd: Implement gossip_mark_channel_unroutable.
Temporarily marks a channel as unroutable.
2018-02-08 00:51:30 +00:00
Christian Decker d05e8ed695 jsonrpc: Add a dev-forget-channel command to remove stale channels
Due to the broadcast failure quite a few users are reporting channels
stuck in awaiting lockin. This commit adds a `dev-forget-channel`
command that checks whether the funding outpoint is in the UTXO, and
forgets the channel if not. The UTXO check can be overridden with the
`force` parameter, but that is dangerous.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-07 00:47:13 +00:00
Christian Decker 3fdb055606 wallet: Make first_blocknum a field of wallet_channel
We were sideloading it, which is awkward, now it's a field that we can
actually use in the code.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2018-02-07 00:47:13 +00:00
Rusty Russell c01f3267d5 common: only log io if they set --debug-subdaemon-io=<daemon> or with SIGUSR1.
Otherwise we just log the type of msg.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell de56dc0ffc common: add logging for peer packets, with status_io.
We log the plaintext, not the encrypted ones.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell e2eb694619 Use common loglevel JSON parser.
This now means we can ask for IO logs for peers, too (that case was missing!)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell b7acd93578 log: separate levels for IO directions, allow msg + io data.
We currently don't handle LOG_IO properly, and we turn it into a string
before handing it to the ->print function, which makes it ugly for
the case where we're using copy_to_parent_log, and also means in
that case we lose *what peer* the IO is coming from.

Now, we handle the io as a separate arg, which is much neater.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell 4856ca5db0 json_log: move command to log.c, share code with listpeers log.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell c076534220 logv: preserve errno.
Logging often gets called in error paths, so this is just good hygiene.
Also, log_io does this already.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell ad401f3a1c hsm: give it its own log, so it has unique prefix.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
Rusty Russell 84bf60f934 status: add multiple levels of logging.
status_trace maps to status_debug.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-07 00:46:49 +00:00
ZmnSCPxj d4478458dd pay: Change origin_index to erring_index, start with local node = 0. 2018-02-06 17:05:12 +01:00
ZmnSCPxj a6c6b8d9d3 pay: Provide detailed errors on `sendpay` or `pay` failure.
Fixes: #866
2018-02-06 17:05:12 +01:00
ZmnSCPxj 452925c05a pay: Tweak interface of remote_routing_failure.
In preparation for a common interface to reporting routing failures
  to JSON-RPC interface.
2018-02-06 17:05:12 +01:00
ZmnSCPxj a1b9fa7538 pay: Add origin_index to struct routing_failure. 2018-02-06 17:05:12 +01:00
ZmnSCPxj 6d789282c9 jsonrpc_errors.h: Header for JSON-RPC errors. 2018-02-06 17:05:12 +01:00
Wladimir J. van der Laan 1ef79854c3 lightningd: Don't crash when libunwind backtrace not available
libunwind does not accept a NULL parameter for the error callback. It
will simply call into the NULL pointer. So add an error callback.

This makes the crash output somewhat more sensible on FreeBSD, where
there is no libunwind stack trace available:

    2018-02-05T20:24:50.598Z lightningd(75556): error getting backtrace: no stack trace because unwind library not available (0)

Signed-off-by: Wladimir J. van der Laan <laanwj@gmail.com>
2018-02-06 01:35:31 +00:00
Igor Cota a0dffc7be2 Fix listpeers JSON command description 2018-02-05 18:23:41 +00:00
ZmnSCPxj 532833cd78 invoice: Return the invoice as a detailed error on expiration 2018-02-05 08:52:42 +00:00
ZmnSCPxj 59abc37258 invoices: Add expiration timer system.
Fixes: #502

Changes behavior of waitinvoice API!!
2018-02-05 08:52:42 +00:00
ZmnSCPxj 1e6747c28e wallet: Pass in timers object during construction.
In preparation for expiration.
2018-02-05 08:52:42 +00:00
ZmnSCPxj f21cb57d27 pay: Also treat nonexistent peer as immediate routing failure. 2018-02-03 21:03:01 +01:00
Rusty Russell de0777cb2c Fix weight calculation, rename cost->weight.
Reported-by: Jon Griffiths
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-03 21:00:55 +01:00
windsok 26957fce07 update max-locktime-blocks description to be accurate 2018-02-03 17:56:08 +01:00
ZmnSCPxj 5f2cd7c11e pay: Report routing failure on immediate fail of send_htlc_out. 2018-02-03 07:11:34 +00:00
ZmnSCPxj d14efde4bc pay: Refactor logging into report_routing_failure. 2018-02-03 07:11:34 +00:00
ZmnSCPxj 4ef168cd97 jsonrpc: Properly report code for detailed errors. 2018-02-02 23:52:28 +00:00
ZmnSCPxj 5b304fce4f jsonrpc: Let freeing of `cmd` free `names`
Fixes: #885
2018-02-02 23:45:18 +00:00
practicalswift 98372e4c0a Mark last parameter of log_to_stdout(...) as intentionally unused 2018-02-02 20:34:22 +01:00
Rusty Russell 9b8fe618f6 pay: remove cmd pointer from htlc_out.
Maintaining it was always fraught, since the command could go away
if the JSON RPC died.  Most recently, it was broken again on shutdown
(see below).

In future we may allow pay commands to block on previous payments, so
it won't even be a 1:1 mapping.  Generalize it: keep commands in a
simple list and do a lookup when a payment fails/succeeds.

Valgrind error file: valgrind-errors.5732
==5732== Invalid read of size 8
==5732==    at 0x4149FD: remove_cmd_from_hout (pay.c:292)
==5732==    by 0x468BAB: notify (tal.c:237)
==5732==    by 0x469077: del_tree (tal.c:400)
==5732==    by 0x4690C7: del_tree (tal.c:410)
==5732==    by 0x46948A: tal_free (tal.c:509)
==5732==    by 0x40F1EA: main (lightningd.c:362)
==5732==  Address 0x69df148 is 1,512 bytes inside a block of size 1,544 free'd
==5732==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5732==    by 0x469150: del_tree (tal.c:421)
==5732==    by 0x46948A: tal_free (tal.c:509)
==5732==    by 0x4198F2: free_htlcs (peer_control.c:1281)
==5732==    by 0x40EBA9: shutdown_subdaemons (lightningd.c:209)
==5732==    by 0x40F1DE: main (lightningd.c:360)
==5732==  Block was alloc'd at
==5732==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5732==    by 0x468C30: allocate (tal.c:250)
==5732==    by 0x4691F7: tal_alloc_ (tal.c:448)
==5732==    by 0x40A279: new_htlc_out (htlc_end.c:143)
==5732==    by 0x41FD64: send_htlc_out (peer_htlcs.c:397)
==5732==    by 0x41511C: send_payment (pay.c:388)
==5732==    by 0x41589E: json_sendpay (pay.c:513)
==5732==    by 0x40D9B1: parse_request (jsonrpc.c:600)
==5732==    by 0x40DCAC: read_json (jsonrpc.c:667)
==5732==    by 0x45C706: next_plan (io.c:59)
==5732==    by 0x45D1DD: do_plan (io.c:387)
==5732==    by 0x45D21B: io_ready (io.c:397)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-02 20:33:49 +01:00
Rusty Russell bd1aa935b9 closingd: don't punish peers who can't negotiate properly.
This is a transitional patch so we can still close channels cleanly;
for want of a better option, I hooked it into --deprecated-apis.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-02 20:33:49 +01:00
Rusty Russell 5deebb85c3 closingd: use the final commitment tx fee as the maximum.
We shouldn't fail negotiation just because they exceeded what we thought
fair: we're better off as long as it's actually <= final commitment fee.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-02 20:33:49 +01:00
Rusty Russell 4ed13401ce lightningd: prefer mutual close over unilateral, even if fee identical.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-02 20:33:49 +01:00