Commit Graph

623 Commits

Author SHA1 Message Date
Christian Decker 6d1bcc2c32 Removing the --ignore-dbversion option
It's no longer used and we definitely do not want to run with an
outdated or future db, so we'll terminate if we can't upgrade or
the version is newer than what we understand.

Signed-off-by: Christian Decker <decker.christian@gmai.com>
2017-09-08 17:02:04 +09:30
Christian Decker 641a3b64cf channel: Use min_cltv_expiry in the channel announcements
So far we were always using the deadline in the announcements, that's
obviously not good, so this introduces the parameter as per spec.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-08 17:02:04 +09:30
Christian Decker d75733f7fb fix: Make sure peer->owner is always set 2017-09-08 17:02:04 +09:30
Christian Decker 0137aea7fc test: Don't assume size_t == u64 in tests 2017-09-08 16:56:07 +09:30
Rusty Russell b19a4516d4 lightningd: fix crash when old openingd still around.
We weren't killing it.  Eventually it would die, and peer_owner_finished()
would access subd->peer->owner, but that peer was freed already.

Closes: #261
Reported-by: Christian Decker <decker.christian@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-06 19:11:21 +02:00
Rusty Russell 5889ad5fc4 dev-disconnect: add blackhole option.
To reproduce the next bug, I had to ensure that one node keeps thinking it's
disconnected, then the other node reconnects, then the first node realizes
it's disconnected.

This code does that, adding a '0' dev-disconnect modifier.  That means
we fork off a process which (due to pipebuf) will accept a little
data, but when the dev_disconnect file is truncated (a hacky, but
effective, signalling mechanism) will exit, as if the socket finally
realized it's not connected any more.

The python tests hang waiting for the daemon to terminate if you leave
the blackhole around; to give a clue as to what's happening in this
case I moved the log dump to before killing the daemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-06 19:11:21 +02:00
Rusty Russell e8dadbc0fb lightningd: fix reconnect while openingd still live.
In this case, we unset the old subd->peer, then freed subd.
peer_owner_finished dereferenced subd->peer->owner, and boom:

test_disconnect_funder (__main__.LightningDTests) ... Fatal signal 11. Log dumped in crash.log

------------------------------- Valgrind errors --------------------------------
Valgrind error file: valgrind-errors.2882
==2882== Invalid read of size 8
==2882==    at 0x413F74: peer_owner_finished (peer_control.c:679)
==2882==    by 0x41EA2C: destroy_subd (subd.c:381)
==2882==    by 0x459700: notify (tal.c:240)
==2882==    by 0x459BB1: del_tree (tal.c:400)
==2882==    by 0x459FC0: tal_free (tal.c:509)
==2882==    by 0x413796: peer_reconnected (peer_control.c:493)
==2882==    by 0x413A6A: add_peer (peer_control.c:592)
==2882==    by 0x40ED1F: handshake_succeeded (new_connection.c:186)
==2882==    by 0x41E3DD: sd_msg_reply (subd.c:262)
==2882==    by 0x41E6BB: sd_msg_read (subd.c:318)
==2882==    by 0x41E4E6: read_fds (subd.c:283)
==2882==    by 0x44DEB4: next_plan (io.c:59)
==2882==  Address 0x838 is not stack'd, malloc'd or (recently) free'd
==2882==

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-06 05:14:38 +09:30
Christian Decker b0c0e28a43 gossip: Simplify announce_signature exchange
The logic of dispatching the announcement_signatures message was
distributed over several places and daemons. This aims to simplify it
by moving it all into `channeld`, making peer_control only report
announcement depth to `channeld`, which then takes care of the
rest. We also do not reuse the funding_locked tx watcher since it is
easier to just fire off a new watcher with the specific purpose of
waiting for the announcement_depth.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-05 12:47:25 +09:30
Christian Decker 1047e891b7 cleanup: Remove IRC flags from default options
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-04 08:47:13 +02:00
Rusty Russell 40fc95921d Cleanup: remove unused IRC flag.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-04 08:47:13 +02:00
Rusty Russell ee9e300da0 gossip: fix address descriptor handling.
1. The code to skip over padding didn't take into account max.
2. It also didn't use symbolic names.
3. We are not supposed to fail on unknown addresses, just stop parsing.
4. We don't use the read_ip/write_ip code, so get rid of it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-03 02:01:54 +02:00
Rusty Russell 4be7e94e0b gossip: make rpc responses correct.
It's not fee_per_kw, it's fee-per-millionth and a base in msatoshi.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-03 02:01:54 +02:00
Rusty Russell 28553308e2 gossip: make channel_announce-set fields unset until channel announce.
Use a negative timestamp as the flag for this, making the test simple.
This allows valgrind to detect that we're accessing them prematurely,
including across the wire on gossip_getchannels_entry.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-03 02:01:54 +02:00
Rusty Russell 1cf33eefe2 lightningd: handle case where channeld fails locally-generated HTLC.
jl777 reported a crash when we try to pay past reserve.  Fix that (and
a whole class of related bugs) and add tests.

