Created
August 12, 2018 14:24
-
-
Save cx0der/c2cd2440a5b15fb875e5ba6a3d5d9157 to your computer and use it in GitHub Desktop.
Mining PyChain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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