From fbbc04e793efdc962c21476136b6da7b8878295b Mon Sep 17 00:00:00 2001 From: Haruka Date: Sun, 14 Jul 2024 03:06:08 +0900 Subject: [PATCH] add webapi function for index update --- db/validator.py | 2 +- webapi/block_routes.py | 46 +++++++++++++++++++++++++++++++++++++++++- webapi/webapi.py | 18 +++-------------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/db/validator.py b/db/validator.py index 547d1d5b..376a7035 100644 --- a/db/validator.py +++ b/db/validator.py @@ -117,7 +117,7 @@ async def get_validator_uptime(self, address: str) -> Optional[float]: await self.message_callback(ExplorerMessage(ExplorerMessage.Type.DatabaseError, e)) raise - async def get_current_validator_count(self): + async def get_current_validator_count(self) -> int: async with self.pool.connection() as conn: async with conn.cursor() as cur: try: diff --git a/webapi/block_routes.py b/webapi/block_routes.py index 904f6f4d..106d2e22 100644 --- a/webapi/block_routes.py +++ b/webapi/block_routes.py @@ -1,10 +1,54 @@ +from typing import Any + from starlette.requests import Request from db import Database from webapi.utils import SJSONResponse +async def get_summary(db: Database): + network_speed = await db.get_network_speed() + validators = await db.get_current_validator_count() + participation_rate = await db.get_network_participation_rate() + block = await db.get_latest_block() + summary = { + "latest_height": block.height, + "latest_timestamp": block.header.metadata.timestamp, + "proof_target": block.header.metadata.proof_target, + "coinbase_target": block.header.metadata.coinbase_target, + "network_speed": network_speed, + "validators": validators, + "participation_rate": participation_rate, + } + return summary + + async def recent_blocks_route(request: Request): db: Database = request.app.state.db recent_blocks = await db.get_recent_blocks_fast(10) - return SJSONResponse(recent_blocks) \ No newline at end of file + return SJSONResponse(recent_blocks) + +async def index_update_route(request: Request): + db: Database = request.app.state.db + last_block = request.query_params.get("last_block") + if last_block is None: + return SJSONResponse({"error": "Missing last_block parameter"}, status_code=400) + try: + last_block = int(last_block) + except ValueError: + return SJSONResponse({"error": "Invalid last_block parameter"}, status_code=400) + if last_block < 0: + return SJSONResponse({"error": "Negative last_block parameter"}, status_code=400) + summary = await get_summary(db) + result: dict[str, Any] = {"summary": summary} + latest_height = await db.get_latest_height() + if latest_height is None: + return SJSONResponse({"error": "Database error"}, status_code=500) + block_count = latest_height - last_block + if block_count < 0: + return SJSONResponse({"summary": summary}) + if block_count > 10: + block_count = 10 + recent_blocks = await db.get_recent_blocks_fast(block_count) + result["recent_blocks"] = recent_blocks + return SJSONResponse(result) \ No newline at end of file diff --git a/webapi/webapi.py b/webapi/webapi.py index 81a45e60..2587266d 100644 --- a/webapi/webapi.py +++ b/webapi/webapi.py @@ -17,7 +17,7 @@ from middleware.auth import AuthMiddleware from middleware.server_timing import ServerTimingMiddleware from util.set_proc_title import set_proc_title -from .block_routes import recent_blocks_route +from .block_routes import get_summary, recent_blocks_route, index_update_route from .error_routes import bad_request, not_found, internal_error from .utils import out_of_sync_check, SJSONResponse @@ -46,20 +46,7 @@ async def sync_info_route(request: Request): async def summary_route(request: Request): db: Database = request.app.state.db - network_speed = await db.get_network_speed() - validators = await db.get_current_validator_count() - participation_rate = await db.get_network_participation_rate() - block = await db.get_latest_block() - summary = { - "latest_height": block.height, - "latest_timestamp": block.header.metadata.timestamp, - "proof_target": block.header.metadata.proof_target, - "coinbase_target": block.header.metadata.coinbase_target, - "network_speed": network_speed, - "validators": validators, - "participation_rate": participation_rate, - } - return SJSONResponse(summary) + return SJSONResponse(await get_summary(db)) routes = [ Route("/", index_route), @@ -67,6 +54,7 @@ async def summary_route(request: Request): Route("/summary", summary_route), Route("/block/recent", recent_blocks_route), + Route("/block/index_update", index_update_route), ] exc_handlers = {