diff --git a/daemon/packets.c b/daemon/packets.c index 91ac80077..48067e144 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -11,6 +11,7 @@ #include "secrets.h" #include "state.h" #include +#include #include #include #include @@ -46,7 +47,7 @@ static Pkt *make_pkt(const tal_t *ctx, Pkt__PktCase type, const void *msg) pkt__init(pkt); pkt->pkt_case = type; /* This is a union, so doesn't matter which we assign. */ - pkt->error = (Error *)tal_steal(ctx, msg); + pkt->error = (Error *)tal_steal(pkt, msg); /* This makes sure all packets are valid. */ #ifndef NDEBUG @@ -67,14 +68,28 @@ static Pkt *make_pkt(const tal_t *ctx, Pkt__PktCase type, const void *msg) return pkt; } -Pkt *pkt_open(const tal_t *ctx, const struct peer *peer, - OpenChannel__AnchorOffer anchor) +static void queue_raw_pkt(struct peer *peer, Pkt *pkt) { - OpenChannel *o = tal(ctx, OpenChannel); + size_t n = tal_count(peer->outpkt); + tal_resize(&peer->outpkt, n+1); + peer->outpkt[n] = pkt; + + /* In case it was waiting for output. */ + io_wake(peer); +} + +static void queue_pkt(struct peer *peer, Pkt__PktCase type, const void *msg) +{ + queue_raw_pkt(peer, make_pkt(peer, type, msg)); +} + +void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor) +{ + OpenChannel *o = tal(peer, OpenChannel); open_channel__init(o); - o->revocation_hash = sha256_to_proto(ctx, &peer->us.revocation_hash); - o->next_revocation_hash = sha256_to_proto(ctx, &peer->us.next_revocation_hash); + o->revocation_hash = sha256_to_proto(o, &peer->us.revocation_hash); + o->next_revocation_hash = sha256_to_proto(o, &peer->us.next_revocation_hash); o->commit_key = pubkey_to_proto(o, &peer->us.commitkey); o->final_key = pubkey_to_proto(o, &peer->us.finalkey); o->delay = tal(o, Locktime); @@ -91,13 +106,13 @@ Pkt *pkt_open(const tal_t *ctx, const struct peer *peer, o->anch = anchor; o->min_depth = peer->us.mindepth; - return make_pkt(ctx, PKT__PKT_OPEN, o); + queue_pkt(peer, PKT__PKT_OPEN, o); } - -Pkt *pkt_anchor(const tal_t *ctx, const struct peer *peer) + +void queue_pkt_anchor(struct peer *peer) { struct signature sig; - OpenAnchor *a = tal(ctx, OpenAnchor); + OpenAnchor *a = tal(peer, OpenAnchor); open_anchor__init(a); a->txid = sha256_to_proto(a, &peer->anchor.txid.sha); @@ -108,13 +123,13 @@ Pkt *pkt_anchor(const tal_t *ctx, const struct peer *peer) peer_sign_theircommit(peer, peer->them.commit, &sig); a->commit_sig = signature_to_proto(a, &sig); - return make_pkt(ctx, PKT__PKT_OPEN_ANCHOR, a); + queue_pkt(peer, PKT__PKT_OPEN_ANCHOR, a); } -Pkt *pkt_open_commit_sig(const tal_t *ctx, const struct peer *peer) +void queue_pkt_open_commit_sig(struct peer *peer) { struct signature sig; - OpenCommitSig *s = tal(ctx, OpenCommitSig); + OpenCommitSig *s = tal(peer, OpenCommitSig); open_commit_sig__init(s); @@ -124,21 +139,21 @@ Pkt *pkt_open_commit_sig(const tal_t *ctx, const struct peer *peer) peer_sign_theircommit(peer, peer->them.commit, &sig); s->sig = signature_to_proto(s, &sig); - return make_pkt(ctx, PKT__PKT_OPEN_COMMIT_SIG, s); + queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s); } -Pkt *pkt_open_complete(const tal_t *ctx, const struct peer *peer) +void queue_pkt_open_complete(struct peer *peer) { - OpenComplete *o = tal(ctx, OpenComplete); + OpenComplete *o = tal(peer, OpenComplete); open_complete__init(o); - return make_pkt(ctx, PKT__PKT_OPEN_COMPLETE, o); + queue_pkt(peer, PKT__PKT_OPEN_COMPLETE, o); } -Pkt *pkt_htlc_add(const tal_t *ctx, const struct peer *peer, +void queue_pkt_htlc_add(struct peer *peer, const struct htlc_progress *htlc_prog) { - UpdateAddHtlc *u = tal(ctx, UpdateAddHtlc); + UpdateAddHtlc *u = tal(peer, UpdateAddHtlc); update_add_htlc__init(u); assert(htlc_prog->stage.type == HTLC_ADD); @@ -148,13 +163,13 @@ Pkt *pkt_htlc_add(const tal_t *ctx, const struct peer *peer, u->r_hash = sha256_to_proto(u, &htlc_prog->stage.add.htlc.rhash); u->expiry = abs_locktime_to_proto(u, &htlc_prog->stage.add.htlc.expiry); - return make_pkt(ctx, PKT__PKT_UPDATE_ADD_HTLC, u); + queue_pkt(peer, PKT__PKT_UPDATE_ADD_HTLC, u); } -Pkt *pkt_htlc_fulfill(const tal_t *ctx, const struct peer *peer, +void queue_pkt_htlc_fulfill(struct peer *peer, const struct htlc_progress *htlc_prog) { - UpdateFulfillHtlc *f = tal(ctx, UpdateFulfillHtlc); + UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc); update_fulfill_htlc__init(f); assert(htlc_prog->stage.type == HTLC_FULFILL); @@ -162,13 +177,13 @@ Pkt *pkt_htlc_fulfill(const tal_t *ctx, const struct peer *peer, f->revocation_hash = sha256_to_proto(f, &htlc_prog->our_revocation_hash); f->r = sha256_to_proto(f, &htlc_prog->stage.fulfill.r); - return make_pkt(ctx, PKT__PKT_UPDATE_FULFILL_HTLC, f); + queue_pkt(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f); } -Pkt *pkt_htlc_fail(const tal_t *ctx, const struct peer *peer, +void queue_pkt_htlc_fail(struct peer *peer, const struct htlc_progress *htlc_prog) { - UpdateFailHtlc *f = tal(ctx, UpdateFailHtlc); + UpdateFailHtlc *f = tal(peer, UpdateFailHtlc); const struct channel_htlc *htlc; update_fail_htlc__init(f); @@ -178,12 +193,12 @@ Pkt *pkt_htlc_fail(const tal_t *ctx, const struct peer *peer, f->revocation_hash = sha256_to_proto(f, &htlc_prog->our_revocation_hash); f->r_hash = sha256_to_proto(f, &htlc->rhash); - return make_pkt(ctx, PKT__PKT_UPDATE_FAIL_HTLC, f); + queue_pkt(peer, PKT__PKT_UPDATE_FAIL_HTLC, f); } -Pkt *pkt_update_accept(const tal_t *ctx, const struct peer *peer) +void queue_pkt_update_accept(struct peer *peer) { - UpdateAccept *u = tal(ctx, UpdateAccept); + UpdateAccept *u = tal(peer, UpdateAccept); const struct htlc_progress *cur = peer->current_htlc; struct signature sig; @@ -195,12 +210,12 @@ Pkt *pkt_update_accept(const tal_t *ctx, const struct peer *peer) u->revocation_hash = sha256_to_proto(u, &cur->our_revocation_hash); - return make_pkt(ctx, PKT__PKT_UPDATE_ACCEPT, u); + queue_pkt(peer, PKT__PKT_UPDATE_ACCEPT, u); } -Pkt *pkt_update_signature(const tal_t *ctx, const struct peer *peer) +void queue_pkt_update_signature(struct peer *peer) { - UpdateSignature *u = tal(ctx, UpdateSignature); + UpdateSignature *u = tal(peer, UpdateSignature); const struct htlc_progress *cur = peer->current_htlc; struct signature sig; struct sha256 preimage; @@ -213,12 +228,12 @@ Pkt *pkt_update_signature(const tal_t *ctx, const struct peer *peer) peer_get_revocation_preimage(peer, peer->commit_tx_counter-1, &preimage); u->revocation_preimage = sha256_to_proto(u, &preimage); - return make_pkt(ctx, PKT__PKT_UPDATE_SIGNATURE, u); + queue_pkt(peer, PKT__PKT_UPDATE_SIGNATURE, u); } -Pkt *pkt_update_complete(const tal_t *ctx, const struct peer *peer) +void queue_pkt_update_complete(struct peer *peer) { - UpdateComplete *u = tal(ctx, UpdateComplete); + UpdateComplete *u = tal(peer, UpdateComplete); struct sha256 preimage; update_complete__init(u); @@ -227,90 +242,94 @@ Pkt *pkt_update_complete(const tal_t *ctx, const struct peer *peer) peer_get_revocation_preimage(peer, peer->commit_tx_counter-1, &preimage); u->revocation_preimage = sha256_to_proto(u, &preimage); - return make_pkt(ctx, PKT__PKT_UPDATE_COMPLETE, u); + queue_pkt(peer, PKT__PKT_UPDATE_COMPLETE, u); } -Pkt *pkt_err(const tal_t *ctx, const char *msg, ...) +Pkt *pkt_err(struct peer *peer, const char *msg, ...) { - Error *e = tal(ctx, Error); + Error *e = tal(peer, Error); va_list ap; error__init(e); va_start(ap, msg); - e->problem = tal_vfmt(ctx, msg, ap); + e->problem = tal_vfmt(e, msg, ap); va_end(ap); - return make_pkt(ctx, PKT__PKT_ERROR, e); + return make_pkt(peer, PKT__PKT_ERROR, e); } -Pkt *pkt_close_clearing(const tal_t *ctx, const struct peer *peer) +void queue_pkt_err(struct peer *peer, Pkt *err) { - CloseClearing *c = tal(ctx, CloseClearing); + queue_raw_pkt(peer, err); +} + +void queue_pkt_close_clearing(struct peer *peer) +{ + CloseClearing *c = tal(peer, CloseClearing); close_clearing__init(c); - return make_pkt(ctx, PKT__PKT_CLOSE_CLEARING, c); + queue_pkt(peer, PKT__PKT_CLOSE_CLEARING, c); } -Pkt *pkt_close_signature(const tal_t *ctx, const struct peer *peer) +void queue_pkt_close_signature(struct peer *peer) { - CloseSignature *c = tal(ctx, CloseSignature); + CloseSignature *c = tal(peer, CloseSignature); struct bitcoin_tx *close_tx; struct signature our_close_sig; close_signature__init(c); - close_tx = peer_create_close_tx(ctx, peer, peer->closing.our_fee); + close_tx = peer_create_close_tx(peer, peer->closing.our_fee); 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; - return make_pkt(ctx, PKT__PKT_CLOSE_SIGNATURE, c); + queue_pkt(peer, PKT__PKT_CLOSE_SIGNATURE, c); } -Pkt *pkt_err_unexpected(const tal_t *ctx, const Pkt *pkt) +Pkt *pkt_err_unexpected(struct peer *peer, const Pkt *pkt) { - return pkt_err(ctx, "Unexpected packet %s", state_name(pkt->pkt_case)); + return pkt_err(peer, "Unexpected packet %s", state_name(pkt->pkt_case)); } /* Process various packets: return an error packet on failure. */ -Pkt *accept_pkt_open(const tal_t *ctx, - struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt) { struct rel_locktime locktime; const OpenChannel *o = pkt->open; if (!proto_to_rel_locktime(o->delay, &locktime)) - return pkt_err(ctx, "Invalid delay"); + return pkt_err(peer, "Invalid delay"); /* FIXME: handle blocks in locktime */ if (o->delay->locktime_case != LOCKTIME__LOCKTIME_SECONDS) - return pkt_err(ctx, "Delay in blocks not accepted"); + return pkt_err(peer, "Delay in blocks not accepted"); if (o->delay->seconds > peer->dstate->config.rel_locktime_max) - return pkt_err(ctx, "Delay too great"); + return pkt_err(peer, "Delay too great"); if (o->min_depth > peer->dstate->config.anchor_confirms_max) - return pkt_err(ctx, "min_depth too great"); + return pkt_err(peer, "min_depth too great"); if (o->initial_fee_rate < peer->dstate->config.commitment_fee_rate_min) - return pkt_err(ctx, "Commitment fee rate too low"); + return pkt_err(peer, "Commitment fee rate too low"); if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) peer->them.offer_anchor = CMD_OPEN_WITH_ANCHOR; else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR) peer->them.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR; else - return pkt_err(ctx, "Unknown offer anchor value"); + return pkt_err(peer, "Unknown offer anchor value"); if (peer->them.offer_anchor == peer->us.offer_anchor) - return pkt_err(ctx, "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)) - return pkt_err(ctx, "Malformed locktime"); + return pkt_err(peer, "Malformed locktime"); peer->them.mindepth = o->min_depth; peer->them.commit_fee_rate = o->initial_fee_rate; if (!proto_to_pubkey(peer->dstate->secpctx, o->commit_key, &peer->them.commitkey)) - return pkt_err(ctx, "Bad commitkey"); + return pkt_err(peer, "Bad commitkey"); if (!proto_to_pubkey(peer->dstate->secpctx, o->final_key, &peer->them.finalkey)) - return pkt_err(ctx, "Bad finalkey"); + return pkt_err(peer, "Bad finalkey"); proto_to_sha256(o->revocation_hash, &peer->them.revocation_hash); proto_to_sha256(o->next_revocation_hash, &peer->them.next_revocation_hash); @@ -321,9 +340,7 @@ Pkt *accept_pkt_open(const tal_t *ctx, return NULL; } -Pkt *accept_pkt_anchor(const tal_t *ctx, - struct peer *peer, - const Pkt *pkt) +Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt) { const OpenAnchor *a = pkt->open_anchor; @@ -341,7 +358,7 @@ Pkt *accept_pkt_anchor(const tal_t *ctx, peer->anchor.satoshis, peer->us.commit_fee_rate); if (!peer->cstate) - return pkt_err(ctx, "Insufficient funds for fee"); + return pkt_err(peer, "Insufficient funds for fee"); /* Now we can make initial (unsigned!) commit txs. */ make_commit_txs(peer, peer, @@ -353,7 +370,7 @@ Pkt *accept_pkt_anchor(const tal_t *ctx, peer->cur_commit.theirsig.stype = SIGHASH_ALL; if (!proto_to_signature(a->commit_sig, &peer->cur_commit.theirsig.sig)) - return pkt_err(ctx, "Malformed signature"); + return pkt_err(peer, "Malformed signature"); /* Their sig should sign our commit tx. */ if (!check_tx_sig(peer->dstate->secpctx, @@ -362,19 +379,18 @@ Pkt *accept_pkt_anchor(const tal_t *ctx, tal_count(peer->anchor.redeemscript), &peer->them.commitkey, &peer->cur_commit.theirsig)) - return pkt_err(ctx, "Bad signature"); + return pkt_err(peer, "Bad signature"); return NULL; } -Pkt *accept_pkt_open_commit_sig(const tal_t *ctx, - 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; peer->cur_commit.theirsig.stype = SIGHASH_ALL; if (!proto_to_signature(s->sig, &peer->cur_commit.theirsig.sig)) - return pkt_err(ctx, "Malformed signature"); + return pkt_err(peer, "Malformed signature"); dump_tx("Checking sig for:", peer->us.commit); dump_key("Using key:", &peer->them.commitkey); @@ -386,13 +402,12 @@ Pkt *accept_pkt_open_commit_sig(const tal_t *ctx, tal_count(peer->anchor.redeemscript), &peer->them.commitkey, &peer->cur_commit.theirsig)) - return pkt_err(ctx, "Bad signature"); + return pkt_err(peer, "Bad signature"); return NULL; } -Pkt *accept_pkt_open_complete(const tal_t *ctx, - struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt) { return NULL; } @@ -409,8 +424,7 @@ static Pkt *decline_htlc(const tal_t *ctx, const char *why) return make_pkt(ctx, PKT__PKT_UPDATE_DECLINE_HTLC, d); } -Pkt *accept_pkt_htlc_add(const tal_t *ctx, - struct peer *peer, const Pkt *pkt, +Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt, Pkt **decline) { const UpdateAddHtlc *u = pkt->update_add_htlc; @@ -422,26 +436,26 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx, proto_to_sha256(u->r_hash, &cur->stage.add.htlc.rhash); proto_to_sha256(u->revocation_hash, &cur->their_revocation_hash); if (!proto_to_abs_locktime(u->expiry, &cur->stage.add.htlc.expiry)) { - err = pkt_err(ctx, "Invalid HTLC expiry"); + err = pkt_err(peer, "Invalid HTLC expiry"); goto fail; } /* FIXME: Handle block-based expiry! */ if (!abs_locktime_is_seconds(&cur->stage.add.htlc.expiry)) { - *decline = decline_htlc(ctx, + *decline = decline_htlc(peer, "HTLC expiry in blocks not supported!"); goto decline; } if (abs_locktime_to_seconds(&cur->stage.add.htlc.expiry) < controlled_time().ts.tv_sec + peer->dstate->config.min_expiry) { - *decline = decline_htlc(ctx, "HTLC expiry too soon!"); + *decline = decline_htlc(peer, "HTLC expiry too soon!"); goto decline; } if (abs_locktime_to_seconds(&cur->stage.add.htlc.expiry) > controlled_time().ts.tv_sec + peer->dstate->config.max_expiry) { - *decline = decline_htlc(ctx, "HTLC expiry too far!"); + *decline = decline_htlc(peer, "HTLC expiry too far!"); goto decline; } @@ -450,7 +464,7 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx, cur->stage.add.htlc.msatoshis, &cur->stage.add.htlc.expiry, &cur->stage.add.htlc.rhash, 0)) { - err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis", + err = pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis", cur->stage.add.htlc.msatoshis); goto fail; } @@ -484,7 +498,7 @@ decline: return NULL; }; -Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt) { const UpdateFailHtlc *f = pkt->update_fail_htlc; struct htlc_progress *cur = tal(peer, struct htlc_progress); @@ -497,7 +511,7 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt) i = funding_find_htlc(&peer->cstate->a, &rhash); if (i == tal_count(peer->cstate->a.htlcs)) { - err = pkt_err(ctx, "Unknown HTLC"); + err = pkt_err(peer, "Unknown HTLC"); goto fail; } @@ -527,8 +541,7 @@ fail: return err; } -Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx, - struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt) { const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc; struct htlc_progress *cur = tal(peer, struct htlc_progress); @@ -543,7 +556,7 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx, sha256(&rhash, &cur->stage.fulfill.r, sizeof(cur->stage.fulfill.r)); i = funding_find_htlc(&peer->cstate->a, &rhash); if (i == tal_count(peer->cstate->a.htlcs)) { - err = pkt_err(ctx, "Unknown HTLC"); + err = pkt_err(peer, "Unknown HTLC"); goto fail; } cur->stage.fulfill.index = i; @@ -611,8 +624,7 @@ static void update_to_new_htlcs(struct peer *peer) peer->commit_tx_counter++; } -Pkt *accept_pkt_update_accept(const tal_t *ctx, - struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_update_accept(struct peer *peer, const Pkt *pkt) { const UpdateAccept *a = pkt->update_accept; struct htlc_progress *cur = peer->current_htlc; @@ -621,7 +633,7 @@ Pkt *accept_pkt_update_accept(const tal_t *ctx, cur->their_sig.stype = SIGHASH_ALL; if (!proto_to_signature(a->sig, &cur->their_sig.sig)) - return pkt_err(ctx, "Malformed signature"); + return pkt_err(peer, "Malformed signature"); /* Now we can make commit tx pair. */ make_commit_txs(cur, peer, &cur->our_revocation_hash, @@ -636,7 +648,7 @@ Pkt *accept_pkt_update_accept(const tal_t *ctx, tal_count(peer->anchor.redeemscript), &peer->them.commitkey, &cur->their_sig)) - return pkt_err(ctx, "Bad signature"); + return pkt_err(peer, "Bad signature"); /* Our next step will be to send the revocation preimage, so * update to new HTLC now so we never use the old one. */ @@ -653,15 +665,13 @@ static bool check_preimage(const Sha256Hash *preimage, const struct sha256 *hash return structeq(&h, hash); } -Pkt *accept_pkt_update_complete(const tal_t *ctx, - struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_update_complete(struct peer *peer, const Pkt *pkt) { /* FIXME: Check preimage against old tx! */ return NULL; } -Pkt *accept_pkt_update_signature(const tal_t *ctx, - struct peer *peer, +Pkt *accept_pkt_update_signature(struct peer *peer, const Pkt *pkt) { const UpdateSignature *s = pkt->update_signature; @@ -669,7 +679,7 @@ Pkt *accept_pkt_update_signature(const tal_t *ctx, cur->their_sig.stype = SIGHASH_ALL; if (!proto_to_signature(s->sig, &cur->their_sig.sig)) - return pkt_err(ctx, "Malformed signature"); + return pkt_err(peer, "Malformed signature"); /* Their sig should sign our new commit tx. */ if (!check_tx_sig(peer->dstate->secpctx, @@ -678,11 +688,11 @@ Pkt *accept_pkt_update_signature(const tal_t *ctx, tal_count(peer->anchor.redeemscript), &peer->them.commitkey, &cur->their_sig)) - return pkt_err(ctx, "Bad signature"); + return pkt_err(peer, "Bad signature"); /* Check their revocation preimage. */ if (!check_preimage(s->revocation_preimage, &peer->them.revocation_hash)) - return pkt_err(ctx, "Bad revocation preimage"); + return pkt_err(peer, "Bad revocation preimage"); /* Our next step will be to send the revocation preimage, so * update to new HTLC now so we never use the old one. */ @@ -690,14 +700,13 @@ Pkt *accept_pkt_update_signature(const tal_t *ctx, return NULL; } -Pkt *accept_pkt_close_clearing(const tal_t *ctx, struct peer *peer, const Pkt *pkt) +Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt) { /* FIXME: Reject unknown odd fields? */ return NULL; } -Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt, - bool *matches) +Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches) { const CloseSignature *c = pkt->close_signature; struct bitcoin_tx *close_tx; @@ -712,7 +721,7 @@ Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt, if ((c->close_fee & 1) || c->close_fee > commit_tx_fee(peer->them.commit, peer->anchor.satoshis)) { - return pkt_err(ctx, "Invalid close fee"); + return pkt_err(peer, "Invalid close fee"); } /* FIXME: Don't accept tiny fee at all? */ @@ -726,10 +735,10 @@ Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt, /* We want more, they should give more. */ if (peer->closing.our_fee > peer->closing.their_fee) { if (c->close_fee <= peer->closing.their_fee) - return pkt_err(ctx, "Didn't increase close fee"); + return pkt_err(peer, "Didn't increase close fee"); } else { if (c->close_fee >= peer->closing.their_fee) - return pkt_err(ctx, "Didn't decrease close fee"); + return pkt_err(peer, "Didn't decrease close fee"); } } @@ -739,14 +748,14 @@ Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt, * transaction, and MUST fail the connection if it is not. */ theirsig.stype = SIGHASH_ALL; if (!proto_to_signature(c->sig, &theirsig.sig)) - return pkt_err(ctx, "Invalid signature format"); + return pkt_err(peer, "Invalid signature format"); - close_tx = peer_create_close_tx(ctx, peer, c->close_fee); + close_tx = peer_create_close_tx(peer, c->close_fee); if (!check_tx_sig(peer->dstate->secpctx, close_tx, 0, peer->anchor.redeemscript, tal_count(peer->anchor.redeemscript), &peer->them.commitkey, &theirsig)) - return pkt_err(ctx, "Invalid signature"); + return pkt_err(peer, "Invalid signature"); tal_free(peer->closing.their_sig); peer->closing.their_sig = tal_dup(peer, diff --git a/daemon/peer.c b/daemon/peer.c index ee471c852..07b35ad3f 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -66,16 +66,6 @@ static struct peer *find_peer(struct lightningd_state *dstate, return NULL; } -static void queue_output_pkt(struct peer *peer, Pkt *pkt) -{ - size_t n = tal_count(peer->outpkt); - tal_resize(&peer->outpkt, n+1); - peer->outpkt[n] = pkt; - - /* In case it was waiting for output. */ - io_wake(peer); -} - static struct json_result *null_response(const tal_t *ctx) { struct json_result *response; @@ -123,10 +113,10 @@ static void state_single(struct peer *peer, const union input *idata) { enum command_status status; - Pkt *outpkt; const struct bitcoin_tx *broadcast; - - status = state(peer, peer, input, idata, &outpkt, &broadcast); + size_t old_outpkts = tal_count(peer->outpkt); + + status = state(peer, input, idata, &broadcast); log_debug(peer->log, "%s => %s", input_name(input), state_name(peer->state)); switch (status) { @@ -145,9 +135,9 @@ static void state_single(struct peer *peer, break; } - if (outpkt) { + if (tal_count(peer->outpkt) > old_outpkts) { + Pkt *outpkt = peer->outpkt[old_outpkts]; log_add(peer->log, " (out %s)", input_name(outpkt->pkt_case)); - queue_output_pkt(peer, outpkt); } if (broadcast) { struct sha256_double txid; @@ -298,7 +288,6 @@ static void destroy_peer(struct peer *peer) static void peer_disconnect(struct io_conn *conn, struct peer *peer) { - Pkt *outpkt; const struct bitcoin_tx *broadcast; log_info(peer->log, "Disconnected"); @@ -316,8 +305,7 @@ static void peer_disconnect(struct io_conn *conn, struct peer *peer) if (peer->cond == PEER_CLOSED) return; - state(peer, peer, INPUT_CONNECTION_LOST, NULL, &outpkt, &broadcast); - assert(!outpkt); + state(peer, INPUT_CONNECTION_LOST, NULL, &broadcast); if (broadcast) { struct sha256_double txid; @@ -858,8 +846,7 @@ void peer_watch_tx(struct peer *peer, add_commit_tx_watch(tx, peer, &txid, spend_tx_done, int2ptr(done)); } -struct bitcoin_tx *peer_create_close_tx(const tal_t *ctx, - const struct peer *peer, u64 fee) +struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee) { struct channel_state cstate; @@ -882,7 +869,7 @@ struct bitcoin_tx *peer_create_close_tx(const tal_t *ctx, cstate.a.pay_msat / 1000, cstate.b.pay_msat / 1000); - return create_close_tx(peer->dstate->secpctx, ctx, + return create_close_tx(peer->dstate->secpctx, peer, &peer->us.finalkey, &peer->them.finalkey, &peer->anchor.txid, @@ -1064,12 +1051,12 @@ void peer_watch_htlcs_cleared(struct peer *peer, } /* Create a bitcoin close tx, using last signature they sent. */ -const struct bitcoin_tx *bitcoin_close(const tal_t *ctx, struct peer *peer) +const struct bitcoin_tx *bitcoin_close(struct peer *peer) { struct bitcoin_tx *close_tx; struct bitcoin_signature our_close_sig; - close_tx = peer_create_close_tx(ctx, peer, peer->closing.their_fee); + close_tx = peer_create_close_tx(peer, peer->closing.their_fee); our_close_sig.stype = SIGHASH_ALL; peer_sign_mutual_close(peer, close_tx, &our_close_sig.sig); @@ -1088,8 +1075,7 @@ const struct bitcoin_tx *bitcoin_close(const tal_t *ctx, struct peer *peer) } /* Create a bitcoin spend tx (to spend our commit's outputs) */ -const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx, - const struct peer *peer) +const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer) { u8 *redeemscript, *linear; const struct bitcoin_tx *commit = peer->us.commit; @@ -1098,14 +1084,14 @@ const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx, unsigned int p2sh_out; /* The redeemscript for a commit tx is fairly complex. */ - redeemscript = bitcoin_redeem_secret_or_delay(ctx, + redeemscript = bitcoin_redeem_secret_or_delay(peer, &peer->us.finalkey, &peer->them.locktime, &peer->them.finalkey, &peer->us.revocation_hash); /* Now, create transaction to spend it. */ - tx = bitcoin_tx(ctx, 1, 1); + tx = bitcoin_tx(peer, 1, 1); bitcoin_txid(commit, &tx->input[0].txid); p2sh_out = find_p2sh_out(commit, redeemscript); tx->input[0].index = p2sh_out; @@ -1130,9 +1116,10 @@ const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx, /* Now, calculate the fee, given length. */ /* FIXME: Dynamic fees! */ - linear = linearize_tx(ctx, tx); + linear = linearize_tx(peer, tx); tx->fee = fee_by_feerate(tal_count(linear), peer->dstate->config.closing_fee_rate); + tal_free(linear); /* FIXME: Fail gracefully in these cases (not worth collecting) */ if (tx->fee > tx->output[0].amount @@ -1154,23 +1141,21 @@ const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx, } /* Create a bitcoin spend tx (to spend their commit's outputs) */ -const struct bitcoin_tx *bitcoin_spend_theirs(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_spend_theirs(const struct peer *peer, const struct bitcoin_event *btc) { FIXME_STUB(peer); } /* Create a bitcoin steal tx (to steal all their commit's outputs) */ -const struct bitcoin_tx *bitcoin_steal(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_steal(const struct peer *peer, struct bitcoin_event *btc) { FIXME_STUB(peer); } /* Sign and return our commit tx */ -const struct bitcoin_tx *bitcoin_commit(const tal_t *ctx, struct peer *peer) +const struct bitcoin_tx *bitcoin_commit(struct peer *peer) { struct bitcoin_signature sig; @@ -1193,16 +1178,14 @@ const struct bitcoin_tx *bitcoin_commit(const tal_t *ctx, struct peer *peer) } /* Create a HTLC refund collection */ -const struct bitcoin_tx *bitcoin_htlc_timeout(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_htlc_timeout(const struct peer *peer, const struct htlc *htlc) { FIXME_STUB(peer); } /* Create a HTLC collection */ -const struct bitcoin_tx *bitcoin_htlc_spend(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_htlc_spend(const struct peer *peer, const struct htlc *htlc) { FIXME_STUB(peer); @@ -1270,7 +1253,7 @@ void bitcoin_release_anchor(struct peer *peer, enum state_input done) } /* Get the bitcoin anchor tx. */ -const struct bitcoin_tx *bitcoin_anchor(const tal_t *ctx, struct peer *peer) +const struct bitcoin_tx *bitcoin_anchor(struct peer *peer) { return peer->anchor.tx; } diff --git a/daemon/peer.h b/daemon/peer.h index bcf882fc4..b0ae5c9eb 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -184,8 +184,7 @@ void make_commit_txs(const tal_t *ctx, void peer_add_htlc_expiry(struct peer *peer, const struct abs_locktime *expiry); -struct bitcoin_tx *peer_create_close_tx(const tal_t *ctx, - const struct peer *peer, u64 fee); +struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee); uint64_t commit_tx_fee(const struct bitcoin_tx *commit, uint64_t anchor_satoshis); diff --git a/state.c b/state.c index 8ded5c28a..896230c44 100644 --- a/state.c +++ b/state.c @@ -78,13 +78,6 @@ static void complete_cmd(struct peer *peer, enum command_status *statusp, *statusp = status; } -static void queue_pkt(Pkt **out, Pkt *pkt) -{ - assert(!*out); - assert(pkt); - *out = pkt; -} - static void queue_tx_broadcast(const struct bitcoin_tx **broadcast, const struct bitcoin_tx *tx) { @@ -93,11 +86,9 @@ static void queue_tx_broadcast(const struct bitcoin_tx **broadcast, *broadcast = tx; } -enum command_status state(const tal_t *ctx, - struct peer *peer, +enum command_status state(struct peer *peer, const enum state_input input, const union input *idata, - Pkt **out, const struct bitcoin_tx **broadcast) { Pkt *decline; @@ -105,7 +96,6 @@ enum command_status state(const tal_t *ctx, Pkt *err; enum command_status cstatus = CMD_NONE; - *out = NULL; *broadcast = NULL; switch (peer->state) { @@ -114,24 +104,22 @@ enum command_status state(const tal_t *ctx, */ case STATE_INIT: if (input_is(input, CMD_OPEN_WITH_ANCHOR)) { - queue_pkt(out, - pkt_open(ctx, peer, - OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)); + queue_pkt_open(peer, + OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR); change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); return next_state(peer, cstatus, STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR); } else if (input_is(input, CMD_OPEN_WITHOUT_ANCHOR)) { change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); - queue_pkt(out, - pkt_open(ctx, peer, - OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)); + queue_pkt_open(peer, + OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR); return next_state(peer, cstatus, STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR); } break; case STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR: if (input_is(input, PKT_OPEN)) { - err = accept_pkt_open(ctx, peer, idata->pkt); + err = accept_pkt_open(peer, idata->pkt); if (err) { complete_cmd(peer, &cstatus, CMD_FAIL); goto err_close_nocleanup; @@ -148,7 +136,7 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR: if (input_is(input, PKT_OPEN)) { - err = accept_pkt_open(ctx, peer, idata->pkt); + err = accept_pkt_open(peer, idata->pkt); if (err) { complete_cmd(peer, &cstatus, CMD_FAIL); goto err_close_nocleanup; @@ -167,7 +155,7 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAIT_FOR_ANCHOR_CREATE: if (input_is(input, BITCOIN_ANCHOR_CREATED)) { - queue_pkt(out, pkt_anchor(ctx, peer)); + queue_pkt_anchor(peer); return next_state(peer, cstatus, STATE_OPEN_WAIT_FOR_COMMIT_SIG); } else if (input_is(input, CMD_CLOSE) @@ -183,13 +171,12 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAIT_FOR_ANCHOR: if (input_is(input, PKT_OPEN_ANCHOR)) { - err = accept_pkt_anchor(ctx, peer, idata->pkt); + err = accept_pkt_anchor(peer, idata->pkt); if (err) { complete_cmd(peer, &cstatus, CMD_FAIL); goto err_close_nocleanup; } - queue_pkt(out, - pkt_open_commit_sig(ctx, peer)); + queue_pkt_open_commit_sig(peer); peer_watch_anchor(peer, BITCOIN_ANCHOR_DEPTHOK, BITCOIN_ANCHOR_TIMEOUT, @@ -210,13 +197,13 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAIT_FOR_COMMIT_SIG: if (input_is(input, PKT_OPEN_COMMIT_SIG)) { - err = accept_pkt_open_commit_sig(ctx, peer, idata->pkt); + err = accept_pkt_open_commit_sig(peer, idata->pkt); if (err) { bitcoin_release_anchor(peer, INPUT_NONE); complete_cmd(peer, &cstatus, CMD_FAIL); goto err_start_unilateral_close; } - queue_tx_broadcast(broadcast, bitcoin_anchor(ctx, peer)); + queue_tx_broadcast(broadcast, bitcoin_anchor(peer)); peer_watch_anchor(peer, BITCOIN_ANCHOR_DEPTHOK, INPUT_NONE, @@ -238,7 +225,7 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAITING_OURANCHOR: if (input_is(input, PKT_OPEN_COMPLETE)) { - err = accept_pkt_open_complete(ctx, peer, idata->pkt); + err = accept_pkt_open_complete(peer, idata->pkt); if (err) { complete_cmd(peer, &cstatus, CMD_FAIL); /* We no longer care about anchor depth. */ @@ -253,8 +240,7 @@ enum command_status state(const tal_t *ctx, /* Fall thru */ case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED: if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { - queue_pkt(out, - pkt_open_complete(ctx, peer)); + queue_pkt_open_complete(peer); if (peer->state == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED) { complete_cmd(peer, &cstatus, CMD_SUCCESS); return next_state(peer, cstatus, @@ -307,7 +293,7 @@ enum command_status state(const tal_t *ctx, break; case STATE_OPEN_WAITING_THEIRANCHOR: if (input_is(input, PKT_OPEN_COMPLETE)) { - err = accept_pkt_open_complete(ctx, peer, idata->pkt); + err = accept_pkt_open_complete(peer, idata->pkt); if (err) { complete_cmd(peer, &cstatus, CMD_FAIL); /* We no longer care about anchor depth. */ @@ -323,12 +309,10 @@ enum command_status state(const tal_t *ctx, case STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED: if (input_is(input, BITCOIN_ANCHOR_TIMEOUT)) { /* Anchor didn't reach blockchain in reasonable time. */ - queue_pkt(out, - pkt_err(ctx, "Anchor timed out")); + queue_pkt_err(peer, pkt_err(peer, "Anchor timed out")); return next_state(peer, cstatus, STATE_ERR_ANCHOR_TIMEOUT); } else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { - queue_pkt(out, - pkt_open_complete(ctx, peer)); + queue_pkt_open_complete(peer); if (peer->state == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED) { complete_cmd(peer, &cstatus, CMD_SUCCESS); return next_state(peer, cstatus, @@ -427,24 +411,19 @@ enum command_status state(const tal_t *ctx, assert(peer->cond == PEER_CMD_OK); if (input_is(input, CMD_SEND_HTLC_ADD)) { /* We are to send an HTLC update. */ - queue_pkt(out, - pkt_htlc_add(ctx, peer, idata->htlc_prog)); + queue_pkt_htlc_add(peer, idata->htlc_prog); change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_HTLC_ACCEPT)); } else if (input_is(input, CMD_SEND_HTLC_FULFILL)) { /* We are to send an HTLC fulfill. */ - queue_pkt(out, - pkt_htlc_fulfill(ctx, peer, - idata->htlc_prog)); + queue_pkt_htlc_fulfill(peer, idata->htlc_prog); change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT)); } else if (input_is(input, CMD_SEND_HTLC_FAIL)) { /* We are to send an HTLC fail. */ - queue_pkt(out, - pkt_htlc_fail(ctx, peer, - idata->htlc_prog)); + queue_pkt_htlc_fail(peer, idata->htlc_prog); change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT)); @@ -520,14 +499,13 @@ enum command_status state(const tal_t *ctx, change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY); goto accept_htlc_fail; } else if (input_is(input, PKT_UPDATE_ACCEPT)) { - err = accept_pkt_update_accept(ctx, peer, idata->pkt); + err = accept_pkt_update_accept(peer, idata->pkt); if (err) { peer_htlc_aborted(peer); complete_cmd(peer, &cstatus, CMD_FAIL); goto err_start_unilateral_close; } - queue_pkt(out, - pkt_update_signature(ctx, peer)); + queue_pkt_update_signature(peer); /* HTLC is signed (though old tx not revoked yet!) */ return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_COMPLETE)); @@ -564,7 +542,7 @@ enum command_status state(const tal_t *ctx, case STATE_WAIT_FOR_UPDATE_COMPLETE_LOWPRIO: case STATE_WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO: if (input_is(input, PKT_UPDATE_COMPLETE)) { - err = accept_pkt_update_complete(ctx, peer, idata->pkt); + err = accept_pkt_update_complete(peer, idata->pkt); if (err) { peer_htlc_aborted(peer); complete_cmd(peer, &cstatus, CMD_FAIL); @@ -607,13 +585,12 @@ enum command_status state(const tal_t *ctx, case STATE_WAIT_FOR_UPDATE_SIG_LOWPRIO: case STATE_WAIT_FOR_UPDATE_SIG_HIGHPRIO: if (input_is(input, PKT_UPDATE_SIGNATURE)) { - err = accept_pkt_update_signature(ctx, peer, idata->pkt); + err = accept_pkt_update_signature(peer, idata->pkt); if (err) { peer_htlc_aborted(peer); goto err_start_unilateral_close; } - queue_pkt(out, - pkt_update_complete(ctx, peer)); + queue_pkt_update_complete(peer); peer_htlc_done(peer); change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK); @@ -646,7 +623,7 @@ enum command_status state(const tal_t *ctx, case STATE_US_CLEARING: /* This is their reply once they're clearing too. */ if (input_is(input, PKT_CLOSE_CLEARING)) { - err = accept_pkt_close_clearing(ctx, peer, idata->pkt); + err = accept_pkt_close_clearing(peer, idata->pkt); if (err) goto err_start_unilateral_close; @@ -658,7 +635,7 @@ enum command_status state(const tal_t *ctx, /* FIXME: We must continue to allow fulfill & fail! */ } else if (input_is(input, CMD_SEND_HTLC_FAIL) || input_is(input, CMD_SEND_HTLC_FULFILL)) { - err = pkt_err(ctx, "FIXME: cmd during clearing."); + err = pkt_err(peer, "FIXME: cmd during clearing."); goto err_start_unilateral_close; } else if (input_is(input, INPUT_CONNECTION_LOST)) { goto start_unilateral_close; @@ -672,7 +649,7 @@ enum command_status state(const tal_t *ctx, goto start_closing_cleared; } else if (input_is(input, CMD_SEND_HTLC_FAIL) || input_is(input, CMD_SEND_HTLC_FULFILL)) { - err = pkt_err(ctx, "FIXME: cmd during clearing."); + err = pkt_err(peer, "FIXME: cmd during clearing."); goto err_start_unilateral_close; } else if (input_is(input, INPUT_CONNECTION_LOST)) { goto start_unilateral_close; @@ -684,8 +661,7 @@ enum command_status state(const tal_t *ctx, case STATE_WAIT_FOR_CLOSE_SIG: if (input_is(input, PKT_CLOSE_SIGNATURE)) { bool matches; - err = accept_pkt_close_sig(ctx, peer, idata->pkt, - &matches); + err = accept_pkt_close_sig(peer, idata->pkt, &matches); if (err) goto err_start_unilateral_close; @@ -696,7 +672,7 @@ enum command_status state(const tal_t *ctx, /* Send close TX. */ queue_tx_broadcast(broadcast, - bitcoin_close(ctx, peer)); + bitcoin_close(peer)); change_peer_cond(peer, PEER_CLOSING, PEER_CLOSED); return next_state(peer, cstatus, @@ -704,12 +680,12 @@ enum command_status state(const tal_t *ctx, } /* Offer the new fee. */ - queue_pkt(out, pkt_close_signature(ctx, peer)); + queue_pkt_close_signature(peer); return unchanged_state(cstatus); } else if (input_is(input, INPUT_CONNECTION_LOST)) { goto start_unilateral_close; } else if (input_is(input, INPUT_CLOSE_COMPLETE_TIMEOUT)) { - err = pkt_err(ctx, "Close timed out"); + err = pkt_err(peer, "Close timed out"); goto err_start_unilateral_close; } else if (input_is_pkt(input)) { goto unexpected_pkt; @@ -796,7 +772,7 @@ enum command_status state(const tal_t *ctx, && input_is(input, BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED)) { BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS) & STATE_CLOSE_OURCOMMIT_BIT)); - tx = bitcoin_spend_ours(ctx, peer); + tx = bitcoin_spend_ours(peer); /* Now we need to wait for our commit to be done. */ queue_tx_broadcast(broadcast, tx); peer_watch_tx(peer, tx, BITCOIN_SPEND_OURS_DONE); @@ -829,9 +805,7 @@ enum command_status state(const tal_t *ctx, INPUT_NO_MORE_HTLCS); return unchanged_state(cstatus); } else if (input_is(input, BITCOIN_HTLC_TOTHEM_TIMEOUT)){ - tx = bitcoin_htlc_timeout(ctx, - peer, - idata->htlc); + tx = bitcoin_htlc_timeout(peer, idata->htlc); /* HTLC timed out, spend it back to us. */ queue_tx_broadcast(broadcast, tx); /* Don't unwatch yet; they could yet @@ -842,7 +816,7 @@ enum command_status state(const tal_t *ctx, BITCOIN_HTLC_RETURN_SPEND_DONE); return unchanged_state(cstatus); } else if (input_is(input, INPUT_RVALUE)) { - tx = bitcoin_htlc_spend(ctx, peer, + tx = bitcoin_htlc_spend(peer, idata->htlc); /* Spend it... */ @@ -889,7 +863,7 @@ enum command_status state(const tal_t *ctx, * (even if they already have, due to tx malleability). */ if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) { - tx = bitcoin_spend_theirs(ctx, peer, idata->btc); + tx = bitcoin_spend_theirs(peer, idata->btc); queue_tx_broadcast(broadcast, tx); peer_watch_tx(peer, tx, BITCOIN_SPEND_THEIRS_DONE); /* HTLC watches: if any, set HTLCs bit. */ @@ -903,7 +877,7 @@ enum command_status state(const tal_t *ctx, return next_state_bits(peer, cstatus, bits); /* This can happen multiple times: need to steal ALL */ } else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) { - tx = bitcoin_steal(ctx, peer, idata->btc); + tx = bitcoin_steal(peer, idata->btc); if (!tx) return next_state(peer, cstatus, STATE_ERR_INFORMATION_LEAK); @@ -947,7 +921,7 @@ unexpected_pkt: if (input_is(input, PKT_ERROR)) { goto start_unilateral_close; } - err = pkt_err_unexpected(ctx, idata->pkt); + err = pkt_err_unexpected(peer, idata->pkt); goto err_start_unilateral_close; unexpected_pkt_nocleanup: @@ -958,7 +932,7 @@ unexpected_pkt_nocleanup: if (input_is(input, PKT_ERROR)) { goto close_nocleanup; } - err = pkt_err_unexpected(ctx, idata->pkt); + err = pkt_err_unexpected(peer, idata->pkt); goto err_close_nocleanup; anchor_unspent: @@ -974,7 +948,7 @@ err_close_nocleanup: * Something went wrong, but we haven't sent anything to the blockchain * so there's nothing to clean up. */ - queue_pkt(out, err); + queue_pkt_err(peer, err); close_nocleanup: change_peer_cond(peer, PEER_CMD_OK, PEER_CLOSED); @@ -984,7 +958,7 @@ err_start_unilateral_close: /* * They timed out, or were broken; we are going to close unilaterally. */ - queue_pkt(out, err); + queue_pkt_err(peer, err); start_unilateral_close: /* @@ -1000,11 +974,11 @@ start_unilateral_close: * timelocked. */ if (peer_has_close_sig(peer)) { - queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer)); + queue_tx_broadcast(broadcast, bitcoin_close(peer)); return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE); } - tx = bitcoin_commit(ctx, peer); + tx = bitcoin_commit(peer); queue_tx_broadcast(broadcast, tx); peer_watch_delayed(peer, tx, BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED); @@ -1024,11 +998,11 @@ them_unilateral: /* * Bitcoind tells us they did unilateral close. */ - queue_pkt(out, pkt_err(ctx, "Commit tx noticed")); + queue_pkt_err(peer, pkt_err(peer, "Commit tx noticed")); /* No more inputs, no more commands. */ set_peer_cond(peer, PEER_CLOSED); - tx = bitcoin_spend_theirs(ctx, peer, idata->btc); + tx = bitcoin_spend_theirs(peer, idata->btc); queue_tx_broadcast(broadcast, tx); peer_watch_tx(peer, tx, BITCOIN_SPEND_THEIRS_DONE); @@ -1044,11 +1018,11 @@ them_unilateral: return next_state(peer, cstatus, STATE_CLOSE_WAIT_SPENDTHEM); accept_htlc_add: - err = accept_pkt_htlc_add(ctx, peer, idata->pkt, &decline); + err = accept_pkt_htlc_add(peer, idata->pkt, &decline); if (err) goto err_start_unilateral_close; if (decline) { - queue_pkt(out, decline); + queue_pkt_err(peer, decline); peer_htlc_declined(peer, decline); /* No update means no priority change. */ change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK); @@ -1056,23 +1030,23 @@ accept_htlc_add: return next_state_nocheck(peer, cstatus, prio(peer->state, STATE_NORMAL)); } - queue_pkt(out, pkt_update_accept(ctx, peer)); + queue_pkt_update_accept(peer); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG)); accept_htlc_fail: - err = accept_pkt_htlc_fail(ctx, peer, idata->pkt); + err = accept_pkt_htlc_fail(peer, idata->pkt); if (err) goto err_start_unilateral_close; - queue_pkt(out, pkt_update_accept(ctx, peer)); + queue_pkt_update_accept(peer); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG)); accept_htlc_fulfill: - err = accept_pkt_htlc_fulfill(ctx, peer, idata->pkt); + err = accept_pkt_htlc_fulfill(peer, idata->pkt); if (err) goto err_start_unilateral_close; - queue_pkt(out, pkt_update_accept(ctx, peer)); + queue_pkt_update_accept(peer); return next_state(peer, cstatus, prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG)); @@ -1080,7 +1054,7 @@ start_clearing: /* * Start a mutual close: tell them we want to clear. */ - queue_pkt(out, pkt_close_clearing(ctx, peer)); + queue_pkt_close_clearing(peer); /* No more commands, we're already closing. */ set_peer_cond(peer, PEER_CLOSING); @@ -1091,11 +1065,11 @@ start_closing_cleared: /* As soon as we send packet, they could close. */ peer_calculate_close_fee(peer); peer_watch_close(peer, BITCOIN_CLOSE_DONE, INPUT_CLOSE_COMPLETE_TIMEOUT); - queue_pkt(out, pkt_close_signature(ctx, peer)); + queue_pkt_close_signature(peer); return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_SIG); accept_clearing: - err = accept_pkt_close_clearing(ctx, peer, idata->pkt); + err = accept_pkt_close_clearing(peer, idata->pkt); if (err) goto err_start_unilateral_close; @@ -1106,7 +1080,7 @@ accept_clearing: set_peer_cond(peer, PEER_CLOSING); /* Tell them we're clearing too. */ - queue_pkt(out, pkt_close_clearing(ctx, peer)); + queue_pkt_close_clearing(peer); return next_state(peer, cstatus, STATE_BOTH_CLEARING); @@ -1127,13 +1101,13 @@ old_commit_spotted: /* * bitcoind reported a broadcast of the not-latest commit tx. */ - queue_pkt(out, pkt_err(ctx, "Otherspend noticed")); + queue_pkt_err(peer, pkt_err(peer, "Otherspend noticed")); /* No more packets, no more commands. */ set_peer_cond(peer, PEER_CLOSED); /* If we can't find it, we're lost. */ - tx = bitcoin_steal(ctx, peer, idata->btc); + tx = bitcoin_steal(peer, idata->btc); if (!tx) return next_state(peer, cstatus, STATE_ERR_INFORMATION_LEAK); diff --git a/state.h b/state.h index 87f6ec2a6..3a227074b 100644 --- a/state.h +++ b/state.h @@ -35,11 +35,9 @@ union input { struct htlc_progress *htlc_prog; }; -enum command_status state(const tal_t *ctx, - struct peer *peer, +enum command_status state(struct peer *peer, const enum state_input input, const union input *idata, - Pkt **out, const struct bitcoin_tx **broadcast); /* Any CMD_SEND_HTLC_* */ @@ -95,64 +93,51 @@ void peer_htlc_aborted(struct peer *peer); const struct htlc *peer_tx_revealed_r_value(struct peer *peer, const struct bitcoin_event *btc); -/* Create various kinds of packets, allocated off @ctx */ -Pkt *pkt_open(const tal_t *ctx, const struct peer *peer, - OpenChannel__AnchorOffer anchor); -Pkt *pkt_anchor(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_open_commit_sig(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_open_complete(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_htlc_add(const tal_t *ctx, const struct peer *peer, - const struct htlc_progress *htlc_prog); -Pkt *pkt_htlc_fulfill(const tal_t *ctx, const struct peer *peer, - const struct htlc_progress *htlc_prog); -Pkt *pkt_htlc_fail(const tal_t *ctx, const struct peer *peer, +/* Send various kinds of packets */ +void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor); +void queue_pkt_anchor(struct peer *peer); +void queue_pkt_open_commit_sig(struct peer *peer); +void queue_pkt_open_complete(struct peer *peer); +void queue_pkt_htlc_add(struct peer *peer, const struct htlc_progress *htlc_prog); -Pkt *pkt_update_accept(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_update_signature(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_update_complete(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_err(const tal_t *ctx, const char *fmt, ...); -Pkt *pkt_close_clearing(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_close_signature(const tal_t *ctx, const struct peer *peer); -Pkt *pkt_err_unexpected(const tal_t *ctx, const Pkt *pkt); +void queue_pkt_htlc_fulfill(struct peer *peer, + const struct htlc_progress *htlc_prog); +void queue_pkt_htlc_fail(struct peer *peer, + const struct htlc_progress *htlc_prog); +void queue_pkt_update_accept(struct peer *peer); +void queue_pkt_update_signature(struct peer *peer); +void queue_pkt_update_complete(struct peer *peer); +void queue_pkt_close_clearing(struct peer *peer); +void queue_pkt_close_signature(struct peer *peer); + +Pkt *pkt_err(struct peer *peer, const char *msg, ...); +void queue_pkt_err(struct peer *peer, Pkt *err); +Pkt *pkt_err_unexpected(struct peer *peer, const Pkt *pkt); /* Process various packets: return an error packet on failure. */ -Pkt *accept_pkt_open(const tal_t *ctx, - struct peer *peer, - const Pkt *pkt); +Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_anchor(const tal_t *ctx, - struct peer *peer, - const Pkt *pkt); +Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_open_commit_sig(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_open_complete(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_htlc_add(const tal_t *ctx, - struct peer *peer, const Pkt *pkt, +Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt, Pkt **decline); -Pkt *accept_pkt_htlc_fail(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_update_accept(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_update_accept(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_update_complete(const tal_t *ctx, - struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_update_complete(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_update_signature(const tal_t *ctx, - struct peer *peer, - const Pkt *pkt); +Pkt *accept_pkt_update_signature(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_close_clearing(const tal_t *ctx, struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt, - bool *matches); +Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt); +Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches); /** * committed_to_htlcs: do we have any locked-in HTLCs? @@ -358,36 +343,31 @@ void bitcoin_create_anchor(struct peer *peer, enum state_input done); void bitcoin_release_anchor(struct peer *peer, enum state_input done); /* Get the bitcoin anchor tx. */ -const struct bitcoin_tx *bitcoin_anchor(const tal_t *ctx, struct peer *peer); +const struct bitcoin_tx *bitcoin_anchor(struct peer *peer); /* Create a bitcoin close tx. */ -const struct bitcoin_tx *bitcoin_close(const tal_t *ctx, struct peer *peer); +const struct bitcoin_tx *bitcoin_close(struct peer *peer); /* Create a bitcoin spend tx (to spend our commit's outputs) */ -const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx, - const struct peer *peer); +const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer); /* Create a bitcoin spend tx (to spend their commit's outputs) */ -const struct bitcoin_tx *bitcoin_spend_theirs(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_spend_theirs(const struct peer *peer, const struct bitcoin_event *btc); /* Create a bitcoin steal tx (to steal all their commit's outputs) */ -const struct bitcoin_tx *bitcoin_steal(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_steal(const struct peer *peer, struct bitcoin_event *btc); /* Create our commit tx */ -const struct bitcoin_tx *bitcoin_commit(const tal_t *ctx, struct peer *peer); +const struct bitcoin_tx *bitcoin_commit(struct peer *peer); /* Create a HTLC refund collection */ -const struct bitcoin_tx *bitcoin_htlc_timeout(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_htlc_timeout(const struct peer *peer, const struct htlc *htlc); /* Create a HTLC collection */ -const struct bitcoin_tx *bitcoin_htlc_spend(const tal_t *ctx, - const struct peer *peer, +const struct bitcoin_tx *bitcoin_htlc_spend(const struct peer *peer, const struct htlc *htlc); #endif /* LIGHTNING_STATE_H */