Cryptocurrency 911 — What are miners really doing?

Andy Jazz
Geek Culture
Published in
8 min readSep 19, 2021

Many of us who have heard about mining either do not know what miners are really doing or have a misunderstanding about it. Most articles and videos on this topic do not reveal the essence of the matter. However, it is obvious to everyone that miners receive good rewards. Why does their job pay so well?

In this story, I will tell you how mining works and will show you to what extent mining loads a CPU of a regular home computer. As example, you can run an app written in Swift language.

Theory

The first main mission of miners (not people, of course, but rather their current ASIC-equipment) is to validate transactions in order to put those transactions into blocks, which in turn form the blockchain (ledger). The second main mission for miners is to maintain the resistance of the blockchain to attacks. Technically speaking, the Bitcoin network functions thanks to a Proof-of-Work consensus mechanism, or simply PoW.

Each new transaction, or Tx for short, firstly enters the MemPool where it waits for its turn for processing (you may call it pending or unconfirmed Tx). Transactions with the max fee are taken from the MemPool first. Transactions with the smallest fee can wait for a confirmation for a very long time — up to 2 or 3 weeks.

Tip: Despite the fact that you may read in some article that a fee is optional in Bitcoin network, you cannot now send BTC coins without paying any fee anymore. If you do not pay the fee, your transaction will be rejected and the funds will be returned to your wallet in a few days.

At the moment (before Lightning Network implemented in every Bitcoin wallet), legacy Bitcoin network processes up to seven transactions per second, on average. And each new block, filled with confirmed transactions, is created approximately every 10 minutes. Now a classic Bitcoin network has a slow performance compared to other blockchains, such as Cardano.

By confirming “legitimate” transactions, miners receive fees as a reward. But an income from all new Tx fees is relatively small (a total could be 0.03 BTC for 3500 Tx in a block). In addition to the above, a mining reward is provided for those miners who solved a math puzzle in order to create a new block for ledger, where all the confirmed transactions will be put. Whoever it will be, the winner takes it all — and mining reward and fees.

In 2021, the reward for creating a new block is 6.25 bitcoins. Bitcoins can be mined until the year 2140. The total amount of bitcoins is around 21 million. Reward halving is scheduled to occur once every 210,000 blocks, or, in other words, every four years.

|-----------------|--------------------|
| Years | Reward, BTC |
|-----------------|--------------------|
| 2009...2012 | 50.000 |
|-----------------|--------------------|
| 2013...2016 | 25.000 |
|-----------------|--------------------|
| 2017...2020 | 12.500 |
|-----------------|--------------------|
| 2021...2024 | 6.250 |
|-----------------|--------------------|
| 2025...2028 | 3.125 |
|-----------------|--------------------|

What a math puzzle is?

In order to solve the aforementioned math puzzle, a huge computational power is needed — so miners are united in mining pools. Each computer in each mining pool looks for a secret number starting with zeros.

Mining Pools in 2019

The name of this secret number is a hash — a unique identifier, consisting of 64 characters in hexadecimal format (or 256 characters in binary format). Bitcoin network’s algorithm is responsible for the formation of a target hash (some threshold value) based on a combination of a predefined inputs. And miners, in their turn, must find a valid hash (that is lower than target hash), by brute force. Blockchain’s hashes are generally produced by a special hashing function called SHA-256.

Let’s take a look at the block 701,125, for example, that has been recently written in ledger. This block was created due to the fact that miners in the AntPool found a rare hash with 19 leading zeros. Here it is:

00000000000000000006467d4ceb7b301b679b4146d7269a270091e5c82938aa

The reward will be received by the mining pool if it finds such a hash first (the reward will be fairly divided among the members of the pool). However, the problem is that it’s extremely difficult to find this hash, because up-to-date computers, even ASICs, are bad at working with large numbers. This hash must be searched for within 10 minutes by all miners in the world. The search is then repeated to find the hash of the next block. This process consumes a fairly large amount of electricity.

On blockchain.com web page, you can also notice five important input parameters that affect the difficulty of finding a valid hash.

  • Difficulty is a mathematical value of how hard it is to find a valid hash for current block. Difficulty says how many hashes are needed per interval. Mining difficulty is adjusted periodically (every two weeks) and continuously.

The mining difficulty determines how long it takes in average (number of attempts) to find a suitable input set that results in a valid hash output in order to mine a block. Usually miners or block creators (like in Cardano network) are required to find a hash below a certain target. The higher the mining difficulty is the more attempts are necessary in average to find a correct result.

  • Merkle Root is the root node of a merkle tree, a descendant of all the hashed pairs in the tree. Or, in other words, a miner’s software converts all the transactions into a summary view called a merkle root, and then hashes it. Each hashed Tx is just further hashed.
  • Nonce is random value that can be adjusted to satisfy the Proof-of-Work.
  • Timestamp in the following format: 2021–09–18 20:41.
  • Version is a block version related to protocol proposals underway.

Sixth important input parameter is a Previous Block Hash.

What do I need a Nonce for?

Technically, miners are trying to find a nonce, that will help them to generate a valid hash. Or I can say that hash entirely depends on nonce in this game. Nonce is a natural decimal number that starts from 0. Hash isn’t predictable (since it’s created with SHA-256 algorithm), so all the miners have to try a lot of times to find a suitable nonce that produces a hash with several leading zeros. The more leading zeroes in hash — the more rare hash is.

In theory, you can find the hash on the first try, when nonce=1, but in practice this is absolutely impossible. Let’s experiment with input nonce value— imagine that we’re looking for a hash starting with just one zero.

nonce = 0
5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9
nonce = 1
6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
nonce = 2
d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35
...
...
nonce = 39
0b918943df0962bc7a1824c0555a389347b4febdc7cf9d1254406d80ce44e3f9

As you can see, I managed to find such a hash on the 39th try. Now two things are obvious for me: firstly, changing the nonce by 1 leads to a dramatic change in the hash value — every bit of the hash changes; secondly, number of tries to find a hash is really high.

Test Mining app for Mac OS

I’ve written a tiny app showing you how mining is computationally intensive. The purpose of this app is a demonstration of the difficulty of finding a hash with given parameters, for instance, with 2, 4, or 8 leading zeros. To run this app just copy-paste the following code into SwiftUI project in Xcode 12.5 or Xcode 13.0.

Tip: Take into consideration, that my Swift code does not reflect the criteria for bitcoin mining! My script fulfills overflow addition (some sort of looping) for nonce, that is unsigned 32-bit integer here.

import SwiftUI
import CryptoKit
struct ContentView: View {

@State var nonce: UInt32 = 0
@State var digest: SHA256.Digest? = nil
@State var zeros: String = "SHA256 digest: 00"

var body: some View {
ZStack {
if #available(macOS 12.0, *) {
Rectangle()
.frame(minWidth: 300, minHeight: 200)
.foregroundColor(.indigo)
}
Button("Start") {
self.startMining()
}
}
}

fileprivate func startMining() {
repeat {
nonce &+= 1

let data = Data(String(counter).utf8)
digest = SHA256.hash(data: data)

print((digest?.description)!)
print("\(counter) tries.")

} while !(digest!.description.starts(with: zeros))
}
}

Bitcoin network uses SHA-256 to generate a 32-byte numbers in a way that requires a predictable amount of processor efforts. Mining equipment is solving blocks’ hash that meet a certain criteria, established by the system.

Run the app and see what we got in the Xcode console if we search for 00:

SHA256 digest: 00328ce57bbc14b33bd6695bc8eb32cdf2fb5f3a7d89ec14a42825e15d39df60
286 tries.

Then change a zero property for "SHA256 digest: 0000" value:

SHA256 digest: 0000a456e7b5a5eb059e721fb431436883143101275c4077f83fe70298f5623d
88484 tries.

And at last, search for 00000000:

SHA256 digest: 00000000690ed426ccf17803ebe2bd0884bcd58a1bb5e7477ead3645f356e7a9
426479724 tries.

As you can see, we need 426 million tries to find a hash number with eight leading zeros 00000000. Now you know how much efforts a mining task requires and to what extent it loads the CPU.

What is happening in reality…

Everything that I have explained before, was just a part of the full picture. In reality, valid hash is formed from a current block header (read further). Since SHA-256 output is absolutely unpredictable, every miner has to find a hash by brute force method. A brute-force search is repeated until miners discover a hash that is less than the target hash composed by Bitcoin network.

Let’s decipher this. Here’s a sequence of actions for all miners:

  • Take a block header as input. Block header includes: nonce, Tx data (messages), timestamp, version, difficulty, merkle root hash and a previous block hash.
  • Change the nonce with 1 increment, while leaving all the parameters unchanged.
  • If a resulting hash is lower than the target hash, the math puzzle is successfully solved and lucky miners are free to submit a new block to the network. All the active nodes in the network, after checking the correctness of the nonce and verifying all the transactions in the mined block, must approve or reject it. If some node thinks that a block is invalid, it doesn’t relay the block. But in case everything’s fine, all network nodes have to update their input values for that block. And rewarding time begins…
  • If someone else won the competition, you must go to the beginning of this sequence and start from scratch.
  • If a race condition occured — the longest branch would succeed (i.e. canonical chain with the most work in it). Also, the longest branch solves the double spending problem.

The hash value of each subsequent block in the blockchain is less than the previous one, which means that in the year 2140 a block with the smallest hash value in the network will be created. Guess what hash it will be?

That’s all for now.

If this post is useful for you, please press the Clap button and hold it.

Happy mining!

--

--