bitcoin: break out method to calculate input weight

We have a required minimum witness weight for dual funded opens.
This commit is contained in:
niftynei 2020-11-17 19:31:50 -06:00 committed by Rusty Russell
parent b23c6c50a0
commit b4aebc17a1
2 changed files with 23 additions and 7 deletions

View file

@ -831,13 +831,13 @@ size_t bitcoin_tx_input_sig_weight(void)
return 1 + 71; return 1 + 71;
} }
/* We only do segwit inputs, and we assume witness is sig + key */ /* Input weight */
size_t bitcoin_tx_simple_input_weight(bool p2sh) 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 */ /* 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 */ /* We always encode the length of the script, even if empty */
weight += 1 * 4; weight += 1 * 4;
@ -846,9 +846,6 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh)
if (p2sh) if (p2sh)
weight += 23 * 4; 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. */ /* Elements inputs have 6 bytes of blank proofs attached. */
if (chainparams->is_elements) if (chainparams->is_elements)
weight += 6; weight += 6;
@ -856,6 +853,19 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh)
return weight; 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) struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw)
{ {
size_t outweight; size_t outweight;

View file

@ -255,6 +255,12 @@ size_t bitcoin_tx_output_weight(size_t outscript_len);
/* Weight to push sig on stack. */ /* Weight to push sig on stack. */
size_t bitcoin_tx_input_sig_weight(void); 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 */ /* We only do segwit inputs, and we assume witness is sig + key */
size_t bitcoin_tx_simple_input_weight(bool p2sh); size_t bitcoin_tx_simple_input_weight(bool p2sh);