From 0e07cc7a361b6f8fc3175fb90c2267007c0b824a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 11 Apr 2016 16:30:43 +0930 Subject: [PATCH] daemon: fix close fee negotiation. We always set *matches to false (outside the branch, oops). We also distinguish the case where we ack from the case where they acked, which removes a FIXME and makes it work. Signed-off-by: Rusty Russell --- daemon/packets.c | 21 ++++++++++++++++----- state.c | 16 ++++++++++++---- state.h | 3 ++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index 0262ca4ef..3acc51ecc 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -405,6 +405,8 @@ void queue_pkt_close_signature(struct peer *peer) 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; + log_info(peer->log, "queue_pkt_close_signature: offered close fee %" + PRIu64, c->close_fee); queue_pkt(peer, PKT__PKT_CLOSE_SIGNATURE, c); } @@ -748,12 +750,17 @@ Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt) return NULL; } -Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches) +Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *acked, + bool *we_agree) { const CloseSignature *c = pkt->close_signature; struct bitcoin_tx *close_tx; struct bitcoin_signature theirsig; + log_info(peer->log, "accept_pkt_close_sig: they offered close fee %" + PRIu64, c->close_fee); + *acked = *we_agree = false; + /* BOLT #2: * * The sender MUST set `close_fee` lower than or equal to the fee of the @@ -805,7 +812,8 @@ Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches) peer->closing.their_fee = c->close_fee; if (peer->closing.our_fee == peer->closing.their_fee) { - *matches = true; + log_info(peer->log, "accept_pkt_close_sig: That's an ack"); + *acked = true; } else { /* Adjust our fee to close on their fee. */ u64 sum; @@ -817,10 +825,13 @@ Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches) if (peer->closing.our_fee & 1) peer->closing.our_fee++; - /* FIXME: Fees may *now* be equal, and they'll - * consider this an ACK! */ + log_info(peer->log, "accept_pkt_close_sig: we change to %"PRIu64, + peer->closing.our_fee); + + /* Corner case: we may now agree with them. */ + if (peer->closing.our_fee == peer->closing.their_fee) + *we_agree = true; } - *matches = false; /* FIXME: Dynamic fee! */ return NULL; diff --git a/state.c b/state.c index 41aa5fb34..a77bc56e1 100644 --- a/state.c +++ b/state.c @@ -516,13 +516,21 @@ enum command_status state(struct peer *peer, break; case STATE_WAIT_FOR_CLOSE_SIG: if (input_is(input, PKT_CLOSE_SIGNATURE)) { - bool matches; - err = accept_pkt_close_sig(peer, idata->pkt, &matches); + bool acked, we_agree; + err = accept_pkt_close_sig(peer, idata->pkt, + &acked, &we_agree); if (err) goto err_start_unilateral_close; - /* Did they offer the same fee we did? */ - if (matches) { + /* Are we about to offer the same fee they did? */ + if (we_agree) { + /* Offer the new fee. */ + queue_pkt_close_signature(peer); + acked = true; + } + + /* Do fees now match? */ + if (acked) { peer_unwatch_close_timeout(peer, INPUT_CLOSE_COMPLETE_TIMEOUT); diff --git a/state.h b/state.h index ff12d1899..0772f90d9 100644 --- a/state.h +++ b/state.h @@ -110,7 +110,8 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt); -Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, bool *matches); +Pkt *accept_pkt_close_sig(struct peer *peer, const Pkt *pkt, + bool *acked, bool *we_agree); /** * committed_to_htlcs: do we have any locked-in HTLCs?