Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: implementation of ethstats plugin #1216

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions trinity/plugins/builtin/ethstats/ethstats_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def __init__(
self.websocket = websocket
self.node_id = node_id

self.send_queue: asyncio.Queue[EthstatsMessage] = asyncio.Queue()
self.recv_queue: asyncio.Queue[EthstatsMessage] = asyncio.Queue()
self.send_queue: asyncio.Queue['EthstatsMessage'] = asyncio.Queue()
self.recv_queue: asyncio.Queue['EthstatsMessage'] = asyncio.Queue()
evgeniuz marked this conversation as resolved.
Show resolved Hide resolved

async def _run(self) -> None:
await self.wait_first(
Expand Down
53 changes: 52 additions & 1 deletion trinity/plugins/builtin/ethstats/ethstats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@

import websockets

from eth.chains.base import (
BaseChain,
)
from p2p.events import (
PeerCountRequest,
PeerCountResponse,
)
from p2p.service import (
BaseService,
)
from trinity import (
__version__,
)
from trinity.extensibility import (
PluginContext,
)
from trinity.constants import (
SYNC_LIGHT,
)
from trinity.plugins.builtin.light_peer_chain_bridge.light_peer_chain_bridge import (
EventBusLightPeerChain,
)
from trinity.utils.db_proxy import (
create_db_manager,
)
from trinity.utils.version import (
construct_trinity_client_identifier,
)
Expand Down Expand Up @@ -44,6 +59,8 @@ def __init__(
self.node_id = node_id
self.node_contact = node_contact

self.chain = self.get_chain()

async def _run(self) -> None:
while self.is_operational:
try:
Expand Down Expand Up @@ -87,19 +104,35 @@ async def statistics_handler(self, client: EthstatsClient) -> None:
while self.is_operational:
await client.send_node_ping()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of these await statements need to be wrapped in self.wait so that they will be properly cancelled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I should wrap every async call in self.wait, but now server_handler and statistics_handler are wrapped in self.wait_first and seem to cancel properly (or maybe I missed something :).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be fine.

await client.send_stats(await self.get_node_stats())
await client.send_block(self.get_node_block())

await self.sleep(5)

def get_node_info(self) -> EthstatsData:
return {
'name': self.node_id,
'contact': self.node_contact,
'node': construct_trinity_client_identifier(),
'net': self.context.chain_config.network_id,
'port': self.context.chain_config.port,
'os': platform.system(),
'os_v': platform.release(),
'client': construct_trinity_client_identifier(),
'client': __version__,
'canUpdateHistory': False,
}

def get_node_block(self) -> EthstatsData:
head = self.chain.get_canonical_head()

return {
'number': head.block_number,
'hash': head.hex_hash,
'difficulty': head.difficulty,
'totalDifficulty': self.chain.get_score(head.hash),
'transactions': [],
'uncles': [],
}

async def get_node_stats(self) -> EthstatsData:
response: PeerCountResponse = await self.context.event_bus.request(
PeerCountRequest()
Expand All @@ -109,3 +142,21 @@ async def get_node_stats(self) -> EthstatsData:
'active': True,
'peers': response.peer_count,
}

def get_chain(self) -> BaseChain:
db_manager = create_db_manager(self.context.chain_config.database_ipc_path)
db_manager.connect()

chain_class = self.context.chain_config.node_class.chain_class

if self.context.chain_config.sync_mode == SYNC_LIGHT:
header_db = db_manager.get_headerdb() # type: ignore
chain = chain_class(
header_db,
peer_chain=EventBusLightPeerChain(self.context.event_bus)
)
else:
db = db_manager.get_db() # type: ignore
chain = chain_class(db)

return chain
14 changes: 6 additions & 8 deletions trinity/plugins/builtin/ethstats/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import asyncio
import platform

from argparse import (
ArgumentParser,
Expand All @@ -9,15 +10,14 @@
from trinity.extensibility import (
BaseIsolatedPlugin,
)
from trinity.utils.shutdown import (
exit_with_service_and_endpoint,
)

from trinity.plugins.builtin.ethstats.ethstats_service import (
EthstatsService,
)

from trinity.utils.shutdown import (
exit_with_service_and_endpoint,
)


class EthstatsPlugin(BaseIsolatedPlugin):

Expand Down Expand Up @@ -47,12 +47,12 @@ def configure_parser(self, arg_parser: ArgumentParser, subparser: _SubParsersAct
ethstats_parser.add_argument(
'--ethstats-node-id',
help='Node ID for stats server',
default=os.environ.get('ETHSTATS_NODE_ID'),
default=os.environ.get('ETHSTATS_NODE_ID', platform.node()),
)
ethstats_parser.add_argument(
'--ethstats-node-contact',
help='Node contact information for stats server',
default=os.environ.get('ETHSTATS_NODE_CONTACT'),
default=os.environ.get('ETHSTATS_NODE_CONTACT', ''),
)

def should_start(self) -> bool:
Expand All @@ -62,8 +62,6 @@ def should_start(self) -> bool:
configuration_provided: bool = all((
self.context.args.ethstats_server_url,
self.context.args.ethstats_server_secret,
self.context.args.ethstats_node_id,
self.context.args.ethstats_node_contact,
))

if not configuration_provided:
Expand Down