What is the weight unit cost of an input? And can you batch/aggregate the inputs or is it linearly growth when you add more inputs to a tx?
2 Answers
What is the weight unit cost of an input?
2 things affect the weight of a tx, the total number of bytes , and the total bytes minus witness data.
Transaction Weight = Base transaction size * 3 + Total transaction size
, where
Base transaction size is the size of the transaction serialised with the witness data stripped.
Total transaction size is the transaction size in bytes serialized as described in BIP144, including base data and witness data.
See BIP141
Can you batch/aggregate the inputs or is it linearly growth when you add more inputs to a tx?
You can't batch inputs, but you can combine them and send them back to yourself first in a larger amount, but that requires an additional transaction. Probably the best you can do now is to use a segwit address to receive funds so when you go to spend the inputs, the witness data lowers the weight of each input.
-
1And to address OP's specific question, yes, this means that transaction size grows linearly with the number of inputs. Sep 27, 2018 at 14:13
-
Thank you for the answer, I am however still wondering how heavy an input is? Sep 27, 2018 at 19:23
-
It depends on the scriptSig, see bitcoin.stackexchange.com/questions/68811/… and en.bitcoin.it/wiki/…– JBaczukSep 27, 2018 at 19:37
The weight of a single input in a transaction always uses:
32
bytes to reference the outpoint id (hash)4
bytes to reference the outpoint index
Then, if the outpoint is a non-segwit p2pkh output the scriptSig uses:
1
byte to denote the scriptSig size1
byte to denote the signature size71
bytes (on average) for the signature1
byte to denote the public key size33
bytes for the public key
In total this makes 143
bytes (on average) to reference a non-segwit p2pkh output with your spending transaction.
For segwit p2sh nested p2pkh outputs, the scriptSig uses instead:
1
byte to denote the scriptSig size1
byte to denote the witness version (0x00)1
byte to denote the public key hash size20
bytes for the public key hash
Further, the witnessScript will use the same amount of bytes as the scriptSig for spending non-witness p2pkh outpoints. However, the witnessScript only counts with a quarter to the total weight.
Therefore a segwit p2sh nested input has a weight contribution of 59
bytes + (107
)/4 bytes = 86
bytes on average.