change fees: more accurate rounding for change amount

We were getting off-by-one for the total amount that the change is for,
since it rounds the fee *down*, independent of the total weight of the
entire tx.

We fix this by using the diff btw the fee of the total weight (w/ and
w/o the change output)
This commit is contained in:
niftynei
2021-07-12 12:06:29 -05:00
committed by neil saitug
parent 914e3dd082
commit 04b6ad06cb
5 changed files with 29 additions and 7 deletions

View File

@@ -277,6 +277,7 @@ static struct command_result *psbt_created(struct command *cmd,
const jsmntok_t *psbttok;
struct out_req *req;
struct amount_sat excess;
u32 weight;
psbttok = json_get_member(buf, result, "psbt");
txp->psbt = json_tok_psbt(txp, buf, psbttok);
@@ -300,6 +301,14 @@ static struct command_result *psbt_created(struct command *cmd,
result->end - result->start,
buf + result->start);
if (!json_to_number(buf, json_get_member(buf, result,
"estimated_final_weight"),
&weight))
return command_fail(cmd, LIGHTNINGD,
"Unparsable estimated_final_weight: '%.*s'",
result->end - result->start,
buf + result->start);
/* If we have an "all" output, now we can derive its value: excess
* in this case will be total value after inputs paid for themselves. */
if (txp->all_output_idx != -1) {
@@ -313,7 +322,7 @@ static struct command_result *psbt_created(struct command *cmd,
}
/* So, do we need change? */
txp->change_amount = change_amount(excess, txp->feerate);
txp->change_amount = change_amount(excess, txp->feerate, weight);
if (amount_sat_eq(txp->change_amount, AMOUNT_SAT(0)))
return finish_txprepare(cmd, txp);