Example #1
0
    def apply(self, community: Community):
        """Apply DiscoveryBooster to the community

        Args:
            community: community to implement DiscoveryBooster

        Returns: None
        """
        if not community:
            return

        self.logger.info(
            f'Apply. Timeout: {self.timeout_in_sec}s. '
            f'Take step interval: {self.take_step_interval_in_sec}s')

        self.community = community

        if not self.walker:
            # values for neighborhood_size and edge_length were found empirically to
            # maximize peer count at the end of a 30 seconds period
            self.walker = EdgeWalk(community,
                                   neighborhood_size=25,
                                   edge_length=25)

        community.register_task(self._take_step_task_name,
                                self.take_step,
                                interval=self.take_step_interval_in_sec)
        community.register_task('finish',
                                self.finish,
                                delay=self.timeout_in_sec)
Example #2
0
 def discover(
     self,
     subcom: IPv8SubCommunity,
     target_peers: int = 20,
     discovery_params: Dict[str, Any] = None,
 ) -> None:
     discovery = ((EdgeWalk(subcom, **discovery_params),
                   target_peers) if discovery_params else
                  (EdgeWalk(subcom), target_peers))
     self.ipv8.strategies.append(discovery)
    def setUp(self):
        while _DEFAULT_ADDRESSES:
            _DEFAULT_ADDRESSES.pop()

        node_count = 3
        self.overlays = [MockCommunity() for _ in range(node_count)]
        self.strategies = [
            EdgeWalk(self.overlays[i], neighborhood_size=1)
            for i in range(node_count)
        ]
