In Blockchain, we ask workers to generate a SHA-256 hash with a certain number of leading zeros. In this case we will add a nonce to create a hash with one leading zero:
Blockchain work |
Code
The following is an outline of the code:
import hashlib import sys val_in='hello' hex_dig='' working='' nouce=0 while True: val=val_in+str(nouce) inp=val.encode() hash_object = hashlib.sha256(inp) hex_dig = hash_object.hexdigest() working=working+val+hex_dig+'\n' if (hex_dig.startswith("0")): break nouce=nouce+1 print 'Result is ',val print 'Hash is ',hex_dig
Within the Bitcoin network we compute a new block a given time periods, and then a hash is calculated on the new transactions. The miners must then find a nonce value which will produce 18 preceding zeros. The miner that finds this will receive a reward of 12.5 BTC: