Skip to content

Instantly share code, notes, and snippets.

@cx0der
Created August 12, 2018 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cx0der/c2cd2440a5b15fb875e5ba6a3d5d9157 to your computer and use it in GitHub Desktop.
Save cx0der/c2cd2440a5b15fb875e5ba6a3d5d9157 to your computer and use it in GitHub Desktop.
Mining PyChain
import hashlib
difficulty = 4
pattern = '0000'
def calculate_hash(data_to_hash):
return hashlib.sha256(data_to_hash.encode("utf-8")).hexdigest()
def mine_block(index, prev_hash, timestamp, data):
for nonce in range(1, maxNonce):
blockVal = str(index) + str(nonce) + prev_hash + str(timestamp) + data
hash = calculate_hash(blockVal)
if hash[:difficulty] == pattern:
return Block(index, nonce, prev_hash, timestamp, data, hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment