Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jun 25, 2021
1 parent af55897 commit be593b8
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 94 deletions.
41 changes: 34 additions & 7 deletions src/run_tribler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@

from PyQt5.QtCore import QSettings

import tribler_core
import tribler_gui
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

import tribler_core
from tribler_core.dependencies import check_for_missing_dependencies
from tribler_core.modules.community_loader import create_default_loader
from tribler_core.utilities.osutils import get_root_state_directory
from tribler_core.version import sentry_url, version_id

import tribler_gui
from tribler_gui.utilities import get_translator

logger = logging.getLogger(__name__)
Expand All @@ -30,8 +29,8 @@ def start_tribler_core(base_path, api_port, api_key, root_state_dir, core_test_m
through the HTTP API.
"""
logger.info(f'Start tribler core. Base path: "{base_path}". API port: "{api_port}". '
f'API key: "{api_key}". Root state dir: "{root_state_dir}". '
f'Core test mode: "{core_test_mode}"')
f'API key: "{api_key}". Root state dir: "{root_state_dir}". '
f'Core test mode: "{core_test_mode}"')

from tribler_core.check_os import check_and_enable_code_tracing, set_process_priority
tribler_core.load_logger_config(root_state_dir)
Expand Down Expand Up @@ -92,7 +91,11 @@ async def start_tribler():
log_dir = config.general.get_path_as_absolute('log_dir', config.state_dir)
trace_logger = check_and_enable_code_tracing('core', log_dir)

session = Session(config, core_test_mode=core_test_mode)
community_loader = create_default_loader(config, chant_testnet=is_chant_testnet(config),
tunnel_testnet=is_tunnel_testnet(config),
bandwidth_testnet=is_bandwidth_testnet(config))
session = Session(config, core_test_mode=core_test_mode, community_loader=community_loader,
chant_testnet=is_chant_testnet(config), trustchain_testnet=is_trustchain_testnet(config))

signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack))
await session.start()
Expand Down Expand Up @@ -125,6 +128,30 @@ def init_sentry_reporter():
logger.info('Sentry has been initialised in debug mode')


def is_chant_testnet(config):
return ('TESTNET' in os.environ or
'CHANT_TESTNET' in os.environ or
config.chant.testnet)


def is_bandwidth_testnet(config):
return ('TESTNET' in os.environ or
'BANDWIDTH_TESTNET' in os.environ or
config.bandwidth_accounting.testnet)


def is_tunnel_testnet(config):
return ('TESTNET' in os.environ or
'TUNNEL_TESTNET' in os.environ or
config.tunnel_community.testnet)


def is_trustchain_testnet(config):
return ('TESTNET' in os.environ or
'TRUSTCHAIN_TESTNET' in os.environ or
config.trustchain.testnet)


def init_boot_logger():
# this logger config will be used before Core and GUI
# set theirs configs explicitly
Expand Down
5 changes: 2 additions & 3 deletions src/tribler-core/run_bandwidth_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from tribler_core.config.tribler_config import TriblerConfig
from tribler_core.modules.bandwidth_accounting.database import BandwidthDatabase
from tribler_core.modules.bandwidth_accounting.launcher import BandwidthCommunityLauncher
from tribler_core.modules.bandwidth_accounting.settings import BandwidthAccountingSettings
from tribler_core.modules.ipv8_module_catalog import BandwidthCommunityLauncher
from tribler_core.session import Session


Expand All @@ -38,12 +38,11 @@ def get_kwargs(self, session):


async def start_crawler(tribler_config):
session = Session(tribler_config)

# We use our own community loader
loader = IPv8CommunityLoader()
session.ipv8_community_loader = loader
loader.set_launcher(BandwidthCommunityCrawlerLauncher())
session = Session(tribler_config, community_loader=loader)

await session.start()

Expand Down
4 changes: 4 additions & 0 deletions src/tribler-core/tribler_core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def mock_dlmgr_get_download(session, mock_dlmgr): # pylint: disable=unused-argu
session.dlmgr.get_download = lambda _: None


@pytest.fixture(name='kwarg', scope='function')
async def _kwarg() -> dict:
return {}

@pytest.fixture(name='session')
async def _session(tribler_config) -> Session:
tribler_config.api.http_port = get_free_port()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from ipv8.loader import overlay, precondition, set_in_session, walk_strategy
from ipv8.loader import overlay, set_in_session, walk_strategy
from ipv8.peerdiscovery.discovery import RandomWalk

from tribler_core.modules.bandwidth_accounting.community import (
BandwidthAccountingCommunity,
BandwidthAccountingTestnetCommunity,
)
from tribler_core.modules.ipv8_module_catalog import IPv8CommunityLauncher, TestnetMixIn
from tribler_core.modules.community_loader import TestnetMixIn, TriblerCommunityLauncher


@overlay(BandwidthAccountingCommunity)
@walk_strategy(RandomWalk)
@set_in_session('bandwidth_community')
class BandwidthCommunityLauncher(IPv8CommunityLauncher):
class BandwidthCommunityLauncher(TriblerCommunityLauncher):
def get_kwargs(self, session):
return {
'settings': session.config.bandwidth_accounting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
walk_strategy,
)
from ipv8.peer import Peer
from tribler_core.config.tribler_config import TriblerConfig

INFINITE = -1
"""
The amount of target_peers for a walk_strategy definition to never stop.
"""


class IPv8CommunityLauncher(CommunityLauncher):
class TriblerCommunityLauncher(CommunityLauncher):
def get_my_peer(self, ipv8, session):
return (Peer(session.trustchain_testnet_keypair) if session.trustchain_testnet()
return (Peer(session.trustchain_testnet_keypair) if session.trustchain_testnet
else Peer(session.trustchain_keypair))

def get_bootstrappers(self, session):
Expand Down Expand Up @@ -50,7 +51,6 @@ def dht_discovery_community():
return DHTDiscoveryCommunity


# strategies
def random_churn():
from ipv8.peerdiscovery.churn import RandomChurn
return RandomChurn
Expand All @@ -77,7 +77,7 @@ def periodic_similarity():
@walk_strategy(random_churn, target_peers=INFINITE)
@walk_strategy(random_walk)
@walk_strategy(periodic_similarity, target_peers=INFINITE)
class IPv8DiscoveryCommunityLauncher(IPv8CommunityLauncher):
class IPv8DiscoveryCommunityLauncher(TriblerCommunityLauncher):
pass


Expand All @@ -87,57 +87,54 @@ class IPv8DiscoveryCommunityLauncher(IPv8CommunityLauncher):
@kwargs(max_peers='60')
@walk_strategy(ping_churn, target_peers=INFINITE)
@walk_strategy(random_walk)
class DHTCommunityLauncher(IPv8CommunityLauncher):
class DHTCommunityLauncher(TriblerCommunityLauncher):
pass


def get_hiddenimports():
"""
Return the set of all hidden imports defined by all CommunityLaunchers in this file.
"""
hiddenimports = set()

for _, obj in inspect.getmembers(sys.modules[__name__]):
hiddenimports.update(getattr(obj, "hiddenimports", set()))

return hiddenimports

def create_default_loader(config: TriblerConfig, tunnel_testnet: bool = False,
bandwidth_testnet: bool = False, chant_testnet: bool = False):
loader = IPv8CommunityLoader()

def register_launchers(session, loader):
"""
Register the default CommunityLaunchers into the given CommunityLoader.
If you define a new default CommunityLauncher, add it here.
= Warning =
Do not perform any state changes in this function. All imports and state changes should be performed within
the CommunityLaunchers themselves to be properly scoped and lazy-loaded.
"""
loader.set_launcher(IPv8DiscoveryCommunityLauncher())
loader.set_launcher(DHTCommunityLauncher())

if session.bandwidth_testnet():
if bandwidth_testnet:
from tribler_core.modules.bandwidth_accounting.launcher import BandwidthTestnetCommunityLauncher
loader.set_launcher(BandwidthTestnetCommunityLauncher())
else:
from tribler_core.modules.bandwidth_accounting.launcher import BandwidthCommunityLauncher
loader.set_launcher(BandwidthCommunityLauncher())

if session.config.tunnel_community.enabled and not session.tunnel_testnet():
if config.tunnel_community.enabled and not tunnel_testnet:
from tribler_core.modules.tunnel.community.launcher import TriblerTunnelCommunityLauncher
loader.set_launcher(TriblerTunnelCommunityLauncher())

if session.config.tunnel_community.enabled and session.tunnel_testnet():
if config.tunnel_community.enabled and tunnel_testnet:
from tribler_core.modules.tunnel.community.launcher import TriblerTunnelTestnetCommunityLauncher
loader.set_launcher(TriblerTunnelTestnetCommunityLauncher())

if session.config.popularity_community.enabled:
if config.popularity_community.enabled:
from tribler_core.modules.popularity.launcher import PopularityCommunityLauncher
loader.set_launcher(PopularityCommunityLauncher())

if session.config.chant.enabled and not session.chant_testnet():
if config.chant.enabled and not chant_testnet:
from tribler_core.modules.metadata_store.community.launcher import GigaChannelCommunityLauncher
loader.set_launcher(GigaChannelCommunityLauncher())

if session.config.chant.enabled and session.chant_testnet():
if config.chant.enabled and chant_testnet:
from tribler_core.modules.metadata_store.community.launcher import GigaChannelTestnetCommunityLauncher
loader.set_launcher(GigaChannelTestnetCommunityLauncher())

return loader


def get_hiddenimports():
"""
Return the set of all hidden imports defined by all CommunityLaunchers in this file.
"""
hiddenimports = set()

for _, obj in inspect.getmembers(sys.modules[__name__]):
hiddenimports.update(getattr(obj, "hiddenimports", set()))

return hiddenimports
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ipv8.loader import overlay, precondition, set_in_session, walk_strategy
from ipv8.loader import overlay, set_in_session, walk_strategy
from ipv8.peerdiscovery.discovery import RandomWalk
from tribler_core.modules.community_loader import INFINITE, TestnetMixIn, TriblerCommunityLauncher

from tribler_core.modules.ipv8_module_catalog import INFINITE, IPv8CommunityLauncher, TestnetMixIn
from tribler_core.modules.metadata_store.community.gigachannel_community import (
GigaChannelCommunity,
GigaChannelTestnetCommunity,
Expand All @@ -14,7 +14,7 @@
# GigaChannelCommunity remote search feature works better with higher amount of connected peers
@walk_strategy(RandomWalk, target_peers=30)
@walk_strategy(RemovePeers, target_peers=INFINITE)
class GigaChannelCommunityLauncher(IPv8CommunityLauncher):
class GigaChannelCommunityLauncher(TriblerCommunityLauncher):
def get_kwargs(self, session):
return {
'settings': session.config.chant,
Expand All @@ -25,7 +25,6 @@ def get_kwargs(self, session):
}



@overlay(GigaChannelTestnetCommunity)
class GigaChannelTestnetCommunityLauncher(TestnetMixIn, GigaChannelCommunityLauncher):
pass
6 changes: 3 additions & 3 deletions src/tribler-core/tribler_core/modules/popularity/launcher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ipv8.loader import overlay, precondition, set_in_session, walk_strategy
from ipv8.loader import overlay, set_in_session, walk_strategy
from ipv8.peerdiscovery.discovery import RandomWalk
from tribler_core.modules.community_loader import INFINITE, TriblerCommunityLauncher

from tribler_core.modules.ipv8_module_catalog import INFINITE, IPv8CommunityLauncher
from tribler_core.modules.metadata_store.community.sync_strategy import RemovePeers
from tribler_core.modules.popularity.community import PopularityCommunity

Expand All @@ -10,7 +10,7 @@
@overlay(PopularityCommunity)
@walk_strategy(RandomWalk, target_peers=30)
@walk_strategy(RemovePeers, target_peers=INFINITE)
class PopularityCommunityLauncher(IPv8CommunityLauncher):
class PopularityCommunityLauncher(TriblerCommunityLauncher):
def get_kwargs(self, session):
return {
'settings': session.config.popularity_community,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import Mock

from tribler_core.config.tribler_config import TriblerConfig
from tribler_core.modules.ipv8_module_catalog import IPv8DiscoveryCommunityLauncher, get_hiddenimports
from tribler_core.modules.community_loader import IPv8DiscoveryCommunityLauncher, get_hiddenimports


def test_hiddenimports():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ipv8.loader import after, overlay, precondition, set_in_session, walk_strategy
from ipv8.loader import after, overlay, set_in_session, walk_strategy
from ipv8.peerdiscovery.discovery import RandomWalk
from tribler_core.modules.community_loader import INFINITE, TestnetMixIn, TriblerCommunityLauncher

from tribler_core.modules.ipv8_module_catalog import INFINITE, IPv8CommunityLauncher, TestnetMixIn
from tribler_core.modules.tunnel.community.community import TriblerTunnelCommunity, TriblerTunnelTestnetCommunity
from tribler_core.modules.tunnel.community.discovery import GoldenRatioStrategy

Expand All @@ -11,7 +11,7 @@
@overlay(TriblerTunnelCommunity)
@walk_strategy(RandomWalk)
@walk_strategy(GoldenRatioStrategy, target_peers=INFINITE)
class TriblerTunnelCommunityLauncher(IPv8CommunityLauncher):
class TriblerTunnelCommunityLauncher(TriblerCommunityLauncher):
def get_kwargs(self, session):
from ipv8.dht.provider import DHTCommunityProvider
from ipv8.messaging.anonymization.community import TunnelSettings
Expand Down
Loading

0 comments on commit be593b8

Please sign in to comment.