From a5d4a3eb2c9ac7fff438f53c4d0c01c594cce739 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 23 Aug 2017 10:24:54 +0930 Subject: [PATCH] lightningd: derive num_revocations_received. It's in the shachain, so storing it is completely redundant. We leave it in for the moment so we can assert() that nothing has changed. Signed-off-by: Rusty Russell --- lightningd/derive_basepoints.h | 12 ++++++++++-- lightningd/peer_control.c | 20 ++++++++++++++++++-- lightningd/peer_htlcs.c | 6 ++++-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lightningd/derive_basepoints.h b/lightningd/derive_basepoints.h index 061b607a6..53714e85c 100644 --- a/lightningd/derive_basepoints.h +++ b/lightningd/derive_basepoints.h @@ -3,6 +3,8 @@ #include "config.h" #include #include +#include +#include struct sha256; @@ -60,7 +62,13 @@ bool per_commit_point(const struct sha256 *shaseed, */ static inline u64 shachain_index(u64 per_commit_index) { - assert(per_commit_index < (1ULL << 48)); - return 281474976710655ULL - per_commit_index; + BUILD_ASSERT((1ULL << SHACHAIN_BITS)-1 == 281474976710655); + assert(per_commit_index < (1ULL << SHACHAIN_BITS)); + return (1ULL << SHACHAIN_BITS)-1 - per_commit_index; +} + +static inline u64 revocations_received(const struct shachain *shachain) +{ + return (1ULL << SHACHAIN_BITS) - (shachain_next_index(shachain) + 1); } #endif /* LIGHTNING_LIGHTNINGD_DERIVE_BASEPOINTS_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 26b53d995..0f849de8e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1487,6 +1487,7 @@ static void peer_start_closingd(struct peer *peer, const tal_t *tmpctx = tal_tmpctx(peer); u8 *initmsg, *local_scriptpubkey; u64 minfee, maxfee, startfee; + u64 num_revocations; if (peer->local_shutdown_idx == -1 || !peer->remote_shutdown_scriptpubkey) { @@ -1529,6 +1530,10 @@ static void peer_start_closingd(struct peer *peer, minfee = maxfee / 2; startfee = (maxfee + minfee)/2; + num_revocations + = revocations_received(&peer->their_shachain.chain); + assert(num_revocations == peer->num_revocations_received); + /* BOLT #3: * * The amounts for each output MUST BE rounded down to whole satoshis. @@ -1551,7 +1556,7 @@ static void peer_start_closingd(struct peer *peer, reconnected, peer->next_index[LOCAL], peer->next_index[REMOTE], - peer->num_revocations_received); + num_revocations); /* We don't expect a response: it will give us feedback on * signatures sent and received, then closing_complete. */ @@ -1658,6 +1663,7 @@ static bool peer_start_channeld(struct peer *peer, enum side *failed_sides; struct short_channel_id funding_channel_id; const u8 *shutdown_scriptpubkey; + u64 num_revocations; /* Now we can consider balance set. */ if (!reconnected) { @@ -1717,6 +1723,16 @@ static bool peer_start_channeld(struct peer *peer, } else shutdown_scriptpubkey = NULL; + num_revocations = revocations_received(&peer->their_shachain.chain); + log_debug(peer->log, "peer->num_revocations_received = %"PRIu64 + " min_index = %"PRIu64 + " revocations_received() = %"PRIu64, + peer->num_revocations_received, + num_revocations, + peer->their_shachain.chain.min_index); + + assert(num_revocations == peer->num_revocations_received); + initmsg = towire_channel_init(tmpctx, &peer->ld->chainparams->genesis_blockhash, peer->funding_txid, @@ -1746,7 +1762,7 @@ static bool peer_start_channeld(struct peer *peer, peer->last_sent_commit, peer->next_index[LOCAL], peer->next_index[REMOTE], - peer->num_revocations_received, + num_revocations, peer->next_htlc_id, htlcs, htlc_states, fulfilled_htlcs, fulfilled_sides, diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 999a3ac48..02d5e4dec 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1135,10 +1135,12 @@ int peer_got_revoke(struct peer *peer, const u8 *msg) return -1; } - if (revokenum != peer->num_revocations_received) { + assert(revocations_received(&peer->their_shachain.chain) + == peer->num_revocations_received); + if (revokenum != revocations_received(&peer->their_shachain.chain)) { peer_internal_error(peer, "got_revoke: expected %"PRIu64 " got %"PRIu64, - peer->num_revocations_received, revokenum); + revocations_received(&peer->their_shachain.chain), revokenum); return -1; }