Confirming transactions on the Bitcoin blockchain
by Alyona Shepilova
Jun 16, 2023
Before any transaction can dream of finding itself on another wallet, it needs to jump through a series of hoops. The first one would be 'Mempool'.
What is a Mempool?
A Mempool is a waiting room for newly-created transactions that haven't been confirmed yet. We often talk about 'the Mempool' as if they were only one. It's not quite so. In reality, each node has its own slightly distinct version of a mempool. Why? Because every node (person) has its own slightly distinct version of hardware. The more powerful it is, the more newborn transactions it can handle.
How does a transaction get into the Mempool?
There's a face control of sorts. Once a new transaction gets transmitted to the network, it gets sent from one node to another, and they run a series of checks to make sure that the transaction is valid. Only after the transaction passes the node's checks can it be allowed into the node's mempool.
And this is where it gets discovered by miners.
Mempool -> Block?
A miner would access the mempool of their local node by running a programme. Remember that miners are getting paid for confirming transactions by adding them to a block, which they'll add to the blockchain?
A block has a size limit of approximately 1 MB, which, on average, fits around 2500 transactions. So, the task at hand is to collect ∼2500 transactions from the mempool (the more, the better), put them into a new block and then add this block to the blockchain before anyone else by doing computational magic. Easy-peasy.
However, if a typical mempool is 300x larger than a typical block, the question arises:
How do miners choose transactions to confirm?
They would often go for those that offer the highest reward (miner fee). It makes sense to do the job that pays the most. Sometimes they would choose their own transactions, sometimes those that have been waiting for the longest...
The selected transactions are organised in a candidate block. Each miner has its own unique version of a candidate block, which they're trying to add to the blockchain. Only one will succeed, while others will fail – their blocks will be discarded, and they'll have to start work on a new candidate block from scratch.
What happens after a transaction makes it to a candidate block?
The computational magic begins! You might have heard that to successfully mine a block, miners need to 'solve a puzzle'. What is this puzzle?
Well, miners are looking for a 64-digits and letters value called 'hash'.
The hash of the new block consists of:
the hash of the previous block
information about all transactions included in the new block
and a random number called 'nonce'
all run through an encoder.
Here's the deal. If you change just one symbol of the nonce – the 64-digits hash will change completely. There's really no rhyme or reason to this, and the system is quite random. You put a random nonce – you get a random hash.
Note that the hash everyone is looking for is not a specific value. Instead, it has an obligatory part, while the rest of the 64 digits can be anything. For example, the Bitcoin protocol needs a new block to be mined every 10 minutes. And since it's so clever, it realises there are too many miners, and if the puzzle is too simple, they will arrive at an answer much sooner. It cannot be allowed. And so, it adjusts the difficulty of the puzzle. It now asks to find a hash the first 19 digits of which will be zeroes. And if the nonce is the key, just imagine how many random keys you will have to insert into the decoder to arrive at your answer.
Essentially, this is guesswork taken to the extreme. In 2022, the hash rate (how many calculations with different nonces can be performed in a second) exceeded 200 EH/s. EH, by the way, stands for trillion (1,000,000,000,000,000,000). So, we've got 200 * (10 to the power of 18) * 60 seconds in a minute * 10 minutes. This is how many attempts with unique nonces have to be made to arrive at a correct answer. A correct nonce is pretty much impossible to guess on the first try, as you can imagine.
Basically, this is what proof-of-work is – putting effort into solving a puzzle. Perhaps one of the injustices of life, but given how much effort goes into finding a correct answer, it's extremely easy to validate it. This is exactly what other nodes do when receiving a candidate block with a correct hash.
Not all cryptocurrencies use this method to confirm transactions, but if yours is quite big and old (think Bitcoin, Litecoin, Doge, Monero etc...), then PoW is your guy.
How do miners get paid?
Ah, an interesting question. Well, their 'salary' consists of two things.
First, there are all the miner fees of all the transactions included in their candidate block. And then, there's also a reward for mining the block (some 6.25 BTC at the time of writing) – these are freshly minted coins that just entered existence and have never been spent before.
The first transaction of each block is called a coinbase transaction: it's created by the miner and aggregates all the miner fees + the block reward. It also includes the miner's BTC address (or indeed several addresses), so they can receive their money.
My miner 'won', what happens next?
Your miner had the most powerful equipment and was faster than everybody else, so they could mine the new block first. Your transaction now has one confirmation. Is this sufficient for the money to be credited to another wallet? Most wallets have a 3-4 confirmations policy, so the money will reach the receiver in 30 to 40 minutes.
Note that all transactions included in the winning block get erased from the mempool (lest they get confirmed again).
If you're worried about the miner – don't be. They'll get their reward (the coinbase transaction) once the block has 100 confirmations (in 16 hours or so).
To sum it up
When you create a transaction, it first gets checked for validity and then goes to mempool. There, along with thousands of other unconfirmed transactions, it gets packed into many candidate blocks, which miners try to add to the blockchain by solving a random puzzle.
Your transaction then gets confirmations, and the money gets credited to another wallet. Voilà.
Block reward – a reward for mining a new block. Some 6.25 BTC at the time of writing.
Candidate block – this is unique for each miner. A block they're trying to add to the blockchain.
Coinbase transaction – the first transaction of each block. It's created by the miner and aggregates all the miner fees + the block reward.
Hash – a piece of data of any length turned into a piece of data of a fixed length. It's impossible to calculate the input from the output without a key – this is why it's a secure method to pass information. Miners are looking for a 64-digits and letters value for a new block. This consists of the previous block's hash, information about all transactions included in the new block, and a random number called 'nonce'.
Hash rate – how many calculations with different nonces can be performed in a second. Has been known to surpass two hundred trillion.
Mempool – the place where all unconfirmed transactions go before they can be picked by miners. But even before that, all newly-created transactions are checked for validity. Every node has its own slightly distinct mempool version, which depends on its hardware capabilities.
Miner fee – a fee for processing a Bitcoin transaction.
Nonce – a random number only used once. When trying to mine a block, miners are looking for such a nonce that would give them the desired outcome. They can go through myriads of them in one go.
Proof-of-work – a mechanism that allows miners to add a new block of transactions to the blockchain by performing complicated but feasible computational calculations, aka solving a math puzzle.