Skip to content

Commit

Permalink
KeyValueStore in PostgreSQL added. (#22)
Browse files Browse the repository at this point in the history
hashrate/max with Cache via KeyValueStore.
  • Loading branch information
lAmeR1 authored Mar 14, 2023
1 parent 6109dd1 commit 77c4cca
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
52 changes: 27 additions & 25 deletions endpoints/get_hashrate.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
28 changes: 28 additions & 0 deletions helper/KeyValueStore.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions models/Variable.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 77c4cca

Please sign in to comment.