Browse Source

daemon: use "local" and "remote" instead of "us" and "them".

This is the language used in BOLT#2; be consistent.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
84f5a82eea
  1. 212
      daemon/packets.c
  2. 230
      daemon/peer.c
  3. 4
      daemon/peer.h
  4. 16
      daemon/secrets.c

212
daemon/packets.c

@ -97,29 +97,29 @@ void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor)
/* Set up out commit info now: rest gets done in setup_first_commit /* Set up out commit info now: rest gets done in setup_first_commit
* once anchor is established. */ * once anchor is established. */
peer->us.commit = talz(peer, struct commit_info); peer->local.commit = talz(peer, struct commit_info);
peer->us.commit->revocation_hash = peer->us.next_revocation_hash; peer->local.commit->revocation_hash = peer->local.next_revocation_hash;
peer_get_revocation_hash(peer, 1, &peer->us.next_revocation_hash); peer_get_revocation_hash(peer, 1, &peer->local.next_revocation_hash);
open_channel__init(o); open_channel__init(o);
o->revocation_hash = sha256_to_proto(o, &peer->us.commit->revocation_hash); o->revocation_hash = sha256_to_proto(o, &peer->local.commit->revocation_hash);
o->next_revocation_hash = sha256_to_proto(o, &peer->us.next_revocation_hash); o->next_revocation_hash = sha256_to_proto(o, &peer->local.next_revocation_hash);
o->commit_key = pubkey_to_proto(o, &peer->us.commitkey); o->commit_key = pubkey_to_proto(o, &peer->local.commitkey);
o->final_key = pubkey_to_proto(o, &peer->us.finalkey); o->final_key = pubkey_to_proto(o, &peer->local.finalkey);
o->delay = tal(o, Locktime); o->delay = tal(o, Locktime);
locktime__init(o->delay); locktime__init(o->delay);
o->delay->locktime_case = LOCKTIME__LOCKTIME_SECONDS; o->delay->locktime_case = LOCKTIME__LOCKTIME_SECONDS;
o->delay->seconds = rel_locktime_to_seconds(&peer->us.locktime); o->delay->seconds = rel_locktime_to_seconds(&peer->local.locktime);
o->initial_fee_rate = peer->us.commit_fee_rate; o->initial_fee_rate = peer->local.commit_fee_rate;
if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
assert(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR); assert(peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR);
else { else {
assert(anchor == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR); assert(anchor == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR);
assert(peer->us.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
} }
o->anch = anchor; o->anch = anchor;
o->min_depth = peer->us.mindepth; o->min_depth = peer->local.mindepth;
queue_pkt(peer, PKT__PKT_OPEN, o); queue_pkt(peer, PKT__PKT_OPEN, o);
} }
@ -141,12 +141,12 @@ void queue_pkt_anchor(struct peer *peer)
} }
/* Sign their commit sig */ /* Sign their commit sig */
peer->them.commit->sig = tal(peer->them.commit, peer->remote.commit->sig = tal(peer->remote.commit,
struct bitcoin_signature); struct bitcoin_signature);
peer->them.commit->sig->stype = SIGHASH_ALL; peer->remote.commit->sig->stype = SIGHASH_ALL;
peer_sign_theircommit(peer, peer->them.commit->tx, peer_sign_theircommit(peer, peer->remote.commit->tx,
&peer->them.commit->sig->sig); &peer->remote.commit->sig->sig);
a->commit_sig = signature_to_proto(a, &peer->them.commit->sig->sig); a->commit_sig = signature_to_proto(a, &peer->remote.commit->sig->sig);
queue_pkt(peer, PKT__PKT_OPEN_ANCHOR, a); queue_pkt(peer, PKT__PKT_OPEN_ANCHOR, a);
} }
@ -157,15 +157,15 @@ void queue_pkt_open_commit_sig(struct peer *peer)
open_commit_sig__init(s); open_commit_sig__init(s);
dump_tx("Creating sig for:", peer->them.commit->tx); dump_tx("Creating sig for:", peer->remote.commit->tx);
dump_key("Using key:", &peer->us.commitkey); dump_key("Using key:", &peer->local.commitkey);
peer->them.commit->sig = tal(peer->them.commit, peer->remote.commit->sig = tal(peer->remote.commit,
struct bitcoin_signature); struct bitcoin_signature);
peer->them.commit->sig->stype = SIGHASH_ALL; peer->remote.commit->sig->stype = SIGHASH_ALL;
peer_sign_theircommit(peer, peer->them.commit->tx, peer_sign_theircommit(peer, peer->remote.commit->tx,
&peer->them.commit->sig->sig); &peer->remote.commit->sig->sig);
s->sig = signature_to_proto(s, &peer->them.commit->sig->sig); s->sig = signature_to_proto(s, &peer->remote.commit->sig->sig);
queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s); queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s);
} }
@ -184,7 +184,7 @@ static void add_our_htlc_ourside(struct peer *peer, void *arg)
struct channel_htlc *htlc = arg; struct channel_htlc *htlc = arg;
/* FIXME: must add even if can't pay fee any more! */ /* FIXME: must add even if can't pay fee any more! */
if (!funding_a_add_htlc(peer->us.staging_cstate, if (!funding_a_add_htlc(peer->local.staging_cstate,
htlc->msatoshis, &htlc->expiry, htlc->msatoshis, &htlc->expiry,
&htlc->rhash, htlc->id)) &htlc->rhash, htlc->id))
fatal("FIXME: Failed to add htlc %"PRIu64" to self on ack", fatal("FIXME: Failed to add htlc %"PRIu64" to self on ack",
@ -209,7 +209,7 @@ void queue_pkt_htlc_add(struct peer *peer,
routing__init(u->route); routing__init(u->route);
/* We're about to send this, so their side will have it from now on. */ /* We're about to send this, so their side will have it from now on. */
if (!funding_b_add_htlc(peer->them.staging_cstate, if (!funding_b_add_htlc(peer->remote.staging_cstate,
htlc_prog->stage.add.htlc.msatoshis, htlc_prog->stage.add.htlc.msatoshis,
&htlc_prog->stage.add.htlc.expiry, &htlc_prog->stage.add.htlc.expiry,
&htlc_prog->stage.add.htlc.rhash, &htlc_prog->stage.add.htlc.rhash,
@ -230,8 +230,8 @@ static void fulfill_their_htlc_ourside(struct peer *peer, void *arg)
{ {
size_t n; size_t n;
n = funding_htlc_by_id(&peer->us.staging_cstate->b, ptr2int(arg)); n = funding_htlc_by_id(&peer->local.staging_cstate->b, ptr2int(arg));
funding_b_fulfill_htlc(peer->us.staging_cstate, n); funding_b_fulfill_htlc(peer->local.staging_cstate, n);
} }
void queue_pkt_htlc_fulfill(struct peer *peer, void queue_pkt_htlc_fulfill(struct peer *peer,
@ -247,8 +247,8 @@ void queue_pkt_htlc_fulfill(struct peer *peer,
f->r = sha256_to_proto(f, &htlc_prog->stage.fulfill.r); f->r = sha256_to_proto(f, &htlc_prog->stage.fulfill.r);
/* We're about to send this, so their side will have it from now on. */ /* We're about to send this, so their side will have it from now on. */
n = funding_htlc_by_id(&peer->them.staging_cstate->a, f->id); n = funding_htlc_by_id(&peer->remote.staging_cstate->a, f->id);
funding_a_fulfill_htlc(peer->them.staging_cstate, n); funding_a_fulfill_htlc(peer->remote.staging_cstate, n);
their_commit_changed(peer); their_commit_changed(peer);
queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f, queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f,
@ -260,8 +260,8 @@ static void fail_their_htlc_ourside(struct peer *peer, void *arg)
{ {
size_t n; size_t n;
n = funding_htlc_by_id(&peer->us.staging_cstate->b, ptr2int(arg)); n = funding_htlc_by_id(&peer->local.staging_cstate->b, ptr2int(arg));
funding_b_fail_htlc(peer->us.staging_cstate, n); funding_b_fail_htlc(peer->local.staging_cstate, n);
} }
void queue_pkt_htlc_fail(struct peer *peer, void queue_pkt_htlc_fail(struct peer *peer,
@ -279,8 +279,8 @@ void queue_pkt_htlc_fail(struct peer *peer,
fail_reason__init(f->reason); fail_reason__init(f->reason);
/* We're about to send this, so their side will have it from now on. */ /* We're about to send this, so their side will have it from now on. */
n = funding_htlc_by_id(&peer->them.staging_cstate->a, f->id); n = funding_htlc_by_id(&peer->remote.staging_cstate->a, f->id);
funding_a_fail_htlc(peer->them.staging_cstate, n); funding_a_fail_htlc(peer->remote.staging_cstate, n);
their_commit_changed(peer); their_commit_changed(peer);
queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FAIL_HTLC, f, queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FAIL_HTLC, f,
@ -294,13 +294,13 @@ void queue_pkt_commit(struct peer *peer)
struct commit_info *ci = talz(peer, struct commit_info); struct commit_info *ci = talz(peer, struct commit_info);
/* Create new commit info for this commit tx. */ /* Create new commit info for this commit tx. */
ci->prev = peer->them.commit; ci->prev = peer->remote.commit;
ci->revocation_hash = peer->them.next_revocation_hash; ci->revocation_hash = peer->remote.next_revocation_hash;
ci->cstate = copy_funding(ci, peer->them.staging_cstate); ci->cstate = copy_funding(ci, peer->remote.staging_cstate);
ci->tx = create_commit_tx(ci, ci->tx = create_commit_tx(ci,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.finalkey, &peer->local.finalkey,
&peer->us.locktime, &peer->local.locktime,
&peer->anchor.txid, &peer->anchor.txid,
peer->anchor.index, peer->anchor.index,
peer->anchor.satoshis, peer->anchor.satoshis,
@ -326,7 +326,7 @@ void queue_pkt_commit(struct peer *peer)
peer_sign_theircommit(peer, ci->tx, &ci->sig->sig); peer_sign_theircommit(peer, ci->tx, &ci->sig->sig);
/* Switch to the new commitment. */ /* Switch to the new commitment. */
peer->them.commit = ci; peer->remote.commit = ci;
peer_check_if_cleared(peer); peer_check_if_cleared(peer);
@ -339,7 +339,7 @@ void queue_pkt_commit(struct peer *peer)
} }
/* Send a preimage for the old commit tx. The one we've just committed to is /* Send a preimage for the old commit tx. The one we've just committed to is
* in peer->us.commit. */ * in peer->local.commit. */
void queue_pkt_revocation(struct peer *peer) void queue_pkt_revocation(struct peer *peer)
{ {
UpdateRevocation *u = tal(peer, UpdateRevocation); UpdateRevocation *u = tal(peer, UpdateRevocation);
@ -347,23 +347,23 @@ void queue_pkt_revocation(struct peer *peer)
update_revocation__init(u); update_revocation__init(u);
assert(peer->commit_tx_counter > 0); assert(peer->commit_tx_counter > 0);
assert(peer->us.commit); assert(peer->local.commit);
assert(peer->us.commit->prev); assert(peer->local.commit->prev);
assert(!peer->us.commit->prev->revocation_preimage); assert(!peer->local.commit->prev->revocation_preimage);
/* We have their signature on the current one, right? */ /* We have their signature on the current one, right? */
assert(peer->us.commit->sig); assert(peer->local.commit->sig);
peer->us.commit->prev->revocation_preimage peer->local.commit->prev->revocation_preimage
= tal(peer->us.commit->prev, struct sha256); = tal(peer->local.commit->prev, struct sha256);
peer_get_revocation_preimage(peer, peer->commit_tx_counter-1, peer_get_revocation_preimage(peer, peer->commit_tx_counter-1,
peer->us.commit->prev->revocation_preimage); peer->local.commit->prev->revocation_preimage);
peer_check_if_cleared(peer); peer_check_if_cleared(peer);
u->revocation_preimage u->revocation_preimage
= sha256_to_proto(u, peer->us.commit->prev->revocation_preimage); = sha256_to_proto(u, peer->local.commit->prev->revocation_preimage);
u->next_revocation_hash = sha256_to_proto(u, u->next_revocation_hash = sha256_to_proto(u,
&peer->us.next_revocation_hash); &peer->local.next_revocation_hash);
u->ack = peer_outgoing_ack(peer); u->ack = peer_outgoing_ack(peer);
queue_pkt(peer, PKT__PKT_UPDATE_REVOCATION, u); queue_pkt(peer, PKT__PKT_UPDATE_REVOCATION, u);
@ -393,7 +393,7 @@ void queue_pkt_close_clearing(struct peer *peer)
CloseClearing *c = tal(peer, CloseClearing); CloseClearing *c = tal(peer, CloseClearing);
close_clearing__init(c); close_clearing__init(c);
redeemscript = bitcoin_redeem_single(c, &peer->us.finalkey); redeemscript = bitcoin_redeem_single(c, &peer->local.finalkey);
peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript); peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript);
c->scriptpubkey.data = tal_dup_arr(c, u8, c->scriptpubkey.data = tal_dup_arr(c, u8,
@ -446,37 +446,37 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt)
if (o->initial_fee_rate < peer->dstate->config.commitment_fee_rate_min) if (o->initial_fee_rate < peer->dstate->config.commitment_fee_rate_min)
return pkt_err(peer, "Commitment fee rate too low"); return pkt_err(peer, "Commitment fee rate too low");
if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
peer->them.offer_anchor = CMD_OPEN_WITH_ANCHOR; peer->remote.offer_anchor = CMD_OPEN_WITH_ANCHOR;
else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR) else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)
peer->them.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR; peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
else else
return pkt_err(peer, "Unknown offer anchor value"); return pkt_err(peer, "Unknown offer anchor value");
if (peer->them.offer_anchor == peer->us.offer_anchor) if (peer->remote.offer_anchor == peer->local.offer_anchor)
return pkt_err(peer, "Only one side can offer anchor"); return pkt_err(peer, "Only one side can offer anchor");
if (!proto_to_rel_locktime(o->delay, &peer->them.locktime)) if (!proto_to_rel_locktime(o->delay, &peer->remote.locktime))
return pkt_err(peer, "Malformed locktime"); return pkt_err(peer, "Malformed locktime");
peer->them.mindepth = o->min_depth; peer->remote.mindepth = o->min_depth;
peer->them.commit_fee_rate = o->initial_fee_rate; peer->remote.commit_fee_rate = o->initial_fee_rate;
if (!proto_to_pubkey(peer->dstate->secpctx, if (!proto_to_pubkey(peer->dstate->secpctx,
o->commit_key, &peer->them.commitkey)) o->commit_key, &peer->remote.commitkey))
return pkt_err(peer, "Bad commitkey"); return pkt_err(peer, "Bad commitkey");
if (!proto_to_pubkey(peer->dstate->secpctx, if (!proto_to_pubkey(peer->dstate->secpctx,
o->final_key, &peer->them.finalkey)) o->final_key, &peer->remote.finalkey))
return pkt_err(peer, "Bad finalkey"); return pkt_err(peer, "Bad finalkey");
/* Set up their commit info now: rest gets done in setup_first_commit /* Set up their commit info now: rest gets done in setup_first_commit
* once anchor is established. */ * once anchor is established. */
peer->them.commit = talz(peer, struct commit_info); peer->remote.commit = talz(peer, struct commit_info);
proto_to_sha256(o->revocation_hash, &peer->them.commit->revocation_hash); proto_to_sha256(o->revocation_hash, &peer->remote.commit->revocation_hash);
proto_to_sha256(o->next_revocation_hash, proto_to_sha256(o->next_revocation_hash,
&peer->them.next_revocation_hash); &peer->remote.next_revocation_hash);
/* Witness script for anchor. */ /* Witness script for anchor. */
peer->anchor.witnessscript peer->anchor.witnessscript
= bitcoin_redeem_2of2(peer, &peer->us.commitkey, = bitcoin_redeem_2of2(peer, &peer->local.commitkey,
&peer->them.commitkey); &peer->remote.commitkey);
return NULL; return NULL;
} }
@ -496,7 +496,7 @@ static Pkt *check_and_save_commit_sig(struct peer *peer,
ci->tx, 0, ci->tx, 0,
NULL, 0, NULL, 0,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->them.commitkey, &peer->remote.commitkey,
ci->sig)) ci->sig))
return pkt_err(peer, "Bad signature"); return pkt_err(peer, "Bad signature");
@ -508,8 +508,8 @@ Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt)
const OpenAnchor *a = pkt->open_anchor; const OpenAnchor *a = pkt->open_anchor;
/* They must be offering anchor for us to try accepting */ /* They must be offering anchor for us to try accepting */
assert(peer->us.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
assert(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR); assert(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR);
proto_to_sha256(a->txid, &peer->anchor.txid.sha); proto_to_sha256(a->txid, &peer->anchor.txid.sha);
peer->anchor.index = a->output_index; peer->anchor.index = a->output_index;
@ -518,14 +518,14 @@ Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt)
if (!setup_first_commit(peer)) if (!setup_first_commit(peer))
return pkt_err(peer, "Insufficient funds for fee"); return pkt_err(peer, "Insufficient funds for fee");
return check_and_save_commit_sig(peer, peer->us.commit, a->commit_sig); return check_and_save_commit_sig(peer, peer->local.commit, a->commit_sig);
} }
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt) Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt)
{ {
const OpenCommitSig *s = pkt->open_commit_sig; const OpenCommitSig *s = pkt->open_commit_sig;
return check_and_save_commit_sig(peer, peer->us.commit, s->sig); return check_and_save_commit_sig(peer, peer->local.commit, s->sig);
} }
Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt) Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt)
@ -563,8 +563,8 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
* A node MUST NOT add a HTLC if it would result in it * A node MUST NOT add a HTLC if it would result in it
* offering more than 300 HTLCs in either commitment transaction. * offering more than 300 HTLCs in either commitment transaction.
*/ */
if (tal_count(peer->them.staging_cstate->a.htlcs) == 300 if (tal_count(peer->remote.staging_cstate->a.htlcs) == 300
|| tal_count(peer->us.staging_cstate->b.htlcs) == 300) || tal_count(peer->local.staging_cstate->b.htlcs) == 300)
return pkt_err(peer, "Too many HTLCs"); return pkt_err(peer, "Too many HTLCs");
/* BOLT #2: /* BOLT #2:
@ -572,14 +572,14 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
* A node MUST NOT set `id` equal to another HTLC which is in * A node MUST NOT set `id` equal to another HTLC which is in
* the current staged commitment transaction. * the current staged commitment transaction.
*/ */
if (funding_htlc_by_id(&peer->them.staging_cstate->a, u->id) if (funding_htlc_by_id(&peer->remote.staging_cstate->a, u->id)
< tal_count(peer->them.staging_cstate->a.htlcs)) < tal_count(peer->remote.staging_cstate->a.htlcs))
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id); return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
/* FIXME: Assert this... */ /* FIXME: Assert this... */
/* Note: these should be in sync, so this should be redundant! */ /* Note: these should be in sync, so this should be redundant! */
if (funding_htlc_by_id(&peer->us.staging_cstate->b, u->id) if (funding_htlc_by_id(&peer->local.staging_cstate->b, u->id)
< tal_count(peer->us.staging_cstate->b.htlcs)) < tal_count(peer->local.staging_cstate->b.htlcs))
return pkt_err(peer, "HTLC id %"PRIu64" clashes for us", u->id); return pkt_err(peer, "HTLC id %"PRIu64" clashes for us", u->id);
/* BOLT #2: /* BOLT #2:
@ -593,7 +593,7 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
/* FIXME: This is wrong! We may have already added more txs to /* FIXME: This is wrong! We may have already added more txs to
* them.staging_cstate, driving that fee up. * them.staging_cstate, driving that fee up.
* We should check against the last version they acknowledged. */ * We should check against the last version they acknowledged. */
if (!funding_a_add_htlc(peer->them.staging_cstate, if (!funding_a_add_htlc(peer->remote.staging_cstate,
u->amount_msat, &expiry, &rhash, u->id)) u->amount_msat, &expiry, &rhash, u->id))
return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis" return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis"
" in your commitment tx", " in your commitment tx",
@ -601,7 +601,7 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
/* If we fail here, we've already changed them.staging_cstate, so /* If we fail here, we've already changed them.staging_cstate, so
* MUST terminate. */ * MUST terminate. */
if (!funding_b_add_htlc(peer->us.staging_cstate, if (!funding_b_add_htlc(peer->local.staging_cstate,
u->amount_msat, &expiry, &rhash, u->id)) u->amount_msat, &expiry, &rhash, u->id))
return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis" return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis"
" in our commitment tx", " in our commitment tx",
@ -623,19 +623,19 @@ static Pkt *find_commited_htlc(struct peer *peer, uint64_t id,
* current commitment transaction, and MUST fail the * current commitment transaction, and MUST fail the
* connection if it does not. * connection if it does not.
*/ */
*n_us = funding_htlc_by_id(&peer->us.commit->cstate->a, id); *n_us = funding_htlc_by_id(&peer->local.commit->cstate->a, id);
if (*n_us == tal_count(peer->us.commit->cstate->a.htlcs)) if (*n_us == tal_count(peer->local.commit->cstate->a.htlcs))
return pkt_err(peer, "Did not find HTLC %"PRIu64, id); return pkt_err(peer, "Did not find HTLC %"PRIu64, id);
/* They must not fail/fulfill twice, so it should be in staging, too. */ /* They must not fail/fulfill twice, so it should be in staging, too. */
*n_us = funding_htlc_by_id(&peer->us.staging_cstate->a, id); *n_us = funding_htlc_by_id(&peer->local.staging_cstate->a, id);
if (*n_us == tal_count(peer->us.staging_cstate->a.htlcs)) if (*n_us == tal_count(peer->local.staging_cstate->a.htlcs))
return pkt_err(peer, "Already removed HTLC %"PRIu64, id); return pkt_err(peer, "Already removed HTLC %"PRIu64, id);
/* FIXME: Assert this... */ /* FIXME: Assert this... */
/* Note: these should match. */ /* Note: these should match. */
*n_them = funding_htlc_by_id(&peer->them.staging_cstate->b, id); *n_them = funding_htlc_by_id(&peer->remote.staging_cstate->b, id);
if (*n_them == tal_count(peer->them.staging_cstate->b.htlcs)) if (*n_them == tal_count(peer->remote.staging_cstate->b.htlcs))
return pkt_err(peer, "Did not find your HTLC %"PRIu64, id); return pkt_err(peer, "Did not find your HTLC %"PRIu64, id);
return NULL; return NULL;
@ -653,8 +653,8 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
/* FIXME: Save reason. */ /* FIXME: Save reason. */
funding_a_fail_htlc(peer->us.staging_cstate, n_us); funding_a_fail_htlc(peer->local.staging_cstate, n_us);
funding_b_fail_htlc(peer->them.staging_cstate, n_them); funding_b_fail_htlc(peer->remote.staging_cstate, n_them);
their_commit_changed(peer); their_commit_changed(peer);
return NULL; return NULL;
} }
@ -674,14 +674,14 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
proto_to_sha256(f->r, &r); proto_to_sha256(f->r, &r);
sha256(&rhash, &r, sizeof(r)); sha256(&rhash, &r, sizeof(r));
if (!structeq(&rhash, &peer->us.staging_cstate->a.htlcs[n_us].rhash)) if (!structeq(&rhash, &peer->local.staging_cstate->a.htlcs[n_us].rhash))
return pkt_err(peer, "Invalid r for %"PRIu64, f->id); return pkt_err(peer, "Invalid r for %"PRIu64, f->id);
/* Same ID must have same rhash */ /* Same ID must have same rhash */
assert(structeq(&rhash, &peer->them.staging_cstate->b.htlcs[n_them].rhash)); assert(structeq(&rhash, &peer->remote.staging_cstate->b.htlcs[n_them].rhash));
funding_a_fulfill_htlc(peer->us.staging_cstate, n_us); funding_a_fulfill_htlc(peer->local.staging_cstate, n_us);
funding_b_fulfill_htlc(peer->them.staging_cstate, n_them); funding_b_fulfill_htlc(peer->remote.staging_cstate, n_them);
their_commit_changed(peer); their_commit_changed(peer);
return NULL; return NULL;
} }
@ -693,13 +693,13 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt)
struct commit_info *ci = talz(peer, struct commit_info); struct commit_info *ci = talz(peer, struct commit_info);
/* Create new commit info for this commit tx. */ /* Create new commit info for this commit tx. */
ci->prev = peer->us.commit; ci->prev = peer->local.commit;
ci->revocation_hash = peer->us.next_revocation_hash; ci->revocation_hash = peer->local.next_revocation_hash;
ci->cstate = copy_funding(ci, peer->us.staging_cstate); ci->cstate = copy_funding(ci, peer->local.staging_cstate);
ci->tx = create_commit_tx(ci, ci->tx = create_commit_tx(ci,
&peer->us.finalkey, &peer->local.finalkey,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->them.locktime, &peer->remote.locktime,
&peer->anchor.txid, &peer->anchor.txid,
peer->anchor.index, peer->anchor.index,
peer->anchor.satoshis, peer->anchor.satoshis,
@ -720,10 +720,10 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt)
return err; return err;
/* Switch to the new commitment. */ /* Switch to the new commitment. */
peer->us.commit = ci; peer->local.commit = ci;
peer->commit_tx_counter++; peer->commit_tx_counter++;
peer_get_revocation_hash(peer, peer->commit_tx_counter + 1, peer_get_revocation_hash(peer, peer->commit_tx_counter + 1,
&peer->us.next_revocation_hash); &peer->local.next_revocation_hash);
peer_check_if_cleared(peer); peer_check_if_cleared(peer);
return NULL; return NULL;
@ -744,21 +744,21 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt)
/* FIXME: Save preimage in shachain too. */ /* FIXME: Save preimage in shachain too. */
if (!check_preimage(r->revocation_preimage, if (!check_preimage(r->revocation_preimage,
&peer->them.commit->prev->revocation_hash)) &peer->remote.commit->prev->revocation_hash))
return pkt_err(peer, "complete preimage incorrect"); return pkt_err(peer, "complete preimage incorrect");
/* They're revoking the previous one. */ /* They're revoking the previous one. */
assert(!peer->them.commit->prev->revocation_preimage); assert(!peer->remote.commit->prev->revocation_preimage);
peer->them.commit->prev->revocation_preimage peer->remote.commit->prev->revocation_preimage
= tal(peer->them.commit->prev, struct sha256); = tal(peer->remote.commit->prev, struct sha256);
proto_to_sha256(r->revocation_preimage, proto_to_sha256(r->revocation_preimage,
peer->them.commit->prev->revocation_preimage); peer->remote.commit->prev->revocation_preimage);
peer_check_if_cleared(peer); peer_check_if_cleared(peer);
/* Save next revocation hash. */ /* Save next revocation hash. */
proto_to_sha256(r->next_revocation_hash, proto_to_sha256(r->next_revocation_hash,
&peer->them.next_revocation_hash); &peer->remote.next_revocation_hash);
return NULL; return NULL;
} }
@ -793,7 +793,7 @@ Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *acked,
* number of satoshis. * number of satoshis.
*/ */
if ((c->close_fee & 1) if ((c->close_fee & 1)
|| c->close_fee > commit_tx_fee(peer->them.commit->tx, || c->close_fee > commit_tx_fee(peer->remote.commit->tx,
peer->anchor.satoshis)) { peer->anchor.satoshis)) {
return pkt_err(peer, "Invalid close fee"); return pkt_err(peer, "Invalid close fee");
} }
@ -829,7 +829,7 @@ Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *acked,
if (!check_tx_sig(peer->dstate->secpctx, close_tx, 0, if (!check_tx_sig(peer->dstate->secpctx, close_tx, 0,
NULL, 0, NULL, 0,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->them.commitkey, &theirsig)) &peer->remote.commitkey, &theirsig))
return pkt_err(peer, "Invalid signature"); return pkt_err(peer, "Invalid signature");
tal_free(peer->closing.their_sig); tal_free(peer->closing.their_sig);

