diff --git a/endpoints/get_hashrate.py b/endpoints/get_hashrate.py index 1929edf..146d93a 100644 --- a/endpoints/get_hashrate.py +++ b/endpoints/get_hashrate.py @@ -1,10 +1,11 @@ # encoding: utf-8 -import time +import json from pydantic import BaseModel from sqlalchemy import select from dbsession import async_session +from helper import KeyValueStore from models.Block import Block from server import app, kaspad_client @@ -52,31 +53,32 @@ async def get_max_hashrate(): """ Returns the current hashrate for Kaspa network in TH/s. """ - global MAXHASH_CACHE - - if time.time() - MAXHASH_CACHE[0] < 60 * 5: - return MAXHASH_CACHE[1] + maxhash_last_value = json.loads((await KeyValueStore.get("maxhash_last_value")) or "{}") + maxhash_last_bluescore = int((await KeyValueStore.get("maxhash_last_bluescore")) or 0) + print(f"Start at {maxhash_last_bluescore}") async with async_session() as s: - tx = await s.execute(select(Block) - .order_by(Block.difficulty.desc()).limit(1)) - - tx = tx.first() - - hashrate = tx[0].difficulty * 2 - hashrate_in_th = hashrate / 1_000_000_000_000 - - response = {"hashrate": hashrate_in_th, - "blockheader": - { - "hash": tx[0].hash, - "timestamp": tx[0].timestamp.isoformat(), - "difficulty": tx[0].difficulty, - "daaScore": tx[0].daa_score, - "blueScore": tx[0].blue_score + block = (await s.execute(select(Block) + .filter(Block.blue_score > maxhash_last_bluescore) + .order_by(Block.difficulty.desc()).limit(1))).scalar() + + hashrate_new = block.difficulty * 2 + hashrate_old = maxhash_last_value.get("blockheader", {}).get("difficulty", 0) * 2 + + await KeyValueStore.set("maxhash_last_bluescore", str(block.blue_score)) + + if hashrate_new > hashrate_old: + response = {"hashrate": hashrate_new / 1_000_000_000_000, + "blockheader": + { + "hash": block.hash, + "timestamp": block.timestamp.isoformat(), + "difficulty": block.difficulty, + "daaScore": block.daa_score, + "blueScore": block.blue_score + } } - } - - MAXHASH_CACHE = (time.time(), response) + await KeyValueStore.set("maxhash_last_value", json.dumps(response)) + return response - return response + return maxhash_last_value diff --git a/helper/KeyValueStore.py b/helper/KeyValueStore.py new file mode 100644 index 0000000..8d159d1 --- /dev/null +++ b/helper/KeyValueStore.py @@ -0,0 +1,28 @@ +# encoding: utf-8 +from sqlalchemy import select, update, insert + +from dbsession import async_session +from models.Variable import KeyValueModel + + +async def get(key): + async with async_session() as s: + result = await s.execute(select(KeyValueModel.value).where(KeyValueModel.key == key)) + return result.scalar() + + +async def set(key, value): + async with async_session() as s: + result = await s.execute(update(KeyValueModel) + .where(KeyValueModel.key == key) + .values(value=value)) + + if result.rowcount == 1: + await s.commit() + return True + + result = await s.execute(insert(KeyValueModel).values(key=key, value=value)) + + await s.commit() + + return True diff --git a/models/Variable.py b/models/Variable.py new file mode 100644 index 0000000..f7ae535 --- /dev/null +++ b/models/Variable.py @@ -0,0 +1,9 @@ +from sqlalchemy import Column, String, Text + +from dbsession import Base + + +class KeyValueModel(Base): + __tablename__ = 'vars' + key = Column(String, primary_key=True) + value = Column(Text)