Each miner collects unverified transactions into a block (about 500 transactions), and then races to solve Bitcoin’s Proof of work.
This challenge is similar to guessing acorrect value from a quadrillion possibilities. With no patterns to go off or learn from, the only strategy is brute force. Try as many values as possible.
This makes it purely a game of processing power.
In reality there are multiple correct values. Read on.
What is Proof of Work?
Proof of Work is the system Bitcoin uses, forcing miners to do a lot of computation in order to find a correct value. While also making correct solutions easily verifiable to any party.
In Bitcoin’s case, this involves a hash function.
While requirements for correct answers change over time, Bitcoin currently requires miners to find inputs to it’s hash function that result in an output which begins with 19 zeros.
A correct solution might look like this:000000000000000000069aef2b97ba47122bfe11d81abb58c91068d331226623
We’ll explain hash functions in a moment.
The first miner to discover a correct solution broadcasts their answer. They win the round.
All other miners then add the winning miners block to their ledgers. The transactions of the winner miner now exist in the blockchain and can no longer be worked on by other miners.
What is a hash function?
A hash function converts an arbitrary string of values (ie: a word, sentence, numbers…) into a fixed length string.
For example, a simple fictional hash function could do something like this.
Input | Output
---------------------------------------------
'hello world' | hd72kf97x
'hello world 2' | l9ancg478
'123' | nfhyeiv00
'I like pasta on rainy days' | x913gzmaw
Bitcoin uses a modified version of SHA-256.
We’ll get back to SHA-256 in a moment.
Recognize that a similar input does NOT result in a similar output. So there’s no incentive to do anything other than guess random inputs.
What are inputs to Bitcoin’s hash function?
The inputs for Bitcoin’s hash function are predetermined. You must follow the rules.
But that doesn’t mean each user uses the same inputs.
There are 6 inputs to Bitcoin’s hash function.
- Block version number. Indicates the current rule set for validating blocks. Currently this is
0x20000004
but changes periodically. Everyone uses the same version as a hash input. - Previous block header hash. This is the output hash associated with the previous block in the blockchain. This ensures the ledger is an unbroken chain with each block referencing the block before it. This will be the same for all miners at a given time.
A recent example:1b9affbba072ba2e923797d3b2050b9b9c8baacf696f84ac9940282b5568c547
. - Current block transactions hash. A hash based on transactions that the miner has included in the current block. This is different for each miner because each miner is likely working on a different block of transactions.
A recent example:11a510d7adcde1ad5c8b33c35f5902f08cdb1d9800161f8af3874258f935ecea
- Current time. A unix timestamp representing the time the miner started hashing inputs for the current block. The miner sets this time so it will be different between miners.
- Target nBits. An encoded version of the criteria that successful hash outputs must meet (aka. begin with at least 19 zeros). The threshold is revised every 2,016 blocks so that the blockchain continues growing at it’s planned rate of 1 block every 10 minutes.
- Nonce (aka. random positive integer). A random number that’s incremented every time inputs are hashed. A 1 integer change in this number completely changes the hash output.
Hash inputs are concatenated and fed to the hashing algorithm.
#################
# Pseudo code #
#################block_header = version + prevHash + curHash + time + diff + nonce
Because changing a single input completely changes the output, it’s unlikely that any 2 miners are generating the same outputs.
What hashing algorithm does Bitcoin use?
Bitcoin uses Double SHA-256.
Which is just the output of SHA-256 re-fed into SHA-256.
#################
# Pseudo code #
#################block_header = version + prevHash + curHash + time + diff + noncesha_function( sha_function( block_header ) )
You can try an online SHA-256 calculator here.
Let’s try hashing something with the same algorithm Bitcoin uses. We’ll start with a string and hash it twice.
Input:Big money
Output 1: 5f5255f01f4d7196484869c824266fbe5c2fa0fad9485dd911d507d2812fab7f
Now let’s re-hash that hash.
Output 2:8aa2a47234e9c320bbe3248b6a03fb5c68546ad0988b3faa6d0731805b19ad16
If you hash this yourself, you will get exactly the same output. That’s the nature of hash functions. Cool, no?
Do all miners work on the same blocks of transactions?
We mentioned this before. But here is a little more explanation.
Miners choose which transactions to include in their blocks. Blocks have a maximum size of 1MB and each transaction is about 0.5kb.
While miners make money from earning Bitcoin on successfully mined blocks. They also make money on transaction fees. These vary by transaction.
So miners have incentive to choose transactions with the highest fees.
- algorithm
- All
- Bitcoin
- Bitcoin mining
- blockchain
- challenge
- change
- code
- continues
- Current
- CZ
- Fed
- Fees
- First
- follow
- function
- game
- GitHub
- Growing
- hash
- hashing
- here
- HTTPS
- ia
- IP
- IT
- LEARN
- Ledger
- LG
- LP
- Making
- medium
- Miners
- Mining
- ML
- money
- MS
- MT
- order
- Other
- power
- proof
- Reality
- Requirements
- rules
- set
- Simple
- Size
- So
- Solutions
- SOLVE
- start
- started
- Strategy
- successful
- system
- time
- transaction
- Transactions
- value
- win
- Work
- world