Skip to content

Commit

Permalink
DI v1 (no tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jul 7, 2021
1 parent 4734550 commit c69f55c
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 224 deletions.
34 changes: 30 additions & 4 deletions src/run_tribler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import asyncio
import logging.config
import os
import signal
import sys
from asyncio import ensure_future, get_event_loop

from PyQt5.QtCore import QSettings

Expand All @@ -12,8 +10,17 @@
from tribler_common.sentry_reporter.sentry_reporter import SentryReporter, SentryStrategy
from tribler_common.sentry_reporter.sentry_scrubber import SentryScrubber
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.session import core_session
from tribler_core.modules.bandwidth_accounting.community import BandwidthAccountingCommunity, \
BandwidthAccountingTestnetCommunity
from tribler_core.modules.dht.community import DHTDiscoveryStrategies
from tribler_core.modules.discovery.community import TriblerDiscoveryStrategies
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 CommunityFactory, core_session
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 @@ -22,6 +29,25 @@
CONFIG_FILE_NAME = 'triblerd.conf'


# pylint: disable=import-outside-toplevel


def communities_gen(config: TriblerConfig):
yield CommunityFactory(create_class=TriblerDiscoveryStrategies) if config.discovery_community.enabled else ...
yield CommunityFactory(create_class=DHTDiscoveryStrategies) if config.dht.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 CommunityFactory(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 CommunityFactory(create_class=tribler_tunnel_cls) if config.tunnel_community.enabled else ...
yield CommunityFactory(create_class=PopularityCommunity) if config.popularity_community.enabled else ...

giga_channel_cls = GigaChannelTestnetCommunity if config.general.testnet else GigaChannelCommunity
yield CommunityFactory(create_class=giga_channel_cls) if config.chant.enabled else ...


def start_tribler_core(base_path, api_port, api_key, root_state_dir, core_test_mode=False):
"""
This method will start a new Tribler session.
Expand Down Expand Up @@ -82,7 +108,7 @@ async def start_tribler():
trace_logger = check_and_enable_code_tracing('core', log_dir)

# Run until core_session exits
await core_session(config)
await core_session(config, communities_cls=list(communities_gen(config)))

if trace_logger:
trace_logger.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from random import Random
from typing import Dict, Union

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 @@ -15,19 +17,21 @@
from tribler_core.modules.bandwidth_accounting.payload import BandwidthTransactionPayload, \
BandwidthTransactionQueryPayload
from tribler_core.modules.bandwidth_accounting.transaction import BandwidthTransactionData
from tribler_core.modules.tribler_community import TriblerCommunity
from tribler_core.modules.community_di_mixin import CommunityDIMixin, DEFAULT_TARGET_PEERS, StrategyFactory
from tribler_core.session import Mediator
from tribler_core.utilities.unicode import hexlify


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

def __init__(self, *args, database: Union[str, Path, BandwidthDatabase], **kwargs) -> None:
def __init__(self, *args, mediator: Mediator = None,
database: Union[str, Path, BandwidthDatabase], **kwargs) -> None:
"""
Initialize the community.
:param persistence: The database that stores transactions, will be created if not provided.
Expand All @@ -37,6 +41,8 @@ def __init__(self, *args, database: Union[str, Path, BandwidthDatabase], **kwarg

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()

Expand All @@ -52,6 +58,15 @@ def __init__(self, *args, database: Union[str, Path, BandwidthDatabase], **kwarg

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
23 changes: 23 additions & 0 deletions src/tribler-core/tribler_core/modules/community_di_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dataclasses import dataclass

from ipv8.peerdiscovery.discovery import RandomWalk

DEFAULT_TARGET_PEERS = 20
INFINITE_TARGET_PEERS = -1


@dataclass
class StrategyFactory:
create_class: type = RandomWalk
target_peers: int = 20


class CommunityDIMixin:
"""Mixin for Dependency Injection
"""

def init_community_di_mixin(self, strategies=None):
self.strategies = strategies

def fill_mediator(self, mediator):
...
135 changes: 0 additions & 135 deletions src/tribler-core/tribler_core/modules/community_loader.py

This file was deleted.

29 changes: 29 additions & 0 deletions src/tribler-core/tribler_core/modules/dht/community.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from ipv8.dht.discovery import DHTDiscoveryCommunity
from ipv8.peerdiscovery.churn import RandomChurn
from ipv8.peerdiscovery.community import PeriodicSimilarity
from ipv8.peerdiscovery.discovery import RandomWalk

from tribler_core.modules.community_di_mixin import (
CommunityDIMixin,
DEFAULT_TARGET_PEERS,
INFINITE_TARGET_PEERS,
StrategyFactory,
)
from tribler_core.session import Mediator


class DHTDiscoveryStrategies(CommunityDIMixin, DHTDiscoveryCommunity):
def __init__(self, *args, mediator=None, **kwargs):
kwargs['max_peers'] = 60
super().__init__(*args, **kwargs)

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

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

mediator.dictionary['dht_community'] = self
22 changes: 22 additions & 0 deletions src/tribler-core/tribler_core/modules/discovery/community.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from ipv8.peerdiscovery.churn import RandomChurn
from ipv8.peerdiscovery.community import DiscoveryCommunity, PeriodicSimilarity
from ipv8.peerdiscovery.discovery import RandomWalk

from tribler_core.modules.community_di_mixin import (
CommunityDIMixin,
DEFAULT_TARGET_PEERS,
INFINITE_TARGET_PEERS,
StrategyFactory,
)


class TriblerDiscoveryStrategies(CommunityDIMixin, DiscoveryCommunity):
def __init__(self, *args, mediator=None, **kwargs):
kwargs['max_peers'] = 100
super().__init__(*args, **kwargs)

self.init_community_di_mixin(strategies=[
StrategyFactory(create_class=RandomChurn, target_peers=INFINITE_TARGET_PEERS),
StrategyFactory(create_class=PeriodicSimilarity, target_peers=INFINITE_TARGET_PEERS),
StrategyFactory(create_class=RandomWalk, target_peers=DEFAULT_TARGET_PEERS),
])
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
from dataclasses import dataclass
from random import sample

from ipv8.peerdiscovery.discovery import RandomWalk
from ipv8.peerdiscovery.network import Network
from ipv8.types import Peer

from pony.orm import db_session

from tribler_common.simpledefs import CHANNELS_VIEW_UUID, NTFY
from tribler_core.modules.community_di_mixin import CommunityDIMixin, INFINITE_TARGET_PEERS, StrategyFactory

from tribler_core.modules.metadata_store.community.discovery_booster import DiscoveryBooster
from tribler_core.modules.metadata_store.community.sync_strategy import RemovePeers
from tribler_core.modules.metadata_store.payload_checker import ObjState
from tribler_core.modules.metadata_store.serialization import CHANNEL_TORRENT
from tribler_core.modules.metadata_store.utils import NoChannelSourcesException
from tribler_core.modules.remote_query_community.community import RemoteQueryCommunity
from tribler_core.session import Mediator
from tribler_core.utilities.unicode import hexlify

minimal_blob_size = 200
Expand Down Expand Up @@ -64,7 +68,7 @@ def get_last_seen_peers_for_channel(self, channel_pk: bytes, channel_id: int, li
return sorted(channel_peers, key=lambda x: x.last_response, reverse=True)[0:limit]


class GigaChannelCommunity(RemoteQueryCommunity):
class GigaChannelCommunity(CommunityDIMixin, RemoteQueryCommunity):
community_id = unhexlify('d3512d0ff816d8ac672eab29a9c1a3a32e17cb13')

def create_introduction_response(self, *args, introduction=None, extra_bytes=b'', prefix=None, new_style=False):
Expand All @@ -77,12 +81,13 @@ def create_introduction_response(self, *args, introduction=None, extra_bytes=b''
new_style=new_style
)

def __init__(self, my_peer, endpoint, network, metadata_store, **kwargs):
self.notifier = kwargs.pop("notifier", None)

def __init__(self, my_peer, endpoint, network, mediator: Mediator = None, max_peers = None, **kwargs):
# ACHTUNG! We create a separate instance of Network for this community because it
# walks aggressively and wants lots of peers, which can interfere with other communities
super().__init__(my_peer, endpoint, Network(), metadata_store, **kwargs)
super().__init__(my_peer, endpoint, Network(), max_peers=50, mediator=mediator, **kwargs)

self.settings = mediator.config.chant
self.mediator = mediator.notifier

# This set contains all the peers that we queried for subscribed channels over time.
# It is emptied regularly. The purpose of this set is to work as a filter so we never query the same
Expand All @@ -94,6 +99,11 @@ def __init__(self, my_peer, endpoint, network, metadata_store, **kwargs):

self.channels_peers = ChannelsPeersMapping()

self.init_community_di_mixin(strategies=[
StrategyFactory(create_class=RandomWalk, target_peers=30),
StrategyFactory(create_class=RemovePeers, target_peers=INFINITE_TARGET_PEERS),
])

def get_random_peers(self, sample_size=None):
# Randomly sample sample_size peers from the complete list of our peers
all_peers = self.get_peers()
Expand Down
Loading

0 comments on commit c69f55c

Please sign in to comment.