Skip to content

Commit 15d7903

Browse files
authored
Update main.py
1 parent 4a61560 commit 15d7903

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
@app.route('/')
3131
def home():
32+
"""Home route to check the status of the API."""
3233
return jsonify({
3334
"message": "Welcome to the Ultra-Advanced Stable-Pi-Core Blockchain API",
3435
"status": "Running",
@@ -38,6 +39,7 @@ def home():
3839

3940
@app.route('/mine', methods=['POST'])
4041
async def mine_block():
42+
"""Route to mine a new block."""
4143
try:
4244
data = request.get_json()
4345
if not data or 'data' not in data:
@@ -59,21 +61,38 @@ async def mine_block():
5961

6062
@app.route('/chain', methods=['GET'])
6163
def get_chain():
64+
"""Route to retrieve the entire blockchain."""
6265
return jsonify(blockchain.to_dict()), 200
6366

6467
@app.route('/validate', methods=['POST'])
6568
async def validate_chain():
69+
"""Route to validate the blockchain."""
6670
try:
6771
is_valid = await asyncio.to_thread(blockchain.validate_chain)
6872
return jsonify({"is_valid": is_valid}), 200
6973
except Exception as e:
7074
logger.error(f"Validation error: {str(e)}")
7175
return jsonify({"error": "Chain validation failed."}), 500
7276

77+
@app.route('/stats', methods=['GET'])
78+
async def get_stats():
79+
"""Route to retrieve blockchain statistics."""
80+
try:
81+
stats = {
82+
"block_count": len(blockchain.chain),
83+
"latest_block": blockchain.chain[-1].to_dict() if blockchain.chain else None,
84+
"consensus_status": consensus_manager.get_status()
85+
}
86+
return jsonify(stats), 200
87+
except Exception as e:
88+
logger.error(f"Error retrieving stats: {str(e)}")
89+
return jsonify({"error": "Failed to retrieve statistics."}), 500
90+
7391
if __name__ == '__main__':
7492
# Load environment variables
7593
port = int(os.environ.get("PORT", 5000))
7694
host = os.environ.get("HOST", "0.0.0.0")
7795

7896
# Start the Flask application
97+
logger.info(f"Starting the application on {host}:{port}")
7998
app.run(host=host, port=port, threaded=True)

0 commit comments

Comments
 (0)