Commit Graph

418 Commits

Author SHA1 Message Date
Rusty Russell 440fec099b peer: expose HTLC state through RPC.
This is much more explicit and clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 04fa3a71a9 peer: use peer->htlcs in JSON getpeers output, instead of cstate array.
We're weaning off the cstate arrays; use the htlc map.  But for the
moment we keep the output basically the same.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 0e78ccca56 daemon: don't allow manual fulfill command until both sides committed.
We had an occasional race where we hadn't gotten the remote revocation
before submitting fulfill (spotted by the HTLC state transition code).

Disallow this, but also add to the json output so we can wait for
an HTLC to be irrevocably committed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 4963f277aa daemon/test: prepare for random-ordered htlcs, cleanup parsing.
If we always remove " from JSON, our parsing becomes simpler; turns
out that we did that in some places, and check()'s eval removed them
from the comparison.

We extract check_balance_single() to check the general balance, then
grep for HTLCs separately.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 1af3428c6c peer: keep a single HTLC map for all htlcs.
Not separate "locally-offered" and "remotely-offered" ones; we can
distinguish them by htlc->state now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 4b5ec85c25 daemon: keep enum htlc_state within struct htlc.
And update the state as HTLCs get moved around.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 22976bdd32 daemon: use HTLC states.
Since we only care about the latest commits, we can simply associate a
state with each HTLC, rather than using queues of HTLCs associated
with each commitment transaction.

This works far better in the context of a database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:45 +09:30
Rusty Russell 60b4543899 peer: fix --port option.
We need SO_REUSEADDR, and we need to memset sockaddr to zero; valgrind
complains for both IPv4 and IPv6, but the invalid sin6_flowinfo causes
the IPv6 bind to fail altogether.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:20 +09:30
Rusty Russell d0285416fb log: ability to log struct rval.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-17 14:46:42 +09:30
Rusty Russell 7b1187d446 check: make (successful) tests less noisy.
Particularly, don't show the output for generating 432 blocks!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-17 14:46:36 +09:30
Rusty Russell 4563ff91e9 lightningd: don't create rpc socket & listening socket until topology known.
We can deref a NULL tip pointer otherwise.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:24 +09:30
Rusty Russell 35f83841da chaintopology: make sure we have a tip before continuing.
We can't service peers until we have some chain topology.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:22 +09:30
Rusty Russell 441d598dca daemon: broadcast transaction when fulfilling onchain.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:19 +09:30
Rusty Russell 8345bff825 cryptopkt: revert ack split in authenticate_packet
We need to remember the transmit order anyway, so a single counter works.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:11 +09:30
Rusty Russell f00ee00fd8 peer: log unexpected packets properly.
And only try to print the contents if they're PKT_ERR.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:10 +09:30
Rusty Russell f086e44358 Fix check-source: reorder includes in alphabetical order.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-20 14:29:53 +09:30
Rusty Russell 6015cedee8 process_estimatefee: fix bad logic.
Refactor the fallback to make it more robust.

Fixes: #28
Reported-by: Jacob <jacobdnd@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-20 11:51:28 +09:30
Rusty Russell 6199b886fa Merge remote-tracking branch 'origin/pr/26' 2016-07-19 12:40:25 +09:30
Rusty Russell 5472f73f9c cryptopkt: update to latest encryption BOLT.
As per lightning-rfc commit b579b16866855da166981192c0f0549517069d4e.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-19 12:37:33 +09:30
Jacob ae52977e42 fixed whitespace 2016-07-07 13:49:01 -05:00
Jacob dc2d83bfbe Replace OpenSSL's RAND_bytes() with libsodium's randombytes_buf() 2016-07-07 13:42:28 -05:00
Jacob 4751522162 Changed openssl's RAND_bytes() to sodium's randombytes_buf() 2016-07-07 13:22:14 -05:00
Rusty Russell df4df8679d chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.

Here's what happens in the log:

	+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
	error message:
	Can't read block from disk
	'

And here's an example problematic getchaintips output:

[
  {
    "height": 419635,
    "hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
    "branchlen": 1,
    "status": "headers-only"
  }, 
  {
    "height": 419634,
    "hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
    "branchlen": 0,
    "status": "active"
  }, 
  {
    "height": 416372,
    "hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
    "branchlen": 1,
    "status": "valid-headers"
  }, 
  {
    "height": 416231,
    "hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
    "branchlen": 1,
    "status": "headers-only"
  }
]

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 13:36:39 +09:30
Rusty Russell 364c2cd2c0 key_from_base58 / pubkey_from_privkey: don't support non-compressed keys.
It just clutters the API, and we don't support them on the wire anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 2a03af4486 Misc minor cleanups.
From doing a code walkthrough with Christian Decker; unnecessary const in
bitcoin/tx.c, an erroneous FIXME, a missing comment, and an unused struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell a613d8d1fb peer: make id a pointer, NULL until we know peer's ID.
Much better than undefined, and testing for NULL is better than
testing for STATE_INIT.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 7e6dc28f70 htlc: store which side created the HTLC.
This is generally redundant, since HTLC pointer is in that side's
commit_info, but makes HTLC completely self-contained.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fbe15bdce2 peer: remove unused struct members.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f2d835522c bitcoin: implement sig_valid.
Update libsecp256k1 has a normalize function, which allows us to test
if the signature was in low-S form.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell c649f62e43 libsecp256k1: update.
Add Makefile target update-secp256k1, and run it.

The only API change is that len is now an IN-OUT parameter to serialization
functions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 42bed80145 pubkey: don't carry around DER encoding.
We just wrap the libsecp256k1 structure, and convert to DER on demand.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 69cb158edd base58, script, protobuf_convert: don't use temporary secp256k1 context.
We use libsecp256k1 to convert signatures to DER; we were creating a
temporary one, but we really should be handing the one we have in dstate
through.  This does that, everywhere.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell a35055c699 log: keep dstate pointer.
We need its secpctx pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 08a910b06e BOLT: fee requirements update.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 3de6ca3d0b BOLT: update comment to indicate HTLC id must be completely unique.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell ffeab49ed3 htlc_add: only limit is on receiving side.
BOLT has been updated, so update code and comments.  The receiving
side check is sufficient, as the limit is per-offerer, and that's the
only way the HTLCs get back to the offerer's side.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fa7934dfe3 htlc: implement deadline as per BOLT.
Thus a node MUST estimate the deadline for successful redemption for
  each HTLC it offers.  A node MUST NOT offer a HTLC after this
  deadline, and MUST fail the connection if an HTLC which it offered is
  in either node's current commitment transaction past this deadline.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 45cac95331 log: logging for HTLCs.
The most complex thing we've logged yet, so we extract the core of the
log_struct_ function to call it multiple times.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell db72f33988 log: simplify union.
A well-placed cast avoids requirement for const and non-const members.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell e877febf43 daemon: allow fulfill command for onchain HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 25d99b91bd daemon: fail any HTLCs which we weren't committed to if we drop to chain.
There's a corner case where they had it in their commit tx, in which
case we can't fail the HTLC until our commit tx has won.  Again, we
use dstate->config.min_htlc_expiry.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell caf50c9ae4 peer: handle fulfull and fail onchain.
We now need to use bitcoin_witness_htlc with the r value, so that API
is updated to take 'struct rval' or 'struct sha256'.

We use the nc->delay amount (ie. dstate->config.min_htlc_expiry) to
wait for a timeout refund to be buried before "failing" upstream.
This should probably be made into a clearer parameter rather than
overloading this one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>


Header from folded patch 'dont-use-peer-nc-in-onchain-code.patch':

peer: Don't use peer->nc->delay for onchain case.

Use the config var directly.  We should be freeing peer->nc when the
connection dies anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 4f1a1ec044 BOLT: update for latest onchain draft.
It now notes that node may already know the redemption preimage,
update to fix.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b8571c1ac8 watch: make it easier for them to self-delete.
Rather than keeping a pointer so they can free themselves, make it
explicit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 3ba25dd994 htlc: keep rval (if known).
This makes struct htlc a complete object, containing its own information.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 69a8ea2ad9 daemon: pay command.
This is the command an actual user would use: it figures out the fee
and route, and pays it if it can.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 21a29d9b4d daemon: fix bug when we close two peers simulatneously.
If a block triggers two peers to close, we ran io_break() on both of them; the
second overrode the first and we didn't end up freeing that one.

Rather than chase such bugs in future, simply iterate to see if any
peers need freeing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 31a5de644a daemon: route fulfill back.
As soon as an HTLC we offered is fulfilled, fulfill the HTLC which
caused it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 21fe4fbba7 daemon: route failures back.
When an we offered HTLC fails, we fail the HTLC which caused it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 2aa8fe8df1 daemon: route payments onwards.
If the fee is high enough, we offer it onwards.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f994a44827 daemon/peer: keep our own node connection information.
Note that the base fee is in millisatoshi, the proportional fee is
in microsatoshi per satoshi. ie. 1,000,000 means charge 1 satoshi for
every satoshi carried.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 37b269f53e daemon: link HTLCs together.
Most HTLCs we offer are triggered by an incoming HTLC from a different
peer.  Save this "source" htlc, so we can fail/fulfill it when we
fail/fulfill this one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell cc4fc4b668 daemon: use htlc pointers everywhere.
No more copies!

