Commit Graph

6686 Commits

Author SHA1 Message Date
Rusty Russell fd2d74aa9b gossipd: defer asking about txouts until we're synced or they're 6 deep.
The first one means we don't discard channels just because we're not
synced, and the second is implied by the spec: don't accept
channel_announcement if the channel isn't 6 deep.  Since LND defers in
such cases, we do too (unless it's newer than the current block, in
which case we simply discard).  Otherwise there's a risk that a slow
node might discard valid gossip.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell 45381bba33 bitcoin: implement is_scid_depth_announceable helper.
The math is a bit tricky, so encapsulate it.

Includes the extra 'e' in 'announcable' as noted by @cdecker :)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell 2a74d53841 Move gossip_constants.h into common/
Turns out we weren't checking the BOLT comments before, so they
needed an overhaul.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell 6f9c5f2936 gossipd: get fed the blockheight from lightningd when we know it.
This will let gossipd be more intelligent about gossiping before we're
synced, and also it might know how far behind we are.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell 6955bf6b86 lightningd: initialize topology synclist earlier.
We were doing it in setup_topology, but that's too late if gossipd
wants to register earlier.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell 3c9ca5d3e1 openingd: ensure that revents fields are initialized.
Valgrind error file: valgrind-errors.112365
==112365== Conditional jump or move depends on uninitialised value(s)
==112365==    at 0x1105E0: main (openingd.c:1504)
==112365== 
==112365== Conditional jump or move depends on uninitialised value(s)
==112365==    at 0x110604: main (openingd.c:1507)
==112365== 
==112365== Conditional jump or move depends on uninitialised value(s)
==112365==    at 0x110628: main (openingd.c:1510)
==112365==

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Saibato e74dfa7fa1 Show in info log the name of the hidden onion service
Signed-off-by: Saibato <saibato.naga@pm.me>
2019-09-25 04:26:15 +08:00
trueptolemy 765416f0ca cleanup: Use `json_out_add_u32` helper to add `max_hops` and `cltv` in `pay` plugin 2019-09-24 16:01:24 +02:00
trueptolemy d8dce6e61f cleanup: Use `u32` as the type of `max_hops` in `gossipd` 2019-09-24 16:01:24 +02:00
trueptolemy 8103e48f8c fix: remove extra semicolon in `json_prepare_tx` 2019-09-24 11:44:13 +08:00
Rusty Russell 14b55d03d5 db: use correct backend for retrieving text fields.
Otherwise we don't have a nul terminator:

ep 22 02:25:42 lntxbot lightningd[804]: 2019/09/22 02:25:42 plugin-jqmethods Initialized jqmethods plugin.
Sep 22 02:30:01 lntxbot lightningd[804]: lightningd: FATAL SIGNAL 11 (version v0.7.2.1-255-g7686068)
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a0c6be send_backtrace
Sep 22 02:30:01 lntxbot lightningd[804]:         common/daemon.c:40
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a0c751 crashdump
Sep 22 02:30:01 lntxbot lightningd[804]:         common/daemon.c:53
Sep 22 02:30:01 lntxbot lightningd[804]: 0x7f1654d30f1f ???                                                                                                                                                                            Sep 22 02:30:01 lntxbot lightningd[804]:         ???:0
Sep 22 02:30:01 lntxbot lightningd[804]: 0x7f1654dad578 ???
Sep 22 02:30:01 lntxbot lightningd[804]:         ???:0
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a48b70 memcpy
Sep 22 02:30:01 lntxbot lightningd[804]:         /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a48b70 tal_dup_
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/tal/tal.c:796
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a46e81 tal_strdup_
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/tal/str/str.c:18
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a2e680 wallet_stmt2payment
Sep 22 02:30:01 lntxbot lightningd[804]:         wallet/wallet.c:2167
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a32711 wallet_payment_list
Sep 22 02:30:01 lntxbot lightningd[804]:         wallet/wallet.c:2446
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039fb0d2 json_listsendpays
Sep 22 02:30:01 lntxbot lightningd[804]:         lightningd/pay.c:989
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039f0d6f parse_request
Sep 22 02:30:01 lntxbot lightningd[804]:         lightningd/jsonrpc.c:650
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039f1039 read_json
Sep 22 02:30:01 lntxbot lightningd[804]:         lightningd/jsonrpc.c:748
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a3ddee next_plan
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/io/io.c:59
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a3e2dd do_plan
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/io/io.c:407
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a3e30a io_ready
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/io/io.c:417
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b03a3fae2 io_loop
Sep 22 02:30:01 lntxbot lightningd[804]:         ccan/ccan/io/poll.c:445
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039ee96c io_loop_with_timers
Sep 22 02:30:01 lntxbot lightningd[804]:         lightningd/io_loop_with_timers.c:24
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039f2796 main
Sep 22 02:30:01 lntxbot lightningd[804]:         lightningd/lightningd.c:835
Sep 22 02:30:01 lntxbot lightningd[804]: 0x7f1654d13b96 ???
Sep 22 02:30:01 lntxbot lightningd[804]:         ???:0
Sep 22 02:30:01 lntxbot lightningd[804]: 0x561b039e1919 ???
Sep 22 02:30:01 lntxbot lightningd[804]:         ???:0
Sep 22 02:30:01 lntxbot lightningd[804]: 0xffffffffffffffff ???
Sep 22 02:30:01 lntxbot lightningd[804]:         ???:0

