Blockchain mining for dummies

Óliver Hierro
3 min readJun 14, 2021
Photo by Albert Hyseni on Unsplash

Some years ago, when I’ve started to hear about blockchain technology, bitcoin, and some other alts, one of the most bizarre things for me was to visualize and understand the mining process.

Specially, when you hear all the time, that is a process very hard to calculate but, at the same time, very simple to check.

Well, in this article, I’ll try to explain as simple as I can at the same time that we test some algorithms and do some basic “mining process.”

Hashing

Hashing is a mathematical process of transform any input into new, length-fixed data.

For example, if you are on Linux, it’s really probably that you have some command to calculate some hash with different algorithms (yeah, there isn’t only one!)

echo "Hola mundo" | md5sum
f1d2e57f0812fd967e39bab6c8f8d762 -

There are some interesting points in the hashing functions:

  • Fixed length: It doesn’t matter if your input has 20 or 20K characters. The output will always be the same length
echo "Hola mundo" | md5sum
f1d2e57f0812fd967e39bab6c8f8d762 -
echo "Hola mundo from a hashing algorithm" | md5sum
c12ff3b34fd524e786569ae0a13cff12 -
  • Variability: One little change in the output generates a totally new hash
echo "ola mundo from a hashing algorithm" | md5sum // I removed a 
// single char
10c905b621d70f7944af0c151bda13dd -
  • Validation: If you have the input and the hashing algorithm, it’s straightforward to check if the document is unaltered
  • Collisions: Good hashing algorithms have a very low collisions rate, meaning it is practically impossible to get the same hash from 2 different inputs.
  • One way: It’s really simple (for computers) to calculate a hash from an input but, it’s totally impossible to get the original input from a hash

Hash + salt

As you can see just above, if we change only a smart part of the input, the output will be totally different and unpredictable until it’s calculated.

Imagine that I told you that I need to calculate the hash of the next input:

<SOME NUMBER> + "Some kind of input"

With this, we can calculate, for example, the next hashes:

echo "0 Some kind of input" | md5sum               
10a61dd1212f5d15a7d7ba325e7f8054 -
echo "1 Some kind of input" | md5sum
50bf0b2e5d7d14fbdd3e7c2da5f98ec7 -
..
echo "50 Some kind of input" | md5sum
fb9cefea377c30b5dde0cf045015d255 -

And now, imagine that I told you that a VALID number is that which makes your hash start with 2 zeros. What can you do to get this valid number?

Try with a lot of different numbers until you found one valid!

for i in {1..100}; do echo "$i: $(echo "$i Some kind of input" | md5sum | cut -c1,2 | grep 00)"; done;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31: 00 // VALID VALUE!!

Ok, our magic number is 31:

cho "31 Some kind of input" | md5sum                                                                  
0050b260e0236ae6c5dea4f201ac402f -

With this process, we’re mining until we found our “imaginary” diamond: the number 31.

Next steps

If we found this value in a blockchain environment, our next task is to broadcast our discovery. The task of the other nodes of the system is to check if our number is valid, but, as you now know, it’s a simple task. (Just take the number and check if the hash starts with 2 zeroes).

This is, roughly, the blockchain mining process.

As the number of zeros increments, the difficulty of getting these numbers is increased.

Bitcoin mining difficulty (from https://www.statista.com/statistics/1224431/mining-difficulty-bitcoin/)

To get an idea of how difficult it could be to get these hashes, if you check the block explorer of bitcoin, you can see that the latest blocks have 19 zeros at the beginning of the hash.

00000000000000000005660b9cd75aa606bf26702a455cac10d1bdd61313b6f4

Links:

--

--

Óliver Hierro

Desarrollador de software en @Hiberus. Java, JavaScript, Angular, Vue. Profesor en @Sanvalero. Papá de Ian y Zoe.