What is bitcoin mining?

Bitcoin mining is a process of assembling transactions into a block, and find a SHA-256 hash of that block which is smaller or equal to the target. Although there are lots of technical challenges, conceptually it is an extremely easy process.

Let me explain what bitcoin mining is about in few lines of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import hashlib
target = '0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
object = 'Hello World!'
hash = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
nounce = -1
printInterval = 10000
while hash > target:
nounce += 1
newObject = object + str(nounce)
hash = hashlib.sha256(newObject.encode('utf-8')).hexdigest()
if nounce % printInterval == 0:
print(newObject, hash)

print("--------------")
print("I mined my first block!")
print((object + str(nounce)), hash)

If you execute above code in python3, you will see outputs similar to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Hello World!0 e59f8bdf1305e382a4919ccefd613d3eebae612aa4c443f3af2d65663de3b075
Hello World!10000 ed455b124e890d4a3df2d7c98e877e0a3ee6d31e56d3c06b6ce403d2bcf4e2d1
Hello World!20000 64de0951829f1dab7a7a8574dc21ea8bc0ff9533c332ea8690d1e99d6fb1547d
Hello World!30000 7f9afc176d90d717de36f545709c1918a2163a7c42253cb2cfc2b4500a32df27
Hello World!40000 0b39e323e3eee0ff5395a92f28193f3fc7106cfaff1f666dadf05229740026bd
Hello World!50000 f65309aeefedcf867c7110de0cf5923fba358aae503c6f31a949b10cb1df4345
Hello World!60000 9e3667e071623834cf818d4ac56de4132cd873371cb6187aed75150d2e21e26d
Hello World!70000 f6891c3b4d39e4dd90874bd969d670927ce87a943d07f47214f69ac7a4e2b120
Hello World!80000 1596058128d8431628825f595c377ba8093534c003bcf7622374c199f650606d
Hello World!90000 ca7793d181e793423458274762a1b91871230b1e9a1ffe1d08b0045354e1c121
Hello World!100000 a1a8afcd5bbc1278a5d2411b1c14c41203cf6f64b7689887c3be59137f7b8582
Hello World!110000 6d527deffadd6c0134157a4c8211b68220c393f6bd7c6839e831cc77720fa7f1
Hello World!120000 4c766a7a3e46da10dbfdcfdcea04c98173b8b000d4f72e850a6ded8c9edc62d7
Hello World!130000 fd36741d4857c463c27c6705c32f8f59988f8da378875345c1ef9951301fcb89
Hello World!140000 948e3b3a1972f4f388f50af4eb357786a6ba52ae8c10cc682d2f91afc1631aae
Hello World!150000 acf645ce21af88aef02895611db44ba3fcc7872c55a16ff1fd8d8e7fc9559ef7
Hello World!160000 177512958f0e8ba9ee0024256af291e7a006d2ce3d2cca89c65539712dca9a37
--------------
I mined my first block!
Hello World!167018 0000ff98e1d01d7d3cf836055f9fe44256f222135c7afd993766e2ff6159d5a4

As you can see, I print out hash every 10000 iteration to save some space. At the end, I found a hash which is smaller than target. Is that it? Yes! This is all about bitcoin mining, and lots of other crypto mining processes are very similar.

In reality, the object won’t be a string. Instead, it is an object containing all transaction information along with other meta data, and encoded into binary form.