Commit Graph

187 Commits

Author SHA1 Message Date
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
Rusty Russell 77a89bcf2b watch: indicate which input of tx is spend the watch txo.
If we generate a tx which spends a heap of TXOs (eg. steal
transaction), we'll need this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 9eabab78ab utils: tal_hexstr() helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 6f2cb72c27 daemon/test: don't generate tiny dust HTLCs in testing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:28:49 +09:30
Rusty Russell 7614e513bd commit_tx: keep permutation map.
This lets us map the HTLCs back to outputs when txs occur onchain.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 76cb195ea1 daemon: dev-disconnect command.
This lets one end experience a disconnect without the other noticing.
Good for testing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell faed0ef736 daemon/test: use config file rather than long cli args.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 4bbb86ae30 daemon: clean up test dirs.
Move final helpers out of test-cli/

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 064309df1a peer: signature in commit_info is always valid.
It's given in the packet which creates the new commit_info, so no need to
make it a pointer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 03a538ca02 check-bolt: check that comments in code match the specs.
And fix the mistakes!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 4fe90ee3f5 peer: fix mutual close detection.
Since 43729c6856 (protocol: add output script to close_clearing message.)
the close scripts are not p2sh, but arbitrary.  Fix the close tx matching.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 43729c6856 protocol: add output script to close_clearing message.
We just use a p2sh to a single address for the moment, but that's simply for
non-segwit wallets; we'll pay to whatever the other side specifies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 20:01:52 +09:30
Rusty Russell f24b73124a Remove txid normalization.
Since any transaction with all segregated-witness inputs is non-malleable,
and all our transactions are that, we can remove normalized txids.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 20:01:52 +09:30
Rusty Russell b1700b1a91 protocol: non-HTLC commit tx outputs are p2wpkh
This is changes the payments to either party to be p2wpkh.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:57:35 +09:30
Rusty Russell bd081d219d protocol: anchor output is now witness 2of2.
Rather than p2sh of a 2of2, it's now a version 0 witness program.
This means that the commit transaction input and mutual close
transaction input are both different.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:55:35 +09:30
Rusty Russell 8bd334380e peer: use tip mediantime for CSV timeout.
Using wallclock is gauche (and I saw it fail once in tests), so fix that
FIXME now it's easy.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:52:35 +09:30
Rusty Russell 85554761c8 bitcoind: eliminate wallet routines.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:51:35 +09:30
Rusty Russell b5a6ac26c7 watch: don't hand blockhash, have commit_tx_depth() use get_last_mediantime()
There isn't a single blockhash; we may be on multiple forks.  But the one
caller which cares is commit_tx_depth(), which wants to know if the tx is
spendable yet.  So that uses get_last_mediantime().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:50:35 +09:30
Rusty Russell e3868b11d2 peer: use watch_txo to watch commit_tx outputs.
We really want to do this for HTLCs; we don't do anything useful yet, but
this code replaces the direct call to bitcoind_watch_addr().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:49:35 +09:30
Rusty Russell 7b4de8e445 watch: use chaintopology
Rather than polling for interesting bitcoin txs via importaddress, we use
the chain topology to register our interest directly.x 

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:48:35 +09:30
Rusty Russell 6e39b0a642 chaintopology: get_last_mediantime()
This gets the median time of the block the tx is in.  If there is more
than one (different tips), it gets the last median time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:46:32 +09:30
Rusty Russell e09795d24e chaintopology: get full tx information for each block.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:42:18 +09:30
Rusty Russell e60b1b4efa bitcoind: use correct endianness for block hashes.
Like txids, we need to reverse them.  We didn't, but then we only used them
to pass to/from bitcoind.  We're about to get them from the block header,
so we need to fix that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:41:20 +09:30
Rusty Russell e7b9a913a6 bitcoind: getrawblock helper.
It turns out using the verbose=false parameter, you can get the raw block
from getblock.  Do that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:40:29 +09:30
Rusty Russell 3d9cb81215 watch: express everything in terms of watch_tx and watch_txo.
With segregated witness, we can (in advance!) specify the txid or tx
output we want to watch, so convert to that now.  For the moment it's
done by pretending we have normalized txids; that goes away after the
conversion.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:39:21 +09:30
Rusty Russell 14d722d48d bitcoind: pretend normalized txids are in the block.
This lets us live in a segwit world, before segwit.  It's a shim which we
can remove once we've changed all our outputs.

We need a few more sleeps in our test script, since we've slowed
things down by doing these calls for every tx in every block.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:38:13 +09:30
Rusty Russell 521d3d53ed chaintopology: keep track of the bitcoin block topology.
This allows us to track precise transaction depth ourselves,
particularly in the case of branching.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:37:13 +09:30
Rusty Russell 01e46d154e bitcoind: getchaintips support.
This allows us to sae the block topology in detail.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:36:13 +09:30
Rusty Russell 1d27428aab log: use real time for logs, even if dev-mocktime is used.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:35:13 +09:30
Rusty Russell 45fa89e134 daemon/test/test.sh: neaten state checks.
Better debugging when things go wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-24 19:34:13 +09:30
Rusty Russell 1568774e62 daemon: assert that all anchor inputs are witness.
Otherwise, they're malleable.  We only care about our own anchor:
their anchor is their problem (and they'll probably get away with it).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:47 +09:30
Rusty Russell eb14111cd0 daemon/wallet: supply a p2wpkh address (as P2SH address).
This is an address that bitcoind will happily pay to, but we know it's
a witness output so our inputs to the anchor are immalleable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-12 14:17:46 +09:30