Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"activityBar.background": "#2E1D71",
"titleBar.activeBackground": "#40299E",
"titleBar.activeForeground": "#FAF9FD"
}
},
"python.pythonPath": "/Users/Ben 2.0/.local/share/virtualenvs/Blockchain-u_affrwC/bin/python"
}
41 changes: 32 additions & 9 deletions basic_block_gp/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,30 @@ def hash(block):
def last_block(self):
return self.chain[-1]

def proof_of_work(self, last_proof):
def proof_of_work(self):
"""
Simple Proof of Work Algorithm
Find a number p such that hash(last_block_string, p) contains 6 leading
zeroes
"""

pass
block_string = json.dumps(self.last_block, sort_keys=True).encode()
proof = 0
while not self.valid_proof(block_string, proof):
proof +=1

return proof

@staticmethod
def valid_proof(last_proof, proof):
def valid_proof(block_string, proof):
"""
Validates the Proof: Does hash(block_string, proof) contain 6
leading zeroes?
"""
# TODO
pass
guess = f'{block_string}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"

def valid_chain(self, chain):
"""
Expand All @@ -100,21 +107,25 @@ def valid_chain(self, chain):
:return: <bool> True if valid, False if not
"""

last_block = chain[0]
prev_block = chain[0]
current_index = 1

while current_index < len(chain):
block = chain[current_index]
print(f'{last_block}')
print(f'{prev_block}')
print(f'{block}')
print("\n-------------------\n")
# Check that the hash of the block is correct
# TODO: Return false if hash isn't correct

if block['previous_hash'] != self.hash(prev_block):
return False
# Check that the Proof of Work is correct
# TODO: Return false if proof isn't correct
block_string = json.dumps(self.prev_block, sort_keys=True).encode()
if not self.valid_proof(block_string, block['proof']):
return False

last_block = block
prev_block = block
current_index += 1

return True
Expand All @@ -135,14 +146,18 @@ def mine():
# We run the proof of work algorithm to get the next proof...
proof = blockchain.proof_of_work()

print(proof)

# We must receive a reward for finding the proof.
# TODO:
# The sender is "0" to signify that this node has mine a new coin
# The recipient is the current node, it did the mining!
# The amount is 1 coin as a reward for mining the next block

blockchain.new_transaction(0, node_identifier, 1)
# Forge the new Block by adding it to the chain
# TODO
last_block_hash = blockchain.hash(blockchain.last_block)
block = blockchain.new_block(proof, last_block_hash)

# Send a response with the new block
response = {
Expand Down Expand Up @@ -177,9 +192,17 @@ def new_transaction():
def full_chain():
response = {
# TODO: Return the chain and its current length
'chain':blockchain.chain,
'length': len(blockchain.chain)
}
return jsonify(response), 200

@app.route('/chain_validity', methods=['GET'])
def chain_validity():
response = {
"valid_chain": blockchain.valid_chain(blockchain.chain)
}
return jsonify(response), 200

# Run the program on port 5000
if __name__ == '__main__':
Expand Down
Loading