Skip to content

Commit

Permalink
Update IPv8 pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman authored and devos50 committed Aug 18, 2020
1 parent 8f1ab43 commit 6ddf9d4
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/pyipv8
Submodule pyipv8 updated 55 files
+1 −4 README.md
+36 −40 doc/basics/overlay_tutorial.rst
+38 −0 doc/further-reading/advanced_identity.rst
+1 −1 ipv8/REST/attestation_endpoint.py
+1 −2 ipv8/REST/base_endpoint.py
+0 −120 ipv8/REST/json_util.py
+2 −2 ipv8/REST/overlays_endpoint.py
+5 −4 ipv8/REST/rest_manager.py
+125 −3 ipv8/REST/tunnel_endpoint.py
+1 −1 ipv8/attestation/trustchain/caches.py
+7 −9 ipv8/attestation/trustchain/community.py
+7 −0 ipv8/attestation/trustchain/payload.py
+6 −8 ipv8/attestation/wallet/community.py
+5 −0 ipv8/attestation/wallet/payload.py
+3 −3 ipv8/attestation/wallet/primitives/attestation.py
+46 −47 ipv8/community.py
+0 −26 ipv8/configuration.py
+14 −23 ipv8/dht/community.py
+8 −15 ipv8/dht/discovery.py
+10 −4 ipv8/dht/payload.py
+8 −14 ipv8/keyvault/public/m2crypto.py
+14 −0 ipv8/messaging/anonymization/caches.py
+136 −146 ipv8/messaging/anonymization/community.py
+3 −3 ipv8/messaging/anonymization/endpoint.py
+49 −55 ipv8/messaging/anonymization/hidden_services.py
+74 −28 ipv8/messaging/anonymization/payload.py
+26 −16 ipv8/messaging/anonymization/tunnel.py
+6 −6 ipv8/messaging/deprecated/encoding.py
+0 −0 ipv8/messaging/interfaces/dispatcher/__init__.py
+142 −0 ipv8/messaging/interfaces/dispatcher/endpoint.py
+20 −1 ipv8/messaging/interfaces/endpoint.py
+2 −2 ipv8/messaging/interfaces/statistics_endpoint.py
+23 −2 ipv8/messaging/interfaces/udp/endpoint.py
+4 −0 ipv8/messaging/payload.py
+94 −4 ipv8/peer.py
+4 −6 ipv8/peerdiscovery/community.py
+19 −12 ipv8/peerdiscovery/network.py
+4 −1 ipv8/peerdiscovery/payload.py
+0 −0 ipv8/test/REST/identity/__init__.py
+8 −2 ipv8/test/REST/rest_base.py
+4 −4 ipv8/test/REST/test_attestation_endpoint.py
+8 −8 ipv8/test/REST/test_identity_endpoint.py
+102 −0 ipv8/test/REST/test_network_endpoint.py
+14 −0 ipv8/test/keyvault/test_crypto.py
+44 −28 ipv8/test/messaging/anonymization/test_community.py
+50 −12 ipv8/test/messaging/anonymization/test_datachecker.py
+46 −0 ipv8/test/messaging/anonymization/test_exit_socket.py
+38 −3 ipv8/test/messaging/anonymization/test_hiddenservices.py
+5 −1 ipv8/test/mocking/endpoint.py
+1 −1 ipv8/test/mocking/exit_socket.py
+0 −10 ipv8/test/test_configuration.py
+102 −0 ipv8/test/test_peer.py
+5 −1 ipv8_service.py
+0 −237 stresstest/endpoint_stresstest.py
+0 −96 stresstest/endpoint_stresstest_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import logging

from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_ANY
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_BT

