Skip to content

Commit

Permalink
Components
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a authored and ichorid committed Aug 11, 2021
1 parent 5f97407 commit 3c1e12a
Show file tree
Hide file tree
Showing 52 changed files with 880 additions and 564 deletions.
12 changes: 1 addition & 11 deletions experiment/popularity_community/initial_filling.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def __init__(self, interval_in_sec, output_file_path, timeout_in_sec, working_di
self._interval_in_sec = interval_in_sec
self._output_file_path = output_file_path

super().__init__(config, timeout_in_sec, working_dir, config_path,
communities_cls=self.communities())
super().__init__(config, timeout_in_sec, working_dir, config_path, components=[])

async def on_tribler_started(self):
await super().on_tribler_started()
Expand All @@ -100,15 +99,6 @@ async def on_tribler_started(self):
session.ipv8.strategies.append((RandomWalk(session.popularity_community),
TARGET_PEERS_COUNT))

def communities(self):
return [
Factory(create_class=TriblerDiscoveryCommunity),
Factory(create_class=TriblerDHTDiscoveryCommunity),
Factory(create_class=ObservablePopularityCommunity,
kwargs={'interval_in_sec': self._interval_in_sec,
'output_file_path': self._output_file_path}),
]


def _parse_argv():
parser = argparse.ArgumentParser(description='Calculate velocity of initial torrents list filling')
Expand Down
58 changes: 33 additions & 25 deletions src/run_tribler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
from tribler_common.version_manager import VersionHistory
from tribler_core.config.tribler_config import TriblerConfig
from tribler_core.dependencies import check_for_missing_dependencies
from tribler_core.modules.bandwidth_accounting.community import BandwidthAccountingCommunity, \
BandwidthAccountingTestnetCommunity
from tribler_core.modules.dht.community import TriblerDHTDiscoveryCommunity
from tribler_core.modules.discovery.community import TriblerDiscoveryCommunity
from tribler_core.modules.metadata_store.community.gigachannel_community import GigaChannelCommunity, \
GigaChannelTestnetCommunity
from tribler_core.modules.popularity.community import PopularityCommunity
from tribler_core.modules.tunnel.community.community import TriblerTunnelCommunity, TriblerTunnelTestnetCommunity
from tribler_core.session import Factory, core_session
from tribler_core.modules.exception_handler.component import ExceptionHandlerComponent
from tribler_core.modules.ipv8.component import Ipv8Component
from tribler_core.modules.libtorrent.module import LibtorrentComponent
from tribler_core.modules.metadata_store.community.component import GigaChannelComponent
from tribler_core.modules.metadata_store.component import MetadataStoreComponent
from tribler_core.modules.metadata_store.manager.component import GigachannelManagerComponent
from tribler_core.modules.payout.component import PayoutComponent
from tribler_core.modules.popularity.component import PopularityComponent
from tribler_core.modules.resource_monitor.component import ResourceMonitorComponent
from tribler_core.modules.torrent_checker.component import TorrentCheckerComponent
from tribler_core.modules.tunnel.component import TunnelsComponent
from tribler_core.modules.version_check.component import VersionCheckComponent
from tribler_core.modules.watch_folder.component import WatchFolderComponent
from tribler_core.restapi.component import RESTComponent
from tribler_core.session import core_session
from tribler_core.upgrade.component import UpgradeComponent
from tribler_core.utilities.osutils import get_root_state_directory
from tribler_core.version import sentry_url, version_id
from tribler_gui.utilities import get_translator
Expand All @@ -34,20 +41,23 @@
# pylint: disable=import-outside-toplevel


def communities_gen(config: TriblerConfig):
yield Factory(create_class=TriblerDiscoveryCommunity) if config.discovery_community.enabled else ...
yield Factory(create_class=TriblerDHTDiscoveryCommunity) if config.dht.enabled else ...
def components_gen(config: TriblerConfig):
yield ExceptionHandlerComponent()
yield RESTComponent() if config.api.http_enabled or config.api.https_enabled else ...
yield UpgradeComponent() if config.upgrader_enabled and not config.core_test_mode else ...
yield MetadataStoreComponent() if config.chant.enabled else ...
yield Ipv8Component() if config.ipv8.enabled else ...
yield LibtorrentComponent() if config.libtorrent.enabled else ...
yield TunnelsComponent()
yield PayoutComponent()
yield TorrentCheckerComponent() if config.torrent_checking.enabled and not config.core_test_mode else ...
yield PopularityComponent() if config.popularity_community.enabled else ...
yield GigaChannelComponent() if config.chant.enabled else ...

bandwidth_accounting_kwargs = {'database': config.state_dir / "sqlite" / "bandwidth.db"}
bandwidth_accounting_cls = BandwidthAccountingTestnetCommunity if config.general.testnet or config.bandwidth_accounting.testnet else BandwidthAccountingCommunity
yield Factory(create_class=bandwidth_accounting_cls, kwargs=bandwidth_accounting_kwargs)

tribler_tunnel_cls = TriblerTunnelTestnetCommunity if config.general.testnet or config.tunnel_community.testnet else TriblerTunnelCommunity
yield Factory(create_class=tribler_tunnel_cls) if config.tunnel_community.enabled else ...
yield Factory(create_class=PopularityCommunity) if config.popularity_community.enabled else ...

giga_channel_cls = GigaChannelTestnetCommunity if config.general.testnet else GigaChannelCommunity
yield Factory(create_class=giga_channel_cls) if config.chant.enabled else ...
yield WatchFolderComponent() if config.watch_folder.enabled else ...
yield ResourceMonitorComponent() if config.resource_monitor.enabled and not config.core_test_mode else ...
yield VersionCheckComponent() if config.general.version_checker_enabled and not config.core_test_mode else ...
yield GigachannelManagerComponent() if config.chant.enabled and config.chant.manager_enabled and config.libtorrent.enabled else ...


def start_tribler_core(base_path, api_port, api_key, root_state_dir, core_test_mode=False):
Expand Down Expand Up @@ -112,9 +122,7 @@ async def start_tribler():
shutdown_event = Event()
signal.signal(signal.SIGTERM, lambda signum, stack: shutdown_event.set)
# Run until core_session exits
await core_session(config,
communities_cls=list(communities_gen(config)),
shutdown_event=shutdown_event)
await core_session(config, components=list(components_gen(config)), shutdown_event=shutdown_event)

if trace_logger:
trace_logger.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from binascii import unhexlify
from pathlib import Path
from random import Random
from typing import Dict, Union
from typing import Dict

from ipv8.community import Community
from ipv8.peer import Peer
from ipv8.peerdiscovery.discovery import RandomWalk
from ipv8.requestcache import RequestCache
from ipv8.types import Address
from tribler_core.modules.bandwidth_accounting import EMPTY_SIGNATURE
Expand All @@ -17,39 +15,35 @@
from tribler_core.modules.bandwidth_accounting.payload import BandwidthTransactionPayload, \
BandwidthTransactionQueryPayload
from tribler_core.modules.bandwidth_accounting.transaction import BandwidthTransactionData
from tribler_core.modules.community_di_mixin import CommunityDIMixin, DEFAULT_TARGET_PEERS, StrategyFactory
from tribler_core.session import Mediator
from tribler_core.modules.tribler_community import TriblerCommunity
from tribler_core.utilities.unicode import hexlify


class BandwidthAccountingCommunity(CommunityDIMixin, Community):
class BandwidthAccountingCommunity(TriblerCommunity):
"""
Community around bandwidth accounting and payouts.
"""
community_id = unhexlify('79b25f2867739261780faefede8f25038de9975d')
DB_NAME = 'bandwidth'
version = b'\x02'

def __init__(self, *args, mediator: Mediator = None,
database: Union[str, Path, BandwidthDatabase], **kwargs) -> None:
def __init__(self, *args, database=None, database_path='', **kwargs) -> None:
"""
Initialize the community.
:param persistence: The database that stores transactions, will be created if not provided.
:param database_path: The path at which the database will be created. Defaults to the current working directory.
"""
self.database = database
self.database_path = Path(database_path)
self.random = Random()

super().__init__(*args, **kwargs)

self.settings = mediator.config.bandwidth_accounting

self.request_cache = RequestCache()
self.my_pk = self.my_peer.public_key.key_to_bin()

if isinstance(database, BandwidthDatabase):
self.database = database
else:
self.database = BandwidthDatabase(db_path=database, my_pub_key=self.my_pk)
if not self.database:
self.database = BandwidthDatabase(self.database_path, self.my_pk)

self.add_message_handler(BandwidthTransactionPayload, self.received_transaction)
self.add_message_handler(BandwidthTransactionQueryPayload, self.received_query)
Expand All @@ -58,15 +52,6 @@ def __init__(self, *args, mediator: Mediator = None,

self.logger.info("Started bandwidth accounting community with public key %s", hexlify(self.my_pk))

self.init_community_di_mixin(strategies=[
StrategyFactory(create_class=RandomWalk, target_peers=DEFAULT_TARGET_PEERS),
])

def fill_mediator(self, mediator):
super().fill_mediator(mediator)

mediator.dictionary['bandwidth_community'] = self

def construct_signed_transaction(self, peer: Peer, amount: int) -> BandwidthTransactionData:
"""
Construct a new signed bandwidth transaction.
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions src/tribler-core/tribler_core/modules/component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging


class Component:
def __init__(self, *args, **kwargs):
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.info('Init')

async def run(self, mediator):
self.logger.info('Run')

async def shutdown(self, mediator):
self.logger.info('Shutdown')
9 changes: 0 additions & 9 deletions src/tribler-core/tribler_core/modules/component_di_mixin.py

This file was deleted.

29 changes: 0 additions & 29 deletions src/tribler-core/tribler_core/modules/dht/community.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/tribler-core/tribler_core/modules/discovery/community.py

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from asyncio import get_event_loop

from tribler_common.sentry_reporter.sentry_reporter import SentryReporter

from tribler_core.modules.component import Component
from tribler_core.modules.exception_handler.exception_handler import CoreExceptionHandler
from tribler_core.utilities.unicode import hexlify


class ExceptionHandlerComponent(Component):
async def run(self, mediator):
await super().run(mediator)
config = mediator.config
trustchain_keypair = mediator.trustchain_keypair

exception_handler = CoreExceptionHandler(self.logger, config=config.error_handling)
get_event_loop().set_exception_handler(exception_handler.unhandled_error_observer)

user_id_str = hexlify(trustchain_keypair.key.pk).encode('utf-8')
SentryReporter.set_user(user_id_str)

mediator.optional['exception_handler'] = exception_handler
Loading

0 comments on commit 3c1e12a

Please sign in to comment.