From 017d199305f4bb05238629bff548ff7db208ed5b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 31 Mar 2016 17:12:20 +1030 Subject: [PATCH] funding: record changes so we can abide by BOLT#2 BOLT#2 says we MUST NOT send out commit messages with no changes (and we drop the connection if the peer does). But that's harder than it sounds: we can have changes in theory which cancel out (eg. fee adjustments, not yet implemented) or others which don't change the commit tx (eg. dust outputs). Simplest is to have a generation count, which also allows us to simply show number of pending changes in RPC. It's 32 bit, but you can only use it to screw yourself really (each side can only add 1500 htlcs, so the rest would have to be fee changes; wrapping will only make us hang up on you). Signed-off-by: Rusty Russell --- funding.c | 9 +++++++-- funding.h | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/funding.c b/funding.c index d67d6b44e..0660a094e 100644 --- a/funding.c +++ b/funding.c @@ -139,6 +139,7 @@ struct channel_state *initial_funding(const tal_t *ctx, cstate->b.htlcs = tal_arr(cstate, struct channel_htlc, 0); cstate->fee_rate = fee_rate; cstate->anchor = anchor_satoshis; + cstate->changes = 0; /* Anchor must fit in 32 bit. */ if (anchor_satoshis >= (1ULL << 32) / 1000) @@ -199,6 +200,7 @@ void adjust_fee(struct channel_state *cstate, uint32_t fee_rate) fee_msat = calculate_fee_msat(total_nondust_htlcs(cstate), fee_rate); recalculate_fees(&cstate->a, &cstate->b, fee_msat); + cstate->changes++; } bool force_fee(struct channel_state *cstate, uint64_t fee) @@ -207,6 +209,7 @@ bool force_fee(struct channel_state *cstate, uint64_t fee) if (fee > 0xFFFFFFFFFFFFFFFFULL / 1000) return false; recalculate_fees(&cstate->a, &cstate->b, fee * 1000); + cstate->changes++; return cstate->a.fee_msat + cstate->b.fee_msat == fee * 1000; } @@ -220,7 +223,7 @@ void invert_cstate(struct channel_state *cstate) } /* Add a HTLC to @creator if it can afford it. */ -static bool add_htlc(const struct channel_state *cstate, +static bool add_htlc(struct channel_state *cstate, struct channel_oneside *creator, struct channel_oneside *recipient, u32 msatoshis, const struct abs_locktime *expiry, @@ -250,11 +253,12 @@ static bool add_htlc(const struct channel_state *cstate, memcheck(&creator->htlcs[n].msatoshis, sizeof(creator->htlcs[n].msatoshis)); memcheck(&creator->htlcs[n].rhash, sizeof(creator->htlcs[n].rhash)); + cstate->changes++; return true; } /* Remove htlc from creator, credit it to beneficiary. */ -static void remove_htlc(const struct channel_state *cstate, +static void remove_htlc(struct channel_state *cstate, struct channel_oneside *creator, struct channel_oneside *beneficiary, struct channel_oneside *non_beneficiary, @@ -285,6 +289,7 @@ static void remove_htlc(const struct channel_state *cstate, memmove(creator->htlcs + i, creator->htlcs + i + 1, (n - i - 1) * sizeof(*creator->htlcs)); tal_resize(&creator->htlcs, n-1); + cstate->changes++; } bool funding_a_add_htlc(struct channel_state *cstate, diff --git a/funding.h b/funding.h index 6385ea51a..ed1cecd63 100644 --- a/funding.h +++ b/funding.h @@ -25,6 +25,8 @@ struct channel_state { uint64_t anchor; /* Satoshis per 1000 bytes. */ uint32_t fee_rate; + /* Generation counter (incremented on every change) */ + uint32_t changes; struct channel_oneside a, b; };