Browse Source

daemon: simplify fee calculation for spends of our own commit tx.

It's not exact, but faking a sig, measuring length, then resigning was
neither exact nor pretty.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
e18aea8d71
  1. 31
      daemon/peer.c

31
daemon/peer.c

@ -1174,41 +1174,32 @@ const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
tx->input[0].amount = tal_dup(tx->input, u64,
&commit->output[p2wsh_out].amount);
tx->output[0].amount = commit->output[p2wsh_out].amount;
tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, &peer->us.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script);
/* Use signature, until we have fee. */
sig.stype = SIGHASH_ALL;
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
tx->input[0].witness = bitcoin_witness_secret(tx, NULL, 0, &sig,
witnessscript);
/* FIXME: Figure out length first, then calc fee! */
/* Witness length can vary, due to DER encoding of sigs, but we
* use 176 from an example run. */
assert(measure_tx_cost(tx) == 83 * 4);
/* Now, calculate the fee, given length. */
/* FIXME: Dynamic fees! */
fee = fee_by_feerate(measure_tx_cost(tx) / 4,
peer->dstate->config.closing_fee_rate);
fee = fee_by_feerate(83 + 176 / 4,
peer->dstate->config.closing_fee_rate);
/* FIXME: Fail gracefully in these cases (not worth collecting) */
if (fee > tx->output[0].amount
|| is_dust_amount(tx->output[0].amount - fee))
if (fee > commit->output[p2wsh_out].amount
|| is_dust_amount(commit->output[p2wsh_out].amount - fee))
fatal("Amount of %"PRIu64" won't cover fee %"PRIu64,
tx->output[0].amount, fee);
commit->output[p2wsh_out].amount, fee);
/* Re-sign with the real values. */
tx->input[0].witness = tal_free(tx->input[0].witness);
tx->output[0].amount -= fee;
tx->output[0].amount = commit->output[p2wsh_out].amount - fee;
sig.stype = SIGHASH_ALL;
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
tx->input[0].witness = bitcoin_witness_secret(tx, NULL, 0, &sig,
witnessscript);
return tx;
}

Loading…
Cancel
Save