Rav3n`s bitcoin simplified: Transaction fees
In this story I want to say few words about transaction fees in Bitcoin network.
To understand how fee is constructed first we need to know how Bitcoin transaction is build (and some other stuff).
Typical/most common transaction type is P2PKH (pay to public key hash).
We have to take it from end: what is "hash"?
Aunt Wiki says (*1): "A hash function is any function that can be used to map data of arbitrary size to data of fixed size. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes"
What it really means?
Hash function can use any input data (text, numbers, video, pdf - any digital goods) and turn them into fixed-length data. No matter if we use 1 byte or 1Tb of data: resulting hash will have same length (ofc it will be longer calculated ;]).
Hashing function are very sensitive to changes of input data, in most cases changing only one bit in input data resulting totally different hash.
Why is this important? Because we can easily and fast prove, that previously hashed/signed data are changed or not.
Second attribute of hashing functions is: irreversibility. You can easily calculate hash of any data, but from hash you CANT calculate data that was used as input.
Why this is important?
To build Bitcoin address we need use some cryptovoodo (*3):
- take private key
- generate public key from it
- hash public key and build address from it
(well it is bit more complicated, but my goal is to SIMPLIFY stuff a bit ;])
In P2PKH transaction we use Bitcoin addresses as outputs, so as I wrote above, we can NOT deduct public key from address. It IS important, because some study says that if we take quantum computer big enough (what will be build in not-so-far future) we can calculate private key from public key.
When we spend coins that we have on address, we need to sign transaction using private key of that address. Signature used in Bitcoin allow anyone verify that signature is correct, because pubic key of that address is part of signature.
So, if we have coins on address that is never used to SEND bitcoins (like new paper wallet or any new, previously unused address) our coins are safe even from quantum computers.
But if we send even small amount of coins from address, that address is less secure, because public key is known for everyone.
Let's make it simple: you shall NOT use same address more than once.
And every HD wallet is helping you doing it automagically :)
Now few words about transaction itself.
If we try compare bitcoin transaction to anything in real world, closest match will be accounting transaction in bookkeeping.
Every transaction have inputs (coins we want to spend) and outputs (addresses we fund). Like in bookkeeping sum of every transaction need to be zero. Every coins we take to transaction need to be spent in that transaction. Any sum we not spent in outputs is claimed as transaction fee (some bad made transactions pays over 50BTC fees...).
Number of inputs and outputs in transaction is limited only by protocol. At this moment limit is 100kb of data. Transaction can be constructed in way we need: one input multiple outputs, many inputs one output, etc.
Most common transaction is one input and two outputs (because of mechanism of rest), we also can often find transactions of many inputs and one output (when someone is collecting for some time on one address then flush all coins). All depends on user needs.
Math inside transaction is really simple: we take sum of all input values, then sum of all output values, and what's left from subtraction of that sums is transaction fee.
Same way it is calculated in block: sum of all inputs of all transactions minus sum of all outputs from transaction equals fees included in block.
Not every number (value) is directly shown in transaction. As input transaction uses transaction hash and output number, in outputs we have to post destination address and amount we send. It's on wallet side to know how many coins we spending, and it is one of many things that are verified by full nodes and miners.
Now we have to understand basics of signatures in transaction (more cryptovoodo).
Signature can be also considered as hashing function. But it is special case of hashing, because we use our private key to make signature.
In Bitcoin transaction we have to make signature under every input we use. This way we can prove, that we are owner of private key for that address - we own coins on this address.
Signature contains pubic key of address, so everyone can easily verify that signature is correct (signing is way more complicated than signature verification).
This is how we make our public key... public. :)
Now we can do some math.
Transaction size can be calculated in formula: (*2)
Ins*148 + Outs*34 +10 +-Ins
where Ins: number of inputs (number, NOT value!)
Outs : number of outputs
We can see, that size (weight) of input is over 4.5 times more than output. All because of signatures.
So, if we take tx that have 50 inputs and 2 outputs we create transaction of over 7400 bytes. Really big one. It is sample what happens, when we use some "magic" program that pay small number of coins daily and we want to spend them.
Most common transaction of 1 input and 2 outs result in about 226 bytes transaction. Adding one input we raise to 370+ bytes, using 5 inputs we have 810+ bytes.
If we make similar changes on outputs side, changes are way smaller. All because of difference between size of input and output.
Transaction part that size matter is input. This is problem for all people that use faucets and other stuff to "be rich" - sometimes cost of sending coins is higher that value we want to send.
Now we can do more math.
Transaction of 1 input, 2 outputs.
Size = 226 bytes
Bitcoin value = $3500
Fee per transaction byte = 200 satoshi
226 * 0.00000200 * 3500 = $1.58
This is cost of one simple transaction.
If we use data from about 3 months ago, when transaction fees spiked to over 420 satoshi/byte we get over $2.5 per simple transaction.
Using current data shown in https://bitcoinfees.21.co/ we can see, that even using 40 sato/byte our transaction eventually will be included in block (we have to wait 5-130 blocks, so 1 to 22 hours, depends on other transactions in network)
For simple transaction and 40s/b we pay about 30 cents. It can be considered as acceptable.
But what if we spending 10 inputs? Transaction is 1558 bytes long, and from 30 cents we fly to $2.20.
For 20 inputs? 3038 byes transaction and $4.25. Still using VERY low fee, and we wait almost 24 hours to confirm it.
So, even if transaction fees are way cheaper than few weeks ago, it is still pain to pay fees for big transactions. Of course rising value of Bitcoin is (not) helping ;]
Site https://bitcoinfees.github.io/ show us historical data for transaction fee values:
My mouse points show near peak value we calculate. This is when it was very painful. Now we have about twice less, but still it is not we can accept as "normal".