From 28e8237d3f194b8b3ce799b5f2422c81a12f6c9b Mon Sep 17 00:00:00 2001 From: rodgerjohnson Date: Fri, 30 Aug 2024 20:37:04 +0900 Subject: [PATCH] Initial Changes to Enable Tron collector for Exporter --- src/collectors.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/configuration.py | 3 ++- src/registries.py | 2 ++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/collectors.py b/src/collectors.py index 5afaa26..84d1dca 100644 --- a/src/collectors.py +++ b/src/collectors.py @@ -393,3 +393,57 @@ def client_version(self): def latency(self): """Returns connection latency.""" return self.interface.latest_query_latency + +class TronCollector(): + """A collector to fetch information about Aptos endpoints.""" + + def __init__(self, url, labels, chain_id, **client_parameters): + + self.labels = labels + self.chain_id = chain_id + self.interface = HttpsInterface(url, client_parameters.get('open_timeout'), + client_parameters.get('ping_timeout')) + + self._logger_metadata = { + 'component': 'TronCollector', + 'url': strip_url(url) + } + self.client_version_payload = { + 'jsonrpc': '2.0', + 'method': "web3_clientVersion", + 'id': 1 + } + self.block_height_payload = { + 'jsonrpc': '2.0', + 'method': "eth_blockNumber", + 'id': 1 + } + + def alive(self): + """Returns true if endpoint is alive, false if not.""" + # Run cached query because we can also fetch client version from this + # later on. This will save us an RPC call per run. + return self.interface.cached_json_rpc_post( + self.client_version_payload) is not None + + def block_height(self): + """Returns blockheight after converting string hexadecimal value to an int""" + result = self.interface.cached_json_rpc_post(self.block_height_payload) + + if result and isinstance(result, str) and result.startswith('0x'): + return int(result, 16) + else: + raise ValueError(f"Invalid block height result: {result}") + + def client_version(self): + """Runs a cached query to return client version.""" + version = self.interface.cached_json_rpc_post( + self.client_version_payload) + if version is None: + return None + client_version = {"client_version": version} + return client_version + + def latency(self): + """Returns connection latency.""" + return self.interface.latest_query_latency diff --git a/src/configuration.py b/src/configuration.py index 93788df..2b8acb2 100644 --- a/src/configuration.py +++ b/src/configuration.py @@ -47,7 +47,8 @@ def endpoints(self): def _load_configuration(self): allowed_providers = self._load_validation_file() supported_collectors = ('evm', 'cardano', 'conflux', 'solana', - 'bitcoin', 'doge', 'filecoin', 'starknet', 'aptos') + 'bitcoin', 'doge', 'filecoin', 'starknet', 'aptos', + 'tron') configuration_schema = Schema({ 'blockchain': diff --git a/src/registries.py b/src/registries.py index 7ad7e80..c1f0482 100644 --- a/src/registries.py +++ b/src/registries.py @@ -83,6 +83,8 @@ def get_collector_registry(self) -> list: collector = collectors.StarknetCollector case "aptos", "aptos": collector = collectors.AptosCollector + case "tron", "tron": + collector = collectors.TronCollector case "evm", other: # pylint: disable=unused-variable collector = collectors.EvmCollector if collector is None: