BlockChain Mining – Solving a special task and waste some engergy

Recently I was reading lots of articles about bitcoin, etherium and blockchain in general.
I was just going deeper into bitcoin and at this point I understood that we have a sha256 hash function that is called on a block and the result is written down in the next block. But there is this mining and on many articles people write „the miner has to solve a special task“.

I will explain you whats meant by this special task.

The informations that we put into our new block are: hash of the previous block, the transactions and a random part.
And this random part contains the magic behind mining. The miner has to find a random number that changes the sha256 sum in a way that we have n-zeros in front.
If you never saw the hash of a bitcoin block than this is the right time.

If you want to see this in action… I wrote a little python script to test this and to get a feeling who long it talk to solve this special mining task.

# -*- coding: utf-8 -*-
from random import randint
import hashlib
import time

start_time = time.time()

myblock = {'mycontent': 'hello', 'random': 1}

def encrypt_string(hash_string):
    sha_signature = hashlib.sha256(hash_string.encode()).hexdigest()
    return sha_signature

while True:
    myblock['random'] = randint(0, 100000000)
    hash_string = str(myblock)
    sha_signature = encrypt_string(hash_string)
    pre = '000000'
    if sha_signature[0:len(pre)] == pre:
        print sha_signature
        print myblock
        print '--- %s seconds ---' % (time.time() - start_time)

As you probably know mining needs a lot of energy. And for what ? For some nice looking hashsums 😉

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.