コード例 #1
0
ファイル: p2p_connection.py プロジェクト: osthafen/spruned
    async def connect(self):
        try:
            async with async_timeout.timeout(self._timeout):
                reader, writer = await self.connector(host=self.hostname, port=self.port)
                peer = self._peer_factory(
                    reader,
                    writer,
                    self._peer_network.magic_header,
                    self._peer_network.parse_from_data,
                    self._peer_network.pack_from_data
                )
                version_data = version_data_for_peer(
                    peer, version=70015, local_services=NODE_NONE, remote_services=NODE_WITNESS
                )
                peer.version = await peer.perform_handshake(**version_data)
                self._event_handler = PeerEvent(peer)
                self._version = peer.version
                Logger.p2p.info('Connected to peer %s', self.version)
                self.peer = peer
                self._setup_events_handler()
        except Exception as e:
            self.peer = None
            Logger.p2p.debug('Exception connecting to %s (%s)', self.hostname, e)
            self.loop.create_task(self.on_error('connect'))
            return

        self.loop.create_task(self.on_connect())
        return self
 def test_Peer_version(self):
     p1, p2 = self.create_peer_pair()
     version_msg = version_data_for_peer(p1)
     version_msg["relay"] = True
     t1 = p1.send_msg("version", **version_msg)
     assert t1 is None
     t2 = run(p2.next_message())
     assert t2 == ("version", version_msg)
コード例 #3
0
ファイル: p2p_connection.py プロジェクト: pabraksas/spruned
    async def connect(self):
        try:
            async with async_timeout.timeout(self._timeout):
                reader, writer = await self.connector(host=self.hostname,
                                                      port=self.port,
                                                      proxy=self.proxy)
                peer = self._peer_factory(reader, writer,
                                          self._peer_network.magic_header,
                                          self._peer_network.parse_from_data,
                                          self._peer_network.pack_from_data)
                version_data = version_data_for_peer(
                    peer,
                    version=70015,
                    local_services=NODE_NONE,
                    remote_services=NODE_WITNESS)
                peer.version = await peer.perform_handshake(**version_data)
                await self._verify_peer(peer)
                self.starting_height = peer.version['last_block_index']
                if self._bloom_filter:
                    filter_bytes, hash_function_count, tweak = self._bloom_filter.filter_load_params(
                    )
                    flags = 0
                    peer.send_msg("filterload",
                                  filter=filter_bytes,
                                  hash_function_count=hash_function_count,
                                  tweak=tweak,
                                  flags=flags)

                self._event_handler = PeerEvent(peer)
                self._version = peer.version

                Logger.p2p.info(
                    'Connected to peer %s:%s (%s)', self.hostname,
                    self.port, self.version and self.version.get(
                        'subversion', b'').decode().strip('/'))
                Logger.p2p.debug('Peer raw response %s', self.version)
                self.peer = peer
                self.connected_at = int(time.time())
                self._setup_events_handler()
        except Exception as e:
            self.peer = None
            self.failed = True
            Logger.p2p.debug('Exception connecting to %s (%s)', self.hostname,
                             str(e))
            self.loop.create_task(self.on_error('connect'))
            return

        self.loop.create_task(self.on_connect())
        return self