From 9b42f49d2c17b9a1b2b9d9a72c2a2560119bd542 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 17 Nov 2017 13:47:48 +1030 Subject: [PATCH] onchaind: fix feerate determination. There were two bugs here. First, grind_feerate() needs to check the actual range of feerates, not the same rate over and over! Secondly, we need to grind the feerate for the HTLC-success tx, too. These were masked by the fact that our tests always use the same feerate! "Untested code is buggy code" Signed-off-by: Rusty Russell --- onchaind/onchain.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/onchaind/onchain.c b/onchaind/onchain.c index ab4d53280..3c0424d63 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -163,7 +163,7 @@ static bool grind_feerate(struct bitcoin_tx *commit_tx, u64 input_amount = *commit_tx->input[0].amount; for (s64 i = feerate_range.max; i >= feerate_range.min; i--) { - u64 fee = feerate_per_kw * multiplier / 1000; + u64 fee = i * multiplier / 1000; if (fee > input_amount) continue; @@ -873,8 +873,25 @@ static void handle_preimage(struct tracked_output **outs, outs[i]->outnum, outs[i]->satoshi * 1000, to_self_delay[LOCAL], - feerate_per_kw, + 0, keyset); + /* BOLT #3: + * + * The fee for an HTLC-success transaction MUST BE + * calculated to match: + * + * 1. Multiply `feerate_per_kw` by 703 and divide by + * 1000 (rounding down). + */ + if (!grind_feerate(tx, outs[i]->remote_htlc_sig, + outs[i]->wscript, 703)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Could not find feerate for" + " signature on HTLC success" + " between %u and %u", + feerate_range.min, + feerate_range.max); + sign_tx_input(tx, 0, NULL, outs[i]->wscript, &htlc_privkey, &keyset->self_htlc_key,