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
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.

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

block = {
# TODO
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_block': previous_hash or self.hash(self.last_block) # or self.hash(self.last_block)
# if previous_hash is not passed in, we can use self.hash(self.chain[-1])
}

# 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,25 +56,33 @@ def hash(self, block):
"""

# Use json.dumps to convert json into a string
string_block = json.dumps(block, sort_keys=True) # makes sure that no matter what all the keys in dictionary are turned
# into strings in alphabetical order
# hash would be different otherwise.
# 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())
# returns an object, so we want a hexadecimal string
# 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


# TODO: Hash this string using sha256



# By itself, the sha256 function returns the hash in a raw string
# that will likely include escaped characters.
# 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,8 +96,14 @@ 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
# convert the block into a string
block_string = json.dumps(block, sort_keys=True)
# how to look for proof?
# Guess a bunch of numbers so we start at 0
proof = 0
while self.valid_proof(block_string, proof) is False:
proof += 1 # while we have not found a good number, keep looking for a good no.
return proof
# return proof

@staticmethod
Expand All @@ -96,11 +118,14 @@ 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
# we are going to make guesses
guess = f'{block_string}{proof}'.encode() # encode the guess to make it work
# then we hash the guess as well
guess_hash = hashlib.sha256(guess).hexdigest()
# hexdigest to make it mutable
# return True or False


# return it only if the first three have 000s
return guess_hash[:3] == "000"
# Instantiate our Node
app = Flask(__name__)

Expand All @@ -114,24 +139,29 @@ 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



@app.route('/chain', methods=['GET'])
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)
Loading