mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
initial_commit_tx, commit_tx: add anchor outputs if needed.
This also means we subtract 660 satoshis more everywhere we subtract the base fee (except for mutual close, where the base fee is still used). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -58,6 +58,19 @@ u8 *to_self_wscript(const tal_t *ctx,
|
||||
&keyset->self_delayed_payment_key);
|
||||
}
|
||||
|
||||
void tx_add_anchor_output(struct bitcoin_tx *tx,
|
||||
const struct pubkey *funding_key)
|
||||
{
|
||||
u8 *wscript = bitcoin_wscript_anchor(tmpctx, funding_key);
|
||||
u8 *p2wsh = scriptpubkey_p2wsh(tmpctx, wscript);
|
||||
|
||||
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
|
||||
* The amount of the output is fixed at 330 sats, the default
|
||||
* dust limit for P2WSH.
|
||||
*/
|
||||
bitcoin_tx_add_output(tx, p2wsh, wscript, AMOUNT_SAT(330));
|
||||
}
|
||||
|
||||
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
unsigned int funding_txout,
|
||||
@@ -80,6 +93,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
struct amount_sat base_fee;
|
||||
struct bitcoin_tx *tx;
|
||||
size_t n, untrimmed;
|
||||
bool to_local, to_remote;
|
||||
struct amount_msat total_pay;
|
||||
struct amount_sat amount;
|
||||
u32 sequence;
|
||||
@@ -108,6 +122,18 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
|
||||
option_anchor_outputs);
|
||||
|
||||
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
|
||||
* If `option_anchor_outputs` applies to the commitment
|
||||
* transaction, also subtract two times the fixed anchor size
|
||||
* of 330 sats from the funder (either `to_local` or
|
||||
* `to_remote`).
|
||||
*/
|
||||
if (option_anchor_outputs
|
||||
&& !amount_sat_add(&base_fee, base_fee, AMOUNT_SAT(660))) {
|
||||
*err_reason = "Funder cannot afford anchor outputs";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* BOLT #3:
|
||||
*
|
||||
* 3. Subtract this base fee from the funder (either `to_local` or
|
||||
@@ -153,8 +179,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
}
|
||||
|
||||
|
||||
/* Worst-case sizing: both to-local and to-remote outputs. */
|
||||
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2, 0);
|
||||
/* Worst-case sizing: both to-local and to-remote outputs + anchors. */
|
||||
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 4, 0);
|
||||
|
||||
/* This could be done in a single loop, but we follow the BOLT
|
||||
* literally to make comments in test vectors clearer. */
|
||||
@@ -186,7 +212,9 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
assert(pos == n);
|
||||
output_order[n] = dummy_local;
|
||||
n++;
|
||||
}
|
||||
to_local = true;
|
||||
} else
|
||||
to_local = false;
|
||||
|
||||
/* BOLT #3:
|
||||
*
|
||||
@@ -209,6 +237,28 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
assert(pos == n);
|
||||
output_order[n] = dummy_remote;
|
||||
n++;
|
||||
to_remote = true;
|
||||
} else
|
||||
to_remote = false;
|
||||
|
||||
if (option_anchor_outputs) {
|
||||
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
|
||||
* if `to_local` exists or there are untrimmed HTLCs, add a `to_local_anchor` output
|
||||
*/
|
||||
if (to_local || untrimmed != 0) {
|
||||
tx_add_anchor_output(tx, &funding_key[side]);
|
||||
output_order[n] = NULL;
|
||||
n++;
|
||||
}
|
||||
|
||||
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
|
||||
* if `to_remote` exists or there are untrimmed HTLCs, add a `to_remote_anchor` output
|
||||
*/
|
||||
if (to_remote || untrimmed != 0) {
|
||||
tx_add_anchor_output(tx, &funding_key[!side]);
|
||||
output_order[n] = NULL;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
assert(n <= tx->wtx->num_outputs);
|
||||
|
||||
Reference in New Issue
Block a user