230
daemon/peer.c

@ -138,7 +138,7 @@ static void peer_breakdown(struct peer *peer)
broadcast_tx(peer, bitcoin_close(peer)); broadcast_tx(peer, bitcoin_close(peer));
/* If we have a signed commit tx (maybe not if we just offered /* If we have a signed commit tx (maybe not if we just offered
* anchor), use it. */ * anchor), use it. */
} else if (peer->us.commit->sig) { } else if (peer->local.commit->sig) {
log_unusual(peer->log, "Peer breakdown: sending commit tx"); log_unusual(peer->log, "Peer breakdown: sending commit tx");
broadcast_tx(peer, bitcoin_commit(peer)); broadcast_tx(peer, bitcoin_commit(peer));
} else } else
@ -258,10 +258,10 @@ static bool committed_to_htlcs(const struct peer *peer)
const struct commit_info *i; const struct commit_info *i;
/* Before anchor exchange, we don't even have cstate. */ /* Before anchor exchange, we don't even have cstate. */
if (!peer->us.commit || !peer->us.commit->cstate) if (!peer->local.commit || !peer->local.commit->cstate)
return false; return false;
i = peer->us.commit; i = peer->local.commit;
while (i && !i->revocation_preimage) { while (i && !i->revocation_preimage) {
if (tal_count(i->cstate->a.htlcs)) if (tal_count(i->cstate->a.htlcs))
return true; return true;
@ -270,7 +270,7 @@ static bool committed_to_htlcs(const struct peer *peer)
i = i->prev; i = i->prev;
} }
i = peer->them.commit; i = peer->remote.commit;
while (i && !i->revocation_preimage) { while (i && !i->revocation_preimage) {
if (tal_count(i->cstate->a.htlcs)) if (tal_count(i->cstate->a.htlcs))
return true; return true;
@ -361,7 +361,7 @@ static struct io_plan *pkt_in(struct io_conn *conn, struct peer *peer)
static void do_anchor_offer(struct peer *peer, void *unused) static void do_anchor_offer(struct peer *peer, void *unused)
{ {
set_current_command(peer, peer->us.offer_anchor, NULL, NULL); set_current_command(peer, peer->local.offer_anchor, NULL, NULL);
} }
/* Crypto is on, we are live. */ /* Crypto is on, we are live. */
@ -369,7 +369,7 @@ static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
{ {
peer_secrets_init(peer); peer_secrets_init(peer);
peer_get_revocation_hash(peer, 0, &peer->us.next_revocation_hash); peer_get_revocation_hash(peer, 0, &peer->local.next_revocation_hash);
assert(peer->state == STATE_INIT); assert(peer->state == STATE_INIT);
@ -421,11 +421,11 @@ static void peer_disconnect(struct io_conn *conn, struct peer *peer)
static void do_commit(struct peer *peer, struct command *jsoncmd) static void do_commit(struct peer *peer, struct command *jsoncmd)
{ {
/* We can have changes we suggested, or changes they suggested. */ /* We can have changes we suggested, or changes they suggested. */
if (peer->them.staging_cstate if (peer->remote.staging_cstate
&& peer->them.commit && peer->remote.commit
&& peer->them.commit->cstate && peer->remote.commit->cstate
&& peer->them.staging_cstate->changes && peer->remote.staging_cstate->changes
!= peer->them.commit->cstate->changes) { != peer->remote.commit->cstate->changes) {
log_debug(peer->log, "do_commit: sending commit command"); log_debug(peer->log, "do_commit: sending commit command");
set_current_command(peer, CMD_SEND_COMMIT, NULL, jsoncmd); set_current_command(peer, CMD_SEND_COMMIT, NULL, jsoncmd);
return; return;
@ -447,7 +447,7 @@ static void try_commit(struct peer *peer)
void their_commit_changed(struct peer *peer) void their_commit_changed(struct peer *peer)
{ {
log_debug(peer->log, "their_commit_changed: changes=%u", log_debug(peer->log, "their_commit_changed: changes=%u",
peer->them.staging_cstate->changes); peer->remote.staging_cstate->changes);
if (!peer->commit_timer) { if (!peer->commit_timer) {
log_debug(peer->log, "their_commit_changed: adding timer"); log_debug(peer->log, "their_commit_changed: adding timer");
peer->commit_timer = new_reltimer(peer->dstate, peer, peer->commit_timer = new_reltimer(peer->dstate, peer,
@ -506,16 +506,16 @@ static struct peer *new_peer(struct lightningd_state *dstate,
peer->fake_close = false; peer->fake_close = false;
io_set_finish(conn, peer_disconnect, peer); io_set_finish(conn, peer_disconnect, peer);
peer->us.offer_anchor = offer_anchor; peer->local.offer_anchor = offer_anchor;
if (!seconds_to_rel_locktime(dstate->config.rel_locktime, if (!seconds_to_rel_locktime(dstate->config.rel_locktime,
&peer->us.locktime)) &peer->local.locktime))
fatal("Invalid locktime configuration %u", fatal("Invalid locktime configuration %u",
dstate->config.rel_locktime); dstate->config.rel_locktime);
peer->us.mindepth = dstate->config.anchor_confirms; peer->local.mindepth = dstate->config.anchor_confirms;
peer->us.commit_fee_rate = dstate->config.commitment_fee_rate; peer->local.commit_fee_rate = dstate->config.commitment_fee_rate;
peer->us.commit = peer->them.commit = NULL; peer->local.commit = peer->remote.commit = NULL;
peer->us.staging_cstate = peer->them.staging_cstate = NULL; peer->local.staging_cstate = peer->remote.staging_cstate = NULL;
/* FIXME: Attach IO logging for this peer. */ /* FIXME: Attach IO logging for this peer. */
tal_add_destructor(peer, destroy_peer); tal_add_destructor(peer, destroy_peer);
@ -746,7 +746,7 @@ static void anchor_depthchange(struct peer *peer, unsigned int depth,
/* Still waiting for it to reach depth? */ /* Still waiting for it to reach depth? */
if (w->depthok != INPUT_NONE) { if (w->depthok != INPUT_NONE) {
if (depth >= peer->us.mindepth) { if (depth >= peer->local.mindepth) {
enum state_input in = w->depthok; enum state_input in = w->depthok;
w->depthok = INPUT_NONE; w->depthok = INPUT_NONE;
/* We don't need the timeout timer any more. */ /* We don't need the timeout timer any more. */
@ -850,10 +850,10 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
htlc = htlc_by_index(ci, i); htlc = htlc_by_index(ci, i);
wscript = bitcoin_redeem_htlc_send(peer, wscript = bitcoin_redeem_htlc_send(peer,
&peer->us.finalkey, &peer->local.finalkey,
&peer->them.finalkey, &peer->remote.finalkey,
&htlc->expiry, &htlc->expiry,
&peer->them.locktime, &peer->remote.locktime,
&ci->revocation_hash, &ci->revocation_hash,
&htlc->rhash); &htlc->rhash);
@ -863,12 +863,12 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
bitcoin_txid(ci->tx, &tx->input[0].txid); bitcoin_txid(ci->tx, &tx->input[0].txid);
satoshis = htlc->msatoshis / 1000; satoshis = htlc->msatoshis / 1000;
tx->input[0].amount = tal_dup(tx->input, u64, &satoshis); tx->input[0].amount = tal_dup(tx->input, u64, &satoshis);
tx->input[0].sequence_number = bitcoin_nsequence(&peer->them.locktime); tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
/* Using a new output address here would be useless: they can tell /* Using a new output address here would be useless: they can tell
* it's our HTLC, and that we collected it via timeout. */ * it's our HTLC, and that we collected it via timeout. */
tx->output[0].script = scriptpubkey_p2sh(tx, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, &peer->us.finalkey)); bitcoin_redeem_single(tx, &peer->local.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script); tx->output[0].script_length = tal_count(tx->output[0].script);
log_unusual(peer->log, "Pre-witness txlen = %zu\n", log_unusual(peer->log, "Pre-witness txlen = %zu\n",
@ -995,9 +995,9 @@ static void resolve_cheating(struct peer *peer)
peer->closing_onchain.resolved[0] = steal_tx; peer->closing_onchain.resolved[0] = steal_tx;
wscripts[n++] wscripts[n++]
= bitcoin_redeem_secret_or_delay(wscripts, = bitcoin_redeem_secret_or_delay(wscripts,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.locktime, &peer->local.locktime,
&peer->us.finalkey, &peer->local.finalkey,
&ci->revocation_hash); &ci->revocation_hash);
} }
@ -1015,19 +1015,19 @@ static void resolve_cheating(struct peer *peer)
if (htlc_a_offered(ci, i)) { if (htlc_a_offered(ci, i)) {
wscripts[n] wscripts[n]
= bitcoin_redeem_htlc_send(wscripts, = bitcoin_redeem_htlc_send(wscripts,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.finalkey, &peer->local.finalkey,
&h->expiry, &h->expiry,
&peer->us.locktime, &peer->local.locktime,
&ci->revocation_hash, &ci->revocation_hash,
&h->rhash); &h->rhash);
} else { } else {
wscripts[n] wscripts[n]
= bitcoin_redeem_htlc_recv(wscripts, = bitcoin_redeem_htlc_recv(wscripts,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.finalkey, &peer->local.finalkey,
&h->expiry, &h->expiry,
&peer->us.locktime, &peer->local.locktime,
&ci->revocation_hash, &ci->revocation_hash,
&h->rhash); &h->rhash);
} }
@ -1144,7 +1144,7 @@ static void our_htlc_depth(struct peer *peer,
/* FIXME: Handle CSV in blocks. */ /* FIXME: Handle CSV in blocks. */
csv_timeout = get_tx_mediantime(peer->dstate, txid) csv_timeout = get_tx_mediantime(peer->dstate, txid)
+ rel_locktime_to_seconds(&peer->them.locktime); + rel_locktime_to_seconds(&peer->remote.locktime);
if (mediantime <= csv_timeout) if (mediantime <= csv_timeout)
return; return;
@ -1289,7 +1289,7 @@ static void our_main_output_depth(struct peer *peer,
/* FIXME: Handle CSV in blocks. */ /* FIXME: Handle CSV in blocks. */
csv_timeout = get_tx_mediantime(peer->dstate, txid) csv_timeout = get_tx_mediantime(peer->dstate, txid)
+ rel_locktime_to_seconds(&peer->them.locktime); + rel_locktime_to_seconds(&peer->remote.locktime);
if (mediantime <= csv_timeout) if (mediantime <= csv_timeout)
return; return;
@ -1509,7 +1509,7 @@ static void anchor_spent(struct peer *peer,
peer->closing_onchain.tx = tal_steal(peer, tx); peer->closing_onchain.tx = tal_steal(peer, tx);
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
peer->closing_onchain.ci = find_commit(peer->them.commit, &txid); peer->closing_onchain.ci = find_commit(peer->remote.commit, &txid);
if (peer->closing_onchain.ci) { if (peer->closing_onchain.ci) {
if (peer->closing_onchain.ci->revocation_preimage) { if (peer->closing_onchain.ci->revocation_preimage) {
set_peer_state(peer, STATE_CLOSE_ONCHAIN_CHEATED, set_peer_state(peer, STATE_CLOSE_ONCHAIN_CHEATED,
@ -1521,11 +1521,11 @@ static void anchor_spent(struct peer *peer,
"anchor_spent"); "anchor_spent");
resolve_their_unilateral(peer); resolve_their_unilateral(peer);
} }
} else if (txidmatch(peer->us.commit->tx, &txid)) { } else if (txidmatch(peer->local.commit->tx, &txid)) {
set_peer_state(peer, set_peer_state(peer,
STATE_CLOSE_ONCHAIN_OUR_UNILATERAL, STATE_CLOSE_ONCHAIN_OUR_UNILATERAL,
"anchor_spent"); "anchor_spent");
peer->closing_onchain.ci = peer->us.commit; peer->closing_onchain.ci = peer->local.commit;
resolve_our_unilateral(peer); resolve_our_unilateral(peer);
} else if (is_mutual_close(peer, tx)) { } else if (is_mutual_close(peer, tx)) {
set_peer_state(peer, set_peer_state(peer,
@ -1606,7 +1606,7 @@ void peer_watch_anchor(struct peer *peer,
if (w->timeout != INPUT_NONE) { if (w->timeout != INPUT_NONE) {
w->timer = new_reltimer(peer->dstate, w, w->timer = new_reltimer(peer->dstate, w,
time_from_sec(7200 time_from_sec(7200
+ 20*peer->us.mindepth), + 20*peer->local.mindepth),
anchor_timeout, w); anchor_timeout, w);
} else } else
w->timer = NULL; w->timer = NULL;
@ -1636,7 +1636,7 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee)
struct channel_state cstate; struct channel_state cstate;
/* We don't need a deep copy here, just fee levels. */ /* We don't need a deep copy here, just fee levels. */
cstate = *peer->us.staging_cstate; cstate = *peer->local.staging_cstate;
if (!force_fee(&cstate, fee)) { if (!force_fee(&cstate, fee)) {
log_unusual(peer->log, log_unusual(peer->log,
"peer_create_close_tx: can't afford fee %"PRIu64, "peer_create_close_tx: can't afford fee %"PRIu64,
@ -1647,10 +1647,10 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee)
log_debug(peer->log, log_debug(peer->log,
"creating close-tx with fee %"PRIu64": to %02x%02x%02x%02x/%02x%02x%02x%02x, amounts %u/%u", "creating close-tx with fee %"PRIu64": to %02x%02x%02x%02x/%02x%02x%02x%02x, amounts %u/%u",
fee, fee,
peer->us.finalkey.der[0], peer->us.finalkey.der[1], peer->local.finalkey.der[0], peer->local.finalkey.der[1],
peer->us.finalkey.der[2], peer->us.finalkey.der[3], peer->local.finalkey.der[2], peer->local.finalkey.der[3],
peer->them.finalkey.der[0], peer->them.finalkey.der[1], peer->remote.finalkey.der[0], peer->remote.finalkey.der[1],
peer->them.finalkey.der[2], peer->them.finalkey.der[3], peer->remote.finalkey.der[2], peer->remote.finalkey.der[3],
cstate.a.pay_msat / 1000, cstate.a.pay_msat / 1000,
cstate.b.pay_msat / 1000); cstate.b.pay_msat / 1000);
@ -1680,7 +1680,7 @@ void peer_calculate_close_fee(struct peer *peer)
* fee of the final commitment transaction, and MUST set * fee of the final commitment transaction, and MUST set
* `close_fee` to an even number of satoshis. * `close_fee` to an even number of satoshis.
*/ */
maxfee = commit_tx_fee(peer->us.commit->tx, peer->anchor.satoshis); maxfee = commit_tx_fee(peer->local.commit->tx, peer->anchor.satoshis);
if (peer->closing.our_fee > maxfee) { if (peer->closing.our_fee > maxfee) {
/* This shouldn't happen: we never accept a commit fee /* This shouldn't happen: we never accept a commit fee
* less than the min_rate, which is greater than the * less than the min_rate, which is greater than the
@ -1741,8 +1741,8 @@ const struct bitcoin_tx *bitcoin_close(struct peer *peer)
= bitcoin_witness_2of2(close_tx->input, = bitcoin_witness_2of2(close_tx->input,
peer->closing.their_sig, peer->closing.their_sig,
&our_close_sig, &our_close_sig,
&peer->them.commitkey, &peer->remote.commitkey,
&peer->us.commitkey); &peer->local.commitkey);
return close_tx; return close_tx;
} }
@ -1751,7 +1751,7 @@ const struct bitcoin_tx *bitcoin_close(struct peer *peer)
const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer) const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
{ {
u8 *witnessscript; u8 *witnessscript;
const struct bitcoin_tx *commit = peer->us.commit->tx; const struct bitcoin_tx *commit = peer->local.commit->tx;
struct bitcoin_signature sig; struct bitcoin_signature sig;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
unsigned int p2wsh_out; unsigned int p2wsh_out;
@ -1759,22 +1759,22 @@ const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
/* The redeemscript for a commit tx is fairly complex. */ /* The redeemscript for a commit tx is fairly complex. */
witnessscript = bitcoin_redeem_secret_or_delay(peer, witnessscript = bitcoin_redeem_secret_or_delay(peer,
&peer->us.finalkey, &peer->local.finalkey,
&peer->them.locktime, &peer->remote.locktime,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.commit->revocation_hash); &peer->local.commit->revocation_hash);
/* Now, create transaction to spend it. */ /* Now, create transaction to spend it. */
tx = bitcoin_tx(peer, 1, 1); tx = bitcoin_tx(peer, 1, 1);
bitcoin_txid(commit, &tx->input[0].txid); bitcoin_txid(commit, &tx->input[0].txid);
p2wsh_out = find_p2wsh_out(commit, witnessscript); p2wsh_out = find_p2wsh_out(commit, witnessscript);
tx->input[0].index = p2wsh_out; tx->input[0].index = p2wsh_out;
tx->input[0].sequence_number = bitcoin_nsequence(&peer->them.locktime); tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
tx->input[0].amount = tal_dup(tx->input, u64, tx->input[0].amount = tal_dup(tx->input, u64,
&commit->output[p2wsh_out].amount); &commit->output[p2wsh_out].amount);
tx->output[0].script = scriptpubkey_p2sh(tx, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, &peer->us.finalkey)); bitcoin_redeem_single(tx, &peer->local.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script); tx->output[0].script_length = tal_count(tx->output[0].script);
/* Witness length can vary, due to DER encoding of sigs, but we /* Witness length can vary, due to DER encoding of sigs, but we
@ -1808,20 +1808,20 @@ const struct bitcoin_tx *bitcoin_commit(struct peer *peer)
struct bitcoin_signature sig; struct bitcoin_signature sig;
/* Can't be signed already, and can't have scriptsig! */ /* Can't be signed already, and can't have scriptsig! */
assert(peer->us.commit->tx->input[0].script_length == 0); assert(peer->local.commit->tx->input[0].script_length == 0);
assert(!peer->us.commit->tx->input[0].witness); assert(!peer->local.commit->tx->input[0].witness);
sig.stype = SIGHASH_ALL; sig.stype = SIGHASH_ALL;
peer_sign_ourcommit(peer, peer->us.commit->tx, &sig.sig); peer_sign_ourcommit(peer, peer->local.commit->tx, &sig.sig);
peer->us.commit->tx->input[0].witness peer->local.commit->tx->input[0].witness
= bitcoin_witness_2of2(peer->us.commit->tx->input, = bitcoin_witness_2of2(peer->local.commit->tx->input,
peer->us.commit->sig, peer->local.commit->sig,
&sig, &sig,
&peer->them.commitkey, &peer->remote.commitkey,
&peer->us.commitkey); &peer->local.commitkey);
return peer->us.commit->tx; return peer->local.commit->tx;
} }
/* Now we can create anchor tx. */ /* Now we can create anchor tx. */
@ -1871,7 +1871,7 @@ static void got_feerate(struct lightningd_state *dstate,
void bitcoin_create_anchor(struct peer *peer, enum state_input done) void bitcoin_create_anchor(struct peer *peer, enum state_input done)
{ {
/* We must be offering anchor for us to try creating it */ /* We must be offering anchor for us to try creating it */
assert(peer->us.offer_anchor); assert(peer->local.offer_anchor);
assert(done == BITCOIN_ANCHOR_CREATED); assert(done == BITCOIN_ANCHOR_CREATED);
bitcoind_estimate_fee(peer->dstate, got_feerate, peer); bitcoind_estimate_fee(peer->dstate, got_feerate, peer);
@ -1896,50 +1896,50 @@ const struct bitcoin_tx *bitcoin_anchor(struct peer *peer)
* insufficient funds. */ * insufficient funds. */
bool setup_first_commit(struct peer *peer) bool setup_first_commit(struct peer *peer)
{ {
assert(!peer->us.commit->tx); assert(!peer->local.commit->tx);
assert(!peer->them.commit->tx); assert(!peer->remote.commit->tx);
/* Revocation hashes already filled in, from pkt_open */ /* Revocation hashes already filled in, from pkt_open */
peer->us.commit->cstate = initial_funding(peer, peer->local.commit->cstate = initial_funding(peer,
peer->us.offer_anchor peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR, == CMD_OPEN_WITH_ANCHOR,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->us.commit_fee_rate); peer->local.commit_fee_rate);
if (!peer->us.commit->cstate) if (!peer->local.commit->cstate)
return false; return false;
peer->them.commit->cstate = initial_funding(peer, peer->remote.commit->cstate = initial_funding(peer,
peer->them.offer_anchor peer->remote.offer_anchor
== CMD_OPEN_WITH_ANCHOR, == CMD_OPEN_WITH_ANCHOR,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->them.commit_fee_rate); peer->remote.commit_fee_rate);
if (!peer->them.commit->cstate) if (!peer->remote.commit->cstate)
return false; return false;
peer->us.commit->tx = create_commit_tx(peer->us.commit, peer->local.commit->tx = create_commit_tx(peer->local.commit,
&peer->us.finalkey, &peer->local.finalkey,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->them.locktime, &peer->remote.locktime,
&peer->anchor.txid, &peer->anchor.txid,
peer->anchor.index, peer->anchor.index,
peer->anchor.satoshis, peer->anchor.satoshis,
&peer->us.commit->revocation_hash, &peer->local.commit->revocation_hash,
peer->us.commit->cstate, peer->local.commit->cstate,
&peer->us.commit->map); &peer->local.commit->map);
peer->them.commit->tx = create_commit_tx(peer->them.commit, peer->remote.commit->tx = create_commit_tx(peer->remote.commit,
&peer->them.finalkey, &peer->remote.finalkey,
&peer->us.finalkey, &peer->local.finalkey,
&peer->us.locktime, &peer->local.locktime,
&peer->anchor.txid, &peer->anchor.txid,
peer->anchor.index, peer->anchor.index,
peer->anchor.satoshis, peer->anchor.satoshis,
&peer->them.commit->revocation_hash, &peer->remote.commit->revocation_hash,
peer->them.commit->cstate, peer->remote.commit->cstate,
&peer->them.commit->map); &peer->remote.commit->map);
peer->us.staging_cstate = copy_funding(peer, peer->us.commit->cstate); peer->local.staging_cstate = copy_funding(peer, peer->local.commit->cstate);
peer->them.staging_cstate = copy_funding(peer, peer->them.commit->cstate); peer->remote.staging_cstate = copy_funding(peer, peer->remote.commit->cstate);
return true; return true;
} }
@ -2003,11 +2003,11 @@ static void json_getpeers(struct command *cmd,
/* FIXME: Report anchor. */ /* FIXME: Report anchor. */
if (!p->us.commit || !p->us.commit->cstate) { if (!p->local.commit || !p->local.commit->cstate) {
json_object_end(response); json_object_end(response);
continue; continue;
} }
last = p->us.commit->cstate; last = p->local.commit->cstate;
json_add_num(response, "our_amount", last->a.pay_msat); json_add_num(response, "our_amount", last->a.pay_msat);
json_add_num(response, "our_fee", last->a.fee_msat); json_add_num(response, "our_fee", last->a.fee_msat);
@ -2017,9 +2017,9 @@ static void json_getpeers(struct command *cmd,
json_add_htlcs(response, "their_htlcs", &last->b); json_add_htlcs(response, "their_htlcs", &last->b);
/* Any changes since then? */ /* Any changes since then? */
if (p->us.staging_cstate->changes != last->changes) if (p->local.staging_cstate->changes != last->changes)
json_add_num(response, "staged_changes", json_add_num(response, "staged_changes",
p->us.staging_cstate->changes p->local.staging_cstate->changes
- last->changes); - last->changes);
json_object_end(response); json_object_end(response);
} }
@ -2061,8 +2061,8 @@ static void check_htlc_expiry(struct peer *peer, void *unused)
/* Check their currently still-existing htlcs for expiry: /* Check their currently still-existing htlcs for expiry:
* We eliminate them from staging as we go. */ * We eliminate them from staging as we go. */
for (i = 0; i < tal_count(peer->them.staging_cstate->a.htlcs); i++) { for (i = 0; i < tal_count(peer->remote.staging_cstate->a.htlcs); i++) {
struct channel_htlc *htlc = &peer->them.staging_cstate->a.htlcs[i]; struct channel_htlc *htlc = &peer->remote.staging_cstate->a.htlcs[i];
/* Not a seconds-based expiry? */ /* Not a seconds-based expiry? */
if (!abs_locktime_is_seconds(&htlc->expiry)) if (!abs_locktime_is_seconds(&htlc->expiry))
@ -2119,8 +2119,8 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc)
* A node MUST NOT add a HTLC if it would result in it * A node MUST NOT add a HTLC if it would result in it
* offering more than 300 HTLCs in either commitment transaction. * offering more than 300 HTLCs in either commitment transaction.
*/ */
if (tal_count(peer->us.staging_cstate->a.htlcs) == 300 if (tal_count(peer->local.staging_cstate->a.htlcs) == 300
|| tal_count(peer->them.staging_cstate->b.htlcs) == 300) { || tal_count(peer->remote.staging_cstate->b.htlcs) == 300) {
command_fail(newhtlc->jsoncmd, "Too many HTLCs"); command_fail(newhtlc->jsoncmd, "Too many HTLCs");
} }
@ -2130,7 +2130,7 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc)
* A node MUST NOT offer `amount_msat` it cannot pay for in * A node MUST NOT offer `amount_msat` it cannot pay for in
* both commitment transactions at the current `fee_rate` * both commitment transactions at the current `fee_rate`
*/ */
cstate = copy_funding(newhtlc, peer->them.staging_cstate); cstate = copy_funding(newhtlc, peer->remote.staging_cstate);
if (!funding_b_add_htlc(cstate, newhtlc->htlc.msatoshis, if (!funding_b_add_htlc(cstate, newhtlc->htlc.msatoshis,
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash, &newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
newhtlc->htlc.id)) { newhtlc->htlc.id)) {
@ -2141,7 +2141,7 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc)
return; return;
} }
cstate = copy_funding(newhtlc, peer->us.staging_cstate); cstate = copy_funding(newhtlc, peer->local.staging_cstate);
if (!funding_a_add_htlc(cstate, newhtlc->htlc.msatoshis, if (!funding_a_add_htlc(cstate, newhtlc->htlc.msatoshis,
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash, &newhtlc->htlc.expiry, &newhtlc->htlc.rhash,
newhtlc->htlc.id)) { newhtlc->htlc.id)) {
@ -2183,7 +2183,7 @@ static void json_newhtlc(struct command *cmd,
return; return;
} }
if (!peer->them.commit || !peer->them.commit->cstate) { if (!peer->remote.commit || !peer->remote.commit->cstate) {
command_fail(cmd, "peer not fully established"); command_fail(cmd, "peer not fully established");
return; return;
} }
@ -2251,11 +2251,11 @@ static size_t find_their_committed_htlc(struct peer *peer,
const struct sha256 *rhash) const struct sha256 *rhash)
{ {
/* Must be in last committed cstate. */ /* Must be in last committed cstate. */
if (funding_find_htlc(&peer->them.commit->cstate->a, rhash) if (funding_find_htlc(&peer->remote.commit->cstate->a, rhash)
== tal_count(peer->them.commit->cstate->a.htlcs)) == tal_count(peer->remote.commit->cstate->a.htlcs))
return tal_count(peer->them.staging_cstate->a.htlcs); return tal_count(peer->remote.staging_cstate->a.htlcs);
return funding_find_htlc(&peer->them.staging_cstate->a, rhash); return funding_find_htlc(&peer->remote.staging_cstate->a, rhash);
} }
struct fulfillhtlc { struct fulfillhtlc {
@ -2276,11 +2276,11 @@ static void do_fullfill(struct peer *peer,
sha256(&rhash, &fulfillhtlc->r, sizeof(fulfillhtlc->r)); sha256(&rhash, &fulfillhtlc->r, sizeof(fulfillhtlc->r));
i = find_their_committed_htlc(peer, &rhash); i = find_their_committed_htlc(peer, &rhash);
if (i == tal_count(peer->them.staging_cstate->a.htlcs)) { if (i == tal_count(peer->remote.staging_cstate->a.htlcs)) {
command_fail(fulfillhtlc->jsoncmd, "preimage htlc not found"); command_fail(fulfillhtlc->jsoncmd, "preimage htlc not found");
return; return;
} }
stage.fulfill.id = peer->them.staging_cstate->a.htlcs[i].id; stage.fulfill.id = peer->remote.staging_cstate->a.htlcs[i].id;
set_htlc_command(peer, fulfillhtlc->jsoncmd, set_htlc_command(peer, fulfillhtlc->jsoncmd,
CMD_SEND_HTLC_FULFILL, &stage); CMD_SEND_HTLC_FULFILL, &stage);
} }
@ -2306,7 +2306,7 @@ static void json_fulfillhtlc(struct command *cmd,
return; return;
} }
if (!peer->them.commit || !peer->them.commit->cstate) { if (!peer->remote.commit || !peer->remote.commit->cstate) {
command_fail(cmd, "peer not fully established"); command_fail(cmd, "peer not fully established");
return; return;
} }
@ -2347,14 +2347,14 @@ static void do_failhtlc(struct peer *peer,
stage.fail.fail = HTLC_FAIL; stage.fail.fail = HTLC_FAIL;
/* Look in peer->them.staging_cstate->a, as that's where we'll /* Look in peer->remote.staging_cstate->a, as that's where we'll
* immediately remove it from: avoids double-handling. */ * immediately remove it from: avoids double-handling. */
i = find_their_committed_htlc(peer, &failhtlc->rhash); i = find_their_committed_htlc(peer, &failhtlc->rhash);
if (i == tal_count(peer->them.staging_cstate->a.htlcs)) { if (i == tal_count(peer->remote.staging_cstate->a.htlcs)) {
command_fail(failhtlc->jsoncmd, "htlc not found"); command_fail(failhtlc->jsoncmd, "htlc not found");
return; return;
} }
stage.fail.id = peer->them.staging_cstate->a.htlcs[i].id; stage.fail.id = peer->remote.staging_cstate->a.htlcs[i].id;
set_htlc_command(peer, failhtlc->jsoncmd, CMD_SEND_HTLC_FAIL, &stage); set_htlc_command(peer, failhtlc->jsoncmd, CMD_SEND_HTLC_FAIL, &stage);
} }
@ -2380,7 +2380,7 @@ static void json_failhtlc(struct command *cmd,
return; return;
} }
if (!peer->them.commit || !peer->them.commit->cstate) { if (!peer->remote.commit || !peer->remote.commit->cstate) {
command_fail(cmd, "peer not fully established"); command_fail(cmd, "peer not fully established");
return; return;
} }
@ -2427,7 +2427,7 @@ static void json_commit(struct command *cmd,
return; return;
} }
if (!peer->them.commit || !peer->them.commit->cstate) { if (!peer->remote.commit || !peer->remote.commit->cstate) {
command_fail(cmd, "peer not fully established"); command_fail(cmd, "peer not fully established");
return; return;
} }
@ -2535,7 +2535,7 @@ static void json_signcommit(struct command *cmd,
return; return;
} }
if (!peer->us.commit->sig) { if (!peer->local.commit->sig) {
command_fail(cmd, "Peer has not given us a signature"); command_fail(cmd, "Peer has not given us a signature");
return; return;
} }

4
daemon/peer.h

@ -223,12 +223,12 @@ struct peer {
bool fake_close; bool fake_close;
/* Stuff we have in common. */ /* Stuff we have in common. */
struct peer_visible_state us, them; struct peer_visible_state local, remote;
}; };
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum); void setup_listeners(struct lightningd_state *dstate, unsigned int portnum);
/* Populates very first peer->{us,them}.commit->{tx,cstate} */ /* Populates very first peer->{local,remote}.commit->{tx,cstate} */
bool setup_first_commit(struct peer *peer); bool setup_first_commit(struct peer *peer);
void set_peer_state(struct peer *peer, enum state newstate, const char *why); void set_peer_state(struct peer *peer, enum state newstate, const char *why);

16
daemon/secrets.c

@ -51,7 +51,7 @@ void peer_sign_theircommit(const struct peer *peer,
NULL, 0, NULL, 0,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->us.commitkey, &peer->local.commitkey,
sig); sig);
} }
@ -65,7 +65,7 @@ void peer_sign_ourcommit(const struct peer *peer,
NULL, 0, NULL, 0,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->us.commitkey, &peer->local.commitkey,
sig); sig);
} }
@ -80,7 +80,7 @@ void peer_sign_spend(const struct peer *peer,
NULL, 0, NULL, 0,
commit_witnessscript, commit_witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->us.finalkey, &peer->local.finalkey,
sig); sig);
} }
@ -95,7 +95,7 @@ void peer_sign_htlc_refund(const struct peer *peer,
NULL, 0, NULL, 0,
htlc_witnessscript, htlc_witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->us.finalkey, &peer->local.finalkey,
sig); sig);
} }
@ -108,7 +108,7 @@ void peer_sign_mutual_close(const struct peer *peer,
NULL, 0, NULL, 0,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->us.commitkey, &peer->local.commitkey,
sig); sig);
} }
@ -124,7 +124,7 @@ void peer_sign_steal_input(const struct peer *peer,
NULL, 0, NULL, 0,
witnessscript, witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->us.finalkey, &peer->local.finalkey,
sig); sig);
} }
@ -142,8 +142,8 @@ void peer_secrets_init(struct peer *peer)
{ {
peer->secrets = tal(peer, struct peer_secrets); peer->secrets = tal(peer, struct peer_secrets);
new_keypair(peer->dstate, &peer->secrets->commit, &peer->us.commitkey); new_keypair(peer->dstate, &peer->secrets->commit, &peer->local.commitkey);
new_keypair(peer->dstate, &peer->secrets->final, &peer->us.finalkey); new_keypair(peer->dstate, &peer->secrets->final, &peer->local.finalkey);
if (RAND_bytes(peer->secrets->revocation_seed.u.u8, if (RAND_bytes(peer->secrets->revocation_seed.u.u8,
sizeof(peer->secrets->revocation_seed.u.u8)) != 1) sizeof(peer->secrets->revocation_seed.u.u8)) != 1)
fatal("Could not get random bytes for revocation seed"); fatal("Could not get random bytes for revocation seed");

Loading…
Cancel
Save