Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/cnt_collector_node/global_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
# Local imports
try:
import config
import load_pairs
import utxo_objects
except ModuleNotFoundError:
try:
from src.cnt_collector_node import config, utxo_objects
from src.cnt_collector_node import config, load_pairs, utxo_objects
except ModuleNotFoundError:
from cnt_collector_node import config, utxo_objects
from cnt_collector_node import config, load_pairs, utxo_objects


class NodeError(Exception):
Expand Down Expand Up @@ -217,7 +218,7 @@ def _return_ca_ssl_context():
return ssl.create_default_context(cafile=certifi.where())


def get_version():
def get_version(pairs: load_pairs.Pairs = None):
"""Return package version to the calling code.

Version is set to a default value if it isn't picked up by importlib
Expand All @@ -230,12 +231,12 @@ def get_version():
except PackageNotFoundError:
# package is not installed
pass
return __version__
return f"cnt-collector-node: {__version__}; pairs-file: {pairs.checksum};"


def get_user_agent() -> str:
def get_user_agent(pairs: load_pairs.Pairs) -> str:
"""Return a user agent to connect to a validator node with."""
return f"cnt-collector-node/{get_version()}"
return f"cnt-collector-node/{get_version(pairs)}"


async def generate_content_signature(
Expand Down
20 changes: 17 additions & 3 deletions src/cnt_collector_node/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import database_initialization
import global_helpers as helpers
import kupo_helper
import load_pairs
import ogmios_helper
import utxo_objects
except ModuleNotFoundError:
Expand All @@ -32,12 +33,22 @@
from src.cnt_collector_node import database_abstraction as dba
from src.cnt_collector_node import database_initialization
from src.cnt_collector_node import global_helpers as helpers
from src.cnt_collector_node import kupo_helper, ogmios_helper, utxo_objects
from src.cnt_collector_node import (
kupo_helper,
load_pairs,
ogmios_helper,
utxo_objects,
)
except ModuleNotFoundError:
from cnt_collector_node import config
from cnt_collector_node import database_abstraction as dba
from cnt_collector_node import global_helpers as helpers
from cnt_collector_node import kupo_helper, ogmios_helper, utxo_objects
from cnt_collector_node import (
kupo_helper,
load_pairs,
ogmios_helper,
utxo_objects,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1172,6 +1183,7 @@ async def _get_source_messages(
app_context: helpers.AppContext,
tokens_pair: utxo_objects.TokensPair,
feed: str,
pairs: load_pairs.Pairs,
) -> list:
"""Query on-chain for the data we require for each pair at each
given source.
Expand All @@ -1195,7 +1207,7 @@ async def _get_source_messages(
security_token_name=source.get("security_token_name"),
# Submitter specific. Possibly belongs in InitialContext.
address=source.get("address"),
collector=helpers.get_user_agent(),
collector=helpers.get_user_agent(pairs),
),
last_block_slot=last_block_slot,
)
Expand All @@ -1212,13 +1224,15 @@ async def check_tokens_pair(
app_context: helpers.AppContext,
identity: dict,
tokens_pair: utxo_objects.TokensPair,
pairs: load_pairs.Pairs,
) -> Union[tuple[dict | str] | tuple[None | str]]:
"""Check a tokens pair"""
feed = tokens_pair.get("name")
source_messages = await _get_source_messages(
app_context=app_context,
tokens_pair=tokens_pair,
feed=feed,
pairs=pairs,
)
if not source_messages:
logger.warning("no source messages for: %s", feed)
Expand Down
15 changes: 9 additions & 6 deletions src/cnt_collector_node/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
try:
import config
import database_initialization
import global_helpers
import global_helpers as helpers
import helper_functions
import kupo_helper
Expand All @@ -27,6 +28,7 @@
except ModuleNotFoundError:
try:
from src.cnt_collector_node import config, database_initialization
from src.cnt_collector_node import global_helpers
from src.cnt_collector_node import global_helpers as helpers
from src.cnt_collector_node import (
helper_functions,
Expand All @@ -36,6 +38,7 @@
)
except ModuleNotFoundError:
from cnt_collector_node import config, database_initialization
from cnt_collector_node import global_helpers
from cnt_collector_node import global_helpers as helpers
from cnt_collector_node import (
helper_functions,
Expand Down Expand Up @@ -209,13 +212,15 @@ def main() -> None:
"""Primary entry point for this script."""
args = parse_arguments()

if args.version:
print(helper_functions.get_version())
sys.exit(0)

# Setup global logging.
helpers.setup_logging(args.debug)

pairs = load_pairs.load(path=args.pairs)

if args.version:
print(global_helpers.get_version(pairs))
sys.exit(0)

if not args.ogmios_url:
logger.error(
"ogmios URL is not set. Please set the OGMIOS_URL environment variable or provide the Ogmios URL using the -o parameter."
Expand All @@ -232,8 +237,6 @@ def main() -> None:
else:
kupo_url = args.kupo_url.rstrip("/")

pairs = load_pairs.load(path=args.pairs)

try:
asyncio.run(
indexer_main(
Expand Down
17 changes: 16 additions & 1 deletion src/cnt_collector_node/load_pairs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Helper module for loading DEX pairs."""

import hashlib
import importlib
import importlib.util
import logging
Expand All @@ -16,6 +17,7 @@ class Pairs:
"""

pairs: dict
checksum: str

@property
def DEX_PAIRS(self): # pylint:disable=C0103
Expand All @@ -42,7 +44,20 @@ def load(path: str) -> Pairs:
logger.error("problem loading module: %s", err)
raise SystemExit from err
try:
return Pairs(pairs=pairs.DEX_PAIRS)
checksum = get_file_checksum(path=path)
return Pairs(pairs=pairs.DEX_PAIRS, checksum=checksum)
except AttributeError as err:
logger.error("pairs module doesn't contain DEX_PAIRS dict")
raise SystemExit from err


def get_file_checksum(path: str):
"""Return a checksum for a file."""
checksum = None
# Open,close, read file and calculate MD5 on its contents
with open(path, "rb") as file_to_check:
# read contents of the file
data = file_to_check.read()
# pipe contents of the file through
checksum = hashlib.md5(data).hexdigest()
return checksum
13 changes: 7 additions & 6 deletions src/cnt_collector_node/submitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import config
import database_abstraction as dba
import database_initialization
import global_helpers
import global_helpers as helpers
import helper_functions
import kupo_helper
Expand All @@ -26,6 +27,7 @@
from src.cnt_collector_node import config
from src.cnt_collector_node import database_abstraction as dba
from src.cnt_collector_node import database_initialization
from src.cnt_collector_node import global_helpers
from src.cnt_collector_node import global_helpers as helpers
from src.cnt_collector_node import (
helper_functions,
Expand All @@ -37,6 +39,7 @@
from cnt_collector_node import config
from cnt_collector_node import database_abstraction as dba
from cnt_collector_node import database_initialization
from cnt_collector_node import global_helpers
from cnt_collector_node import global_helpers as helpers
from cnt_collector_node import (
helper_functions,
Expand Down Expand Up @@ -127,6 +130,7 @@ async def process_dex_pairs(
app_context=app_context,
identity=identity,
tokens_pair=tokens_pair,
pairs=pairs,
)
except sqlite3.OperationalError as err:
logger.error("database query error: %s", err)
Expand Down Expand Up @@ -304,17 +308,14 @@ def main() -> None:
args.print_help()
sys.exit()

if args.version:
print(helper_functions.get_version())
sys.exit(0)

# Setup global logging.
helpers.setup_logging(args.debug)

pairs = load_pairs.load(path=args.pairs)

# Setup global logging.
helpers.setup_logging(args.debug)
if args.version:
print(global_helpers.get_version(pairs))
sys.exit(0)

asyncio.run(
cnt_main(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_check_pair_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from src.cnt_collector_node import config
from src.cnt_collector_node import global_helpers as helpers
from src.cnt_collector_node import utxo_objects
from src.cnt_collector_node import load_pairs, utxo_objects
from src.cnt_collector_node.database_initialization import _create_database
from src.cnt_collector_node.helper_functions import (
_validate_min_ada,
Expand Down Expand Up @@ -1661,6 +1661,7 @@ async def test_check_tokens_pair(
app_context=app_context,
identity=test_node_id,
tokens_pair=tokens_pair,
pairs=load_pairs.Pairs({}, "12345"),
)
assert message == expected_message
assert dt == expected_dt
Expand Down