Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain.py inovation #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 30 additions & 8 deletions blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def valid_chain(self, chain):
# Check that the Proof of Work is correct
if not self.valid_proof(last_block['proof'], block['proof'], last_block_hash):
return False

#Check the maths
if not self.valid_maths(last_block['math'],current_index)
return False
last_block = block
current_index += 1

Expand Down Expand Up @@ -112,6 +114,7 @@ def new_block(self, proof, previous_hash):
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'math': self.math(len(self.chain)),
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}

Expand Down Expand Up @@ -154,25 +157,38 @@ def hash(block):
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()


def proof_of_work(self, last_block):
"""
Simple Proof of Work Algorithm:

- Find a number p' such that hash(pp') contains leading 4 zeroes
""" Simple Proof of Work Algorithm:

- Find a number p' such that hash(pp') contains leading 4 zeroes

- Where p is the previous proof, and p' is the new proof

:param last_block: <dict> last Block

:return: <int>
"""

""" last_proof = last_block['proof'] last_hash = self.hash(last_block)
last_proof = last_block['proof']

last_hash = self.hash(last_block)

proof = 0

while self.valid_proof(last_proof, proof, last_hash) is False:

proof += 1

return proof








@staticmethod
def valid_proof(last_proof, proof, last_hash):
Expand All @@ -189,8 +205,12 @@ def valid_proof(last_proof, proof, last_hash):
guess = f'{last_proof}{proof}{last_hash}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"


def valid_maths(math, index):
if math!=math(index):
return False
return True
def math(index):
return True
# Instantiate the Node
app = Flask(__name__)

Expand All @@ -206,6 +226,7 @@ def mine():
# We run the proof of work algorithm to get the next proof...
last_block = blockchain.last_block
proof = blockchain.proof_of_work(last_block)
math = blockchain.math(len(blockchain.chain))

# We must receive a reward for finding the proof.
# The sender is "0" to signify that this node has mined a new coin.
Expand All @@ -224,6 +245,7 @@ def mine():
'index': block['index'],
'transactions': block['transactions'],
'proof': block['proof'],
'math': math,
'previous_hash': block['previous_hash'],
}
return jsonify(response), 200
Expand Down