06 - UTXO and Balance
Overview
- Description:: UTXO, balance input output, light clients, proof of work
UTXO
In Bitcoin nothing is “made from nothing”.
We always talk about UTXO: Unspent Transaction Output! Whenever someone receives a Bitcoin, this someone has an UTXO.
Important
The concept of value is entirely made from wallet: behind the scenes we only talk about unspent transactions
Once you have an unspent transaction it could never be split up.
For example: if you have a 20 bitcoin UTXO and want to pay 1 bitcoin, your transaction must consume the entire 20 bitcoin UTXO and produce two outputs: one paying 1 bitcoin to your desired recipient and another paying 19 bitcoin in change back to your wallet. As a result, most bitcoin transactions will generate change.
Usually Bitcoin exceeding part transaction could be used in two ways:
- as a charge back
- as a fee
Important
if you don’t generate another returning to your wallet transaction, the exceeding part will be returned as miner’s fee
Inputs and outputs
- every transaction creates outputs, which are recorded on the bitcoin ledger
- UTXO
- transaction inputs are pointers to UTXO
- they point to a specific UTXO by reference to the transaction hash and sequence number where the UTXO is recorded in the blockchain
- to use each other, we use a locking/unlocking script
Transaction and fees balance
- miners earns by mining new blocks (solving crypto-puzzles), which contains a set of transactions
- higher the fees, higher the chance to include the transaction with the higher fees into the processing block
Fees = Sum(Inputs) – Sum(Outputs).
Transaction occurs, if and only if:
- Every input must be valid and not yet spent
- The transaction must have a signature matching the owner of the input
- The total value of the inputs must equal or exceed the total value of the outputs
More on PoW: nonce, and how
- look for a value (nonce) such that the binary digest of the block containing it begins with a given number of zero bits
- simple to verify, hard to calculate
PoW is calculated for each block:
- every block has hash of the previous block
- every block has a nonce that if combined with the data, hash must meet the difficulty
- example: difficulty is 3, so if data+nonce is hashed it must gives a hash starting with 000…
In real world:
-
hash(previous block hash + version info + merkle root (hash of the hashes of the transactions) + timestamp + difficulty level + nonce) must produces a hash with leading 0 = difficulty level
The algorithm:
- Start with Nonce at 0.
- Combine the block data and Nonce, i.e., the calculation target is the string “Hello, world!0.”
- Apply the SHA-256 hash function to “Hello, world!0” and calculate the hash value.
- Check if the calculated hash value meets the Difficulty conditions (starting with “0000”).
- If not met, increase the Nonce by 1 and repeat steps 2–4.
- If met, the calculation is complete.
Coinbase transaction
- The coinbase transaction is the first transaction in a block. Always created by a miner, it includes a single coinbase.
- A coinbase is a special field used as the sole input for coinbase transactions. The coinbase allows claiming the block reward and provides up to 100 bytes for arbitrary data.
Simple payment verification
SPV allows a lightweight client to verify that a transaction is included in the Bitcoin blockchain, without downloading the entire blockchain.
The SPV client only needs download the block headers, which are much smaller than the full blocks. To verify that a transaction is in a block, a SPV client requests a proof of inclusion, in the form of a Merkle branch.