Exemplo n.º 1
0
    def on_generic_introduction_request(self, source_address, data, prefix):
        auth, dist, payload = self._ez_unpack_auth(IntroductionRequestPayload,
                                                   data)
        peer = Peer(auth.public_key_bin, source_address)
        peer.address = UDPv4LANAddress(*payload.source_lan_address)
        peer.last_response = time.time()

        service_id = prefix[2:]
        self.on_peer_introduction_request(peer, source_address, service_id)

        self.network.add_verified_peer(peer)
        self.network.discover_services(peer, [
            service_id,
        ])

        intro_peers = [
            p for p in self.network.get_peers_for_service(service_id)
            if not p == peer
        ]
        if intro_peers:
            intro_peer = random.choice(intro_peers)
        else:
            intro_peer = None

        packet = self.create_introduction_response(payload.destination_address,
                                                   peer.address,
                                                   payload.identifier,
                                                   introduction=intro_peer,
                                                   prefix=prefix)
        self.endpoint.send(peer.address, packet)
Exemplo n.º 2
0
    def on_generic_introduction_request(self, source_address, data, prefix):
        auth, dist, payload = self._ez_unpack_auth(IntroductionRequestPayload,
                                                   data)
        peer = Peer(auth.public_key_bin, source_address)
        peer.last_response = time.time()

        self.network.add_verified_peer(peer)
        self.network.discover_services(peer, [
            prefix[2:],
        ])

        intro_peers = [
            p for p in self.network.get_peers_for_service(prefix[2:])
            if not (p == peer)
        ]
        if intro_peers:
            intro_peer = random.choice(intro_peers)
        else:
            intro_peer = None

        packet = self.create_introduction_response(payload.destination_address,
                                                   peer.address,
                                                   payload.identifier,
                                                   introduction=intro_peer)

        packet = prefix + packet[22:-self.signature_length]
        signature = default_eccrypto.create_signature(self.my_peer.key, packet)

        self.endpoint.send(peer.address, packet + signature)
Exemplo n.º 3
0
    def test_channels_peers_mapping_drop_excess_peers(self):
        """
        Test dropping old excess peers from a channel to peers mapping
        """
        mapping = ChannelsPeersMapping()
        chan_pk = Mock()
        chan_id = 123

        num_excess_peers = 20
        t = time.time() - 1000
        first_peer_timestamp = t
        for k in range(0, mapping.max_peers_per_channel + num_excess_peers):
            peer = Peer(default_eccrypto.generate_key("very-low"),
                        ("1.2.3.4", 5))
            peer.last_response = t
            t += 1.0
            mapping.add(peer, chan_pk, chan_id)
            if k == 0:
                first_peer_timestamp = peer.last_response

        chan_peers_3 = mapping.get_last_seen_peers_for_channel(
            chan_pk, chan_id, 3)
        assert len(chan_peers_3) == 3

        chan_peers = mapping.get_last_seen_peers_for_channel(chan_pk, chan_id)
        assert len(chan_peers) == mapping.max_peers_per_channel

        assert chan_peers_3 == chan_peers[0:3]
        assert chan_peers == sorted(chan_peers,
                                    key=lambda x: x.last_response,
                                    reverse=True)

        # Make sure only the older peers are dropped as excess
        for p in chan_peers:
            assert p.last_response > first_peer_timestamp

        # Test removing a peer directly, e.g. as a result of a query timeout
        peer = Peer(default_eccrypto.generate_key("very-low"), ("1.2.3.4", 5))
        mapping.add(peer, chan_pk, chan_id)
        mapping.remove_peer(peer)
        for p in chan_peers:
            mapping.remove_peer(p)

        assert mapping.get_last_seen_peers_for_channel(chan_pk, chan_id) == []

        # Make sure the stuff is cleaned up
        assert len(mapping._peers_channels) == 0
        assert len(mapping._channels_dict) == 0