Example #4
0
    def load_ipv8_overlays(self):
        if self.config.get_testnet():
            peer = Peer(self.trustchain_testnet_keypair)
        else:
            peer = Peer(self.trustchain_keypair)
        discovery_community = DiscoveryCommunity(peer, self.ipv8.endpoint,
                                                 self.ipv8.network)
        discovery_community.resolve_dns_bootstrap_addresses()
        self.ipv8.overlays.append(discovery_community)
        self.ipv8.strategies.append((RandomChurn(discovery_community), -1))
        self.ipv8.strategies.append(
            (PeriodicSimilarity(discovery_community), -1))
        self.ipv8.strategies.append((RandomWalk(discovery_community), 20))

        # TrustChain Community
        if self.config.get_trustchain_enabled():
            from ipv8.attestation.trustchain.community import TrustChainCommunity, \
                TrustChainTestnetCommunity

            community_cls = TrustChainTestnetCommunity if self.config.get_testnet(
            ) else TrustChainCommunity
            self.trustchain_community = community_cls(
                peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                working_directory=self.config.get_state_dir())
            self.ipv8.overlays.append(self.trustchain_community)
            self.ipv8.strategies.append(
                (EdgeWalk(self.trustchain_community), 20))

            tc_wallet = TrustchainWallet(self.trustchain_community)
            self.wallets[tc_wallet.get_identifier()] = tc_wallet

        # DHT Community
        if self.config.get_dht_enabled():
            from ipv8.dht.discovery import DHTDiscoveryCommunity

            self.dht_community = DHTDiscoveryCommunity(peer,
                                                       self.ipv8.endpoint,
                                                       self.ipv8.network)
            self.ipv8.overlays.append(self.dht_community)
            self.ipv8.strategies.append((RandomWalk(self.dht_community), 20))
            self.ipv8.strategies.append((PingChurn(self.dht_community), -1))

        # Tunnel Community
        if self.config.get_tunnel_community_enabled():
            from tribler_core.modules.tunnel.community.triblertunnel_community import TriblerTunnelCommunity,\
                                                                               TriblerTunnelTestnetCommunity
            from tribler_core.modules.tunnel.community.discovery import GoldenRatioStrategy
            community_cls = TriblerTunnelTestnetCommunity if self.config.get_testnet() else \
                TriblerTunnelCommunity

            random_slots = self.config.get_tunnel_community_random_slots()
            competing_slots = self.config.get_tunnel_community_competing_slots(
            )

            dht_provider = DHTCommunityProvider(self.dht_community,
                                                self.config.get_ipv8_port())
            settings = TunnelSettings()
            settings.min_circuits = 3
            settings.max_circuits = 10
            self.tunnel_community = community_cls(
                peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                tribler_session=self,
                dht_provider=dht_provider,
                ipv8=self.ipv8,
                bandwidth_wallet=self.wallets["MB"],
                random_slots=random_slots,
                competing_slots=competing_slots,
                settings=settings)
            self.ipv8.overlays.append(self.tunnel_community)
            self.ipv8.strategies.append(
                (RandomWalk(self.tunnel_community), 20))
            self.ipv8.strategies.append(
                (GoldenRatioStrategy(self.tunnel_community), -1))

        # Market Community
        if self.config.get_market_community_enabled(
        ) and self.config.get_dht_enabled():
            from anydex.core.community import MarketCommunity, MarketTestnetCommunity

            community_cls = MarketTestnetCommunity if self.config.get_testnet(
            ) else MarketCommunity
            self.market_community = community_cls(
                peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                trustchain=self.trustchain_community,
                dht=self.dht_community,
                wallets=self.wallets,
                working_directory=self.config.get_state_dir(),
                record_transactions=self.config.get_record_transactions())

            self.ipv8.overlays.append(self.market_community)

            self.ipv8.strategies.append(
                (RandomWalk(self.market_community), 20))

        # Popular Community
        if self.config.get_popularity_community_enabled():
            from tribler_core.modules.popularity.popularity_community import PopularityCommunity

            self.popularity_community = PopularityCommunity(
                peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                metadata_store=self.mds,
                torrent_checker=self.torrent_checker)

            self.ipv8.overlays.append(self.popularity_community)
            self.ipv8.strategies.append(
                (RandomWalk(self.popularity_community), 20))

        # Gigachannel Community
        if self.config.get_chant_enabled():
            from tribler_core.modules.metadata_store.community.gigachannel_community import GigaChannelCommunity, GigaChannelTestnetCommunity
            from tribler_core.modules.metadata_store.community.sync_strategy import SyncChannels

            community_cls = GigaChannelTestnetCommunity if self.config.get_testnet(
            ) else GigaChannelCommunity
            self.gigachannel_community = community_cls(peer,
                                                       self.ipv8.endpoint,
                                                       self.ipv8.network,
                                                       self.mds,
                                                       notifier=self.notifier)

            self.ipv8.overlays.append(self.gigachannel_community)

            self.ipv8.strategies.append(
                (RandomWalk(self.gigachannel_community), 20))
            self.ipv8.strategies.append(
                (SyncChannels(self.gigachannel_community), 20))

            # Gigachannel RemoteQuery Community
            from tribler_core.modules.metadata_store.community.remote_query_community \
                import RemoteQueryCommunity, RemoteQueryTestnetCommunity

            community_cls = RemoteQueryTestnetCommunity if self.config.get_testnet(
            ) else RemoteQueryCommunity
            self.remote_query_community = community_cls(peer,
                                                        self.ipv8.endpoint,
                                                        self.ipv8.network,
                                                        self.mds,
                                                        notifier=self.notifier)

            self.ipv8.overlays.append(self.remote_query_community)
            self.ipv8.strategies.append(
                (RandomWalk(self.remote_query_community), 50))
