2016-01-21 21:14:27 +01:00
|
|
|
#include "bitcoin/script.h"
|
2016-01-21 21:15:05 +01:00
|
|
|
#include "bitcoin/tx.h"
|
2016-01-21 21:15:28 +01:00
|
|
|
#include "close_tx.h"
|
2016-03-31 08:43:20 +02:00
|
|
|
#include "commit_tx.h"
|
2016-01-21 21:15:28 +01:00
|
|
|
#include "controlled_time.h"
|
2016-04-11 09:01:43 +02:00
|
|
|
#include "cryptopkt.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "names.h"
|
|
|
|
#include "peer.h"
|
|
|
|
#include "protobuf_convert.h"
|
|
|
|
#include "secrets.h"
|
|
|
|
#include "state.h"
|
2016-05-03 03:29:20 +02:00
|
|
|
#include "utils.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
2016-03-30 08:27:18 +02:00
|
|
|
#include <ccan/io/io.h>
|
2016-01-21 21:14:13 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-03-31 08:43:20 +02:00
|
|
|
#include <ccan/ptrint/ptrint.h>
|
2016-01-21 21:15:05 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2016-01-21 21:15:27 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2016-01-21 21:15:28 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2016-01-21 21:15:27 +01:00
|
|
|
#include <inttypes.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:15:05 +01:00
|
|
|
static void dump_tx(const char *str, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
u8 *linear = linearize_tx(NULL, tx);
|
2016-05-03 03:29:20 +02:00
|
|
|
printf("%s:%s\n", str, tal_hexstr(linear, linear, tal_count(linear)));
|
2016-01-21 21:15:05 +01:00
|
|
|
tal_free(linear);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_key(const char *str, const struct pubkey *key)
|
|
|
|
{
|
2016-05-03 03:29:20 +02:00
|
|
|
printf("%s:%s\n", str, tal_hexstr(NULL, key->der, sizeof(key->der)));
|
2016-01-21 21:15:05 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
/* Wrap (and own!) member inside Pkt */
|
|
|
|
static Pkt *make_pkt(const tal_t *ctx, Pkt__PktCase type, const void *msg)
|
|
|
|
{
|
|
|
|
Pkt *pkt = tal(ctx, Pkt);
|
|
|
|
|
|
|
|
pkt__init(pkt);
|
|
|
|
pkt->pkt_case = type;
|
|
|
|
/* This is a union, so doesn't matter which we assign. */
|
2016-03-30 08:27:18 +02:00
|
|
|
pkt->error = (Error *)tal_steal(pkt, msg);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
|
|
|
/* This makes sure all packets are valid. */
|
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
u8 *packed;
|
|
|
|
Pkt *cpy;
|
|
|
|
|
|
|
|
len = pkt__get_packed_size(pkt);
|
|
|
|
packed = tal_arr(pkt, u8, len);
|
|
|
|
pkt__pack(pkt, packed);
|
|
|
|
cpy = pkt__unpack(NULL, len, memcheck(packed, len));
|
|
|
|
assert(cpy);
|
|
|
|
pkt__free_unpacked(cpy, NULL);
|
|
|
|
tal_free(packed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
static void queue_raw_pkt(struct peer *peer, Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
size_t n = tal_count(peer->outpkt);
|
|
|
|
tal_resize(&peer->outpkt, n+1);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->outpkt[n] = pkt;
|
2016-03-30 08:27:18 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
log_debug(peer->log, "Queued pkt %s", pkt_name(pkt->pkt_case));
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
/* In case it was waiting for output. */
|
|
|
|
io_wake(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_pkt(struct peer *peer, Pkt__PktCase type, const void *msg)
|
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_raw_pkt(peer, make_pkt(peer, type, msg));
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
static struct commit_info *new_commit_info(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct commit_info *ci = talz(ctx, struct commit_info);
|
|
|
|
ci->unacked_changes = tal_arr(ci, union htlc_staging, 0);
|
|
|
|
return ci;
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor)
|
|
|
|
{
|
|
|
|
OpenChannel *o = tal(peer, OpenChannel);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Set up out commit info now: rest gets done in setup_first_commit
|
|
|
|
* once anchor is established. */
|
2016-05-26 07:55:25 +02:00
|
|
|
peer->local.commit = new_commit_info(peer);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.commit->revocation_hash = peer->local.next_revocation_hash;
|
|
|
|
peer_get_revocation_hash(peer, 1, &peer->local.next_revocation_hash);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
open_channel__init(o);
|
2016-05-26 07:55:24 +02:00
|
|
|
o->revocation_hash = sha256_to_proto(o, &peer->local.commit->revocation_hash);
|
|
|
|
o->next_revocation_hash = sha256_to_proto(o, &peer->local.next_revocation_hash);
|
|
|
|
o->commit_key = pubkey_to_proto(o, &peer->local.commitkey);
|
|
|
|
o->final_key = pubkey_to_proto(o, &peer->local.finalkey);
|
2016-01-21 21:14:13 +01:00
|
|
|
o->delay = tal(o, Locktime);
|
|
|
|
locktime__init(o->delay);
|
|
|
|
o->delay->locktime_case = LOCKTIME__LOCKTIME_SECONDS;
|
2016-05-26 07:55:24 +02:00
|
|
|
o->delay->seconds = rel_locktime_to_seconds(&peer->local.locktime);
|
|
|
|
o->initial_fee_rate = peer->local.commit_fee_rate;
|
2016-01-21 21:14:13 +01:00
|
|
|
if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR);
|
2016-01-21 21:14:13 +01:00
|
|
|
else {
|
|
|
|
assert(anchor == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR);
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
o->anch = anchor;
|
2016-05-26 07:55:24 +02:00
|
|
|
o->min_depth = peer->local.mindepth;
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_OPEN, o);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-03-30 08:27:18 +02:00
|
|
|
|
|
|
|
void queue_pkt_anchor(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
OpenAnchor *a = tal(peer, OpenAnchor);
|
2016-01-21 21:14:27 +01:00
|
|
|
|
|
|
|
open_anchor__init(a);
|
|
|
|
a->txid = sha256_to_proto(a, &peer->anchor.txid.sha);
|
|
|
|
a->output_index = peer->anchor.index;
|
|
|
|
a->amount = peer->anchor.satoshis;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* This shouldn't happen! */
|
|
|
|
if (!setup_first_commit(peer)) {
|
|
|
|
queue_pkt_err(peer,
|
|
|
|
pkt_err(peer,
|
|
|
|
"Own anchor has insufficient funds"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_OPEN_ANCHOR, a);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_open_commit_sig(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
OpenCommitSig *s = tal(peer, OpenCommitSig);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
|
|
|
open_commit_sig__init(s);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
dump_tx("Creating sig for:", peer->remote.commit->tx);
|
|
|
|
dump_key("Using key:", &peer->local.commitkey);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.commit->sig = tal(peer->remote.commit,
|
2016-05-04 08:43:50 +02:00
|
|
|
struct bitcoin_signature);
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.commit->sig->stype = SIGHASH_ALL;
|
|
|
|
peer_sign_theircommit(peer, peer->remote.commit->tx,
|
|
|
|
&peer->remote.commit->sig->sig);
|
|
|
|
s->sig = signature_to_proto(s, &peer->remote.commit->sig->sig);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_open_complete(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
OpenComplete *o = tal(peer, OpenComplete);
|
2016-01-21 21:15:05 +01:00
|
|
|
|
|
|
|
open_complete__init(o);
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_OPEN_COMPLETE, o);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
void queue_pkt_htlc_add(struct peer *peer, const struct channel_htlc *htlc)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateAddHtlc *u = tal(peer, UpdateAddHtlc);
|
2016-06-28 23:19:20 +02:00
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
|
|
|
update_add_htlc__init(u);
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
u->id = htlc->id;
|
|
|
|
u->amount_msat = htlc->msatoshis;
|
|
|
|
u->r_hash = sha256_to_proto(u, &htlc->rhash);
|
|
|
|
u->expiry = abs_locktime_to_proto(u, &htlc->expiry);
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: routing! */
|
|
|
|
u->route = tal(u, Routing);
|
|
|
|
routing__init(u->route);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC addition to the unacked
|
|
|
|
* changeset for its remote commitment
|
|
|
|
*/
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!funding_add_htlc(peer->remote.staging_cstate,
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc->msatoshis,
|
|
|
|
&htlc->expiry,
|
|
|
|
&htlc->rhash,
|
|
|
|
htlc->id, OURS))
|
2016-03-31 08:43:20 +02:00
|
|
|
fatal("Could not add HTLC?");
|
2016-06-28 23:19:20 +02:00
|
|
|
|
|
|
|
stage.add.add = HTLC_ADD;
|
|
|
|
stage.add.htlc = *htlc;
|
|
|
|
add_unacked(&peer->remote, &stage);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
remote_changes_pending(peer);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
peer_add_htlc_expiry(peer, &htlc->expiry);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_ADD_HTLC, u);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc);
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-05-26 07:55:24 +02:00
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
update_fulfill_htlc__init(f);
|
2016-05-26 07:55:24 +02:00
|
|
|
f->id = id;
|
2016-06-28 23:19:20 +02:00
|
|
|
f->r = rval_to_proto(f, r);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC fulfill/fail to the
|
|
|
|
* unacked changeset for its remote commitment
|
|
|
|
*/
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
|
|
|
|
assert(htlc);
|
|
|
|
funding_fulfill_htlc(peer->remote.staging_cstate, htlc, THEIRS);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
stage.fulfill.fulfill = HTLC_FULFILL;
|
|
|
|
stage.fulfill.id = f->id;
|
|
|
|
stage.fulfill.r = *r;
|
|
|
|
add_unacked(&peer->remote, &stage);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
remote_changes_pending(peer);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
void queue_pkt_htlc_fail(struct peer *peer, u64 id)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateFailHtlc *f = tal(peer, UpdateFailHtlc);
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-05-26 07:55:24 +02:00
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-08 01:11:15 +01:00
|
|
|
update_fail_htlc__init(f);
|
2016-05-26 07:55:24 +02:00
|
|
|
f->id = id;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: reason! */
|
|
|
|
f->reason = tal(f, FailReason);
|
|
|
|
fail_reason__init(f->reason);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The sending node MUST add the HTLC fulfill/fail to the
|
|
|
|
* unacked changeset for its remote commitment
|
|
|
|
*/
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
|
|
|
|
assert(htlc);
|
|
|
|
funding_fail_htlc(peer->remote.staging_cstate, htlc, THEIRS);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
stage.fail.fail = HTLC_FAIL;
|
|
|
|
stage.fail.id = f->id;
|
|
|
|
add_unacked(&peer->remote, &stage);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
remote_changes_pending(peer);
|
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_FAIL_HTLC, f);
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, we're sending a signature for their pending changes. */
|
|
|
|
void queue_pkt_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
UpdateCommit *u = tal(peer, UpdateCommit);
|
2016-05-26 07:55:25 +02:00
|
|
|
struct commit_info *ci = new_commit_info(peer);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* Create new commit info for this commit tx. */
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->prev = peer->remote.commit;
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->commit_num = ci->prev->commit_num + 1;
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->revocation_hash = peer->remote.next_revocation_hash;
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A sending node MUST apply all remote acked and unacked
|
|
|
|
* changes except unacked fee changes to the remote commitment
|
|
|
|
* before generating `sig`. */
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->cstate = copy_funding(ci, peer->remote.staging_cstate);
|
2016-03-31 08:43:20 +02:00
|
|
|
ci->tx = create_commit_tx(ci,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.finalkey,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.locktime,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.locktime,
|
2016-03-31 08:43:20 +02:00
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&ci->revocation_hash,
|
2016-05-02 08:35:56 +02:00
|
|
|
ci->cstate,
|
2016-05-26 07:55:24 +02:00
|
|
|
THEIRS,
|
2016-05-02 08:35:56 +02:00
|
|
|
&ci->map);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
log_debug(peer->log, "Signing tx for %u/%u msatoshis, %zu/%zu htlcs",
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->cstate->side[OURS].pay_msat,
|
|
|
|
ci->cstate->side[THEIRS].pay_msat,
|
|
|
|
tal_count(ci->cstate->side[OURS].htlcs),
|
|
|
|
tal_count(ci->cstate->side[THEIRS].htlcs));
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send an `update_commit` message which does
|
|
|
|
* not include any updates.
|
|
|
|
*/
|
|
|
|
assert(ci->prev->cstate->changes != ci->cstate->changes);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-05-04 08:43:50 +02:00
|
|
|
ci->sig = tal(ci, struct bitcoin_signature);
|
|
|
|
ci->sig->stype = SIGHASH_ALL;
|
|
|
|
peer_sign_theircommit(peer, ci->tx, &ci->sig->sig);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Switch to the new commitment. */
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.commit = ci;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Now send message */
|
|
|
|
update_commit__init(u);
|
2016-05-04 08:43:50 +02:00
|
|
|
u->sig = signature_to_proto(u, &ci->sig->sig);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_COMMIT, u);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* At revocation time, we apply the changeset to the other side. */
|
|
|
|
static void apply_changeset(struct peer *peer,
|
|
|
|
struct peer_visible_state *which,
|
2016-05-26 07:55:24 +02:00
|
|
|
enum channel_side side,
|
2016-05-26 07:55:24 +02:00
|
|
|
const union htlc_staging *changes,
|
|
|
|
size_t num_changes)
|
|
|
|
{
|
|
|
|
size_t i;
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < num_changes; i++) {
|
|
|
|
switch (changes[i].type) {
|
|
|
|
case HTLC_ADD:
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(which->staging_cstate,
|
|
|
|
changes[i].add.htlc.id, side);
|
|
|
|
if (htlc)
|
2016-05-26 07:55:24 +02:00
|
|
|
fatal("Can't add duplicate HTLC id %"PRIu64,
|
|
|
|
changes[i].add.htlc.id);
|
|
|
|
if (!funding_add_htlc(which->staging_cstate,
|
|
|
|
changes[i].add.htlc.msatoshis,
|
|
|
|
&changes[i].add.htlc.expiry,
|
|
|
|
&changes[i].add.htlc.rhash,
|
2016-05-26 07:55:24 +02:00
|
|
|
changes[i].add.htlc.id, side))
|
|
|
|
fatal("Adding HTLC to %s failed",
|
|
|
|
side == OURS ? "ours" : "theirs");
|
2016-05-26 07:55:24 +02:00
|
|
|
continue;
|
|
|
|
case HTLC_FAIL:
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(which->staging_cstate,
|
2016-05-26 07:55:24 +02:00
|
|
|
changes[i].fail.id, !side);
|
2016-06-28 23:19:20 +02:00
|
|
|
if (!htlc)
|
2016-05-26 07:55:24 +02:00
|
|
|
fatal("Can't fail non-exisent HTLC id %"PRIu64,
|
|
|
|
changes[i].fail.id);
|
2016-06-28 23:19:20 +02:00
|
|
|
funding_fail_htlc(which->staging_cstate, htlc, !side);
|
2016-05-26 07:55:24 +02:00
|
|
|
continue;
|
|
|
|
case HTLC_FULFILL:
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(which->staging_cstate,
|
|
|
|
changes[i].fulfill.id, !side);
|
|
|
|
if (!htlc)
|
2016-05-26 07:55:24 +02:00
|
|
|
fatal("Can't fulfill non-exisent HTLC id %"PRIu64,
|
|
|
|
changes[i].fulfill.id);
|
2016-06-28 23:19:20 +02:00
|
|
|
funding_fulfill_htlc(which->staging_cstate, htlc, !side);
|
2016-05-26 07:55:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Send a preimage for the old commit tx. The one we've just committed to is
|
2016-05-26 07:55:24 +02:00
|
|
|
* in peer->local.commit. */
|
2016-03-31 08:43:20 +02:00
|
|
|
void queue_pkt_revocation(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
UpdateRevocation *u = tal(peer, UpdateRevocation);
|
2016-05-26 07:55:25 +02:00
|
|
|
struct commit_info *ci;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
update_revocation__init(u);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.commit);
|
2016-05-26 07:55:25 +02:00
|
|
|
ci = peer->local.commit->prev;
|
|
|
|
assert(ci);
|
|
|
|
assert(!ci->revocation_preimage);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* We have their signature on the current one, right? */
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.commit->sig);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
ci->revocation_preimage = tal(ci, struct sha256);
|
|
|
|
peer_get_revocation_preimage(peer, ci->commit_num,
|
|
|
|
ci->revocation_preimage);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
u->revocation_preimage = sha256_to_proto(u, ci->revocation_preimage);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
u->next_revocation_hash = sha256_to_proto(u,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.next_revocation_hash);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_REVOCATION, u);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The node sending `update_revocation` MUST add the local unacked
|
|
|
|
* changes to the set of remote acked changes.
|
|
|
|
*/
|
2016-05-26 07:55:25 +02:00
|
|
|
/* Note: this means the unacked changes as of the commit we're
|
|
|
|
* revoking */
|
2016-05-26 07:55:24 +02:00
|
|
|
apply_changeset(peer, &peer->remote, THEIRS,
|
2016-05-26 07:55:25 +02:00
|
|
|
ci->unacked_changes, tal_count(ci->unacked_changes));
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
if (tal_count(ci->unacked_changes))
|
2016-05-26 07:55:24 +02:00
|
|
|
remote_changes_pending(peer);
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* We should never look at this again. */
|
|
|
|
ci->unacked_changes = tal_free(ci->unacked_changes);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *pkt_err(struct peer *peer, const char *msg, ...)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
Error *e = tal(peer, Error);
|
2016-01-21 21:15:28 +01:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
error__init(e);
|
|
|
|
va_start(ap, msg);
|
2016-03-30 08:27:18 +02:00
|
|
|
e->problem = tal_vfmt(e, msg, ap);
|
2016-01-21 21:15:28 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
return make_pkt(peer, PKT__PKT_ERROR, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void queue_pkt_err(struct peer *peer, Pkt *err)
|
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_raw_pkt(peer, err);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_close_clearing(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-04-24 12:31:52 +02:00
|
|
|
u8 *redeemscript;
|
2016-03-30 08:27:18 +02:00
|
|
|
CloseClearing *c = tal(peer, CloseClearing);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
close_clearing__init(c);
|
2016-05-26 07:55:24 +02:00
|
|
|
redeemscript = bitcoin_redeem_single(c, &peer->local.finalkey);
|
2016-04-24 12:31:52 +02:00
|
|
|
peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript);
|
|
|
|
|
|
|
|
c->scriptpubkey.data = tal_dup_arr(c, u8,
|
|
|
|
peer->closing.our_script,
|
|
|
|
tal_count(peer->closing.our_script),
|
|
|
|
0);
|
|
|
|
c->scriptpubkey.len = tal_count(c->scriptpubkey.data);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_CLOSE_CLEARING, c);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_close_signature(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
CloseSignature *c = tal(peer, CloseSignature);
|
2016-03-24 02:39:41 +01:00
|
|
|
struct bitcoin_tx *close_tx;
|
|
|
|
struct signature our_close_sig;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
close_signature__init(c);
|
2016-03-30 08:27:18 +02:00
|
|
|
close_tx = peer_create_close_tx(peer, peer->closing.our_fee);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
peer_sign_mutual_close(peer, close_tx, &our_close_sig);
|
|
|
|
c->sig = signature_to_proto(c, &our_close_sig);
|
|
|
|
c->close_fee = peer->closing.our_fee;
|
2016-04-11 09:00:43 +02:00
|
|
|
log_info(peer->log, "queue_pkt_close_signature: offered close fee %"
|
|
|
|
PRIu64, c->close_fee);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_CLOSE_SIGNATURE, c);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *pkt_err_unexpected(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
return pkt_err(peer, "Unexpected packet %s", pkt_name(pkt->pkt_case));
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process various packets: return an error packet on failure. */
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
|
|
|
struct rel_locktime locktime;
|
2016-01-21 21:14:27 +01:00
|
|
|
const OpenChannel *o = pkt->open;
|
2016-01-21 21:14:13 +01:00
|
|
|
|
|
|
|
if (!proto_to_rel_locktime(o->delay, &locktime))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Invalid delay");
|
2016-01-21 21:14:13 +01:00
|
|
|
/* FIXME: handle blocks in locktime */
|
|
|
|
if (o->delay->locktime_case != LOCKTIME__LOCKTIME_SECONDS)
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Delay in blocks not accepted");
|
2016-01-21 21:14:13 +01:00
|
|
|
if (o->delay->seconds > peer->dstate->config.rel_locktime_max)
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Delay too great");
|
2016-01-21 21:14:13 +01:00
|
|
|
if (o->min_depth > peer->dstate->config.anchor_confirms_max)
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "min_depth too great");
|
2016-03-24 02:42:43 +01:00
|
|
|
if (o->initial_fee_rate < peer->dstate->config.commitment_fee_rate_min)
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Commitment fee rate too low");
|
2016-01-21 21:14:13 +01:00
|
|
|
if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.offer_anchor = CMD_OPEN_WITH_ANCHOR;
|
2016-01-21 21:14:13 +01:00
|
|
|
else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
|
2016-01-21 21:14:13 +01:00
|
|
|
else
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Unknown offer anchor value");
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (peer->remote.offer_anchor == peer->local.offer_anchor)
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Only one side can offer anchor");
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!proto_to_rel_locktime(o->delay, &peer->remote.locktime))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Malformed locktime");
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->remote.mindepth = o->min_depth;
|
|
|
|
peer->remote.commit_fee_rate = o->initial_fee_rate;
|
2016-01-21 21:14:13 +01:00
|
|
|
if (!proto_to_pubkey(peer->dstate->secpctx,
|
2016-05-26 07:55:24 +02:00
|
|
|
o->commit_key, &peer->remote.commitkey))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Bad commitkey");
|
2016-01-21 21:14:13 +01:00
|
|
|
if (!proto_to_pubkey(peer->dstate->secpctx,
|
2016-05-26 07:55:24 +02:00
|
|
|
o->final_key, &peer->remote.finalkey))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Bad finalkey");
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* Set up their commit info now: rest gets done in setup_first_commit
|
|
|
|
* once anchor is established. */
|
2016-05-26 07:55:25 +02:00
|
|
|
peer->remote.commit = new_commit_info(peer);
|
2016-05-26 07:55:24 +02:00
|
|
|
proto_to_sha256(o->revocation_hash, &peer->remote.commit->revocation_hash);
|
2016-03-31 08:43:20 +02:00
|
|
|
proto_to_sha256(o->next_revocation_hash,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.next_revocation_hash);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-04-24 12:25:35 +02:00
|
|
|
/* Witness script for anchor. */
|
|
|
|
peer->anchor.witnessscript
|
2016-05-26 07:55:24 +02:00
|
|
|
= bitcoin_redeem_2of2(peer, &peer->local.commitkey,
|
|
|
|
&peer->remote.commitkey);
|
2016-01-21 21:14:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Save and check signature. */
|
|
|
|
static Pkt *check_and_save_commit_sig(struct peer *peer,
|
|
|
|
struct commit_info *ci,
|
|
|
|
const Signature *pb)
|
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
struct bitcoin_signature *sig = tal(ci, struct bitcoin_signature);
|
|
|
|
|
2016-05-04 08:43:50 +02:00
|
|
|
assert(!ci->sig);
|
2016-06-28 23:19:20 +02:00
|
|
|
sig->stype = SIGHASH_ALL;
|
|
|
|
if (!proto_to_signature(pb, &sig->sig))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Malformed signature");
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
log_debug(peer->log, "Checking sig for %u/%u msatoshis, %zu/%zu htlcs",
|
|
|
|
ci->cstate->side[OURS].pay_msat,
|
|
|
|
ci->cstate->side[THEIRS].pay_msat,
|
|
|
|
tal_count(ci->cstate->side[OURS].htlcs),
|
|
|
|
tal_count(ci->cstate->side[THEIRS].htlcs));
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Their sig should sign our commit tx. */
|
|
|
|
if (!check_tx_sig(peer->dstate->secpctx,
|
|
|
|
ci->tx, 0,
|
2016-04-24 12:25:35 +02:00
|
|
|
NULL, 0,
|
|
|
|
peer->anchor.witnessscript,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.commitkey,
|
2016-06-28 23:19:20 +02:00
|
|
|
sig))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Bad signature");
|
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
ci->sig = sig;
|
2016-03-31 08:43:20 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:14:27 +01:00
|
|
|
const OpenAnchor *a = pkt->open_anchor;
|
|
|
|
|
|
|
|
/* They must be offering anchor for us to try accepting */
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
|
|
|
|
assert(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR);
|
2016-01-21 21:14:27 +01:00
|
|
|
|
|
|
|
proto_to_sha256(a->txid, &peer->anchor.txid.sha);
|
|
|
|
peer->anchor.index = a->output_index;
|
|
|
|
peer->anchor.satoshis = a->amount;
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
if (!setup_first_commit(peer))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Insufficient funds for fee");
|
2016-01-21 21:14:27 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:15:05 +01:00
|
|
|
const OpenCommitSig *s = pkt->open_commit_sig;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
return check_and_save_commit_sig(peer, peer->local.commit, s->sig);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt)
|
2016-03-08 01:09:15 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/*
|
|
|
|
* We add changes to both our staging cstate (as they did when they sent
|
|
|
|
* it) and theirs (as they will when we ack it).
|
|
|
|
*/
|
|
|
|
Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
const UpdateAddHtlc *u = pkt->update_add_htlc;
|
|
|
|
struct sha256 rhash;
|
|
|
|
struct abs_locktime expiry;
|
2016-05-26 07:55:24 +02:00
|
|
|
struct channel_htlc *htlc;
|
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* `amount_msat` MUST BE greater than 0.
|
|
|
|
*/
|
|
|
|
if (u->amount_msat == 0)
|
|
|
|
return pkt_err(peer, "Invalid amount_msat");
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
proto_to_sha256(u->r_hash, &rhash);
|
|
|
|
if (!proto_to_abs_locktime(u->expiry, &expiry))
|
|
|
|
return pkt_err(peer, "Invalid HTLC expiry");
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Handle block-based expiry! */
|
|
|
|
if (!abs_locktime_is_seconds(&expiry))
|
|
|
|
return pkt_err(peer, "HTLC expiry in blocks not supported!");
|
2016-01-21 21:15:27 +01:00
|
|
|
|
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->remote.staging_cstate->side[THEIRS].htlcs) == 300
|
2016-05-26 07:55:24 +02:00
|
|
|
|| tal_count(peer->local.staging_cstate->side[THEIRS].htlcs) == 300)
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Too many HTLCs");
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT set `id` equal to another HTLC which is in
|
2016-06-25 06:38:47 +02:00
|
|
|
* any unrevoked commitment transaction.
|
2016-03-31 08:43:20 +02:00
|
|
|
*/
|
2016-06-25 06:38:47 +02:00
|
|
|
/* Note that it's not *our* problem if they do this, it's
|
|
|
|
* theirs (future confusion). Nonetheless, we detect and
|
|
|
|
* error for them. */
|
2016-06-28 23:19:20 +02:00
|
|
|
if (funding_htlc_by_id(peer->remote.staging_cstate, u->id, THEIRS)
|
|
|
|
|| funding_htlc_by_id(peer->remote.commit->cstate, u->id, THEIRS)) {
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
|
2016-06-25 06:38:47 +02:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
if (funding_htlc_by_id(peer->local.staging_cstate, u->id, THEIRS)
|
|
|
|
|| funding_htlc_by_id(peer->local.commit->cstate, u->id, THEIRS)) {
|
2016-06-25 06:38:47 +02:00
|
|
|
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ...and the receiving node MUST add the HTLC addition to the
|
|
|
|
* unacked changeset for its local commitment. */
|
|
|
|
htlc = funding_add_htlc(peer->local.staging_cstate,
|
|
|
|
u->amount_msat, &expiry, &rhash, u->id, THEIRS);
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT offer `amount_msat` it cannot pay for in
|
|
|
|
* both commitment transactions at the current `fee_rate` (see
|
|
|
|
* "Fee Calculation" ). A node SHOULD fail the connection if
|
|
|
|
* this occurs.
|
|
|
|
*/
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: This is wrong! We may have already added more txs to
|
|
|
|
* them.staging_cstate, driving that fee up.
|
|
|
|
* We should check against the last version they acknowledged. */
|
2016-05-26 07:55:24 +02:00
|
|
|
if (!htlc)
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis"
|
|
|
|
" in your commitment tx",
|
|
|
|
u->amount_msat);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
stage.add.add = HTLC_ADD;
|
|
|
|
stage.add.htlc = *htlc;
|
|
|
|
add_unacked(&peer->local, &stage);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
peer_add_htlc_expiry(peer, &expiry);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
|
|
|
|
/* FIXME: Fees must be sufficient. */
|
|
|
|
return NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
static Pkt *find_commited_htlc(struct peer *peer, uint64_t id,
|
|
|
|
struct channel_htlc **local_htlc)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST check that `id` corresponds to an HTLC in its
|
|
|
|
* current commitment transaction, and MUST fail the
|
|
|
|
* connection if it does not.
|
|
|
|
*/
|
2016-06-28 23:19:20 +02:00
|
|
|
htlc = funding_htlc_by_id(peer->local.commit->cstate, id, OURS);
|
|
|
|
if (!htlc)
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Did not find HTLC %"PRIu64, id);
|
|
|
|
|
|
|
|
/* They must not fail/fulfill twice, so it should be in staging, too. */
|
2016-06-28 23:19:20 +02:00
|
|
|
*local_htlc = funding_htlc_by_id(peer->local.staging_cstate, id, OURS);
|
|
|
|
if (!*local_htlc)
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Already removed HTLC %"PRIu64, id);
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
return NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-08 01:11:15 +01:00
|
|
|
const UpdateFailHtlc *f = pkt->update_fail_htlc;
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
Pkt *err;
|
2016-05-26 07:55:24 +02:00
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
err = find_commited_htlc(peer, f->id, &htlc);
|
2016-03-31 08:43:20 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* FIXME: Save reason. */
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
funding_fail_htlc(peer->local.staging_cstate, htlc, OURS);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ... and the receiving node MUST add the HTLC fulfill/fail
|
|
|
|
* to the unacked changeset for its local commitment.
|
|
|
|
*/
|
|
|
|
stage.fail.fail = HTLC_FAIL;
|
|
|
|
stage.fail.id = f->id;
|
|
|
|
add_unacked(&peer->local, &stage);
|
2016-01-21 21:15:28 +01:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:15:28 +01:00
|
|
|
const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc;
|
2016-06-28 23:19:20 +02:00
|
|
|
struct channel_htlc *htlc;
|
2016-06-28 23:19:20 +02:00
|
|
|
struct sha256 rhash;
|
|
|
|
struct rval r;
|
2016-01-21 21:15:28 +01:00
|
|
|
Pkt *err;
|
2016-05-26 07:55:24 +02:00
|
|
|
union htlc_staging stage;
|
2016-03-15 07:37:31 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
err = find_commited_htlc(peer, f->id, &htlc);
|
2016-03-31 08:43:20 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Now, it must solve the HTLC rhash puzzle. */
|
2016-06-28 23:19:20 +02:00
|
|
|
proto_to_rval(f->r, &r);
|
2016-03-31 08:43:20 +02:00
|
|
|
sha256(&rhash, &r, sizeof(r));
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
if (!structeq(&rhash, &htlc->rhash))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Invalid r for %"PRIu64, f->id);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ... and the receiving node MUST add the HTLC fulfill/fail
|
|
|
|
* to the unacked changeset for its local commitment.
|
|
|
|
*/
|
2016-06-28 23:19:20 +02:00
|
|
|
funding_fulfill_htlc(peer->local.staging_cstate, htlc, OURS);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
stage.fulfill.fulfill = HTLC_FULFILL;
|
|
|
|
stage.fulfill.id = f->id;
|
|
|
|
stage.fulfill.r = r;
|
|
|
|
add_unacked(&peer->local, &stage);
|
2016-01-21 21:15:28 +01:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
const UpdateCommit *c = pkt->update_commit;
|
|
|
|
Pkt *err;
|
2016-05-26 07:55:25 +02:00
|
|
|
struct commit_info *ci = new_commit_info(peer);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
/* Create new commit info for this commit tx. */
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->prev = peer->local.commit;
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->commit_num = ci->prev->commit_num + 1;
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->revocation_hash = peer->local.next_revocation_hash;
|
2016-05-26 07:55:24 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node MUST apply all local acked and unacked
|
|
|
|
* changes except unacked fee changes to the local commitment
|
|
|
|
*/
|
|
|
|
/* (We already applied them to staging_cstate as we went) */
|
2016-05-26 07:55:24 +02:00
|
|
|
ci->cstate = copy_funding(ci, peer->local.staging_cstate);
|
2016-03-31 08:43:20 +02:00
|
|
|
ci->tx = create_commit_tx(ci,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.finalkey,
|
|
|
|
&peer->remote.finalkey,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.locktime,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.locktime,
|
2016-03-31 08:43:20 +02:00
|
|
|
&peer->anchor.txid,
|
|
|
|
peer->anchor.index,
|
|
|
|
peer->anchor.satoshis,
|
|
|
|
&ci->revocation_hash,
|
2016-05-02 08:35:56 +02:00
|
|
|
ci->cstate,
|
2016-05-26 07:55:24 +02:00
|
|
|
OURS,
|
2016-05-02 08:35:56 +02:00
|
|
|
&ci->map);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send an `update_commit` message which does
|
|
|
|
* not include any updates.
|
|
|
|
*/
|
|
|
|
if (ci->prev->cstate->changes == ci->cstate->changes)
|
|
|
|
return pkt_err(peer, "Empty commit");
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
err = check_and_save_commit_sig(peer, ci, c->sig);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Switch to the new commitment. */
|
2016-05-26 07:55:24 +02:00
|
|
|
peer->local.commit = ci;
|
2016-05-26 07:55:24 +02:00
|
|
|
peer_get_revocation_hash(peer, ci->commit_num + 1,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->local.next_revocation_hash);
|
2016-05-03 03:33:20 +02:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
return NULL;
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
2016-01-21 21:15:27 +01:00
|
|
|
|
|
|
|
static bool check_preimage(const Sha256Hash *preimage, const struct sha256 *hash)
|
|
|
|
{
|
|
|
|
struct sha256 h;
|
|
|
|
|
|
|
|
proto_to_sha256(preimage, &h);
|
|
|
|
sha256(&h, &h, sizeof(h));
|
|
|
|
return structeq(&h, hash);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
const UpdateRevocation *r = pkt->update_revocation;
|
2016-05-26 07:55:25 +02:00
|
|
|
struct commit_info *ci = peer->remote.commit->prev;
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The receiver of `update_revocation` MUST check that the
|
|
|
|
* SHA256 hash of `revocation_preimage` matches the previous commitment
|
|
|
|
* transaction, and MUST fail if it does not.
|
|
|
|
*/
|
2016-05-26 07:55:25 +02:00
|
|
|
if (!check_preimage(r->revocation_preimage, &ci->revocation_hash))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "complete preimage incorrect");
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* They're revoking the previous one. */
|
2016-05-26 07:55:25 +02:00
|
|
|
assert(!ci->revocation_preimage);
|
|
|
|
ci->revocation_preimage = tal(ci, struct sha256);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
proto_to_sha256(r->revocation_preimage, ci->revocation_preimage);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-06-23 17:11:10 +02:00
|
|
|
// save revocation preimages in shachain
|
2016-06-25 06:50:15 +02:00
|
|
|
if (!shachain_add_hash(&peer->their_preimages, 0xFFFFFFFFFFFFFFFFL - ci->commit_num, ci->revocation_preimage))
|
|
|
|
return pkt_err(peer, "preimage not next in shachain");
|
2016-06-23 16:38:35 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Save next revocation hash. */
|
|
|
|
proto_to_sha256(r->next_revocation_hash,
|
2016-05-26 07:55:24 +02:00
|
|
|
&peer->remote.next_revocation_hash);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* The receiver of `update_revocation`... MUST add the remote
|
|
|
|
* unacked changes to the set of local acked changes.
|
|
|
|
*/
|
2016-05-26 07:55:24 +02:00
|
|
|
apply_changeset(peer, &peer->local, OURS,
|
2016-05-26 07:55:25 +02:00
|
|
|
ci->unacked_changes,
|
|
|
|
tal_count(ci->unacked_changes));
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
/* Should never examine these again. */
|
|
|
|
ci->unacked_changes = tal_free(ci->unacked_changes);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt)
|
2016-03-24 02:39:41 +01:00
|
|
|
{
|
2016-04-24 12:31:52 +02:00
|
|
|
const CloseClearing *c = pkt->close_clearing;
|
|
|
|
|
|
|
|
/* FIXME: Filter for non-standardness? */
|
|
|
|
peer->closing.their_script = tal_dup_arr(peer, u8,
|
|
|
|
c->scriptpubkey.data,
|
|
|
|
c->scriptpubkey.len, 0);
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|