from tribler_common.simpledefs import (
DLSTATUS_ALLOCATING_DISKSPACE,
Expand Down Expand Up @@ -72,7 +72,7 @@ def get_status(self):
@return DLSTATUS_* """
if not self.lt_status:
return (DLSTATUS_CIRCUITS if not self.download.session.tunnel_community
or self.download.session.tunnel_community.get_candidates(PEER_FLAG_EXIT_ANY)
or self.download.session.tunnel_community.get_candidates(PEER_FLAG_EXIT_BT)
else DLSTATUS_EXIT_NODES) if self.download.config.get_hops() > 0 else DLSTATUS_WAITING4HASHCHECK
elif self.get_error():
return DLSTATUS_STOPPED_ON_ERROR
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from random import sample

from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_ANY
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_BT
from ipv8.peerdiscovery.discovery import DiscoveryStrategy


Expand Down Expand Up @@ -59,7 +59,7 @@ def take_step(self):

peer_count = len(peers)
if peer_count > self.target_peers:
exit_peers = set(self.overlay.get_candidates(PEER_FLAG_EXIT_ANY))
exit_peers = set(self.overlay.get_candidates(PEER_FLAG_EXIT_BT))
exit_count = len(exit_peers)
ratio = 1.0 - exit_count / peer_count # Peer count is > 0 per definition
if ratio < self.golden_ratio:
Expand Down
45 changes: 21 additions & 24 deletions src/tribler-core/tribler_core/modules/tunnel/community/payload.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from ipv8.attestation.trustchain.payload import HalfBlockPayload
from ipv8.messaging.lazy_payload import VariablePayload
from ipv8.messaging.payload import Payload
from ipv8.messaging.lazy_payload import VariablePayload, vp_compile


class PayoutPayload(HalfBlockPayload):

msg_id = 23
format_list = HalfBlockPayload.format_list + ["I", "I"]

def __init__(self, public_key, sequence_number, link_public_key, link_sequence_number, previous_hash,
Expand Down Expand Up @@ -42,7 +41,7 @@ def from_unpack_list(cls, *args):


class BalanceResponsePayload(HalfBlockPayload):

msg_id = 26
format_list = ["I"] + HalfBlockPayload.format_list

def __init__(self, circuit_id, public_key, sequence_number, link_public_key, link_sequence_number, previous_hash,
Expand All @@ -54,7 +53,7 @@ def __init__(self, circuit_id, public_key, sequence_number, link_public_key, lin

@classmethod
def from_half_block(cls, block, circuit_id):
return BalanceResponsePayload(
return cls(
circuit_id,
block.public_key,
block.sequence_number,
Expand All @@ -74,38 +73,36 @@ def to_pack_list(self):

@classmethod
def from_unpack_list(cls, *args):
return BalanceResponsePayload(*args)

return cls(*args)

class BalanceRequestPayload(Payload):

format_list = ['I']

def __init__(self, circuit_id):
super(BalanceRequestPayload, self).__init__()
self._circuit_id = circuit_id
class RelayBalanceResponsePayload(BalanceResponsePayload):
msg_id = 27

def to_pack_list(self):
data = [('I', self.circuit_id)]

return data
@vp_compile
class BalanceRequestPayload(VariablePayload):
msg_id = 24
format_list = ['I']
names = ['circuit_id']

@classmethod
def from_unpack_list(cls, circuit_id):
return BalanceRequestPayload(circuit_id)

@property
def circuit_id(self):
return self._circuit_id
@vp_compile
class RelayBalanceRequestPayload(VariablePayload):
msg_id = 25
format_list = ['I']
names = ['circuit_id']


@vp_compile
class HTTPRequestPayload(VariablePayload):

msg_id = 28
format_list = ['I', 'I', 'varlenH', 'varlenH']
names = ['circuit_id', 'identifier', 'target', 'request']


@vp_compile
class HTTPResponsePayload(VariablePayload):

msg_id = 29
format_list = ['I', 'I', 'H', 'H', 'varlenH']
names = ['circuit_id', 'identifier', 'part', 'total', 'response']
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ipv8.attestation.trustchain.block import EMPTY_PK
from ipv8.messaging.anonymization.caches import CreateRequestCache
from ipv8.messaging.anonymization.community import message_to_payload, tc_lazy_wrapper_unsigned
from ipv8.messaging.anonymization.community import unpack_cell
from ipv8.messaging.anonymization.hidden_services import HiddenTunnelCommunity
from ipv8.messaging.anonymization.payload import (
EstablishIntroPayload,
Expand All @@ -29,8 +29,9 @@
CIRCUIT_TYPE_RP_DOWNLOADER,
CIRCUIT_TYPE_RP_SEEDER,
EXIT_NODE,
PEER_FLAG_EXIT_ANY,
RelayRoute,
PEER_FLAG_EXIT_BT,
PEER_FLAG_EXIT_IPV8,
RelayRoute
)
from ipv8.peer import Peer
from ipv8.peerdiscovery.network import Network
Expand All @@ -49,6 +50,8 @@
HTTPRequestPayload,
HTTPResponsePayload,
PayoutPayload,
RelayBalanceRequestPayload,
RelayBalanceResponsePayload
)
from tribler_core.modules.tunnel.socks5.server import Socks5Server
from tribler_core.utilities import path_util
Expand Down Expand Up @@ -85,7 +88,8 @@ def __init__(self, *args, **kwargs):

if self.tribler_session:
if self.tribler_session.config.get_tunnel_community_exitnode_enabled():
self.settings.peer_flags.add(PEER_FLAG_EXIT_ANY)
self.settings.peer_flags.add(PEER_FLAG_EXIT_BT)
self.settings.peer_flags.add(PEER_FLAG_EXIT_IPV8)
self.settings.peer_flags.add(PEER_FLAG_EXIT_HTTP)

if not socks_listen_ports:
Expand All @@ -110,25 +114,14 @@ def __init__(self, *args, **kwargs):

self.dispatcher.set_socks_servers(self.socks_servers)

self.decode_map.update({
chr(23): self.on_payout_block,
})

self.decode_map_private.update({
chr(24): self.on_balance_request_cell,
chr(25): self.on_relay_balance_request_cell,
chr(26): self.on_balance_response_cell,
chr(27): self.on_relay_balance_response_cell,
chr(28): self.on_http_request,
chr(29): self.on_http_response
})

message_to_payload["balance-request"] = (24, BalanceRequestPayload)
message_to_payload["relay-balance-request"] = (25, BalanceRequestPayload)
message_to_payload["balance-response"] = (26, BalanceResponsePayload)
message_to_payload["relay-balance-response"] = (27, BalanceResponsePayload)
message_to_payload["http-request"] = (28, HTTPRequestPayload)
message_to_payload["http-response"] = (29, HTTPResponsePayload)
self.add_message_handler(PayoutPayload, self.on_payout_block)

self.add_cell_handler(BalanceRequestPayload, self.on_balance_request_cell)
self.add_cell_handler(RelayBalanceRequestPayload, self.on_relay_balance_request_cell)
self.add_cell_handler(BalanceResponsePayload, self.on_balance_response_cell)
self.add_cell_handler(RelayBalanceResponsePayload, self.on_relay_balance_response_cell)
self.add_cell_handler(HTTPRequestPayload, self.on_http_request)
self.add_cell_handler(HTTPResponsePayload, self.on_http_response)

NO_CRYPTO_PACKETS.extend([24, 26])

Expand All @@ -151,7 +144,7 @@ def cache_exitnodes_to_disk(self):
:returns: None
"""
exit_nodes = Network()
for peer in self.get_candidates(PEER_FLAG_EXIT_ANY):
for peer in self.get_candidates(PEER_FLAG_EXIT_BT):
exit_nodes.add_verified_peer(peer)
self.logger.debug('Writing exit nodes to cache: %s', self.exitnode_cache)
with open(self.exitnode_cache, 'wb') as cache:
Expand Down Expand Up @@ -238,8 +231,7 @@ def should_join_circuit(self, create_payload, previous_node_address):
shared_secret, _, _ = self.crypto.generate_diffie_shared_secret(create_payload.key)
self.relay_session_keys[circuit_id] = self.crypto.generate_session_keys(shared_secret)

self.send_cell(Peer(create_payload.node_public_key, previous_node_address), "balance-request",
BalanceRequestPayload(circuit_id))
self.send_cell(Peer(create_payload.node_public_key, previous_node_address), BalanceRequestPayload(circuit_id))

self.directions.pop(circuit_id, None)
self.relay_session_keys.pop(circuit_id, None)
Expand Down Expand Up @@ -272,21 +264,19 @@ async def on_payout_block(self, source_address, data):
self.logger.warning("Not proceeding with payout - received payout block is not valid")
return

def on_balance_request_cell(self, source_address, data, _):
payload = self._ez_unpack_noauth(BalanceRequestPayload, data, global_time=False)

@unpack_cell(BalanceRequestPayload)
def on_balance_request_cell(self, source_address, payload, _):
if self.request_cache.has("create", payload.circuit_id):
request = self.request_cache.get("create", payload.circuit_id)
forwarding_relay = RelayRoute(request.from_circuit_id, request.peer)
self.send_cell(forwarding_relay.peer, "relay-balance-request",
BalanceRequestPayload(forwarding_relay.circuit_id))
self.send_cell(forwarding_relay.peer, RelayBalanceRequestPayload(forwarding_relay.circuit_id))
elif self.request_cache.has("retry", payload.circuit_id):
self.on_balance_request(payload)
else:
self.logger.warning("Circuit creation cache for id %s not found!", payload.circuit_id)

def on_relay_balance_request_cell(self, source_address, data, _):
payload = self._ez_unpack_noauth(BalanceRequestPayload, data, global_time=False)
@unpack_cell(RelayBalanceRequestPayload)
def on_relay_balance_request_cell(self, source_address, payload, _):
self.on_balance_request(payload)

def on_balance_request(self, payload):
Expand All @@ -307,36 +297,28 @@ def on_balance_request(self, payload):
# We either send the response directly or relay the response to the last verified hop
circuit = self.circuits[payload.circuit_id]
if not circuit.hops:
self.increase_bytes_sent(circuit, self.send_cell(circuit.peer,
"balance-response",
BalanceResponsePayload.from_half_block(
latest_block, circuit.circuit_id)))
self.send_cell(circuit.peer, BalanceResponsePayload.from_half_block(latest_block, circuit.circuit_id))
else:
self.increase_bytes_sent(circuit, self.send_cell(circuit.peer,
"relay-balance-response",
BalanceResponsePayload.from_half_block(
latest_block, circuit.circuit_id)))
self.send_cell(circuit.peer, RelayBalanceResponsePayload.from_half_block(latest_block, circuit.circuit_id))

def on_balance_response_cell(self, source_address, data, _):
payload = self._ez_unpack_noauth(BalanceResponsePayload, data, global_time=False)
@unpack_cell(BalanceResponsePayload)
def on_balance_response_cell(self, source_address, payload, _):
block = TriblerBandwidthBlock.from_payload(payload, self.serializer)
if not block.transaction:
self.on_token_balance(payload.circuit_id, 0)
else:
self.on_token_balance(payload.circuit_id,
block.transaction[b"total_up"] - block.transaction[b"total_down"])

def on_relay_balance_response_cell(self, source_address, data, _):
payload = self._ez_unpack_noauth(BalanceResponsePayload, data, global_time=False)
@unpack_cell(RelayBalanceResponsePayload)
def on_relay_balance_response_cell(self, source_address, payload, _):
block = TriblerBandwidthBlock.from_payload(payload, self.serializer)

# At this point, we don't have the circuit ID of the follow-up hop. We have to iterate over the items in the
# request cache and find the link to the next hop.
for cache in self.request_cache._identifiers.values():
if isinstance(cache, CreateRequestCache) and cache.from_circuit_id == payload.circuit_id:
self.send_cell(cache.to_peer,
"balance-response",
BalanceResponsePayload.from_half_block(block, cache.to_circuit_id))
self.send_cell(cache.to_peer, BalanceResponsePayload.from_half_block(block, cache.to_circuit_id))

def readd_bittorrent_peers(self):
for torrent, peers in list(self.bittorrent_peers.items()):
Expand Down Expand Up @@ -602,7 +584,7 @@ async def unload(self):
def get_lookup_info_hash(self, info_hash):
return hashlib.sha1(b'tribler anonymous download' + hexlify(info_hash).encode('utf-8')).digest()

@tc_lazy_wrapper_unsigned(HTTPRequestPayload)
@unpack_cell(HTTPRequestPayload)
async def on_http_request(self, source_address, payload, circuit_id):
if circuit_id not in self.exit_sockets:
self.logger.warning("Received unexpected http-request")
Expand Down Expand Up @@ -652,11 +634,11 @@ async def on_http_request(self, source_address, payload, circuit_id):

num_cells = math.ceil(len(response) / MAX_HTTP_PACKET_SIZE)
for i in range(num_cells):
self.send_cell(source_address, "http-response",
self.send_cell(source_address,
HTTPResponsePayload(circuit_id, payload.identifier, i, num_cells,
response[i*MAX_HTTP_PACKET_SIZE:(i+1)*MAX_HTTP_PACKET_SIZE]))

@tc_lazy_wrapper_unsigned(HTTPResponsePayload)
@unpack_cell(HTTPResponsePayload)
def on_http_response(self, source_address, payload, circuit_id):
if not self.request_cache.has("http-request", payload.identifier):
self.logger.warning("Received unexpected http-response")
Expand Down Expand Up @@ -689,11 +671,8 @@ async def perform_http_request(self, destination, request, hops=1):
raise RuntimeError('No HTTP exits available')

cache = self.request_cache.add(HTTPRequestCache(self, circuit.circuit_id))
self.increase_bytes_sent(circuit, self.send_cell(circuit.peer, "http-request",
HTTPRequestPayload(circuit.circuit_id,
cache.number,
encode_address(*destination),
request)))
self.send_cell(circuit.peer, HTTPRequestPayload(circuit.circuit_id, cache.number,
encode_address(*destination), request))
return await cache.response_future


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from asyncio import all_tasks, gather, sleep

from ipv8.keyvault.crypto import ECCrypto
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_ANY
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_BT
from ipv8.peer import Peer
from ipv8.peerdiscovery.community import DiscoveryCommunity
from ipv8.peerdiscovery.network import Network
Expand Down Expand Up @@ -114,7 +114,7 @@ async def load_tunnel_community_in_session(self, session, exitnode=False, start_
overlay = self.test_class(tunnel_peer, session.ipv8.endpoint, session.ipv8.network,
tribler_session=session, settings={"max_circuits": 1})
if exitnode:
overlay.settings.peer_flags.add(PEER_FLAG_EXIT_ANY)
overlay.settings.peer_flags.add(PEER_FLAG_EXIT_BT)
overlay._use_main_thread = False
overlay.dht_provider = MockDHTProvider(Peer(overlay.my_peer.key, overlay.my_estimated_wan))
overlay.settings.remove_tunnel_delay = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest.mock import Mock

from ipv8.keyvault.crypto import default_eccrypto
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_ANY
from ipv8.messaging.anonymization.tunnel import PEER_FLAG_EXIT_BT
from ipv8.peer import Peer
from ipv8.peerdiscovery.network import Network

Expand All @@ -16,7 +16,7 @@ def __init__(self):
self.network = Network()

def get_candidates(self, flag):
return self.exit_candidates if flag == PEER_FLAG_EXIT_ANY else []
return self.exit_candidates if flag == PEER_FLAG_EXIT_BT else []

def get_peers(self):
return self.network.verified_peers
Expand Down
Loading

0 comments on commit 6ddf9d4

Please sign in to comment.