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
57 changes: 31 additions & 26 deletions Pipfile.lock

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

44 changes: 33 additions & 11 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,
'time': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}

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

def hash(self, block):
"""
Expand All @@ -56,8 +62,12 @@ def hash(self, block):
# or we'll have inconsistent hashes

# TODO: Create the block_string
string_object = json.dumps(block, sort_keys=True)
block_string = string_object.encode()

# TODO: Hash this string using sha256
raw_hash = hashlib.sha256(block_string)
hex_hash = raw_hash.hexdigest()

# By itself, the sha256 function returns the hash in a raw string
# that will likely include escaped characters.
Expand All @@ -66,7 +76,7 @@ def hash(self, block):
# easier to work with and understand

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

@property
def last_block(self):
Expand All @@ -80,9 +90,11 @@ 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):
Expand All @@ -96,9 +108,10 @@ 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 @@ -109,16 +122,23 @@ def valid_proof(block_string, proof):

# Instantiate the Blockchain
blockchain = Blockchain()
print(blockchain.chain)
print(blockchain.hash(blockchain.last_block))


@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)
new_block = blockchain.new_block(proof, previous_hash)


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

return jsonify(response), 200
Expand All @@ -128,6 +148,8 @@ 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

Expand Down
Loading