Skip to content

Commit

Permalink
kill util.CURRENT_BLOCK_TIME; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 15, 2025
1 parent 3546adb commit a3a2bdf
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 45 deletions.
5 changes: 2 additions & 3 deletions counterparty-core/counterpartycore/lib/api/apiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from counterpartycore.lib import (
config,
exceptions,
util,
)
from counterpartycore.lib.api import apiwatcher, dbbuilder, queries, verbose, wsgi
from counterpartycore.lib.api.routes import ROUTES, function_needs_db
Expand Down Expand Up @@ -68,9 +67,9 @@ def is_server_ready():
CurrentState().current_backend_height() - 1,
]:
return True
if util.CURRENT_BLOCK_TIME is None:
if CurrentState().current_block_time() is None:
return False
if time.time() - util.CURRENT_BLOCK_TIME < 60:
if time.time() - CurrentState().current_block_time() < 60:
return True
return False

Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/apiv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ def get_dispenser_info(tx_hash=None, tx_index=None):
oracle_fiat_label,
oracle_price_last_updated,
) = ledger.ledger.get_oracle_last_price(
db, dispenser["oracle_address"], helpers.CURRENT_BLOCK_INDEX
db, dispenser["oracle_address"], CurrentState().current_block_index()
)

if oracle_price > 0:
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/api/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ledger,
)
from counterpartycore.lib.api import compose
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.utils import helpers

D = decimal.Decimal
Expand Down Expand Up @@ -367,7 +368,7 @@ def inject_fiat_price(ledger_db, dispenser):
dispenser["fiat_unit"],
dispenser["oracle_price_last_updated"],
) = ledger.ledger.get_oracle_last_price(
ledger_db, dispenser["oracle_address"], helpers.CURRENT_BLOCK_INDEX
ledger_db, dispenser["oracle_address"], CurrentState().current_block_index()
)

if dispenser["oracle_price"] > 0:
Expand Down
42 changes: 8 additions & 34 deletions counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import signal
import sys
import threading
import time

import gunicorn.app.base
import waitress
Expand All @@ -14,55 +13,30 @@
from gunicorn.errors import AppImportError
from werkzeug.serving import make_server

from counterpartycore.lib import backend, config, ledger, util
from counterpartycore.lib import config
from counterpartycore.lib.api import apiwatcher
from counterpartycore.lib.cli import log
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.utils import database, helpers
from counterpartycore.lib.utils import database

multiprocessing.set_start_method("spawn", force=True)

logger = logging.getLogger(config.LOGGER_NAME)


def get_backend_height():
block_count = backend.bitcoind.getblockcount()
blocks_behind = backend.bitcoind.get_blocks_behind()
return block_count + blocks_behind


class BackendHeight(metaclass=helpers.SingletonMeta):
def __init__(self):
self.backend_height = get_backend_height()
self.last_update = time.time()

def get(self):
if time.time() - self.last_update > 0: # one second cache
self.backend_height = get_backend_height()
self.last_update = time.time()
return self.backend_height


def refresh_current_state(ledger_db, state_db):
CurrentState().set_current_block_index(apiwatcher.get_last_block_parsed(state_db))

if CurrentState().current_block_index():
last_block = ledger.ledger.get_block(ledger_db, CurrentState().current_block_index())
if last_block:
util.CURRENT_BLOCK_TIME = last_block["block_time"]
else:
util.CURRENT_BLOCK_TIME = 0
else:
util.CURRENT_BLOCK_TIME = 0
CurrentState().set_current_block_index(0)
current_block_index = CurrentState().current_block_index()
current_backend_height = CurrentState().current_backend_height()

if CurrentState().current_backend_height() > CurrentState().current_block_index():
if current_backend_height > current_block_index:
logger.debug(
f"Counterparty is currently behind Bitcoin Core. ({CurrentState().current_block_index()} < {CurrentState().current_backend_height()})"
f"Counterparty is currently behind Bitcoin Core. ({current_block_index} < {current_backend_height})"
)
elif CurrentState().current_backend_height() < CurrentState().current_block_index():
elif current_backend_height < current_block_index:
logger.debug(
f"Bitcoin Core is currently behind the network. ({CurrentState().current_block_index()} > {CurrentState().current_backend_height()})"
f"Bitcoin Core is currently behind the network. ({current_block_index} > {current_backend_height})"
)


Expand Down
15 changes: 14 additions & 1 deletion counterparty-core/counterpartycore/lib/ledger/currentstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from flask import request

from counterpartycore.lib import backend
from counterpartycore.lib import backend, ledger
from counterpartycore.lib.utils import helpers
from counterpartycore.lib.utils.database import LedgerDBConnectionPool

BACKEND_HEIGHT_REFRSH_INTERVAL = 3

Expand Down Expand Up @@ -34,10 +35,22 @@ def get(self, key):

def set_current_block_index(self, block_index):
self.state["CURRENT_BLOCK_INDEX"] = block_index
if block_index:
with LedgerDBConnectionPool().connection() as ledger_db:
last_block = ledger.ledger.get_block(
ledger_db, CurrentState().current_block_index()
)
if last_block:
self.state["CURRENT_BLOCK_TIME"] = last_block["block_time"]
else:
self.state["CURRENT_BLOCK_TIME"] = 0

def current_block_index(self):
return self.state.get("CURRENT_BLOCK_INDEX")

def current_block_time(self):
return self.state.get("CURRENT_BLOCK_TIME")

def current_backend_height(self):
if time.time() - self.last_update >= BACKEND_HEIGHT_REFRSH_INTERVAL:
self.backend_height = get_backend_height()

Check warning

Code scanning / pylint

Attribute 'backend_height' defined outside init. Warning

Attribute 'backend_height' defined outside __init__.
Expand Down
1 change: 0 additions & 1 deletion counterparty-core/counterpartycore/lib/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
CURRENT_TX_HASH = None
PARSING_MEMPOOL = False
BLOCK_PARSER_STATUS = "starting"
CURRENT_BLOCK_TIME = None
7 changes: 4 additions & 3 deletions counterparty-core/counterpartycore/lib/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import apsw.bestpractice
import apsw.ext
import psutil
from counterpartycore.lib import config, exceptions, ledger
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib import config, exceptions

# from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.utils import helpers
from termcolor import cprint

Expand Down Expand Up @@ -161,7 +162,7 @@ def initialise_db():
logger.info(f"Connecting to database... (SQLite {apsw.apswversion()})")
db = get_connection(read_only=False)

CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
# CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))

return db

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def get_tx_index(node, tx_hash):


def get_last_tx_index(node):
time.sleep(2) # wait for utils.CURRENT_BLOCK_INDEX to be updated and cache expired (each .5s)
time.sleep(
2
) # wait for CurrentState().current_block_index() to be updated and cache expired (each .5s)
result = node.api_call("transactions?limit=1")
if "result" in result:
return result["result"][0]["tx_index"]
Expand Down

0 comments on commit a3a2bdf

Please sign in to comment.