I tried changing the cstate->side[].htlcs to htlc_map rather than a
simple pointer array, but we rely on those array indices heavily for
permutation mapping, and it turned into a major rewrite (especially
for the steal case).

Eventually, we're going to want to reconstruct the commit info for
older commit txs rather than keeping all the permutation and
per-commit-info HTLC information in memory, so we can do the work
then.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 67ac2d2081 daemon: code for HTLC maps.
We currently keep copies of HTLCs in each commit_info structure, but
that's redundant.  Keep per-peer per-direction maps of HTLCs, then we can
just throw pointers around (next patch).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell fecd91ab2a Move funding.[ch] to daemon/channel.[ch].
It's a more logical name, and a more logical place.  We change
"funding" to "channel" in the remaining exposed symbols, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b2fdc86740 daemon: check and use routing info in HTLC packet.
We only support being the end node for the moment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f4f0c1aa18 daemon: add dev-routefail command.
We're about to change the code so that if it can't route, it will fail
the HTLC.  The current low-level tests will hate this, so have a dev switch
to turn that off.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell bf3acfab62 daemon/htlc: including routing information.
This is the logical place for it to belong: with the HTLC.  For the manually-created
HTLCs, we create a simple one-hop route.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 4902907628 daemon: onion routing API
No meat; it doesn't encrypt as yet, but the API to wrap/unwrap routes is here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f06855ff6c daemon: extract newhtlc code into common function.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell b1ac490bea daemon: add-route RPC command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 26a03acfd0 daemon: routing infrastructure.
Simple routing topology, and ability to ask about the cheapest route.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 27da8f77b5 daemon: expose find_peer(), rename other to find_peer_json().
This is the more normal case; find by ID.  The low-level json commands are
really just for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell a3375516e5 daemon: don't ever use timeouts in seconds, always blocks,
The protocol still supports both, but we now only support blocks.

It's hard to do risk management with timeouts in seconds, given block
variance.  This is also signficantly simpler, as HTLC timeouts are
always fired in response to blocks, not wall-clock times.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell c1e1299249 daemon: always log error packets we create.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 5296b7f9a0 log: add structure logging.
Uses a gcc extension (cast to union) for typechecking, but that can be
removed for compilers which don't support it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 04b3e8f91d daemon: use siphash for hashes.
Remove ccan/hash (aka Jenkins lookup3) altogether.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell ba9df99770 daemon: wire in payment.
This actually uses the accept-payment data to make payments.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 5027410ab1 daemon: payment infrastructure.
A new 'accept-payment' command tells the node to fulfill HTLCs using
the R value if the amount is correct.  It's not wired in yet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell f1af56fcee daemon: save acked changes, so we can process them when confirmed on both sides.
We need to know when changes are fully committed by both sides:
1) For their HTLC_ADDs, this is when we can fulfill/fail/route.
2) For their HTLC_FAILs, this is when we can fail incoming.

For HTLC_FULFULL we don't need to wait: as soon as we know the preimage
we can propogate it.

