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

Extract loaders from ipv8_module_catalog.py #6182

Merged
merged 5 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 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,8 @@ 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)
session = Session(config, core_test_mode=core_test_mode, community_loader=community_loader)

signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack))
await session.start()
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.community_loader import TestnetMixIn, TriblerCommunityLauncher


@overlay(BandwidthAccountingCommunity)
@walk_strategy(RandomWalk)
@set_in_session('bandwidth_community')
class BandwidthCommunityLauncher(TriblerCommunityLauncher):
def get_kwargs(self, session):
return {
'settings': session.config.bandwidth_accounting,
'database_path': session.config.state_dir / "sqlite" / "bandwidth.db",
}


@overlay(BandwidthAccountingTestnetCommunity)
class BandwidthTestnetCommunityLauncher(TestnetMixIn, BandwidthCommunityLauncher):
pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pydantic import Field

from tribler_core.config.tribler_config_section import TriblerConfigSection


class BandwidthAccountingSettings(TriblerConfigSection):
testnet: bool = False
testnet: bool = Field(default=False, env='BANDWIDTH_TESTNET')
outgoing_query_interval: int = 30 # The interval at which we send out queries to other peers, in seconds.
max_tx_returned_in_query: int = 10 # The maximum number of bandwidth transactions to return in response to a query.
129 changes: 129 additions & 0 deletions src/tribler-core/tribler_core/modules/community_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# pylint: disable=import-outside-toplevel
from ipv8.loader import (
CommunityLauncher,
IPv8CommunityLoader,
kwargs,
overlay,
precondition,
set_in_session,
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 TriblerCommunityLauncher(CommunityLauncher):
def get_my_peer(self, ipv8, session):
trustchain_testnet = session.config.general.testnet or session.config.trustchain.testnet
return (Peer(session.trustchain_testnet_keypair) if trustchain_testnet
else Peer(session.trustchain_keypair))

def get_bootstrappers(self, session):
from ipv8.bootstrapping.dispersy.bootstrapper import DispersyBootstrapper
from ipv8.configuration import DISPERSY_BOOTSTRAPPER
bootstrap_override = session.config.ipv8.bootstrap_override
if bootstrap_override:
address, port = bootstrap_override.split(':')
return [(DispersyBootstrapper, {"ip_addresses": [(address, int(port))],
"dns_addresses": []})]
return [(DispersyBootstrapper, DISPERSY_BOOTSTRAPPER['init'])]


class TestnetMixIn:
def should_launch(self, _):
return True


def discovery_community():
from ipv8.peerdiscovery.community import DiscoveryCommunity
return DiscoveryCommunity


def dht_discovery_community():
from ipv8.dht.discovery import DHTDiscoveryCommunity
return DHTDiscoveryCommunity


def random_churn():
from ipv8.peerdiscovery.churn import RandomChurn
return RandomChurn


def ping_churn():
from ipv8.dht.churn import PingChurn
return PingChurn


def random_walk():
from ipv8.peerdiscovery.discovery import RandomWalk
return RandomWalk


def periodic_similarity():
from ipv8.peerdiscovery.community import PeriodicSimilarity
return PeriodicSimilarity


@precondition('session.config.discovery_community.enabled')
@overlay(discovery_community)
@kwargs(max_peers='100')
@walk_strategy(random_churn, target_peers=INFINITE)
@walk_strategy(random_walk)
@walk_strategy(periodic_similarity, target_peers=INFINITE)
class IPv8DiscoveryCommunityLauncher(TriblerCommunityLauncher):
pass


@precondition('session.config.dht.enabled')
@set_in_session('dht_community')
@overlay(dht_discovery_community)
@kwargs(max_peers='60')
@walk_strategy(ping_churn, target_peers=INFINITE)
@walk_strategy(random_walk)
class DHTCommunityLauncher(TriblerCommunityLauncher):
pass


def create_default_loader(config: TriblerConfig):
loader = IPv8CommunityLoader()

loader.set_launcher(IPv8DiscoveryCommunityLauncher())
loader.set_launcher(DHTCommunityLauncher())

bandwidth_accounting_testnet = config.general.testnet or config.bandwidth_accounting.testnet
if bandwidth_accounting_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())

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

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

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

chant_testnet = config.general.testnet or config.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 config.chant.enabled and chant_testnet:
from tribler_core.modules.metadata_store.community.launcher import GigaChannelTestnetCommunityLauncher
loader.set_launcher(GigaChannelTestnetCommunityLauncher())

return loader
Loading