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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-08-23 10:24:54 +09:30 committed by Christian Decker
parent 9c3ac38544
commit a5d4a3eb2c
3 changed files with 32 additions and 6 deletions

View File

@ -3,6 +3,8 @@
#include "config.h"
#include <bitcoin/privkey.h>
#include <bitcoin/pubkey.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/crypto/shachain/shachain.h>
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 */

View File

@ -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,

View File

@ -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;
}