Skip to content

Commit 54a0b69

Browse files
authored
Merge pull request #192 from Hemanth11011/feature/myfeature
Add basic blockchain and update README
2 parents b431c2f + 20b8826 commit 54a0b69

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-9
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import datetime
2+
import hashlib
3+
import json
4+
from flask import Flask,jsonify
5+
6+
# Building the Blockchain
7+
8+
class Blockchain:
9+
10+
def __init__(self):
11+
self.chain = []
12+
self.create_block(proof = 1, previous_hash='0')
13+
14+
def create_block(self,proof,previous_hash):
15+
block = {'index': len(self.chain)+1,
16+
'timestamp': str(datetime.datetime.now()),
17+
'proof': proof,
18+
'previous_hash': previous_hash
19+
}
20+
self.chain.append(block)
21+
return block
22+
23+
def get_previous_block(self):
24+
return self.chain[-1]
25+
26+
def proof_of_work(self,previous_proof):
27+
new_proof = 1
28+
check_proof = False
29+
while check_proof is False:
30+
hash_operation = hashlib.sha256(str(new_proof**2 - previous_proof**2).encode()).hexdigest()
31+
if hash_operation[:4] == '0000':
32+
check_proof = True
33+
else:
34+
new_proof += 1
35+
return new_proof
36+
37+
def hash(self,block):
38+
encoded_block = json.dumps(block,sort_keys=True).encode()
39+
return hashlib.sha256(encoded_block).hexdigest()
40+
41+
def is_chain_valid(self,chain):
42+
previous_block = chain[0]
43+
block_index = 1
44+
while block_index < len(chain):
45+
block = chain[block_index]
46+
if block['previous_hash'] != self.hash(previous_block):
47+
return False
48+
previous_proof = previous_block['proof']
49+
proof = block['proof']
50+
hash_operation = hashlib.sha256(str(proof**2 - previous_proof**2).encode()).hexdigest()
51+
if hash_operation[:4] != '0000':
52+
return False
53+
previous_block = block
54+
block_index += 1
55+
return True
56+
57+
# Mining the Blockchain
58+
59+
app = Flask(__name__)
60+
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
61+
62+
blockchain = Blockchain()
63+
64+
@app.route('/mine_block', methods=['GET'])
65+
def mine_block():
66+
previous_block = blockchain.get_previous_block()
67+
previous_proof = previous_block['proof']
68+
proof = blockchain.proof_of_work(previous_proof)
69+
previous_hash = blockchain.hash(previous_block)
70+
block = blockchain.create_block(proof, previous_hash)
71+
response = {'message': 'Congratulations, you just mined a block',
72+
'index': block['index'],
73+
'timestamp': block['timestamp'],
74+
'proof': block['proof'],
75+
'previous_hash': block['previous_hash']}
76+
return jsonify(response), 200
77+
78+
@app.route('/get_chain', methods=['GET'])
79+
def get_chain():
80+
response = {'chain':blockchain.chain,
81+
'length':len(blockchain.chain)}
82+
return jsonify(response), 200
83+
84+
@app.route('/is_valid', methods=['GET'])
85+
def is_valid():
86+
is_valid = blockchain.is_chain_valid(blockchain.chain)
87+
if is_valid:
88+
response = {'message':'The Blockchain is valid'}
89+
else:
90+
response = {'message':'The Blockchain is invalid'}
91+
return jsonify(response),200
92+
93+
app.run(host='0.0.0.0',port=5000)
94+
95+
96+
97+
98+
99+

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
2. **Clone your fork:** Clone your forked repository to your local machine.
2323

24-
```bash
25-
git clone https://github.com/your-username/A-Z-Python-Projects.git
26-
```
24+
```bash
25+
git clone https://github.com/your-username/A-Z-Python-Projects.git
26+
```
2727

2828
3. **Choose your project:** Explore the project folders, pick a Python project that aligns with your interest and skill level.
2929

@@ -41,15 +41,15 @@ We welcome contributions from developers of all skill levels. Follow these steps
4141

4242
3. **Commit your changes:** Commit your changes to your branch.
4343

44-
```bash
45-
git commit -m "Merged and update README"
46-
```
44+
```bash
45+
git commit -m "Merged and update README"
46+
```
4747

4848
4. **Push your changes:** Push your branch to your forked repository on GitHub.
4949

50-
```bash
51-
git push origin feature/your-feature-name
52-
```
50+
```bash
51+
git push origin feature/your-feature-name
52+
```
5353

5454
5. **Create a Pull Request:** Go to the [A-Z-Python-Projects repository](https://github.com/Techiral/A-Z-Python-Projects/) on GitHub and click on the "New Pull Request" button. Select your branch, add a descriptive title and comments, and submit the Pull Request.
5555

@@ -94,6 +94,7 @@ We appreciate the contributions from the following community members:
9494
- [Harsimran Singh](https://github.com/Harsimran-19)
9595
- [Ezhill Ragesh](https://github.com/ezhillragesh)
9696
- [Brunda Bharadwaj](https://github.com/brundabharadwaj/)
97+
- [Hemanth Singh](https://github.com/Hemanth11011)
9798

9899
---
99100

0 commit comments

Comments
 (0)