Skip to content

Instantly share code, notes, and snippets.

@tuxcanfly
Last active December 15, 2015 18:59
Show Gist options
  • Save tuxcanfly/5308479 to your computer and use it in GitHub Desktop.
Save tuxcanfly/5308479 to your computer and use it in GitHub Desktop.
from hashlib import sha256
def mine_block(message, target, prev_block=''):
"""
use the message and previous block to generate a new block
"""
nonce = 0
if prev_block:
message = '%s\n%s' % (message, prev_block)
maybe = message
sha = sha256(maybe).hexdigest()
while not sha.startswith('0' * target):
maybe = '%s ------ [%s]' % (message, str(nonce))
sha = sha256(maybe).hexdigest()
nonce += 1
return maybe
target = 4
first_block = mine_block("G1: Let's attack at 1337 hours", target=target)
second_block = mine_block("G2: Let's attack at 1337 hours", target=target, prev_block=first_block)
third_block = mine_block("G4: Let's attack at 1337 hours", target=target, prev_block=second_block)
print first_block
print '========='
print second_block
print '========='
print third_block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment