From a7a9f1c9d30125e58ff93982771afd5e8420ff02 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Aug 2019 16:46:48 +0930 Subject: [PATCH] onchaind: fix bug when grinding feerates with multiple possibilities. Fixes: #2820 Signed-off-by: Rusty Russell --- onchaind/onchaind.c | 11 +++++++++-- onchaind/test/run-grind_feerate-bug.c | 14 ++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 0723e06bd..f3517e2f9 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -169,8 +169,15 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, * 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding * down). */ - if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) - return grind_htlc_tx_fee(&fee, tx, remotesig, wscript, 663); + if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) { + struct amount_sat grindfee; + if (grind_htlc_tx_fee(&grindfee, tx, remotesig, wscript, 663)) { + /* Cache this for next time */ + fee = grindfee; + return true; + } + return false; + } if (!amount_sat_sub(&amount, amount, fee)) status_failed(STATUS_FAIL_INTERNAL_ERROR, diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index 73069ab14..0046ffcd7 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -85,6 +85,10 @@ bool shachain_get_secret(const struct shachain *shachain UNNEEDED, u64 commit_num UNNEEDED, struct secret *preimage UNNEEDED) { fprintf(stderr, "shachain_get_secret called!\n"); abort(); } +/* Generated stub for status_failed */ +void status_failed(enum status_failreason code UNNEEDED, + const char *fmt UNNEEDED, ...) +{ fprintf(stderr, "status_failed called!\n"); abort(); } /* Generated stub for status_setup_sync */ void status_setup_sync(int fd UNNEEDED) { fprintf(stderr, "status_setup_sync called!\n"); abort(); } @@ -228,16 +232,6 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, return tx; } -void status_failed(enum status_failreason code UNNEEDED, - const char *fmt, ...) -{ - /* FIXME! */ - fprintf(stderr, "XFAIL: %s!\n", fmt); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); - exit(0); -} - int main(void) { struct bitcoin_signature remotesig;