From b4aebc17a1e610264d4e3823edfa8747504efad8 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 17 Nov 2020 19:31:50 -0600 Subject: [PATCH] bitcoin: break out method to calculate input weight We have a required minimum witness weight for dual funded opens. --- bitcoin/tx.c | 24 +++++++++++++++++------- bitcoin/tx.h | 6 ++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 59e328a08..88aca4004 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -831,13 +831,13 @@ size_t bitcoin_tx_input_sig_weight(void) return 1 + 71; } -/* We only do segwit inputs, and we assume witness is sig + key */ -size_t bitcoin_tx_simple_input_weight(bool p2sh) +/* Input weight */ +size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight) { - size_t weight; + size_t weight = witness_weight; /* Input weight: txid + index + sequence */ - weight = (32 + 4 + 4) * 4; + weight += (32 + 4 + 4) * 4; /* We always encode the length of the script, even if empty */ weight += 1 * 4; @@ -846,9 +846,6 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh) if (p2sh) weight += 23 * 4; - /* Account for witness (1 byte count + sig + key) */ - weight += 1 + (bitcoin_tx_input_sig_weight() + 1 + 33); - /* Elements inputs have 6 bytes of blank proofs attached. */ if (chainparams->is_elements) weight += 6; @@ -856,6 +853,19 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh) return weight; } +size_t bitcoin_tx_simple_input_witness_weight(void) +{ + /* Account for witness (1 byte count + sig + key) */ + return 1 + (bitcoin_tx_input_sig_weight() + 1 + 33); +} + +/* We only do segwit inputs, and we assume witness is sig + key */ +size_t bitcoin_tx_simple_input_weight(bool p2sh) +{ + return bitcoin_tx_input_weight(p2sh, + bitcoin_tx_simple_input_witness_weight()); +} + struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw) { size_t outweight; diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 73c3173a2..c2926e273 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -255,6 +255,12 @@ size_t bitcoin_tx_output_weight(size_t outscript_len); /* Weight to push sig on stack. */ size_t bitcoin_tx_input_sig_weight(void); +/* Segwit input, but with parameter for witness weight (size) */ +size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight); + +/* The witness weight for a simple (sig + key) input */ +size_t bitcoin_tx_simple_input_witness_weight(void); + /* We only do segwit inputs, and we assume witness is sig + key */ size_t bitcoin_tx_simple_input_weight(bool p2sh);