def build(ctx, loop=asyncio.get_event_loop()): # pragma: no cover from spruned.daemon.electrod.electrod_connection import ElectrodConnectionPool from spruned.daemon.electrod.electrod_interface import ElectrodInterface from spruned.daemon.electrod.electrod_fee_estimation import EstimateFeeConsensusProjector, \ EstimateFeeConsensusCollector network = ctx.get_network() peers = load_electrum_servers(ctx) fees_collector = EstimateFeeConsensusCollector(proxy=ctx.proxy) _ = [ fees_collector.add_peer(peer) for peer in [x[0] + '/' + x[1] for x in peers] ] electrod_pool = ElectrodConnectionPool( connections=network['electrum_concurrency'], peers=peers, ipv6=False, proxy=ctx.proxy, tor=ctx.tor) electrod_interface = ElectrodInterface( electrod_pool, loop, fees_projector=EstimateFeeConsensusProjector(), fees_collector=fees_collector) electrod_interface.add_on_connected_callback( electrod_interface.bootstrap_collector) return electrod_pool, electrod_interface
def __init__(self, hostname: str, protocol: str, keepalive=180, client=StratumClient, serverinfo=ServerInfo, nickname=None, use_tor=False, loop=None, start_score=10, timeout=10, expire_errors_after=180, is_online_checker: callable = None, delayer=async_delayed_task, network=ctx.get_network()): self.protocol = protocol self.port = self.protocol self.keepalive = keepalive self.client = client() self.serverinfo_factory = serverinfo self.client.keepalive_interval = keepalive self.nickname = nickname or binascii.hexlify(os.urandom(8)).decode() self.network = network super().__init__(hostname=hostname, use_tor=use_tor, loop=loop, start_score=start_score, is_online_checker=is_online_checker, timeout=timeout, delayer=delayer, expire_errors_after=expire_errors_after)
def __init__(self, connectionpool: ElectrodConnectionPool, loop=asyncio.get_event_loop()): self._network = ctx.get_network() self.pool = connectionpool self._checkpoints = self._network['checkpoints'] self.loop = loop
def build(ctx, loop=asyncio.get_event_loop()): # pragma: no cover from spruned.daemon.electrod.electrod_connection import ElectrodConnectionPool from spruned.daemon.electrod.electrod_interface import ElectrodInterface network = ctx.get_network() electrod_pool = ElectrodConnectionPool( connections=network["electrum_concurrency"], peers=load_electrum_servers(ctx)) electrod_interface = ElectrodInterface(electrod_pool, loop) return electrod_pool, electrod_interface
def __init__(self, connectionpool: ElectrodConnectionPool, loop=asyncio.get_event_loop(), fees_projector: EstimateFeeConsensusProjector = None, fees_collector: EstimateFeeConsensusCollector = None): self._network = ctx.get_network() self.pool = connectionpool self._checkpoints = self._network['checkpoints'] self.loop = loop self._fees_projector = fees_projector self._fees_collector = fees_collector self._collector_bootstrap = False
def load_p2p_peers(): from spruned.application.context import ctx _local = ctx.datadir + '/p2p_peers.json' local_peers = [] if os.path.exists(_local) and os.path.isfile(_local): with open(_local, 'r') as fr: try: local_peers = json.load(fr)['p2p_peers'] except JSONDecodeError: os.remove(_local) network = ctx.get_network() _current_path = os.path.dirname(os.path.abspath(__file__)) with open(_current_path + '/p2p_peers.json', 'r') as f: hardcoded_peers = json.load(f)[network['alias']] local_peers = [peer for peer in local_peers if peer not in hardcoded_peers] peers = local_peers + hardcoded_peers if ctx.tor: return [s for s in peers if '.onion' in s[0]] else: return [s for s in peers if '.onion' not in s[0]]
def test_no_local_headers(self): """ a new header is received, best_height is 3000 there is no local header the chunk 0 is requested, but there are no peers available due race condition then the chunk 0 is fetched and saved then the chunk 1 is fetched and saved new height is 3000 reactor is in sync with the network """ header_timestamp = int(time.time()) net_header = { "block_height": 3000, "block_hash": "cc" * 32, "timestamp": header_timestamp, "prev_block_hash": "00" * 32, "header_bytes": b"0" * 80 } peer = Mock(server_info='mock_peer') loc_header = None self.repo.get_best_header.return_value = loc_header _chunk_1 = make_headers(0, 2015, ctx.get_network()['checkpoints'][0]) _chunk_2 = make_headers(2016, 2999, _chunk_1[-1]['block_height']) _chunk_2.append(net_header) self.interface.get_headers_in_range_from_chunks.side_effect = [ exceptions.NoPeersException, async_coro(None), async_coro((Mock(), _chunk_1)), async_coro((Mock(), _chunk_2)) ] self.interface.get_header.return_value = async_coro( (Mock(), net_header)) self.repo.save_headers.side_effect = lambda x, **k: x self.loop.run_until_complete(self.sut.on_new_header(peer, net_header)) self.assertEqual(self.sut._last_processed_header, net_header) self.assertTrue(self.sut.synced) self.assertEqual(4, len(self.interface.method_calls)) self.assertEqual(5, len(self.repo.method_calls)) self.assertEqual(0, len(self.electrod_loop.method_calls))