Skip to content

Commit

Permalink
add webapi function for index update
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaMa committed Jul 13, 2024
1 parent ad10546 commit fbbc04e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion db/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
46 changes: 45 additions & 1 deletion webapi/block_routes.py
Original file line number Diff line number Diff line change
@@ -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)
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)
18 changes: 3 additions & 15 deletions webapi/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,27 +46,15 @@ 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),
Route("/sync", sync_info_route),
Route("/summary", summary_route),

Route("/block/recent", recent_blocks_route),
Route("/block/index_update", index_update_route),
]

exc_handlers = {
Expand Down

0 comments on commit fbbc04e

Please sign in to comment.