Reported-by: @fiatjaf
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-23 02:05:53 +00:00
Rusty Russell b55ff34f93 gossipd: fix corner case where gossip msg too old after pending delay.
Happened under Travis with --dev-fast-gossip (90 second prune time), but can
happen anyway if gossip is almost 2 weeks old when we receive it:

2019-09-20T19:16:51.367Z DEBUG lightning_gossipd(20972): Received node_announcement for node 022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59
2019-09-20T19:16:51.376Z DEBUG lightning_gossipd(20972): Ignoring node_announcement timestamp 1569006918 for 022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59
2019-09-20T19:16:51.669Z **BROKEN** lightning_gossipd(20972): pending node_announcement 01013094af771d60f4de69bb39ce045e4edf4a06fe6c80078dfa4fab58ab5617d6ad4fa34b6d3437380db0a8293cea348bbc77f714ef71fcd8515bfc82336667441f00005d852546022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59022d2253494c454e544152544953542d633961313734610000000000000000000000000000 malformed? (version c9a174a)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 20:56:11 +02:00
Christian Decker 9915386c4a pytest: Stabilize test_no_fee_estimate against UTXO selection issues
The test was implicitly relying on us selecting the larger output and then not
touching the smaller, leaving it there for the final `withdraw` to claim. This
ordering of UTXOs is not guaranteed, and in particular can fail when switching
DB backends. To stabilize we just need to make sure to select the change
output as well.
2019-09-22 02:03:43 +00:00
Christian Decker 063b319da1 pytest: Have the DB provider search for the postgres binary
This replaces the hard-coded path to the `postgres` and `initdb` binaries with
a slightly more flexible search. It'll pick the newest version installed.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 136d4b2568 pytest: Consolidate fee-fetching in test_setchannelfee_usage
The short_channel_id is already in text format, no need to hexlify it :-)

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker efc4aa94a3 pytest: Skip some tests that assume we have a sqlite3 db on postgres
These will not work since they touch the DB file itself.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker b89222f2d9 db: Adjust queries to work with postgres
The DB field type has to match the size of the accessor-type, and we had to
split the `REPLACE INTO` and `INSERT INTO OR IGNORE` queries into two
queries (update and insert if not updated) since there is no portable UPSERT
operation, but impact should be minimal.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker d901304120 db: Change table field types to be more specific
sqlite3 was forgiving, postgres isn't, so let's make sure we use the strictest
field type possible, relaxing when rewriting.

The commit consists just of the following mapping

 - INTEGER -> BIGSERIAL if it is the primary key
 - INTEGER -> BIGINT if it is an amount or a reference to a primary key

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 416af636f0 db: Check execution when accessing the result of a statement
This was already done in `db_step` but `db_count_changes` and
`db_last_insert_id` also rely on the statement being executed. Furthermore we
now check that the statement was executed before freeing it, so it can't
happen that we dispose of a statement we meant to execute but forgot.

The combination of these could be used to replace the pending_statement
tracking based on lists, since we now make sure to execute all statements and
we use the memleak checker to make sure we don't keep a statement in memory.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 1f935cbd85 db: Strengthen some null-checks on queries
sqlite3 will just report 0 for anything that it thinks should be numeric, or
is accessed using a numeric accessor. Postgres does not, so we need to check
for is_null before trying to read it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 07f8d9046a db: Change migrations to use types of the correct cardinality
sqlite3 doesn't really do any validation whatsoever, and there is no
difference between 64bit and 32bit numbers. Posgtres on the other hand gets
very upset if the size doesn't match.

This commit swaps out handwavy types with the ones that should be there :-)

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 2ddf168d59 db: Implement SQL statement rewriting
We now have an abstract rewriter that will perform some common extractions and
replacements (type replacement for example), that can then be customized in
derived classes.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 6b26ce5764 db: Split the vars table to have type-specific columns
This was weird right from the start, so we just split the table into integers
and blobs, so each column has a well-defined format. It is also required for
postgres not to cry about explicit casts in the `paramTypes` array.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 074af11e43 db: Select driver by dsn prefix
We were doing exact matches before, but prefix is sufficient.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker ad89699888 db: Allow some internal queries to fail
The first ever query to check if the version DB exists may fail. We allow
this, but we need to restart the DB transaction since postgres fails the
current transaction and rolls back any changes.

