Files
palladum-lightning/common/close_tx.c
Rusty Russell 6e5cb299dd global: remove unnecessary includes from C files.
Basically, `devtools/reduce-includes.sh */*.c`.

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s
After:
	real    0m37.872000-39.974000(39.5466+/-0.59)s
	user    3m1.211000-14.968000(12.4556+/-3.9)s
	sys     0m35.008000-36.830000(36.4143+/-0.5)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

After:
	real    0m18.384000-21.307000(20.8605+/-0.92)s
	user    2m5.585000-26.843000(23.6017+/-6.7)s
	sys     0m19.650000-22.003000(21.4943+/-0.69)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30

81 lines
2.2 KiB
C

#include "config.h"
#include <assert.h>
#include <bitcoin/script.h>
#include <common/close_tx.h>
#include <common/permute_tx.h>
#include <common/psbt_keypath.h>
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const struct chainparams *chainparams,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key,
const u8 *our_script,
const u8 *their_script,
const u8 *funding_wscript,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
struct amount_sat to_us,
struct amount_sat to_them,
struct amount_sat dust_limit)
{
struct bitcoin_tx *tx;
size_t num_outputs = 0;
struct amount_sat total_out;
u8 *script;
assert(amount_sat_add(&total_out, to_us, to_them));
assert(amount_sat_less_eq(total_out, funding_sats));
/* BOLT #3:
*
* ## Legacy Closing Transaction
*
* This variant is used for `closing_signed` messages (i.e. where
* `option_simple_close` is not negotiated).
*
* Note that there are two possible variants for each node.
*
* * version: 2
* * locktime: 0
* * txin count: 1
*/
/* Now create close tx: one input, two outputs. */
tx = bitcoin_tx(ctx, chainparams, 1, 2, 0);
/* Our input spends the anchor tx output. */
bitcoin_tx_add_input(tx, funding,
BITCOIN_TX_DEFAULT_SEQUENCE, NULL,
funding_sats, NULL, funding_wscript);
if (amount_sat_greater_eq(to_us, dust_limit)) {
script = tal_dup_talarr(tx, u8, our_script);
/* One output is to us. */
bitcoin_tx_add_output(tx, script, NULL, to_us);
assert((local_wallet_index == NULL) == (local_wallet_ext_key == NULL));
if (local_wallet_index) {
size_t script_len = tal_bytelen(script);
psbt_add_keypath_to_last_output(
tx, *local_wallet_index, local_wallet_ext_key,
is_p2tr(script, script_len, NULL));
}
num_outputs++;
}
if (amount_sat_greater_eq(to_them, dust_limit)) {
script = tal_dup_talarr(tx, u8, their_script);
/* Other output is to them. */
bitcoin_tx_add_output(tx, script, NULL, to_them);
num_outputs++;
}
/* Can't have no outputs at all! */
if (num_outputs == 0)
return tal_free(tx);
permute_outputs(tx, NULL, NULL);
bitcoin_tx_finalize(tx);
assert(bitcoin_tx_check(tx));
return tx;
}