Skip to content
Open

WIP #93

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: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ verify_ssl = true

[dev-packages]
flake8 = "*"
autopep8 = "*"
pylint = "*"

[packages]
requests = ">=2.20.0"
flask = ">=1.0.0"
pip = "*"

[requires]
python_version = "3.7"
94 changes: 81 additions & 13 deletions Pipfile.lock

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

15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Block-Chain-Python

https://imgur.com/xSlgvtl

## Projects

### Part 1 in Class Project
* Basic Setup and Proof of Work (basic_block_gp)

- Basic Setup and Proof of Work (basic_block_gp)

### Part 1 Take Home Project
* Client Miners (client_mining_p)

- Client Miners (client_mining_p)

### Part 2 in Class Project
* Basic Transactions (basic_transactions_gp)

- Basic Transactions (basic_transactions_gp)

### Part 2 Take Home Project
* Basic Wallet (basic_wallet_p)

Based on blockchain by dvf. Used under MIT license: https://github.com/dvf/blockchain
- Basic Wallet (basic_wallet_p)

Based on blockchain by dvf. Used under MIT license: https://github.com/dvf/blockchain
46 changes: 39 additions & 7 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 @@ -56,8 +62,12 @@ def hash(self, block):
# or we'll have inconsistent hashes

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

# TODO: Hash this string using sha256
# first convert to a byte string

hashed_block = hashlib.sha256(block_string.encode())

# 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 hashed_block.hexdigest()

@property
def last_block(self):
Expand All @@ -81,8 +91,13 @@ def proof_of_work(self, block):
:return: A valid proof for the provided block
"""
# TODO
pass
block_string = json.dumps(block, sort_keys=True)
proof = 0

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

@staticmethod
def valid_proof(block_string, proof):
Expand All @@ -97,7 +112,12 @@ def valid_proof(block_string, proof):
:return: True if the resulting hash is a valid proof, False otherwise
"""
# TODO
pass
guess = block_string + str(proof)
guess = guess.encode()
hash_value = hashlib.sha256(guess).hexdigest()

return hash_value[:3] == "000"

# return True or False


Expand All @@ -111,14 +131,25 @@ def valid_proof(block_string, proof):
blockchain = Blockchain()


@app.route("/", methods=['GET'])
def greet():
response = {
"greeting": "Hello, World!"
}

return jsonify(response), 200


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

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

return jsonify(response), 200
Expand All @@ -127,7 +158,8 @@ def mine():
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
# TODO: Return the chain and its current length
"len": len(blockchain.chain),
"chain": blockchain.chain
}
return jsonify(response), 200

Expand Down
Loading