For the moment, we simply log and assert; acting on it comes later.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell eeb9b9de84 funding: make funding_htlc_by_id() return pointer, not offset.
While the pointer is only valid until the funding changes, that's also
true of the offset; and a pointer has a convenient "not found"
sentinal value.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 156d1be9ed daemon: struct rval to represent r values.
We've been stuffing these into sha256s, but they're actually nonces.
Create a new structure for that for clarity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 1abc676c4f daemon: always take packets into pkt_in, then have it demultiplex.
Cleanly separates packet handling functions, and the weird transition cases.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell b4f0d32b09 daemon: always terminate waiting manual update command on failure.
We missed some cases, resulting in hanging commands.  Just check whenever
we fail.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 1444d407f3 daemon: remove normal operation loop from state.c
It's now in its own little state machine, which is more typesafe.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell bc5800b1c1 state: remove unused fields from union input
And make the add/fail/fulfill arg a pointer to a union htlc_staging
directly, removing struct htlc_progress.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 50bffd67c2 daemon: don't save invalid commit signatures.
We don't want anyone to think the commitment tx is signed, so only
assign ci->sig after validation.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 43db90391a daemon: fix case where commit has nothing to do.
Prevents assert() triggering in queue_pkt_commit().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell b820f5a0dd Makefile: fix submodule.
Christian noted that missing daemon/jsmn/jsmn.h didn't trigger the git
submodule weirdness.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 9046597344 daemon: tighten check for invalid IDs.
We don't have to, but helps debugging.  Language in latest rev of BOLT#2
has been tightened too (aa2e1919de0826beaf92e0b3b441a6ab9fce6261)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 11:59:15 +09:30
Rusty Russell 3928cbe30e daemon: fail connection if preimage isn't valid for shachain.
This means they generated it wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-06-25 14:20:15 +09:30
Rusty Russell 6bbd05c001 Merge remote-tracking branch 'origin/pr/25' 2016-06-25 14:19:57 +09:30
Rusty Russell 2436207a7a test: detect segwit correctly, assume master branch.
Segwit was merged, but the strings changed between there and segwit4
(also, my BIP9 patch changed the output).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-06-25 14:13:08 +09:30
sstone be1a230ae8 fix formatting issues 2016-06-23 17:11:10 +02:00
sstone 07e6ed0c16 save received revocation preimages in shachain.
make sure that preimages are generated in reverse order.
2016-06-23 16:38:35 +02:00
Rusty Russell 49ebed737d daemon/test: test differential fees.
This would have revealed the previous breakage (and I tested that!),
plus now we test negotiate on closing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-30 11:18:46 +09:30
sstone 385c2a5905 setup_first_commit: initialize their remote commit with their commit fee rate 2016-05-27 16:53:01 +02:00
Rusty Russell 40b550a9d2 daemon: don't log bogus warning on failed anchors.
We no longer get bitcoind to manage our transactions for us, so we don't
need to -zapwallettxs when an anchor fails.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 773a6088e4 daemon: reorder and collapse functions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 400d415172 daemon: remove pending input and command queues.
we don't use them any more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 889db659c5 test: test commands during commit phase.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 4110376e87 daemon: allow commands during commit.
There's no real reason to avoid commands for the next commit; this has
the benefit that we can remove the infrastructure to queue commands.
The only exceptions are the commit command and the opening phase.

We still only allow one commit at a time, but that's mainly run off a
timer which can try again later.  For the JSONRPC API used for
testing, we can simply fail the commit if one is in progress.

For opening we add an explicit peer_open_complete() call in place of
using the command infrastructure.

Commands are now outside the state machine altogether: we simply have
it return the new state instead of the command status.  The JSONRPC
functions can also now run commands directly.

This removes the idea of "peercond" as well: you can simply examine
the states to determine whether an input is valid.  There are
fine-grained helpers for this now, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell d4862938c8 daemon: move unacked queue into commit_info struct.
We're about to allow changes while we're waiting for a commit ack.
This means we can't have a single "unacked changes" queue; when we
receive the revocation reply, we need to apply the unacked changes
known at the time we sent the commit, not any we've created since
then.

Note that we still only have a single staged_commit; we never have two
outstanding commits, since for simplicity we will still block
following update_commit pending the reply to the current one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell 36fc62ab81 test: add --crash option to cause nodes to crash dump on test failure.
This causes full logs to be dropped in "crash.log".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell ca9d775f9a lightning-cli: don't print junk on error.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:25 +09:30
Rusty Russell f662424b7b protocol: don't sign initial commitment for non-funder.
As per lightning-rfc commit b8469aa758a1a7ebbd73c987be3e5207b778241b
("re-protocol: don't hand signature to non-funding side initially.")

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5188b14c7c daemon: fix unwatch anchor depth.
We still need to watch the anchor output in this case: that's what
makes us handle the commit transcction we broadcast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell b9d4f7c0ab daemon: dev-output command.
Useful for controlling conversations between two nodes, by
blocking one's output.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 97bc4ed0cb daemon/test: test mutual close with outstanding HTLCS.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5aed0e12f8 daemon: remove closing states from state machine.
We already removed the on-chain states, now we remove the "clearing" state
(which wasn't fully implemented anyway).

This turns into two smaller state machines: one for clearing, which
still allows HTLCs to be failed and fulfilled, and one for mutual
close negotiation which only allows close_signature messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 4c63845969 daemon: make funding directions local-centric.
Previous to this, we kept the remote side's 'struct channel_state'
backwards: peer->remote.commit->cstate.side[OURS] was their HTLCs,
and [THEIRS] was our HTLCs.  This made some things easier, but was
horrible for readability.

This inverts things so we keep track of the remote side's state from
our point of view: [OURS] is ours, [THEIRS] is theirs.  Which makes
much more sense.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 148bd793cd daemon/test: test overlapping commits.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 45a6f81c3c protocol: remove ack fields.
As per lightning-rfc commit 8ee09e749990a11fa53bea03d5961cfde4be4616,
we remove the acks from the protocol now they're no longer needed (and
all the infrastructure).