This just commits (and fails) and starts a new transaction so the rest of the
migration can continue.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker acf72de5be db: Implement postgres driver primitives
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker db6abda3a4 db: Adjust some db migrations to be compatible with postgres
Needed to change a couple of migrations. The changes are mostly innocuous:

 - changing BLOB to TEXT for short_channel_ids which is the correct type
   anyway, and sqlite3 treats them the same anyway.
 - Use `int` for version since the byte representation is checked by postgres.
 - Change anything that is INT, but will be bound to u64 to BIGINT (again
   postgres checks these more carefully than sqlite3).

Two migrations were replaced with dummy values, since they are buried deep
enough, and I found no portable way of expressing `strftime()` and `INSERT OR
IGNORE`.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 640e64cb73 db: Switch statement lookup to use the original query instead
Using a generated identifier with filename and line proved to be brittle since
compilers assign the __LINE__ macro differently on multi-line macro
invocations.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 3df8ccd3e0 db: Reorder migrations to reflect their relationship
This is dangerous but needed since postgres is not as forgiving about
unsatisfied foreign key constraints even while in a DB transaction.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker e2f7c86271 db: Move remainder of the sqlite3 into the apropriate file
We used to do some of the setup work in db.c, which is now free of any
sqlite3-specific code. In addition we also switch over to fully qualified DSNs
to specify the location of the wallet.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker bf613fa48a postgres: Add postgres statement rewriting support
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 5953a5051c cli: Add command line option to specify the wallet location
Will be demuxed into starting the selected DB backend in one of the next
commits. Defaults to the old database location.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 96a22b4003 pytest: Add db_provider and db instances for configurable backends
We will soon have a postgres backend as well, so we need a way to control the
postgres process and to provision DBs to the nodes. The two interfaces are the
dsn that we pass to the node, and the python query interface needed to query
from tests.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Christian Decker 62dc8dc110 make: Add configuration detection and linking of libpq
If we have the client library for postgres configure will define HAVE_POSTGRES
the same way it already handled libsqlite3 an we start linking against it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-09-22 02:03:43 +00:00
Rusty Russell 27790832a5 gossipd: gossip_queries_ex is not longer experimental.
The master spec has some typos which make it not parse, so I created
a PR and generated the CSV from that:

https://github.com/lightningnetwork/lightning-rfc/pull/673

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell 895e552475 BOLT: update to master with gossip_queries_ex.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell 6eb838ddda wire: update csv to current bolt.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell 8fdfdcec74 BOLT: Update to include BOLT11 features.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell e5564173e7 BOLT: update to cover minor changes.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell 0da074cad3 BOLT: update bolt quotes now that TLVs are not experimental.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
Rusty Russell ccbc46a476 check-bolt: escape { and }.
These are special in extended regexs, and so we fail to match once we fix
the BOLT comment in common/test/run-bigsize.c

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-22 01:17:11 +00:00
fiatjaf 1d5bbc874a format JSON snippets with jq. 2019-09-21 19:41:43 +02:00
Rusty Russell 6a8d18c7e3 gossipd: naming cleanups.
Suggested-by: @cdecker.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 39c9dcbafc ratelimit: adjust based on --dev-fast-gossip, test.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 147eaced2e developer: consolidiate gossip timing options into one --dev-fast-gossip.
It's generally clearer to have simple hardcoded numbers with an
#if DEVELOPER around it, than apparent variables which aren't, really.

Interestingly, our pruning test was always kinda broken: we have to pass
two cycles, since l2 will refresh the channel once to avoid pruning.

Do the more obvious thing, and cut the network in half and check that
l1 and l3 time out.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 8139164aa0 gossipd: disallow far future (+1 day) or far past (2 weeks) timestamps.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 76860683aa gossipd: only allow one channel_update per direction per day.
And similar for node_announcement.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell a92ead48bf gossipd: ignore redundant channel_update and node_announcement.
If you send a message which simply changes timestamp and signature, we
drop it.  You shouldn't be doing that, and the door to ignoring them
was opened by by option_gossip_query_ex, which would allow clients to
ignore updates with the same checksum.

This is more aggressive at reducing spam messages, but we allow refreshes
(to be conservative, we allow them even when 1/2 of the way through the
refresh period).

I dropped the now-unnecessary sleep from test_gossip_pruning, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 46e0f1efcc gossipd: refresh every 13 days, not every 7.
One day is plenty of time to propagate the update.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00
Rusty Russell 06afb408d8 gossipd: bias lower bit of timestamp to ensure alternation.
This is useful for various "partial timestamp" forms of propagation
in future, esp. minisketch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-20 06:55:00 +00:00