From bccd10c6d86e20157aa360c16f68ff41850f320d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 3 May 2016 11:03:20 +0930 Subject: [PATCH] daemon: don't code check for cleared commits in the main loop, use pending queue. Signed-off-by: Rusty Russell --- daemon/packets.c | 6 ++++++ daemon/peer.c | 22 +++++++++++++++------- daemon/peer.h | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index 26ff5b808..690eec534 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -320,6 +320,8 @@ void queue_pkt_commit(struct peer *peer) /* Switch to the new commitment. */ peer->them.commit = ci; + peer_check_if_cleared(peer); + /* Now send message */ update_commit__init(u); u->sig = signature_to_proto(u, &ci->sig.sig); @@ -348,6 +350,7 @@ void queue_pkt_revocation(struct peer *peer) = tal(peer->us.commit->prev, struct sha256); peer_get_revocation_preimage(peer, peer->commit_tx_counter-1, peer->us.commit->prev->revocation_preimage); + peer_check_if_cleared(peer); u->revocation_preimage = sha256_to_proto(u, peer->us.commit->prev->revocation_preimage); @@ -708,6 +711,8 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt) peer->commit_tx_counter++; peer_get_revocation_hash(peer, peer->commit_tx_counter + 1, &peer->us.next_revocation_hash); + + peer_check_if_cleared(peer); return NULL; } @@ -736,6 +741,7 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt) proto_to_sha256(r->revocation_preimage, peer->them.commit->prev->revocation_preimage); + peer_check_if_cleared(peer); /* Save next revocation hash. */ proto_to_sha256(r->next_revocation_hash, diff --git a/daemon/peer.c b/daemon/peer.c index dfabcd4ad..1476429bb 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -216,7 +216,7 @@ static void queue_cmd_(struct peer *peer, try_command(peer); }; -static void UNNEEDED queue_input(struct peer *peer, +static void queue_input(struct peer *peer, enum state_input input, const union input *idata) { @@ -266,12 +266,6 @@ static void state_event(struct peer *peer, state_single(peer, input, idata); - if (peer->cleared != INPUT_NONE && !committed_to_htlcs(peer)) { - enum state_input all_done = peer->cleared; - peer->cleared = INPUT_NONE; - state_single(peer, all_done, NULL); - } - pend = list_pop(&peer->pending_input, struct pending_input, list); if (pend) { state_event(peer, pend->input, &pend->idata); @@ -281,6 +275,18 @@ static void state_event(struct peer *peer, try_command(peer); } +void peer_check_if_cleared(struct peer *peer) +{ + if (peer->cleared == INPUT_NONE) + return; + + if (committed_to_htlcs(peer)) + return; + + queue_input(peer, peer->cleared, NULL); + peer->cleared = INPUT_NONE; +} + static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer) { struct out_pkt out; @@ -1128,6 +1134,8 @@ void peer_watch_htlcs_cleared(struct peer *peer, assert(peer->cleared == INPUT_NONE); assert(all_done != INPUT_NONE); peer->cleared = all_done; + + peer_check_if_cleared(peer); } /* Create a bitcoin close tx, using last signature they sent. */ diff --git a/daemon/peer.h b/daemon/peer.h index e3cdf32c9..23e818faa 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -209,6 +209,9 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum); /* Populates very first peer->{us,them}.commit->{tx,cstate} */ bool setup_first_commit(struct peer *peer); +/* Call this after commit changes, or revocation accepted/sent. */ +void peer_check_if_cleared(struct peer *peer); + void peer_add_htlc_expiry(struct peer *peer, const struct abs_locktime *expiry);