Skip to content

Commit 017d199

Browse files
committed
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 <[email protected]>
1 parent b7a7234 commit 017d199

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

funding.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct channel_state *initial_funding(const tal_t *ctx,
139139
cstate->b.htlcs = tal_arr(cstate, struct channel_htlc, 0);
140140
cstate->fee_rate = fee_rate;
141141
cstate->anchor = anchor_satoshis;
142+
cstate->changes = 0;
142143

143144
/* Anchor must fit in 32 bit. */
144145
if (anchor_satoshis >= (1ULL << 32) / 1000)
@@ -199,6 +200,7 @@ void adjust_fee(struct channel_state *cstate, uint32_t fee_rate)
199200
fee_msat = calculate_fee_msat(total_nondust_htlcs(cstate), fee_rate);
200201

201202
recalculate_fees(&cstate->a, &cstate->b, fee_msat);
203+
cstate->changes++;
202204
}
203205

204206
bool force_fee(struct channel_state *cstate, uint64_t fee)
@@ -207,6 +209,7 @@ bool force_fee(struct channel_state *cstate, uint64_t fee)
207209
if (fee > 0xFFFFFFFFFFFFFFFFULL / 1000)
208210
return false;
209211
recalculate_fees(&cstate->a, &cstate->b, fee * 1000);
212+
cstate->changes++;
210213
return cstate->a.fee_msat + cstate->b.fee_msat == fee * 1000;
211214
}
212215

@@ -220,7 +223,7 @@ void invert_cstate(struct channel_state *cstate)
220223
}
221224

222225
/* Add a HTLC to @creator if it can afford it. */
223-
static bool add_htlc(const struct channel_state *cstate,
226+
static bool add_htlc(struct channel_state *cstate,
224227
struct channel_oneside *creator,
225228
struct channel_oneside *recipient,
226229
u32 msatoshis, const struct abs_locktime *expiry,
@@ -250,11 +253,12 @@ static bool add_htlc(const struct channel_state *cstate,
250253
memcheck(&creator->htlcs[n].msatoshis,
251254
sizeof(creator->htlcs[n].msatoshis));
252255
memcheck(&creator->htlcs[n].rhash, sizeof(creator->htlcs[n].rhash));
256+
cstate->changes++;
253257
return true;
254258
}
255259

256260
/* Remove htlc from creator, credit it to beneficiary. */
257-
static void remove_htlc(const struct channel_state *cstate,
261+
static void remove_htlc(struct channel_state *cstate,
258262
struct channel_oneside *creator,
259263
struct channel_oneside *beneficiary,
260264
struct channel_oneside *non_beneficiary,
@@ -285,6 +289,7 @@ static void remove_htlc(const struct channel_state *cstate,
285289
memmove(creator->htlcs + i, creator->htlcs + i + 1,
286290
(n - i - 1) * sizeof(*creator->htlcs));
287291
tal_resize(&creator->htlcs, n-1);
292+
cstate->changes++;
288293
}
289294

290295
bool funding_a_add_htlc(struct channel_state *cstate,

funding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct channel_state {
2525
uint64_t anchor;
2626
/* Satoshis per 1000 bytes. */
2727
uint32_t fee_rate;
28+
/* Generation counter (incremented on every change) */
29+
uint32_t changes;
2830
struct channel_oneside a, b;
2931
};
3032

0 commit comments

Comments
 (0)