def discover( self, subcom: IPv8SubCommunity, target_peers: int = 20, discovery_params: Dict[str, Any] = None, ) -> None: discovery = ((RandomWalk(subcom, **discovery_params), target_peers) if discovery_params else (RandomWalk(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 = [ RandomWalk(self.overlays[i]) for i in range(node_count) ]
async def on_tribler_started(self): await super().on_tribler_started() session = self.session peer = Peer(session.trustchain_keypair) session.popularity_community = ObservablePopularityCommunity(self._interval_in_sec, self._output_file_path, peer, session.ipv8.endpoint, session.ipv8.network, metadata_store=session.mds, torrent_checker=session.torrent_checker) session.ipv8.overlays.append(session.popularity_community) session.ipv8.strategies.append((RandomWalk(session.popularity_community), TARGET_PEERS_COUNT))
async def on_tribler_started(self): await super().on_tribler_started() session = self.session peer = Peer(session.trustchain_keypair) crawler_settings = SimpleNamespace(output_file_path=self._output_file_path, peers_count_csv_file_path=self._peers_count_csv_file_path) session.remote_query_community = TorrentCrawler(peer, session.ipv8.endpoint, session.ipv8.network, session.mds, crawler_settings) session.ipv8.overlays.append(session.remote_query_community) session.ipv8.strategies.append((RandomWalk(session.remote_query_community, timeout=RANDOM_WALK_TIMEOUT_IN_SEC, window_size=RANDOM_WALK_WINDOW_SIZE, reset_chance=RANDOM_WALK_RESET_CHANCE), UNLIMITED))
def initialise_community_by_default(self, community, default_random_walk_max_peers=20): community.bootstrappers.append(self.make_bootstrapper()) # Value of `target_peers` must not be equal to the value of `max_peers` for the community. # This causes a deformed network topology and makes it harder for peers to connect to others. # More information: https://github.com/Tribler/py-ipv8/issues/979#issuecomment-896643760 # # Then: # random_walk_max_peers should be less than community.max_peers: random_walk_max_peers = min(default_random_walk_max_peers, community.max_peers - 10) # random_walk_max_peers should be greater than 0 random_walk_max_peers = max(0, random_walk_max_peers) self.ipv8.add_strategy(community, RandomWalk(community), random_walk_max_peers)
def start_trust(self, options): """ Main method to startup the trust. """ root = logging.getLogger() root.setLevel(logging.INFO) stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setLevel(logging.INFO) stderr_handler.setFormatter( logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")) root.addHandler(stderr_handler) self.ipv8 = IPv8(configuration) # Peer my_peer = self.ipv8.keys.get('my peer') # trust community trust_community = TrustCommunity(my_peer, self.ipv8.endpoint, self.ipv8.network) self.ipv8.overlays.append(trust_community) self.ipv8.strategies.append((RandomWalk(trust_community), 10)) def signal_handler(sig, _): msg("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True self.restapi.stop() self.ipv8.stop() signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) self.restapi = RESTManager(self.ipv8) reactor.callLater(0.0, self.restapi.start, 8001)
async def run(self): config = self.session.config ipv8_component = await self.require_component(Ipv8Component) self._ipv8 = ipv8_component.ipv8 peer = ipv8_component.peer metadata_store_component = await self.require_component( MetadataStoreComponent) torrent_checker_component = await self.require_component( TorrentCheckerComponent) community = ObservablePopularityCommunity( peer, self._ipv8.endpoint, self._ipv8.network, settings=config.popularity_community, rqc_settings=config.remote_query_community, metadata_store=metadata_store_component.mds, torrent_checker=torrent_checker_component.torrent_checker) self.community = community self._ipv8.add_strategy(community, RandomWalk(community), 30) self._ipv8.add_strategy(community, RemovePeers(community), -1) community.bootstrappers.append(ipv8_component.make_bootstrapper())
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))
def start_anydex(self, options): """ Main method to startup AnyDex. """ config = get_anydex_configuration() if options["statedir"]: # If we use a custom state directory, update various variables for key in config["keys"]: key["file"] = os.path.join(options["statedir"], key["file"]) for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["initialize"]["working_directory"] = options["statedir"] if 'testnet' in options and options['testnet']: for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["class"] = "TrustChainTestnetCommunity" self.ipv8 = IPv8(config, enable_statistics=options['statistics']) def signal_handler(sig, _): msg("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True if self.restapi: self.restapi.stop() self.ipv8.stop() signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) msg("Starting AnyDex") if not options['no-rest-api']: self.restapi = RESTManager(self.ipv8) reactor.callLater(0.0, self.restapi.start) factory = WebSocketServerFactory(u"ws://127.0.0.1:9000") factory.protocol = AnyDexWebsocketProtocol reactor.listenTCP(9000, factory) # Get Trustchain + DHT overlays for overlay in self.ipv8.overlays: if isinstance(overlay, TrustChainTestnetCommunity): self.trustchain = overlay elif isinstance(overlay, DHTDiscoveryCommunity): self.dht = overlay # Initialize wallets dummy_wallet1 = DummyWallet1() self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1 dummy_wallet2 = DummyWallet2() self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2 # Load market community self.market = MarketTestnetCommunity(self.trustchain.my_peer, self.ipv8.endpoint, self.ipv8.network, trustchain=self.trustchain, dht=self.dht, wallets=self.wallets, working_directory=options["statedir"], record_transactions=False) self.ipv8.overlays.append(self.market) self.ipv8.strategies.append((RandomWalk(self.market), 20))
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)
async def start_anydex(self, options): """ Main method to startup AnyDex. """ config = get_anydex_configuration() if options.statedir: # If we use a custom state directory, update various variables for key in config["keys"]: key["file"] = os.path.join(options.statedir, key["file"]) for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["initialize"][ "working_directory"] = options.statedir if options.testnet: for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["class"] = "TrustChainTestnetCommunity" self.ipv8 = IPv8(config, enable_statistics=options.statistics) await self.ipv8.start() print("Starting AnyDex") if not options.no_rest_api: self.restapi = RESTManager(self.ipv8) await self.restapi.start(options.apiport) async def signal_handler(sig): print("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True if self.restapi: await self.restapi.stop() self.restapi = None await self.ipv8.stop() get_event_loop().stop() signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig))) signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig))) # Get Trustchain + DHT overlays for overlay in self.ipv8.overlays: if isinstance(overlay, (TrustChainCommunity, TrustChainTestnetCommunity)): self.trustchain = overlay elif isinstance(overlay, DHTDiscoveryCommunity): self.dht = overlay # Initialize wallets dummy_wallet1 = DummyWallet1() self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1 dummy_wallet2 = DummyWallet2() self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2 # Load market community self.market = MarketTestnetCommunity( self.trustchain.my_peer, self.ipv8.endpoint, self.ipv8.network, trustchain=self.trustchain, dht=self.dht, wallets=self.wallets, working_directory=options.statedir, record_transactions=False, is_matchmaker=not options.no_matchmaker) self.ipv8.overlays.append(self.market) self.ipv8.strategies.append((RandomWalk(self.market), 20))
async def start_anydex(self, options): """ Main method to startup AnyDex. """ config = get_anydex_configuration() if options.statedir: # If we use a custom state directory, update various variables for key in config["keys"]: key["file"] = os.path.join(options.statedir, key["file"]) for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["initialize"]["working_directory"] = options.statedir if options.testnet: for community in config["overlays"]: if community["class"] == "TrustChainCommunity": community["class"] = "TrustChainTestnetCommunity" self.ipv8 = IPv8(config, enable_statistics=options.statistics) await self.ipv8.start() print("Starting AnyDex") if not options.no_rest_api: self.restapi = RESTManager(self.ipv8) await self.restapi.start(options.apiport) async def signal_handler(sig): print("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True if self.restapi: await self.restapi.stop() self.restapi = None await self.ipv8.stop() get_event_loop().stop() signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig))) signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig))) # Get Trustchain + DHT overlays for overlay in self.ipv8.overlays: # Logger level required by bitcoinlib import overlay.logger.setLevel('INFO') if isinstance(overlay, (TrustChainCommunity, TrustChainTestnetCommunity)): self.trustchain = overlay elif isinstance(overlay, DHTDiscoveryCommunity): self.dht = overlay # Initialize dummy wallets dummy_wallet1 = DummyWallet1() self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1 dummy_wallet2 = DummyWallet2() self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2 # Bitcoinlib imports required to be later due to logger overlap from anydex.wallet.bitcoinlib.bitcoinlib_wallets import BitcoinTestnetWallet, BitcoinWallet, LitecoinWallet, \ LitecoinTestnetWallet, DashWallet, DashTestnetWallet # Initialize bitcoin wallets btc_wallet = BitcoinWallet(os.path.join(options.statedir, 'sqlite')) btc_wallet.create_wallet() self.wallets[btc_wallet.get_identifier()] = btc_wallet btc_testnet_wallet = BitcoinTestnetWallet(os.path.join(options.statedir, 'sqlite')) btc_testnet_wallet.create_wallet() self.wallets[btc_testnet_wallet.get_identifier()] = btc_testnet_wallet # Initialize litecoin wallets ltc_wallet = LitecoinWallet(os.path.join(options.statedir, 'sqlite')) ltc_wallet.create_wallet() self.wallets[ltc_wallet.get_identifier()] = ltc_wallet ltc_testnet_wallet = LitecoinTestnetWallet(os.path.join(options.statedir, 'sqlite')) ltc_testnet_wallet.create_wallet() self.wallets[ltc_testnet_wallet.get_identifier()] = ltc_testnet_wallet # Initialize dash wallets dash_wallet = DashWallet(os.path.join(options.statedir, 'sqlite')) dash_wallet.create_wallet() self.wallets[dash_wallet.get_identifier()] = dash_wallet # DASH TESTNET HAS NO PROVIDERS IN BITCOINLIB # dash_testnet_wallet = DashTestnetWallet(os.path.join(options.statedir, 'sqlite')) # dash_testnet_wallet.create_wallet() # self.wallets[dash_testnet_wallet.get_identifier()] = dash_testnet_wallet # Initialize ethereum wallets eth_wallet = EthereumWallet(os.path.join(options.statedir, 'sqlite')) eth_wallet.create_wallet() self.wallets[eth_wallet.get_identifier()] = eth_wallet eth_testnet_wallet = EthereumTestnetWallet(os.path.join(options.statedir, 'sqlite')) eth_testnet_wallet.create_wallet() self.wallets[eth_testnet_wallet.get_identifier()] = eth_testnet_wallet # Initialize iota wallets iota_wallet = IotaWallet(os.path.join(options.statedir, 'sqlite')) iota_wallet.create_wallet() self.wallets[iota_wallet.get_identifier()] = iota_wallet iota_testnet_wallet = IotaTestnetWallet(os.path.join(options.statedir, 'sqlite')) iota_testnet_wallet.create_wallet() self.wallets[iota_testnet_wallet.get_identifier()] = iota_testnet_wallet # Initialize monero wallets # xmr_wallet = MoneroWallet(os.path.join(options.statedir, 'sqlite')) # xmr_wallet.create_wallet() # self.wallets[xmr_wallet.get_identifier()] = xmr_wallet # xmr_testnet_wallet = MoneroTestnetWallet(os.path.join(options.statedir, 'sqlite')) # xmr_testnet_wallet.create_wallet() # self.wallets[xmr_testnet_wallet.get_identifier()] = xmr_testnet_wallet # Initialize stellar wallets xlm_wallet = StellarWallet(os.path.join(options.statedir, 'sqlite')) xlm_wallet.create_wallet() self.wallets[xlm_wallet.get_identifier()] = xlm_wallet xlm_testnet_wallet = StellarTestnetWallet(os.path.join(options.statedir, 'sqlite')) xlm_testnet_wallet.create_wallet() self.wallets[xlm_testnet_wallet.get_identifier()] = xlm_testnet_wallet # Load market community self.market = MarketTestnetCommunity(self.trustchain.my_peer, self.ipv8.endpoint, self.ipv8.network, trustchain=self.trustchain, dht=self.dht, wallets=self.wallets, working_directory=options.statedir, record_transactions=False, is_matchmaker=not options.no_matchmaker) self.ipv8.overlays.append(self.market) self.ipv8.strategies.append((RandomWalk(self.market), 20))