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-08-18 06:53:46 +02:00
|
|
|
#include "chaintopology.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-04-11 09:01:43 +02:00
|
|
|
#include "cryptopkt.h"
|
2016-06-30 01:38:11 +02:00
|
|
|
#include "htlc.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "names.h"
|
2016-08-18 06:53:45 +02:00
|
|
|
#include "packets.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#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-08-18 06:53:45 +02:00
|
|
|
#include <ccan/array_size/array_size.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
|
|
|
|
|
|
|
/* 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;
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
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-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "Queued pkt %s (order=%"PRIu64")",
|
|
|
|
pkt_name(pkt->pkt_case), peer->order_counter);
|
2016-05-26 07:55:24 +02:00
|
|
|
|
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-11-08 22:34:28 +01:00
|
|
|
void queue_pkt_open(struct peer *peer, bool offer_anchor)
|
2016-03-30 08:27:18 +02:00
|
|
|
{
|
|
|
|
OpenChannel *o = tal(peer, OpenChannel);
|
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);
|
2016-12-02 08:42:58 +01:00
|
|
|
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);
|
2016-06-28 23:19:21 +02:00
|
|
|
o->delay->locktime_case = LOCKTIME__LOCKTIME_BLOCKS;
|
|
|
|
o->delay->blocks = rel_locktime_to_blocks(&peer->local.locktime);
|
2016-05-26 07:55:24 +02:00
|
|
|
o->initial_fee_rate = peer->local.commit_fee_rate;
|
2016-11-08 22:34:28 +01:00
|
|
|
if (offer_anchor)
|
|
|
|
o->anch = OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR;
|
|
|
|
else
|
|
|
|
o->anch = OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_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-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);
|
|
|
|
|
2017-01-25 00:34:23 +01:00
|
|
|
s->sig = signature_to_proto(s, peer->remote.commit->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-30 01:38:11 +02:00
|
|
|
void queue_pkt_htlc_add(struct peer *peer, struct htlc *htlc)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateAddHtlc *u = tal(peer, UpdateAddHtlc);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
|
|
|
update_add_htlc__init(u);
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
u->id = htlc->id;
|
2016-09-06 09:17:49 +02:00
|
|
|
u->amount_msat = htlc->msatoshi;
|
2016-06-30 01:38:11 +02:00
|
|
|
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
|
|
|
u->route = tal(u, Routing);
|
|
|
|
routing__init(u->route);
|
2016-06-30 01:38:11 +02:00
|
|
|
u->route->info.data = tal_dup_arr(u, u8,
|
|
|
|
htlc->routing,
|
|
|
|
tal_count(htlc->routing),
|
|
|
|
0);
|
2016-06-30 01:38:10 +02:00
|
|
|
u->route->info.len = tal_count(u->route->info.data);
|
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-08-18 06:53:45 +02:00
|
|
|
void queue_pkt_htlc_fulfill(struct peer *peer, struct htlc *htlc)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
|
|
|
update_fulfill_htlc__init(f);
|
2016-06-30 01:38:11 +02:00
|
|
|
f->id = htlc->id;
|
2016-08-18 06:53:45 +02:00
|
|
|
f->r = rval_to_proto(f, htlc->r);
|
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-06-30 01:38:11 +02:00
|
|
|
void queue_pkt_htlc_fail(struct peer *peer, struct htlc *htlc)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-30 08:27:18 +02:00
|
|
|
UpdateFailHtlc *f = tal(peer, UpdateFailHtlc);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-08 01:11:15 +01:00
|
|
|
update_fail_htlc__init(f);
|
2016-06-30 01:38:11 +02:00
|
|
|
f->id = htlc->id;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
f->reason = tal(f, FailReason);
|
|
|
|
fail_reason__init(f->reason);
|
2016-08-31 07:21:41 +02:00
|
|
|
f->reason->info.len = tal_count(htlc->fail);
|
|
|
|
f->reason->info.data = tal_dup_arr(f->reason, u8,
|
|
|
|
htlc->fail, f->reason->info.len, 0);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_FAIL_HTLC, f);
|
2016-03-31 08:43:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:01:19 +02:00
|
|
|
void queue_pkt_feechange(struct peer *peer, u64 feerate)
|
|
|
|
{
|
|
|
|
UpdateFee *f = tal(peer, UpdateFee);
|
|
|
|
|
|
|
|
update_fee__init(f);
|
|
|
|
f->fee_rate = feerate;
|
|
|
|
|
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_FEE, f);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* OK, we're sending a signature for their pending changes. */
|
2017-01-25 00:34:23 +01:00
|
|
|
void queue_pkt_commit(struct peer *peer, const secp256k1_ecdsa_signature *sig)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
|
|
|
UpdateCommit *u = tal(peer, UpdateCommit);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Now send message */
|
|
|
|
update_commit__init(u);
|
2016-08-18 06:55:13 +02:00
|
|
|
if (sig)
|
2017-01-25 00:34:23 +01:00
|
|
|
u->sig = signature_to_proto(u, sig);
|
2016-08-18 06:55:13 +02:00
|
|
|
else
|
|
|
|
u->sig = NULL;
|
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
|
|
|
|
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-08-18 06:53:45 +02:00
|
|
|
void queue_pkt_revocation(struct peer *peer,
|
|
|
|
const struct sha256 *preimage,
|
|
|
|
const struct sha256 *next_hash)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
UpdateRevocation *u = tal(peer, UpdateRevocation);
|
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-08-18 06:53:46 +02:00
|
|
|
u->revocation_preimage = sha256_to_proto(u, preimage);
|
2016-08-18 06:55:08 +02:00
|
|
|
u->next_revocation_hash = sha256_to_proto(u, next_hash);
|
2016-03-31 08:43:20 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_UPDATE_REVOCATION, u);
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 14:55:15 +01:00
|
|
|
/* Send a serialized nested packet. */
|
|
|
|
void queue_pkt_nested(struct peer *peer,
|
|
|
|
int type,
|
|
|
|
const u8 *nested_pkt)
|
|
|
|
{
|
|
|
|
NestedPkt *pb = tal(peer, NestedPkt);
|
|
|
|
nested_pkt__init(pb);
|
|
|
|
pb->type = type;
|
|
|
|
pb->inner_pkt.len = tal_count(nested_pkt);
|
|
|
|
pb->inner_pkt.data = tal_dup_arr(pb, u8, nested_pkt, pb->inner_pkt.len, 0);
|
|
|
|
queue_pkt(peer, PKT__PKT_NESTED, pb);
|
|
|
|
}
|
|
|
|
|
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-06-28 23:19:21 +02:00
|
|
|
log_unusual(peer->log, "Sending PKT_ERROR: %s", e->problem);
|
2016-03-30 08:27:18 +02:00
|
|
|
return make_pkt(peer, PKT__PKT_ERROR, e);
|
|
|
|
}
|
|
|
|
|
2016-10-07 09:09:34 +02:00
|
|
|
Pkt *pkt_init(struct peer *peer, u64 ack)
|
2016-08-18 06:55:08 +02:00
|
|
|
{
|
2016-10-07 09:09:34 +02:00
|
|
|
Init *i = tal(peer, Init);
|
|
|
|
init__init(i);
|
|
|
|
i->ack = ack;
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-10-07 09:10:04 +02:00
|
|
|
*
|
|
|
|
* A node SHOULD set the `features` field of the `init`
|
|
|
|
* message to a bitset representing features it supports.
|
|
|
|
*/
|
|
|
|
/* No features yet! */
|
2016-10-07 09:09:34 +02:00
|
|
|
return make_pkt(peer, PKT__PKT_INIT, i);
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
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-08-18 06:53:46 +02:00
|
|
|
void queue_pkt_close_shutdown(struct peer *peer)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
CloseShutdown *c = tal(peer, CloseShutdown);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
close_shutdown__init(c);
|
2016-04-24 12:31:52 +02:00
|
|
|
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-08-18 06:53:46 +02:00
|
|
|
queue_pkt(peer, PKT__PKT_CLOSE_SHUTDOWN, 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;
|
2017-01-25 00:33:42 +01:00
|
|
|
secp256k1_ecdsa_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-11-07 13:33:02 +01:00
|
|
|
close_tx = peer_create_close_tx(c, 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);
|
2016-12-02 08:42:58 +01:00
|
|
|
c->sig = signature_to_proto(c, &our_close_sig);
|
2016-03-24 02:39:41 +01:00
|
|
|
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-08-18 06:53:45 +02:00
|
|
|
Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt,
|
|
|
|
struct sha256 *revocation_hash,
|
|
|
|
struct sha256 *next_revocation_hash)
|
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-08-18 06:53:46 +02:00
|
|
|
u64 feerate = get_feerate(peer->dstate);
|
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-06-28 23:19:21 +02:00
|
|
|
if (o->delay->locktime_case != LOCKTIME__LOCKTIME_BLOCKS)
|
|
|
|
return pkt_err(peer, "Delay in seconds not accepted");
|
|
|
|
if (o->delay->blocks > peer->dstate->config.locktime_max)
|
2016-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "Delay %u too great", o->delay->blocks);
|
2016-01-21 21:14:13 +01:00
|
|
|
if (o->min_depth > peer->dstate->config.anchor_confirms_max)
|
2016-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "min_depth %u too great", o->min_depth);
|
2016-08-18 06:53:46 +02:00
|
|
|
if (o->initial_fee_rate
|
|
|
|
< feerate * peer->dstate->config.commitment_fee_min_percent / 100)
|
2016-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "Commitment fee %u below %"PRIu64" x %u%%",
|
|
|
|
o->initial_fee_rate, feerate,
|
|
|
|
peer->dstate->config.commitment_fee_min_percent);
|
2016-10-07 05:30:17 +02:00
|
|
|
if (peer->dstate->config.commitment_fee_max_percent != 0
|
|
|
|
&& (o->initial_fee_rate
|
|
|
|
> feerate * peer->dstate->config.commitment_fee_max_percent/100))
|
2016-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "Commitment fee %u above %"PRIu64" x %u%%",
|
|
|
|
o->initial_fee_rate, feerate,
|
|
|
|
peer->dstate->config.commitment_fee_max_percent);
|
2016-01-21 21:14:13 +01:00
|
|
|
if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
|
2016-11-08 22:34:24 +01:00
|
|
|
peer->remote.offer_anchor = true;
|
2016-01-21 21:14:13 +01:00
|
|
|
else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)
|
2016-11-08 22:34:24 +01:00
|
|
|
peer->remote.offer_anchor = false;
|
2016-01-21 21:14:13 +01:00
|
|
|
else
|
2016-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "Unknown offer anchor value %u",
|
|
|
|
o->anch);
|
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-10-07 05:30:16 +02:00
|
|
|
return pkt_err(peer, "Exactly one side can offer anchor (we %s)",
|
2016-11-08 22:34:24 +01:00
|
|
|
peer->local.offer_anchor ? "do" : "don't");
|
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-12-02 08:42:58 +01:00
|
|
|
if (!proto_to_pubkey(o->commit_key, &peer->remote.commitkey))
|
2016-03-30 08:27:18 +02:00
|
|
|
return pkt_err(peer, "Bad commitkey");
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!proto_to_pubkey(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
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
proto_to_sha256(o->revocation_hash, revocation_hash);
|
|
|
|
proto_to_sha256(o->next_revocation_hash, next_revocation_hash);
|
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-11-08 22:34:24 +01:00
|
|
|
assert(!peer->local.offer_anchor);
|
|
|
|
assert(peer->remote.offer_anchor);
|
2016-01-21 21:14:27 +01:00
|
|
|
|
2016-09-13 05:10:32 +02:00
|
|
|
if (anchor_too_large(a->amount))
|
|
|
|
return pkt_err(peer, "Anchor millisatoshis exceeds 32 bits");
|
2016-11-11 00:02:04 +01:00
|
|
|
|
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-05-26 07:55:24 +02:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt,
|
2017-01-25 00:34:23 +01:00
|
|
|
secp256k1_ecdsa_signature *sig)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-01-21 21:15:05 +01:00
|
|
|
const OpenCommitSig *s = pkt->open_commit_sig;
|
|
|
|
|
2017-01-25 00:34:23 +01:00
|
|
|
if (!proto_to_signature(s->sig, sig))
|
2016-08-18 06:53:45 +02:00
|
|
|
return pkt_err(peer, "Malformed signature");
|
|
|
|
return NULL;
|
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).
|
|
|
|
*/
|
2016-08-18 06:53:45 +02:00
|
|
|
Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt, struct htlc **h)
|
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-01-21 21:15:28 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-03-31 08:43:20 +02:00
|
|
|
*
|
|
|
|
* `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-06-28 23:19:21 +02:00
|
|
|
if (abs_locktime_is_seconds(&expiry))
|
|
|
|
return pkt_err(peer, "HTLC expiry in seconds not supported!");
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-03-31 08:43:20 +02:00
|
|
|
*
|
|
|
|
* A node MUST NOT add a HTLC if it would result in it
|
2016-07-01 03:49:28 +02:00
|
|
|
* offering more than 300 HTLCs in the remote commitment transaction.
|
2016-03-31 08:43:20 +02:00
|
|
|
*/
|
2016-08-31 08:36:32 +02:00
|
|
|
if (peer->remote.staging_cstate->side[REMOTE].num_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
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-03-31 08:43:20 +02:00
|
|
|
*
|
2016-07-01 03:49:28 +02:00
|
|
|
* A node MUST set `id` to a unique identifier for this HTLC
|
|
|
|
* amongst all past or future `update_add_htlc` messages.
|
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-08-18 06:53:45 +02:00
|
|
|
if (htlc_get(&peer->htlcs, u->id, REMOTE))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-05-26 07:55:24 +02:00
|
|
|
*
|
|
|
|
* ...and the receiving node MUST add the HTLC addition to the
|
|
|
|
* unacked changeset for its local commitment. */
|
2016-08-18 06:53:45 +02:00
|
|
|
*h = peer_new_htlc(peer, u->id, u->amount_msat, &rhash,
|
|
|
|
abs_locktime_to_blocks(&expiry),
|
|
|
|
u->route->info.data, u->route->info.len,
|
|
|
|
NULL, RCVD_ADD_HTLC);
|
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
|
|
|
|
2016-06-28 23:19:20 +02:00
|
|
|
static Pkt *find_commited_htlc(struct peer *peer, uint64_t id,
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc **local_htlc)
|
2016-03-31 08:43:20 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
*local_htlc = htlc_get(&peer->htlcs, id, LOCAL);
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-03-31 08:43:20 +02:00
|
|
|
*
|
|
|
|
* 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-08-18 06:53:46 +02:00
|
|
|
if (!(*local_htlc))
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "Did not find HTLC %"PRIu64, id);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if ((*local_htlc)->state != SENT_ADD_ACK_REVOCATION)
|
|
|
|
return pkt_err(peer, "HTLC %"PRIu64" state %s", id,
|
|
|
|
htlc_state_name((*local_htlc)->state));
|
2016-03-31 08:43:20 +02:00
|
|
|
|
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-09-02 04:32:18 +02:00
|
|
|
Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt, struct htlc **h,
|
|
|
|
u8 **fail)
|
2016-01-21 21:14:13 +01:00
|
|
|
{
|
2016-03-08 01:11:15 +01:00
|
|
|
const UpdateFailHtlc *f = pkt->update_fail_htlc;
|
2016-01-21 21:15:28 +01:00
|
|
|
Pkt *err;
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
err = find_commited_htlc(peer, f->id, h);
|
2016-03-31 08:43:20 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-31 07:21:41 +02:00
|
|
|
if ((*h)->r)
|
|
|
|
return pkt_err(peer, "HTLC %"PRIu64" already fulfilled",
|
|
|
|
(*h)->id);
|
|
|
|
|
2016-09-02 04:32:18 +02:00
|
|
|
*fail = tal_dup_arr(*h, u8, f->reason->info.data, f->reason->info.len,0);
|
2016-01-21 21:15:28 +01:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt, struct htlc **h,
|
2016-09-02 04:31:18 +02:00
|
|
|
struct rval *r)
|
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 sha256 rhash;
|
2016-01-21 21:15:28 +01:00
|
|
|
Pkt *err;
|
2016-03-15 07:37:31 +01:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
err = find_commited_htlc(peer, f->id, h);
|
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-09-02 04:31:18 +02:00
|
|
|
proto_to_rval(f->r, r);
|
|
|
|
sha256(&rhash, r, sizeof(*r));
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
if (!structeq(&rhash, &(*h)->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-01-21 21:15:28 +01:00
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:03:30 +02:00
|
|
|
Pkt *accept_pkt_update_fee(struct peer *peer, const Pkt *pkt, u64 *feerate)
|
|
|
|
{
|
|
|
|
const UpdateFee *f = pkt->update_fee;
|
|
|
|
|
|
|
|
*feerate = f->fee_rate;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt,
|
2017-01-25 00:34:23 +01:00
|
|
|
secp256k1_ecdsa_signature *sig)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-03-31 08:43:20 +02:00
|
|
|
const UpdateCommit *c = pkt->update_commit;
|
2016-05-03 03:33:20 +02:00
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!c->sig && sig)
|
|
|
|
return pkt_err(peer, "Expected signature");
|
|
|
|
|
|
|
|
if (!sig && c->sig)
|
|
|
|
return pkt_err(peer, "Unexpected signature");
|
|
|
|
|
|
|
|
if (!sig && !c->sig)
|
|
|
|
return NULL;
|
|
|
|
|
2017-01-25 00:34:23 +01:00
|
|
|
if (!proto_to_signature(c->sig, sig))
|
2016-08-18 06:53:45 +02:00
|
|
|
return pkt_err(peer, "Malformed signature");
|
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
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
const UpdateRevocation *r = pkt->update_revocation;
|
2016-08-18 06:53:46 +02:00
|
|
|
struct sha256 h, preimage;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(peer->their_prev_revocation_hash);
|
|
|
|
proto_to_sha256(r->revocation_preimage, &preimage);
|
2016-01-21 21:14:13 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-05-26 07:55:24 +02:00
|
|
|
*
|
|
|
|
* 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-08-18 06:53:46 +02:00
|
|
|
sha256(&h, &preimage, sizeof(preimage));
|
2016-08-18 06:55:08 +02:00
|
|
|
if (!structeq(&h, peer->their_prev_revocation_hash)) {
|
|
|
|
log_unusual(peer->log, "Incorrect preimage for %"PRIu64,
|
|
|
|
peer->remote.commit->commit_num - 1);
|
2016-03-31 08:43:20 +02:00
|
|
|
return pkt_err(peer, "complete preimage incorrect");
|
2016-08-18 06:55:08 +02:00
|
|
|
}
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-06-23 17:11:10 +02:00
|
|
|
// save revocation preimages in shachain
|
2016-08-18 06:53:45 +02:00
|
|
|
if (!shachain_add_hash(&peer->their_preimages,
|
2016-08-18 06:53:46 +02:00
|
|
|
0xFFFFFFFFFFFFFFFFL
|
|
|
|
- (peer->remote.commit->commit_num - 1),
|
|
|
|
&preimage))
|
2016-06-25 06:50:15 +02:00
|
|
|
return pkt_err(peer, "preimage not next in shachain");
|
2016-06-23 16:38:35 +02:00
|
|
|
|
2016-08-18 06:55:08 +02:00
|
|
|
log_debug(peer->log, "Got revocation preimage %"PRIu64,
|
|
|
|
peer->remote.commit->commit_num - 1);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Clear the previous revocation hash. */
|
|
|
|
peer->their_prev_revocation_hash
|
|
|
|
= tal_free(peer->their_prev_revocation_hash);
|
|
|
|
|
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
|
|
|
return NULL;
|
2016-01-21 21:14:13 +01:00
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
Pkt *accept_pkt_close_shutdown(struct peer *peer, const Pkt *pkt)
|
2016-03-24 02:39:41 +01:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
const CloseShutdown *c = pkt->close_shutdown;
|
2016-04-24 12:31:52 +02:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
/* FIXME-OLD #2:
|
2016-08-31 08:36:32 +02:00
|
|
|
*
|
|
|
|
* 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG`
|
|
|
|
* (pay to pubkey hash), OR
|
|
|
|
* 2. `OP_HASH160` `20` 20-bytes `OP_EQUAL` (pay to script hash), OR
|
|
|
|
* 3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), OR
|
|
|
|
* 4. `OP_0` `32` 32-bytes (version 0 pay to witness script hash)
|
|
|
|
*
|
|
|
|
* A node receiving `close_shutdown` SHOULD fail the connection
|
|
|
|
* `script_pubkey` is not one of those forms.
|
|
|
|
*/
|
|
|
|
if (!is_p2pkh(c->scriptpubkey.data, c->scriptpubkey.len)
|
|
|
|
&& !is_p2sh(c->scriptpubkey.data, c->scriptpubkey.len)
|
|
|
|
&& !is_p2wpkh(c->scriptpubkey.data, c->scriptpubkey.len)
|
|
|
|
&& !is_p2wsh(c->scriptpubkey.data, c->scriptpubkey.len)) {
|
|
|
|
log_broken_blob(peer->log, "Bad script_pubkey %s",
|
|
|
|
c->scriptpubkey.data, c->scriptpubkey.len);
|
|
|
|
return pkt_err(peer, "Bad script_pubkey");
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:31:52 +02:00
|
|
|
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;
|
|
|
|
}
|