In test_lightning.py I had to make non-async path for sendpay() non-threaded
to get the exception passed through for testing.

Closes: #236
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-30 11:36:37 +02:00
Rusty Russell bbed5e3411 Rename subdaemons, move them into top level.
We leave the *build* results in lightningd/ for ease of in-place testing though.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 739b163f8b Makefiles: simplify dependencies.
Gather all binaries and objects and make the depend on external
requirements and common headers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell f42f34b82d external: new subdirectory for all external libraries and submodules.
You will want to 'make distclean' after this.

I also removed libsecp; we use the one in in libwally anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell c6976cd947 shachain: always build 48 bit version.
No more special Makefile hacks required.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 153c622157 lightningd: remove lightningd_state.
Some fields were redundant, some are simply moved into 'struct lightningd'.
All routines updated to hand 'struct lightningd *ld' now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 77bcaf0a25 getinfo: remove testnet flag and have network flag instead.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 0170224fd0 dev_restart: remove this command.
We'll end up doing this via packet traffic (like permfail) I imagine.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 8c22bd9ee1 headers: fix up header idempotent lines.
For future reference, done via:
	for f in `find wire/ bitcoin/ common/ lightningd -name '*.h' ! -name 'gen*'`; do ID=`echo -n LIGHTNING/$f | tr 'a-z' 'A-Z' | tr -cs 'A-Z0-9' _`; sed 's/^#\(ifndef\|define\) .*_H$/#\1 '$ID/ < $f | sed 's,#endif /..*_H ./$,#endif /* '$ID' */,' | bagto $f; done

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell a37c165cb9 common: move some files out of lightningd/
Basically all files shared by different daemons.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 8375857116 common: absorb remaining files from daemon/
Also, we split the more sophisticated json_add helpers to avoid pulling in
everything into lightning-cli, and unify the routines to print struct
short_channel_id (it's ':',  not '/' too).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell a3c51168db routing: use status() instead of log-to-nowhere.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 85ff95e829 common: new directory for any shared objects.
To avoid everything pulling in HTLCs stuff to the opening daemon, we
split the channel and commit_tx routines into initial_channel and
initial_commit_tx (no HTLC support) and move full HTLC supporting versions
into channeld.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 8088e5cadb protobuf: remove
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 3c22aaa213 htlc: move htlc structure into channeld.
Other places require the flags and states, but the structure is
only needed in channeld, and even then we can remove several fields.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 052c9f42d4 bitcoin/script: remove routines unnecessary for modern daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Christian Decker 4b64b7f2aa lightningd: Fix channel-persistence for channels with commits
I was hoping to defer HTLC updates until we actually store HTLCs, but
we need to flush to DB whenever balances update as well.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-26 16:37:53 +09:30
Rusty Russell d7db0be5da lightningd: start onchaind when we see funding spent.
We're very simple about it: if there's a reorganization, we restart.  Otherwise
we tell it about everything.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-23 17:38:22 +02:00
Rusty Russell 1b2fb18944 onchaind: basic daemon.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-23 17:38:22 +02:00
Rusty Russell 1396cf6249 lightningd: remove num_revocations_received.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-23 17:38:22 +02:00
Rusty Russell a5d4a3eb2c lightningd: derive num_revocations_received.
It's in the shachain, so storing it is completely redundant.  We leave
it in for the moment so we can assert() that nothing has changed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-23 17:38:22 +02:00
Christian Decker 9c3ac38544 lighthningd: Make sure peer->htlcs is initialized
When loading from DB the list of htlcs was not being initialized which
caused a segfault when the first commit came around, this fixes it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker def3d77a70 lightningd: Base peer->seed off of the channel ID
The peer->seed needs to be unique for each channel, since bitcoin
pubkeys and the shachain are generated from it. However we also need
to guarantee that the same seed is generated for a given channel every
time, e.g., upon a restart. The DB channel ID is guaranteed to be
unique, and will not change throughout the lifetime of a channel, so
we simply mix it in, instead of a separate increasing counter.

We also needed to make sure to store in the DB before deriving the
seed, in order to get an ID assigned by the DB.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker e51d261f51 lightningd: Load persisted channels on startup
This is the big one, and it's completely anticlimactic: it loads all
channels that have reached opening and are not marked as
closingd_complete into memory, that's it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker 1a94e7282b db: Make peers unique by node_id
We should only ever have a single entry for each node_id, enforcing
this on the DB.
2017-08-23 10:23:54 +09:30
Christian Decker 4bc0750882 wallet: Moving wallet_channel->peer_id into the peer struct
This was supposed to be a temporary solution anyway, and I had a
rather annoying mixup between peer_id and unique_id, the latter of
which is actually a connection identifier.
2017-08-23 10:23:54 +09:30
Christian Decker 3fefd5f46d master: Only persist the peer/channel if we have reached openingd
There is no point in doing so before, and it'd be creating empty
entries for each connection.
2017-08-23 10:23:54 +09:30
Christian Decker e9b69b195a wallet: Store channel to db whenever something changes
This is as coarse grained as it can get, and therefore slow, but it'll
store the channel (sans HTLCs) to database so we can recover them upon
restart.
2017-08-23 10:23:54 +09:30
Christian Decker 406d52042b peer: Add channel to peer and save upon opening a channel
Add the channel to the peer on the two open paths (fundee and funder)
and store it into the database. Currently fails when opening a channel
to a known peer after loading from DB because we attempt to insert a
new peer with the same node_id. Will fix later.
2017-08-23 10:23:54 +09:30
Christian Decker 17173a6322 jsonrpc: Add `connected` field to getpeers
This was implicitly indicated by the presence of the owner field,
making it explicit.
2017-08-23 10:23:54 +09:30
Christian Decker 221fe8c5f3 reconnect: Re-assign peer->netaddr on reconnect, could have changed
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Rusty Russell 91116fe67c gossip: include chain_hash in gossip messages.
As per lightning-rfc change 956e8809d9d1ee87e31b855923579b96943d5e63
"BOLT 7: add chain_hashes values to channel_update and channel_announcment"

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell ffddb91e3e gossipd: hand in chain_hash.
This will be needed for gossip messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell 2b9d3bb0b7 channeld: hand in chain_hash.
This will be needed for gossip messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell b3514d3430 generate-wire.py: generate chain_hash fields as sha256_double.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell 1ecd687eec sphinx: update padding as per latest BOLT 4
BOLT 04: increase max size of onion payload messages (#227)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell fea412dd94 BOLT: Update text to latest commit.
This brings us up to 955e874acc535ab2c74c1cf0eab61896ea4224ff in
https://github.com/lightningnetwork/lightning-rfc

This doesn't actually change anything; the only actual change is held back
for the next commit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-22 14:56:02 +02:00
Rusty Russell 8ffdeea522 daemon/chaintopology: hand full txs to watch_tx()
This is required for onchaind: we want to watch all descendents by default,
as to do otherwise would be racy, which means we need to traverse the outputs
when a tx appears.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 04db39558d htlc_tx: use keyset abstraction.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 4bfaaef408 keyset: abstraction over what keys we need for a specific commitment.
onchaind will need to do similar logic to channeld, so this allows them
to share much more code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell edd27d21b5 lightningd: save last htlc sigs.
This belongs in the db, but until then...

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell b6d7ad0614 lightningd: store the htlc stubs.
These belong in the database, but for now just keep an array.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 893335244d openingd: supply initial tx as well as signature.
And store in peer->last_tx/peer->last_sig like all other places,
that way we broadcast it if we need to.

Note: the removal of tmpctx in funder_channel() is needed because we
use txs[0], which was allocated off tmpctx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 80e28707a3 channeld: send commit tx and signature to master.
This also means we can simply drop it to chain on error.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell c389215a35 closingd: send the tx to master as we negotiate.
We can derive fees from this, and also broadcast at any time without
having to reconstruct it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell dbfac68c3f lightningd: keep last valid tx, and its signature.
This avoids us having to reconstruct it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 8e0c19c76a lightningd: add dev-fail command to inject permenant failure.
A couple of double-free bugs founnd doing this, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 524e56cf56 dev_disconnect: support '*N' syntax for disconnecting on the N'th occurance.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 99581bd709 dev_disconnect: support 'permfail' line to permanently fail peer.
The master daemon checks for this after a subdaemon dies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell f5f8ca5352 onchain/onchain_wire: routines to marshal/unmarshal htlc stubs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell f765e0e846 bip32: marshal/unmarshal routines.
Neater than using a u8 array as we do now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell bcc9ed9aa6 lightningd/htlc_wire: marshal/unmashal bitcoin tx.
We want to keep the last valid tx, and its signature, for broadcast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell a4f290daba htlc_wire: marshal/unmarshal shachain object.
We want to hand it to onchaind.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 217df2d2e8 channeld: fix shutdown when we have to complete HTLCs.
We need to check if we exit after sending a revoke_and_ack, otherwise
channeld ends up getting the closing_signed packet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell f5624af965 lightningd: fix error packet we send due to permanent error.
tal_strdup() doesn't set tal_count(), so we end up sending an ERROR
packet with an empty message.  Wrap this and get it right.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell ce24731da9 lightningd: use internal_error rather than freeing peer.
Should never free peer directly: we need to log error, etc.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 709991fed1 subd: fix case where we replycb calls peer_internal_error().
It frees the sd, destroy_subd() frees sd->conn, then we call io_close(conn).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Christian Decker 6dbd99ddc6 gossip: Fix a race condition between release_peer and fail_peer
There was a race condition that would cause an assertion to segfault
if a call to release_peer was interleaved with a fail_peer. The
release_peer was making the peer non-local, which was then causing the
assertion in fail_peer to fail. Now we just have 3 cases: not found,
local, and non-local.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-17 10:31:55 +09:30
Christian Decker cc5af13e05 wallet: Implement channel_config persistence 2017-08-10 12:34:58 +09:30
Christian Decker ca00ec0bb8 htlc: Include htlc_wire.h in peer_htlcs.h to avoid forward decl 2017-08-10 12:34:58 +09:30
Christian Decker 85d2256898 wallet: Use persistent shachain for remote secrets 2017-08-10 12:34:58 +09:30
Christian Decker 28cc92cd15 gossip: Use bit 3 (mask 0x08) to signal initial routing sync
After quite some back and forth we seem to finally agree on the bit
3 (mask 0x08) to signal optional initial_routing_sync.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-09 10:01:33 +09:30
Rusty Russell 40895e4550 Update to match spec: "BOLT7: Reorder feature bitmaps in order to allow future changes"
aka. a257554456cda98afd1532c302c0e5e84de0455e

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-03 14:26:41 +02:00
Rusty Russell 2b7c091e70 closing: update to match proposal to restart negotiation on reconnect.
This simplifies significantly, as we don't need to remember what we offered.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-02 11:45:30 +09:30
Christian Decker 35847206b2 fix: Allow pings while waiting for the channel to lock
This was causing failures on testnet where confirmations are not
immediate.

Reported-by: Fabrice Drouin @sstone
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-07-22 09:52:14 +09:30
Christian Decker 2bf92c9063 peer: Check localfeatures and globalfeatures against what we support (#195)
We support a number of features already, so failing connections
whenever we see an even bit set is not a good idea. This turned out to
kill our connections to eclair.

Also, the spec says that the LSB / bit 0 is to be counted as index 0, and
therefore even. So we need to check the lower of each 2-bit-tuple not
the higher one.
2017-07-16 10:38:26 +09:30
Christian Decker 7dc693963d script: Consolidate scripts to use pubkey_to_hash160 2017-07-15 10:19:33 +09:30
sstone f371b6df20 sphinx: fix payload amount encoding
it was changed to 64 bits
2017-07-14 19:27:33 +02:00
Christian Decker 83c8c3fc52 opening: Use the correct chainparams to open a channel
We were using the bitcoin genesis blockhash for all networks, which is
not correct, and would result in the open being aborted when talking
to other implementations.

Reported-by: @sstone and @pm47
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-07-12 11:30:23 +09:30
Christian Decker fbc2b65a3d opening: Pass network_index through to openingd
This is needed in order to open channels with the correct
genesis_blockhash.

Reported-By: @sstone
2017-07-12 11:30:23 +09:30
Christian Decker 843e21826a opts: Remove --regtest flag and pass chainparams to bitcoind 2017-07-12 11:30:23 +09:30
Christian Decker cf16b5faea options: Add option to set the network 2017-07-12 11:30:23 +09:30
Christian Decker 9370a3cdd2 opening: Fix switched parameters for towire_accept_channel
Turns out we were serializing two parameters in the wrong order.
2017-07-12 11:00:26 +09:30
Christian Decker 1a1e29a4bc gossip: Re-initiate the broadcast timer upon reconnect
We weren't registering reconnecting peers for broadcasts. Just
starting a timer is enough. Also added an integration test to check
that the gossip sync is being resumed.
2017-07-12 11:00:26 +09:30
Rusty Russell 03be40ea8b lightningd: don't reset balance when we reconnect!
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 8b71ea924a lightningd: don't crash when simply using closingd just for retransmission.
test_closing_negotiation_reconnect (__main__.LightningDTests) ... peer state CLOSINGD_COMPLETE should be CLOSINGD_SIGEXCHANGE

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell a87d50acc9 Makefile: clean daemons on clean.
Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 7d3e074863 Misc feedback minor fixes.
Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 87232ec25c lightningd: new peer state CLOSINGD_COMPLETE.
This is a transitional state, while we're waiting to see the
closing tx onchain (which is To Be Implemented).

The simplest way to do re-transmission is to re-use closingd, and just
disallow any updates.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell c1f21b9fce lightningd/peer_control.c: rename peer->balance.
I made the mistake of thinking it was a [NUM_SIDES] array, but
it's actually our balance, and it's in millisatoshi.  Rename
for clarity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 4223a91842 lightningd: update daemon list.
This is used to check that versions are correct on startup.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 0c5e238a48 lightningd: handle reconnect during shutdown and closing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 0c07856ae4 lightningd/peer_control.c: remove unnecessary reconnected field.
It can be on the stack.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell a827d2b2bb lightningd: add in support for closingd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell e5a8a7502c lightningd/closing: subdaemon for closing negotiation.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 6fa90c926a channeld: exit after shutdown when no more HTLCs.
Ready for the introduction of closingd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 80886cda8a daemon_conn: fix daemon_conn_sync_flush.
We need to set fd to blocking before trying to sync write.  Use
io_fd_block() elsewhere, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 94a33a7d15 channeld: use an enum side not a bool for funder/
This predated our handling of enums.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 7884343c15 tools/generate-wire.py: enums are assignable.
Simplfies their marshalling/unmarshalling.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-12 10:21:16 +09:30
Rusty Russell 9ec804341c channeld: fix intermittant failure on reading init msg from master.
As tracked down by Christian; by setting up the master conn first,
we make the master fd async.  This means that the synchronous read
(in init_channel) can fail with -EAGAIN, and indeed, Christian
saw this when not running under valgrind.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-03 19:43:34 +09:30
Rusty Russell 249085ac10 channeld: reorder functions more logically.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-03 19:43:34 +09:30
Rusty Russell c05f63a4db peer_control: fix double-state transition on reconnect.
We actually don't need to transition if we're reconnecting, and logic
to go to CHANNELD_NORMAL was wrong: we checked that we'd seen funding tx
locked, but not that we'd received a msg from the remote peer.

We need to fix the tests now we no longer double-transition, too.

Fixes: #188
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-07-03 19:43:34 +09:30
Christian Decker f9a2f73072 Addressing feedback from PR #187 2017-06-30 13:42:00 +02:00
Christian Decker 70f4484ba8 channeld: Pass through the agreed feerate_per_kw to channeld
We were erroneously using the fee_base which refers to the fees we
require for forwarding transactions instead of the on-chain fees.
2017-06-30 19:55:17 +09:30
Christian Decker 5c80963577 bug: channel_update parameters slipped by one 2017-06-30 19:55:17 +09:30
Christian Decker 5e1dcea5b8 opening: Passing feerate_per_kw through to master 2017-06-30 19:55:17 +09:30
Christian Decker 02aba77698 channel: Passing channel_flags through to channeld 2017-06-30 19:55:17 +09:30
Rusty Russell ac1172c7b0 lightningd: check scriptpubkey in shutdown.
Important: a non-standard one can make the closing tx not propagate.

Drive-by cut&paste message fix, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 4881129f54 peer_fail_permanent: handle take() arg.
We assume this in at least one caller.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell ee9ebbc184 channeld: fix `funding_locked` retransmission using latest revision of BOLT2.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 42c9cb58c7 channeld: rename commit_index to next_index.
This is what it actually is, and makes it clearer when we refer to the
spec.  It's the commitment we're currently updating, which is the next
commitment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell d411b07ba2 channeld: fix make_revocation_msg() to take index of revoke.
It currently takes index + 1 (ie. current commit), which is confusing
in this context.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 1679f2da06 channeld: update to send next commitment number on connection reestablish.
As per latest BOLT revision.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 05b9e0d538 channeld: do connection reestablishment synchronously.
We currently have the problem that the master can send new HTLCs before
we've processed the incoming reestablish message.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell 456fa39380 sync_crypto_write: support take(msg)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell de5bf56ffa opening: update to new open_channel with channel_flags.
While we're there, make the announcement conditional on it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell d492f3872c wire/peer_wire: rename gossip_msg / unknown_msg / unknown_msg_discardable
The next patch includes wire/peer_wire.h and causes a compile error
as lightningd/gossip_control.c defined its own gossip_msg function.

New names are clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-29 00:08:21 +02:00
Rusty Russell fc4e2d8b48 lightningd: fix typo.
If we're going to die, at least we can do so with dignity.

Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 99289bece7 lightningd/peer_control.c: fix memleak.
Reported-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell ac703ff9dc lgihtningd: remove extraneous whitespace.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell e2c5104aab lightningd: immediately fail HTLCs coming in from shutting down channels.
This can happen even without a protocol violation, if the incoming
update_add_htlc crosses over our outgoing shutdown.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 866ac79850 channeld: shutdown support.
We keep the scriptpubkey to send until after a commitment_signed (or,
in the corner case, if there's no pending commitment).  When we
receive a shutdown from the peer, we pass it up to the master.

It's up to the master not to add any more HTLCs, which works because
we move from CHANNELD_NORMAL to CHANNELD_SHUTTING_DOWN.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell ee00c2c508 lightningd: remove peer->gossip_client_fd
We don't need to keep this around any more: by handing it to
subdaemons we ensure we'll close it if the peer disconnects, and we
also add code to get a new one on reconnection.

Because getting a gossip_fd is async, we re-check the peer state after
it gets back.  This is kind of annoying: perhaps if we were to hand
the reconnected peer through gossipd (with a flag to immediately
return it) we could get the gossip fd that way and unify the paths?

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 4185153d81 gossipd: interface to get a client gossip_fd for a reconnect.
At the moment, master simply keeps the gossip fd open when peer
disconnects.  That's inefficient, and wrong anyway (it may want a
complete new sync, or may not, but we'll currently send all the
messages including stale ones).

This interface will be required for restart anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell ea52b0a8ff openingd: pass back gossip fd on completion.
For the moment, master still keeps a dup of gossip_fd, but that's the next
patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 2ff50107ac lightningd/hsm_control: use a simple fd for HSM.
Now we're always sync, just use an fd.  Put the hsm_sync_read() helper
here, too, and do HSM init sync which makes things much simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 6e59f85666 subd: expose raw API for getting a single fd to a subdaemon.
We're going to use this for the HSM.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 4ef3e9e3fb lightningd/peer_control: get announcement signature from HSM synchronously.
We temporarily play games with the hsm fd; those will go away once
we're done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell d1d84a81d2 lightningd/new_connection: get handshake fd from HSM synchronously.
We temporarily play games with the hsm fd; those will go away once
we're done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 7213312f57 lightningd: remove fd from struct peer.
With no async calls left, we can just use a stack variable for the fd.

And we're now *always* in the hands of some daemon, unless we're
disconnected, so owner is only NULL in that case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell f2d4309add lightningd/subd: explicit failure reply support.
We had a terrible hack in gossip when a peer didn't exist.  Formalize
a pattern when code+200 is a failure (with no fds passed), and use it
here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 31ff5a49f4 lightningd/peer_control: remove cs from struct peer.
Now we're not async, we don't need to pack it into struct peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 232a182b25 lightningd: get fd from HSM synchronously.
This means there's no GETTING_HSMFD state at all any more.  We
temporarily play games with the hsm fd; those will go away once we're
done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 77620ea06f lightningd: get funding signature from HSM synchronously.
This means there's no GETTING_SIG_FROM_HSM state at all any more.  We
temporarily play games with the hsm fd; those will go away once we're
done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 40ce29beac wire_sync_write: support take()
We often want it to free the message after writing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Christian Decker 938ab67a01 wallet: move can_spend to wallet.c and json_addfunds to walletrpc.c 2017-06-23 16:02:20 +09:30
Christian Decker d6656358b5 wallet: Add bip32_base to the wallet struct
Wallet should really be the container for anything bip32 related, so
I'd like to slowly wean off of `ld->bip32_base` in favor of
`ld->wallet->bip32_base`
2017-06-23 16:02:20 +09:30
Christian Decker 8cdab9da1e wallet: Move json_newaddr to wallet.c 2017-06-23 16:02:20 +09:30
Christian Decker 8c4f381afb hsm: Sign withdrawal transactions 2017-06-23 16:02:20 +09:30
Christian Decker a775b52941 wallet: Return change satoshis when selecting coins
We'd be computing them later most of the time anyway.
2017-06-23 16:02:20 +09:30
Christian Decker 3f594172b2 hsm: Add messages for withdrawal signature requests 2017-06-23 16:02:20 +09:30
Christian Decker 479036604b wallet: Implement withdrawal transaction generation
This will be used by both the master daemon and the HSM to
generate (hopefully) identical transactions for signature and
broadcast.
2017-06-23 16:02:20 +09:30
Christian Decker 7738bccf42 wallet: Move coin-selection primitives to wallet
We'll re-use them a few times so having them at a central location is
nice. We also fix a bug that was unreserving UTXO entries upon free,
instead of promoting them to being spent.
2017-06-23 16:02:20 +09:30
Christian Decker 40165ba6d5 script: Use pkh to construct p2pkh output scripts
So far we always needed to know the public key, which was not the case
for addresses that we don't own. Moving the hashing outside of the
script construction allows us to send to arbitrary addresses. I also
added the hash computation to the pubkey primitives.
2017-06-23 16:02:20 +09:30
Rusty Russell 773d2c09e1 lightningd: push incoming HTLCs into SENT_REMOVE_HTLC upon outgoing resolution.
When we get a fail/fulfill on an outgoing HTLC, we tell the correspoding
incoming HTLC about it.  But if that peer is disconnected, we don't.

The better solution is to copy the preimage/malformed/failmessage and mark
the incoming HTLC as resolved.  This is done most simply by marking it
SENT_REMOVE_HTLC, which will work in the database case as well.

channeld now re-transmits appropriately when it gets started with an HTLC
in that state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell 9e941e26ed channeld: increment remote commit_index before receiving revoke.
This matches what the master does: increments commit index when we send
commit_sig.  Thus if we restart at that point, we match.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30
Rusty Russell d95adf7f33 subd: hand through fatal messages as well to callback.
This matters in one case: channeld receiving a bad message is a
permenant failure, whereas losing a connection is transient.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-23 09:29:42 +09:30