Skip to content

Commit 6283e44

Browse files
committed
Add EVM HTTP generic collector
1 parent 6c7155e commit 6283e44

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*.out
1313
local*
1414

15+
.idea
16+
1517
# Python
1618
venv
1719
__pycache__

src/collectors.py

+54
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,57 @@ def client_version(self):
446446
def latency(self):
447447
"""Returns connection latency."""
448448
return self.interface.latest_query_latency
449+
450+
451+
class EvmHttpCollector():
452+
"""A collector to fetch information from EVM HTTPS endpoints."""
453+
454+
def __init__(self, url, labels, chain_id, **client_parameters):
455+
456+
self.labels = labels
457+
self.chain_id = chain_id
458+
self.interface = HttpsInterface(url, client_parameters.get('open_timeout'),
459+
client_parameters.get('ping_timeout'))
460+
461+
self._logger_metadata = {
462+
'component': 'EvmHttpCollector',
463+
'url': strip_url(url)
464+
}
465+
self.client_version_payload = {
466+
'jsonrpc': '2.0',
467+
'method': "web3_clientVersion",
468+
'id': 1
469+
}
470+
self.block_height_payload = {
471+
'jsonrpc': '2.0',
472+
'method': "eth_blockNumber",
473+
'id': 1
474+
}
475+
476+
def alive(self):
477+
"""Returns true if endpoint is alive, false if not."""
478+
# Run cached query because we can also fetch client version from this
479+
# later on. This will save us an RPC call per run.
480+
return self.interface.cached_json_rpc_post(
481+
self.client_version_payload) is not None
482+
483+
def block_height(self):
484+
"""Cached query and returns blockheight after converting hex string value to an int"""
485+
result = self.interface.cached_json_rpc_post(self.block_height_payload)
486+
487+
if result and isinstance(result, str) and result.startswith('0x'):
488+
return int(result, 16)
489+
raise ValueError(f"Invalid block height result: {result}")
490+
491+
def client_version(self):
492+
"""Runs a cached query to return client version."""
493+
version = self.interface.cached_json_rpc_post(
494+
self.client_version_payload)
495+
if version is None:
496+
return None
497+
client_version = {"client_version": version}
498+
return client_version
499+
500+
def latency(self):
501+
"""Returns connection latency."""
502+
return self.interface.latest_query_latency

src/configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def endpoints(self):
4646

4747
def _load_configuration(self):
4848
allowed_providers = self._load_validation_file()
49-
supported_collectors = ('evm', 'cardano', 'conflux', 'solana',
49+
supported_collectors = ('evm', 'evmhttp', 'cardano', 'conflux', 'solana',
5050
'bitcoin', 'doge', 'filecoin', 'starknet', 'aptos',
5151
'tron')
5252

src/registries.py

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def get_collector_registry(self) -> list:
9090
collector = collectors.AptosCollector
9191
case "tron", "tron":
9292
collector = collectors.TronCollector
93+
case "evmhttp", other: # pylint: disable=unused-variable
94+
collector = collectors.EvmHttpCollector
9395
case "evm", other: # pylint: disable=unused-variable
9496
collector = collectors.EvmCollector
9597
if collector is None:

src/test_registries.py

+9
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ def test_get_collector_registry_for_tron(self):
157157
with mock.patch('collectors.TronCollector', new=mock.Mock()) as collector:
158158
helper_test_collector_registry(self, collector)
159159

160+
@mock.patch.dict(os.environ, {
161+
"CONFIG_FILE_PATH": "tests/fixtures/configuration_evmhttp.yaml",
162+
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
163+
})
164+
def test_get_collector_registry_for_evmhttp(self):
165+
"""Tests that the EVM HTTP collector is called with the correct args"""
166+
self.collector_registry = CollectorRegistry()
167+
with mock.patch('collectors.EvmHttpCollector', new=mock.Mock()) as collector:
168+
helper_test_collector_registry(self, collector)
160169

161170
@mock.patch.dict(os.environ, {
162171
"CONFIG_FILE_PATH": "tests/fixtures/configuration_evm.yaml",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
blockchain: "Evm"
2+
chain_id: 1234
3+
network_name: "TestNetwork"
4+
network_type: "Testnet"
5+
integration_maturity: "development"
6+
canonical_name: "test-network-testnet"
7+
chain_selector: 121212
8+
collector: "evmhttp"
9+
endpoints:
10+
- url: https://test1.com
11+
provider: TestProvider1
12+
- url: https://test2.com
13+
provider: TestProvider2
14+
- url: https://test3.com
15+
provider: TestProvider3

0 commit comments

Comments
 (0)