diff --git a/counterparty-core/counterpartycore/lib/api/apiserver.py b/counterparty-core/counterpartycore/lib/api/apiserver.py index 1955abecc..31c36297b 100644 --- a/counterparty-core/counterpartycore/lib/api/apiserver.py +++ b/counterparty-core/counterpartycore/lib/api/apiserver.py @@ -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 @@ -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 diff --git a/counterparty-core/counterpartycore/lib/api/apiv1.py b/counterparty-core/counterpartycore/lib/api/apiv1.py index 94ada2840..c9007f914 100644 --- a/counterparty-core/counterpartycore/lib/api/apiv1.py +++ b/counterparty-core/counterpartycore/lib/api/apiv1.py @@ -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: diff --git a/counterparty-core/counterpartycore/lib/api/verbose.py b/counterparty-core/counterpartycore/lib/api/verbose.py index 43e3fb4c3..30c0bd84a 100644 --- a/counterparty-core/counterpartycore/lib/api/verbose.py +++ b/counterparty-core/counterpartycore/lib/api/verbose.py @@ -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 @@ -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: diff --git a/counterparty-core/counterpartycore/lib/api/wsgi.py b/counterparty-core/counterpartycore/lib/api/wsgi.py index baaa5cecf..2d6748f12 100644 --- a/counterparty-core/counterpartycore/lib/api/wsgi.py +++ b/counterparty-core/counterpartycore/lib/api/wsgi.py @@ -4,7 +4,6 @@ import signal import sys import threading -import time import gunicorn.app.base import waitress @@ -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})" ) diff --git a/counterparty-core/counterpartycore/lib/ledger/currentstate.py b/counterparty-core/counterpartycore/lib/ledger/currentstate.py index c65285570..f68490cd6 100644 --- a/counterparty-core/counterpartycore/lib/ledger/currentstate.py +++ b/counterparty-core/counterpartycore/lib/ledger/currentstate.py @@ -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 @@ -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() diff --git a/counterparty-core/counterpartycore/lib/util.py b/counterparty-core/counterpartycore/lib/util.py index 8cab60d55..ae4029fdb 100644 --- a/counterparty-core/counterpartycore/lib/util.py +++ b/counterparty-core/counterpartycore/lib/util.py @@ -1,4 +1,3 @@ CURRENT_TX_HASH = None PARSING_MEMPOOL = False BLOCK_PARSER_STATUS = "starting" -CURRENT_BLOCK_TIME = None diff --git a/counterparty-core/counterpartycore/lib/utils/database.py b/counterparty-core/counterpartycore/lib/utils/database.py index 3f942d088..cb425b26a 100644 --- a/counterparty-core/counterpartycore/lib/utils/database.py +++ b/counterparty-core/counterpartycore/lib/utils/database.py @@ -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 @@ -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 diff --git a/counterparty-core/counterpartycore/test/regtest/testscenarios.py b/counterparty-core/counterpartycore/test/regtest/testscenarios.py index 9d0cbd91b..25d7f98bf 100644 --- a/counterparty-core/counterpartycore/test/regtest/testscenarios.py +++ b/counterparty-core/counterpartycore/test/regtest/testscenarios.py @@ -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"]