Example #5
0
class DiscoveryBooster:
    """This class is designed for increasing the speed of peers' discovery during a limited time.

    It can be applied to any community.
    """

    # fmt: off

    def __init__(self,
                 timeout_in_sec: float = 10.0,
                 take_step_interval_in_sec: float = 0.05,
                 walker: DiscoveryStrategy = None):
        """

        Args:
            timeout_in_sec: DiscoveryBooster work timeout. When this timeout will be reached,
                `finish` function will be called.
            take_step_interval_in_sec: Сall frequency of walker's `take_step` function.
            walker: walker that will be used during boost period.
        """
        self.logger = logging.getLogger(self.__class__.__name__)

        self.timeout_in_sec = timeout_in_sec
        self.take_step_interval_in_sec = take_step_interval_in_sec
        self.walker = walker

        self.community = None

        self._take_step_task_name = 'take step'

    def apply(self, community: Community):
        """Apply DiscoveryBooster to the community

        Args:
            community: community to implement DiscoveryBooster

        Returns: None
        """
        if not community:
            return

        self.logger.info(
            f'Apply. Timeout: {self.timeout_in_sec}s. '
            f'Take step interval: {self.take_step_interval_in_sec}s')

        self.community = community

        if not self.walker:
            # values for neighborhood_size and edge_length were found empirically to
            # maximize peer count at the end of a 30 seconds period
            self.walker = EdgeWalk(community,
                                   neighborhood_size=25,
                                   edge_length=25)

        community.register_task(self._take_step_task_name,
                                self.take_step,
                                interval=self.take_step_interval_in_sec)
        community.register_task('finish',
                                self.finish,
                                delay=self.timeout_in_sec)

    def finish(self):
        """Finish DiscoveryBooster work.

        This function returns defaults max_peers to the community.

        Will be called automatically from community's task manager.

        Returns: None
        """
        self.logger.info(
            f'Finish. Cancel pending task: {self._take_step_task_name}')
        self.community.cancel_pending_task(self._take_step_task_name)

    def take_step(self):
        """Take a step by invoke `walker.take_step()`

        Will be called automatically from community's task manager.

        Returns: None
        """
        self.logger.debug('Take a step')
        self.walker.take_step()
    def start(self, options, service):
        """
        Main method to startup the cli and add a signal handler.
        """

        msg("Service: Starting")

        self.service = service

        # State directory
        state_directory = options['statedir']
        util.create_directory_if_not_exists(state_directory)

        # port
        network_port = options['port']

        # Initial configuration
        configuration = get_default_configuration()
        configuration['address'] = "0.0.0.0"
        configuration['port'] = network_port
        configuration['keys'] = [{
            'alias':
            'my peer',
            'generation':
            u"curve25519",
            'file':
            os.path.join(state_directory, u"ec.pem")
        }]
        configuration['logger'] = {'level': "ERROR"}
        configuration['overlays'] = [{
            'class':
            'DiscoveryCommunity',
            'key':
            "my peer",
            'walkers': [{
                'strategy': 'RandomWalk',
                'peers': -1,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': 'RandomChurn',
                'peers': -1,
                'init': {
                    'sample_size': 64,
                    'ping_interval': 1.0,
                    'inactive_time': 1.0,
                    'drop_time': 3.0
                }
            }],
            'initialize': {},
            'on_start': [('resolve_dns_bootstrap_addresses', )]
        }]
        configuration['overlays'] = []

        # IPv8 instance
        self.ipv8 = IPv8(configuration)

        # Network port
        actual_network_port = self.ipv8.endpoint.get_address()[1]

        # Peer
        self.my_peer = self.ipv8.keys.get('my peer')

        # Trustchain community
        self.trustchain_community = TrustChainTestnetCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            working_directory=state_directory)
        self.ipv8.overlays.append(self.trustchain_community)
        self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 10))

        # Event bus
        self.bus = EventBus()

        # module community
        self.module_community = ModuleCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            trustchain=self.trustchain_community,
            bus=self.bus,
            working_directory=state_directory,
            ipv8=self.ipv8,
            service=self.service)
        self.ipv8.overlays.append(self.module_community)
        self.ipv8.strategies.append((RandomWalk(self.module_community), 10))

        # CLI
        self.cli = CLI(self, self.ipv8, self.module_community)

        def signal_handler(sig, _):
            msg("Service: Received shut down signal %s" % sig)
            if not self._stopping:
                self.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.rest_api = RESTManager(self.ipv8)
        self.rest_api.start(actual_network_port + 1000)
        self.rest_api.root_endpoint.putChild('module',
                                             ModuleRootEndpoint(self.ipv8))

        StandardIO(self.cli)