Skip to content

Commit

Permalink
try to kill util.CURRENT_BACKEND_HEIGHT
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 15, 2025
1 parent 71ccee8 commit 0befea3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
14 changes: 7 additions & 7 deletions counterparty-core/counterpartycore/lib/api/apiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def is_server_ready():
return True
except RuntimeError:
pass
if util.CURRENT_BACKEND_HEIGHT is None:
if CurrentState().current_backend_height() is None:
return False
if CurrentState().current_block_index() in [
util.CURRENT_BACKEND_HEIGHT,
util.CURRENT_BACKEND_HEIGHT - 1,
CurrentState().current_backend_height(),
CurrentState().current_backend_height() - 1,
]:
return True
if util.CURRENT_BLOCK_TIME is None:
Expand All @@ -86,7 +86,7 @@ def api_root():
with StateDBConnectionPool().connection() as state_db:
counterparty_height = apiwatcher.get_last_block_parsed(state_db)

backend_height = util.CURRENT_BACKEND_HEIGHT
backend_height = CurrentState().current_backend_height()
if backend_height is None:
if config.FORCE:
server_ready = True
Expand All @@ -99,7 +99,7 @@ def api_root():
"server_ready": server_ready,
"network": network,
"version": config.VERSION_STRING,
"backend_height": util.CURRENT_BACKEND_HEIGHT,
"backend_height": CurrentState().current_backend_height(),
"counterparty_height": counterparty_height,
"documentation": "https://counterpartycore.docs.apiary.io/",
"routes": f"{request.url_root}v2/routes",
Expand Down Expand Up @@ -174,7 +174,7 @@ def return_result(
response.headers["X-COUNTERPARTY-HEIGHT"] = CurrentState().current_block_index()
response.headers["X-COUNTERPARTY-READY"] = is_server_ready()
response.headers["X-COUNTERPARTY-VERSION"] = config.VERSION_STRING
response.headers["X-BITCOIN-HEIGHT"] = util.CURRENT_BACKEND_HEIGHT
response.headers["X-BITCOIN-HEIGHT"] = CurrentState().current_backend_height()
response.headers["Content-Type"] = "application/json"
set_cors_headers(response)

Expand Down Expand Up @@ -311,7 +311,7 @@ def handle_route(**kwargs):
logger.trace(f"API Request - {request.remote_addr} {request.method} {request.url}")
logger.debug(get_log_prefix(query_args))

if not config.FORCE and util.CURRENT_BACKEND_HEIGHT is None:
if not config.FORCE and CurrentState().current_backend_height() is None:
return return_result(
503,
error="Backend still not ready. Please try again later.",
Expand Down
9 changes: 4 additions & 5 deletions counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def get(self):
def refresh_current_state(ledger_db, state_db):
CurrentState().set_current_block_index(apiwatcher.get_last_block_parsed(state_db))

util.CURRENT_BACKEND_HEIGHT = BackendHeight().get()
if CurrentState().current_block_index():
last_block = ledger.ledger.get_block(ledger_db, CurrentState().current_block_index())
if last_block:
Expand All @@ -57,13 +56,13 @@ def refresh_current_state(ledger_db, state_db):
util.CURRENT_BLOCK_TIME = 0
CurrentState().set_current_block_index(0)

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


Expand Down
19 changes: 19 additions & 0 deletions counterparty-core/counterpartycore/lib/ledger/currentstate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import time

from counterpartycore.lib import backend
from counterpartycore.lib.utils import helpers

BACKEND_HEIGHT_REFRSH_INTERVAL = 3


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


class CurrentState(metaclass=helpers.SingletonMeta):
def __init__(self):
self.state = {}
self.last_update = 0

def set(self, key, value):
self.state[key] = value
Expand All @@ -16,3 +28,10 @@ def set_current_block_index(self, block_index):

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

def current_backend_height(self):
print("current_backend_height")
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__.
self.last_update = time.time()
return self.backend_height
1 change: 0 additions & 1 deletion counterparty-core/counterpartycore/lib/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CURRENT_TX_HASH = None
PARSING_MEMPOOL = False
BLOCK_PARSER_STATUS = "starting"
CURRENT_BACKEND_HEIGHT = None
CURRENT_BLOCK_TIME = None
6 changes: 4 additions & 2 deletions counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import requests
from bitcoinutils.keys import PublicKey

from counterpartycore.lib import config, exceptions, ledger, util
from counterpartycore.lib import config, exceptions, ledger
from counterpartycore.lib.api import apiserver as api_v2
from counterpartycore.lib.api import apiv1 as api
from counterpartycore.lib.api import dbbuilder
Expand Down Expand Up @@ -353,7 +353,7 @@ def apiserver_v2(request, cp_server):
def is_server_ready():
return True

util.CURRENT_BACKEND_HEIGHT = 0
# util.CURRENT_BACKEND_HEIGHT = 0

api_v2.is_server_ready = is_server_ready

Expand Down Expand Up @@ -728,3 +728,5 @@ class MockSingletonMeta:
pass

monkeypatch.setattr("counterpartycore.lib.utils.helpers.SingletonMeta", MockSingletonMeta)

# monkeypatch.setattr("counterpartycore.lib.ledger.current_state.get_backend_height", lambda: 0)

0 comments on commit 0befea3

Please sign in to comment.