We also place the commit number in the commit_info where it logically
belongs, removing it from the peer struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 2bf43f1ebd daemon: handle HTLC as per BOLT #2 algorithm.
From BOLT#2 (rev 8ee09e749990a11fa53bea03d5961cfde4be4616):

   Thus each node (conceptually) tracks:
...
   3. Two *unacked changesets*: one for the local commitment (their proposals) and one for the remote (our proposals)
   4. Two *acked changesets*: one for the local commitment (our proposals, acknowledged) and one for the remote (their proposals, acknowledged).

   (Note that an implementation MAY optimize this internally, for
   example, pre-applying the changesets in some cases).

In our case, we apply the unacked changes immediately into
staging_cstate, and save them in an unacked_changes array.  That array
gets applied to staging_cstate as soon as it's acked (we only allow
one outstanding update_commit, so we only need one array).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell cf7a7a7273 funding: use sides[OURS/THEIRS] instead of a and b.
This is a little clearer, and handling arrays is easier than separate
variables.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 84f5a82eea daemon: use "local" and "remote" instead of "us" and "them".
This is the language used in BOLT#2; be consistent.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 311ae9b4d8 daemon: remove unused functions.
These were left over from when the state machine handled onchain
transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 0ceee86098 daemon: don't allow fulfill/fail on uncommitted HTLCs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell f43cc72d6a Makefile: add generated packet names.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell fabdcaf62b daemon: close correctly when guest fails.
Otherwise we can receive another packet.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 5a2a9126c3 daemon: don't allow new RPC commands when peer closing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:24:39 +09:30
Rusty Russell b6339af195 daemon: remove unused CMD_REQUEUE.
This was a remnant of the old code which used to alternate priority
for making changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:23:39 +09:30
Rusty Russell c710a64ccf Makefile: support for suppressing valgrind.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-17 13:49:54 +09:30
Rusty Russell 35d1b13cde daemon: commit outstanding changes via timer.
While useful for testing, it doesn't make sense to have an explicit commit
command; we should commit whenever there are outstanding changes.

