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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ flake8 = "*"
[packages]
requests = ">=2.20.0"
flask = ">=1.0.0"
flask-cors = "*"

[requires]
python_version = "3.7"
72 changes: 46 additions & 26 deletions Pipfile.lock

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

46 changes: 34 additions & 12 deletions basic_block_gp/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):
self.current_transactions = []

# Create the genesis block
self.new_block(previous_hash=1, proof=100)
self.new_block(previous_hash="I'm a teapot", proof=100)

def new_block(self, proof, previous_hash=None):
"""
Expand All @@ -31,15 +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_hash': previous_hash or 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(block):
def hash(self, block):
"""
Creates a SHA-256 hash of a Block

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

# TODO: Create the block_string
# stringifys json # keys in same order for hashes
string_object = json.dumps(block, sort_keys=True)
# strip metadata from string
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 +78,7 @@ def hash(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 +92,12 @@ 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,10 +111,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[:3] == "000"

# Instantiate our Node
app = Flask(__name__)
Expand All @@ -109,16 +124,21 @@ 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