Skip to content

Instantly share code, notes, and snippets.

Last active Apr 8, 2018
What would you like to do?
Step 4: Proof of Work
import hashlib
import json
from time import time
from uuid import uuid4
class Blockchain(object):
def proof_of_work(self, last_proof):
Simple Proof of Work Algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
- p is the previous proof, and p' is the new proof
:param last_proof: <int>
:return: <int>
proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1
return proof
def valid_proof(last_proof, proof):
Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?
:param last_proof: <int> Previous Proof
:param proof: <int> Current Proof
:return: <bool> True if correct, False if not.
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"
Copy link

zhouzhou84 commented Mar 29, 2018

guess = f'{last_proof}{proof}'.encode()

File "", line 50
SyntaxError: invalid syntax

that single quote mark has some problems ,could you please help me...

Copy link

taroballs commented Apr 8, 2018

I met the same mistakes but I fixed it with improving my python version to 3.6.@zhouzhou84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment