2016-01-21 21:14:25 +01:00
|
|
|
#include "bitcoind.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-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-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-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-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-01-21 21:14:25 +01:00
|
|
|
u64 satoshis;
|
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-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-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);
|
|
|
|
|
|
|
|
status = state(peer, input, idata, &broadcast);
|
2016-01-21 21:14:25 +01:00
|
|
|
log_debug(peer->log, "%s => %s",
|
|
|
|
input_name(input), state_name(peer->state));
|
|
|
|
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;
|
|
|
|
case CMD_REQUEUE:
|
|
|
|
log_add(peer->log, " (Command requeue)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
if (tal_count(peer->outpkt) > old_outpkts) {
|
2016-03-30 08:27:41 +02:00
|
|
|
Pkt *outpkt = peer->outpkt[old_outpkts].pkt;
|
2016-01-21 21:14:25 +01:00
|
|
|
log_add(peer->log, " (out %s)", input_name(outpkt->pkt_case));
|
|
|
|
}
|
|
|
|
if (broadcast) {
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
|
|
|
bitcoin_txid(broadcast, &txid);
|
|
|
|
/* FIXME: log_struct */
|
|
|
|
log_add(peer->log, " (tx %02x%02x%02x%02x...)",
|
|
|
|
txid.sha.u.u8[0], txid.sha.u.u8[1],
|
|
|
|
txid.sha.u.u8[2], txid.sha.u.u8[3]);
|
|
|
|
bitcoind_send_tx(peer->dstate, broadcast);
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
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? */
|
|
|
|
if (state_is_error(peer->state)) {
|
|
|
|
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. */
|
|
|
|
if (peer->state == STATE_CLOSED)
|
|
|
|
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))
|
|
|
|
|
|
|
|
static void queue_cmd_(struct peer *peer,
|
|
|
|
void (*dequeue)(struct peer *peer, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct pending_cmd *pend = tal(peer, struct pending_cmd);
|
|
|
|
|
|
|
|
pend->dequeue = dequeue;
|
|
|
|
pend->arg = arg;
|
|
|
|
|
|
|
|
list_add_tail(&peer->pending_cmd, &pend->list);
|
|
|
|
try_command(peer);
|
|
|
|
};
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* All unrevoked commit txs must have no HTLCs in them. */
|
|
|
|
bool committed_to_htlcs(const struct peer *peer)
|
|
|
|
{
|
|
|
|
const struct commit_info *i;
|
|
|
|
|
|
|
|
/* Before anchor exchange, we don't even have cstate. */
|
|
|
|
if (!peer->us.commit || !peer->us.commit->cstate)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
i = peer->us.commit;
|
|
|
|
while (i && !i->revocation_preimage) {
|
|
|
|
if (tal_count(i->cstate->a.htlcs))
|
|
|
|
return true;
|
|
|
|
if (tal_count(i->cstate->b.htlcs))
|
|
|
|
return true;
|
|
|
|
i = i->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = peer->them.commit;
|
|
|
|
while (i && !i->revocation_preimage) {
|
|
|
|
if (tal_count(i->cstate->a.htlcs))
|
|
|
|
return true;
|
|
|
|
if (tal_count(i->cstate->b.htlcs))
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
state_single(peer, input, idata);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
if (peer->cleared != INPUT_NONE && !committed_to_htlcs(peer)) {
|
|
|
|
enum state_input all_done = peer->cleared;
|
|
|
|
peer->cleared = INPUT_NONE;
|
|
|
|
state_single(peer, all_done, NULL);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
try_command(peer);
|
2016-01-21 21:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2016-03-30 08:27:41 +02:00
|
|
|
struct out_pkt out;
|
2016-03-15 07:38:31 +01:00
|
|
|
size_t n = tal_count(peer->outpkt);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-03-15 07:38:31 +01:00
|
|
|
if (n == 0) {
|
2016-01-21 21:15:28 +01:00
|
|
|
/* We close the connection once we've sent everything. */
|
|
|
|
if (peer->cond == PEER_CLOSED)
|
|
|
|
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-03-30 08:27:41 +02:00
|
|
|
return peer_write_packet(conn, peer, out.pkt, out.ack_cb, out.ack_arg,
|
|
|
|
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. */
|
|
|
|
if (peer->cond != PEER_CLOSED)
|
|
|
|
state_event(peer, peer->inpkt->pkt_case, &idata);
|
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)
|
|
|
|
{
|
|
|
|
set_current_command(peer, peer->us.offer_anchor, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
peer_get_revocation_hash(peer, 0, &peer->us.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)
|
|
|
|
{
|
|
|
|
const struct bitcoin_tx *broadcast;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Try to reconnect. */
|
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-03-30 08:27:18 +02:00
|
|
|
state(peer, INPUT_CONNECTION_LOST, NULL, &broadcast);
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
if (broadcast) {
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
|
|
|
bitcoin_txid(broadcast, &txid);
|
|
|
|
/* FIXME: log_struct */
|
2016-03-24 02:39:44 +01:00
|
|
|
log_debug(peer->log, "INPUT_CONN_LOST: tx %02x%02x%02x%02x...",
|
2016-01-21 21:14:25 +01:00
|
|
|
txid.sha.u.u8[0], txid.sha.u.u8[1],
|
|
|
|
txid.sha.u.u8[2], txid.sha.u.u8[3]);
|
|
|
|
bitcoind_send_tx(peer->dstate, broadcast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-30 08:27:41 +02:00
|
|
|
peer->outpkt = tal_arr(peer, struct out_pkt, 0);
|
2016-01-21 21:15:28 +01:00
|
|
|
peer->curr_cmd.cmd = INPUT_NONE;
|
|
|
|
list_head_init(&peer->pending_cmd);
|
2016-03-08 01:13:15 +01:00
|
|
|
peer->commit_tx_counter = 0;
|
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;
|
|
|
|
peer->cleared = INPUT_NONE;
|
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;
|
|
|
|
io_set_finish(conn, peer_disconnect, peer);
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
peer->us.offer_anchor = offer_anchor;
|
2016-01-21 21:14:13 +01:00
|
|
|
if (!seconds_to_rel_locktime(dstate->config.rel_locktime,
|
|
|
|
&peer->us.locktime))
|
|
|
|
fatal("Invalid locktime configuration %u",
|
|
|
|
dstate->config.rel_locktime);
|
2016-01-21 21:11:49 +01:00
|
|
|
peer->us.mindepth = dstate->config.anchor_confirms;
|
2016-03-24 02:42:43 +01:00
|
|
|
peer->us.commit_fee_rate = dstate->config.commitment_fee_rate;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2016-01-21 21:14:27 +01:00
|
|
|
peer->us.commit = peer->them.commit = NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
peer->us.staging_cstate = peer->them.staging_cstate = NULL;
|
|
|
|
|
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-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-01-21 21:14:27 +01:00
|
|
|
peer->anchor.satoshis = connect->satoshis;
|
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-01-21 21:14:25 +01:00
|
|
|
jsmntok_t *host, *port, *satoshis;
|
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,
|
|
|
|
"satoshis", &satoshis,
|
|
|
|
NULL)) {
|
2016-01-21 21:14:25 +01:00
|
|
|
command_fail(cmd, "Need host, port and satoshis");
|
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-01-21 21:14:25 +01:00
|
|
|
if (!json_tok_u64(buffer, satoshis, &connect->satoshis))
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid number",
|
|
|
|
(int)(satoshis->end - satoshis->start),
|
|
|
|
buffer + satoshis->start);
|
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;
|
|
|
|
enum state_input unspent;
|
|
|
|
enum state_input theyspent;
|
|
|
|
enum state_input otherspent;
|
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-01-21 21:15:05 +01:00
|
|
|
static void anchor_depthchange(struct peer *peer, int depth,
|
2016-01-21 21:15:28 +01:00
|
|
|
const struct sha256_double *blkhash,
|
2016-01-21 21:15:05 +01:00
|
|
|
struct anchor_watch *w)
|
|
|
|
{
|
|
|
|
/* Still waiting for it to reach depth? */
|
|
|
|
if (w->depthok != INPUT_NONE) {
|
|
|
|
/* Beware sign! */
|
|
|
|
if (depth >= (int)peer->us.mindepth) {
|
|
|
|
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
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (depth < 0 && w->unspent != INPUT_NONE) {
|
|
|
|
enum state_input in = w->unspent;
|
|
|
|
w->unspent = INPUT_NONE;
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, in, NULL);
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't compare scriptSigs: we don't know them anyway! */
|
|
|
|
static bool txmatch(const struct bitcoin_tx *txa, const struct bitcoin_tx *txb)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (txa->version != txb->version
|
|
|
|
|| txa->input_count != txb->input_count
|
|
|
|
|| txa->output_count != txb->output_count
|
|
|
|
|| txa->lock_time != txb->lock_time)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < txa->input_count; i++) {
|
|
|
|
if (!structeq(&txa->input[i].txid, &txb->input[i].txid)
|
|
|
|
|| txa->input[i].index != txb->input[i].index
|
|
|
|
|| txa->input[i].sequence_number != txb->input[i].sequence_number)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < txa->output_count; i++) {
|
|
|
|
if (txa->output[i].amount != txb->output[i].amount
|
|
|
|
|| txa->output[i].script_length != txb->output[i].script_length
|
|
|
|
|| memcmp(txa->output[i].script, txb->output[i].script,
|
|
|
|
txa->output[i].script_length != 0))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* We may have two possible "current" commits; this loop will check them both. */
|
|
|
|
static bool is_unrevoked_commit(const struct commit_info *ci,
|
|
|
|
const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
while (ci && !ci->revocation_preimage) {
|
|
|
|
if (txmatch(ci->tx, tx))
|
|
|
|
return true;
|
|
|
|
ci = ci->prev;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
/* A mutual close is a simple 2 output p2sh to the final addresses, but
|
|
|
|
* without knowing fee we can't determine order, so examine each output. */
|
|
|
|
static bool is_mutual_close(const struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-03-24 02:39:41 +01:00
|
|
|
const u8 *ctx, *our_p2sh, *their_p2sh;
|
|
|
|
bool matches;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
if (tx->output_count != 2)
|
2016-01-21 21:15:28 +01:00
|
|
|
return false;
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
if (!is_p2sh(tx->output[0].script, tx->output[0].script_length)
|
|
|
|
|| !is_p2sh(tx->output[1].script, tx->output[1].script_length))
|
2016-01-21 21:15:28 +01:00
|
|
|
return false;
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
/* FIXME: Cache these! */
|
|
|
|
ctx = tal(NULL, u8);
|
|
|
|
our_p2sh = scriptpubkey_p2sh(ctx,
|
|
|
|
bitcoin_redeem_single(tx, &peer->us.finalkey));
|
|
|
|
their_p2sh = scriptpubkey_p2sh(ctx,
|
|
|
|
bitcoin_redeem_single(tx, &peer->them.finalkey));
|
|
|
|
|
|
|
|
matches =
|
|
|
|
(memcmp(tx->output[0].script, our_p2sh, tal_count(our_p2sh)) == 0
|
|
|
|
&& memcmp(tx->output[1].script, their_p2sh, tal_count(their_p2sh)) == 0)
|
|
|
|
|| (memcmp(tx->output[0].script, their_p2sh, tal_count(their_p2sh)) == 0
|
|
|
|
&& memcmp(tx->output[1].script, our_p2sh, tal_count(our_p2sh)) == 0);
|
|
|
|
tal_free(ctx);
|
|
|
|
return matches;
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_depth_cb(struct peer *peer, int depth)
|
|
|
|
{
|
|
|
|
if (depth >= peer->dstate->config.forever_confirms) {
|
|
|
|
state_event(peer, BITCOIN_CLOSE_DONE, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
struct anchor_watch *w)
|
|
|
|
{
|
|
|
|
union input idata;
|
|
|
|
|
|
|
|
/* FIXME: change type in idata? */
|
|
|
|
idata.btc = (struct bitcoin_event *)tx;
|
2016-03-31 08:43:20 +02:00
|
|
|
if (is_unrevoked_commit(peer->them.commit, tx))
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, w->theyspent, &idata);
|
2016-03-24 02:39:41 +01:00
|
|
|
else if (is_mutual_close(peer, tx))
|
2016-01-21 21:15:28 +01:00
|
|
|
add_close_tx_watch(peer, peer, tx, close_depth_cb);
|
2016-01-21 21:15:05 +01:00
|
|
|
else
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, w->otherspent, &idata);
|
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,
|
|
|
|
enum state_input timeout,
|
|
|
|
enum state_input unspent,
|
|
|
|
enum state_input theyspent,
|
|
|
|
enum state_input otherspent)
|
|
|
|
{
|
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;
|
|
|
|
w->unspent = unspent;
|
|
|
|
w->theyspent = theyspent;
|
|
|
|
w->otherspent = otherspent;
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
add_anchor_watch(w, peer, &peer->anchor.txid, peer->anchor.index,
|
2016-01-21 21:15:05 +01:00
|
|
|
anchor_depthchange,
|
|
|
|
anchor_spent,
|
|
|
|
w);
|
|
|
|
|
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) {
|
|
|
|
w->timer = oneshot_timeout(peer->dstate, w,
|
|
|
|
7200 + 20*peer->us.mindepth,
|
|
|
|
anchor_timeout, w);
|
|
|
|
} 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-01-21 21:15:28 +01:00
|
|
|
static void commit_tx_depth(struct peer *peer, int depth,
|
|
|
|
const struct sha256_double *blkhash,
|
|
|
|
ptrint_t *canspend)
|
|
|
|
{
|
|
|
|
log_debug(peer->log, "Commit tx reached depth %i", depth);
|
|
|
|
/* FIXME: Handle locktime in blocks, as well as seconds! */
|
|
|
|
|
|
|
|
/* Fell out of a block? */
|
|
|
|
if (depth < 0) {
|
|
|
|
/* Forget any old block. */
|
|
|
|
peer->cur_commit.start_time = 0;
|
|
|
|
memset(&peer->cur_commit.blockid, 0xFF,
|
|
|
|
sizeof(peer->cur_commit.blockid));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In a new block? */
|
|
|
|
if (!structeq(blkhash, &peer->cur_commit.blockid)) {
|
|
|
|
peer->cur_commit.start_time = 0;
|
|
|
|
peer->cur_commit.blockid = *blkhash;
|
|
|
|
bitcoind_get_mediantime(peer->dstate, blkhash,
|
|
|
|
&peer->cur_commit.start_time);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't yet know the median start time? */
|
|
|
|
if (!peer->cur_commit.start_time)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* FIXME: We should really use bitcoin time here. */
|
|
|
|
if (controlled_time().ts.tv_sec > peer->cur_commit.start_time
|
|
|
|
+ rel_locktime_to_seconds(&peer->them.locktime)) {
|
|
|
|
/* Free this watch; we're done */
|
|
|
|
peer->cur_commit.watch = tal_free(peer->cur_commit.watch);
|
|
|
|
state_event(peer, ptr2int(canspend), NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: We tell bitcoind to watch all the outputs, which is overkill */
|
|
|
|
static void watch_tx_outputs(struct peer *peer, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
varint_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) {
|
|
|
|
struct ripemd160 redeemhash;
|
|
|
|
if (!is_p2sh(tx->output[i].script, tx->output[i].script_length))
|
|
|
|
fatal("Unexpected non-p2sh output");
|
|
|
|
memcpy(&redeemhash, tx->output[i].script+2, sizeof(redeemhash));
|
|
|
|
bitcoind_watch_addr(peer->dstate, &redeemhash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Watch the commit tx until our side is spendable. */
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_watch_delayed(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
enum state_input canspend)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
struct sha256_double txid;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* We only ever spend the last one. */
|
|
|
|
assert(tx == peer->us.commit->tx);
|
2016-01-21 21:15:28 +01:00
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
memset(&peer->cur_commit.blockid, 0xFF,
|
|
|
|
sizeof(peer->cur_commit.blockid));
|
|
|
|
peer->cur_commit.watch
|
|
|
|
= add_commit_tx_watch(tx, peer, &txid, commit_tx_depth,
|
|
|
|
int2ptr(canspend));
|
|
|
|
|
|
|
|
watch_tx_outputs(peer, tx);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
static void spend_tx_done(struct peer *peer, int depth,
|
|
|
|
const struct sha256_double *blkhash,
|
|
|
|
ptrint_t *done)
|
|
|
|
{
|
|
|
|
log_debug(peer->log, "tx reached depth %i", depth);
|
|
|
|
if (depth >= (int)peer->dstate->config.forever_confirms)
|
|
|
|
state_event(peer, ptr2int(done), NULL);
|
|
|
|
}
|
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
/* Watch this tx until it's buried enough to be forgotten. */
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_watch_tx(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
enum state_input done)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
struct sha256_double txid;
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
log_debug(peer->log, "Watching tx %02x%02x%02x%02x...",
|
|
|
|
txid.sha.u.u8[0],
|
|
|
|
txid.sha.u.u8[1],
|
|
|
|
txid.sha.u.u8[2],
|
|
|
|
txid.sha.u.u8[3]);
|
|
|
|
|
|
|
|
add_commit_tx_watch(tx, peer, &txid, spend_tx_done, int2ptr(done));
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
|
|
|
struct channel_state cstate;
|
|
|
|
|
|
|
|
/* We don't need a deep copy here, just fee levels. */
|
2016-03-31 08:43:20 +02:00
|
|
|
cstate = *peer->us.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-01-21 21:15:28 +01:00
|
|
|
peer->us.finalkey.der[0], peer->us.finalkey.der[1],
|
|
|
|
peer->us.finalkey.der[2], peer->us.finalkey.der[3],
|
|
|
|
peer->them.finalkey.der[0], peer->them.finalkey.der[1],
|
|
|
|
peer->them.finalkey.der[2], peer->them.finalkey.der[3],
|
|
|
|
cstate.a.pay_msat / 1000,
|
|
|
|
cstate.b.pay_msat / 1000);
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
return create_close_tx(peer->dstate->secpctx, peer,
|
2016-03-24 02:39:41 +01:00
|
|
|
&peer->us.finalkey,
|
|
|
|
&peer->them.finalkey,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
cstate.a.pay_msat / 1000,
|
|
|
|
cstate.b.pay_msat / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-24 02:42:43 +01: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-03-31 08:43:20 +02:00
|
|
|
maxfee = commit_tx_fee(peer->us.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
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_has_close_sig(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->closing.their_sig;
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
static void send_close_timeout(struct peer *peer)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
/* FIXME: Remove any close_tx watches! */
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, INPUT_CLOSE_COMPLETE_TIMEOUT, NULL);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
void peer_watch_close(struct peer *peer,
|
|
|
|
enum state_input done, enum state_input timedout)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
/* We save some work by assuming these. */
|
|
|
|
assert(done == BITCOIN_CLOSE_DONE);
|
|
|
|
|
|
|
|
/* FIXME: We can't send CLOSE, so timeout immediately */
|
2016-01-21 21:15:27 +01:00
|
|
|
if (!peer->conn) {
|
2016-01-21 21:15:28 +01:00
|
|
|
assert(timedout == INPUT_CLOSE_COMPLETE_TIMEOUT);
|
|
|
|
oneshot_timeout(peer->dstate, peer, 0,
|
|
|
|
send_close_timeout, peer);
|
2016-01-21 21:15:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Give them a reasonable time to respond. */
|
|
|
|
/* FIXME: config? */
|
|
|
|
if (timedout != INPUT_NONE) {
|
|
|
|
assert(timedout == INPUT_CLOSE_COMPLETE_TIMEOUT);
|
|
|
|
peer->close_watch_timeout
|
|
|
|
= oneshot_timeout(peer->dstate, peer, 120,
|
|
|
|
send_close_timeout, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* anchor_spent will get called, we match against close_tx there. */
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
void peer_unwatch_close_timeout(struct peer *peer, enum state_input timedout)
|
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
assert(peer->close_watch_timeout);
|
|
|
|
peer->close_watch_timeout = tal_free(peer->close_watch_timeout);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
bool peer_watch_our_htlc_outputs(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
enum state_input tous_timeout,
|
|
|
|
enum state_input tothem_spent,
|
|
|
|
enum state_input tothem_timeout)
|
|
|
|
{
|
2016-03-24 02:39:44 +01:00
|
|
|
if (committed_to_htlcs(peer))
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
return false;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
bool peer_watch_their_htlc_outputs(struct peer *peer,
|
|
|
|
const struct bitcoin_event *tx,
|
|
|
|
enum state_input tous_timeout,
|
|
|
|
enum state_input tothem_spent,
|
|
|
|
enum state_input tothem_timeout)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
void peer_unwatch_htlc_output(struct peer *peer,
|
|
|
|
const struct htlc *htlc,
|
|
|
|
enum state_input all_done)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
void peer_unwatch_all_htlc_outputs(struct peer *peer)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
void peer_watch_htlc_spend(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
const struct htlc *htlc,
|
|
|
|
enum state_input done)
|
|
|
|
{
|
2016-01-21 21:15:27 +01:00
|
|
|
/* FIXME! */
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
void peer_unwatch_htlc_spend(struct peer *peer,
|
|
|
|
const struct htlc *htlc,
|
|
|
|
enum state_input all_done)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An on-chain transaction revealed an R value. */
|
|
|
|
const struct htlc *peer_tx_revealed_r_value(struct peer *peer,
|
|
|
|
const struct bitcoin_event *btc)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* Complete the close_tx, using signatures. */
|
|
|
|
close_tx->input[0].script
|
|
|
|
= scriptsig_p2sh_2of2(close_tx,
|
|
|
|
peer->closing.their_sig,
|
|
|
|
&our_close_sig,
|
|
|
|
&peer->them.commitkey,
|
|
|
|
&peer->us.commitkey);
|
|
|
|
close_tx->input[0].script_length
|
|
|
|
= tal_count(close_tx->input[0].script);
|
|
|
|
|
|
|
|
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-03-24 02:42:43 +01:00
|
|
|
u8 *redeemscript, *linear;
|
2016-03-31 08:43:20 +02:00
|
|
|
const struct bitcoin_tx *commit = peer->us.commit->tx;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct bitcoin_signature sig;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
unsigned int p2sh_out;
|
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-03-30 08:27:18 +02:00
|
|
|
redeemscript = bitcoin_redeem_secret_or_delay(peer,
|
2016-01-21 21:15:28 +01:00
|
|
|
&peer->us.finalkey,
|
2016-01-21 21:14:13 +01:00
|
|
|
&peer->them.locktime,
|
2016-01-21 21:15:28 +01:00
|
|
|
&peer->them.finalkey,
|
2016-03-31 08:43:20 +02:00
|
|
|
&peer->us.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);
|
|
|
|
p2sh_out = find_p2sh_out(commit, redeemscript);
|
|
|
|
tx->input[0].index = p2sh_out;
|
|
|
|
tx->input[0].input_amount = commit->output[p2sh_out].amount;
|
2016-01-21 21:15:28 +01:00
|
|
|
tx->input[0].sequence_number = bitcoin_nsequence(&peer->them.locktime);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
tx->output[0].amount = commit->output[p2sh_out].amount;
|
2016-01-21 21:14:13 +01:00
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx,
|
2016-01-21 21:15:28 +01:00
|
|
|
bitcoin_redeem_single(tx, &peer->us.finalkey));
|
2016-01-21 21:14:13 +01:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Use signature, until we have fee. */
|
2016-01-21 21:14:13 +01:00
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-01-21 21:15:28 +01:00
|
|
|
peer_sign_spend(peer, tx, redeemscript, &sig.sig);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
tx->input[0].script = scriptsig_p2sh_secret(tx, NULL, 0, &sig,
|
|
|
|
redeemscript,
|
|
|
|
tal_count(redeemscript));
|
|
|
|
tx->input[0].script_length = tal_count(tx->input[0].script);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* FIXME: Figure out length first, then calc fee! */
|
|
|
|
|
|
|
|
/* Now, calculate the fee, given length. */
|
|
|
|
/* FIXME: Dynamic fees! */
|
2016-03-30 08:27:18 +02:00
|
|
|
linear = linearize_tx(peer, tx);
|
2016-03-24 02:42:43 +01:00
|
|
|
tx->fee = fee_by_feerate(tal_count(linear),
|
|
|
|
peer->dstate->config.closing_fee_rate);
|
2016-03-30 08:27:18 +02:00
|
|
|
tal_free(linear);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
/* FIXME: Fail gracefully in these cases (not worth collecting) */
|
|
|
|
if (tx->fee > tx->output[0].amount
|
|
|
|
|| is_dust_amount(tx->output[0].amount - tx->fee))
|
|
|
|
fatal("Amount of %"PRIu64" won't cover fee %"PRIu64,
|
|
|
|
tx->output[0].amount, tx->fee);
|
|
|
|
|
|
|
|
/* Re-sign with the real values. */
|
|
|
|
tx->input[0].script_length = 0;
|
|
|
|
tx->output[0].amount -= tx->fee;
|
|
|
|
peer_sign_spend(peer, tx, redeemscript, &sig.sig);
|
|
|
|
|
|
|
|
tx->input[0].script = scriptsig_p2sh_secret(tx, NULL, 0, &sig,
|
|
|
|
redeemscript,
|
|
|
|
tal_count(redeemscript));
|
|
|
|
tx->input[0].script_length = tal_count(tx->input[0].script);
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
return tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a bitcoin spend tx (to spend their commit's outputs) */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_spend_theirs(const struct peer *peer,
|
2016-01-21 21:14:13 +01:00
|
|
|
const struct bitcoin_event *btc)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a bitcoin steal tx (to steal all their commit's outputs) */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_steal(const struct peer *peer,
|
2016-01-21 21:14:13 +01:00
|
|
|
struct bitcoin_event *btc)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Can't be signed already! */
|
2016-03-31 08:43:20 +02:00
|
|
|
assert(peer->us.commit->tx->input[0].script_length == 0);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
sig.stype = SIGHASH_ALL;
|
2016-03-31 08:43:20 +02:00
|
|
|
peer_sign_ourcommit(peer, peer->us.commit->tx, &sig.sig);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
peer->us.commit->tx->input[0].script
|
|
|
|
= scriptsig_p2sh_2of2(peer->us.commit->tx,
|
|
|
|
peer->us.commit->sig,
|
2016-01-21 21:15:28 +01:00
|
|
|
&sig,
|
|
|
|
&peer->them.commitkey,
|
|
|
|
&peer->us.commitkey);
|
2016-03-31 08:43:20 +02:00
|
|
|
peer->us.commit->tx->input[0].script_length
|
|
|
|
= tal_count(peer->us.commit->tx->input[0].script);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
return peer->us.commit->tx;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a HTLC refund collection */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_htlc_timeout(const struct peer *peer,
|
2016-01-21 21:14:13 +01:00
|
|
|
const struct htlc *htlc)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a HTLC collection */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_htlc_spend(const struct peer *peer,
|
2016-01-21 21:14:13 +01:00
|
|
|
const struct htlc *htlc)
|
|
|
|
{
|
|
|
|
FIXME_STUB(peer);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:27 +01:00
|
|
|
static void created_anchor(struct lightningd_state *dstate,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
struct peer *peer)
|
|
|
|
{
|
|
|
|
bitcoin_txid(tx, &peer->anchor.txid);
|
|
|
|
peer->anchor.index = find_p2sh_out(tx, peer->anchor.redeemscript);
|
|
|
|
assert(peer->anchor.satoshis == tx->output[peer->anchor.index].amount);
|
|
|
|
/* We'll need this later, when we're told to broadcast it. */
|
|
|
|
peer->anchor.tx = tal_steal(peer, tx);
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
state_event(peer, BITCOIN_ANCHOR_CREATED, NULL);
|
2016-01-21 21:14:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
/* Start creation of the bitcoin anchor tx. */
|
|
|
|
void bitcoin_create_anchor(struct peer *peer, enum state_input done)
|
|
|
|
{
|
2016-01-21 21:14:27 +01:00
|
|
|
struct sha256 h;
|
|
|
|
struct ripemd160 redeemhash;
|
|
|
|
char *p2shaddr;
|
|
|
|
|
|
|
|
/* We must be offering anchor for us to try creating it */
|
|
|
|
assert(peer->us.offer_anchor);
|
|
|
|
|
|
|
|
sha256(&h, peer->anchor.redeemscript,
|
|
|
|
tal_count(peer->anchor.redeemscript));
|
|
|
|
ripemd160(&redeemhash, h.u.u8, sizeof(h));
|
|
|
|
|
|
|
|
p2shaddr = p2sh_to_base58(peer, peer->dstate->config.testnet,
|
|
|
|
&redeemhash);
|
|
|
|
|
|
|
|
assert(done == BITCOIN_ANCHOR_CREATED);
|
|
|
|
|
|
|
|
bitcoind_create_payment(peer->dstate, p2shaddr, peer->anchor.satoshis,
|
|
|
|
created_anchor, 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-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)
|
|
|
|
{
|
|
|
|
assert(!peer->us.commit->tx);
|
|
|
|
assert(!peer->them.commit->tx);
|
|
|
|
|
|
|
|
/* Revocation hashes already filled in, from pkt_open */
|
|
|
|
peer->us.commit->cstate = initial_funding(peer,
|
|
|
|
peer->us.offer_anchor
|
|
|
|
== CMD_OPEN_WITH_ANCHOR,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
peer->us.commit_fee_rate);
|
|
|
|
if (!peer->us.commit->cstate)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
peer->them.commit->cstate = initial_funding(peer,
|
|
|
|
peer->them.offer_anchor
|
|
|
|
== CMD_OPEN_WITH_ANCHOR,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
peer->them.commit_fee_rate);
|
|
|
|
if (!peer->them.commit->cstate)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
peer->us.commit->tx = create_commit_tx(peer->us.commit,
|
|
|
|
&peer->us.finalkey,
|
|
|
|
&peer->them.finalkey,
|
|
|
|
&peer->them.locktime,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&peer->us.commit->revocation_hash,
|
|
|
|
peer->us.commit->cstate);
|
|
|
|
|
|
|
|
peer->them.commit->tx = create_commit_tx(peer->them.commit,
|
|
|
|
&peer->them.finalkey,
|
|
|
|
&peer->us.finalkey,
|
|
|
|
&peer->us.locktime,
|
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&peer->them.commit->revocation_hash,
|
|
|
|
peer->them.commit->cstate);
|
|
|
|
|
|
|
|
peer->us.staging_cstate = copy_funding(peer, peer->us.commit->cstate);
|
|
|
|
peer->them.staging_cstate = copy_funding(peer, peer->them.commit->cstate);
|
|
|
|
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
|
|
|
/* This is money we can count on. */
|
|
|
|
static const struct channel_state *last_signed_state(const struct commit_info *i)
|
|
|
|
{
|
|
|
|
while (i) {
|
|
|
|
if (i->sig)
|
|
|
|
return i->cstate;
|
|
|
|
i = i->prev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Report anchor. */
|
|
|
|
|
|
|
|
last = last_signed_state(p->us.commit);
|
|
|
|
if (!last) {
|
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-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
json_add_num(response, "our_amount", last->a.pay_msat);
|
|
|
|
json_add_num(response, "our_fee", last->a.fee_msat);
|
|
|
|
json_add_num(response, "their_amount", last->b.pay_msat);
|
|
|
|
json_add_num(response, "their_fee", last->b.fee_msat);
|
|
|
|
json_add_htlcs(response, "our_htlcs", &last->a);
|
|
|
|
json_add_htlcs(response, "their_htlcs", &last->b);
|
|
|
|
|
|
|
|
/* Any changes since then? */
|
|
|
|
if (p->us.staging_cstate->changes != last->changes)
|
|
|
|
json_add_num(response, "staged_changes",
|
|
|
|
p->us.staging_cstate->changes
|
|
|
|
- last->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. */
|
|
|
|
for (i = 0; i < tal_count(peer->them.staging_cstate->a.htlcs); i++) {
|
|
|
|
struct channel_htlc *htlc = &peer->them.staging_cstate->a.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)
|
|
|
|
{
|
|
|
|
time_t when;
|
|
|
|
|
|
|
|
/* Add 30 seconds to be sure peers agree on timeout. */
|
|
|
|
when = abs_locktime_to_seconds(expiry) - controlled_time().ts.tv_sec;
|
|
|
|
when += 30;
|
|
|
|
|
|
|
|
oneshot_timeout(peer->dstate, peer, when, htlc_expiry_timeout, peer);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* offering more than 1500 HTLCs in either commitment transaction.
|
|
|
|
*/
|
|
|
|
if (tal_count(peer->us.staging_cstate->a.htlcs) == 1500
|
|
|
|
|| tal_count(peer->them.staging_cstate->b.htlcs) == 1500) {
|
|
|
|
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`
|
|
|
|
*/
|
|
|
|
cstate = copy_funding(newhtlc, peer->them.staging_cstate);
|
|
|
|
if (!funding_b_add_htlc(cstate, newhtlc->htlc.msatoshis,
|
|
|
|
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
|
|
|
|
newhtlc->htlc.id)) {
|
|
|
|
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-03-31 08:43:20 +02:00
|
|
|
cstate = copy_funding(newhtlc, peer->us.staging_cstate);
|
2016-03-24 02:42:43 +01:00
|
|
|
if (!funding_a_add_htlc(cstate, newhtlc->htlc.msatoshis,
|
2016-03-31 08:43:20 +02:00
|
|
|
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
|
|
|
|
newhtlc->htlc.id)) {
|
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-03-31 08:43:20 +02:00
|
|
|
if (!peer->them.commit || !peer->them.commit->cstate) {
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
queue_cmd(peer, do_newhtlc, newhtlc);
|
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-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-03-31 08:43:20 +02:00
|
|
|
i = funding_find_htlc(&peer->them.staging_cstate->a, &rhash);
|
|
|
|
if (i == tal_count(peer->them.staging_cstate->a.htlcs)) {
|
|
|
|
command_fail(fulfillhtlc->jsoncmd, "preimage htlc not found");
|
2016-01-21 21:15:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-03-31 08:43:20 +02:00
|
|
|
stage.fulfill.id = peer->them.staging_cstate->a.htlcs[i].id;
|
|
|
|
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-03-31 08:43:20 +02:00
|
|
|
if (!peer->them.commit || !peer->them.commit->cstate) {
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
queue_cmd(peer, do_fullfill, fulfillhtlc);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-31 08:43:20 +02:00
|
|
|
/* Look in peer->them.staging_cstate->a, as that's where we'll
|
|
|
|
* immediately remove it from: avoids double-handling. */
|
|
|
|
/* FIXME: Make sure it's also committed in previous commit tx! */
|
|
|
|
i = funding_find_htlc(&peer->them.staging_cstate->a, &failhtlc->rhash);
|
|
|
|
if (i == tal_count(peer->them.staging_cstate->a.htlcs)) {
|
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-03-31 08:43:20 +02:00
|
|
|
stage.fail.id = peer->them.staging_cstate->a.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-03-31 08:43:20 +02:00
|
|
|
if (!peer->them.commit || !peer->them.commit->cstate) {
|
|
|
|
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-01-21 21:15:28 +01:00
|
|
|
queue_cmd(peer, do_failhtlc, failhtlc);
|
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 do_commit(struct peer *peer, struct command *jsoncmd)
|
|
|
|
{
|
|
|
|
/* We can have changes we suggested, or changes they suggested. */
|
|
|
|
if (peer->them.staging_cstate->changes == peer->them.commit->cstate->changes) {
|
|
|
|
command_fail(jsoncmd, "no changes to commit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_command(peer, CMD_SEND_COMMIT, NULL, jsoncmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->them.commit || !peer->them.commit->cstate) {
|
|
|
|
command_fail(cmd, "peer not fully established");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_cmd(peer, do_commit, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
};
|