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

MVP Done #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
63 changes: 34 additions & 29 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 33 additions & 12 deletions basic_block_gp/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ def new_block(self, proof, previous_hash=None):
"""

block = {
# TODO
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.last_block)
}

# Reset the current list of transactions
self.current_transactions = []
# Append the chain to the block
self.chain.append(block)
# Return the new block
pass
return block

def hash(self, block):
"""
Expand All @@ -48,12 +54,15 @@ def hash(self, block):
"""

# Use json.dumps to convert json into a string
string_block = json.dumps(block, sort_keys=True)
# Use hashlib.sha256 to create a hash
# It requires a `bytes-like` object, which is what
# .encode() does.
raw_hash = hashlib.sha256(string_block.encode())
# It converts the Python string into a byte string.
# We must make sure that the Dictionary is Ordered,
# or we'll have inconsistent hashes


# TODO: Create the block_string

Expand All @@ -64,9 +73,10 @@ def hash(self, block):
# This can be hard to read, but .hexdigest() converts the
# hash to a string of hexadecimal characters, which is
# easier to work with and understand
hex_hash = raw_hash.hexdigest()

# TODO: Return the hashed block string in hexadecimal format
pass
return hex_hash

@property
def last_block(self):
Expand All @@ -80,14 +90,18 @@ def proof_of_work(self, block):
in an effort to find a number that is a valid proof
:return: A valid proof for the provided block
"""
# TODO
pass
# return proof
block_string = json.dumps(block, sort_keys=True)

proof = 0
while self.valid_proof(block_string, proof) is False:
proof += 1

return proof

@staticmethod
def valid_proof(block_string, proof):
"""
Validates the Proof: Does hash(block_string, proof) contain 3
Validates the Proof: Does hash(block_string + proof) contain 3
leading zeroes? Return true if the proof is valid
:param block_string: <string> The stringified block to use to
check in combination with `proof`
Expand All @@ -96,9 +110,11 @@ def valid_proof(block_string, proof):
correct number of leading zeroes.
:return: True if the resulting hash is a valid proof, False otherwise
"""
# TODO
pass
# return True or False

guess = f'{block_string}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()

return guess_hash[:6] == "000000"


# Instantiate our Node
Expand All @@ -114,11 +130,14 @@ def valid_proof(block_string, proof):
@app.route('/mine', methods=['GET'])
def mine():
# Run the proof of work algorithm to get the next proof
proof = blockchain.proof_of_work(blockchain.last_block)

# Forge the new Block by adding it to the chain with the proof
previous_hash = blockchain.hash(blockchain.last_block)
block = blockchain.new_block(proof, previous_hash)

response = {
# TODO: Send a JSON response with the new block
'new_block': block
}

return jsonify(response), 200
Expand All @@ -128,10 +147,12 @@ def mine():
def full_chain():
response = {
# TODO: Return the chain and its current length
'chain': blockchain.chain,
'length': len(blockchain.chain)
}
return jsonify(response), 200


# Run the program on port 5000
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
app.run(host='0.0.0.0', port=5000)
Loading