We have a 10ms timer to allow limited batching, however.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:30:11 +09:30
Rusty Russell fe1ba96332 daemon: time options use opt_time.
Currently this mean --bitcoin-poll; we're going to change the other time
options to block heights anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:29:12 +09:30
Rusty Russell 6c7facfd2d daemon: time option support.
No need to have all times in seconds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:28:17 +09:30
Rusty Russell 82c2325467 timeout: make all timers one-shot.
It's closer to what we want, and simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-10 06:26:09 +09:30
Rusty Russell 4beaedfa49 daemon/test: clean up Makefile.
This means mkae tells us directly what failed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:47 +09:30
Rusty Russell 604122e787 daemon: permute input in steal transaction.
This is just generally good practice.  All our other txs are single-input,
so we've not needed to permute inputs before.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 16:22:11 +09:30
Rusty Russell 082eaf406e daemon/test: fix spending check.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 15:56:47 +09:30
Rusty Russell 09de557c69 daemon: update limit to reflect latest BOLT#2.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 14:42:52 +09:30
Rusty Russell d6603adc2f daemon/test: test stealing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 12:00:30 +09:30
Rusty Russell 5e40b264dd daemon: handle cheating.
As per onchain.md.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:55 +09:30
Rusty Russell 4cb6cd1f90 daemon: handle information leak.
As per onchain.md; log that an unknown spend occurred, and weep.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:56:32 +09:30
Rusty Russell 1be98d860f daemon: handle receipt of error packets.
If it's all printable, print it, otherwise dump hex.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:55:52 +09:30
Rusty Russell e1c6f2d630 daemon: dev-signcommit command to sign the current commit tx.
Do not use this!  We use it to test stealing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-06 11:54:49 +09:30
Rusty Russell effcb73a48 daemon/test: speed up and clean up tests.
They would sometimes fail under load, if using valgrind.  Retry
properly rather than relying on random sleeps.  Also, takes "make
check" running time here from 1m31.864s to 1m16.872s.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:36 +09:30
Rusty Russell 7ae15401dd daemon: log all state changes the same way.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-05 14:23:34 +09:30
Rusty Russell 623eec4068 daemon/test: test unilateral close.
We use dev-disconnect to convince one node the other has disconnected
(but not vice versa), to get deterministic behaviour.  We do this with
one HTLC outstanding, to test the HTLC timeout path.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell 0f9889f2c6 state: trim unused states.
Now we never enter the state machine if we're dealing with on-chain
transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f29a6043d2 daemon: open-code handling of on-chain states.
Once we see an on-chain tx, we ignore the state machine and handle it
as per the onchain.md draft.  This specifies a *resolution* for each
output, and we're done when they're irrevocable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:14:22 +09:30
Rusty Russell f6b36b9be3 Revert: 064309df1a "peer: signature in commit_info is always valid."
It's not quite true: if we offer the anchor, we have a commitinfo
without their signature yet.  So make it a pointer again.  Since we
always allocate struct commit_info with talz, it starts as a NULL
pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:13:50 +09:30
Rusty Russell c94f77fd14 forever-confirms: warn if less than 100.
BOLT #onchain considers 100 the minimum depth to be "irrevocable".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:50 +09:30
Rusty Russell 5eb50345ae daemon: implement bitcoin_htlc_timeout()
This is called when an HTLC times out, and we need to send it back to
ourselves.  We also adjust the locktime, since in practice we should
refuse an HTLC less than our locktime.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:12:47 +09:30
Rusty Russell a4125313ce peer: helpers to extract a given HTLC from commit_info.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:47 +09:30
Rusty Russell 1b49d2afa6 chaintopology: always track txs we broadcast ourselves.
This is inefficient, but it means we always know the tx depth.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:11:16 +09:30
Rusty Russell 7d4d2977b6 watch: depth callback is always >= 0
We don't report conflicts, just depths.  So we report 0 if it's in a
main chain which loses to another, otherwise it's always positive.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:39 +09:30
Rusty Russell 4e102ccfcf chaintopology: simply track txids, not watches.
This is less efficient, but simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:10:37 +09:30
Rusty Russell 57ec0397ad chaintopology: only deal with the main chain.
Since bitcoind doesn't propagate non-main chains, there's little point
trying to be smart when we see them.  This simplifies things immensely.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:06:19 +09:30
Rusty Russell 17167704a6 daemon: handle bitcoin transaction re-broadcasting.
It's primitive, but we re-broadcast any txs not included in the main
chain every time the tip moves.  We only track transactions we are
watching, but that turns out to cover every transaction we generate
anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell c1dc0e54fa daemon/bitcoind: bitcoind_sendrawtx to send hex string.
This can fail.  Real cases include both sides dumping their commitment
txs in testing (only one can succeed).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 8fe2ba0ab3 daemon/bitcoind: don't eliminate output on non-zero exit status.
Abort if caller wasn't expecting it, otherwise save the result.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell 29db78ea43 daemon: always call state machine through state_single.
That logs transitions, and we're about to patch it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-04 16:03:10 +09:30
Rusty Russell e18aea8d71 daemon: simplify fee calculation for spends of our own commit tx.
It's not exact, but faking a sig, measuring length, then resigning was
neither exact nor pretty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell 12b37d5f80 daemon: fix logic which determines how anchor output was spent.
We watch the anchor output, and separate it into different cases.
This is simpler with segwit (txids are known before sigs), but we also
had missed the case of our own commit transaction spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell eb5d832963 state: don't spend the "to-us" output from their commit tx.
There's no reason to, it's a simple p2wpkh to our key.

We still spend the "to-us" from our commit tx, since it could be
theoretically be stolen by the revocation value, and it's a complex
p2wsh which a normal wallet won't have the information to spend.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:50 +09:30
Rusty Russell c4713a6ed5 state: use htlc_onchain structure
Turns out that we want to pass information about the commit info, the
HTLC number and (sometimes) the R value, so create a struct for that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell bccd10c6d8 daemon: don't code check for cleared commits in the main loop, use pending queue.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 33a477d65d daemon: add pending input queue.
You can't re-enter the state machine from a callback, so this allows you
to queue an input for when it returns.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell c94c495257 daemon: allow multiple watches on the same tx.
This turns out to make life easier for watching HTLC timeouts (we just
place a new watch for each HTLC).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30