2016-01-21 21:14:25 +01:00
|
|
|
#include "bitcoind.h"
|
2016-04-24 12:20:35 +02:00
|
|
|
#include "chaintopology.h"
|
2016-01-21 21:15:27 +01:00
|
|
|
#include "close_tx.h"
|
2016-01-21 21:14:27 +01:00
|
|
|
#include "commit_tx.h"
|
2016-01-21 21:15:28 +01:00
|
|
|
#include "controlled_time.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "cryptopkt.h"
|
2016-08-18 06:55:14 +02:00
|
|
|
#include "db.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "dns.h"
|
2016-01-21 21:14:27 +01:00
|
|
|
#include "find_p2sh_out.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "jsonrpc.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
2016-01-21 21:14:25 +01:00
|
|
|
#include "names.h"
|
2016-08-18 06:55:14 +02:00
|
|
|
#include "netaddr.h"
|
2016-06-30 01:38:10 +02:00
|
|
|
#include "onion.h"
|
2016-08-18 06:53:45 +02:00
|
|
|
#include "output_to_htlc.h"
|
2016-08-18 06:53:45 +02:00
|
|
|
#include "packets.h"
|
2016-06-30 01:38:11 +02:00
|
|
|
#include "pay.h"
|
2016-06-28 23:19:20 +02:00
|
|
|
#include "payment.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "peer.h"
|
2016-05-06 08:52:11 +02:00
|
|
|
#include "permute_tx.h"
|
2016-05-26 07:55:24 +02:00
|
|
|
#include "protobuf_convert.h"
|
2016-03-31 08:43:20 +02:00
|
|
|
#include "pseudorand.h"
|
2016-08-18 06:53:46 +02:00
|
|
|
#include "remove_dust.h"
|
2016-06-30 01:38:10 +02:00
|
|
|
#include "routing.h"
|
2016-01-21 21:14:25 +01:00
|
|
|
#include "secrets.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include "state.h"
|
2016-01-21 21:15:27 +01:00
|
|
|
#include "timeout.h"
|
2016-05-06 04:24:49 +02:00
|
|
|
#include "utils.h"
|
2016-04-12 05:37:04 +02:00
|
|
|
#include "wallet.h"
|
2016-01-21 21:14:27 +01:00
|
|
|
#include <bitcoin/base58.h>
|
|
|
|
#include <bitcoin/script.h>
|
2016-01-21 21:14:25 +01:00
|
|
|
#include <bitcoin/tx.h>
|
|
|
|
#include <ccan/array_size/array_size.h>
|
2016-05-04 08:41:47 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/io/io.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/list/list.h>
|
2016-05-04 08:44:22 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/noerr/noerr.h>
|
2016-01-21 21:15:28 +01:00
|
|
|
#include <ccan/ptrint/ptrint.h>
|
2016-01-21 21:15:27 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2016-01-21 21:15:05 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
#include <errno.h>
|
2016-01-21 21:15:27 +01:00
|
|
|
#include <inttypes.h>
|
2016-08-18 06:55:08 +02:00
|
|
|
#include <netdb.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_connecting {
|
|
|
|
/* This owns us, so we're freed after command_fail or command_success */
|
|
|
|
struct command *cmd;
|
|
|
|
const char *name, *port;
|
2016-04-12 05:37:04 +02:00
|
|
|
struct anchor_input *input;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
static bool command_htlc_set_fail(struct peer *peer, struct htlc *htlc,
|
|
|
|
enum fail_error error_code, const char *why);
|
2016-08-18 06:53:45 +02:00
|
|
|
static bool command_htlc_fail(struct peer *peer, struct htlc *htlc);
|
|
|
|
static bool command_htlc_fulfill(struct peer *peer, struct htlc *htlc);
|
|
|
|
static void try_commit(struct peer *peer);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
bool peer_add_their_commit(struct peer *peer,
|
2016-08-18 06:53:45 +02:00
|
|
|
const struct sha256_double *txid, u64 commit_num)
|
|
|
|
{
|
|
|
|
struct their_commit *tc = tal(peer, struct their_commit);
|
|
|
|
tc->txid = *txid;
|
|
|
|
tc->commit_num = commit_num;
|
|
|
|
list_add_tail(&peer->their_commits, &tc->list);
|
2016-08-18 06:55:14 +02:00
|
|
|
|
|
|
|
return db_add_commit_map(peer, txid, commit_num);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* Create a bitcoin close tx, using last signature they sent. */
|
|
|
|
static const struct bitcoin_tx *bitcoin_close(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct bitcoin_tx *close_tx;
|
|
|
|
struct bitcoin_signature our_close_sig;
|
|
|
|
|
|
|
|
close_tx = peer_create_close_tx(peer, peer->closing.their_fee);
|
|
|
|
|
|
|
|
our_close_sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_mutual_close(peer, close_tx, &our_close_sig.sig);
|
|
|
|
|
|
|
|
close_tx->input[0].witness
|
|
|
|
= bitcoin_witness_2of2(close_tx->input,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
peer->closing.their_sig,
|
|
|
|
&our_close_sig,
|
|
|
|
&peer->remote.commitkey,
|
|
|
|
&peer->local.commitkey);
|
|
|
|
|
|
|
|
return close_tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a bitcoin spend tx (to spend our commit's outputs) */
|
|
|
|
static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
|
|
|
|
{
|
|
|
|
u8 *witnessscript;
|
|
|
|
const struct bitcoin_tx *commit = peer->local.commit->tx;
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
unsigned int p2wsh_out;
|
|
|
|
uint64_t fee;
|
|
|
|
|
|
|
|
/* The redeemscript for a commit tx is fairly complex. */
|
|
|
|
witnessscript = bitcoin_redeem_secret_or_delay(peer,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.locktime,
|
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.commit->revocation_hash);
|
|
|
|
|
|
|
|
/* Now, create transaction to spend it. */
|
|
|
|
tx = bitcoin_tx(peer, 1, 1);
|
|
|
|
tx->input[0].txid = peer->local.commit->txid;
|
|
|
|
p2wsh_out = find_p2wsh_out(commit, witnessscript);
|
|
|
|
tx->input[0].index = p2wsh_out;
|
|
|
|
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64,
|
|
|
|
&commit->output[p2wsh_out].amount);
|
|
|
|
|
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx,
|
|
|
|
bitcoin_redeem_single(tx,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey));
|
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
|
|
|
/* Witness length can vary, due to DER encoding of sigs, but we
|
|
|
|
* use 176 from an example run. */
|
|
|
|
assert(measure_tx_cost(tx) == 83 * 4);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
fee = fee_by_feerate(83 + 176 / 4, get_feerate(peer->dstate));
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
|
|
|
if (fee > commit->output[p2wsh_out].amount
|
2016-08-18 06:53:46 +02:00
|
|
|
|| is_dust(commit->output[p2wsh_out].amount - fee))
|
2016-08-18 06:53:45 +02:00
|
|
|
fatal("Amount of %"PRIu64" won't cover fee %"PRIu64,
|
|
|
|
commit->output[p2wsh_out].amount, fee);
|
|
|
|
|
|
|
|
tx->output[0].amount = commit->output[p2wsh_out].amount - fee;
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
|
|
|
|
|
|
|
|
tx->input[0].witness = bitcoin_witness_secret(tx,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
NULL, 0, &sig,
|
|
|
|
witnessscript);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sign and return our commit tx */
|
|
|
|
static const struct bitcoin_tx *bitcoin_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
|
|
|
|
/* Can't be signed already, and can't have scriptsig! */
|
|
|
|
assert(peer->local.commit->tx->input[0].script_length == 0);
|
|
|
|
assert(!peer->local.commit->tx->input[0].witness);
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig.sig);
|
|
|
|
|
|
|
|
peer->local.commit->tx->input[0].witness
|
|
|
|
= bitcoin_witness_2of2(peer->local.commit->tx->input,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
peer->local.commit->sig,
|
|
|
|
&sig,
|
|
|
|
&peer->remote.commitkey,
|
|
|
|
&peer->local.commitkey);
|
|
|
|
|
|
|
|
return peer->local.commit->tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 commit_tx_fee(const struct bitcoin_tx *commit, u64 anchor_satoshis)
|
|
|
|
{
|
|
|
|
uint64_t i, total = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < commit->output_count; i++)
|
|
|
|
total += commit->output[i].amount;
|
|
|
|
|
|
|
|
assert(anchor_satoshis >= total);
|
|
|
|
return anchor_satoshis - total;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
2016-07-01 03:57:57 +02:00
|
|
|
if (peer->id && pubkey_eq(peer->id, id))
|
2016-06-28 23:19:21 +02:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
void debug_dump_peers(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
|
|
|
if (!peer->local.commit
|
|
|
|
|| !peer->remote.commit)
|
|
|
|
continue;
|
2016-08-31 08:36:32 +02:00
|
|
|
log_debug_struct(peer->log, "our cstate: %s",
|
|
|
|
struct channel_state,
|
|
|
|
peer->local.commit->cstate);
|
|
|
|
log_debug_struct(peer->log, "their cstate: %s",
|
|
|
|
struct channel_state,
|
|
|
|
peer->remote.commit->cstate);
|
2016-08-18 06:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
static struct peer *find_peer_json(struct lightningd_state *dstate,
|
2016-03-15 07:37:31 +01:00
|
|
|
const char *buffer,
|
|
|
|
jsmntok_t *peeridtok)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-03-15 07:37:31 +01:00
|
|
|
struct pubkey peerid;
|
|
|
|
|
|
|
|
if (!pubkey_from_hexstr(dstate->secpctx,
|
|
|
|
buffer + peeridtok->start,
|
|
|
|
peeridtok->end - peeridtok->start, &peerid))
|
|
|
|
return NULL;
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
return find_peer(dstate, &peerid);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
static bool peer_uncommitted_changes(const struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-08-26 08:01:19 +02:00
|
|
|
enum feechange_state i;
|
2016-05-26 07:55:25 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (htlc_has(h, HTLC_REMOTE_F_PENDING))
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-26 08:01:19 +02:00
|
|
|
/* Pending feechange we sent, or pending ack of theirs. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(peer->feechanges); i++) {
|
|
|
|
if (!peer->feechanges[i])
|
|
|
|
continue;
|
|
|
|
if (feechange_state_flags(i) & HTLC_REMOTE_F_PENDING)
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
return false;
|
2016-05-26 07:55:25 +02:00
|
|
|
}
|
2016-05-26 07:55:25 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void remote_changes_pending(struct peer *peer)
|
|
|
|
{
|
|
|
|
if (!peer->commit_timer) {
|
|
|
|
log_debug(peer->log, "remote_changes_pending: adding timer");
|
|
|
|
peer->commit_timer = new_reltimer(peer->dstate, peer,
|
|
|
|
peer->dstate->config.commit_time,
|
|
|
|
try_commit, peer);
|
|
|
|
} else
|
|
|
|
log_debug(peer->log, "remote_changes_pending: timer already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_update_complete(struct peer *peer)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
log_debug(peer->log, "peer_update_complete");
|
|
|
|
if (peer->commit_jsoncmd) {
|
|
|
|
command_success(peer->commit_jsoncmd,
|
|
|
|
null_response(peer->commit_jsoncmd));
|
|
|
|
peer->commit_jsoncmd = NULL;
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* Have we got more changes in the meantime? */
|
|
|
|
if (peer_uncommitted_changes(peer)) {
|
|
|
|
log_debug(peer->log, "peer_update_complete: more changes!");
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
void peer_open_complete(struct peer *peer, const char *problem)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-05-26 07:55:25 +02:00
|
|
|
if (problem)
|
|
|
|
log_unusual(peer->log, "peer open failed: %s", problem);
|
2016-08-18 06:55:13 +02:00
|
|
|
else
|
2016-05-26 07:55:25 +02:00
|
|
|
log_debug(peer->log, "peer open complete");
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static bool set_peer_state(struct peer *peer, enum state newstate,
|
|
|
|
const char *caller, bool db_commit)
|
2016-05-05 06:53:34 +02:00
|
|
|
{
|
|
|
|
log_debug(peer->log, "%s: %s => %s", caller,
|
|
|
|
state_name(peer->state), state_name(newstate));
|
|
|
|
peer->state = newstate;
|
2016-08-18 06:55:13 +02:00
|
|
|
|
|
|
|
/* We can only route in normal state. */
|
|
|
|
if (!state_is_normal(peer->state))
|
|
|
|
peer->nc = tal_free(peer->nc);
|
2016-08-18 06:55:14 +02:00
|
|
|
|
|
|
|
if (db_commit)
|
|
|
|
return db_update_state(peer);
|
|
|
|
return true;
|
2016-05-05 06:53:34 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
static void peer_breakdown(struct peer *peer)
|
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
if (peer->commit_jsoncmd) {
|
|
|
|
command_fail(peer->commit_jsoncmd, "peer breakdown");
|
|
|
|
peer->commit_jsoncmd = NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
/* If we have a closing tx, use it. */
|
|
|
|
if (peer->closing.their_sig) {
|
|
|
|
log_unusual(peer->log, "Peer breakdown: sending close tx");
|
|
|
|
broadcast_tx(peer, bitcoin_close(peer));
|
|
|
|
/* If we have a signed commit tx (maybe not if we just offered
|
2016-08-18 06:55:13 +02:00
|
|
|
* anchor, or they supplied anchor, or no outputs to us). */
|
2016-08-18 06:55:14 +02:00
|
|
|
} else if (peer->local.commit && peer->local.commit->sig) {
|
2016-05-04 08:44:22 +02:00
|
|
|
log_unusual(peer->log, "Peer breakdown: sending commit tx");
|
|
|
|
broadcast_tx(peer, bitcoin_commit(peer));
|
2016-05-26 07:55:24 +02:00
|
|
|
} else {
|
2016-05-04 08:44:22 +02:00
|
|
|
log_info(peer->log, "Peer breakdown: nothing to do");
|
2016-05-26 07:55:24 +02:00
|
|
|
/* We close immediately. */
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_CLOSED, __func__, false);
|
|
|
|
db_forget_peer(peer);
|
2016-05-26 07:55:24 +02:00
|
|
|
io_wake(peer);
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* All unrevoked commit txs must have no HTLCs in them. */
|
|
|
|
static bool committed_to_htlcs(const struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
2016-08-18 06:53:45 +02:00
|
|
|
h;
|
2016-08-18 06:53:45 +02:00
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
if (htlc_is_dead(h))
|
2016-08-18 06:53:45 +02:00
|
|
|
continue;
|
|
|
|
return true;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_close(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
/* Tell writer to wrap it up (may have to xmit first) */
|
|
|
|
io_wake(peer);
|
|
|
|
/* We do nothing more. */
|
|
|
|
return io_wait(conn, NULL, io_never, NULL);
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
/* Communication failed: send err (if non-NULL), then dump to chain and close. */
|
|
|
|
static bool peer_comms_err(struct peer *peer, Pkt *err)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
queue_pkt_err(peer, err);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* FIXME: Save state here? */
|
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, __func__, false);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer_breakdown(peer);
|
2016-06-28 23:19:20 +02:00
|
|
|
return false;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt, const char *where)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
log_unusual(peer->log, "%s: received unexpected pkt %u (%s) in %s",
|
|
|
|
where, pkt->pkt_case, pkt_name(pkt->pkt_case),
|
|
|
|
state_name(peer->state));
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
if (pkt->pkt_case != PKT__PKT_ERROR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Check packet for weird chars. */
|
|
|
|
for (p = pkt->error->problem; *p; p++) {
|
|
|
|
if (cisprint(*p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p = tal_hexstr(peer, pkt->error->problem,
|
|
|
|
strlen(pkt->error->problem));
|
|
|
|
log_unusual(peer->log, "Error pkt (hex) %s", p);
|
|
|
|
tal_free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_unusual(peer->log, "Error pkt '%s'", pkt->error->problem);
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* Unexpected packet received: stop listening, start breakdown procedure. */
|
2016-08-18 06:55:13 +02:00
|
|
|
static bool peer_received_unexpected_pkt(struct peer *peer, const Pkt *pkt,
|
|
|
|
const char *where)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
peer_unexpected_pkt(peer, pkt, where);
|
2016-06-28 23:19:20 +02:00
|
|
|
return peer_comms_err(peer, pkt_err_unexpected(peer, pkt));
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
void set_htlc_rval(struct peer *peer,
|
|
|
|
struct htlc *htlc, const struct rval *rval)
|
|
|
|
{
|
|
|
|
assert(!htlc->r);
|
2016-08-31 07:21:41 +02:00
|
|
|
assert(!htlc->fail);
|
2016-08-18 06:55:13 +02:00
|
|
|
htlc->r = tal_dup(htlc, struct rval, rval);
|
2016-08-18 06:55:14 +02:00
|
|
|
db_htlc_fulfilled(peer, htlc);
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
void set_htlc_fail(struct peer *peer,
|
|
|
|
struct htlc *htlc, const void *fail, size_t len)
|
|
|
|
{
|
|
|
|
assert(!htlc->r);
|
|
|
|
assert(!htlc->fail);
|
|
|
|
htlc->fail = tal_dup_arr(htlc, u8, fail, len, 0);
|
|
|
|
db_htlc_failed(peer, htlc);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void route_htlc_onwards(struct peer *peer,
|
|
|
|
struct htlc *htlc,
|
|
|
|
u64 msatoshis,
|
|
|
|
const BitcoinPubkey *pb_id,
|
2016-08-18 06:55:08 +02:00
|
|
|
const u8 *rest_of_route,
|
|
|
|
const struct peer *only_dest)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
struct peer *next;
|
2016-08-31 07:21:41 +02:00
|
|
|
struct htlc *newhtlc;
|
|
|
|
enum fail_error error_code;
|
|
|
|
const char *err;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
if (!only_dest) {
|
|
|
|
log_debug_struct(peer->log, "Forwarding HTLC %s",
|
|
|
|
struct sha256, &htlc->rhash);
|
|
|
|
log_add(peer->log, " (id %"PRIu64")", htlc->id);
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
if (!proto_to_pubkey(peer->dstate->secpctx, pb_id, &id)) {
|
|
|
|
log_unusual(peer->log,
|
|
|
|
"Malformed pubkey for HTLC %"PRIu64, htlc->id);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, BAD_REQUEST_400,
|
|
|
|
"Malformed pubkey");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
next = find_peer(peer->dstate, &id);
|
|
|
|
if (!next || !next->nc) {
|
|
|
|
log_unusual(peer->log, "Can't route HTLC %"PRIu64": no %speer ",
|
|
|
|
htlc->id, next ? "ready " : "");
|
|
|
|
log_add_struct(peer->log, "%s", struct pubkey, &id);
|
|
|
|
if (!peer->dstate->dev_never_routefail)
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, NOT_FOUND_404,
|
|
|
|
"Unknown peer");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
if (only_dest && next != only_dest)
|
|
|
|
return;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* Offered fee must be sufficient. */
|
|
|
|
if (htlc->msatoshis - msatoshis < connection_fee(next->nc, msatoshis)) {
|
|
|
|
log_unusual(peer->log,
|
|
|
|
"Insufficient fee for HTLC %"PRIu64
|
|
|
|
": %"PRIi64" on %"PRIu64,
|
|
|
|
htlc->id, htlc->msatoshis - msatoshis,
|
|
|
|
msatoshis);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, PAYMENT_REQUIRED_402,
|
|
|
|
"Insufficent fee");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
log_debug_struct(peer->log, "HTLC forward to %s",
|
|
|
|
struct pubkey, next->id);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* This checks the HTLC itself is possible. */
|
2016-08-31 07:21:41 +02:00
|
|
|
err = command_htlc_add(next, msatoshis,
|
|
|
|
abs_locktime_to_blocks(&htlc->expiry)
|
|
|
|
- next->nc->delay,
|
|
|
|
&htlc->rhash, htlc, rest_of_route,
|
|
|
|
&error_code, &newhtlc);
|
|
|
|
if (err)
|
|
|
|
command_htlc_set_fail(peer, htlc, error_code, err);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static void their_htlc_added(struct peer *peer, struct htlc *htlc,
|
|
|
|
struct peer *only_dest)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
RouteStep *step;
|
|
|
|
const u8 *rest_of_route;
|
|
|
|
struct payment *payment;
|
|
|
|
|
|
|
|
if (abs_locktime_is_seconds(&htlc->expiry)) {
|
|
|
|
log_unusual(peer->log, "HTLC %"PRIu64" is in seconds", htlc->id);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, BAD_REQUEST_400,
|
|
|
|
"bad locktime");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (abs_locktime_to_blocks(&htlc->expiry) <=
|
|
|
|
get_block_height(peer->dstate) + peer->dstate->config.min_htlc_expiry) {
|
|
|
|
log_unusual(peer->log, "HTLC %"PRIu64" expires too soon:"
|
|
|
|
" block %u",
|
|
|
|
htlc->id, abs_locktime_to_blocks(&htlc->expiry));
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, BAD_REQUEST_400,
|
|
|
|
"expiry too soon");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (abs_locktime_to_blocks(&htlc->expiry) >
|
|
|
|
get_block_height(peer->dstate) + peer->dstate->config.max_htlc_expiry) {
|
|
|
|
log_unusual(peer->log, "HTLC %"PRIu64" expires too far:"
|
|
|
|
" block %u",
|
|
|
|
htlc->id, abs_locktime_to_blocks(&htlc->expiry));
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, BAD_REQUEST_400,
|
|
|
|
"expiry too far");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
step = onion_unwrap(peer, htlc->routing, tal_count(htlc->routing),
|
|
|
|
&rest_of_route);
|
|
|
|
if (!step) {
|
|
|
|
log_unusual(peer->log, "Bad onion, failing HTLC %"PRIu64,
|
|
|
|
htlc->id);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, BAD_REQUEST_400,
|
|
|
|
"invalid onion");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (step->next_case) {
|
|
|
|
case ROUTE_STEP__NEXT_END:
|
2016-08-18 06:55:08 +02:00
|
|
|
if (only_dest)
|
|
|
|
return;
|
2016-08-18 06:53:45 +02:00
|
|
|
payment = find_payment(peer->dstate, &htlc->rhash);
|
|
|
|
if (!payment) {
|
|
|
|
log_unusual(peer->log, "No payment for HTLC %"PRIu64,
|
|
|
|
htlc->id);
|
|
|
|
log_add_struct(peer->log, " rhash=%s",
|
|
|
|
struct sha256, &htlc->rhash);
|
|
|
|
if (unlikely(!peer->dstate->dev_never_routefail))
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc,
|
|
|
|
UNAUTHORIZED_401,
|
|
|
|
"unknown rhash");
|
2016-08-18 06:53:45 +02:00
|
|
|
goto free_rest;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (htlc->msatoshis != payment->msatoshis) {
|
|
|
|
log_unusual(peer->log, "Short payment for HTLC %"PRIu64
|
|
|
|
": %"PRIu64" not %"PRIu64 " satoshi!",
|
|
|
|
htlc->id,
|
|
|
|
htlc->msatoshis,
|
|
|
|
payment->msatoshis);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc,
|
|
|
|
UNAUTHORIZED_401,
|
|
|
|
"incorrect amount");
|
2016-08-18 06:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info(peer->log, "Immediately resolving HTLC %"PRIu64,
|
|
|
|
htlc->id);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
set_htlc_rval(peer, htlc, &payment->r);
|
2016-08-18 06:53:45 +02:00
|
|
|
command_htlc_fulfill(peer, htlc);
|
|
|
|
goto free_rest;
|
|
|
|
|
|
|
|
case ROUTE_STEP__NEXT_BITCOIN:
|
|
|
|
route_htlc_onwards(peer, htlc, step->amount, step->bitcoin,
|
2016-08-18 06:55:08 +02:00
|
|
|
rest_of_route, only_dest);
|
2016-08-18 06:53:45 +02:00
|
|
|
goto free_rest;
|
|
|
|
default:
|
|
|
|
log_info(peer->log, "Unknown step type %u", step->next_case);
|
2016-08-31 07:21:41 +02:00
|
|
|
command_htlc_set_fail(peer, htlc, VERSION_NOT_SUPPORTED_505,
|
|
|
|
"unknown step type");
|
2016-08-18 06:53:45 +02:00
|
|
|
goto free_rest;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_rest:
|
|
|
|
tal_free(rest_of_route);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void our_htlc_failed(struct peer *peer, struct htlc *htlc)
|
|
|
|
{
|
2016-08-31 07:21:41 +02:00
|
|
|
assert(htlc_owner(htlc) == LOCAL);
|
|
|
|
if (htlc->src) {
|
|
|
|
set_htlc_fail(htlc->src->peer, htlc->src,
|
|
|
|
htlc->fail, tal_count(htlc->fail));
|
2016-08-18 06:53:45 +02:00
|
|
|
command_htlc_fail(htlc->src->peer, htlc->src);
|
2016-08-31 07:21:41 +02:00
|
|
|
} else
|
2016-08-31 08:34:59 +02:00
|
|
|
complete_pay_command(peer->dstate, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 12:44:57 +02:00
|
|
|
static void our_htlc_fulfilled(struct peer *peer, struct htlc *htlc)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
if (htlc->src) {
|
2016-08-18 06:55:13 +02:00
|
|
|
set_htlc_rval(htlc->src->peer, htlc->src, htlc->r);
|
2016-08-18 06:53:45 +02:00
|
|
|
command_htlc_fulfill(htlc->src->peer, htlc->src);
|
|
|
|
} else {
|
2016-08-31 08:34:59 +02:00
|
|
|
complete_pay_command(peer->dstate, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:42:57 +02:00
|
|
|
/* FIXME: Slow! */
|
|
|
|
static struct htlc *htlc_with_source(struct peer *peer, struct htlc *src)
|
|
|
|
{
|
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
|
|
|
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (h->src == src)
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
/* peer has come back online: re-send any we have to send to them. */
|
|
|
|
static void retry_all_routing(struct peer *restarted_peer)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
2016-08-30 12:43:57 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-08-18 06:55:08 +02:00
|
|
|
|
2016-08-30 12:42:57 +02:00
|
|
|
/* Look for added htlcs from other peers which need to go here. */
|
2016-08-18 06:55:08 +02:00
|
|
|
list_for_each(&restarted_peer->dstate->peers, peer, list) {
|
|
|
|
if (peer == restarted_peer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (h->state != RCVD_ADD_ACK_REVOCATION)
|
|
|
|
continue;
|
2016-08-30 12:42:57 +02:00
|
|
|
if (htlc_with_source(peer, h))
|
|
|
|
continue;
|
2016-08-18 06:55:08 +02:00
|
|
|
their_htlc_added(peer, h, restarted_peer);
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 12:43:57 +02:00
|
|
|
|
|
|
|
/* Catch any HTLCs which are fulfilled, but the message got reset
|
|
|
|
* by reconnect. */
|
|
|
|
for (h = htlc_map_first(&restarted_peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&restarted_peer->htlcs, &it)) {
|
2016-08-31 07:21:43 +02:00
|
|
|
if (h->state != RCVD_ADD_ACK_REVOCATION)
|
|
|
|
continue;
|
|
|
|
if (h->r)
|
2016-08-30 12:43:57 +02:00
|
|
|
command_htlc_fulfill(restarted_peer, h);
|
2016-08-31 07:21:43 +02:00
|
|
|
else if (h->fail)
|
|
|
|
command_htlc_fail(restarted_peer, h);
|
2016-08-30 12:43:57 +02:00
|
|
|
}
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
static bool adjust_cstate_side(struct channel_state *cstate,
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc *h,
|
|
|
|
enum htlc_state old, enum htlc_state new,
|
|
|
|
enum htlc_side side)
|
|
|
|
{
|
|
|
|
int oldf = htlc_state_flags(old), newf = htlc_state_flags(new);
|
|
|
|
bool old_committed, new_committed;
|
|
|
|
|
|
|
|
/* We applied changes to staging_cstate when we first received
|
|
|
|
* add/remove packet, so we could make sure it was valid. Don't
|
|
|
|
* do that again. */
|
|
|
|
if (old == SENT_ADD_HTLC || old == RCVD_REMOVE_HTLC
|
|
|
|
|| old == RCVD_ADD_HTLC || old == SENT_REMOVE_HTLC)
|
2016-08-31 08:36:32 +02:00
|
|
|
return true;
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
old_committed = (oldf & HTLC_FLAG(side, HTLC_F_COMMITTED));
|
|
|
|
new_committed = (newf & HTLC_FLAG(side, HTLC_F_COMMITTED));
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (old_committed && !new_committed) {
|
|
|
|
if (h->r)
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fulfill_htlc(cstate, h);
|
2016-08-18 06:53:46 +02:00
|
|
|
else
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fail_htlc(cstate, h);
|
2016-08-18 06:53:46 +02:00
|
|
|
} else if (!old_committed && new_committed) {
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!cstate_add_htlc(cstate, h, false)) {
|
|
|
|
log_broken_struct(h->peer->log,
|
|
|
|
"Cannot afford htlc %s",
|
|
|
|
struct htlc, h);
|
|
|
|
log_add_struct(h->peer->log, " channel state %s",
|
|
|
|
struct channel_state, cstate);
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
2016-08-31 08:36:32 +02:00
|
|
|
return true;
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* We apply changes to staging_cstate when we first PENDING, so we can
|
|
|
|
* make sure they're valid. So here we change the staging_cstate on
|
|
|
|
* the revocation receive (ie. when acked). */
|
2016-08-31 08:36:32 +02:00
|
|
|
static bool adjust_cstates(struct peer *peer, struct htlc *h,
|
2016-08-18 06:53:46 +02:00
|
|
|
enum htlc_state old, enum htlc_state new)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
2016-08-31 08:36:32 +02:00
|
|
|
return adjust_cstate_side(peer->remote.staging_cstate, h, old, new,
|
|
|
|
REMOTE)
|
|
|
|
&& adjust_cstate_side(peer->local.staging_cstate, h, old, new,
|
|
|
|
LOCAL);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
static void adjust_cstate_fee_side(struct channel_state *cstate,
|
|
|
|
const struct feechange *f,
|
|
|
|
enum feechange_state old,
|
|
|
|
enum feechange_state new,
|
|
|
|
enum htlc_side side)
|
|
|
|
{
|
|
|
|
/* We applied changes to staging_cstate when we first received
|
|
|
|
* feechange packet, so we could make sure it was valid. Don't
|
|
|
|
* do that again. */
|
|
|
|
if (old == SENT_FEECHANGE || old == RCVD_FEECHANGE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Feechanges only ever get applied to the side which created them:
|
|
|
|
* ours gets applied when they ack, theirs gets applied when we ack. */
|
|
|
|
if (side == LOCAL && new == RCVD_FEECHANGE_REVOCATION)
|
|
|
|
adjust_fee(cstate, f->fee_rate);
|
|
|
|
else if (side == REMOTE && new == SENT_FEECHANGE_REVOCATION)
|
|
|
|
adjust_fee(cstate, f->fee_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adjust_cstates_fee(struct peer *peer, const struct feechange *f,
|
|
|
|
enum feechange_state old,
|
|
|
|
enum feechange_state new)
|
|
|
|
{
|
|
|
|
adjust_cstate_fee_side(peer->remote.staging_cstate, f, old, new, REMOTE);
|
|
|
|
adjust_cstate_fee_side(peer->local.staging_cstate, f, old, new, LOCAL);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void check_both_committed(struct peer *peer, struct htlc *h)
|
|
|
|
{
|
|
|
|
if (!htlc_has(h, HTLC_ADDING) && !htlc_has(h, HTLC_REMOVING))
|
|
|
|
log_debug(peer->log,
|
|
|
|
"Both committed to %s of %s HTLC %"PRIu64 "(%s)",
|
|
|
|
h->state == SENT_ADD_ACK_REVOCATION
|
|
|
|
|| h->state == RCVD_ADD_ACK_REVOCATION ? "ADD"
|
|
|
|
: h->r ? "FULFILL" : "FAIL",
|
|
|
|
htlc_owner(h) == LOCAL ? "our" : "their",
|
|
|
|
h->id, htlc_state_name(h->state));
|
|
|
|
|
|
|
|
switch (h->state) {
|
|
|
|
case RCVD_REMOVE_ACK_REVOCATION:
|
|
|
|
/* If it was fulfilled, we handled it immediately. */
|
2016-08-31 07:21:41 +02:00
|
|
|
if (h->fail)
|
2016-08-18 06:53:46 +02:00
|
|
|
our_htlc_failed(peer, h);
|
|
|
|
break;
|
|
|
|
case RCVD_ADD_ACK_REVOCATION:
|
2016-08-18 06:55:08 +02:00
|
|
|
their_htlc_added(peer, h, NULL);
|
2016-08-18 06:53:46 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
struct htlcs_table {
|
2016-08-18 06:53:46 +02:00
|
|
|
enum htlc_state from, to;
|
|
|
|
};
|
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
struct feechanges_table {
|
|
|
|
enum feechange_state from, to;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *changestates(struct peer *peer,
|
|
|
|
const struct htlcs_table *table,
|
|
|
|
size_t n,
|
|
|
|
const struct feechanges_table *ftable,
|
|
|
|
size_t n_ftable,
|
|
|
|
bool db_commit)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
|
|
|
bool changed = false;
|
2016-08-26 08:01:19 +02:00
|
|
|
size_t i;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (h->state == table[i].from) {
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!adjust_cstates(peer, h,
|
|
|
|
table[i].from, table[i].to))
|
|
|
|
return "accounting error";
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!htlc_changestate(h, table[i].from,
|
|
|
|
table[i].to, db_commit))
|
|
|
|
return "database error";
|
2016-08-18 06:53:46 +02:00
|
|
|
check_both_committed(peer, h);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 08:01:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n_ftable; i++) {
|
|
|
|
struct feechange *f = peer->feechanges[ftable[i].from];
|
|
|
|
if (!f)
|
|
|
|
continue;
|
|
|
|
adjust_cstates_fee(peer, f, ftable[i].from, ftable[i].to);
|
|
|
|
if (!feechange_changestate(peer, f,
|
|
|
|
ftable[i].from, ftable[i].to,
|
|
|
|
db_commit))
|
|
|
|
return "database error";
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send an `update_commit` message which does
|
|
|
|
* not include any updates.
|
|
|
|
*/
|
|
|
|
if (!changed)
|
|
|
|
return "no changes made";
|
|
|
|
return NULL;
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the io loop while we're negotiating closing tx. */
|
|
|
|
static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
const CloseSignature *c = pkt->close_signature;
|
|
|
|
struct bitcoin_tx *close_tx;
|
|
|
|
struct bitcoin_signature theirsig;
|
|
|
|
|
|
|
|
assert(peer->state == STATE_MUTUAL_CLOSING);
|
|
|
|
|
|
|
|
if (pkt->pkt_case != PKT__PKT_CLOSE_SIGNATURE)
|
2016-08-18 06:55:13 +02:00
|
|
|
return peer_received_unexpected_pkt(peer, pkt, __func__);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
log_info(peer->log, "closing_pkt_in: they offered close fee %"PRIu64,
|
|
|
|
c->close_fee);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sender MUST set `close_fee` lower than or equal to the fee of the
|
|
|
|
* final commitment transaction, and MUST set `close_fee` to an even
|
|
|
|
* number of satoshis.
|
|
|
|
*/
|
|
|
|
if ((c->close_fee & 1)
|
|
|
|
|| c->close_fee > commit_tx_fee(peer->remote.commit->tx,
|
|
|
|
peer->anchor.satoshis)) {
|
|
|
|
return peer_comms_err(peer, pkt_err(peer, "Invalid close fee"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Don't accept tiny fee at all? */
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
... otherwise it SHOULD propose a
|
|
|
|
value strictly between the received `close_fee` and its
|
|
|
|
previously-sent `close_fee`.
|
|
|
|
*/
|
|
|
|
if (peer->closing.their_sig) {
|
|
|
|
/* We want more, they should give more. */
|
|
|
|
if (peer->closing.our_fee > peer->closing.their_fee) {
|
|
|
|
if (c->close_fee <= peer->closing.their_fee)
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Didn't increase close fee"));
|
|
|
|
} else {
|
|
|
|
if (c->close_fee >= peer->closing.their_fee)
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Didn't decrease close fee"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The receiver MUST check `sig` is valid for the close
|
|
|
|
* transaction with the given `close_fee`, and MUST fail the
|
|
|
|
* connection if it is not. */
|
|
|
|
theirsig.stype = SIGHASH_ALL;
|
|
|
|
if (!proto_to_signature(peer->dstate->secpctx, c->sig, &theirsig.sig))
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Invalid signature format"));
|
|
|
|
|
|
|
|
close_tx = peer_create_close_tx(peer, c->close_fee);
|
|
|
|
if (!check_tx_sig(peer->dstate->secpctx, close_tx, 0,
|
|
|
|
NULL, 0,
|
|
|
|
peer->anchor.witnessscript,
|
|
|
|
&peer->remote.commitkey, &theirsig))
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Invalid signature"));
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
tal_free(peer->closing.their_sig);
|
|
|
|
peer->closing.their_sig = tal_dup(peer,
|
|
|
|
struct bitcoin_signature, &theirsig);
|
|
|
|
peer->closing.their_fee = c->close_fee;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->closing.sigs_in++;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_update_their_closing(peer)) {
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Database error"));
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (peer->closing.our_fee != peer->closing.their_fee) {
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* If the receiver agrees with the fee, it SHOULD reply with a
|
|
|
|
* `close_signature` with the same `close_fee` value,
|
|
|
|
* otherwise it SHOULD propose a value strictly between the
|
|
|
|
* received `close_fee` and its previously-sent `close_fee`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Adjust our fee to close on their fee. */
|
|
|
|
u64 sum;
|
|
|
|
|
|
|
|
/* Beware overflow! */
|
|
|
|
sum = (u64)peer->closing.our_fee + peer->closing.their_fee;
|
|
|
|
|
|
|
|
peer->closing.our_fee = sum / 2;
|
|
|
|
if (peer->closing.our_fee & 1)
|
|
|
|
peer->closing.our_fee++;
|
|
|
|
|
|
|
|
log_info(peer->log, "accept_pkt_close_sig: we change to %"PRIu64,
|
|
|
|
peer->closing.our_fee);
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->closing.closing_order = peer->order_counter++;
|
2016-08-18 06:55:14 +02:00
|
|
|
|
|
|
|
if (!db_update_our_closing(peer)) {
|
|
|
|
return peer_comms_err(peer,
|
|
|
|
pkt_err(peer, "Database error"));
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_pkt_close_signature(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note corner case: we may *now* agree with them! */
|
|
|
|
if (peer->closing.our_fee == peer->closing.their_fee) {
|
|
|
|
log_info(peer->log, "accept_pkt_close_sig: we agree");
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* Once a node has sent or received a `close_signature` with
|
|
|
|
* matching `close_fee` it SHOULD close the connection and
|
|
|
|
* SHOULD sign and broadcast the final closing transaction.
|
|
|
|
*/
|
|
|
|
broadcast_tx(peer, bitcoin_close(peer));
|
2016-06-28 23:19:20 +02:00
|
|
|
return false;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
return true;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* We can get update_commit in both normal and shutdown states. */
|
2016-08-18 06:53:45 +02:00
|
|
|
static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
Pkt *err;
|
2016-08-18 06:55:14 +02:00
|
|
|
const char *errmsg;
|
2016-08-18 06:53:46 +02:00
|
|
|
struct sha256 preimage;
|
2016-08-18 06:53:46 +02:00
|
|
|
struct commit_info *ci;
|
2016-08-18 06:55:13 +02:00
|
|
|
bool to_them_only;
|
2016-08-18 06:53:45 +02:00
|
|
|
/* FIXME: We can actually merge these two... */
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct htlcs_table commit_changes[] = {
|
2016-08-18 06:53:45 +02:00
|
|
|
{ RCVD_ADD_REVOCATION, RCVD_ADD_ACK_COMMIT },
|
|
|
|
{ RCVD_REMOVE_HTLC, RCVD_REMOVE_COMMIT },
|
|
|
|
{ RCVD_ADD_HTLC, RCVD_ADD_COMMIT },
|
|
|
|
{ RCVD_REMOVE_REVOCATION, RCVD_REMOVE_ACK_COMMIT }
|
|
|
|
};
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct feechanges_table commit_feechanges[] = {
|
|
|
|
{ RCVD_FEECHANGE_REVOCATION, RCVD_FEECHANGE_ACK_COMMIT },
|
|
|
|
{ RCVD_FEECHANGE, RCVD_FEECHANGE_COMMIT }
|
|
|
|
};
|
|
|
|
static const struct htlcs_table revocation_changes[] = {
|
2016-08-18 06:53:45 +02:00
|
|
|
{ RCVD_ADD_ACK_COMMIT, SENT_ADD_ACK_REVOCATION },
|
|
|
|
{ RCVD_REMOVE_COMMIT, SENT_REMOVE_REVOCATION },
|
|
|
|
{ RCVD_ADD_COMMIT, SENT_ADD_REVOCATION },
|
|
|
|
{ RCVD_REMOVE_ACK_COMMIT, SENT_REMOVE_ACK_REVOCATION }
|
|
|
|
};
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct feechanges_table revocation_feechanges[] = {
|
|
|
|
{ RCVD_FEECHANGE_ACK_COMMIT, SENT_FEECHANGE_ACK_REVOCATION },
|
|
|
|
{ RCVD_FEECHANGE_COMMIT, SENT_FEECHANGE_REVOCATION }
|
|
|
|
};
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
ci = new_commit_info(peer, peer->local.commit->commit_num + 1);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_start_transaction(peer))
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send an `update_commit` message which does
|
|
|
|
* not include any updates.
|
|
|
|
*/
|
2016-08-26 08:01:19 +02:00
|
|
|
errmsg = changestates(peer,
|
|
|
|
commit_changes, ARRAY_SIZE(commit_changes),
|
|
|
|
commit_feechanges, ARRAY_SIZE(commit_feechanges),
|
|
|
|
true);
|
2016-08-18 06:55:14 +02:00
|
|
|
if (errmsg) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "%s", errmsg);
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Create new commit info for this commit tx. */
|
|
|
|
ci->revocation_hash = peer->local.next_revocation_hash;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node MUST apply all local acked and unacked
|
|
|
|
* changes except unacked fee changes to the local commitment
|
|
|
|
*/
|
|
|
|
/* (We already applied them to staging_cstate as we went) */
|
|
|
|
ci->cstate = copy_cstate(ci, peer->local.staging_cstate);
|
|
|
|
ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash,
|
2016-08-18 06:55:13 +02:00
|
|
|
ci->cstate, LOCAL, &to_them_only);
|
2016-08-18 06:53:45 +02:00
|
|
|
bitcoin_txid(ci->tx, &ci->txid);
|
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
log_debug(peer->log, "Check tx %"PRIu64" sig", ci->commit_num);
|
|
|
|
log_add_struct(peer->log, " for %s", struct channel_state, ci->cstate);
|
2016-08-18 06:55:14 +02:00
|
|
|
log_add_struct(peer->log, " (txid %s)", struct sha256_double, &ci->txid);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* If the commitment transaction has only a single output which pays
|
|
|
|
* to the other node, `sig` MUST be unset. Otherwise, a sending node
|
|
|
|
* MUST apply all remote acked and unacked changes except unacked fee
|
|
|
|
* changes to the remote commitment before generating `sig`.
|
|
|
|
*/
|
|
|
|
if (!to_them_only)
|
|
|
|
ci->sig = tal(ci, struct bitcoin_signature);
|
|
|
|
|
|
|
|
err = accept_pkt_commit(peer, pkt, ci->sig);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node MUST apply all local acked and unacked changes
|
|
|
|
* except unacked fee changes to the local commitment, then it MUST
|
|
|
|
* check `sig` is valid for that transaction.
|
|
|
|
*/
|
2016-08-18 06:55:13 +02:00
|
|
|
if (ci->sig && !check_tx_sig(peer->dstate->secpctx,
|
|
|
|
ci->tx, 0,
|
|
|
|
NULL, 0,
|
|
|
|
peer->anchor.witnessscript,
|
|
|
|
&peer->remote.commitkey,
|
2016-08-18 06:55:14 +02:00
|
|
|
ci->sig)) {
|
|
|
|
db_abort_transaction(peer);
|
2016-08-18 06:53:45 +02:00
|
|
|
return pkt_err(peer, "Bad signature");
|
2016-08-18 06:55:14 +02:00
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Switch to the new commitment. */
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_free(peer->local.commit);
|
2016-08-18 06:53:45 +02:00
|
|
|
peer->local.commit = ci;
|
2016-08-18 06:55:14 +02:00
|
|
|
peer->local.commit->order = peer->order_counter++;
|
|
|
|
|
|
|
|
if (!db_new_commit_info(peer, OURS, NULL)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "Database error");
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
peer_get_revocation_hash(peer, ci->commit_num + 1,
|
|
|
|
&peer->local.next_revocation_hash);
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->their_commitsigs++;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Now, send the revocation. */
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* We have their signature on the current one, right? */
|
2016-08-18 06:55:13 +02:00
|
|
|
assert(to_them_only || peer->local.commit->sig);
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(peer->local.commit->commit_num > 0);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
errmsg = changestates(peer,
|
|
|
|
revocation_changes, ARRAY_SIZE(revocation_changes),
|
|
|
|
revocation_feechanges,
|
|
|
|
ARRAY_SIZE(revocation_feechanges),
|
|
|
|
true);
|
2016-08-18 06:55:14 +02:00
|
|
|
if (errmsg) {
|
|
|
|
log_broken(peer->log, "queue_pkt_revocation: %s", errmsg);
|
2016-08-31 08:36:32 +02:00
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "Database error");
|
2016-08-18 06:55:14 +02:00
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
peer_get_revocation_preimage(peer, peer->local.commit->commit_num - 1,
|
|
|
|
&preimage);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Fire off timer if this ack caused new changes */
|
|
|
|
if (peer_uncommitted_changes(peer))
|
2016-08-18 06:53:45 +02:00
|
|
|
remote_changes_pending(peer);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_commit_transaction(peer))
|
|
|
|
return pkt_err(peer, "Database error");
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
queue_pkt_revocation(peer, &preimage, &peer->local.next_revocation_hash);
|
2016-08-18 06:53:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static Pkt *handle_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
struct htlc *htlc;
|
|
|
|
Pkt *err;
|
|
|
|
|
|
|
|
err = accept_pkt_htlc_add(peer, pkt, &htlc);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(htlc->state == RCVD_ADD_HTLC);
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT offer `amount_msat` it cannot pay for in
|
|
|
|
* the remote commitment transaction at the current `fee_rate` (see
|
|
|
|
* "Fee Calculation" ). A node SHOULD fail the connection if
|
|
|
|
* this occurs.
|
|
|
|
*/
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!cstate_add_htlc(peer->local.staging_cstate, htlc, true)) {
|
|
|
|
u64 id = htlc->id;
|
|
|
|
log_broken_struct(peer->log, "They cannot afford htlc %s",
|
|
|
|
struct htlc, htlc);
|
|
|
|
log_add_struct(peer->log, " cstate %s",
|
|
|
|
struct channel_state,
|
|
|
|
peer->local.staging_cstate);
|
2016-08-18 06:53:45 +02:00
|
|
|
tal_free(htlc);
|
2016-08-31 08:36:32 +02:00
|
|
|
return pkt_err(peer, "Cannot afford htlc %"PRIu64, id);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pkt *handle_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
struct htlc *htlc;
|
|
|
|
Pkt *err;
|
|
|
|
|
|
|
|
err = accept_pkt_htlc_fail(peer, pkt, &htlc);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fail_htlc(peer->local.staging_cstate, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ... and the receiving node MUST add the HTLC fulfill/fail
|
|
|
|
* to the unacked changeset for its local commitment.
|
|
|
|
*/
|
2016-08-18 06:55:14 +02:00
|
|
|
htlc_changestate(htlc, SENT_ADD_ACK_REVOCATION, RCVD_REMOVE_HTLC, false);
|
2016-08-18 06:53:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pkt *handle_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
struct htlc *htlc;
|
|
|
|
Pkt *err;
|
2016-08-18 06:55:08 +02:00
|
|
|
bool was_already_fulfilled;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
/* Reconnect may mean HTLC was already fulfilled. That's OK. */
|
|
|
|
err = accept_pkt_htlc_fulfill(peer, pkt, &htlc, &was_already_fulfilled);
|
2016-08-18 06:53:45 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* We can relay this upstream immediately. */
|
2016-08-18 06:55:08 +02:00
|
|
|
if (!was_already_fulfilled)
|
2016-08-30 12:44:57 +02:00
|
|
|
our_htlc_fulfilled(peer, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ... and the receiving node MUST add the HTLC fulfill/fail
|
|
|
|
* to the unacked changeset for its local commitment.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fulfill_htlc(peer->local.staging_cstate, htlc);
|
2016-08-18 06:55:14 +02:00
|
|
|
htlc_changestate(htlc, SENT_ADD_ACK_REVOCATION, RCVD_REMOVE_HTLC, false);
|
2016-08-18 06:53:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:03:30 +02:00
|
|
|
static void set_feechange(struct peer *peer, u64 fee_rate,
|
|
|
|
enum feechange_state state)
|
|
|
|
{
|
|
|
|
/* If we already have a feechange for this commit, simply update it. */
|
|
|
|
if (peer->feechanges[state]) {
|
|
|
|
log_debug(peer->log, "Feechange: fee %"PRIu64" to %"PRIu64,
|
|
|
|
peer->feechanges[state]->fee_rate,
|
|
|
|
fee_rate);
|
|
|
|
peer->feechanges[state]->fee_rate = fee_rate;
|
|
|
|
} else {
|
|
|
|
log_debug(peer->log, "Feechange: New fee %"PRIu64, fee_rate);
|
|
|
|
peer->feechanges[state] = new_feechange(peer, fee_rate, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pkt *handle_pkt_feechange(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
u64 feerate;
|
|
|
|
Pkt *err;
|
|
|
|
|
|
|
|
err = accept_pkt_update_fee(peer, pkt, &feerate);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST NOT send a `fee_rate` which it could not
|
|
|
|
* afford (see "Fee Calculation), were it applied to the receiving
|
|
|
|
* node's commitment transaction. The receiving node SHOULD fail the
|
|
|
|
* connection if this occurs.
|
|
|
|
*/
|
|
|
|
if (!can_afford_feerate(peer->local.staging_cstate, feerate, REMOTE))
|
|
|
|
return pkt_err(peer, "Cannot afford feerate %"PRIu64,
|
|
|
|
feerate);
|
|
|
|
|
|
|
|
set_feechange(peer, feerate, RCVD_FEECHANGE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static Pkt *handle_pkt_revocation(struct peer *peer, const Pkt *pkt,
|
|
|
|
enum state next_state)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
Pkt *err;
|
2016-08-18 06:55:14 +02:00
|
|
|
const char *errmsg;
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct htlcs_table changes[] = {
|
2016-08-18 06:53:45 +02:00
|
|
|
{ SENT_ADD_COMMIT, RCVD_ADD_REVOCATION },
|
|
|
|
{ SENT_REMOVE_ACK_COMMIT, RCVD_REMOVE_ACK_REVOCATION },
|
|
|
|
{ SENT_ADD_ACK_COMMIT, RCVD_ADD_ACK_REVOCATION },
|
|
|
|
{ SENT_REMOVE_COMMIT, RCVD_REMOVE_REVOCATION }
|
|
|
|
};
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct feechanges_table feechanges[] = {
|
|
|
|
{ SENT_FEECHANGE_COMMIT, RCVD_FEECHANGE_REVOCATION },
|
|
|
|
{ SENT_FEECHANGE_ACK_COMMIT, RCVD_FEECHANGE_ACK_REVOCATION }
|
|
|
|
};
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
err = accept_pkt_revocation(peer, pkt);
|
2016-08-18 06:53:45 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The receiver of `update_revocation`... MUST add the remote
|
|
|
|
* unacked changes to the set of local acked changes.
|
|
|
|
*/
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_start_transaction(peer))
|
|
|
|
return pkt_err(peer, "database error");
|
2016-08-26 08:01:19 +02:00
|
|
|
errmsg = changestates(peer, changes, ARRAY_SIZE(changes),
|
|
|
|
feechanges, ARRAY_SIZE(feechanges), true);
|
2016-08-18 06:55:14 +02:00
|
|
|
if (errmsg) {
|
|
|
|
log_broken(peer->log, "accept_pkt_revocation: %s", errmsg);
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "failure accepting update_revocation: %s",
|
|
|
|
errmsg);
|
|
|
|
}
|
|
|
|
if (!db_save_shachain(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
if (!db_update_next_revocation_hash(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
if (!set_peer_state(peer, next_state, __func__, true)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
if (!db_remove_their_prev_revocation_hash(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
if (!db_commit_transaction(peer))
|
|
|
|
return pkt_err(peer, "database error");
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_calculate_close_fee(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* Use actual worst-case length of close tx: based on BOLT#02's
|
|
|
|
* commitment tx numbers, but only 1 byte for output count */
|
|
|
|
const uint64_t txsize = 41 + 221 + 10 + 32 + 32;
|
|
|
|
uint64_t maxfee;
|
|
|
|
|
|
|
|
peer->closing.our_fee
|
2016-08-18 06:53:46 +02:00
|
|
|
= fee_by_feerate(txsize, get_feerate(peer->dstate));
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
* The sender MUST set `close_fee` lower than or equal to the
|
|
|
|
* fee of the final commitment transaction, and MUST set
|
|
|
|
* `close_fee` to an even number of satoshis.
|
|
|
|
*/
|
|
|
|
maxfee = commit_tx_fee(peer->local.commit->tx, peer->anchor.satoshis);
|
|
|
|
if (peer->closing.our_fee > maxfee) {
|
2016-08-18 06:53:46 +02:00
|
|
|
/* This could only happen if the fee rate dramatically */
|
2016-08-18 06:53:45 +02:00
|
|
|
log_unusual(peer->log,
|
|
|
|
"Closing fee %"PRIu64" exceeded commit fee %"PRIu64", reducing.",
|
|
|
|
peer->closing.our_fee, maxfee);
|
|
|
|
peer->closing.our_fee = maxfee;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* This can happen if actual commit txfee is odd. */
|
|
|
|
if (peer->closing.our_fee & 1)
|
|
|
|
peer->closing.our_fee--;
|
|
|
|
}
|
|
|
|
assert(!(peer->closing.our_fee & 1));
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static bool start_closing_in_transaction(struct peer *peer)
|
|
|
|
{
|
|
|
|
assert(!committed_to_htlcs(peer));
|
|
|
|
|
|
|
|
if (!set_peer_state(peer, STATE_MUTUAL_CLOSING, __func__, true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
peer_calculate_close_fee(peer);
|
|
|
|
peer->closing.closing_order = peer->order_counter++;
|
|
|
|
if (!db_update_our_closing(peer))
|
|
|
|
return false;
|
|
|
|
queue_pkt_close_signature(peer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pkt *start_closing(struct peer *peer)
|
|
|
|
{
|
|
|
|
if (!db_start_transaction(peer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!start_closing_in_transaction(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!db_commit_transaction(peer))
|
|
|
|
goto fail;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the io loop while we're doing shutdown. */
|
2016-08-18 06:53:46 +02:00
|
|
|
static bool shutdown_pkt_in(struct peer *peer, const Pkt *pkt)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
Pkt *err = NULL;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(peer->state == STATE_SHUTDOWN
|
|
|
|
|| peer->state == STATE_SHUTDOWN_COMMITTING);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
switch (pkt->pkt_case) {
|
|
|
|
case PKT__PKT_UPDATE_REVOCATION:
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->state == STATE_SHUTDOWN)
|
2016-05-26 07:55:24 +02:00
|
|
|
err = pkt_err_unexpected(peer, pkt);
|
|
|
|
else {
|
2016-08-18 06:55:14 +02:00
|
|
|
err = handle_pkt_revocation(peer, pkt, STATE_SHUTDOWN);
|
|
|
|
if (!err)
|
2016-06-28 23:19:20 +02:00
|
|
|
peer_update_complete(peer);
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PKT__PKT_UPDATE_ADD_HTLC:
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send a `update_add_htlc` after a
|
2016-08-18 06:53:46 +02:00
|
|
|
* `close_shutdown` */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (peer->closing.their_script)
|
2016-08-18 06:53:46 +02:00
|
|
|
err = pkt_err(peer, "Update during shutdown");
|
2016-05-26 07:55:24 +02:00
|
|
|
else
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_add(peer, pkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
break;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
case PKT__PKT_CLOSE_SHUTDOWN:
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* A node... MUST NOT send more than one `close_shutdown`. */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (peer->closing.their_script)
|
|
|
|
err = pkt_err_unexpected(peer, pkt);
|
2016-08-18 06:55:14 +02:00
|
|
|
else {
|
2016-08-18 06:53:46 +02:00
|
|
|
err = accept_pkt_close_shutdown(peer, pkt);
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!err) {
|
|
|
|
if (!db_set_their_closing_script(peer))
|
|
|
|
err = pkt_err(peer, "database error");
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PKT__PKT_UPDATE_FULFILL_HTLC:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_fulfill(peer, pkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
break;
|
|
|
|
case PKT__PKT_UPDATE_FAIL_HTLC:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_fail(peer, pkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
break;
|
2016-08-26 08:03:30 +02:00
|
|
|
case PKT__PKT_UPDATE_FEE:
|
|
|
|
err = handle_pkt_feechange(peer, pkt);
|
|
|
|
break;
|
2016-05-26 07:55:24 +02:00
|
|
|
case PKT__PKT_UPDATE_COMMIT:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_commit(peer, pkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
break;
|
|
|
|
case PKT__PKT_ERROR:
|
2016-08-18 06:55:13 +02:00
|
|
|
peer_unexpected_pkt(peer, pkt, __func__);
|
2016-06-28 23:19:20 +02:00
|
|
|
return peer_comms_err(peer, NULL);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
case PKT__PKT_AUTH:
|
|
|
|
case PKT__PKT_OPEN:
|
|
|
|
case PKT__PKT_OPEN_ANCHOR:
|
|
|
|
case PKT__PKT_OPEN_COMMIT_SIG:
|
|
|
|
case PKT__PKT_OPEN_COMPLETE:
|
|
|
|
case PKT__PKT_CLOSE_SIGNATURE:
|
|
|
|
default:
|
2016-08-18 06:55:13 +02:00
|
|
|
peer_unexpected_pkt(peer, pkt, __func__);
|
2016-05-26 07:55:24 +02:00
|
|
|
err = pkt_err_unexpected(peer, pkt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-31 08:36:31 +02:00
|
|
|
if (!err && !committed_to_htlcs(peer) && peer->closing.their_script)
|
2016-08-18 06:55:14 +02:00
|
|
|
err = start_closing(peer);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (err)
|
2016-06-28 23:19:20 +02:00
|
|
|
return peer_comms_err(peer, err);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
return true;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static bool peer_start_shutdown(struct peer *peer)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-08-18 06:55:14 +02:00
|
|
|
enum state newstate;
|
|
|
|
u8 *redeemscript;
|
|
|
|
|
|
|
|
if (!db_start_transaction(peer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!db_begin_shutdown(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
/* If they started close, we might not have sent ours. */
|
2016-08-18 06:55:14 +02:00
|
|
|
assert(!peer->closing.our_script);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
redeemscript = bitcoin_redeem_single(peer,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey);
|
|
|
|
|
|
|
|
peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript);
|
|
|
|
tal_free(redeemscript);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node SHOULD send a `close_shutdown` (if it has
|
|
|
|
* not already) after receiving `close_shutdown`.
|
|
|
|
*/
|
|
|
|
peer->closing.shutdown_order = peer->order_counter++;
|
|
|
|
if (!db_set_our_closing_script(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_pkt_close_shutdown(peer);
|
|
|
|
|
|
|
|
if (peer->state == STATE_NORMAL_COMMITTING)
|
|
|
|
newstate = STATE_SHUTDOWN_COMMITTING;
|
|
|
|
else {
|
|
|
|
assert(peer->state == STATE_NORMAL);
|
|
|
|
newstate = STATE_SHUTDOWN;
|
|
|
|
}
|
|
|
|
if (!set_peer_state(peer, newstate, __func__, true)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return false;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Catch case where we've exchanged and had no HTLCs anyway. */
|
2016-08-18 06:55:14 +02:00
|
|
|
if (peer->closing.their_script && !committed_to_htlcs(peer)) {
|
|
|
|
if (!start_closing_in_transaction(peer)) {
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
2016-08-18 06:55:14 +02:00
|
|
|
return db_commit_transaction(peer);
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
/* This is the io loop while we're in normal mode. */
|
2016-06-28 23:19:20 +02:00
|
|
|
static bool normal_pkt_in(struct peer *peer, const Pkt *pkt)
|
2016-06-28 23:19:20 +02:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
Pkt *err = NULL;
|
2016-06-28 23:19:20 +02:00
|
|
|
|
|
|
|
assert(peer->state == STATE_NORMAL
|
|
|
|
|| peer->state == STATE_NORMAL_COMMITTING);
|
|
|
|
|
|
|
|
switch (pkt->pkt_case) {
|
|
|
|
case PKT_UPDATE_ADD_HTLC:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_add(peer, pkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PKT_UPDATE_FULFILL_HTLC:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_fulfill(peer, pkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PKT_UPDATE_FAIL_HTLC:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_htlc_fail(peer, pkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
break;
|
|
|
|
|
2016-08-26 08:03:30 +02:00
|
|
|
case PKT__PKT_UPDATE_FEE:
|
|
|
|
err = handle_pkt_feechange(peer, pkt);
|
|
|
|
break;
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
case PKT_UPDATE_COMMIT:
|
2016-08-18 06:53:45 +02:00
|
|
|
err = handle_pkt_commit(peer, pkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
break;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
case PKT_CLOSE_SHUTDOWN:
|
|
|
|
err = accept_pkt_close_shutdown(peer, pkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
if (err)
|
|
|
|
break;
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!peer_start_shutdown(peer)) {
|
|
|
|
err = pkt_err(peer, "database error");
|
|
|
|
break;
|
2016-06-28 23:19:20 +02:00
|
|
|
}
|
2016-06-28 23:19:20 +02:00
|
|
|
return true;
|
2016-06-28 23:19:20 +02:00
|
|
|
|
|
|
|
case PKT_UPDATE_REVOCATION:
|
|
|
|
if (peer->state == STATE_NORMAL_COMMITTING) {
|
2016-08-18 06:55:14 +02:00
|
|
|
err = handle_pkt_revocation(peer, pkt, STATE_NORMAL);
|
|
|
|
if (!err)
|
2016-06-28 23:19:20 +02:00
|
|
|
peer_update_complete(peer);
|
2016-06-28 23:19:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall thru. */
|
|
|
|
default:
|
2016-08-18 06:55:13 +02:00
|
|
|
return peer_received_unexpected_pkt(peer, pkt, __func__);
|
2016-06-28 23:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2016-06-28 23:19:20 +02:00
|
|
|
return peer_comms_err(peer, err);
|
2016-06-28 23:19:20 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
return true;
|
2016-06-28 23:19:20 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void state_single(struct peer *peer,
|
2016-01-21 21:14:25 +01:00
|
|
|
const enum state_input input,
|
2016-06-28 23:19:20 +02:00
|
|
|
const Pkt *pkt)
|
2016-01-21 21:14:25 +01:00
|
|
|
{
|
|
|
|
const struct bitcoin_tx *broadcast;
|
2016-05-26 07:55:25 +02:00
|
|
|
enum state newstate;
|
2016-03-30 08:27:18 +02:00
|
|
|
size_t old_outpkts = tal_count(peer->outpkt);
|
2016-05-05 06:53:34 +02:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
newstate = state(peer, input, pkt, &broadcast);
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, newstate, input_name(input), false);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
/* We never come here again once we leave opening states. */
|
|
|
|
if (state_is_normal(peer->state)) {
|
|
|
|
assert(!peer->nc);
|
|
|
|
peer->nc = add_connection(peer->dstate,
|
|
|
|
&peer->dstate->id, peer->id,
|
|
|
|
peer->dstate->config.fee_base,
|
|
|
|
peer->dstate->config.fee_per_satoshi,
|
|
|
|
peer->dstate->config.min_htlc_expiry,
|
|
|
|
peer->dstate->config.min_htlc_expiry);
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* If we added uncommitted changes, we should have set them to send. */
|
|
|
|
if (peer_uncommitted_changes(peer))
|
|
|
|
assert(peer->commit_timer);
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
if (tal_count(peer->outpkt) > old_outpkts) {
|
2016-05-26 07:55:24 +02:00
|
|
|
Pkt *outpkt = peer->outpkt[old_outpkts];
|
2016-05-26 07:55:24 +02:00
|
|
|
log_add(peer->log, " (out %s)", pkt_name(outpkt->pkt_case));
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
2016-05-04 08:33:10 +02:00
|
|
|
if (broadcast)
|
|
|
|
broadcast_tx(peer, broadcast);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
if (state_is_error(peer->state)) {
|
2016-05-26 07:55:25 +02:00
|
|
|
/* Breakdown is common, others less so. */
|
|
|
|
if (peer->state != STATE_ERR_BREAKDOWN)
|
|
|
|
log_broken(peer->log, "Entered error state %s",
|
|
|
|
state_name(peer->state));
|
2016-05-26 07:55:24 +02:00
|
|
|
peer_breakdown(peer);
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* Start output if not running already; it will close conn. */
|
2016-01-21 21:15:28 +01:00
|
|
|
io_wake(peer);
|
2016-08-18 06:55:14 +02:00
|
|
|
} else if (!state_is_opening(peer->state)) {
|
|
|
|
/* Now in STATE_NORMAL, so save. */
|
|
|
|
if (!db_start_transaction(peer)
|
|
|
|
|| !db_update_state(peer)
|
|
|
|
|| !db_commit_transaction(peer)) {
|
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, __func__,
|
|
|
|
false);
|
|
|
|
peer_breakdown(peer);
|
|
|
|
|
|
|
|
/* Start output if not running already; it will close conn. */
|
|
|
|
io_wake(peer);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void state_event(struct peer *peer,
|
|
|
|
const enum state_input input,
|
2016-06-28 23:19:20 +02:00
|
|
|
const Pkt *pkt)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
if (!state_is_opening(peer->state)) {
|
2016-05-26 07:55:24 +02:00
|
|
|
log_unusual(peer->log,
|
|
|
|
"Unexpected input %s while state %s",
|
|
|
|
input_name(input), state_name(peer->state));
|
|
|
|
} else {
|
2016-06-28 23:19:20 +02:00
|
|
|
state_single(peer, input, pkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Create a HTLC fulfill transaction for onchain.tx[out_num]. */
|
2016-07-01 03:46:58 +02:00
|
|
|
static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
|
2016-08-18 06:53:46 +02:00
|
|
|
unsigned int out_num)
|
2016-07-01 03:46:58 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct htlc *htlc = peer->onchain.htlcs[out_num];
|
|
|
|
const u8 *wscript = peer->onchain.wscripts[out_num];
|
2016-07-01 03:46:58 +02:00
|
|
|
struct bitcoin_signature sig;
|
|
|
|
u64 fee, satoshis;
|
|
|
|
|
|
|
|
assert(htlc->r);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
tx->input[0].index = out_num;
|
2016-08-18 06:53:46 +02:00
|
|
|
tx->input[0].txid = peer->onchain.txid;
|
2016-07-01 03:46:58 +02:00
|
|
|
satoshis = htlc->msatoshis / 1000;
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &satoshis);
|
|
|
|
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
|
|
|
|
|
|
|
|
/* Using a new output address here would be useless: they can tell
|
|
|
|
* it's their HTLC, and that we collected it via rval. */
|
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx,
|
2016-07-01 03:49:28 +02:00
|
|
|
bitcoin_redeem_single(tx,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey));
|
2016-07-01 03:46:58 +02:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
|
|
|
log_debug(peer->log, "Pre-witness txlen = %zu\n",
|
|
|
|
measure_tx_cost(tx) / 4);
|
|
|
|
|
|
|
|
assert(measure_tx_cost(tx) == 83 * 4);
|
|
|
|
|
|
|
|
/* Witness length can vary, due to DER encoding of sigs, but we
|
|
|
|
* use 539 from an example run. */
|
2016-08-18 06:53:46 +02:00
|
|
|
fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate));
|
2016-07-01 03:46:58 +02:00
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (fee > satoshis || is_dust(satoshis - fee))
|
2016-07-01 03:46:58 +02:00
|
|
|
fatal("HTLC fulfill amount of %"PRIu64" won't cover fee %"PRIu64,
|
|
|
|
satoshis, fee);
|
|
|
|
|
|
|
|
tx->output[0].amount = satoshis - fee;
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_htlc_fulfill(peer, tx, wscript, &sig.sig);
|
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
tx->input[0].witness = bitcoin_witness_htlc(tx, peer->dstate->secpctx,
|
|
|
|
htlc->r, &sig, wscript);
|
2016-07-01 03:46:58 +02:00
|
|
|
|
|
|
|
log_debug(peer->log, "tx cost for htlc fulfill tx: %zu",
|
|
|
|
measure_tx_cost(tx));
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
static bool command_htlc_set_fail(struct peer *peer, struct htlc *htlc,
|
|
|
|
enum fail_error error_code, const char *why)
|
|
|
|
{
|
|
|
|
const u8 *fail = failinfo_create(htlc, peer->dstate->secpctx,
|
|
|
|
&peer->dstate->id, error_code, why);
|
|
|
|
|
|
|
|
set_htlc_fail(peer, htlc, fail, tal_count(fail));
|
|
|
|
tal_free(fail);
|
|
|
|
return command_htlc_fail(peer, htlc);
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
static bool command_htlc_fail(struct peer *peer, struct htlc *htlc)
|
2016-05-26 07:55:25 +02:00
|
|
|
{
|
2016-07-01 03:57:57 +02:00
|
|
|
/* If onchain, nothing we can do. */
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!state_can_remove_htlc(peer->state))
|
|
|
|
return false;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC fulfill/fail to the
|
|
|
|
* unacked changeset for its remote commitment
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fail_htlc(peer->remote.staging_cstate, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
htlc_changestate(htlc, RCVD_ADD_ACK_REVOCATION, SENT_REMOVE_HTLC, false);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
queue_pkt_htlc_fail(peer, htlc);
|
2016-05-26 07:55:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If the node receives... a redemption preimage for an unresolved *commitment
|
|
|
|
* tx* output it was offered, it MUST *resolve* the output by spending it using
|
|
|
|
* the preimage.
|
|
|
|
*/
|
|
|
|
static bool fulfill_onchain(struct peer *peer, struct htlc *htlc)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (i = 0; i < tal_count(peer->onchain.htlcs); i++) {
|
|
|
|
if (peer->onchain.htlcs[i] == htlc) {
|
2016-07-01 03:49:28 +02:00
|
|
|
/* Already irrevocably resolved? */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.resolved[i])
|
2016-07-01 03:49:28 +02:00
|
|
|
return false;
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[i]
|
2016-08-18 06:53:46 +02:00
|
|
|
= htlc_fulfill_tx(peer, i);
|
2016-08-18 06:53:46 +02:00
|
|
|
broadcast_tx(peer, peer->onchain.resolved[i]);
|
2016-07-01 03:49:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fatal("Unknown HTLC to fulfill onchain");
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static bool command_htlc_fulfill(struct peer *peer, struct htlc *htlc)
|
2016-05-26 07:55:25 +02:00
|
|
|
{
|
2016-07-01 03:49:28 +02:00
|
|
|
if (peer->state == STATE_CLOSE_ONCHAIN_THEIR_UNILATERAL
|
|
|
|
|| peer->state == STATE_CLOSE_ONCHAIN_OUR_UNILATERAL) {
|
|
|
|
return fulfill_onchain(peer, htlc);
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!state_can_remove_htlc(peer->state))
|
|
|
|
return false;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC fulfill/fail to the
|
|
|
|
* unacked changeset for its remote commitment
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
cstate_fulfill_htlc(peer->remote.staging_cstate, htlc);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
htlc_changestate(htlc, RCVD_ADD_ACK_REVOCATION, SENT_REMOVE_HTLC, false);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
|
|
|
|
queue_pkt_htlc_fulfill(peer, htlc);
|
2016-05-26 07:55:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
const char *command_htlc_add(struct peer *peer, u64 msatoshis,
|
|
|
|
unsigned int expiry,
|
|
|
|
const struct sha256 *rhash,
|
|
|
|
struct htlc *src,
|
|
|
|
const u8 *route,
|
|
|
|
u32 *error_code,
|
|
|
|
struct htlc **htlc)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
struct abs_locktime locktime;
|
|
|
|
|
|
|
|
if (!blocks_to_abs_locktime(expiry, &locktime)) {
|
|
|
|
log_unusual(peer->log, "add_htlc: fail: bad expiry %u", expiry);
|
2016-08-31 07:21:41 +02:00
|
|
|
*error_code = BAD_REQUEST_400;
|
|
|
|
return "bad expiry";
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (expiry < get_block_height(peer->dstate) + peer->dstate->config.min_htlc_expiry) {
|
|
|
|
log_unusual(peer->log, "add_htlc: fail: expiry %u is too soon",
|
|
|
|
expiry);
|
2016-08-31 07:21:41 +02:00
|
|
|
*error_code = BAD_REQUEST_400;
|
|
|
|
return "expiry too soon";
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (expiry > get_block_height(peer->dstate) + peer->dstate->config.max_htlc_expiry) {
|
|
|
|
log_unusual(peer->log, "add_htlc: fail: expiry %u is too far",
|
|
|
|
expiry);
|
2016-08-31 07:21:41 +02:00
|
|
|
*error_code = BAD_REQUEST_400;
|
|
|
|
return "expiry too far";
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT add a HTLC if it would result in it
|
2016-07-01 03:49:28 +02:00
|
|
|
* offering more than 300 HTLCs in the remote commitment transaction.
|
2016-06-28 23:19:21 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->remote.staging_cstate->side[OURS].num_htlcs == 300) {
|
2016-06-28 23:19:21 +02:00
|
|
|
log_unusual(peer->log, "add_htlc: fail: already at limit");
|
2016-08-31 07:21:41 +02:00
|
|
|
*error_code = SERVICE_UNAVAILABLE_503;
|
|
|
|
return "channel full";
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!state_can_add_htlc(peer->state)) {
|
|
|
|
log_unusual(peer->log, "add_htlc: fail: peer state %s",
|
|
|
|
state_name(peer->state));
|
2016-08-31 07:21:41 +02:00
|
|
|
*error_code = NOT_FOUND_404;
|
|
|
|
return "peer not available";
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
*htlc = peer_new_htlc(peer, peer->htlc_id_counter,
|
|
|
|
msatoshis, rhash, expiry, route, tal_count(route),
|
|
|
|
src, SENT_ADD_HTLC);
|
2016-06-30 01:38:11 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC addition to the unacked
|
|
|
|
* changeset for its remote commitment
|
|
|
|
*/
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!cstate_add_htlc(peer->remote.staging_cstate, *htlc, true)) {
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT offer `amount_msat` it cannot pay for in
|
|
|
|
* the remote commitment transaction at the current `fee_rate`
|
|
|
|
*/
|
|
|
|
log_unusual(peer->log, "add_htlc: fail: Cannot afford %"PRIu64
|
|
|
|
" milli-satoshis in their commit tx",
|
|
|
|
msatoshis);
|
|
|
|
log_add_struct(peer->log, " channel state %s",
|
|
|
|
struct channel_state,
|
|
|
|
peer->remote.staging_cstate);
|
|
|
|
*htlc = tal_free(*htlc);
|
|
|
|
*error_code = SERVICE_UNAVAILABLE_503;
|
|
|
|
return "cannot afford htlc";
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
queue_pkt_htlc_add(peer, *htlc);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Make sure we never offer the same one twice. */
|
|
|
|
peer->htlc_id_counter++;
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
return NULL;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
Pkt *out;
|
2016-03-15 07:38:31 +01:00
|
|
|
size_t n = tal_count(peer->outpkt);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-03-15 07:38:31 +01:00
|
|
|
if (n == 0) {
|
2016-01-21 21:15:28 +01:00
|
|
|
/* We close the connection once we've sent everything. */
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!state_can_io(peer->state)) {
|
|
|
|
log_debug(peer->log, "pkt_out: no IO possible, closing");
|
2016-01-21 21:15:28 +01:00
|
|
|
return io_close(conn);
|
2016-06-30 01:38:11 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
return io_out_wait(conn, peer, pkt_out, peer);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
if (peer->fake_close || !peer->output_enabled)
|
|
|
|
return io_out_wait(conn, peer, pkt_out, peer);
|
|
|
|
|
2016-03-15 07:38:31 +01:00
|
|
|
out = peer->outpkt[0];
|
|
|
|
memmove(peer->outpkt, peer->outpkt + 1, (sizeof(*peer->outpkt)*(n-1)));
|
|
|
|
tal_resize(&peer->outpkt, n-1);
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "pkt_out: writing %s", pkt_name(out->pkt_case));
|
2016-05-26 07:55:24 +02:00
|
|
|
return peer_write_packet(conn, peer, out, pkt_out);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static void clear_output_queue(struct peer *peer)
|
|
|
|
{
|
|
|
|
size_t i, n = tal_count(peer->outpkt);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
tal_free(peer->outpkt[i]);
|
|
|
|
tal_resize(&peer->outpkt, 0);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
static struct io_plan *pkt_in(struct io_conn *conn, struct peer *peer)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
bool keep_going;
|
|
|
|
|
|
|
|
/* We ignore packets if they tell us to, or we're closing already */
|
|
|
|
if (peer->fake_close || !state_can_io(peer->state))
|
|
|
|
keep_going = true;
|
|
|
|
else if (state_is_normal(peer->state))
|
|
|
|
keep_going = normal_pkt_in(peer, peer->inpkt);
|
2016-08-18 06:53:46 +02:00
|
|
|
else if (state_is_shutdown(peer->state))
|
|
|
|
keep_going = shutdown_pkt_in(peer, peer->inpkt);
|
2016-05-26 07:55:24 +02:00
|
|
|
else if (peer->state == STATE_MUTUAL_CLOSING)
|
2016-06-28 23:19:20 +02:00
|
|
|
keep_going = closing_pkt_in(peer, peer->inpkt);
|
|
|
|
else {
|
2016-06-28 23:19:20 +02:00
|
|
|
state_event(peer, peer->inpkt->pkt_case, peer->inpkt);
|
2016-06-28 23:19:20 +02:00
|
|
|
keep_going = true;
|
2016-04-11 09:01:43 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
peer->inpkt = tal_free(peer->inpkt);
|
|
|
|
if (keep_going)
|
|
|
|
return peer_read_packet(conn, peer, pkt_in);
|
|
|
|
else
|
|
|
|
return peer_close(conn, peer);
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
/*
|
|
|
|
* This only works because we send one update at a time, and they can't
|
|
|
|
* ask for it again if they've already sent the `update_revocation` acking it.
|
|
|
|
*/
|
|
|
|
static void retransmit_updates(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MAY simply retransmit messages which are identical to the
|
|
|
|
* previous transmission. */
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
switch (h->state) {
|
|
|
|
case SENT_ADD_COMMIT:
|
|
|
|
log_debug(peer->log, "Retransmitting add HTLC %"PRIu64,
|
|
|
|
h->id);
|
|
|
|
queue_pkt_htlc_add(peer, h);
|
|
|
|
break;
|
|
|
|
case SENT_REMOVE_COMMIT:
|
|
|
|
log_debug(peer->log, "Retransmitting %s HTLC %"PRIu64,
|
|
|
|
h->r ? "fulfill" : "fail", h->id);
|
|
|
|
if (h->r)
|
|
|
|
queue_pkt_htlc_fulfill(peer, h);
|
|
|
|
else
|
|
|
|
queue_pkt_htlc_fail(peer, h);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 08:01:19 +02:00
|
|
|
|
|
|
|
/* This feechange may not be appropriate any more, but that's no
|
|
|
|
* different from when we sent it last time. And this avoids us
|
|
|
|
* creating different commit txids on retransmission */
|
|
|
|
if (peer->feechanges[SENT_FEECHANGE_COMMIT]) {
|
|
|
|
u64 feerate = peer->feechanges[SENT_FEECHANGE_COMMIT]->fee_rate;
|
|
|
|
log_debug(peer->log,
|
|
|
|
"Retransmitting feechange %"PRIu64, feerate);
|
|
|
|
queue_pkt_feechange(peer, feerate);
|
|
|
|
}
|
|
|
|
assert(!peer->feechanges[SENT_FEECHANGE]);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* On disconnection, a node MUST reverse any uncommitted changes sent by the
|
|
|
|
* other side (ie. `update_add_htlc`, `update_fee`, `update_fail_htlc` and
|
|
|
|
* `update_fulfill_htlc` for which no `update_commit` has been received). A
|
|
|
|
* node SHOULD retain the `r` value from the `update_fulfill_htlc`, however.
|
|
|
|
*/
|
|
|
|
static void forget_uncommitted_changes(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct htlc *h;
|
|
|
|
struct htlc_map_iter it;
|
|
|
|
bool retry;
|
|
|
|
|
|
|
|
if (!peer->remote.commit || !peer->remote.commit->cstate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_debug(peer->log, "Forgetting uncommitted");
|
2016-08-31 08:36:32 +02:00
|
|
|
log_debug_struct(peer->log, "LOCAL: changing from %s",
|
|
|
|
struct channel_state, peer->local.staging_cstate);
|
|
|
|
log_add_struct(peer->log, " to %s",
|
|
|
|
struct channel_state, peer->local.commit->cstate);
|
|
|
|
log_debug_struct(peer->log, "REMOTE: changing from %s",
|
|
|
|
struct channel_state, peer->remote.staging_cstate);
|
|
|
|
log_add_struct(peer->log, " to %s",
|
|
|
|
struct channel_state, peer->remote.commit->cstate);
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
tal_free(peer->local.staging_cstate);
|
|
|
|
tal_free(peer->remote.staging_cstate);
|
|
|
|
peer->local.staging_cstate
|
|
|
|
= copy_cstate(peer, peer->local.commit->cstate);
|
|
|
|
peer->remote.staging_cstate
|
|
|
|
= copy_cstate(peer, peer->remote.commit->cstate);
|
|
|
|
|
|
|
|
/* We forget everything we're routing, and re-send. This
|
|
|
|
* works for the reload-from-database case as well as the
|
|
|
|
* normal reconnect. */
|
|
|
|
again:
|
|
|
|
retry = false;
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
switch (h->state) {
|
|
|
|
case SENT_ADD_HTLC:
|
2016-08-28 05:21:57 +02:00
|
|
|
/* Adjust counter to lowest HTLC removed */
|
|
|
|
if (peer->htlc_id_counter > h->id) {
|
|
|
|
log_debug(peer->log,
|
|
|
|
"Lowering htlc_id_counter to %"PRIu64,
|
|
|
|
h->id);
|
|
|
|
peer->htlc_id_counter = h->id;
|
|
|
|
}
|
|
|
|
/* Fall thru */
|
2016-08-18 06:55:08 +02:00
|
|
|
case RCVD_ADD_HTLC:
|
|
|
|
log_debug(peer->log, "Forgetting %s %"PRIu64,
|
|
|
|
htlc_state_name(h->state), h->id);
|
|
|
|
/* May miss some due to delete reorg. */
|
|
|
|
tal_free(h);
|
|
|
|
retry = true;
|
|
|
|
break;
|
|
|
|
case RCVD_REMOVE_HTLC:
|
|
|
|
log_debug(peer->log, "Undoing %s %"PRIu64,
|
|
|
|
htlc_state_name(h->state), h->id);
|
|
|
|
htlc_undostate(h, RCVD_REMOVE_HTLC,
|
|
|
|
SENT_ADD_ACK_REVOCATION);
|
|
|
|
break;
|
|
|
|
case SENT_REMOVE_HTLC:
|
|
|
|
log_debug(peer->log, "Undoing %s %"PRIu64,
|
|
|
|
htlc_state_name(h->state), h->id);
|
|
|
|
htlc_undostate(h, SENT_REMOVE_HTLC,
|
|
|
|
RCVD_ADD_ACK_REVOCATION);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retry)
|
|
|
|
goto again;
|
2016-08-26 08:01:19 +02:00
|
|
|
|
|
|
|
/* Forget uncommitted feechanges */
|
|
|
|
peer->feechanges[SENT_FEECHANGE]
|
|
|
|
= tal_free(peer->feechanges[SENT_FEECHANGE]);
|
|
|
|
peer->feechanges[RCVD_FEECHANGE]
|
|
|
|
= tal_free(peer->feechanges[RCVD_FEECHANGE]);
|
2016-08-28 05:21:57 +02:00
|
|
|
|
|
|
|
/* Make sure our HTLC counter is correct. */
|
|
|
|
if (peer->htlc_id_counter != 0)
|
|
|
|
assert(htlc_get(&peer->htlcs, peer->htlc_id_counter-1, LOCAL));
|
|
|
|
assert(!htlc_get(&peer->htlcs, peer->htlc_id_counter, LOCAL));
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
static void retransmit_pkts(struct peer *peer, s64 ack)
|
2016-08-18 06:55:08 +02:00
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
log_debug(peer->log, "Our order counter is %"PRIi64", their ack %"PRIi64,
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->order_counter, ack);
|
|
|
|
|
|
|
|
if (ack > peer->order_counter) {
|
2016-08-18 06:55:13 +02:00
|
|
|
log_unusual(peer->log, "reconnect ack %"PRIi64" > %"PRIi64,
|
2016-08-18 06:55:08 +02:00
|
|
|
ack, peer->order_counter);
|
|
|
|
peer_comms_err(peer, pkt_err(peer, "invalid ack"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
log_debug(peer->log, "They acked %"PRIi64", remote=%"PRIi64" local=%"PRIi64,
|
|
|
|
ack, peer->remote.commit ? peer->remote.commit->order : -2,
|
|
|
|
peer->local.commit ? peer->local.commit->order : -2);
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MAY assume that only one of each type of message need be
|
|
|
|
* retransmitted. A node SHOULD retransmit the last of each message
|
|
|
|
* type which was not counted by the `ack` field.
|
|
|
|
*/
|
|
|
|
while (ack < peer->order_counter) {
|
|
|
|
if (peer->remote.commit && ack == peer->remote.commit->order) {
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* Before retransmitting `update_commit`, the node
|
|
|
|
* MUST send appropriate `update_add_htlc`,
|
|
|
|
* `update_fee`, `update_fail_htlc` or
|
|
|
|
* `update_fulfill_htlc` messages (the other node will
|
|
|
|
* have forgotten them, as required above).
|
|
|
|
*/
|
|
|
|
retransmit_updates(peer);
|
|
|
|
queue_pkt_commit(peer, peer->remote.commit->sig);
|
|
|
|
} else if (peer->local.commit
|
|
|
|
&& ack == peer->local.commit->order) {
|
|
|
|
/* Re-transmit revocation. */
|
|
|
|
struct sha256 preimage, next;
|
|
|
|
u64 commit_num = peer->local.commit->commit_num - 1;
|
|
|
|
|
|
|
|
/* Make sure we don't revoke current commit! */
|
|
|
|
assert(commit_num < peer->local.commit->commit_num);
|
|
|
|
peer_get_revocation_preimage(peer, commit_num,&preimage);
|
|
|
|
peer_get_revocation_hash(peer, commit_num + 2, &next);
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(peer->log, "Re-sending revocation hash %"PRIu64,
|
|
|
|
commit_num + 2);
|
|
|
|
log_add_struct(peer->log, "value %s", struct sha256,
|
|
|
|
&next);
|
|
|
|
log_add_struct(peer->log, "local.next=%s", struct sha256,
|
|
|
|
&peer->local.next_revocation_hash);
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "Re-sending revocation %"PRIu64,
|
|
|
|
commit_num);
|
|
|
|
queue_pkt_revocation(peer, &preimage, &next);
|
|
|
|
} else if (ack == peer->closing.shutdown_order) {
|
|
|
|
log_debug(peer->log, "Re-sending shutdown");
|
|
|
|
queue_pkt_close_shutdown(peer);
|
|
|
|
} else if (ack == peer->closing.closing_order) {
|
|
|
|
log_debug(peer->log, "Re-sending closing order");
|
|
|
|
queue_pkt_close_signature(peer);
|
|
|
|
} else {
|
|
|
|
log_broken(peer->log, "Can't rexmit %"PRIu64
|
2016-08-18 06:55:13 +02:00
|
|
|
" when local commit %"PRIi64" and remote %"PRIi64,
|
2016-08-18 06:55:08 +02:00
|
|
|
ack,
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->local.commit ? peer->local.commit->order : -2,
|
|
|
|
peer->remote.commit ? peer->remote.commit->order : -2);
|
2016-08-18 06:55:08 +02:00
|
|
|
peer_comms_err(peer, pkt_err(peer, "invalid ack"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ack++;
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:43:57 +02:00
|
|
|
/* We might need to update HTLCs which were from other peers. */
|
2016-08-18 06:55:08 +02:00
|
|
|
retry_all_routing(peer);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
/* Crypto is on, we are live. */
|
|
|
|
static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
peer_secrets_init(peer);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer_get_revocation_hash(peer, 0, &peer->local.next_revocation_hash);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
assert(peer->state == STATE_INIT);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_create_peer(peer))
|
|
|
|
fatal("Database error in %s", __func__);
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
state_event(peer, peer->local.offer_anchor, NULL);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
assert(!peer->connected);
|
|
|
|
peer->connected = true;
|
2016-01-21 21:14:25 +01:00
|
|
|
return io_duplex(conn,
|
|
|
|
peer_read_packet(conn, peer, pkt_in),
|
|
|
|
pkt_out(conn, peer));
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void destroy_peer(struct peer *peer)
|
|
|
|
{
|
2016-01-21 21:14:25 +01:00
|
|
|
if (peer->conn)
|
|
|
|
io_close(peer->conn);
|
2016-01-21 21:11:49 +01:00
|
|
|
list_del_from(&peer->dstate->peers, &peer->list);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static void try_reconnect(struct peer *peer);
|
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
static void peer_disconnect(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
log_info(peer->log, "Disconnected");
|
|
|
|
|
|
|
|
/* No longer connected. */
|
|
|
|
peer->conn = NULL;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->connected = false;
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
/* Not even set up yet? Simply free.*/
|
|
|
|
if (peer->state == STATE_INIT) {
|
|
|
|
tal_free(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:59:43 +02:00
|
|
|
/* Completely dead? Free it now. */
|
|
|
|
if (peer->state == STATE_CLOSED) {
|
|
|
|
io_break(peer);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* This is an unexpected close. */
|
2016-08-18 06:55:08 +02:00
|
|
|
if (state_can_io(peer->state)) {
|
|
|
|
forget_uncommitted_changes(peer);
|
2016-08-18 06:55:13 +02:00
|
|
|
try_reconnect(peer);
|
2016-05-05 06:53:34 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:03:40 +02:00
|
|
|
static u64 desired_commit_feerate(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
return get_feerate(dstate) * dstate->config.commitment_fee_percent / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void maybe_propose_new_feerate(struct peer *peer)
|
|
|
|
{
|
|
|
|
u64 rate, max_rate;
|
|
|
|
|
|
|
|
rate = desired_commit_feerate(peer->dstate);
|
|
|
|
max_rate = approx_max_feerate(peer->remote.commit->cstate, LOCAL);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST NOT send a `fee_rate` which it could not
|
|
|
|
* afford (see "Fee Calculation), were it applied to the receiving
|
|
|
|
* node's commitment transaction. */
|
|
|
|
if (rate > max_rate) {
|
|
|
|
log_debug(peer->log,
|
|
|
|
"Cannot afford feerate %"PRIi64" using %"PRIi64,
|
|
|
|
rate, max_rate);
|
|
|
|
rate = max_rate;
|
|
|
|
|
|
|
|
/* If this is less than we have no, don't change! */
|
|
|
|
if (rate < peer->local.staging_cstate->fee_rate) {
|
|
|
|
log_debug(peer->log, "Leaving old rate in place");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No fee rate change? Fine. */
|
|
|
|
if (peer->local.staging_cstate->fee_rate == rate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_feechange(peer, rate, SENT_FEECHANGE);
|
|
|
|
queue_pkt_feechange(peer, rate);
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:00:11 +02:00
|
|
|
static void do_commit(struct peer *peer, struct command *jsoncmd)
|
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
struct commit_info *ci;
|
2016-08-18 06:55:14 +02:00
|
|
|
const char *errmsg;
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct htlcs_table changes[] = {
|
2016-08-18 06:53:45 +02:00
|
|
|
{ SENT_ADD_HTLC, SENT_ADD_COMMIT },
|
|
|
|
{ SENT_REMOVE_REVOCATION, SENT_REMOVE_ACK_COMMIT },
|
|
|
|
{ SENT_ADD_REVOCATION, SENT_ADD_ACK_COMMIT},
|
|
|
|
{ SENT_REMOVE_HTLC, SENT_REMOVE_COMMIT}
|
|
|
|
};
|
2016-08-26 08:01:19 +02:00
|
|
|
static const struct feechanges_table feechanges[] = {
|
|
|
|
{ SENT_FEECHANGE, SENT_FEECHANGE_COMMIT },
|
|
|
|
{ SENT_FEECHANGE_REVOCATION, SENT_FEECHANGE_ACK_COMMIT}
|
|
|
|
};
|
2016-08-18 06:55:13 +02:00
|
|
|
bool to_us_only;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-26 08:03:40 +02:00
|
|
|
/* If we want to change the payrate, do it now. */
|
|
|
|
maybe_propose_new_feerate(peer);
|
|
|
|
|
2016-05-09 23:00:11 +02:00
|
|
|
/* We can have changes we suggested, or changes they suggested. */
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!peer_uncommitted_changes(peer)) {
|
|
|
|
log_debug(peer->log, "do_commit: no changes to commit");
|
|
|
|
if (jsoncmd)
|
2016-06-28 23:19:20 +02:00
|
|
|
command_fail(jsoncmd, "no changes to commit");
|
|
|
|
return;
|
2016-05-09 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "do_commit: sending commit command %"PRIu64,
|
|
|
|
peer->remote.commit->commit_num + 1);
|
2016-05-26 07:55:25 +02:00
|
|
|
|
|
|
|
assert(state_can_commit(peer->state));
|
|
|
|
assert(!peer->commit_jsoncmd);
|
2016-06-28 23:19:20 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
peer->commit_jsoncmd = jsoncmd;
|
2016-08-18 06:53:46 +02:00
|
|
|
ci = new_commit_info(peer, peer->remote.commit->commit_num + 1);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(!peer->their_prev_revocation_hash);
|
|
|
|
peer->their_prev_revocation_hash
|
|
|
|
= tal_dup(peer, struct sha256,
|
|
|
|
&peer->remote.commit->revocation_hash);
|
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!db_start_transaction(peer))
|
|
|
|
goto database_error;
|
2016-08-18 06:55:14 +02:00
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
errmsg = changestates(peer, changes, ARRAY_SIZE(changes),
|
|
|
|
feechanges, ARRAY_SIZE(feechanges), true);
|
2016-08-18 06:55:14 +02:00
|
|
|
if (errmsg) {
|
|
|
|
log_broken(peer->log, "queue_pkt_commit: %s", errmsg);
|
2016-08-31 08:36:32 +02:00
|
|
|
goto database_error;
|
2016-08-18 06:55:14 +02:00
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Create new commit info for this commit tx. */
|
|
|
|
ci->revocation_hash = peer->remote.next_revocation_hash;
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2016-08-18 06:55:13 +02:00
|
|
|
* ...a sending node MUST apply all remote acked and unacked
|
2016-08-18 06:53:45 +02:00
|
|
|
* changes except unacked fee changes to the remote commitment
|
|
|
|
* before generating `sig`. */
|
|
|
|
ci->cstate = copy_cstate(ci, peer->remote.staging_cstate);
|
|
|
|
ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash,
|
2016-08-18 06:55:13 +02:00
|
|
|
ci->cstate, REMOTE, &to_us_only);
|
2016-08-18 06:53:45 +02:00
|
|
|
bitcoin_txid(ci->tx, &ci->txid);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!to_us_only) {
|
2016-08-31 08:36:32 +02:00
|
|
|
log_debug(peer->log, "Signing tx %"PRIu64, ci->commit_num);
|
|
|
|
log_add_struct(peer->log, " for %s",
|
|
|
|
struct channel_state, ci->cstate);
|
|
|
|
log_add_struct(peer->log, " (txid %s)",
|
|
|
|
struct sha256_double, &ci->txid);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
ci->sig = tal(ci, struct bitcoin_signature);
|
|
|
|
ci->sig->stype = SIGHASH_ALL;
|
|
|
|
peer_sign_theircommit(peer, ci->tx, &ci->sig->sig);
|
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Switch to the new commitment. */
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_free(peer->remote.commit);
|
2016-08-18 06:53:45 +02:00
|
|
|
peer->remote.commit = ci;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->remote.commit->order = peer->order_counter++;
|
2016-08-31 08:36:32 +02:00
|
|
|
if (!db_new_commit_info(peer, THEIRS, peer->their_prev_revocation_hash))
|
|
|
|
goto database_error;
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* We don't need to remember their commit if we don't give sig. */
|
|
|
|
if (ci->sig && !peer_add_their_commit(peer, &ci->txid, ci->commit_num))
|
2016-08-31 08:36:32 +02:00
|
|
|
goto database_error;
|
2016-08-18 06:55:14 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->state == STATE_SHUTDOWN) {
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_SHUTDOWN_COMMITTING, __func__, true);
|
2016-05-26 07:55:25 +02:00
|
|
|
} else {
|
|
|
|
assert(peer->state == STATE_NORMAL);
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_NORMAL_COMMITTING, __func__, true);
|
2016-05-09 23:00:11 +02:00
|
|
|
}
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!db_commit_transaction(peer))
|
2016-08-31 08:36:32 +02:00
|
|
|
goto database_error;
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
queue_pkt_commit(peer, ci->sig);
|
2016-08-31 08:36:32 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
database_error:
|
|
|
|
db_abort_transaction(peer);
|
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, __func__, false);
|
|
|
|
peer_breakdown(peer);
|
2016-05-09 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* FIXME: don't spin on this timer if we're not connected! */
|
2016-05-09 23:00:11 +02:00
|
|
|
static void try_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
peer->commit_timer = NULL;
|
2016-05-26 07:55:25 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
if (state_can_commit(peer->state) && peer->connected)
|
2016-05-26 07:55:25 +02:00
|
|
|
do_commit(peer, NULL);
|
|
|
|
else {
|
2016-08-18 06:55:08 +02:00
|
|
|
/* FIXME: try again when we receive revocation /
|
|
|
|
* reconnect, rather than using timer! */
|
2016-05-26 07:55:25 +02:00
|
|
|
log_debug(peer->log, "try_commit: state=%s, re-queueing timer",
|
|
|
|
state_name(peer->state));
|
|
|
|
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
}
|
2016-05-09 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
struct commit_info *new_commit_info(const tal_t *ctx, u64 commit_num)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct commit_info *ci = tal(ctx, struct commit_info);
|
|
|
|
ci->commit_num = commit_num;
|
|
|
|
ci->tx = NULL;
|
|
|
|
ci->cstate = NULL;
|
|
|
|
ci->sig = NULL;
|
2016-08-18 06:55:13 +02:00
|
|
|
ci->order = (s64)-1LL;
|
2016-08-18 06:53:45 +02:00
|
|
|
return ci;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static bool peer_reconnected(struct peer *peer,
|
|
|
|
struct io_conn *conn,
|
|
|
|
int addr_type, int addr_protocol,
|
|
|
|
struct io_data *iod,
|
2016-08-18 06:55:13 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
bool we_connected)
|
2016-08-18 06:55:08 +02:00
|
|
|
{
|
2016-08-18 06:55:14 +02:00
|
|
|
char *name;
|
|
|
|
struct netaddr addr;
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
assert(structeq(peer->id, id));
|
|
|
|
|
|
|
|
peer->io_data = tal_steal(peer, iod);
|
2016-08-18 06:55:14 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
/* FIXME: Attach IO logging for this peer. */
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!netaddr_from_fd(io_conn_fd(conn), addr_type, addr_protocol, &addr))
|
2016-08-18 06:55:08 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If we free peer, conn should be closed, but can't be freed
|
|
|
|
* immediately so don't make peer a parent. */
|
|
|
|
peer->conn = conn;
|
|
|
|
io_set_finish(conn, peer_disconnect, peer);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
name = netaddr_name(peer, &addr);
|
|
|
|
log_info(peer->log, "Reconnected %s %s",
|
|
|
|
we_connected ? "out to" : "in from", name);
|
|
|
|
tal_free(name);
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
struct peer *new_peer(struct lightningd_state *dstate,
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *log,
|
2016-08-18 06:55:13 +02:00
|
|
|
enum state state,
|
|
|
|
enum state_input offer_anchor)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:11:49 +01:00
|
|
|
struct peer *peer = tal(dstate, struct peer);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
assert(offer_anchor == CMD_OPEN_WITH_ANCHOR
|
|
|
|
|| offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->state = state;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->connected = false;
|
2016-07-01 03:57:57 +02:00
|
|
|
peer->id = NULL;
|
2016-01-21 21:11:49 +01:00
|
|
|
peer->dstate = dstate;
|
2016-01-21 21:11:48 +01:00
|
|
|
peer->io_data = NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
peer->secrets = NULL;
|
2016-01-21 21:11:49 +01:00
|
|
|
list_head_init(&peer->watches);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->outpkt = tal_arr(peer, Pkt *, 0);
|
2016-05-26 07:55:25 +02:00
|
|
|
peer->commit_jsoncmd = NULL;
|
2016-05-04 08:33:10 +02:00
|
|
|
list_head_init(&peer->outgoing_txs);
|
2016-08-18 06:53:45 +02:00
|
|
|
list_head_init(&peer->their_commits);
|
2016-08-18 06:53:45 +02:00
|
|
|
peer->anchor.ok_depth = -1;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->order_counter = 0;
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->their_commitsigs = 0;
|
2016-01-21 21:15:28 +01:00
|
|
|
peer->cur_commit.watch = NULL;
|
2016-03-24 02:39:41 +01:00
|
|
|
peer->closing.their_sig = NULL;
|
2016-05-02 08:26:56 +02:00
|
|
|
peer->closing.our_script = NULL;
|
|
|
|
peer->closing.their_script = NULL;
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->closing.shutdown_order = (s64)-1LL;
|
|
|
|
peer->closing.closing_order = (s64)-1LL;
|
2016-08-18 06:55:08 +02:00
|
|
|
peer->closing.sigs_in = 0;
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.tx = NULL;
|
|
|
|
peer->onchain.resolved = NULL;
|
|
|
|
peer->onchain.htlcs = NULL;
|
|
|
|
peer->onchain.wscripts = NULL;
|
2016-05-09 23:00:11 +02:00
|
|
|
peer->commit_timer = NULL;
|
2016-06-30 01:38:11 +02:00
|
|
|
peer->nc = NULL;
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->their_prev_revocation_hash = NULL;
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->conn = NULL;
|
2016-05-02 08:34:56 +02:00
|
|
|
peer->fake_close = false;
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->output_enabled = true;
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.offer_anchor = offer_anchor;
|
2016-06-28 23:19:21 +02:00
|
|
|
if (!blocks_to_rel_locktime(dstate->config.locktime_blocks,
|
|
|
|
&peer->local.locktime))
|
|
|
|
fatal("Could not convert locktime_blocks");
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.mindepth = dstate->config.anchor_confirms;
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->local.commit = peer->remote.commit = NULL;
|
|
|
|
peer->local.staging_cstate = peer->remote.staging_cstate = NULL;
|
2016-08-31 08:36:31 +02:00
|
|
|
peer->log = tal_steal(peer, log);
|
|
|
|
log_debug(peer->log, "New peer %p", peer);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
htlc_map_init(&peer->htlcs);
|
2016-08-26 08:01:19 +02:00
|
|
|
memset(peer->feechanges, 0, sizeof(peer->feechanges));
|
2016-08-18 06:55:13 +02:00
|
|
|
shachain_init(&peer->their_preimages);
|
|
|
|
|
|
|
|
list_add(&dstate->peers, &peer->list);
|
|
|
|
tal_add_destructor(peer, destroy_peer);
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static struct peer_address *find_address(struct lightningd_state *dstate,
|
|
|
|
const struct pubkey *id)
|
|
|
|
{
|
|
|
|
struct peer_address *i;
|
|
|
|
|
|
|
|
list_for_each(&dstate->addresses, i, list) {
|
|
|
|
if (structeq(&id->pubkey, &i->id.pubkey))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool add_peer_address(struct lightningd_state *dstate,
|
|
|
|
const struct pubkey *id,
|
|
|
|
const struct netaddr *addr)
|
|
|
|
{
|
|
|
|
struct peer_address *a = find_address(dstate, id);
|
|
|
|
if (a) {
|
|
|
|
a->addr = *addr;
|
|
|
|
} else {
|
|
|
|
a = tal(dstate, struct peer_address);
|
|
|
|
a->addr = *addr;
|
|
|
|
a->id = *id;
|
|
|
|
list_add_tail(&dstate->addresses, &a->list);
|
|
|
|
}
|
|
|
|
return db_add_peer_address(dstate, a);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
static bool peer_first_connected(struct peer *peer,
|
|
|
|
struct io_conn *conn,
|
|
|
|
int addr_type, int addr_protocol,
|
|
|
|
struct io_data *iod,
|
|
|
|
const struct pubkey *id,
|
|
|
|
bool we_connected)
|
|
|
|
{
|
2016-08-31 08:36:31 +02:00
|
|
|
char *name, *idstr;
|
2016-08-18 06:55:14 +02:00
|
|
|
struct netaddr addr;
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->io_data = tal_steal(peer, iod);
|
|
|
|
peer->id = tal_dup(peer, struct pubkey, id);
|
2016-08-26 08:03:40 +02:00
|
|
|
peer->local.commit_fee_rate = desired_commit_feerate(peer->dstate);
|
2016-06-30 01:38:11 +02:00
|
|
|
|
2016-08-28 05:21:57 +02:00
|
|
|
peer->htlc_id_counter = 0;
|
2016-08-18 06:55:13 +02:00
|
|
|
|
|
|
|
/* If we free peer, conn should be closed, but can't be freed
|
|
|
|
* immediately so don't make peer a parent. */
|
|
|
|
peer->conn = conn;
|
|
|
|
io_set_finish(conn, peer_disconnect, peer);
|
2016-06-30 01:38:11 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
peer->anchor.min_depth = get_block_height(peer->dstate);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* FIXME: Attach IO logging for this peer. */
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!netaddr_from_fd(io_conn_fd(conn), addr_type, addr_protocol, &addr))
|
|
|
|
return false;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* Save/update address if we connected to them. */
|
|
|
|
if (we_connected && !add_peer_address(peer->dstate, peer->id, &addr))
|
|
|
|
return false;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
name = netaddr_name(peer, &addr);
|
2016-08-31 08:36:31 +02:00
|
|
|
idstr = pubkey_to_hexstr(name, peer->dstate->secpctx, peer->id);
|
|
|
|
log_info(peer->log, "Connected %s %s id %s, changing prefix",
|
|
|
|
we_connected ? "out to" : "in from", name, idstr);
|
|
|
|
set_log_prefix(peer->log, tal_fmt(name, "%s:", idstr));
|
2016-08-18 06:55:14 +02:00
|
|
|
tal_free(name);
|
2016-05-09 23:00:11 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
log_debug(peer->log, "Using fee rate %"PRIu64,
|
|
|
|
peer->local.commit_fee_rate);
|
2016-08-18 06:55:14 +02:00
|
|
|
return true;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static u64 peer_commitsigs_received(struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->their_commitsigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 peer_revocations_received(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* How many preimages we've received. */
|
|
|
|
return -peer->their_preimages.min_index;
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
static void htlc_destroy(struct htlc *htlc)
|
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
if (!htlc_map_del(&htlc->peer->htlcs, htlc))
|
2016-06-30 01:38:11 +02:00
|
|
|
fatal("Could not find htlc to destroy");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct htlc *peer_new_htlc(struct peer *peer,
|
|
|
|
u64 id,
|
|
|
|
u64 msatoshis,
|
|
|
|
const struct sha256 *rhash,
|
|
|
|
u32 expiry,
|
|
|
|
const u8 *route,
|
|
|
|
size_t routelen,
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *src,
|
2016-08-18 06:53:45 +02:00
|
|
|
enum htlc_state state)
|
2016-06-30 01:38:11 +02:00
|
|
|
{
|
|
|
|
struct htlc *h = tal(peer, struct htlc);
|
|
|
|
h->peer = peer;
|
2016-08-18 06:53:45 +02:00
|
|
|
h->state = state;
|
2016-06-30 01:38:11 +02:00
|
|
|
h->id = id;
|
|
|
|
h->msatoshis = msatoshis;
|
|
|
|
h->rhash = *rhash;
|
2016-06-30 01:39:11 +02:00
|
|
|
h->r = NULL;
|
2016-08-31 07:21:41 +02:00
|
|
|
h->fail = NULL;
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!blocks_to_abs_locktime(expiry, &h->expiry))
|
|
|
|
fatal("Invalid HTLC expiry %u", expiry);
|
|
|
|
h->routing = tal_dup_arr(h, u8, route, routelen, 0);
|
2016-06-30 01:38:11 +02:00
|
|
|
h->src = src;
|
2016-08-18 06:53:45 +02:00
|
|
|
if (htlc_owner(h) == LOCAL) {
|
2016-07-01 03:49:28 +02:00
|
|
|
if (src) {
|
|
|
|
h->deadline = abs_locktime_to_blocks(&src->expiry)
|
|
|
|
- peer->dstate->config.deadline_blocks;
|
|
|
|
} else
|
|
|
|
/* If we're paying, give it a little longer. */
|
|
|
|
h->deadline = expiry
|
|
|
|
+ peer->dstate->config.min_htlc_expiry;
|
|
|
|
} else {
|
2016-08-18 06:53:45 +02:00
|
|
|
assert(htlc_owner(h) == REMOTE);
|
2016-06-30 01:38:11 +02:00
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
htlc_map_add(&peer->htlcs, h);
|
2016-06-30 01:38:11 +02:00
|
|
|
tal_add_destructor(h, htlc_destroy);
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
static struct io_plan *reconnect_pkt_in(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
if (peer->inpkt->pkt_case != PKT__PKT_RECONNECT) {
|
2016-08-18 06:55:13 +02:00
|
|
|
peer_received_unexpected_pkt(peer, peer->inpkt, __func__);
|
2016-08-18 06:55:08 +02:00
|
|
|
return pkt_out(conn, peer);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* We need to eliminate queue now. */
|
2016-08-18 06:55:13 +02:00
|
|
|
clear_output_queue(peer);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* They might have missed the error, tell them before hanging up */
|
|
|
|
if (state_is_error(peer->state)) {
|
|
|
|
queue_pkt_err(peer, pkt_err(peer, "In error state %s",
|
|
|
|
state_name(peer->state)));
|
|
|
|
return pkt_out(conn, peer);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
/* Send any packets they missed. */
|
|
|
|
retransmit_pkts(peer, peer->inpkt->reconnect->ack);
|
|
|
|
|
|
|
|
/* We let the conversation go this far in case they missed the
|
|
|
|
* close packets. But now we can close if we're done. */
|
|
|
|
if (!state_can_io(peer->state)) {
|
|
|
|
log_debug(peer->log, "State %s, closing immediately",
|
|
|
|
state_name(peer->state));
|
2016-08-18 06:55:14 +02:00
|
|
|
return pkt_out(conn, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We could have commitments pending from before. */
|
|
|
|
if (peer_uncommitted_changes(peer)) {
|
|
|
|
log_debug(peer->log, "reconnect_pkt_in: changes pending.");
|
|
|
|
remote_changes_pending(peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Back into normal mode. */
|
|
|
|
assert(!peer->connected);
|
|
|
|
peer->connected = true;
|
|
|
|
return io_duplex(conn,
|
|
|
|
peer_read_packet(conn, peer, pkt_in),
|
|
|
|
pkt_out(conn, peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *read_reconnect_pkt(struct io_conn *conn,
|
|
|
|
struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer_read_packet(conn, peer, reconnect_pkt_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *crypto_on_reconnect(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
|
|
|
const struct pubkey *id,
|
2016-08-18 06:55:13 +02:00
|
|
|
struct peer *peer,
|
|
|
|
bool we_connected)
|
2016-08-18 06:55:08 +02:00
|
|
|
{
|
|
|
|
u64 sigs, revokes, shutdown, closing;
|
|
|
|
|
|
|
|
/* Setup peer->conn and peer->io_data */
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!peer_reconnected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
|
|
|
|
iod, id, we_connected))
|
2016-08-18 06:55:08 +02:00
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
sigs = peer_commitsigs_received(peer);
|
|
|
|
revokes = peer_revocations_received(peer);
|
|
|
|
shutdown = peer->closing.their_script ? 1 : 0;
|
|
|
|
closing = peer->closing.sigs_in;
|
|
|
|
log_debug(peer->log,
|
|
|
|
"Reconnecting with ack %"PRIu64" sigs + %"PRIu64" revokes"
|
|
|
|
" + %"PRIu64" shutdown + %"PRIu64" closing",
|
|
|
|
sigs, revokes, shutdown, closing);
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node reconnecting after receiving or sending an `open_channel`
|
|
|
|
* message SHOULD send a `reconnect` message on the new connection
|
|
|
|
* immediately after it has validated the `authenticate` message. */
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST set the `ack` field in the `reconnect` message to the
|
|
|
|
* the sum of previously-processed messages of types
|
|
|
|
* `open_commit_sig`, `update_commit`, `update_revocation`,
|
|
|
|
* `close_shutdown` and `close_signature`. */
|
|
|
|
return peer_write_packet(conn, peer,
|
|
|
|
pkt_reconnect(peer, sigs + revokes
|
|
|
|
+ shutdown + closing),
|
|
|
|
read_reconnect_pkt);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
static struct io_plan *crypto_on_reconnect_in(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *log,
|
2016-08-18 06:55:13 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
struct peer *peer)
|
|
|
|
{
|
2016-08-31 08:36:31 +02:00
|
|
|
assert(log == peer->log);
|
2016-08-18 06:55:13 +02:00
|
|
|
return crypto_on_reconnect(conn, dstate, iod, id, peer, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *crypto_on_reconnect_out(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *log,
|
2016-08-18 06:55:13 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
struct peer *peer)
|
|
|
|
{
|
2016-08-31 08:36:31 +02:00
|
|
|
assert(log == peer->log);
|
2016-08-18 06:55:13 +02:00
|
|
|
return crypto_on_reconnect(conn, dstate, iod, id, peer, true);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *crypto_on_out(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *log,
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
struct json_connecting *connect)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:14:25 +01:00
|
|
|
/* Initiator currently funds channel */
|
2016-08-31 08:36:31 +02:00
|
|
|
struct peer *peer = new_peer(dstate, log, STATE_INIT, CMD_OPEN_WITH_ANCHOR);
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
|
|
|
|
iod, id, true)) {
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(connect->cmd, "Failed to make peer for %s:%s",
|
|
|
|
connect->name, connect->port);
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->io_data = tal_steal(peer, iod);
|
|
|
|
peer->id = tal_dup(peer, struct pubkey, id);
|
2016-04-12 05:37:04 +02:00
|
|
|
peer->anchor.input = tal_steal(peer, connect->input);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
command_success(connect->cmd, null_response(connect));
|
2016-08-18 06:53:46 +02:00
|
|
|
return peer_crypto_on(conn, peer);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *peer_connected_out(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct json_connecting *connect)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *l;
|
|
|
|
const char *name;
|
|
|
|
struct netaddr addr;
|
2016-08-18 06:53:46 +02:00
|
|
|
|
2016-08-31 08:36:31 +02:00
|
|
|
l = new_log(conn, dstate->log_record, "OUT-%s:%s:",
|
|
|
|
connect->name, connect->port);
|
|
|
|
|
|
|
|
if (!netaddr_from_fd(io_conn_fd(conn), SOCK_STREAM, IPPROTO_TCP, &addr)) {
|
|
|
|
log_unusual(l, "Failed to get netaddr: %s", strerror(errno));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
name = netaddr_name(conn, &addr);
|
|
|
|
|
|
|
|
log_debug(l, "Connected out to %s", name);
|
|
|
|
return peer_crypto_setup(conn, dstate, NULL, l, crypto_on_out, connect);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *crypto_on_in(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
2016-08-31 08:36:31 +02:00
|
|
|
struct log *log,
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
void *unused)
|
|
|
|
{
|
2016-08-18 06:55:08 +02:00
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST handle continuing a previous channel on a new encrypted
|
|
|
|
* transport. */
|
|
|
|
peer = find_peer(dstate, id);
|
|
|
|
if (peer) {
|
|
|
|
/* Close any existing connection, without side effects. */
|
|
|
|
if (peer->conn) {
|
2016-08-31 08:36:31 +02:00
|
|
|
log_debug(log, "This is reconnect for peer %p", peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "Reconnect: closing old conn %p for new conn %p",
|
|
|
|
peer->conn, conn);
|
|
|
|
io_set_finish(peer->conn, NULL, NULL);
|
|
|
|
io_close(peer->conn);
|
|
|
|
peer->conn = NULL;
|
|
|
|
peer->connected = false;
|
|
|
|
}
|
2016-08-31 08:36:31 +02:00
|
|
|
return crypto_on_reconnect_in(conn, dstate, iod, peer->log, id,
|
|
|
|
peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Initiator currently funds channel */
|
2016-08-31 08:36:31 +02:00
|
|
|
peer = new_peer(dstate, log, STATE_INIT, CMD_OPEN_WITHOUT_ANCHOR);
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
|
|
|
|
iod, id, false))
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_close(conn);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
return peer_crypto_on(conn, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_connected_in(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate)
|
|
|
|
{
|
2016-08-31 08:36:31 +02:00
|
|
|
struct netaddr addr;
|
|
|
|
struct log *l;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
if (!netaddr_from_fd(io_conn_fd(conn), SOCK_STREAM, IPPROTO_TCP, &addr))
|
|
|
|
return false;
|
|
|
|
name = netaddr_name(conn, &addr);
|
|
|
|
l = new_log(conn, dstate->log_record, "IN-%s:", name);
|
|
|
|
|
|
|
|
log_debug(l, "Connected in");
|
2016-08-18 06:53:46 +02:00
|
|
|
|
2016-08-31 08:36:31 +02:00
|
|
|
return peer_crypto_setup(conn, dstate, NULL, l, crypto_on_in, NULL);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
static int make_listen_fd(struct lightningd_state *dstate,
|
2016-01-21 21:11:48 +01:00
|
|
|
int domain, void *addr, socklen_t len)
|
|
|
|
{
|
|
|
|
int fd = socket(domain, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_debug(dstate->base_log, "Failed to create %u socket: %s",
|
2016-01-21 21:11:48 +01:00
|
|
|
domain, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:20 +02:00
|
|
|
if (addr) {
|
|
|
|
int on = 1;
|
|
|
|
|
|
|
|
/* Re-use, please.. */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
|
|
|
|
log_unusual(dstate->base_log,
|
|
|
|
"Failed setting socket reuse: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
if (bind(fd, addr, len) != 0) {
|
|
|
|
log_unusual(dstate->base_log,
|
|
|
|
"Failed to bind on %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 5) != 0) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(dstate->base_log,
|
|
|
|
"Failed to listen on %u socket: %s",
|
2016-01-21 21:11:48 +01:00
|
|
|
domain, strerror(errno));
|
2016-08-18 06:53:20 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return fd;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:20 +02:00
|
|
|
fail:
|
2016-01-21 21:11:48 +01:00
|
|
|
close_noerr(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
socklen_t len;
|
|
|
|
int fd1, fd2;
|
|
|
|
u16 listen_port;
|
|
|
|
|
2016-08-18 06:53:20 +02:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2016-01-21 21:11:48 +01:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = htons(portnum);
|
|
|
|
|
2016-08-18 06:53:20 +02:00
|
|
|
memset(&addr6, 0, sizeof(addr6));
|
2016-01-21 21:11:48 +01:00
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
addr6.sin6_addr = in6addr_any;
|
|
|
|
addr6.sin6_port = htons(portnum);
|
|
|
|
|
|
|
|
/* IPv6, since on Linux that (usually) binds to IPv4 too. */
|
2016-01-21 21:11:49 +01:00
|
|
|
fd1 = make_listen_fd(dstate, AF_INET6, portnum ? &addr6 : NULL,
|
2016-01-21 21:11:48 +01:00
|
|
|
sizeof(addr6));
|
|
|
|
if (fd1 >= 0) {
|
|
|
|
struct sockaddr_in6 in6;
|
|
|
|
|
|
|
|
len = sizeof(in6);
|
|
|
|
if (getsockname(fd1, (void *)&in6, &len) != 0) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Failed get IPv6 sockname: %s",
|
|
|
|
strerror(errno));
|
|
|
|
close_noerr(fd1);
|
|
|
|
} else {
|
|
|
|
addr.sin_port = in6.sin6_port;
|
|
|
|
listen_port = ntohs(addr.sin_port);
|
2016-01-21 21:11:49 +01:00
|
|
|
log_info(dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Creating IPv6 listener on port %u",
|
|
|
|
listen_port);
|
2016-01-21 21:11:49 +01:00
|
|
|
io_new_listener(dstate, fd1, peer_connected_in, dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just in case, aim for the same port... */
|
2016-01-21 21:11:49 +01:00
|
|
|
fd2 = make_listen_fd(dstate, AF_INET,
|
2016-01-21 21:11:48 +01:00
|
|
|
addr.sin_port ? &addr : NULL, sizeof(addr));
|
|
|
|
if (fd2 >= 0) {
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(fd2, (void *)&addr, &len) != 0) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Failed get IPv4 sockname: %s",
|
|
|
|
strerror(errno));
|
|
|
|
close_noerr(fd2);
|
|
|
|
} else {
|
|
|
|
listen_port = ntohs(addr.sin_port);
|
2016-01-21 21:11:49 +01:00
|
|
|
log_info(dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Creating IPv4 listener on port %u",
|
|
|
|
listen_port);
|
2016-01-21 21:11:49 +01:00
|
|
|
io_new_listener(dstate, fd2, peer_connected_in, dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd1 < 0 && fd2 < 0)
|
|
|
|
fatal("Could not bind to a network address");
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
static void peer_failed(struct lightningd_state *dstate,
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_connecting *connect)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
/* FIXME: Better diagnostics! */
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(connect->cmd, "Failed to connect to peer %s:%s",
|
|
|
|
connect->name, connect->port);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_connect(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_connecting *connect;
|
2016-04-12 05:37:04 +02:00
|
|
|
jsmntok_t *host, *port, *txtok;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
int output;
|
|
|
|
size_t txhexlen;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"host", &host,
|
|
|
|
"port", &port,
|
2016-04-12 05:37:04 +02:00
|
|
|
"tx", &txtok,
|
2016-03-15 07:37:30 +01:00
|
|
|
NULL)) {
|
2016-04-12 05:37:04 +02:00
|
|
|
command_fail(cmd, "Need host, port and tx to a wallet address");
|
2016-01-21 21:11:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
connect = tal(cmd, struct json_connecting);
|
|
|
|
connect->cmd = cmd;
|
|
|
|
connect->name = tal_strndup(connect, buffer + host->start,
|
|
|
|
host->end - host->start);
|
|
|
|
connect->port = tal_strndup(connect, buffer + port->start,
|
|
|
|
port->end - port->start);
|
2016-04-12 05:37:04 +02:00
|
|
|
connect->input = tal(connect, struct anchor_input);
|
|
|
|
|
|
|
|
txhexlen = txtok->end - txtok->start;
|
|
|
|
tx = bitcoin_tx_from_hex(connect->input, buffer + txtok->start,
|
|
|
|
txhexlen);
|
|
|
|
if (!tx) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid transaction",
|
|
|
|
txtok->end - txtok->start,
|
|
|
|
buffer + txtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &connect->input->txid);
|
|
|
|
|
|
|
|
/* Find an output we know how to spend. */
|
|
|
|
connect->input->w = NULL;
|
|
|
|
for (output = 0; output < tx->output_count; output++) {
|
|
|
|
connect->input->w
|
|
|
|
= wallet_can_spend(cmd->dstate, &tx->output[output]);
|
|
|
|
if (connect->input->w)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!connect->input->w) {
|
|
|
|
command_fail(cmd, "Tx doesn't send to wallet address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect->input->index = output;
|
|
|
|
connect->input->amount = tx->output[output].amount;
|
2016-01-21 21:11:49 +01:00
|
|
|
if (!dns_resolve_and_connect(cmd->dstate, connect->name, connect->port,
|
2016-01-21 21:11:48 +01:00
|
|
|
peer_connected_out, peer_failed, connect)) {
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(cmd, "DNS failed");
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command connect_command = {
|
|
|
|
"connect",
|
|
|
|
json_connect,
|
2016-08-19 03:04:12 +02:00
|
|
|
"Connect to a {host} at {port} using hex-encoded {tx} to fund",
|
2016-01-21 21:11:48 +01:00
|
|
|
"Returns an empty result on success"
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
/* Have any of our HTLCs passed their deadline? */
|
2016-08-18 06:53:46 +02:00
|
|
|
static bool any_deadline_past(struct peer *peer)
|
2016-07-01 03:49:28 +02:00
|
|
|
{
|
|
|
|
u32 height = get_block_height(peer->dstate);
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (htlc_is_dead(h))
|
|
|
|
continue;
|
|
|
|
if (htlc_owner(h) != LOCAL)
|
|
|
|
continue;
|
|
|
|
if (height >= h->deadline) {
|
2016-07-01 03:49:28 +02:00
|
|
|
log_unusual_struct(peer->log,
|
|
|
|
"HTLC %s deadline has passed",
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc, h);
|
2016-07-01 03:49:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
static void check_htlc_expiry(struct peer *peer)
|
|
|
|
{
|
|
|
|
u32 height = get_block_height(peer->dstate);
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Check their currently still-existing htlcs for expiry */
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
assert(!abs_locktime_is_seconds(&h->expiry));
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Only their consider HTLCs which are completely locked in. */
|
|
|
|
if (h->state != RCVD_ADD_ACK_REVOCATION)
|
|
|
|
continue;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* We give it an extra block, to avoid the worst of the
|
|
|
|
* inter-node timing issues. */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (height <= abs_locktime_to_blocks(&h->expiry))
|
2016-06-28 23:19:21 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* This can fail only if we're in an error state. */
|
2016-08-31 07:21:41 +02:00
|
|
|
if (!command_htlc_set_fail(peer, h,
|
|
|
|
REQUEST_TIMEOUT_408, "timed out"))
|
2016-06-28 23:19:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-07-01 03:49:28 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* To save logic elsewhere (ie. to avoid signing a new commit with a
|
|
|
|
* past-deadline HTLC) we also check staged HTLCs.
|
|
|
|
*/
|
|
|
|
if (!state_is_normal(peer->state))
|
|
|
|
return;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (any_deadline_past(peer)) {
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, __func__, false);
|
2016-07-01 03:49:28 +02:00
|
|
|
peer_breakdown(peer);
|
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result anchor_depthchange(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
2016-01-21 21:15:05 +01:00
|
|
|
{
|
|
|
|
/* Still waiting for it to reach depth? */
|
2016-08-18 06:53:45 +02:00
|
|
|
if (state_is_opening(peer->state)) {
|
|
|
|
if ((int)depth >= peer->anchor.ok_depth) {
|
|
|
|
state_event(peer, BITCOIN_ANCHOR_DEPTHOK, NULL);
|
|
|
|
peer->anchor.ok_depth = -1;
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
} else if (depth == 0)
|
|
|
|
/* FIXME: Report losses! */
|
|
|
|
fatal("Funding transaction was unspent!");
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Since this gets called on every new block, check HTLCs here. */
|
|
|
|
check_htlc_expiry(peer);
|
2016-08-26 08:03:40 +02:00
|
|
|
|
|
|
|
/* If fee rate has changed, fire off update to change it. */
|
|
|
|
if (peer->local.staging_cstate->fee_rate
|
|
|
|
!= desired_commit_feerate(peer->dstate)) {
|
|
|
|
log_debug(peer->log, "fee rate changed to %"PRIu64,
|
|
|
|
desired_commit_feerate(peer->dstate));
|
|
|
|
remote_changes_pending(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST update bitcoin fees if it estimates that the
|
|
|
|
* current commitment transaction will not be processed in a
|
|
|
|
* timely manner (see "Risks With HTLC Timeouts").
|
|
|
|
*/
|
|
|
|
/* FIXME: BOLT should say what to do if it can't! We drop conn. */
|
|
|
|
if (peer->local.commit->cstate->fee_rate < get_feerate(peer->dstate)) {
|
|
|
|
log_broken(peer->log, "fee rate %"PRIu64" lower than %"PRIu64,
|
|
|
|
peer->local.commit->cstate->fee_rate,
|
|
|
|
get_feerate(peer->dstate));
|
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, __func__, false);
|
|
|
|
peer_breakdown(peer);
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static bool outputscript_eq(const struct bitcoin_tx_output *out,
|
|
|
|
size_t i, const u8 *script)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
if (out[i].script_length != tal_count(script))
|
|
|
|
return false;
|
|
|
|
return memcmp(out[i].script, script, out[i].script_length) == 0;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* This tx is their commitment;
|
2016-08-18 06:53:46 +02:00
|
|
|
* fill in onchain.htlcs[], wscripts[], to_us_idx and to_them_idx */
|
2016-08-18 06:53:46 +02:00
|
|
|
static bool map_onchain_outputs(struct peer *peer,
|
|
|
|
const struct sha256 *rhash,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
enum htlc_side side,
|
|
|
|
unsigned int commit_num)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
|
|
|
u8 *to_us, *to_them, *to_them_wscript, *to_us_wscript;
|
|
|
|
struct htlc_output_map *hmap;
|
|
|
|
size_t i;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.to_us_idx = peer->onchain.to_them_idx = -1;
|
|
|
|
peer->onchain.htlcs = tal_arr(tx, struct htlc *, tx->output_count);
|
|
|
|
peer->onchain.wscripts = tal_arr(tx, const u8 *, tx->output_count);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
to_us = commit_output_to_us(tx, peer, rhash, side, &to_us_wscript);
|
|
|
|
to_them = commit_output_to_them(tx, peer, rhash, side,
|
2016-08-18 06:53:45 +02:00
|
|
|
&to_them_wscript);
|
|
|
|
|
|
|
|
/* Now generate the wscript hashes for every possible HTLC. */
|
2016-08-18 06:53:46 +02:00
|
|
|
hmap = get_htlc_output_map(tx, peer, rhash, side, commit_num);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) {
|
|
|
|
log_debug(peer->log, "%s: output %zi", __func__, i);
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.to_us_idx == -1
|
2016-08-18 06:53:46 +02:00
|
|
|
&& outputscript_eq(tx->output, i, to_us)) {
|
2016-08-18 06:53:45 +02:00
|
|
|
log_add(peer->log, " -> to us");
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.htlcs[i] = NULL;
|
|
|
|
peer->onchain.wscripts[i] = to_us_wscript;
|
|
|
|
peer->onchain.to_us_idx = i;
|
2016-08-18 06:53:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.to_them_idx == -1
|
2016-08-18 06:53:46 +02:00
|
|
|
&& outputscript_eq(tx->output, i, to_them)) {
|
2016-08-18 06:53:45 +02:00
|
|
|
log_add(peer->log, " -> to them");
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.htlcs[i] = NULL;
|
|
|
|
peer->onchain.wscripts[i] = to_them_wscript;
|
|
|
|
peer->onchain.to_them_idx = i;
|
2016-08-18 06:53:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Must be an HTLC output */
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.htlcs[i] = txout_get_htlc(hmap,
|
2016-08-18 06:53:45 +02:00
|
|
|
tx->output[i].script,
|
|
|
|
tx->output[i].script_length,
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.wscripts+i);
|
|
|
|
if (!peer->onchain.htlcs[i]) {
|
2016-08-18 06:53:45 +02:00
|
|
|
log_add(peer->log, "no HTLC found");
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_steal(peer->onchain.htlcs, peer->onchain.htlcs[i]);
|
|
|
|
tal_steal(peer->onchain.wscripts, peer->onchain.wscripts[i]);
|
|
|
|
log_add(peer->log, "HTLC %"PRIu64, peer->onchain.htlcs[i]->id);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
|
|
|
tal_free(hmap);
|
|
|
|
return true;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
fail:
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_free(hmap);
|
|
|
|
return false;
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
static bool is_mutual_close(const struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-05-02 08:26:56 +02:00
|
|
|
const u8 *ours, *theirs;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-02 08:26:56 +02:00
|
|
|
ours = peer->closing.our_script;
|
|
|
|
theirs = peer->closing.their_script;
|
|
|
|
/* If we don't know the closing scripts, can't have signed them. */
|
|
|
|
if (!ours || !theirs)
|
2016-01-21 21:15:28 +01:00
|
|
|
return false;
|
|
|
|
|
2016-05-02 08:26:56 +02:00
|
|
|
if (tx->output_count != 2)
|
2016-01-21 21:15:28 +01:00
|
|
|
return false;
|
2016-03-24 02:39:41 +01:00
|
|
|
|
2016-05-02 08:26:56 +02:00
|
|
|
/* Without knowing fee amounts, can't determine order. Check both. */
|
|
|
|
if (scripteq(tx->output[0].script, tx->output[0].script_length,
|
|
|
|
ours, tal_count(ours))
|
|
|
|
&& scripteq(tx->output[1].script, tx->output[1].script_length,
|
|
|
|
theirs, tal_count(theirs)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (scripteq(tx->output[0].script, tx->output[0].script_length,
|
|
|
|
theirs, tal_count(theirs))
|
|
|
|
&& scripteq(tx->output[1].script, tx->output[1].script_length,
|
|
|
|
ours, tal_count(ours)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Create a HTLC refund collection for onchain.tx output out_num. */
|
2016-05-04 08:44:22 +02:00
|
|
|
static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
|
2016-08-18 06:53:46 +02:00
|
|
|
unsigned int out_num)
|
2016-05-04 08:42:47 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct htlc *htlc = peer->onchain.htlcs[out_num];
|
|
|
|
const u8 *wscript = peer->onchain.wscripts[out_num];
|
2016-05-04 08:42:47 +02:00
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
u64 fee, satoshis;
|
|
|
|
|
|
|
|
/* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */
|
|
|
|
tx->lock_time = htlc->expiry.locktime;
|
2016-08-18 06:53:46 +02:00
|
|
|
tx->input[0].index = out_num;
|
2016-08-18 06:53:46 +02:00
|
|
|
tx->input[0].txid = peer->onchain.txid;
|
2016-05-04 08:42:47 +02:00
|
|
|
satoshis = htlc->msatoshis / 1000;
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &satoshis);
|
2016-05-26 07:55:24 +02:00
|
|
|
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
|
2016-05-04 08:42:47 +02:00
|
|
|
|
|
|
|
/* Using a new output address here would be useless: they can tell
|
|
|
|
* it's our HTLC, and that we collected it via timeout. */
|
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx,
|
2016-07-01 03:49:28 +02:00
|
|
|
bitcoin_redeem_single(tx,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey));
|
2016-05-04 08:42:47 +02:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
|
|
|
log_unusual(peer->log, "Pre-witness txlen = %zu\n",
|
|
|
|
measure_tx_cost(tx) / 4);
|
|
|
|
|
|
|
|
assert(measure_tx_cost(tx) == 83 * 4);
|
|
|
|
|
|
|
|
/* Witness length can vary, due to DER encoding of sigs, but we
|
|
|
|
* use 539 from an example run. */
|
2016-08-18 06:53:46 +02:00
|
|
|
fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate));
|
2016-05-04 08:42:47 +02:00
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (fee > satoshis || is_dust(satoshis - fee))
|
2016-05-04 08:42:47 +02:00
|
|
|
fatal("HTLC refund amount of %"PRIu64" won't cover fee %"PRIu64,
|
|
|
|
satoshis, fee);
|
|
|
|
|
|
|
|
tx->output[0].amount = satoshis - fee;
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_htlc_refund(peer, tx, wscript, &sig.sig);
|
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
tx->input[0].witness = bitcoin_witness_htlc(tx, peer->dstate->secpctx,
|
|
|
|
NULL, &sig, wscript);
|
2016-05-04 08:42:47 +02:00
|
|
|
|
|
|
|
log_unusual(peer->log, "tx cost for htlc timeout tx: %zu",
|
|
|
|
measure_tx_cost(tx));
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
static void reset_onchain_closing(struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.tx) {
|
2016-08-31 08:36:32 +02:00
|
|
|
log_unusual_struct(peer->log,
|
|
|
|
"New anchor spend, forgetting old tx %s",
|
|
|
|
struct sha256_double, &peer->onchain.txid);
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.tx = tal_free(peer->onchain.tx);
|
|
|
|
peer->onchain.resolved = NULL;
|
|
|
|
peer->onchain.htlcs = NULL;
|
|
|
|
peer->onchain.wscripts = NULL;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct bitcoin_tx *irrevocably_resolved(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* We can't all be irrevocably resolved until the commit tx is,
|
|
|
|
* so just mark that as resolving us. */
|
2016-08-18 06:53:46 +02:00
|
|
|
return peer->onchain.tx;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
/* We usually don't fail HTLCs we offered, but if the peer breaks down
|
|
|
|
* before we've confirmed it, this is exactly what happens. */
|
|
|
|
static void fail_own_htlc(struct peer *peer, struct htlc *htlc)
|
|
|
|
{
|
|
|
|
set_htlc_fail(peer, htlc, "peer closed", strlen("peer closed"));
|
|
|
|
our_htlc_failed(peer, htlc);
|
|
|
|
}
|
|
|
|
|
2016-07-01 03:46:58 +02:00
|
|
|
/* We've spent an HTLC output to get our funds back. There's still a
|
|
|
|
* chance that they could also spend the HTLC output (using the preimage),
|
|
|
|
* so we need to wait for some confirms.
|
|
|
|
*
|
|
|
|
* However, we don't want to wait too long: our upstream will get upset if
|
|
|
|
* their HTLC has timed out and we don't close it. So we wait one less
|
|
|
|
* than the HTLC timeout difference.
|
|
|
|
*/
|
|
|
|
static enum watch_result our_htlc_timeout_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc *htlc)
|
2016-07-01 03:46:58 +02:00
|
|
|
{
|
|
|
|
if (depth == 0)
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
if (depth + 1 < peer->dstate->config.min_htlc_expiry)
|
|
|
|
return KEEP_WATCHING;
|
2016-08-31 07:21:41 +02:00
|
|
|
fail_own_htlc(peer, htlc);
|
2016-07-01 03:46:58 +02:00
|
|
|
return DELETE_WATCH;
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result our_htlc_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
enum htlc_side whose_commit,
|
|
|
|
unsigned int out_num)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc *h = peer->onchain.htlcs[out_num];
|
2016-06-28 23:19:21 +02:00
|
|
|
u32 height;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* Must be in a block. */
|
|
|
|
if (depth == 0)
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
height = get_block_height(peer->dstate);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If the *commitment tx* is the other node's, the output is
|
|
|
|
* considered *timed out* once the HTLC is expired. If the
|
|
|
|
* *commitment tx* is this node's, the output is considered *timed
|
|
|
|
* out* once the HTLC is expired, AND the output's
|
|
|
|
* `OP_CHECKSEQUENCEVERIFY` delay has passed.
|
|
|
|
*/
|
2016-06-28 23:19:21 +02:00
|
|
|
if (height < abs_locktime_to_blocks(&h->expiry))
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (whose_commit == LOCAL) {
|
2016-06-28 23:19:21 +02:00
|
|
|
if (depth < rel_locktime_to_blocks(&peer->remote.locktime))
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If the output has *timed out* and not been *resolved*, the node
|
|
|
|
* MUST *resolve* the output by spending it.
|
|
|
|
*/
|
2016-06-30 01:40:11 +02:00
|
|
|
/* FIXME: we should simply delete this watch if HTLC is fulfilled. */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!peer->onchain.resolved[out_num]) {
|
|
|
|
peer->onchain.resolved[out_num] = htlc_timeout_tx(peer, out_num);
|
|
|
|
watch_tx(peer->onchain.resolved[out_num],
|
2016-07-01 03:46:58 +02:00
|
|
|
peer,
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[out_num],
|
2016-08-18 06:53:46 +02:00
|
|
|
our_htlc_timeout_depth, h);
|
2016-08-18 06:53:46 +02:00
|
|
|
broadcast_tx(peer, peer->onchain.resolved[out_num]);
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
2016-06-30 01:40:11 +02:00
|
|
|
return DELETE_WATCH;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result our_htlc_depth_theircommit(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
ptrint_t *out_num)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
return our_htlc_depth(peer, depth, txid, REMOTE, ptr2int(out_num));
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static enum watch_result our_htlc_depth_ourcommit(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
ptrint_t *out_num)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
return our_htlc_depth(peer, depth, txid, LOCAL, ptr2int(out_num));
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result their_htlc_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
ptrint_t *out_num)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-06-28 23:19:21 +02:00
|
|
|
u32 height;
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct htlc *htlc = peer->onchain.htlcs[ptr2int(out_num)];
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* Must be in a block. */
|
|
|
|
if (depth == 0)
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
height = get_block_height(peer->dstate);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Otherwise, if the output HTLC has expired, it is considered
|
|
|
|
* *irrevocably resolved*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (height < abs_locktime_to_blocks(&htlc->expiry))
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[ptr2int(out_num)] = irrevocably_resolved(peer);
|
2016-06-30 01:40:11 +02:00
|
|
|
return DELETE_WATCH;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result our_main_output_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Not past CSV timeout? */
|
|
|
|
if (depth < rel_locktime_to_blocks(&peer->remote.locktime))
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(peer->onchain.to_us_idx != -1);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 1. _A's main output_: A node SHOULD spend this output to a
|
|
|
|
* convenient address. This avoids having to remember the
|
|
|
|
* complicated witness script associated with that particular
|
|
|
|
* channel for later spending. ... If the output is spent (as
|
|
|
|
* recommended), the output is *resolved* by the spending
|
|
|
|
* transaction
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[peer->onchain.to_us_idx]
|
2016-08-18 06:53:46 +02:00
|
|
|
= bitcoin_spend_ours(peer);
|
2016-08-18 06:53:46 +02:00
|
|
|
broadcast_tx(peer, peer->onchain.resolved[peer->onchain.to_us_idx]);
|
2016-06-30 01:40:11 +02:00
|
|
|
return DELETE_WATCH;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 03:49:06 +02:00
|
|
|
/* Any of our HTLCs we didn't have in our commitment tx, but they did,
|
|
|
|
* we can't fail until we're sure our commitment tx will win. */
|
|
|
|
static enum watch_result our_unilateral_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-07-01 03:49:06 +02:00
|
|
|
|
|
|
|
if (depth < peer->dstate->config.min_htlc_expiry)
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (htlc_owner(h) == LOCAL
|
|
|
|
&& !htlc_has(h, HTLC_LOCAL_F_COMMITTED)
|
|
|
|
&& htlc_has(h, HTLC_REMOTE_F_COMMITTED)) {
|
|
|
|
log_debug(peer->log,
|
|
|
|
"%s:failing uncommitted htlc %"PRIu64,
|
|
|
|
__func__, h->id);
|
2016-08-31 07:21:41 +02:00
|
|
|
fail_own_htlc(peer, h);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
2016-07-01 03:49:06 +02:00
|
|
|
}
|
|
|
|
return DELETE_WATCH;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static enum watch_result our_htlc_spent(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
size_t input_num,
|
|
|
|
struct htlc *h)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
struct sha256 sha;
|
|
|
|
struct rval preimage;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* If a node sees a redemption transaction...the node MUST extract the
|
|
|
|
* preimage from the transaction input witness. This is either to
|
|
|
|
* prove payment (if this node originated the payment), or to redeem
|
|
|
|
* the corresponding incoming HTLC from another peer.
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
/* This is the form of all HTLC spends. */
|
|
|
|
if (!tx->input[input_num].witness
|
|
|
|
|| tal_count(tx->input[input_num].witness) != 3
|
|
|
|
|| tal_count(tx->input[input_num].witness[1]) != sizeof(preimage))
|
|
|
|
fatal("Impossible HTLC spend for %"PRIu64, h->id);
|
|
|
|
|
|
|
|
/* Our timeout tx has all-zeroes, so we can distinguish it. */
|
|
|
|
if (memeqzero(tx->input[input_num].witness[1], sizeof(preimage)))
|
|
|
|
/* They might try to race us. */
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
|
|
|
|
memcpy(&preimage, tx->input[input_num].witness[1], sizeof(preimage));
|
|
|
|
sha256(&sha, &preimage, sizeof(preimage));
|
|
|
|
|
|
|
|
/* FIXME: This could happen with a ripemd collision, since
|
|
|
|
* script.c only checks that ripemd matches... */
|
|
|
|
if (!structeq(&sha, &h->rhash))
|
|
|
|
fatal("HTLC redeemed with incorrect r value?");
|
|
|
|
|
|
|
|
log_unusual(peer->log, "Peer redeemed HTLC %"PRIu64" on-chain",
|
|
|
|
h->id);
|
|
|
|
log_add_struct(peer->log, " using rvalue %s", struct rval, &preimage);
|
|
|
|
|
2016-08-30 12:44:57 +02:00
|
|
|
set_htlc_rval(peer, h, &preimage);
|
|
|
|
our_htlc_fulfilled(peer, h);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* If a node sees a redemption transaction, the output is considered
|
|
|
|
* *irrevocably resolved*... Note that we don't care about the fate of
|
|
|
|
* the redemption transaction itself once we've extracted the
|
|
|
|
* preimage; the knowledge is not revocable.
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[tx->input[input_num].index]
|
2016-08-18 06:53:46 +02:00
|
|
|
= irrevocably_resolved(peer);
|
|
|
|
return DELETE_WATCH;
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void resolve_our_htlc(struct peer *peer,
|
|
|
|
unsigned int out_num,
|
|
|
|
enum watch_result (*cb)(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double*,
|
|
|
|
ptrint_t *out_num))
|
|
|
|
{
|
2016-05-04 08:44:22 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* A node MUST watch for spends of *commitment tx* outputs for HTLCs
|
|
|
|
* it offered; each one must be *resolved* by a timeout transaction
|
|
|
|
* (the node pays back to itself) or redemption transaction (the other
|
|
|
|
* node provides the redemption preimage).
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
watch_txo(peer->onchain.tx, peer, &peer->onchain.txid, out_num,
|
|
|
|
our_htlc_spent, peer->onchain.htlcs[out_num]);
|
|
|
|
watch_txid(peer->onchain.tx, peer,
|
|
|
|
&peer->onchain.txid, cb, int2ptr(out_num));
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void resolve_their_htlc(struct peer *peer, unsigned int out_num)
|
|
|
|
{
|
2016-05-04 08:44:22 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* If the node ... already knows... a redemption preimage for an
|
|
|
|
* unresolved *commitment tx* output it was offered, it MUST *resolve*
|
|
|
|
* the output by spending it using the preimage.
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.htlcs[out_num]->r) {
|
|
|
|
peer->onchain.resolved[out_num] = htlc_fulfill_tx(peer, out_num);
|
|
|
|
broadcast_tx(peer, peer->onchain.resolved[out_num]);
|
2016-08-18 06:53:46 +02:00
|
|
|
} else {
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Otherwise, if the output HTLC has expired, it is considered
|
|
|
|
* *irrevocably resolved*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
watch_tx(peer->onchain.tx, peer, peer->onchain.tx,
|
2016-08-18 06:53:46 +02:00
|
|
|
their_htlc_depth, int2ptr(out_num));
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* When node A sees its own *commitment tx*:
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
static void resolve_our_unilateral(struct peer *peer)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct bitcoin_tx *tx = peer->onchain.tx;
|
2016-08-18 06:53:46 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* This only works because we always watch for a long time before
|
|
|
|
* freeing peer, by which time this has resolved. We could create
|
|
|
|
* resolved[] entries for these uncommitted HTLCs, too. */
|
|
|
|
watch_tx(tx, peer, tx, our_unilateral_depth, NULL);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
for (i = 0; i < tx->output_count; i++) {
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 1. _A's main output_: A node SHOULD spend this output to a
|
|
|
|
* convenient address. ... A node MUST wait until the
|
|
|
|
* `OP_CHECKSEQUENCEVERIFY` delay has passed (as specified
|
|
|
|
* by the other node's `open_channel` `delay` field) before
|
|
|
|
* spending the output.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (i == peer->onchain.to_us_idx)
|
2016-08-18 06:53:46 +02:00
|
|
|
watch_tx(tx, peer, tx, our_main_output_depth, NULL);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 2. _B's main output_: No action required, this output is
|
|
|
|
* considered *resolved* by the *commitment tx*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
else if (i == peer->onchain.to_them_idx)
|
|
|
|
peer->onchain.resolved[i] = tx;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 3. _A's offered HTLCs_: See On-chain HTLC Handling: Our
|
|
|
|
* Offers below.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
else if (htlc_owner(peer->onchain.htlcs[i]) == LOCAL)
|
2016-08-18 06:53:46 +02:00
|
|
|
resolve_our_htlc(peer, i, our_htlc_depth_ourcommit);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 4. _B's offered HTLCs_: See On-chain HTLC Handling: Their
|
|
|
|
* Offers below.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
resolve_their_htlc(peer, i);
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Similarly, when node A sees a *commitment tx* from B:
|
|
|
|
*/
|
|
|
|
static void resolve_their_unilateral(struct peer *peer)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct bitcoin_tx *tx = peer->onchain.tx;
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) {
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 1. _A's main output_: No action is required; this is a
|
|
|
|
* simple P2WPKH output. This output is considered
|
|
|
|
* *resolved* by the *commitment tx*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (i == peer->onchain.to_us_idx)
|
|
|
|
peer->onchain.resolved[i] = tx;
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 2. _B's main output_: No action required, this output is
|
|
|
|
* considered *resolved* by the *commitment tx*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
else if (i == peer->onchain.to_them_idx)
|
|
|
|
peer->onchain.resolved[i] = tx;
|
2016-08-18 06:53:46 +02:00
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 3. _A's offered HTLCs_: See On-chain HTLC Handling: Our
|
|
|
|
* Offers below.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
else if (htlc_owner(peer->onchain.htlcs[i]) == LOCAL)
|
2016-08-18 06:53:46 +02:00
|
|
|
resolve_our_htlc(peer, i, our_htlc_depth_theircommit);
|
|
|
|
/*
|
|
|
|
* 4. _B's offered HTLCs_: See On-chain HTLC Handling: Their
|
|
|
|
* Offers below.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
resolve_their_htlc(peer, i);
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_mutual_close(struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
unsigned int i;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node doesn't need to do anything else as it has already agreed to
|
|
|
|
* the output, which is sent to its specified scriptpubkey (see BOLT
|
2016-08-18 06:53:46 +02:00
|
|
|
* #2 "4.1: Closing initiation: close_shutdown").
|
2016-05-04 08:44:22 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
for (i = 0; i < peer->onchain.tx->output_count; i++)
|
|
|
|
peer->onchain.resolved[i] = irrevocably_resolved(peer);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
/* No HTLCs. */
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.htlcs = tal_arrz(peer->onchain.tx,
|
|
|
|
struct htlc *,
|
|
|
|
peer->onchain.tx->output_count);
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called every time the tx spending the funding tx changes depth. */
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result check_for_resolution(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
2016-05-04 08:44:22 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
size_t i, n = tal_count(peer->onchain.resolved);
|
2016-05-04 08:44:22 +02:00
|
|
|
size_t forever = peer->dstate->config.forever_confirms;
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node MUST *resolve* all outputs as specified below, and MUST be
|
|
|
|
* prepared to resolve them multiple times in case of blockchain
|
|
|
|
* reorganizations.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n; i++)
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!peer->onchain.resolved[i])
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Outputs which are *resolved* by a transaction are considered
|
|
|
|
* *irrevocably resolved* once they are included in a block at least
|
|
|
|
* 100 deep on the most-work blockchain.
|
|
|
|
*/
|
|
|
|
if (depth < forever)
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
bitcoin_txid(peer->onchain.resolved[i], &txid);
|
2016-05-04 08:44:22 +02:00
|
|
|
if (get_tx_depth(peer->dstate, &txid) < forever)
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node MUST monitor the blockchain for transactions which spend any
|
|
|
|
* output which is not *irrevocably resolved* until all outputs are
|
|
|
|
* *irrevocably resolved*.
|
|
|
|
*/
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_CLOSED, "check_for_resolution", false);
|
|
|
|
db_forget_peer(peer);
|
2016-05-26 07:55:25 +02:00
|
|
|
|
|
|
|
/* It's theoretically possible that peer is still writing output */
|
|
|
|
if (!peer->conn)
|
|
|
|
io_break(peer);
|
2016-06-28 23:19:20 +02:00
|
|
|
else
|
|
|
|
io_wake(peer);
|
2016-06-30 01:40:11 +02:00
|
|
|
|
|
|
|
return DELETE_WATCH;
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
static bool find_their_old_tx(struct peer *peer,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
u64 *idx)
|
|
|
|
{
|
|
|
|
/* FIXME: Don't keep these in memory, search db here. */
|
|
|
|
struct their_commit *tc;
|
|
|
|
|
|
|
|
log_debug_struct(peer->log, "Finding txid %s", struct sha256_double,
|
|
|
|
txid);
|
|
|
|
list_for_each(&peer->their_commits, tc, list) {
|
|
|
|
if (structeq(&tc->txid, txid)) {
|
|
|
|
*idx = tc->commit_num;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void resolve_their_steal(struct peer *peer,
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct sha256 *revocation_preimage)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
int i, n;
|
2016-08-18 06:53:46 +02:00
|
|
|
const struct bitcoin_tx *tx = peer->onchain.tx;
|
2016-08-18 06:53:45 +02:00
|
|
|
struct bitcoin_tx *steal_tx;
|
|
|
|
size_t wsize = 0;
|
|
|
|
u64 input_total = 0, fee;
|
|
|
|
|
|
|
|
/* Create steal_tx: don't need to steal to_us output */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (peer->onchain.to_us_idx == -1)
|
2016-08-18 06:53:45 +02:00
|
|
|
steal_tx = bitcoin_tx(tx, tx->output_count, 1);
|
|
|
|
else
|
|
|
|
steal_tx = bitcoin_tx(tx, tx->output_count - 1, 1);
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
log_debug(peer->log, "Analyzing tx to steal:");
|
|
|
|
for (i = 0; i < tx->output_count; i++) {
|
|
|
|
/* BOLT #onchain:
|
|
|
|
* 1. _A's main output_: No action is required; this is a
|
|
|
|
* simple P2WPKH output. This output is considered
|
|
|
|
* *resolved* by the *commitment tx*.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (i == peer->onchain.to_us_idx) {
|
2016-08-18 06:53:45 +02:00
|
|
|
log_debug(peer->log, "%i is to-us, ignoring", i);
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[i] = tx;
|
2016-08-18 06:53:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 2. _B's main output_: The node MUST *resolve* this by
|
|
|
|
* spending using the revocation preimage.
|
|
|
|
*
|
|
|
|
* 3. _A's offered HTLCs_: The node MUST *resolve* this by
|
|
|
|
* spending using the revocation preimage.
|
|
|
|
*
|
|
|
|
* 4. _B's offered HTLCs_: The node MUST *resolve* this by
|
|
|
|
* spending using the revocation preimage.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved[i] = steal_tx;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
/* Connect it up. */
|
2016-08-18 06:53:46 +02:00
|
|
|
steal_tx->input[n].txid = peer->onchain.txid;
|
2016-08-18 06:53:45 +02:00
|
|
|
steal_tx->input[n].index = i;
|
|
|
|
steal_tx->input[n].amount = tal_dup(steal_tx, u64,
|
|
|
|
&tx->output[i].amount);
|
|
|
|
/* Track witness size, for fee. */
|
2016-08-18 06:53:46 +02:00
|
|
|
wsize += tal_count(peer->onchain.wscripts[n]);
|
2016-08-18 06:53:45 +02:00
|
|
|
input_total += tx->output[i].amount;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
assert(n == steal_tx->input_count);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
fee = get_feerate(peer->dstate)
|
2016-08-18 06:53:45 +02:00
|
|
|
* (measure_tx_cost(steal_tx) + wsize) / 1000;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (fee > input_total || is_dust(input_total - fee)) {
|
2016-08-18 06:53:45 +02:00
|
|
|
log_unusual(peer->log, "Not worth stealing tiny amount %"PRIu64,
|
|
|
|
input_total);
|
|
|
|
/* Consider them all resolved by steal tx. */
|
2016-08-18 06:53:46 +02:00
|
|
|
for (i = 0; i < tal_count(peer->onchain.resolved); i++)
|
|
|
|
peer->onchain.resolved[i] = tx;
|
2016-08-18 06:53:45 +02:00
|
|
|
tal_free(steal_tx);
|
2016-08-18 06:53:46 +02:00
|
|
|
return;
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
steal_tx->output[0].amount = input_total - fee;
|
|
|
|
steal_tx->output[0].script = scriptpubkey_p2sh(steal_tx,
|
|
|
|
bitcoin_redeem_single(steal_tx,
|
|
|
|
peer->dstate->secpctx,
|
|
|
|
&peer->local.finalkey));
|
|
|
|
steal_tx->output[0].script_length = tal_count(steal_tx->output[0].script);
|
|
|
|
|
|
|
|
/* Now, we can sign them all (they're all of same form). */
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-08-18 06:53:46 +02:00
|
|
|
peer_sign_steal_input(peer, steal_tx, i,
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.wscripts[i],
|
2016-08-18 06:53:46 +02:00
|
|
|
&sig.sig);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
steal_tx->input[i].witness
|
|
|
|
= bitcoin_witness_secret(steal_tx,
|
|
|
|
peer->dstate->secpctx,
|
2016-08-18 06:53:46 +02:00
|
|
|
revocation_preimage,
|
|
|
|
sizeof(*revocation_preimage),
|
2016-08-18 06:53:45 +02:00
|
|
|
&sig,
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.wscripts[i]);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
broadcast_tx(peer, steal_tx);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sha256 *get_rhash(struct peer *peer, u64 commit_num,
|
|
|
|
struct sha256 *rhash)
|
|
|
|
{
|
|
|
|
struct sha256 preimage;
|
|
|
|
|
|
|
|
/* Previous revoked tx? */
|
|
|
|
if (shachain_get_hash(&peer->their_preimages,
|
|
|
|
0xFFFFFFFFFFFFFFFFL - commit_num,
|
|
|
|
&preimage)) {
|
|
|
|
sha256(rhash, &preimage, sizeof(preimage));
|
|
|
|
return tal_dup(peer, struct sha256, &preimage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Current tx? */
|
|
|
|
if (commit_num == peer->remote.commit->commit_num) {
|
|
|
|
*rhash = peer->remote.commit->revocation_hash;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last tx, but we haven't got revoke for it yet? */
|
|
|
|
assert(commit_num == peer->remote.commit->commit_num-1);
|
|
|
|
*rhash = *peer->their_prev_revocation_hash;
|
|
|
|
return NULL;
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:05 +01:00
|
|
|
/* We assume the tx is valid! Don't do a blockchain.info and feed this
|
|
|
|
* invalid transactions! */
|
2016-06-30 01:40:11 +02:00
|
|
|
static enum watch_result anchor_spent(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
size_t input_num,
|
|
|
|
void *unused)
|
2016-01-21 21:15:05 +01:00
|
|
|
{
|
2016-05-26 07:55:25 +02:00
|
|
|
Pkt *err;
|
|
|
|
enum state newstate;
|
2016-08-18 06:53:45 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
2016-08-18 06:53:46 +02:00
|
|
|
u64 commit_num;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-05-03 03:30:20 +02:00
|
|
|
assert(input_num < tx->input_count);
|
|
|
|
|
|
|
|
/* We only ever sign single-input txs. */
|
2016-08-18 06:53:45 +02:00
|
|
|
if (input_num != 0) {
|
|
|
|
log_broken(peer->log, "Anchor spend by non-single input tx");
|
|
|
|
goto unknown_spend;
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* We may have been following a different spend. Forget it. */
|
|
|
|
reset_onchain_closing(peer);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.tx = tal_steal(peer, tx);
|
|
|
|
bitcoin_txid(tx, &peer->onchain.txid);
|
2016-05-03 03:36:20 +02:00
|
|
|
|
2016-07-01 03:49:06 +02:00
|
|
|
/* If we have any HTLCs we're not committed to yet, fail them now. */
|
2016-08-18 06:53:45 +02:00
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h;
|
|
|
|
h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (h->state == SENT_ADD_HTLC) {
|
2016-08-31 07:21:41 +02:00
|
|
|
fail_own_htlc(peer, h);
|
2016-08-18 06:53:45 +02:00
|
|
|
}
|
2016-07-01 03:49:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* We need to resolve every output. */
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->onchain.resolved
|
2016-08-18 06:53:46 +02:00
|
|
|
= tal_arrz(tx, const struct bitcoin_tx *, tx->output_count);
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* A mutual close tx. */
|
|
|
|
if (is_mutual_close(peer, tx)) {
|
|
|
|
newstate = STATE_CLOSE_ONCHAIN_MUTUAL;
|
|
|
|
err = NULL;
|
|
|
|
resolve_mutual_close(peer);
|
|
|
|
/* Our unilateral */
|
2016-08-18 06:53:46 +02:00
|
|
|
} else if (structeq(&peer->local.commit->txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
&peer->onchain.txid)) {
|
2016-05-26 07:55:25 +02:00
|
|
|
newstate = STATE_CLOSE_ONCHAIN_OUR_UNILATERAL;
|
|
|
|
/* We're almost certainly closed to them by now. */
|
|
|
|
err = pkt_err(peer, "Our own unilateral close tx seen");
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!map_onchain_outputs(peer,
|
|
|
|
&peer->local.commit->revocation_hash,
|
|
|
|
tx, LOCAL,
|
|
|
|
peer->local.commit->commit_num)) {
|
|
|
|
log_broken(peer->log,
|
|
|
|
"Can't resolve own anchor spend %"PRIu64"!",
|
|
|
|
commit_num);
|
|
|
|
goto unknown_spend;
|
|
|
|
}
|
|
|
|
resolve_our_unilateral(peer);
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Must be their unilateral */
|
2016-08-18 06:53:46 +02:00
|
|
|
} else if (find_their_old_tx(peer, &peer->onchain.txid,
|
2016-08-18 06:53:46 +02:00
|
|
|
&commit_num)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
struct sha256 *preimage, rhash;
|
|
|
|
|
|
|
|
preimage = get_rhash(peer, commit_num, &rhash);
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!map_onchain_outputs(peer, &rhash, tx, REMOTE, commit_num)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Should not happen */
|
|
|
|
log_broken(peer->log,
|
|
|
|
"Can't resolve known anchor spend %"PRIu64"!",
|
|
|
|
commit_num);
|
|
|
|
goto unknown_spend;
|
|
|
|
}
|
|
|
|
if (preimage) {
|
|
|
|
newstate = STATE_CLOSE_ONCHAIN_CHEATED;
|
|
|
|
err = pkt_err(peer, "Revoked transaction seen");
|
2016-08-18 06:53:46 +02:00
|
|
|
resolve_their_steal(peer, preimage);
|
2016-08-18 06:53:46 +02:00
|
|
|
} else {
|
|
|
|
newstate = STATE_CLOSE_ONCHAIN_THEIR_UNILATERAL;
|
|
|
|
err = pkt_err(peer, "Unilateral close tx seen");
|
2016-08-18 06:53:46 +02:00
|
|
|
resolve_their_unilateral(peer);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
2016-05-06 04:26:32 +02:00
|
|
|
} else {
|
2016-05-04 08:44:22 +02:00
|
|
|
/* FIXME: Log harder! */
|
2016-08-18 06:53:46 +02:00
|
|
|
log_broken(peer->log,
|
|
|
|
"Unknown anchor spend! Funds may be lost!");
|
2016-08-18 06:53:45 +02:00
|
|
|
goto unknown_spend;
|
2016-05-06 04:26:32 +02:00
|
|
|
}
|
2016-05-26 07:55:25 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node MAY send a descriptive error packet in this case.
|
|
|
|
*/
|
|
|
|
if (err && state_can_io(peer->state))
|
|
|
|
queue_pkt_err(peer, err);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* Don't need to save to DB: it will be replayed if we crash. */
|
|
|
|
set_peer_state(peer, newstate, "anchor_spent", false);
|
2016-05-26 07:55:25 +02:00
|
|
|
|
|
|
|
/* If we've just closed connection, make output close it. */
|
|
|
|
io_wake(peer);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node SHOULD fail the connection if it is not already
|
|
|
|
* closed when it sees the funding transaction spent.
|
|
|
|
*/
|
|
|
|
assert(!state_can_io(peer->state));
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(peer->onchain.resolved != NULL);
|
2016-05-04 08:44:22 +02:00
|
|
|
watch_tx(tx, peer, tx, check_for_resolution, NULL);
|
|
|
|
|
2016-06-30 01:40:11 +02:00
|
|
|
return KEEP_WATCHING;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
|
|
|
unknown_spend:
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node SHOULD report an error to the operator if it
|
|
|
|
* sees a transaction spend the funding transaction
|
|
|
|
* output which does not fall into one of these
|
|
|
|
* categories (mutual close, unilateral close, or
|
|
|
|
* cheating attempt). Such a transaction implies its
|
|
|
|
* private key has leaked, and funds may be lost.
|
|
|
|
*/
|
2016-08-18 06:55:14 +02:00
|
|
|
/* FIXME: Save to db. */
|
|
|
|
set_peer_state(peer, STATE_ERR_INFORMATION_LEAK, "anchor_spent", false);
|
2016-08-18 06:53:45 +02:00
|
|
|
return DELETE_WATCH;
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void anchor_timeout(struct peer *peer)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
/* FIXME: We could just forget timeout once we're not opening. */
|
|
|
|
if (state_is_opening(peer->state))
|
|
|
|
state_event(peer, BITCOIN_ANCHOR_TIMEOUT, NULL);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_watch_anchor(struct peer *peer,
|
2016-08-18 06:53:45 +02:00
|
|
|
int depth,
|
2016-01-21 21:14:13 +01:00
|
|
|
enum state_input depthok,
|
2016-05-04 08:44:22 +02:00
|
|
|
enum state_input timeout)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
/* We assume this. */
|
|
|
|
assert(depthok == BITCOIN_ANCHOR_DEPTHOK);
|
|
|
|
assert(timeout == BITCOIN_ANCHOR_TIMEOUT || timeout == INPUT_NONE);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
peer->anchor.ok_depth = depth;
|
|
|
|
watch_txid(peer, peer, &peer->anchor.txid, anchor_depthchange, NULL);
|
|
|
|
watch_txo(peer, peer, &peer->anchor.txid, 0, anchor_spent, NULL);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* For anchor timeout, expect 20 minutes per block, +2 hours.
|
|
|
|
*
|
|
|
|
* Probability(no block in time N) = e^(-N/600).
|
|
|
|
* Thus for 1 block, P = e^(-(7200+1*1200)/600) = 0.83 in a million.
|
|
|
|
*
|
|
|
|
* Glenn Willen says, if we want to know how many 10-minute intervals for
|
|
|
|
* a 1 in a million chance of spurious failure for N blocks, put
|
|
|
|
* this into http://www.wolframalpha.com:
|
|
|
|
*
|
|
|
|
* e^(-x) * sum x^i / fact(i), i=0 to N < 1/1000000
|
|
|
|
*
|
|
|
|
* N=20: 51
|
|
|
|
* N=10: 35
|
|
|
|
* N=8: 31
|
|
|
|
* N=6: 28
|
|
|
|
* N=4: 24
|
|
|
|
* N=3: 22
|
|
|
|
* N=2: 20
|
|
|
|
*
|
|
|
|
* So, our formula of 12 + N*2 holds for N <= 20 at least.
|
|
|
|
*/
|
2016-08-18 06:53:45 +02:00
|
|
|
if (timeout != INPUT_NONE)
|
|
|
|
new_reltimer(peer->dstate, peer,
|
|
|
|
time_from_sec(7200 + 20*peer->anchor.ok_depth),
|
|
|
|
anchor_timeout, peer);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
|
|
|
struct channel_state cstate;
|
|
|
|
|
|
|
|
/* We don't need a deep copy here, just fee levels. */
|
2016-05-26 07:55:24 +02:00
|
|
|
cstate = *peer->local.staging_cstate;
|
2016-03-24 02:42:43 +01:00
|
|
|
if (!force_fee(&cstate, fee)) {
|
|
|
|
log_unusual(peer->log,
|
2016-08-18 06:53:45 +02:00
|
|
|
"peer_create_close_tx: can't afford fee %"PRIu64,
|
|
|
|
fee);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
log_debug(peer->log,
|
|
|
|
"creating close-tx with fee %"PRIu64" amounts %u/%u to ",
|
|
|
|
fee,
|
|
|
|
cstate.side[OURS].pay_msat / 1000,
|
|
|
|
cstate.side[THEIRS].pay_msat / 1000);
|
|
|
|
log_add_struct(peer->log, "%s", struct pubkey, &peer->local.finalkey);
|
|
|
|
log_add_struct(peer->log, "/%s", struct pubkey, &peer->remote.finalkey);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
return create_close_tx(peer->dstate->secpctx, peer,
|
|
|
|
peer->closing.our_script,
|
|
|
|
peer->closing.their_script,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
cstate.side[OURS].pay_msat / 1000,
|
|
|
|
cstate.side[THEIRS].pay_msat / 1000);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Creation the bitcoin anchor tx, spending output user provided. */
|
|
|
|
void bitcoin_create_anchor(struct peer *peer)
|
2016-01-21 21:14:27 +01:00
|
|
|
{
|
2016-04-12 05:37:04 +02:00
|
|
|
u64 fee;
|
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
|
2016-04-12 06:47:47 +02:00
|
|
|
size_t i;
|
2016-04-11 09:10:53 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* We must be offering anchor for us to try creating it */
|
|
|
|
assert(peer->local.offer_anchor);
|
|
|
|
|
2016-04-24 12:25:35 +02:00
|
|
|
tx->output[0].script = scriptpubkey_p2wsh(tx, peer->anchor.witnessscript);
|
2016-04-12 05:37:04 +02:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
|
|
|
/* Add input script length. FIXME: This is normal case, not exact. */
|
2016-08-18 06:53:46 +02:00
|
|
|
fee = fee_by_feerate(measure_tx_cost(tx)/4 + 1+73 + 1+33 + 1,
|
|
|
|
get_feerate(peer->dstate));
|
2016-04-12 05:37:04 +02:00
|
|
|
if (fee >= peer->anchor.input->amount)
|
|
|
|
/* FIXME: Report an error here!
|
|
|
|
* We really should set this when they do command, but
|
|
|
|
* we need to modify state to allow immediate anchor
|
|
|
|
* creation: using estimate_fee is a convenient workaround. */
|
|
|
|
fatal("Amount %"PRIu64" below fee %"PRIu64,
|
|
|
|
peer->anchor.input->amount, fee);
|
|
|
|
|
|
|
|
tx->output[0].amount = peer->anchor.input->amount - fee;
|
|
|
|
|
|
|
|
tx->input[0].txid = peer->anchor.input->txid;
|
|
|
|
tx->input[0].index = peer->anchor.input->index;
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64,
|
|
|
|
&peer->anchor.input->amount);
|
2016-04-11 09:10:53 +02:00
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
wallet_add_signed_input(peer->dstate, peer->anchor.input->w, tx, 0);
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &peer->anchor.txid);
|
|
|
|
peer->anchor.tx = tx;
|
|
|
|
peer->anchor.index = 0;
|
2016-01-21 21:14:27 +01:00
|
|
|
/* We'll need this later, when we're told to broadcast it. */
|
2016-04-12 05:37:04 +02:00
|
|
|
peer->anchor.satoshis = tx->output[0].amount;
|
2016-01-21 21:14:27 +01:00
|
|
|
|
2016-04-12 06:47:47 +02:00
|
|
|
/* To avoid malleation, all inputs must be segwit! */
|
|
|
|
for (i = 0; i < tx->input_count; i++)
|
|
|
|
assert(tx->input[i].witness);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* We didn't end up broadcasting the anchor: we don't need to do anything
|
|
|
|
* to "release" TXOs, since we have our own internal wallet now. */
|
2016-01-21 21:14:13 +01:00
|
|
|
void bitcoin_release_anchor(struct peer *peer, enum state_input done)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the bitcoin anchor tx. */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_anchor(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:14:27 +01:00
|
|
|
return peer->anchor.tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Sets up the initial cstate and commit tx for both nodes: false if
|
|
|
|
* insufficient funds. */
|
|
|
|
bool setup_first_commit(struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
bool to_them_only, to_us_only;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(!peer->local.commit->tx);
|
|
|
|
assert(!peer->remote.commit->tx);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* Revocation hashes already filled in, from pkt_open */
|
2016-06-30 01:38:10 +02:00
|
|
|
peer->local.commit->cstate = initial_cstate(peer,
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->anchor.satoshis,
|
|
|
|
peer->local.commit_fee_rate,
|
|
|
|
peer->local.offer_anchor
|
|
|
|
== CMD_OPEN_WITH_ANCHOR ?
|
|
|
|
OURS : THEIRS);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->local.commit->cstate)
|
2016-03-31 08:43:20 +02:00
|
|
|
return false;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
peer->remote.commit->cstate = initial_cstate(peer,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
peer->remote.commit_fee_rate,
|
|
|
|
peer->local.offer_anchor
|
|
|
|
== CMD_OPEN_WITH_ANCHOR ?
|
|
|
|
OURS : THEIRS);
|
|
|
|
if (!peer->remote.commit->cstate)
|
|
|
|
return false;
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.commit->tx = create_commit_tx(peer->local.commit,
|
2016-08-18 06:53:45 +02:00
|
|
|
peer,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.commit->revocation_hash,
|
|
|
|
peer->local.commit->cstate,
|
2016-08-18 06:55:13 +02:00
|
|
|
LOCAL, &to_them_only);
|
2016-08-18 06:53:45 +02:00
|
|
|
bitcoin_txid(peer->local.commit->tx, &peer->local.commit->txid);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
peer->remote.commit->tx = create_commit_tx(peer->remote.commit,
|
2016-08-18 06:53:45 +02:00
|
|
|
peer,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.commit->revocation_hash,
|
|
|
|
peer->remote.commit->cstate,
|
2016-08-18 06:55:13 +02:00
|
|
|
REMOTE, &to_us_only);
|
|
|
|
assert(to_them_only != to_us_only);
|
|
|
|
|
|
|
|
/* If we offer anchor, their commit is to-us only. */
|
|
|
|
assert(to_us_only == (peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR));
|
2016-08-18 06:53:45 +02:00
|
|
|
bitcoin_txid(peer->remote.commit->tx, &peer->remote.commit->txid);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate);
|
|
|
|
peer->remote.staging_cstate = copy_cstate(peer, peer->remote.commit->cstate);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
return true;
|
2016-01-21 21:14:27 +01:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static struct io_plan *peer_reconnect(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
/* In case they reconnected to us already. */
|
|
|
|
if (peer->conn)
|
|
|
|
return io_close(conn);
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "Reconnected, doing crypto...");
|
2016-08-18 06:55:13 +02:00
|
|
|
peer->conn = conn;
|
|
|
|
assert(!peer->connected);
|
2016-08-18 06:55:08 +02:00
|
|
|
|
|
|
|
assert(peer->id);
|
|
|
|
return peer_crypto_setup(conn, peer->dstate,
|
2016-08-31 08:36:31 +02:00
|
|
|
peer->id, peer->log,
|
|
|
|
crypto_on_reconnect_out, peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* We can't only retry when we want to send: they may want to send us
|
|
|
|
* something but not be able to connect (NAT). So keep retrying.. */
|
2016-08-18 06:55:08 +02:00
|
|
|
static void reconnect_failed(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
/* Already otherwise connected (ie. they connected in)? */
|
|
|
|
if (peer->conn) {
|
|
|
|
log_debug(peer->log, "reconnect_failed: already connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(peer->log, "Setting timer to re-connect");
|
|
|
|
new_reltimer(peer->dstate, peer, time_from_sec(15), try_reconnect, peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *init_conn(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
struct addrinfo a;
|
2016-08-18 06:55:14 +02:00
|
|
|
struct peer_address *addr = find_address(peer->dstate, peer->id);
|
2016-08-18 06:55:08 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
netaddr_to_addrinfo(&a, &addr->addr);
|
2016-08-18 06:55:08 +02:00
|
|
|
return io_connect(conn, &a, peer_reconnect, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_reconnect(struct peer *peer)
|
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
struct io_conn *conn;
|
2016-08-18 06:55:14 +02:00
|
|
|
struct peer_address *addr;
|
|
|
|
char *name;
|
2016-08-18 06:55:14 +02:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Already reconnected? */
|
|
|
|
if (peer->conn) {
|
|
|
|
log_debug(peer->log, "try_reconnect: already connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
addr = find_address(peer->dstate, peer->id);
|
|
|
|
if (!addr) {
|
|
|
|
log_debug(peer->log, "try_reconnect: no known address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = socket(addr->addr.saddr.s.sa_family, addr->addr.type,
|
|
|
|
addr->addr.protocol);
|
2016-08-18 06:55:08 +02:00
|
|
|
if (fd < 0) {
|
2016-08-18 06:55:13 +02:00
|
|
|
log_broken(peer->log, "do_reconnect: failed to create socket: %s",
|
2016-08-18 06:55:08 +02:00
|
|
|
strerror(errno));
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, "do_reconnect", false);
|
2016-08-18 06:55:13 +02:00
|
|
|
peer_breakdown(peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!peer->conn);
|
2016-08-18 06:55:13 +02:00
|
|
|
conn = io_new_conn(peer->dstate, fd, init_conn, peer);
|
2016-08-18 06:55:14 +02:00
|
|
|
name = netaddr_name(peer, &addr->addr);
|
|
|
|
log_debug(peer->log, "Trying to reconnect to %s", name);
|
|
|
|
tal_free(name);
|
2016-08-18 06:55:13 +02:00
|
|
|
io_set_finish(conn, reconnect_failed, peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void reconnect_peers(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
list_for_each(&dstate->peers, peer, list)
|
|
|
|
try_reconnect(peer);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void json_add_abstime(struct json_result *response,
|
|
|
|
const char *id,
|
|
|
|
const struct abs_locktime *t)
|
|
|
|
{
|
|
|
|
json_object_start(response, id);
|
|
|
|
if (abs_locktime_is_seconds(t))
|
|
|
|
json_add_num(response, "second", abs_locktime_to_seconds(t));
|
|
|
|
else
|
|
|
|
json_add_num(response, "block", abs_locktime_to_blocks(t));
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
static void json_add_htlcs(struct json_result *response,
|
|
|
|
const char *id,
|
2016-08-18 06:53:45 +02:00
|
|
|
struct peer *peer,
|
|
|
|
enum htlc_side owner)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
struct htlc *h;
|
|
|
|
const struct htlc_map *htlcs = &peer->htlcs;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
json_array_start(response, id);
|
2016-08-18 06:53:45 +02:00
|
|
|
for (h = htlc_map_first(htlcs, &it); h; h = htlc_map_next(htlcs, &it)) {
|
|
|
|
if (htlc_owner(h) != owner)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Ignore completed HTLCs. */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (htlc_is_dead(h))
|
2016-08-18 06:53:45 +02:00
|
|
|
continue;
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
json_object_start(response, NULL);
|
2016-08-18 06:53:45 +02:00
|
|
|
json_add_u64(response, "msatoshis", h->msatoshis);
|
|
|
|
json_add_abstime(response, "expiry", &h->expiry);
|
|
|
|
json_add_hex(response, "rhash", &h->rhash, sizeof(h->rhash));
|
|
|
|
json_add_string(response, "state", htlc_state_name(h->state));
|
2016-01-21 21:15:28 +01:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: add history command which shows all prior and current commit txs */
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* FIXME: Somehow we should show running DNS lookups! */
|
|
|
|
static void json_getpeers(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_array_start(response, "peers");
|
2016-01-21 21:11:49 +01:00
|
|
|
list_for_each(&cmd->dstate->peers, p, list) {
|
2016-03-31 08:43:20 +02:00
|
|
|
const struct channel_state *last;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "name", log_prefix(p->log));
|
2016-01-21 21:14:25 +01:00
|
|
|
json_add_string(response, "state", state_name(p->state));
|
|
|
|
|
2016-07-01 03:57:57 +02:00
|
|
|
if (p->id)
|
2016-07-01 03:49:28 +02:00
|
|
|
json_add_pubkey(response, cmd->dstate->secpctx,
|
2016-07-01 03:57:57 +02:00
|
|
|
"peerid", p->id);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-30 12:41:57 +02:00
|
|
|
json_add_bool(response, "connected", p->connected);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Report anchor. */
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!p->local.commit || !p->local.commit->cstate) {
|
2016-01-21 21:15:28 +01:00
|
|
|
json_object_end(response);
|
2016-03-31 08:43:20 +02:00
|
|
|
continue;
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
last = p->local.commit->cstate;
|
2016-05-02 08:31:56 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
json_add_num(response, "our_amount", last->side[OURS].pay_msat);
|
|
|
|
json_add_num(response, "our_fee", last->side[OURS].fee_msat);
|
|
|
|
json_add_num(response, "their_amount", last->side[THEIRS].pay_msat);
|
|
|
|
json_add_num(response, "their_fee", last->side[THEIRS].fee_msat);
|
2016-08-18 06:53:45 +02:00
|
|
|
json_add_htlcs(response, "our_htlcs", p, LOCAL);
|
|
|
|
json_add_htlcs(response, "their_htlcs", p, REMOTE);
|
2016-01-21 21:11:48 +01:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command getpeers_command = {
|
|
|
|
"getpeers",
|
|
|
|
json_getpeers,
|
|
|
|
"List the current peers",
|
|
|
|
"Returns a 'peers' array"
|
|
|
|
};
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-08-30 12:39:57 +02:00
|
|
|
static void json_gethtlcs(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok, *resolvedtok;
|
|
|
|
bool resolved = false;
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
struct htlc *h;
|
|
|
|
struct htlc_map_iter it;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
"?resolved", &resolvedtok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolvedtok && !json_tok_bool(buffer, resolvedtok, &resolved)) {
|
|
|
|
command_fail(cmd, "resolved must be true or false");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_array_start(response, "htlcs");
|
|
|
|
for (h = htlc_map_first(&peer->htlcs, &it);
|
|
|
|
h; h = htlc_map_next(&peer->htlcs, &it)) {
|
|
|
|
if (htlc_is_dead(h) && !resolved)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_u64(response, "id", h->id);
|
|
|
|
json_add_string(response, "state", htlc_state_name(h->state));
|
|
|
|
json_add_u64(response, "msatoshis", h->msatoshis);
|
|
|
|
json_add_abstime(response, "expiry", &h->expiry);
|
|
|
|
json_add_hex(response, "rhash", &h->rhash, sizeof(h->rhash));
|
|
|
|
if (h->r)
|
|
|
|
json_add_hex(response, "r", h->r, sizeof(*h->r));
|
|
|
|
if (htlc_owner(h) == LOCAL) {
|
|
|
|
json_add_num(response, "deadline", h->deadline);
|
|
|
|
if (h->src) {
|
|
|
|
json_object_start(response, "src");
|
|
|
|
json_add_pubkey(response, cmd->dstate->secpctx,
|
|
|
|
"peerid", h->src->peer->id);
|
|
|
|
json_add_u64(response, "id", h->src->id);
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (h->routing)
|
|
|
|
json_add_hex(response, "routing",
|
|
|
|
h->routing, tal_count(h->routing));
|
|
|
|
}
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command gethtlcs_command = {
|
|
|
|
"gethtlcs",
|
|
|
|
json_gethtlcs,
|
|
|
|
"List HTLCs for {peer}; all if {resolved} is true.",
|
|
|
|
"Returns a 'htlcs' array"
|
|
|
|
};
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
/* To avoid freeing underneath ourselves, we free outside event loop. */
|
|
|
|
void cleanup_peers(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
struct peer *peer, *next;
|
|
|
|
|
|
|
|
list_for_each_safe(&dstate->peers, peer, next, list) {
|
|
|
|
/* Deletes itself from list. */
|
|
|
|
if (!peer->conn && peer->state == STATE_CLOSED)
|
|
|
|
tal_free(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
static void json_newhtlc(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
2016-03-08 01:00:15 +01:00
|
|
|
jsmntok_t *peeridtok, *msatoshistok, *expirytok, *rhashtok;
|
2016-01-21 21:15:27 +01:00
|
|
|
unsigned int expiry;
|
2016-06-28 23:19:21 +02:00
|
|
|
u64 msatoshis;
|
|
|
|
struct sha256 rhash;
|
2016-08-18 06:53:46 +02:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
struct htlc *htlc;
|
2016-08-31 07:21:41 +02:00
|
|
|
const char *err;
|
|
|
|
enum fail_error error_code;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
"msatoshis", &msatoshistok,
|
|
|
|
"expiry", &expirytok,
|
|
|
|
"rhash", &rhashtok,
|
|
|
|
NULL)) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Need peerid, msatoshis, expiry and rhash");
|
2016-01-21 21:15:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-01-21 21:15:27 +01:00
|
|
|
if (!peer) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
2016-01-21 21:15:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->remote.commit || !peer->remote.commit->cstate) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(cmd, "peer not fully established");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:41:57 +02:00
|
|
|
if (!peer->connected) {
|
|
|
|
command_fail(cmd, "peer not connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
if (!json_tok_u64(buffer, msatoshistok, &msatoshis)) {
|
2016-01-21 21:15:27 +01:00
|
|
|
command_fail(cmd, "'%.*s' is not a valid number",
|
|
|
|
(int)(msatoshistok->end - msatoshistok->start),
|
|
|
|
buffer + msatoshistok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_tok_number(buffer, expirytok, &expiry)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid number",
|
|
|
|
(int)(expirytok->end - expirytok->start),
|
|
|
|
buffer + expirytok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hex_decode(buffer + rhashtok->start,
|
|
|
|
rhashtok->end - rhashtok->start,
|
2016-06-28 23:19:21 +02:00
|
|
|
&rhash, sizeof(rhash))) {
|
2016-01-21 21:15:27 +01:00
|
|
|
command_fail(cmd, "'%.*s' is not a valid sha256 hash",
|
|
|
|
(int)(rhashtok->end - rhashtok->start),
|
|
|
|
buffer + rhashtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(peer->log, "JSON command to add new HTLC");
|
2016-08-31 07:21:41 +02:00
|
|
|
err = command_htlc_add(peer, msatoshis, expiry, &rhash, NULL,
|
2016-08-31 08:36:08 +02:00
|
|
|
onion_create(cmd, cmd->dstate->secpctx,
|
|
|
|
NULL, NULL, 0),
|
2016-08-31 07:21:41 +02:00
|
|
|
&error_code, &htlc);
|
|
|
|
if (err) {
|
|
|
|
command_fail(cmd, "could not add htlc: %u:%s", error_code, err);
|
2016-06-28 23:19:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(peer->log, "JSON new HTLC is %"PRIu64, htlc->id);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_u64(response, "id", htlc->id);
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command newhtlc_command = {
|
|
|
|
"newhtlc",
|
|
|
|
json_newhtlc,
|
2016-06-28 23:19:21 +02:00
|
|
|
"Offer {peerid} an HTLC worth {msatoshis} in {expiry} (block number) with {rhash}",
|
2016-08-18 06:53:46 +02:00
|
|
|
"Returns { id: u64 } result on success"
|
2016-01-21 21:15:27 +01:00
|
|
|
};
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
static void json_fulfillhtlc(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
2016-08-18 06:53:46 +02:00
|
|
|
jsmntok_t *peeridtok, *idtok, *rtok;
|
|
|
|
u64 id;
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *htlc;
|
2016-08-18 06:53:46 +02:00
|
|
|
struct sha256 rhash;
|
|
|
|
struct rval r;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
2016-08-18 06:53:46 +02:00
|
|
|
"id", &idtok,
|
2016-03-15 07:37:30 +01:00
|
|
|
"r", &rtok,
|
|
|
|
NULL)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
command_fail(cmd, "Need peerid, id and r");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-01-21 21:15:28 +01:00
|
|
|
if (!peer) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->remote.commit || !peer->remote.commit->cstate) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(cmd, "peer not fully established");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:41:57 +02:00
|
|
|
if (!peer->connected) {
|
|
|
|
command_fail(cmd, "peer not connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!json_tok_u64(buffer, idtok, &id)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid id",
|
|
|
|
(int)(idtok->end - idtok->start),
|
|
|
|
buffer + idtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
if (!hex_decode(buffer + rtok->start,
|
|
|
|
rtok->end - rtok->start,
|
2016-05-26 07:55:25 +02:00
|
|
|
&r, sizeof(r))) {
|
2016-01-21 21:15:28 +01:00
|
|
|
command_fail(cmd, "'%.*s' is not a valid sha256 preimage",
|
|
|
|
(int)(rtok->end - rtok->start),
|
|
|
|
buffer + rtok->start);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-26 07:55:25 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
htlc = htlc_get(&peer->htlcs, id, REMOTE);
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!htlc) {
|
2016-05-26 07:55:25 +02:00
|
|
|
command_fail(cmd, "preimage htlc not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (htlc->state != RCVD_ADD_ACK_REVOCATION) {
|
|
|
|
command_fail(cmd, "htlc in state %s",
|
|
|
|
htlc_state_name(htlc->state));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sha256(&rhash, &r, sizeof(r));
|
|
|
|
if (!structeq(&htlc->rhash, &rhash)) {
|
|
|
|
command_fail(cmd, "preimage incorrect");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* This can happen if we're disconnected, and thus haven't sent
|
|
|
|
* fulfill yet; we stored r in database immediately. */
|
|
|
|
if (!htlc->r)
|
|
|
|
set_htlc_rval(peer, htlc, &r);
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
if (command_htlc_fulfill(peer, htlc))
|
2016-05-26 07:55:25 +02:00
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
else
|
|
|
|
command_fail(cmd,
|
|
|
|
"htlc_fulfill not possible in state %s",
|
|
|
|
state_name(peer->state));
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
2016-06-30 01:38:11 +02:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
const struct json_command fulfillhtlc_command = {
|
|
|
|
"fulfillhtlc",
|
|
|
|
json_fulfillhtlc,
|
2016-08-18 06:53:46 +02:00
|
|
|
"Redeem htlc proposed by {peerid} of {id} using {r}",
|
2016-01-21 21:15:28 +01:00
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void json_failhtlc(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
2016-08-31 07:21:41 +02:00
|
|
|
jsmntok_t *peeridtok, *idtok, *reasontok;
|
2016-08-18 06:53:46 +02:00
|
|
|
u64 id;
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *htlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
2016-08-18 06:53:46 +02:00
|
|
|
"id", &idtok,
|
2016-08-31 07:21:41 +02:00
|
|
|
"reason", &reasontok,
|
2016-03-15 07:37:30 +01:00
|
|
|
NULL)) {
|
2016-08-31 07:21:41 +02:00
|
|
|
command_fail(cmd, "Need peerid, id and reason");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-01-21 21:15:28 +01:00
|
|
|
if (!peer) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->remote.commit || !peer->remote.commit->cstate) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(cmd, "peer not fully established");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:41:57 +02:00
|
|
|
if (!peer->connected) {
|
|
|
|
command_fail(cmd, "peer not connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!json_tok_u64(buffer, idtok, &id)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid id",
|
|
|
|
(int)(idtok->end - idtok->start),
|
|
|
|
buffer + idtok->start);
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
htlc = htlc_get(&peer->htlcs, id, REMOTE);
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!htlc) {
|
2016-08-18 06:53:46 +02:00
|
|
|
command_fail(cmd, "preimage htlc not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (htlc->state != RCVD_ADD_ACK_REVOCATION) {
|
|
|
|
command_fail(cmd, "htlc in state %s",
|
|
|
|
htlc_state_name(htlc->state));
|
2016-05-26 07:55:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
set_htlc_fail(peer, htlc, buffer + reasontok->start,
|
|
|
|
reasontok->end - reasontok->start);
|
2016-06-30 01:38:11 +02:00
|
|
|
if (command_htlc_fail(peer, htlc))
|
2016-05-26 07:55:25 +02:00
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
else
|
|
|
|
command_fail(cmd,
|
|
|
|
"htlc_fail not possible in state %s",
|
|
|
|
state_name(peer->state));
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
2016-06-30 01:38:11 +02:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
const struct json_command failhtlc_command = {
|
|
|
|
"failhtlc",
|
|
|
|
json_failhtlc,
|
2016-08-31 07:21:41 +02:00
|
|
|
"Fail htlc proposed by {peerid} which has {id}, using {reason}",
|
2016-01-21 21:15:28 +01:00
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
static void json_commit(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-03-31 08:43:20 +02:00
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->remote.commit || !peer->remote.commit->cstate) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(cmd, "peer not fully established");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:41:57 +02:00
|
|
|
if (!peer->connected) {
|
|
|
|
command_fail(cmd, "peer not connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!state_can_commit(peer->state)) {
|
|
|
|
command_fail(cmd, "peer in state %s", state_name(peer->state));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_commit(peer, cmd);
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command commit_command = {
|
|
|
|
"commit",
|
|
|
|
json_commit,
|
|
|
|
"Commit all staged HTLC changes with {peerid}",
|
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void json_close(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
2016-03-08 01:00:15 +01:00
|
|
|
jsmntok_t *peeridtok;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
NULL)) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Need peerid");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-01-21 21:15:28 +01:00
|
|
|
if (!peer) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!state_is_normal(peer->state) && !state_is_opening(peer->state)) {
|
2016-05-26 07:55:24 +02:00
|
|
|
command_fail(cmd, "Peer is already closing: state %s",
|
|
|
|
state_name(peer->state));
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
if (!peer_start_shutdown(peer)) {
|
|
|
|
command_fail(cmd, "Database error");
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command close_command = {
|
|
|
|
"close",
|
|
|
|
json_close,
|
2016-03-08 01:00:15 +01:00
|
|
|
"Close the channel with peer {peerid}",
|
2016-01-21 21:15:28 +01:00
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
2016-05-02 08:34:56 +02:00
|
|
|
|
2016-08-26 08:03:47 +02:00
|
|
|
static void json_feerate(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
jsmntok_t *feeratetok;
|
|
|
|
u64 feerate;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"feerate", &feeratetok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need feerate");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_tok_u64(buffer, feeratetok, &feerate)) {
|
|
|
|
command_fail(cmd, "Invalid feerate");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmd->dstate->config.default_fee_rate = feerate;
|
|
|
|
|
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command feerate_command = {
|
|
|
|
"dev-feerate",
|
|
|
|
json_feerate,
|
|
|
|
"Change the (default) fee rate to {feerate}",
|
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
|
|
|
|
2016-05-02 08:34:56 +02:00
|
|
|
static void json_disconnect(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-05-02 08:34:56 +02:00
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->conn) {
|
|
|
|
command_fail(cmd, "Peer is already disconnected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't actually close it, since for testing we want only
|
|
|
|
* one side to freak out. We just ensure we ignore it. */
|
|
|
|
log_debug(peer->log, "Pretending connection is closed");
|
|
|
|
peer->fake_close = true;
|
2016-08-30 12:41:57 +02:00
|
|
|
peer->connected = false;
|
2016-08-18 06:55:14 +02:00
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, "json_disconnect", false);
|
2016-05-04 08:44:22 +02:00
|
|
|
peer_breakdown(peer);
|
2016-05-02 08:34:56 +02:00
|
|
|
|
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
static void json_reconnect(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->conn) {
|
|
|
|
command_fail(cmd, "Peer is already disconnected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should reconnect on its own. */
|
|
|
|
io_close(peer->conn);
|
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:24:49 +02:00
|
|
|
static void json_signcommit(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok;
|
|
|
|
u8 *linear;
|
|
|
|
const struct bitcoin_tx *tx;
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-05-06 04:24:49 +02:00
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->local.commit->sig) {
|
2016-05-06 04:24:49 +02:00
|
|
|
command_fail(cmd, "Peer has not given us a signature");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tx = bitcoin_commit(peer);
|
|
|
|
linear = linearize_tx(cmd, tx);
|
|
|
|
|
|
|
|
/* Clear witness for potential future uses. */
|
|
|
|
tx->input[0].witness = tal_free(tx->input[0].witness);
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "tx",
|
|
|
|
tal_hexstr(cmd, linear, tal_count(linear)));
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
static void json_output(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
jsmntok_t *peeridtok, *enabletok;
|
|
|
|
bool enable;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
"enable", &enabletok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need peerid and enable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
peer = find_peer_json(cmd->dstate, buffer, peeridtok);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer) {
|
|
|
|
command_fail(cmd, "Could not find peer with that peerid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->conn) {
|
|
|
|
command_fail(cmd, "Peer is already disconnected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_tok_bool(buffer, enabletok, &enable)) {
|
|
|
|
command_fail(cmd, "enable must be true or false");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(peer->log, "dev-output: output %s",
|
|
|
|
enable ? "enabled" : "disabled");
|
|
|
|
peer->output_enabled = enable;
|
|
|
|
|
|
|
|
/* Flush any outstanding output */
|
|
|
|
if (peer->output_enabled)
|
|
|
|
io_wake(peer);
|
|
|
|
|
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
const struct json_command output_command = {
|
|
|
|
"dev-output",
|
|
|
|
json_output,
|
|
|
|
"Enable/disable any messages to peer {peerid} depending on {enable}",
|
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
|
|
|
|
2016-05-02 08:34:56 +02:00
|
|
|
const struct json_command disconnect_command = {
|
|
|
|
"dev-disconnect",
|
|
|
|
json_disconnect,
|
2016-08-18 06:55:08 +02:00
|
|
|
"Force a disconnect with peer {peerid}",
|
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct json_command reconnect_command = {
|
|
|
|
"dev-reconnect",
|
|
|
|
json_reconnect,
|
|
|
|
"Force a reconnect with peer {peerid}",
|
2016-05-02 08:34:56 +02:00
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
2016-05-06 04:24:49 +02:00
|
|
|
|
|
|
|
const struct json_command signcommit_command = {
|
|
|
|
"dev-signcommit",
|
|
|
|
json_signcommit,
|
|
|
|
"Sign and return the current commit with peer {peerid}",
|
|
|
|
"Returns a hex string on success"
|
|
|
|
};
|