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-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-01-21 21:11:48 +01:00
|
|
|
#include "peer.h"
|
2016-05-06 08:52:11 +02:00
|
|
|
#include "permute_tx.h"
|
2016-03-31 08:43:20 +02:00
|
|
|
#include "pseudorand.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-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:14:13 +01:00
|
|
|
#define FIXME_STUB(peer) do { log_broken((peer)->dstate->base_log, "%s:%u: Implement %s!", __FILE__, __LINE__, __func__); abort(); } while(0)
|
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
struct pending_cmd {
|
|
|
|
struct list_node list;
|
|
|
|
void (*dequeue)(struct peer *, void *arg);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2016-05-03 03:32:20 +02:00
|
|
|
struct pending_input {
|
|
|
|
struct list_node list;
|
|
|
|
enum state_input input;
|
|
|
|
union input idata;
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
static struct peer *find_peer(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;
|
2016-01-21 21:15:27 +01:00
|
|
|
struct peer *peer;
|
2016-03-15 07:37:31 +01:00
|
|
|
|
|
|
|
if (!pubkey_from_hexstr(dstate->secpctx,
|
|
|
|
buffer + peeridtok->start,
|
|
|
|
peeridtok->end - peeridtok->start, &peerid))
|
|
|
|
return NULL;
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
2016-03-15 07:37:31 +01:00
|
|
|
if (peer->state != STATE_INIT && pubkey_eq(&peer->id, &peerid))
|
2016-01-21 21:15:27 +01:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:25 +01:00
|
|
|
static struct json_result *null_response(const tal_t *ctx)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:14:25 +01:00
|
|
|
struct json_result *response;
|
|
|
|
|
|
|
|
response = new_json_result(ctx);
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_object_end(response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_cmd_complete(struct peer *peer, enum command_status status)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
assert(peer->curr_cmd.cmd != INPUT_NONE);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* If it's a json command, complete that now. */
|
|
|
|
if (peer->curr_cmd.jsoncmd) {
|
2016-01-21 21:14:25 +01:00
|
|
|
if (status == CMD_FAIL)
|
|
|
|
/* FIXME: y'know, details. */
|
2016-01-21 21:15:28 +01:00
|
|
|
command_fail(peer->curr_cmd.jsoncmd, "Failed");
|
2016-01-21 21:14:25 +01:00
|
|
|
else {
|
|
|
|
assert(status == CMD_SUCCESS);
|
2016-01-21 21:15:28 +01:00
|
|
|
command_success(peer->curr_cmd.jsoncmd,
|
|
|
|
null_response(peer->curr_cmd.jsoncmd));
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
peer->curr_cmd.cmd = INPUT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_current_command(struct peer *peer,
|
|
|
|
const enum state_input input,
|
|
|
|
void *idata,
|
|
|
|
struct command *jsoncmd)
|
|
|
|
{
|
|
|
|
assert(peer->curr_cmd.cmd == INPUT_NONE);
|
|
|
|
assert(input != INPUT_NONE);
|
|
|
|
|
|
|
|
peer->curr_cmd.cmd = input;
|
|
|
|
/* This is a union, so assign to any member. */
|
|
|
|
peer->curr_cmd.cmddata.pkt = idata;
|
|
|
|
peer->curr_cmd.jsoncmd = jsoncmd;
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 06:53:34 +02:00
|
|
|
void set_peer_state(struct peer *peer, enum state newstate, const char *caller)
|
|
|
|
{
|
|
|
|
log_debug(peer->log, "%s: %s => %s", caller,
|
|
|
|
state_name(peer->state), state_name(newstate));
|
|
|
|
peer->state = newstate;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
static void peer_breakdown(struct peer *peer)
|
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->cond = PEER_CLOSED;
|
|
|
|
|
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
|
|
|
|
* anchor), use it. */
|
2016-05-26 07:55:24 +02:00
|
|
|
} else if (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));
|
|
|
|
} else
|
|
|
|
log_info(peer->log, "Peer breakdown: nothing to do");
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
static bool peer_uncommitted_changes(const struct peer *peer)
|
|
|
|
{
|
|
|
|
/* Not initialized yet? */
|
|
|
|
if (!peer->remote.staging_cstate
|
|
|
|
|| !peer->remote.commit
|
|
|
|
|| !peer->remote.commit->cstate)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* We could have proposed changes to their commit */
|
|
|
|
return peer->remote.staging_cstate->changes
|
|
|
|
!= peer->remote.commit->cstate->changes;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const union input *idata)
|
|
|
|
{
|
|
|
|
enum command_status status;
|
|
|
|
const struct bitcoin_tx *broadcast;
|
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-03-30 08:27:18 +02:00
|
|
|
status = state(peer, input, idata, &broadcast);
|
2016-05-05 06:53:34 +02:00
|
|
|
log_debug(peer->log, "Peer condition: %s", cstatus_name(peer->cond));
|
2016-01-21 21:14:25 +01:00
|
|
|
switch (status) {
|
|
|
|
case CMD_NONE:
|
|
|
|
break;
|
|
|
|
case CMD_SUCCESS:
|
|
|
|
log_add(peer->log, " (command success)");
|
|
|
|
peer_cmd_complete(peer, CMD_SUCCESS);
|
|
|
|
break;
|
|
|
|
case CMD_FAIL:
|
|
|
|
log_add(peer->log, " (command FAIL)");
|
|
|
|
peer_cmd_complete(peer, CMD_FAIL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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-05-26 07:55:24 +02:00
|
|
|
if (peer->state == STATE_ERR_BREAKDOWN)
|
|
|
|
peer_breakdown(peer);
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Start output if not running already; it will close conn. */
|
2016-01-21 21:15:28 +01:00
|
|
|
if (peer->cond == PEER_CLOSED)
|
2016-01-21 21:15:28 +01:00
|
|
|
io_wake(peer);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* FIXME: Some of these should just result in this peer being killed? */
|
2016-05-04 08:44:22 +02:00
|
|
|
else if (state_is_error(peer->state)) {
|
2016-01-21 21:15:28 +01:00
|
|
|
log_broken(peer->log, "Entered error state %s",
|
|
|
|
state_name(peer->state));
|
|
|
|
fatal("Peer entered error state");
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* Break out and free this peer if it's completely done. */
|
2016-04-11 08:59:43 +02:00
|
|
|
if (peer->state == STATE_CLOSED && !peer->conn)
|
2016-01-21 21:15:28 +01:00
|
|
|
io_break(peer);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void try_command(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* If we can accept a command, and we have one queued, run it. */
|
|
|
|
while (peer->cond == PEER_CMD_OK
|
|
|
|
&& !list_empty(&peer->pending_cmd)) {
|
|
|
|
struct pending_cmd *pend = list_pop(&peer->pending_cmd,
|
|
|
|
struct pending_cmd, list);
|
|
|
|
|
|
|
|
assert(peer->curr_cmd.cmd == INPUT_NONE);
|
|
|
|
|
|
|
|
/* This can fail to enqueue a command! */
|
|
|
|
pend->dequeue(peer, pend->arg);
|
|
|
|
tal_free(pend);
|
|
|
|
|
|
|
|
if (peer->curr_cmd.cmd != INPUT_NONE) {
|
|
|
|
state_single(peer, peer->curr_cmd.cmd,
|
|
|
|
&peer->curr_cmd.cmddata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define queue_cmd(peer, cb, arg) \
|
|
|
|
queue_cmd_((peer), \
|
|
|
|
typesafe_cb_preargs(void, void *, \
|
|
|
|
(cb), (arg), \
|
|
|
|
struct peer *), \
|
|
|
|
(arg))
|
|
|
|
|
2016-05-26 07:54:39 +02:00
|
|
|
static bool queue_cmd_(struct peer *peer,
|
2016-01-21 21:15:28 +01:00
|
|
|
void (*dequeue)(struct peer *peer, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
2016-05-26 07:54:39 +02:00
|
|
|
struct pending_cmd *pend;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:54:39 +02:00
|
|
|
if (peer->cond == PEER_CLOSING || peer->cond == PEER_CLOSED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pend = tal(peer, struct pending_cmd);
|
2016-01-21 21:15:28 +01:00
|
|
|
pend->dequeue = dequeue;
|
|
|
|
pend->arg = arg;
|
|
|
|
|
|
|
|
list_add_tail(&peer->pending_cmd, &pend->list);
|
|
|
|
try_command(peer);
|
2016-05-26 07:54:39 +02:00
|
|
|
return true;
|
2016-01-21 21:15:28 +01:00
|
|
|
};
|
|
|
|
|
2016-05-03 03:33:20 +02:00
|
|
|
static void queue_input(struct peer *peer,
|
2016-05-03 03:32:20 +02:00
|
|
|
enum state_input input,
|
|
|
|
const union input *idata)
|
|
|
|
{
|
|
|
|
struct pending_input *pend = tal(peer, struct pending_input);
|
|
|
|
|
|
|
|
pend->input = input;
|
|
|
|
if (idata)
|
|
|
|
pend->idata = *idata;
|
|
|
|
list_add_tail(&peer->pending_input, &pend->list);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* All unrevoked commit txs must have no HTLCs in them. */
|
2016-05-04 08:44:22 +02:00
|
|
|
static bool committed_to_htlcs(const struct peer *peer)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
|
|
|
const struct commit_info *i;
|
|
|
|
|
|
|
|
/* Before anchor exchange, we don't even have cstate. */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!peer->local.commit || !peer->local.commit->cstate)
|
2016-03-31 08:43:20 +02:00
|
|
|
return false;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
i = peer->local.commit;
|
2016-03-31 08:43:20 +02:00
|
|
|
while (i && !i->revocation_preimage) {
|
2016-05-26 07:55:24 +02:00
|
|
|
if (tal_count(i->cstate->side[OURS].htlcs))
|
2016-03-31 08:43:20 +02:00
|
|
|
return true;
|
2016-05-26 07:55:24 +02:00
|
|
|
if (tal_count(i->cstate->side[THEIRS].htlcs))
|
2016-03-31 08:43:20 +02:00
|
|
|
return true;
|
|
|
|
i = i->prev;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
i = peer->remote.commit;
|
2016-03-31 08:43:20 +02:00
|
|
|
while (i && !i->revocation_preimage) {
|
2016-05-26 07:55:24 +02:00
|
|
|
if (tal_count(i->cstate->side[OURS].htlcs))
|
2016-03-31 08:43:20 +02:00
|
|
|
return true;
|
2016-05-26 07:55:24 +02:00
|
|
|
if (tal_count(i->cstate->side[THEIRS].htlcs))
|
2016-03-31 08:43:20 +02:00
|
|
|
return true;
|
|
|
|
i = i->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void state_event(struct peer *peer,
|
|
|
|
const enum state_input input,
|
|
|
|
const union input *idata)
|
|
|
|
{
|
2016-05-03 03:32:20 +02:00
|
|
|
struct pending_input *pend;
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
state_single(peer, input, idata);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
2016-05-03 03:32:20 +02:00
|
|
|
pend = list_pop(&peer->pending_input, struct pending_input, list);
|
|
|
|
if (pend) {
|
|
|
|
state_event(peer, pend->input, &pend->idata);
|
|
|
|
tal_free(pend);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
try_command(peer);
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:33:20 +02:00
|
|
|
void peer_check_if_cleared(struct peer *peer)
|
|
|
|
{
|
|
|
|
if (peer->cleared == INPUT_NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (committed_to_htlcs(peer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
queue_input(peer, peer->cleared, NULL);
|
|
|
|
peer->cleared = INPUT_NONE;
|
|
|
|
}
|
|
|
|
|
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-05-04 08:44:22 +02:00
|
|
|
if (peer->fake_close)
|
|
|
|
return io_out_wait(conn, peer, pkt_out, peer);
|
|
|
|
|
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-05-04 08:44:22 +02:00
|
|
|
if (peer->cond == PEER_CLOSED)
|
2016-01-21 21:15:28 +01:00
|
|
|
return io_close(conn);
|
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-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-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-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-01-21 21:14:25 +01:00
|
|
|
union input idata;
|
|
|
|
const tal_t *ctx = tal(peer, char);
|
|
|
|
|
|
|
|
idata.pkt = tal_steal(ctx, peer->inpkt);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* We ignore packets if they tell us to. */
|
2016-05-02 08:34:56 +02:00
|
|
|
if (!peer->fake_close && peer->cond != PEER_CLOSED) {
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, peer->inpkt->pkt_case, &idata);
|
2016-04-11 09:01:43 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
/* Free peer->inpkt unless stolen above. */
|
|
|
|
tal_free(ctx);
|
|
|
|
|
|
|
|
return peer_read_packet(conn, peer, pkt_in);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void do_anchor_offer(struct peer *peer, void *unused)
|
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
set_current_command(peer, peer->local.offer_anchor, NULL, NULL);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Using queue_cmd is overkill here, but it works. */
|
|
|
|
queue_cmd(peer, do_anchor_offer, NULL);
|
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-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;
|
|
|
|
|
|
|
|
/* 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
|
|
|
/* FIXME: Try to reconnect. */
|
2016-04-11 08:59:43 +02:00
|
|
|
/* This is an expected close. */
|
2016-03-24 02:39:44 +01:00
|
|
|
if (peer->cond == PEER_CLOSED)
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-05-05 06:53:34 +02:00
|
|
|
if (peer->state != STATE_ERR_BREAKDOWN) {
|
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, "peer_disconnect");
|
2016-05-04 08:44:22 +02:00
|
|
|
peer_breakdown(peer);
|
2016-05-05 06:53:34 +02:00
|
|
|
}
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:00:11 +02:00
|
|
|
static void do_commit(struct peer *peer, struct command *jsoncmd)
|
|
|
|
{
|
|
|
|
/* We can have changes we suggested, or changes they suggested. */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (peer->remote.staging_cstate
|
|
|
|
&& peer->remote.commit
|
|
|
|
&& peer->remote.commit->cstate
|
|
|
|
&& peer->remote.staging_cstate->changes
|
|
|
|
!= peer->remote.commit->cstate->changes) {
|
2016-05-09 23:00:11 +02:00
|
|
|
log_debug(peer->log, "do_commit: sending commit command");
|
|
|
|
set_current_command(peer, CMD_SEND_COMMIT, NULL, jsoncmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_debug(peer->log, "do_commit: no changes to commit");
|
|
|
|
|
|
|
|
if (jsoncmd) {
|
|
|
|
command_fail(jsoncmd, "no changes to commit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
queue_cmd(peer, do_commit, (struct command *)NULL);
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
void remote_changes_pending(struct peer *peer)
|
2016-05-09 23:00:11 +02:00
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
log_debug(peer->log, "remote_changes_pending: changes=%u",
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.staging_cstate->changes);
|
2016-05-09 23:00:11 +02:00
|
|
|
if (!peer->commit_timer) {
|
2016-05-26 07:55:24 +02:00
|
|
|
log_debug(peer->log, "remote_changes_pending: adding timer");
|
2016-05-09 23:00:11 +02:00
|
|
|
peer->commit_timer = new_reltimer(peer->dstate, peer,
|
|
|
|
peer->dstate->config.commit_time,
|
|
|
|
try_commit, peer);
|
|
|
|
} else
|
2016-05-26 07:55:24 +02:00
|
|
|
log_debug(peer->log, "remote_changes_pending: timer already exists");
|
2016-05-09 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
static struct peer *new_peer(struct lightningd_state *dstate,
|
2016-01-21 21:11:48 +01:00
|
|
|
struct io_conn *conn,
|
|
|
|
int addr_type, int addr_protocol,
|
2016-01-21 21:11:49 +01:00
|
|
|
enum state_input offer_anchor,
|
2016-01-21 21:11:48 +01:00
|
|
|
const char *in_or_out)
|
|
|
|
{
|
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-01-21 21:11:48 +01:00
|
|
|
/* FIXME: Stop listening if too many peers? */
|
2016-01-21 21:11:49 +01:00
|
|
|
list_add(&dstate->peers, &peer->list);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
peer->state = STATE_INIT;
|
|
|
|
peer->cond = PEER_CMD_OK;
|
2016-01-21 21:11:49 +01:00
|
|
|
peer->dstate = dstate;
|
2016-01-21 21:11:48 +01:00
|
|
|
peer->addr.type = addr_type;
|
|
|
|
peer->addr.protocol = addr_protocol;
|
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-01-21 21:15:28 +01:00
|
|
|
peer->curr_cmd.cmd = INPUT_NONE;
|
|
|
|
list_head_init(&peer->pending_cmd);
|
2016-05-03 03:32:20 +02:00
|
|
|
list_head_init(&peer->pending_input);
|
2016-05-04 08:33:10 +02:00
|
|
|
list_head_init(&peer->outgoing_txs);
|
2016-01-21 21:15:28 +01:00
|
|
|
peer->close_watch_timeout = NULL;
|
2016-01-21 21:15:28 +01:00
|
|
|
peer->anchor.watches = NULL;
|
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-03-24 02:39:41 +01:00
|
|
|
peer->cleared = INPUT_NONE;
|
2016-05-04 08:44:22 +02:00
|
|
|
peer->closing_onchain.tx = NULL;
|
|
|
|
peer->closing_onchain.resolved = NULL;
|
|
|
|
peer->closing_onchain.ci = NULL;
|
2016-05-09 23:00:11 +02:00
|
|
|
peer->commit_timer = NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Make it different from other node (to catch bugs!), but a
|
|
|
|
* round number for simple eyeballing. */
|
|
|
|
peer->htlc_id_counter = pseudorand(1ULL << 32) * 1000;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:14:25 +01: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;
|
2016-05-02 08:34:56 +02:00
|
|
|
peer->fake_close = false;
|
2016-01-21 21:14:25 +01:00
|
|
|
io_set_finish(conn, peer_disconnect, peer);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.offer_anchor = offer_anchor;
|
2016-01-21 21:14:13 +01:00
|
|
|
if (!seconds_to_rel_locktime(dstate->config.rel_locktime,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.locktime))
|
2016-01-21 21:14:13 +01:00
|
|
|
fatal("Invalid locktime configuration %u",
|
|
|
|
dstate->config.rel_locktime);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.mindepth = dstate->config.anchor_confirms;
|
|
|
|
peer->local.commit_fee_rate = dstate->config.commitment_fee_rate;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.commit = peer->remote.commit = NULL;
|
|
|
|
peer->local.staging_cstate = peer->remote.staging_cstate = NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* FIXME: Attach IO logging for this peer. */
|
|
|
|
tal_add_destructor(peer, destroy_peer);
|
|
|
|
|
|
|
|
peer->addr.addrlen = sizeof(peer->addr.saddr);
|
|
|
|
if (getpeername(io_conn_fd(conn), &peer->addr.saddr.s,
|
|
|
|
&peer->addr.addrlen) != 0) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Could not get address for peer: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return tal_free(peer);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
peer->log = new_log(peer, dstate->log_record, "%s%s:%s:",
|
|
|
|
log_prefix(dstate->base_log), in_or_out,
|
2016-01-21 21:11:48 +01:00
|
|
|
netaddr_name(peer, &peer->addr));
|
2016-05-09 23:00:11 +02:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static struct io_plan *peer_connected_out(struct io_conn *conn,
|
2016-01-21 21:11:49 +01:00
|
|
|
struct lightningd_state *dstate,
|
2016-01-21 21:11:48 +01:00
|
|
|
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-01-21 21:11:49 +01:00
|
|
|
struct peer *peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
|
2016-01-21 21:11:49 +01:00
|
|
|
CMD_OPEN_WITH_ANCHOR, "out");
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!peer) {
|
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-01-21 21:11:48 +01:00
|
|
|
log_info(peer->log, "Connected out to %s:%s",
|
|
|
|
connect->name, connect->port);
|
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));
|
|
|
|
return peer_crypto_setup(conn, peer, peer_crypto_on);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_connected_in(struct io_conn *conn,
|
2016-01-21 21:11:49 +01:00
|
|
|
struct lightningd_state *dstate)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:11:49 +01:00
|
|
|
struct peer *peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
|
2016-01-21 21:11:49 +01:00
|
|
|
CMD_OPEN_WITHOUT_ANCHOR, "in");
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!peer)
|
|
|
|
return io_close(conn);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
log_info(peer->log, "Peer connected in");
|
2016-01-21 21:14:25 +01:00
|
|
|
return peer_crypto_setup(conn, peer, peer_crypto_on);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!addr || bind(fd, addr, len) == 0) {
|
|
|
|
if (listen(fd, 5) == 0)
|
|
|
|
return fd;
|
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));
|
|
|
|
} else
|
2016-01-21 21:11:49 +01:00
|
|
|
log_debug(dstate->base_log, "Failed to bind on %u socket: %s",
|
2016-01-21 21:11:48 +01:00
|
|
|
domain, strerror(errno));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = htons(portnum);
|
|
|
|
|
|
|
|
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-01-21 21:14:25 +01:00
|
|
|
"Connect to a {host} at {port} offering anchor of {satoshis}",
|
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-01-21 21:14:13 +01:00
|
|
|
struct anchor_watch {
|
2016-01-21 21:15:28 +01:00
|
|
|
struct peer *peer;
|
2016-01-21 21:14:13 +01:00
|
|
|
enum state_input depthok;
|
|
|
|
enum state_input timeout;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* If timeout != INPUT_NONE, this is the timer. */
|
|
|
|
struct oneshot *timer;
|
2016-01-21 21:14:13 +01:00
|
|
|
};
|
|
|
|
|
2016-05-04 08:40:39 +02:00
|
|
|
static void anchor_depthchange(struct peer *peer, unsigned int depth,
|
2016-04-24 12:09:21 +02:00
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
2016-01-21 21:15:05 +01:00
|
|
|
{
|
2016-04-24 12:09:21 +02:00
|
|
|
struct anchor_watch *w = peer->anchor.watches;
|
2016-05-04 08:44:22 +02:00
|
|
|
|
2016-01-21 21:15:05 +01:00
|
|
|
/* Still waiting for it to reach depth? */
|
|
|
|
if (w->depthok != INPUT_NONE) {
|
2016-05-26 07:55:24 +02:00
|
|
|
if (depth >= peer->local.mindepth) {
|
2016-01-21 21:15:05 +01:00
|
|
|
enum state_input in = w->depthok;
|
|
|
|
w->depthok = INPUT_NONE;
|
2016-01-21 21:15:28 +01:00
|
|
|
/* We don't need the timeout timer any more. */
|
|
|
|
w->timer = tal_free(w->timer);
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, in, NULL);
|
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-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:36:20 +02:00
|
|
|
/* Yay, segwit! We can just compare txids, even though we don't have both
|
|
|
|
* signatures. */
|
|
|
|
static bool txidmatch(const struct bitcoin_tx *tx,
|
|
|
|
const struct sha256_double *txid)
|
2016-01-21 21:15:05 +01:00
|
|
|
{
|
2016-05-03 03:36:20 +02:00
|
|
|
struct sha256_double tx_txid;
|
2016-01-21 21:15:05 +01:00
|
|
|
|
2016-05-03 03:36:20 +02:00
|
|
|
bitcoin_txid(tx, &tx_txid);
|
|
|
|
return structeq(txid, &tx_txid);
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:36:20 +02:00
|
|
|
static struct commit_info *find_commit(struct commit_info *ci,
|
|
|
|
const struct sha256_double *txid)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
2016-05-03 03:36:20 +02:00
|
|
|
while (ci) {
|
|
|
|
if (txidmatch(ci->tx, txid))
|
|
|
|
return ci;
|
2016-03-31 08:43:20 +02:00
|
|
|
ci = ci->prev;
|
|
|
|
}
|
2016-05-03 03:36:20 +02:00
|
|
|
return NULL;
|
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-05-04 08:42:47 +02:00
|
|
|
static struct channel_htlc *htlc_by_index(const struct commit_info *ci,
|
|
|
|
size_t index)
|
2016-05-04 08:41:47 +02:00
|
|
|
{
|
|
|
|
if (ci->map[index] == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* First two are non-HTLC outputs to us, them. */
|
|
|
|
assert(index >= 2);
|
|
|
|
index -= 2;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (index < tal_count(ci->cstate->side[OURS].htlcs))
|
|
|
|
return cast_const(struct channel_htlc *,
|
|
|
|
ci->cstate->side[OURS].htlcs)
|
2016-05-04 08:41:47 +02:00
|
|
|
+ index;
|
2016-05-26 07:55:24 +02:00
|
|
|
index -= tal_count(ci->cstate->side[OURS].htlcs);
|
|
|
|
assert(index < tal_count(ci->cstate->side[THEIRS].htlcs));
|
|
|
|
return cast_const(struct channel_htlc *, ci->cstate->side[THEIRS].htlcs)
|
|
|
|
+ index;
|
2016-05-04 08:41:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
static bool htlc_is_ours(const struct commit_info *ci, size_t index)
|
2016-05-04 08:41:47 +02:00
|
|
|
{
|
|
|
|
assert(index >= 2);
|
|
|
|
index -= 2;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
return index < tal_count(ci->cstate->side[OURS].htlcs);
|
2016-05-04 08:41:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:42:47 +02:00
|
|
|
/* Create a HTLC refund collection */
|
2016-05-04 08:44:22 +02:00
|
|
|
static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
|
2016-05-04 08:42:47 +02:00
|
|
|
const struct commit_info *ci,
|
|
|
|
unsigned int i)
|
|
|
|
{
|
|
|
|
u8 *wscript;
|
|
|
|
struct channel_htlc *htlc;
|
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
u64 fee, satoshis;
|
|
|
|
|
|
|
|
htlc = htlc_by_index(ci, i);
|
|
|
|
|
|
|
|
wscript = bitcoin_redeem_htlc_send(peer,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.finalkey,
|
2016-05-04 08:42:47 +02:00
|
|
|
&htlc->expiry,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.locktime,
|
2016-05-04 08:42:47 +02:00
|
|
|
&ci->revocation_hash,
|
|
|
|
&htlc->rhash);
|
|
|
|
|
|
|
|
/* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */
|
|
|
|
tx->lock_time = htlc->expiry.locktime;
|
|
|
|
tx->input[0].index = 0;
|
|
|
|
bitcoin_txid(ci->tx, &tx->input[0].txid);
|
|
|
|
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-05-26 07:55:24 +02:00
|
|
|
bitcoin_redeem_single(tx, &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. */
|
|
|
|
/* FIXME: Dynamic fees! */
|
|
|
|
fee = fee_by_feerate(83 + 539 / 4,
|
|
|
|
peer->dstate->config.closing_fee_rate);
|
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
|
|
|
if (fee > satoshis || is_dust_amount(satoshis - fee))
|
|
|
|
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);
|
|
|
|
|
|
|
|
tx->input[0].witness = bitcoin_witness_htlc(tx, NULL, &sig, wscript);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (peer->closing_onchain.tx) {
|
|
|
|
/* FIXME: Log old txid */
|
|
|
|
log_unusual(peer->log, "New anchor spend, forgetting old");
|
|
|
|
peer->closing_onchain.tx = tal_free(peer->closing_onchain.tx);
|
|
|
|
peer->closing_onchain.resolved = NULL;
|
|
|
|
peer->closing_onchain.ci = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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. */
|
|
|
|
return peer->closing_onchain.tx;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:26:55 +02:00
|
|
|
static void connect_input(const struct commit_info *ci,
|
|
|
|
struct bitcoin_tx_input *input,
|
|
|
|
u32 index)
|
|
|
|
{
|
|
|
|
bitcoin_txid(ci->tx, &input->txid);
|
|
|
|
input->index = index;
|
|
|
|
input->amount = tal_dup(ci, u64, &ci->tx->output[index].amount);
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
static void resolve_cheating(struct peer *peer)
|
|
|
|
{
|
2016-05-06 04:26:55 +02:00
|
|
|
const struct bitcoin_tx *tx = peer->closing_onchain.tx;
|
|
|
|
const struct commit_info *ci = peer->closing_onchain.ci;
|
|
|
|
struct bitcoin_tx *steal_tx;
|
|
|
|
u8 **wscripts;
|
|
|
|
size_t i, n, num_to_steal;
|
2016-05-06 08:52:11 +02:00
|
|
|
int *map;
|
2016-05-06 04:26:55 +02:00
|
|
|
|
|
|
|
peer->closing_onchain.resolved
|
|
|
|
= tal_arrz(tx, const struct bitcoin_tx *, tal_count(ci->map));
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If a node sees a *commitment tx* for which it has a revocation
|
|
|
|
* preimage, it *resolves* the funding transaction output:
|
|
|
|
*
|
|
|
|
* 1. _A's main output_: No action is required; this is a
|
|
|
|
* simple P2WPKH output. This output is considered
|
|
|
|
* *resolved* by the *commitment tx*.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Their commit tx, so our output is [1], theirs in [0]. */
|
|
|
|
peer->closing_onchain.resolved[1] = tx;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
num_to_steal = 0;
|
|
|
|
if (ci->map[0] == -1)
|
|
|
|
peer->closing_onchain.resolved[0] = tx;
|
|
|
|
else
|
|
|
|
num_to_steal++;
|
|
|
|
|
|
|
|
for (i = 2; i < tal_count(ci->map); i++)
|
|
|
|
if (ci->map[i] == -1)
|
|
|
|
peer->closing_onchain.resolved[i] = tx;
|
|
|
|
else
|
|
|
|
num_to_steal++;
|
|
|
|
|
|
|
|
/* Nothing to steal? */
|
|
|
|
if (num_to_steal == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* The node MAY use a single transaction to *resolve* all the
|
|
|
|
* outputs; due to the 450 HTLC-per-party limit (See BOLT #2:
|
|
|
|
* 3.2. Adding an HTLC) this can be done within a standard
|
|
|
|
* transaction.
|
|
|
|
*/
|
|
|
|
steal_tx = bitcoin_tx(peer, num_to_steal, 1);
|
|
|
|
|
|
|
|
wscripts = tal_arr(steal_tx, u8 *, num_to_steal);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (ci->map[0] != -1) {
|
|
|
|
connect_input(ci, &steal_tx->input[n], ci->map[0]);
|
2016-05-06 04:30:30 +02:00
|
|
|
peer->closing_onchain.resolved[0] = steal_tx;
|
2016-05-06 04:26:55 +02:00
|
|
|
wscripts[n++]
|
|
|
|
= bitcoin_redeem_secret_or_delay(wscripts,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.locktime,
|
|
|
|
&peer->local.finalkey,
|
2016-05-06 04:26:55 +02:00
|
|
|
&ci->revocation_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 2; i < tal_count(ci->map); i++) {
|
|
|
|
struct channel_htlc *h;
|
|
|
|
|
|
|
|
if (ci->map[i] == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
peer->closing_onchain.resolved[i] = steal_tx;
|
|
|
|
|
|
|
|
connect_input(ci, &steal_tx->input[n], ci->map[i]);
|
|
|
|
|
|
|
|
h = htlc_by_index(ci, i);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!htlc_is_ours(ci, i)) {
|
2016-05-06 04:26:55 +02:00
|
|
|
wscripts[n]
|
|
|
|
= bitcoin_redeem_htlc_send(wscripts,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.finalkey,
|
2016-05-06 04:26:55 +02:00
|
|
|
&h->expiry,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.locktime,
|
2016-05-06 04:26:55 +02:00
|
|
|
&ci->revocation_hash,
|
|
|
|
&h->rhash);
|
|
|
|
} else {
|
|
|
|
wscripts[n]
|
|
|
|
= bitcoin_redeem_htlc_recv(wscripts,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.finalkey,
|
2016-05-06 04:26:55 +02:00
|
|
|
&h->expiry,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.locktime,
|
2016-05-06 04:26:55 +02:00
|
|
|
&ci->revocation_hash,
|
|
|
|
&h->rhash);
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
assert(n == num_to_steal);
|
|
|
|
|
2016-05-06 08:52:11 +02:00
|
|
|
/* This obscures the order in which HTLCs were received, at least. */
|
|
|
|
map = tal_arr(steal_tx, int, num_to_steal);
|
|
|
|
permute_inputs(steal_tx->input, steal_tx->input_count, map);
|
|
|
|
|
2016-05-06 04:26:55 +02:00
|
|
|
/* Now, we can sign them all (they're all of same form). */
|
|
|
|
for (n = 0; n < num_to_steal; n++) {
|
|
|
|
struct bitcoin_signature sig;
|
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-05-06 08:52:11 +02:00
|
|
|
peer_sign_steal_input(peer, steal_tx, map[n], wscripts[n], &sig.sig);
|
2016-05-06 04:26:55 +02:00
|
|
|
|
2016-05-06 08:52:11 +02:00
|
|
|
steal_tx->input[map[n]].witness
|
2016-05-06 04:26:55 +02:00
|
|
|
= bitcoin_witness_secret(steal_tx,
|
|
|
|
ci->revocation_preimage,
|
|
|
|
sizeof(*ci->revocation_preimage),
|
|
|
|
&sig,
|
|
|
|
wscripts[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcast_tx(peer, steal_tx);
|
2016-05-04 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void our_htlc_spent(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
size_t input_num,
|
|
|
|
ptrint_t *pi)
|
|
|
|
{
|
|
|
|
struct channel_htlc *h;
|
|
|
|
struct sha256 preimage, sha;
|
|
|
|
size_t i = ptr2int(pi);
|
|
|
|
|
|
|
|
/* It should be spending the HTLC we expect. */
|
|
|
|
assert(peer->closing_onchain.ci->map[i] == tx->input[input_num].index);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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 %zu", i);
|
|
|
|
|
|
|
|
/* Our timeout tx has all-zeroes, so we can distinguish it. */
|
|
|
|
if (memeqzero(tx->input[input_num].witness[1], sizeof(preimage)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(&preimage, tx->input[input_num].witness[1], sizeof(preimage));
|
|
|
|
sha256(&sha, &preimage, sizeof(preimage));
|
|
|
|
|
|
|
|
h = htlc_by_index(peer->closing_onchain.ci, i);
|
|
|
|
|
|
|
|
/* 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 %zu on-chain using r value",
|
|
|
|
i);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved[i] = irrevocably_resolved(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void our_htlc_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
bool our_commit,
|
|
|
|
size_t i)
|
|
|
|
{
|
|
|
|
u32 mediantime;
|
|
|
|
struct channel_htlc *h;
|
|
|
|
|
|
|
|
/* Must be in a block. */
|
|
|
|
if (depth == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mediantime = get_tip_mediantime(peer->dstate);
|
|
|
|
h = htlc_by_index(peer->closing_onchain.ci, i);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FIXME: Handle expiry in blocks. */
|
|
|
|
if (mediantime < abs_locktime_to_seconds(&h->expiry))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (our_commit) {
|
|
|
|
u32 csv_timeout;
|
|
|
|
|
|
|
|
/* FIXME: Handle CSV in blocks. */
|
|
|
|
csv_timeout = get_tx_mediantime(peer->dstate, txid)
|
2016-05-26 07:55:24 +02:00
|
|
|
+ rel_locktime_to_seconds(&peer->remote.locktime);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
if (mediantime <= csv_timeout)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If the output has *timed out* and not been *resolved*, the node
|
|
|
|
* MUST *resolve* the output by spending it.
|
|
|
|
*/
|
|
|
|
if (!peer->closing_onchain.resolved[i]) {
|
|
|
|
peer->closing_onchain.resolved[i]
|
|
|
|
= htlc_timeout_tx(peer, peer->closing_onchain.ci, i);
|
|
|
|
broadcast_tx(peer, peer->closing_onchain.resolved[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void our_htlc_depth_ourcommit(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
ptrint_t *i)
|
|
|
|
{
|
|
|
|
our_htlc_depth(peer, depth, txid, true, ptr2int(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void our_htlc_depth_theircommit(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
ptrint_t *i)
|
|
|
|
{
|
|
|
|
our_htlc_depth(peer, depth, txid, false, ptr2int(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_our_htlcs(struct peer *peer,
|
|
|
|
const struct commit_info *ci,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
const struct bitcoin_tx **resolved,
|
|
|
|
bool from_ourcommit,
|
|
|
|
size_t start, size_t num)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
for (i = start; i < start + num; i++) {
|
|
|
|
/* Doesn't exist? Resolved by tx itself. */
|
|
|
|
if (ci->map[i] == -1) {
|
|
|
|
resolved[i] = tx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
watch_txo(tx, peer, &txid, ci->map[i], our_htlc_spent,
|
|
|
|
int2ptr(i));
|
|
|
|
watch_txid(tx, peer, &txid,
|
|
|
|
from_ourcommit
|
|
|
|
? our_htlc_depth_ourcommit
|
|
|
|
: our_htlc_depth_theircommit,
|
|
|
|
int2ptr(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* If the node receives a redemption preimage for a *commitment tx* output it
|
|
|
|
* was offered, it MUST *resolve* the output by spending it using the
|
|
|
|
* preimage. Otherwise, the other node could spend it once it as *timed out*
|
|
|
|
* as above.
|
|
|
|
*/
|
|
|
|
bool resolve_one_htlc(struct peer *peer, u64 id, const struct sha256 *preimage)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void their_htlc_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
ptrint_t *pi)
|
|
|
|
{
|
|
|
|
u32 mediantime;
|
|
|
|
struct channel_htlc *h;
|
|
|
|
size_t i = ptr2int(pi);
|
|
|
|
|
|
|
|
/* Must be in a block. */
|
|
|
|
if (depth == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mediantime = get_tip_mediantime(peer->dstate);
|
|
|
|
h = htlc_by_index(peer->closing_onchain.ci, i);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Otherwise, if the output HTLC has expired, it is considered
|
|
|
|
* *irrevocably resolved*.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FIXME: Handle expiry in blocks. */
|
|
|
|
if (mediantime < abs_locktime_to_seconds(&h->expiry))
|
|
|
|
return;
|
|
|
|
|
|
|
|
peer->closing_onchain.resolved[i] = irrevocably_resolved(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_their_htlcs(struct peer *peer,
|
|
|
|
const struct commit_info *ci,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
const struct bitcoin_tx **resolved,
|
|
|
|
size_t start, size_t num)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = start; i < start + num; i++) {
|
|
|
|
/* Doesn't exist? Resolved by tx itself. */
|
|
|
|
if (ci->map[i] == -1) {
|
|
|
|
resolved[i] = tx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch_tx(tx, peer, tx, their_htlc_depth, int2ptr(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void our_main_output_depth(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
|
|
|
{
|
|
|
|
u32 mediantime, csv_timeout;
|
|
|
|
|
|
|
|
/* Not in block any more? */
|
|
|
|
if (depth == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mediantime = get_tip_mediantime(peer->dstate);
|
|
|
|
|
|
|
|
/* FIXME: Handle CSV in blocks. */
|
|
|
|
csv_timeout = get_tx_mediantime(peer->dstate, txid)
|
2016-05-26 07:55:24 +02:00
|
|
|
+ rel_locktime_to_seconds(&peer->remote.locktime);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
if (mediantime <= csv_timeout)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Already done? (FIXME: Delete after first time) */
|
|
|
|
if (peer->closing_onchain.resolved[0])
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved[0] = bitcoin_spend_ours(peer);
|
|
|
|
broadcast_tx(peer, peer->closing_onchain.resolved[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* When node A sees its own *commitment tx*:
|
|
|
|
*/
|
|
|
|
static void resolve_our_unilateral(struct peer *peer)
|
|
|
|
{
|
|
|
|
const struct bitcoin_tx *tx = peer->closing_onchain.tx;
|
|
|
|
const struct commit_info *ci = peer->closing_onchain.ci;
|
|
|
|
size_t num_ours, num_theirs;
|
|
|
|
|
|
|
|
peer->closing_onchain.resolved
|
|
|
|
= tal_arrz(tx, const struct bitcoin_tx *, tal_count(ci->map));
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
watch_tx(tx, peer, tx, our_main_output_depth, NULL);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 2. _B's main output_: No action required, this output is considered
|
|
|
|
* *resolved* by the *commitment tx*.
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved[1] = tx;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
num_ours = tal_count(ci->cstate->side[OURS].htlcs);
|
|
|
|
num_theirs = tal_count(ci->cstate->side[THEIRS].htlcs);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 3. _A's offered HTLCs_: See On-chain HTLC Handling: Our Offers below.
|
|
|
|
*/
|
|
|
|
resolve_our_htlcs(peer, ci, tx,
|
|
|
|
peer->closing_onchain.resolved,
|
|
|
|
true, 2, num_ours);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 4. _B's offered HTLCs_: See On-chain HTLC Handling: Their
|
|
|
|
* Offers below.
|
|
|
|
*/
|
|
|
|
resolve_their_htlcs(peer, ci, tx,
|
|
|
|
peer->closing_onchain.resolved,
|
|
|
|
2 + num_ours, num_theirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* Similarly, when node A sees a *commitment tx* from B:
|
|
|
|
*/
|
|
|
|
static void resolve_their_unilateral(struct peer *peer)
|
|
|
|
{
|
|
|
|
const struct bitcoin_tx *tx = peer->closing_onchain.tx;
|
|
|
|
const struct commit_info *ci = peer->closing_onchain.ci;
|
|
|
|
size_t num_ours, num_theirs;
|
|
|
|
|
|
|
|
peer->closing_onchain.resolved
|
|
|
|
= tal_arrz(tx, const struct bitcoin_tx *, tal_count(ci->map));
|
|
|
|
|
|
|
|
/* 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*.
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved[1] = tx;
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 2. _B's main output_: No action required, this output is
|
|
|
|
* considered *resolved* by the *commitment tx*.
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved[0] = tx;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
num_ours = tal_count(ci->cstate->side[OURS].htlcs);
|
|
|
|
num_theirs = tal_count(ci->cstate->side[THEIRS].htlcs);
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 3. _A's offered HTLCs_: See On-chain HTLC Handling: Our Offers below.
|
|
|
|
*/
|
|
|
|
resolve_our_htlcs(peer, ci, tx,
|
|
|
|
peer->closing_onchain.resolved,
|
|
|
|
false, 2 + num_theirs, num_ours);
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* 4. _B's offered HTLCs_: See On-chain HTLC Handling: Their
|
|
|
|
* Offers below.
|
|
|
|
*/
|
|
|
|
resolve_their_htlcs(peer, ci, tx,
|
|
|
|
peer->closing_onchain.resolved,
|
|
|
|
2, num_theirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_mutual_close(struct peer *peer)
|
|
|
|
{
|
|
|
|
const struct bitcoin_tx *tx = peer->closing_onchain.tx;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
* #2 "4.1: Closing initiation: close_clearing").
|
|
|
|
*/
|
|
|
|
peer->closing_onchain.resolved
|
|
|
|
= tal_arr(tx, const struct bitcoin_tx *, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called every time the tx spending the funding tx changes depth. */
|
|
|
|
static void check_for_resolution(struct peer *peer,
|
|
|
|
unsigned int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *unused)
|
|
|
|
{
|
|
|
|
size_t i, n = tal_count(peer->closing_onchain.resolved);
|
|
|
|
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++)
|
|
|
|
if (!peer->closing_onchain.resolved[i])
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
|
|
|
bitcoin_txid(peer->closing_onchain.resolved[i], &txid);
|
|
|
|
if (get_tx_depth(peer->dstate, &txid) < forever)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer, STATE_CLOSED, "check_for_resolution");
|
2016-05-04 08:44:22 +02:00
|
|
|
io_break(peer);
|
|
|
|
}
|
|
|
|
|
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! */
|
|
|
|
static void anchor_spent(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
2016-05-03 03:30:20 +02:00
|
|
|
size_t input_num,
|
2016-04-24 12:09:21 +02:00
|
|
|
void *unused)
|
2016-01-21 21:15:05 +01:00
|
|
|
{
|
2016-05-03 03:36:20 +02:00
|
|
|
struct sha256_double txid;
|
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. */
|
|
|
|
if (input_num != 0)
|
|
|
|
fatal("Anchor spend by non-single input tx");
|
2016-05-04 08:44:22 +02:00
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node SHOULD fail the connection if it is not already
|
|
|
|
* closed when it sees the funding transaction spent.
|
|
|
|
*/
|
|
|
|
if (peer->cond != PEER_CLOSED) {
|
|
|
|
peer->cond = PEER_CLOSED;
|
|
|
|
|
|
|
|
/* BOLT #onchain:
|
|
|
|
*
|
|
|
|
* A node MAY send a descriptive error packet in this case.
|
|
|
|
*/
|
|
|
|
queue_pkt_err(peer,
|
|
|
|
pkt_err(peer, "Funding transaction spent!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We may have been following a different spend. Forget it. */
|
|
|
|
reset_onchain_closing(peer);
|
|
|
|
|
|
|
|
peer->closing_onchain.tx = tal_steal(peer, tx);
|
2016-05-03 03:36:20 +02:00
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->closing_onchain.ci = find_commit(peer->remote.commit, &txid);
|
2016-05-04 08:44:22 +02:00
|
|
|
if (peer->closing_onchain.ci) {
|
|
|
|
if (peer->closing_onchain.ci->revocation_preimage) {
|
2016-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer, STATE_CLOSE_ONCHAIN_CHEATED,
|
|
|
|
"anchor_spent");
|
2016-05-04 08:44:22 +02:00
|
|
|
resolve_cheating(peer);
|
|
|
|
} else {
|
2016-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer,
|
|
|
|
STATE_CLOSE_ONCHAIN_THEIR_UNILATERAL,
|
|
|
|
"anchor_spent");
|
2016-05-04 08:44:22 +02:00
|
|
|
resolve_their_unilateral(peer);
|
2016-05-03 03:36:20 +02:00
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
} else if (txidmatch(peer->local.commit->tx, &txid)) {
|
2016-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer,
|
|
|
|
STATE_CLOSE_ONCHAIN_OUR_UNILATERAL,
|
|
|
|
"anchor_spent");
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->closing_onchain.ci = peer->local.commit;
|
2016-05-04 08:44:22 +02:00
|
|
|
resolve_our_unilateral(peer);
|
2016-05-03 03:36:20 +02:00
|
|
|
} else if (is_mutual_close(peer, tx)) {
|
2016-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer,
|
|
|
|
STATE_CLOSE_ONCHAIN_MUTUAL,
|
|
|
|
"anchor_spent");
|
2016-05-04 08:44:22 +02:00
|
|
|
resolve_mutual_close(peer);
|
2016-05-06 04:26:32 +02:00
|
|
|
} else {
|
|
|
|
/* 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-05-04 08:44:22 +02:00
|
|
|
/* FIXME: Log harder! */
|
2016-05-06 04:26:32 +02:00
|
|
|
log_broken(peer->log, "Unknown tx spend! Funds may be lost!");
|
|
|
|
set_peer_state(peer,
|
|
|
|
STATE_ERR_INFORMATION_LEAK,
|
|
|
|
"anchor_spent");
|
|
|
|
/* No longer call into the state machine. */
|
|
|
|
peer->anchor.watches->depthok = INPUT_NONE;
|
|
|
|
return;
|
|
|
|
}
|
2016-05-04 08:44:22 +02:00
|
|
|
assert(peer->closing_onchain.resolved != NULL);
|
|
|
|
watch_tx(tx, peer, tx, check_for_resolution, NULL);
|
|
|
|
|
|
|
|
/* No longer call into the state machine. */
|
|
|
|
peer->anchor.watches->depthok = INPUT_NONE;
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void anchor_timeout(struct anchor_watch *w)
|
|
|
|
{
|
|
|
|
assert(w == w->peer->anchor.watches);
|
|
|
|
state_event(w->peer, w->timeout, NULL);
|
|
|
|
|
|
|
|
/* Freeing this gets rid of the other watches, and timer, too. */
|
|
|
|
w->peer->anchor.watches = tal_free(w);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_watch_anchor(struct peer *peer,
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
struct anchor_watch *w;
|
|
|
|
|
|
|
|
w = peer->anchor.watches = tal(peer, struct anchor_watch);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
w->peer = peer;
|
2016-01-21 21:15:05 +01:00
|
|
|
w->depthok = depthok;
|
|
|
|
w->timeout = timeout;
|
|
|
|
|
2016-04-24 12:09:21 +02:00
|
|
|
watch_txid(w, peer, &peer->anchor.txid, anchor_depthchange, NULL);
|
|
|
|
watch_txo(w, 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.
|
|
|
|
*/
|
|
|
|
if (w->timeout != INPUT_NONE) {
|
2016-05-09 22:56:09 +02:00
|
|
|
w->timer = new_reltimer(peer->dstate, w,
|
|
|
|
time_from_sec(7200
|
2016-05-26 07:55:24 +02:00
|
|
|
+ 20*peer->local.mindepth),
|
2016-05-09 22:56:09 +02:00
|
|
|
anchor_timeout, w);
|
2016-01-21 21:15:28 +01:00
|
|
|
} else
|
|
|
|
w->timer = NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void peer_unwatch_anchor_depth(struct peer *peer,
|
|
|
|
enum state_input depthok,
|
|
|
|
enum state_input timeout)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
assert(peer->anchor.watches);
|
|
|
|
peer->anchor.watches = tal_free(peer->anchor.watches);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
uint64_t commit_tx_fee(const struct bitcoin_tx *commit, uint64_t 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-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,
|
|
|
|
"peer_create_close_tx: can't afford fee %"PRIu64,
|
|
|
|
fee);
|
2016-03-24 02:39:41 +01:00
|
|
|
return NULL;
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
log_debug(peer->log,
|
2016-03-24 02:39:41 +01:00
|
|
|
"creating close-tx with fee %"PRIu64": to %02x%02x%02x%02x/%02x%02x%02x%02x, amounts %u/%u",
|
|
|
|
fee,
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.finalkey.der[0], peer->local.finalkey.der[1],
|
|
|
|
peer->local.finalkey.der[2], peer->local.finalkey.der[3],
|
|
|
|
peer->remote.finalkey.der[0], peer->remote.finalkey.der[1],
|
|
|
|
peer->remote.finalkey.der[2], peer->remote.finalkey.der[3],
|
2016-05-26 07:55:24 +02:00
|
|
|
cstate.side[OURS].pay_msat / 1000,
|
|
|
|
cstate.side[THEIRS].pay_msat / 1000);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
return create_close_tx(peer->dstate->secpctx, peer,
|
2016-04-24 12:31:52 +02:00
|
|
|
peer->closing.our_script,
|
|
|
|
peer->closing.their_script,
|
2016-03-24 02:39:41 +01:00
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
2016-05-26 07:55:24 +02:00
|
|
|
cstate.side[OURS].pay_msat / 1000,
|
|
|
|
cstate.side[THEIRS].pay_msat / 1000);
|
2016-03-24 02:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void peer_calculate_close_fee(struct peer *peer)
|
|
|
|
{
|
2016-03-24 02:42:43 +01:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* FIXME: Dynamic fee */
|
|
|
|
peer->closing.our_fee
|
|
|
|
= fee_by_feerate(txsize, peer->dstate->config.closing_fee_rate);
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
* The sender MUST set `close_fee` lower than or equal to the
|
2016-05-02 08:28:56 +02:00
|
|
|
* fee of the final commitment transaction, and MUST set
|
2016-03-24 02:39:41 +01:00
|
|
|
* `close_fee` to an even number of satoshis.
|
|
|
|
*/
|
2016-05-26 07:55:24 +02:00
|
|
|
maxfee = commit_tx_fee(peer->local.commit->tx, peer->anchor.satoshis);
|
2016-03-24 02:42:43 +01:00
|
|
|
if (peer->closing.our_fee > maxfee) {
|
|
|
|
/* This shouldn't happen: we never accept a commit fee
|
|
|
|
* less than the min_rate, which is greater than the
|
|
|
|
* closing_fee_rate. Also, our txsize estimate for
|
|
|
|
* the closing tx is 2 bytes smaller than the commitment tx. */
|
|
|
|
log_unusual(peer->log,
|
|
|
|
"Closing fee %"PRIu64" exceeded commit fee %"PRIu64", reducing.",
|
|
|
|
peer->closing.our_fee, maxfee);
|
|
|
|
peer->closing.our_fee = maxfee;
|
|
|
|
|
|
|
|
/* 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-03-24 02:39:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
2016-05-06 04:25:52 +02:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
/* 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-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
void peer_watch_htlcs_cleared(struct peer *peer,
|
|
|
|
enum state_input all_done)
|
|
|
|
{
|
|
|
|
assert(peer->cleared == INPUT_NONE);
|
|
|
|
assert(all_done != INPUT_NONE);
|
|
|
|
peer->cleared = all_done;
|
2016-05-03 03:33:20 +02:00
|
|
|
|
|
|
|
peer_check_if_cleared(peer);
|
2016-03-24 02:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a bitcoin close tx, using last signature they sent. */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_close(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-24 02:39:41 +01:00
|
|
|
struct bitcoin_tx *close_tx;
|
|
|
|
struct bitcoin_signature our_close_sig;
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
close_tx = peer_create_close_tx(peer, peer->closing.their_fee);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
our_close_sig.stype = SIGHASH_ALL;
|
|
|
|
peer_sign_mutual_close(peer, close_tx, &our_close_sig.sig);
|
|
|
|
|
2016-04-24 12:25:35 +02:00
|
|
|
close_tx->input[0].witness
|
|
|
|
= bitcoin_witness_2of2(close_tx->input,
|
|
|
|
peer->closing.their_sig,
|
|
|
|
&our_close_sig,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.commitkey,
|
|
|
|
&peer->local.commitkey);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
return close_tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a bitcoin spend tx (to spend our commit's outputs) */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-04-24 12:27:35 +02:00
|
|
|
u8 *witnessscript;
|
2016-05-26 07:55:24 +02:00
|
|
|
const struct bitcoin_tx *commit = peer->local.commit->tx;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct bitcoin_signature sig;
|
|
|
|
struct bitcoin_tx *tx;
|
2016-04-24 12:27:35 +02:00
|
|
|
unsigned int p2wsh_out;
|
2016-04-11 09:02:43 +02:00
|
|
|
uint64_t fee;
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* The redeemscript for a commit tx is fairly complex. */
|
2016-04-24 12:27:35 +02:00
|
|
|
witnessscript = bitcoin_redeem_secret_or_delay(peer,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.locktime,
|
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.commit->revocation_hash);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
|
|
|
/* Now, create transaction to spend it. */
|
2016-03-30 08:27:18 +02:00
|
|
|
tx = bitcoin_tx(peer, 1, 1);
|
2016-01-21 21:14:13 +01:00
|
|
|
bitcoin_txid(commit, &tx->input[0].txid);
|
2016-04-24 12:27:35 +02:00
|
|
|
p2wsh_out = find_p2wsh_out(commit, witnessscript);
|
|
|
|
tx->input[0].index = p2wsh_out;
|
2016-05-26 07:55:24 +02:00
|
|
|
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
|
2016-04-11 09:13:53 +02:00
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64,
|
2016-04-24 12:27:35 +02:00
|
|
|
&commit->output[p2wsh_out].amount);
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx,
|
2016-05-26 07:55:24 +02:00
|
|
|
bitcoin_redeem_single(tx, &peer->local.finalkey));
|
2016-01-21 21:14:13 +01:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
2016-05-03 03:37:20 +02:00
|
|
|
/* 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-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
/* FIXME: Dynamic fees! */
|
2016-05-03 03:37:20 +02:00
|
|
|
fee = fee_by_feerate(83 + 176 / 4,
|
|
|
|
peer->dstate->config.closing_fee_rate);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
2016-05-03 03:37:20 +02:00
|
|
|
if (fee > commit->output[p2wsh_out].amount
|
|
|
|
|| is_dust_amount(commit->output[p2wsh_out].amount - fee))
|
2016-03-24 02:42:43 +01:00
|
|
|
fatal("Amount of %"PRIu64" won't cover fee %"PRIu64,
|
2016-05-03 03:37:20 +02:00
|
|
|
commit->output[p2wsh_out].amount, fee);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-05-03 03:37:20 +02:00
|
|
|
tx->output[0].amount = commit->output[p2wsh_out].amount - fee;
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-05-03 03:37:20 +02:00
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-04-24 12:27:35 +02:00
|
|
|
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
|
|
|
|
|
|
|
|
tx->input[0].witness = bitcoin_witness_secret(tx, NULL, 0, &sig,
|
|
|
|
witnessscript);
|
2016-05-03 03:37:20 +02:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
return tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Sign and return our commit tx */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_commit(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
struct bitcoin_signature sig;
|
|
|
|
|
2016-04-24 12:25:35 +02:00
|
|
|
/* Can't be signed already, and can't have scriptsig! */
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.commit->tx->input[0].script_length == 0);
|
|
|
|
assert(!peer->local.commit->tx->input[0].witness);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-05-26 07:55:24 +02:00
|
|
|
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig.sig);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.commit->tx->input[0].witness
|
|
|
|
= bitcoin_witness_2of2(peer->local.commit->tx->input,
|
|
|
|
peer->local.commit->sig,
|
2016-04-24 12:25:35 +02:00
|
|
|
&sig,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.commitkey,
|
|
|
|
&peer->local.commitkey);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
return peer->local.commit->tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Now we can create anchor tx. */
|
|
|
|
static void got_feerate(struct lightningd_state *dstate,
|
|
|
|
u64 rate, 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-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-04-24 12:25:35 +02:00
|
|
|
fee = fee_by_feerate(measure_tx_cost(tx)/4 + 1+73 + 1+33 + 1, rate);
|
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:15:28 +01:00
|
|
|
state_event(peer, BITCOIN_ANCHOR_CREATED, NULL);
|
2016-01-21 21:14:27 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Creation the bitcoin anchor tx, spending output user provided. */
|
2016-01-21 21:14:13 +01:00
|
|
|
void bitcoin_create_anchor(struct peer *peer, enum state_input done)
|
|
|
|
{
|
2016-01-21 21:14:27 +01:00
|
|
|
/* We must be offering anchor for us to try creating it */
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.offer_anchor);
|
2016-01-21 21:14:27 +01:00
|
|
|
|
|
|
|
assert(done == BITCOIN_ANCHOR_CREATED);
|
2016-04-12 05:37:04 +02:00
|
|
|
bitcoind_estimate_fee(peer->dstate, got_feerate, peer);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We didn't end up broadcasting the anchor: release the utxos.
|
|
|
|
* If done != INPUT_NONE, remove existing create_anchor too. */
|
|
|
|
void bitcoin_release_anchor(struct peer *peer, enum state_input done)
|
|
|
|
{
|
2016-01-21 21:14:27 +01:00
|
|
|
|
|
|
|
/* FIXME: stop bitcoind command */
|
|
|
|
log_unusual(peer->log, "Anchor not spent, please -zapwallettxs");
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-05-26 07:55:24 +02:00
|
|
|
void add_unacked(struct peer_visible_state *which,
|
|
|
|
const union htlc_staging *stage)
|
|
|
|
{
|
|
|
|
size_t n = tal_count(which->unacked_changes);
|
|
|
|
tal_resize(&which->unacked_changes, n+1);
|
|
|
|
which->unacked_changes[n] = *stage;
|
|
|
|
}
|
|
|
|
|
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-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-05-26 07:55:24 +02:00
|
|
|
peer->local.commit->cstate = initial_funding(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-05-26 07:55:24 +02:00
|
|
|
peer->remote.commit->cstate = copy_funding(peer,
|
|
|
|
peer->local.commit->cstate);
|
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-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.locktime,
|
|
|
|
&peer->remote.locktime,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&peer->local.commit->revocation_hash,
|
|
|
|
peer->local.commit->cstate,
|
|
|
|
OURS,
|
|
|
|
&peer->local.commit->map);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
peer->remote.commit->tx = create_commit_tx(peer->remote.commit,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.finalkey,
|
|
|
|
&peer->local.locktime,
|
|
|
|
&peer->remote.locktime,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&peer->remote.commit->revocation_hash,
|
|
|
|
peer->remote.commit->cstate,
|
|
|
|
THEIRS,
|
|
|
|
&peer->remote.commit->map);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.staging_cstate = copy_funding(peer, peer->local.commit->cstate);
|
|
|
|
peer->remote.staging_cstate = copy_funding(peer, peer->remote.commit->cstate);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
peer->local.unacked_changes = tal_arr(peer, union htlc_staging, 0);
|
|
|
|
peer->remote.unacked_changes = tal_arr(peer, union htlc_staging, 0);
|
|
|
|
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
const struct channel_oneside *side)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
json_array_start(response, id);
|
2016-01-21 21:15:28 +01:00
|
|
|
for (i = 0; i < tal_count(side->htlcs); i++) {
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_u64(response, "msatoshis", side->htlcs[i].msatoshis);
|
|
|
|
json_add_abstime(response, "expiry", &side->htlcs[i].expiry);
|
|
|
|
json_add_hex(response, "rhash",
|
|
|
|
&side->htlcs[i].rhash,
|
|
|
|
sizeof(side->htlcs[i].rhash));
|
|
|
|
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! */
|
|
|
|
/* FIXME: Show status of peers! */
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
json_add_string(response, "cmd", input_name(p->curr_cmd.cmd));
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
/* This is only valid after crypto setup. */
|
|
|
|
if (p->state != STATE_INIT)
|
2016-03-08 01:00:15 +01:00
|
|
|
json_add_hex(response, "peerid",
|
2016-03-30 08:24:16 +02:00
|
|
|
p->id.der, sizeof(p->id.der));
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-04 08:44:22 +02:00
|
|
|
json_add_bool(response, "connected", p->conn && !p->fake_close);
|
|
|
|
|
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);
|
|
|
|
json_add_htlcs(response, "our_htlcs", &last->side[OURS]);
|
|
|
|
json_add_htlcs(response, "their_htlcs", &last->side[THEIRS]);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* Any changes since then? */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (p->local.staging_cstate->changes != last->changes)
|
2016-05-26 07:55:24 +02:00
|
|
|
json_add_num(response, "local_staged_changes",
|
2016-05-26 07:55:24 +02:00
|
|
|
p->local.staging_cstate->changes
|
2016-03-31 08:43:20 +02:00
|
|
|
- last->changes);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (p->remote.staging_cstate->changes
|
|
|
|
!= p->remote.commit->cstate->changes)
|
|
|
|
json_add_num(response, "remote_staged_changes",
|
|
|
|
p->remote.staging_cstate->changes
|
|
|
|
- p->remote.commit->cstate->changes);
|
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-01-21 21:15:28 +01:00
|
|
|
static void set_htlc_command(struct peer *peer,
|
|
|
|
struct command *jsoncmd,
|
|
|
|
enum state_input cmd,
|
2016-03-15 07:37:31 +01:00
|
|
|
const union htlc_staging *stage)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: memleak! */
|
|
|
|
/* FIXME: Get rid of struct htlc_progress */
|
|
|
|
struct htlc_progress *progress = tal(peer, struct htlc_progress);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
progress->stage = *stage;
|
|
|
|
set_current_command(peer, cmd, progress, jsoncmd);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* FIXME: Keep a timeout for each peer, in case they're unresponsive. */
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* FIXME: Make sure no HTLCs in any unrevoked commit tx are live. */
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void check_htlc_expiry(struct peer *peer, void *unused)
|
|
|
|
{
|
|
|
|
size_t i;
|
2016-03-15 07:37:31 +01:00
|
|
|
union htlc_staging stage;
|
|
|
|
|
|
|
|
stage.fail.fail = HTLC_FAIL;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Check their currently still-existing htlcs for expiry:
|
|
|
|
* We eliminate them from staging as we go. */
|
2016-05-26 07:55:24 +02:00
|
|
|
for (i = 0; i < tal_count(peer->remote.staging_cstate->side[THEIRS].htlcs); i++) {
|
|
|
|
struct channel_htlc *htlc = &peer->remote.staging_cstate->side[THEIRS].htlcs[i];
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* Not a seconds-based expiry? */
|
|
|
|
if (!abs_locktime_is_seconds(&htlc->expiry))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Not well-expired? */
|
|
|
|
if (controlled_time().ts.tv_sec - 30
|
|
|
|
< abs_locktime_to_seconds(&htlc->expiry))
|
|
|
|
continue;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
stage.fail.id = htlc->id;
|
|
|
|
set_htlc_command(peer, NULL, CMD_SEND_HTLC_FAIL, &stage);
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void htlc_expiry_timeout(struct peer *peer)
|
|
|
|
{
|
|
|
|
log_debug(peer->log, "Expiry timedout!");
|
|
|
|
queue_cmd(peer, check_htlc_expiry, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_add_htlc_expiry(struct peer *peer,
|
|
|
|
const struct abs_locktime *expiry)
|
|
|
|
{
|
2016-05-09 22:56:09 +02:00
|
|
|
struct timeabs absexpiry;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
/* Add 30 seconds to be sure peers agree on timeout. */
|
2016-05-09 22:56:09 +02:00
|
|
|
absexpiry.ts.tv_sec = abs_locktime_to_seconds(expiry) + 30;
|
|
|
|
absexpiry.ts.tv_nsec = 0;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-09 22:56:09 +02:00
|
|
|
new_abstimer(peer->dstate, peer, absexpiry, htlc_expiry_timeout, peer);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
struct newhtlc {
|
2016-03-15 07:37:31 +01:00
|
|
|
struct channel_htlc htlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct command *jsoncmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* We do final checks just before we start command, as things may have
|
|
|
|
* changed. */
|
|
|
|
static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc)
|
|
|
|
{
|
|
|
|
struct channel_state *cstate;
|
2016-03-15 07:37:31 +01:00
|
|
|
union htlc_staging stage;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Now we can assign counter and guarantee uniqueness. */
|
|
|
|
newhtlc->htlc.id = peer->htlc_id_counter;
|
2016-03-15 07:37:31 +01:00
|
|
|
stage.add.add = HTLC_ADD;
|
|
|
|
stage.add.htlc = newhtlc->htlc;
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT add a HTLC if it would result in it
|
2016-05-06 07:12:52 +02:00
|
|
|
* offering more than 300 HTLCs in either commitment transaction.
|
2016-03-31 08:43:20 +02:00
|
|
|
*/
|
2016-05-26 07:55:24 +02:00
|
|
|
if (tal_count(peer->local.staging_cstate->side[OURS].htlcs) == 300
|
2016-05-26 07:55:24 +02:00
|
|
|
|| tal_count(peer->remote.staging_cstate->side[OURS].htlcs) == 300) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(newhtlc->jsoncmd, "Too many HTLCs");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT offer `amount_msat` it cannot pay for in
|
|
|
|
* both commitment transactions at the current `fee_rate`
|
|
|
|
*/
|
2016-05-26 07:55:24 +02:00
|
|
|
cstate = copy_funding(newhtlc, peer->remote.staging_cstate);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!funding_add_htlc(cstate, newhtlc->htlc.msatoshis,
|
|
|
|
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
|
2016-05-26 07:55:24 +02:00
|
|
|
newhtlc->htlc.id, OURS)) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(newhtlc->jsoncmd,
|
|
|
|
"Cannot afford %"PRIu64
|
|
|
|
" milli-satoshis in their commit tx",
|
|
|
|
newhtlc->htlc.msatoshis);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
cstate = copy_funding(newhtlc, peer->local.staging_cstate);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!funding_add_htlc(cstate, newhtlc->htlc.msatoshis,
|
|
|
|
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
|
|
|
|
newhtlc->htlc.id, OURS)) {
|
2016-01-21 21:15:28 +01:00
|
|
|
command_fail(newhtlc->jsoncmd,
|
2016-03-31 08:43:20 +02:00
|
|
|
"Cannot afford %"PRIu64
|
|
|
|
" milli-satoshis in our commit tx",
|
2016-03-15 07:37:31 +01:00
|
|
|
newhtlc->htlc.msatoshis);
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Make sure we never offer the same one twice. */
|
|
|
|
peer->htlc_id_counter++;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Never propose duplicate rvalues? */
|
|
|
|
set_htlc_command(peer, newhtlc->jsoncmd, CMD_SEND_HTLC_ADD, &stage);
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
struct newhtlc *newhtlc;
|
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-03-15 07:37:31 +01:00
|
|
|
peer = find_peer(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-01-21 21:15:27 +01:00
|
|
|
/* Attach to cmd until it's complete. */
|
2016-01-21 21:15:28 +01:00
|
|
|
newhtlc = tal(cmd, struct newhtlc);
|
|
|
|
newhtlc->jsoncmd = cmd;
|
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
if (!json_tok_u64(buffer, msatoshistok, &newhtlc->htlc.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;
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
if (!seconds_to_abs_locktime(expiry, &newhtlc->htlc.expiry)) {
|
2016-01-21 21:15:27 +01:00
|
|
|
command_fail(cmd, "'%.*s' is not a valid number",
|
|
|
|
(int)(expirytok->end - expirytok->start),
|
|
|
|
buffer + expirytok->start);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
if (abs_locktime_to_seconds(&newhtlc->htlc.expiry) <
|
2016-01-21 21:15:28 +01:00
|
|
|
controlled_time().ts.tv_sec + peer->dstate->config.min_expiry) {
|
|
|
|
command_fail(cmd, "HTLC expiry too soon!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
if (abs_locktime_to_seconds(&newhtlc->htlc.expiry) >
|
2016-01-21 21:15:28 +01:00
|
|
|
controlled_time().ts.tv_sec + peer->dstate->config.max_expiry) {
|
|
|
|
command_fail(cmd, "HTLC expiry too far!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
if (!hex_decode(buffer + rhashtok->start,
|
|
|
|
rhashtok->end - rhashtok->start,
|
2016-03-15 07:37:31 +01:00
|
|
|
&newhtlc->htlc.rhash,
|
|
|
|
sizeof(newhtlc->htlc.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-05-26 07:54:39 +02:00
|
|
|
if (!queue_cmd(peer, do_newhtlc, newhtlc))
|
|
|
|
command_fail(cmd, "Peer closing");
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Use HTLC ids, not r values! */
|
2016-01-21 21:15:27 +01:00
|
|
|
const struct json_command newhtlc_command = {
|
|
|
|
"newhtlc",
|
|
|
|
json_newhtlc,
|
2016-03-08 01:00:15 +01:00
|
|
|
"Offer {peerid} an HTLC worth {msatoshis} in {expiry} (in seconds since Jan 1 1970) with {rhash}",
|
2016-01-21 21:15:27 +01:00
|
|
|
"Returns an empty result on success"
|
|
|
|
};
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* Looks for their HTLC, but must be committed. */
|
|
|
|
static size_t find_their_committed_htlc(struct peer *peer,
|
|
|
|
const struct sha256 *rhash)
|
|
|
|
{
|
|
|
|
/* Must be in last committed cstate. */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (funding_find_htlc(peer->remote.commit->cstate, rhash, THEIRS) == -1)
|
2016-05-26 07:55:24 +02:00
|
|
|
return -1;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
return funding_find_htlc(peer->remote.staging_cstate, rhash, THEIRS);
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
struct fulfillhtlc {
|
|
|
|
struct command *jsoncmd;
|
|
|
|
struct sha256 r;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void do_fullfill(struct peer *peer,
|
|
|
|
struct fulfillhtlc *fulfillhtlc)
|
|
|
|
{
|
|
|
|
struct sha256 rhash;
|
|
|
|
size_t i;
|
2016-03-15 07:37:31 +01:00
|
|
|
union htlc_staging stage;
|
|
|
|
|
|
|
|
stage.fulfill.fulfill = HTLC_FULFILL;
|
|
|
|
stage.fulfill.r = fulfillhtlc->r;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
sha256(&rhash, &fulfillhtlc->r, sizeof(fulfillhtlc->r));
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
i = find_their_committed_htlc(peer, &rhash);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (i == -1) {
|
2016-03-31 08:43:20 +02:00
|
|
|
command_fail(fulfillhtlc->jsoncmd, "preimage htlc not found");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
stage.fulfill.id = peer->remote.staging_cstate->side[THEIRS].htlcs[i].id;
|
2016-03-31 08:43:20 +02:00
|
|
|
set_htlc_command(peer, fulfillhtlc->jsoncmd,
|
2016-03-15 07:37:31 +01:00
|
|
|
CMD_SEND_HTLC_FULFILL, &stage);
|
2016-01-21 21:15:28 +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-03-08 01:00:15 +01:00
|
|
|
jsmntok_t *peeridtok, *rtok;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct fulfillhtlc *fulfillhtlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
"r", &rtok,
|
|
|
|
NULL)) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Need peerid and r");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
peer = find_peer(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-01-21 21:15:28 +01:00
|
|
|
fulfillhtlc = tal(cmd, struct fulfillhtlc);
|
|
|
|
fulfillhtlc->jsoncmd = cmd;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
if (!hex_decode(buffer + rtok->start,
|
|
|
|
rtok->end - rtok->start,
|
2016-01-21 21:15:28 +01:00
|
|
|
&fulfillhtlc->r, sizeof(fulfillhtlc->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:54:39 +02:00
|
|
|
if (!queue_cmd(peer, do_fullfill, fulfillhtlc))
|
|
|
|
command_fail(cmd, "Peer closing");
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command fulfillhtlc_command = {
|
|
|
|
"fulfillhtlc",
|
|
|
|
json_fulfillhtlc,
|
2016-03-08 01:00:15 +01:00
|
|
|
"Redeem htlc proposed by {peerid} 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
|
|
|
struct failhtlc {
|
|
|
|
struct command *jsoncmd;
|
|
|
|
struct sha256 rhash;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void do_failhtlc(struct peer *peer,
|
|
|
|
struct failhtlc *failhtlc)
|
|
|
|
{
|
|
|
|
size_t i;
|
2016-03-15 07:37:31 +01:00
|
|
|
union htlc_staging stage;
|
|
|
|
|
|
|
|
stage.fail.fail = HTLC_FAIL;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* Look in peer->remote.staging_cstate->a, as that's where we'll
|
2016-03-31 08:43:20 +02:00
|
|
|
* immediately remove it from: avoids double-handling. */
|
2016-05-26 07:55:24 +02:00
|
|
|
i = find_their_committed_htlc(peer, &failhtlc->rhash);
|
2016-05-26 07:55:24 +02:00
|
|
|
if (i == -1) {
|
2016-01-21 21:15:28 +01:00
|
|
|
command_fail(failhtlc->jsoncmd, "htlc not found");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 07:55:24 +02:00
|
|
|
stage.fail.id = peer->remote.staging_cstate->side[THEIRS].htlcs[i].id;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
set_htlc_command(peer, failhtlc->jsoncmd, CMD_SEND_HTLC_FAIL, &stage);
|
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-03-08 01:00:15 +01:00
|
|
|
jsmntok_t *peeridtok, *rhashtok;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct failhtlc *failhtlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"peerid", &peeridtok,
|
|
|
|
"rhash", &rhashtok,
|
|
|
|
NULL)) {
|
2016-03-08 01:00:15 +01:00
|
|
|
command_fail(cmd, "Need peerid and rhash");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
peer = find_peer(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-01-21 21:15:28 +01:00
|
|
|
failhtlc = tal(cmd, struct failhtlc);
|
|
|
|
failhtlc->jsoncmd = cmd;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
if (!hex_decode(buffer + rhashtok->start,
|
|
|
|
rhashtok->end - rhashtok->start,
|
2016-01-21 21:15:28 +01:00
|
|
|
&failhtlc->rhash, sizeof(failhtlc->rhash))) {
|
2016-01-21 21:15:28 +01:00
|
|
|
command_fail(cmd, "'%.*s' is not a valid sha256 preimage",
|
|
|
|
(int)(rhashtok->end - rhashtok->start),
|
|
|
|
buffer + rhashtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:54:39 +02:00
|
|
|
if (!queue_cmd(peer, do_failhtlc, failhtlc))
|
|
|
|
command_fail(cmd, "Peer closing");
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command failhtlc_command = {
|
|
|
|
"failhtlc",
|
|
|
|
json_failhtlc,
|
2016-03-08 01:00:15 +01:00
|
|
|
"Fail htlc proposed by {peerid} which has redeem hash {rhash}",
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = find_peer(cmd->dstate, buffer, peeridtok);
|
|
|
|
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-05-26 07:54:39 +02:00
|
|
|
if (!queue_cmd(peer, do_commit, cmd))
|
|
|
|
command_fail(cmd, "Peer closing");
|
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-03-15 07:37:31 +01:00
|
|
|
peer = find_peer(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;
|
|
|
|
}
|
|
|
|
if (peer->cond == PEER_CLOSING) {
|
|
|
|
command_fail(cmd, "Peer is already closing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlike other things, CMD_CLOSE is always valid. */
|
|
|
|
log_debug(peer->log, "Sending CMD_CLOSE");
|
|
|
|
state_event(peer, CMD_CLOSE, NULL);
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = find_peer(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-05-05 06:53:34 +02:00
|
|
|
set_peer_state(peer, STATE_ERR_BREAKDOWN, "json_disconnect");
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = find_peer(cmd->dstate, buffer, peeridtok);
|
|
|
|
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-02 08:34:56 +02:00
|
|
|
const struct json_command disconnect_command = {
|
|
|
|
"dev-disconnect",
|
|
|
|
json_disconnect,
|
|
|
|
"Force a disconned with peer {peerid}",
|
|
|
|
"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"
|
|
|
|
};
|