From 880ae748d1d6b5b2c2e274df25493db24649fe15 Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 20:57:41 +0300 Subject: [PATCH 1/7] Delete requirements.txt file, add setup.py instead. --- requirements.txt | 9 --------- setup.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) delete mode 100644 requirements.txt create mode 100644 setup.py diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index f3accc7..0000000 --- a/requirements.txt +++ /dev/null @@ -1,9 +0,0 @@ -asyncio -pylint -pytest -pycryptodome -pytest-asyncio -click -base58 -pymultihash -multiaddr diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..db80f6e --- /dev/null +++ b/setup.py @@ -0,0 +1,31 @@ +import setuptools + + +classifiers = [ + ( + "Programming Language :: Python :: %s" % x + ) + for x in "3.5 3.6".split() +] + + +setuptools.setup( + name="libp2p", + description="libp2p implementation written in python", + version="0.0.1", + license="MIT", + platforms=["unix", "linux", "osx"], + classifiers=classifiers, + install_requires=[ + "pylint", + "pytest", + "pycryptodome", + "pytest-asyncio", + "click", + "base58", + "pymultihash", + ], + packages="libp2p", + zip_safe=False, +) + From ce6ddb27a9ddae1223a43410295dfa235aebedb1 Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 21:38:56 +0300 Subject: [PATCH 2/7] Migrate to new project structure. --- .travis.yml | 7 ++-- conftest.py | 0 libp2p/__init__.py | 38 +++++++++++++++++++ {encryption => libp2p/encryption}/__init__.py | 0 {encryption => libp2p/encryption}/secio.py | 0 {host => libp2p/host}/__init__.py | 0 {host => libp2p/host}/basic_host.py | 0 {host => libp2p/host}/host_interface.py | 0 {kademlia => libp2p/kademlia}/__init__.py | 0 {kademlia => libp2p/kademlia}/crawling.py | 8 ++-- {kademlia => libp2p/kademlia}/network.py | 14 +++---- {kademlia => libp2p/kademlia}/node.py | 2 +- {kademlia => libp2p/kademlia}/protocol.py | 6 +-- {kademlia => libp2p/kademlia}/routing.py | 9 +++-- {kademlia => libp2p/kademlia}/storage.py | 0 {kademlia => libp2p/kademlia}/utils.py | 0 libp2p/libp2p.py | 35 ----------------- {network => libp2p/network}/__init__.py | 0 .../network}/connection/__init__.py | 0 .../network}/connection/raw_connection.py | 0 .../connection/raw_connection_interface.py | 1 + {network => libp2p/network}/multiaddr.py | 0 .../network}/network_interface.py | 0 .../network}/stream/__init__.py | 0 .../network}/stream/net_stream.py | 0 .../network}/stream/net_stream_interface.py | 1 + {network => libp2p/network}/swarm.py | 6 ++- {peer => libp2p/peer}/README.md | 0 {peer => libp2p/peer}/__init__.py | 0 {peer => libp2p/peer}/addrbook_interface.py | 3 +- {peer => libp2p/peer}/id.py | 0 {peer => libp2p/peer}/peerdata.py | 2 + {peer => libp2p/peer}/peerdata_interface.py | 1 + {peer => libp2p/peer}/peerinfo.py | 7 ++-- .../peer}/peermetadata_interface.py | 2 +- {peer => libp2p/peer}/peerstore.py | 2 + {peer => libp2p/peer}/peerstore_interface.py | 2 + .../protocol_muxer}/__init__.py | 0 .../protocol_muxer}/multiselect.py | 3 ++ .../protocol_muxer}/multiselect_client.py | 6 +-- .../multiselect_client_interface.py | 1 + .../multiselect_communicator.py | 3 +- .../multiselect_communicator_interface.py | 1 + .../multiselect_muxer_interface.py | 1 + .../stream_muxer}/__init__.py | 0 .../stream_muxer}/mplex/__init__.py | 0 .../stream_muxer}/mplex/constants.py | 0 .../stream_muxer}/mplex/mplex.py | 1 + .../stream_muxer}/mplex/mplex_stream.py | 1 + .../stream_muxer}/mplex/utils.py | 3 ++ .../muxed_connection_interface.py | 0 .../stream_muxer}/muxed_stream_interface.py | 1 + .../stream_muxer}/yamux/__init__.py | 0 {transport => libp2p/transport}/__init__.py | 0 .../transport}/listener_interface.py | 0 {transport => libp2p/transport}/tcp/tcp.py | 7 ++-- .../transport}/transport_interface.py | 0 {transport => libp2p/transport}/upgrader.py | 4 +- setup.py | 10 ++--- tests/examples/test_chat.py | 13 ++----- tests/libp2p/test_libp2p.py | 4 +- tests/network/test_connection.py | 3 +- tests/peer/test_addrbook.py | 4 +- tests/peer/test_peerid.py | 3 +- tests/peer/test_peerinfo.py | 3 +- tests/peer/test_peermetadata.py | 6 ++- tests/peer/test_peerstore.py | 2 +- tests/protocol_muxer/test_protocol_muxer.py | 4 +- tests/transport/test_tcp.py | 2 +- 69 files changed, 132 insertions(+), 100 deletions(-) delete mode 100644 conftest.py rename {encryption => libp2p/encryption}/__init__.py (100%) rename {encryption => libp2p/encryption}/secio.py (100%) rename {host => libp2p/host}/__init__.py (100%) rename {host => libp2p/host}/basic_host.py (100%) rename {host => libp2p/host}/host_interface.py (100%) rename {kademlia => libp2p/kademlia}/__init__.py (100%) rename {kademlia => libp2p/kademlia}/crawling.py (97%) rename {kademlia => libp2p/kademlia}/network.py (96%) rename {kademlia => libp2p/kademlia}/node.py (99%) rename {kademlia => libp2p/kademlia}/protocol.py (97%) rename {kademlia => libp2p/kademlia}/routing.py (97%) rename {kademlia => libp2p/kademlia}/storage.py (100%) rename {kademlia => libp2p/kademlia}/utils.py (100%) delete mode 100644 libp2p/libp2p.py rename {network => libp2p/network}/__init__.py (100%) rename {network => libp2p/network}/connection/__init__.py (100%) rename {network => libp2p/network}/connection/raw_connection.py (100%) rename {network => libp2p/network}/connection/raw_connection_interface.py (99%) rename {network => libp2p/network}/multiaddr.py (100%) rename {network => libp2p/network}/network_interface.py (100%) rename {network => libp2p/network}/stream/__init__.py (100%) rename {network => libp2p/network}/stream/net_stream.py (100%) rename {network => libp2p/network}/stream/net_stream_interface.py (99%) rename {network => libp2p/network}/swarm.py (97%) rename {peer => libp2p/peer}/README.md (100%) rename {peer => libp2p/peer}/__init__.py (100%) rename {peer => libp2p/peer}/addrbook_interface.py (99%) rename {peer => libp2p/peer}/id.py (100%) rename {peer => libp2p/peer}/peerdata.py (99%) rename {peer => libp2p/peer}/peerdata_interface.py (99%) rename {peer => libp2p/peer}/peerinfo.py (89%) rename {peer => libp2p/peer}/peermetadata_interface.py (98%) rename {peer => libp2p/peer}/peerstore.py (99%) rename {peer => libp2p/peer}/peerstore_interface.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/__init__.py (100%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect_client.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect_client_interface.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect_communicator.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect_communicator_interface.py (99%) rename {protocol_muxer => libp2p/protocol_muxer}/multiselect_muxer_interface.py (99%) rename {stream_muxer => libp2p/stream_muxer}/__init__.py (100%) rename {stream_muxer => libp2p/stream_muxer}/mplex/__init__.py (100%) rename {stream_muxer => libp2p/stream_muxer}/mplex/constants.py (100%) rename {stream_muxer => libp2p/stream_muxer}/mplex/mplex.py (99%) rename {stream_muxer => libp2p/stream_muxer}/mplex/mplex_stream.py (99%) rename {stream_muxer => libp2p/stream_muxer}/mplex/utils.py (99%) rename {stream_muxer => libp2p/stream_muxer}/muxed_connection_interface.py (100%) rename {stream_muxer => libp2p/stream_muxer}/muxed_stream_interface.py (99%) rename {stream_muxer => libp2p/stream_muxer}/yamux/__init__.py (100%) rename {transport => libp2p/transport}/__init__.py (100%) rename {transport => libp2p/transport}/listener_interface.py (100%) rename {transport => libp2p/transport}/tcp/tcp.py (94%) rename {transport => libp2p/transport}/transport_interface.py (100%) rename {transport => libp2p/transport}/upgrader.py (88%) diff --git a/.travis.yml b/.travis.yml index 77ec45a..40e47f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,15 +4,16 @@ python: - "3.6" install: - - pip install -r requirements.txt - pip install codecov pytest pytest-cov + - python setup.py develop script: - - pytest --cov=./ -v - - pylint --rcfile=.pylintrc encryption host libp2p network peer stream_muxer transport tests + - py.test --cov=./libp2p tests/ + - pylint --rcfile=.pylintrc libp2p tests after_success: - codecov notifications: slack: py-libp2p:RK0WVoQZhQXLgIKfHNPL1TR2 + diff --git a/conftest.py b/conftest.py deleted file mode 100644 index e69de29..0000000 diff --git a/libp2p/__init__.py b/libp2p/__init__.py index e69de29..781ca88 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -0,0 +1,38 @@ +from Crypto.PublicKey import RSA + +import multiaddr + +from .peer.peerstore import PeerStore +from .peer.id import id_from_public_key +from .network.swarm import Swarm +from .host.basic_host import BasicHost +from .transport.upgrader import TransportUpgrader +from .transport.tcp.tcp import TCP + + +async def new_node( + id_opt=None, transport_opt=None, + muxer_opt=None, sec_opt=None, peerstore=None): + + if id_opt is None: + new_key = RSA.generate(2048, e=65537) + id_opt = id_from_public_key(new_key.publickey()) + # private_key = new_key.exportKey("PEM") + + transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"] + transport_opt = [multiaddr.Multiaddr(t) for t in transport_opt] + muxer_opt = muxer_opt or ["mplex/6.7.0"] + sec_opt = sec_opt or ["secio"] + peerstore = peerstore or PeerStore() + + upgrader = TransportUpgrader(sec_opt, transport_opt) + swarm = Swarm(id_opt, peerstore, upgrader) + tcp = TCP() + swarm.add_transport(tcp) + await swarm.listen(transport_opt[0]) + + # TODO enable support for other host type + # TODO routing unimplemented + host = BasicHost(swarm) + + return host diff --git a/encryption/__init__.py b/libp2p/encryption/__init__.py similarity index 100% rename from encryption/__init__.py rename to libp2p/encryption/__init__.py diff --git a/encryption/secio.py b/libp2p/encryption/secio.py similarity index 100% rename from encryption/secio.py rename to libp2p/encryption/secio.py diff --git a/host/__init__.py b/libp2p/host/__init__.py similarity index 100% rename from host/__init__.py rename to libp2p/host/__init__.py diff --git a/host/basic_host.py b/libp2p/host/basic_host.py similarity index 100% rename from host/basic_host.py rename to libp2p/host/basic_host.py diff --git a/host/host_interface.py b/libp2p/host/host_interface.py similarity index 100% rename from host/host_interface.py rename to libp2p/host/host_interface.py diff --git a/kademlia/__init__.py b/libp2p/kademlia/__init__.py similarity index 100% rename from kademlia/__init__.py rename to libp2p/kademlia/__init__.py diff --git a/kademlia/crawling.py b/libp2p/kademlia/crawling.py similarity index 97% rename from kademlia/crawling.py rename to libp2p/kademlia/crawling.py index b1460c2..fc5d918 100644 --- a/kademlia/crawling.py +++ b/libp2p/kademlia/crawling.py @@ -1,13 +1,13 @@ from collections import Counter import logging -from kademlia.node import Node, NodeHeap -from kademlia.utils import gather_dict +from .kademlia.node import Node, NodeHeap +from .kademlia.utils import gather_dict log = logging.getLogger(__name__) -class SpiderCrawl(object): +class SpiderCrawl: """ Crawl the network and look for given 160-bit keys. """ @@ -148,7 +148,7 @@ class NodeSpiderCrawl(SpiderCrawl): return await self.find() -class RPCFindResponse(object): +class RPCFindResponse: def __init__(self, response): """ A wrapper for the result of a RPC find. diff --git a/kademlia/network.py b/libp2p/kademlia/network.py similarity index 96% rename from kademlia/network.py rename to libp2p/kademlia/network.py index ca87d3e..fb433db 100644 --- a/kademlia/network.py +++ b/libp2p/kademlia/network.py @@ -6,17 +6,17 @@ import pickle import asyncio import logging -from kademlia.protocol import KademliaProtocol -from kademlia.utils import digest -from kademlia.storage import ForgetfulStorage -from kademlia.node import Node -from kademlia.crawling import ValueSpiderCrawl -from kademlia.crawling import NodeSpiderCrawl +from .kademlia.protocol import KademliaProtocol +from .kademlia.utils import digest +from .kademlia.storage import ForgetfulStorage +from .kademlia.node import Node +from .kademlia.crawling import ValueSpiderCrawl +from .kademlia.crawling import NodeSpiderCrawl log = logging.getLogger(__name__) -class Server(object): +class Server: """ High level view of a node instance. This is the object that should be created to start listening as an active node on the network. diff --git a/kademlia/node.py b/libp2p/kademlia/node.py similarity index 99% rename from kademlia/node.py rename to libp2p/kademlia/node.py index b25e9f8..f130dcc 100644 --- a/kademlia/node.py +++ b/libp2p/kademlia/node.py @@ -31,7 +31,7 @@ class Node: return "%s:%s" % (self.ip, str(self.port)) -class NodeHeap(object): +class NodeHeap: """ A heap of nodes ordered by distance to a given node. """ diff --git a/kademlia/protocol.py b/libp2p/kademlia/protocol.py similarity index 97% rename from kademlia/protocol.py rename to libp2p/kademlia/protocol.py index 6e22f8b..2bcf276 100644 --- a/kademlia/protocol.py +++ b/libp2p/kademlia/protocol.py @@ -4,9 +4,9 @@ import logging from rpcudp.protocol import RPCProtocol -from kademlia.node import Node -from kademlia.routing import RoutingTable -from kademlia.utils import digest +from .kademlia.node import Node +from .kademlia.routing import RoutingTable +from .kademlia.utils import digest log = logging.getLogger(__name__) diff --git a/kademlia/routing.py b/libp2p/kademlia/routing.py similarity index 97% rename from kademlia/routing.py rename to libp2p/kademlia/routing.py index 85f3dda..67aad00 100644 --- a/kademlia/routing.py +++ b/libp2p/kademlia/routing.py @@ -4,10 +4,11 @@ import operator import asyncio from collections import OrderedDict -from kademlia.utils import OrderedSet, sharedPrefix, bytesToBitString + +from .kademlia.utils import OrderedSet, sharedPrefix, bytesToBitString -class KBucket(object): +class KBucket: def __init__(self, rangeLower, rangeUpper, ksize): self.range = (rangeLower, rangeUpper) self.nodes = OrderedDict() @@ -79,7 +80,7 @@ class KBucket(object): return len(self.nodes) -class TableTraverser(object): +class TableTraverser: def __init__(self, table, startNode): index = table.getBucketFor(startNode) table.buckets[index].touchLastUpdated() @@ -111,7 +112,7 @@ class TableTraverser(object): raise StopIteration -class RoutingTable(object): +class RoutingTable: def __init__(self, protocol, ksize, node): """ @param node: The node that represents this server. It won't diff --git a/kademlia/storage.py b/libp2p/kademlia/storage.py similarity index 100% rename from kademlia/storage.py rename to libp2p/kademlia/storage.py diff --git a/kademlia/utils.py b/libp2p/kademlia/utils.py similarity index 100% rename from kademlia/utils.py rename to libp2p/kademlia/utils.py diff --git a/libp2p/libp2p.py b/libp2p/libp2p.py deleted file mode 100644 index 8682ca7..0000000 --- a/libp2p/libp2p.py +++ /dev/null @@ -1,35 +0,0 @@ -from Crypto.PublicKey import RSA -import multiaddr -from peer.peerstore import PeerStore -from peer.id import id_from_public_key -from network.swarm import Swarm -from host.basic_host import BasicHost -from transport.upgrader import TransportUpgrader -from transport.tcp.tcp import TCP - - -async def new_node(id_opt=None, transport_opt=None, \ - muxer_opt=None, sec_opt=None, peerstore=None): - - if id_opt is None: - new_key = RSA.generate(2048, e=65537) - id_opt = id_from_public_key(new_key.publickey()) - # private_key = new_key.exportKey("PEM") - - transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"] - transport_opt = [multiaddr.Multiaddr(t) for t in transport_opt] - muxer_opt = muxer_opt or ["mplex/6.7.0"] - sec_opt = sec_opt or ["secio"] - peerstore = peerstore or PeerStore() - - upgrader = TransportUpgrader(sec_opt, transport_opt) - swarm = Swarm(id_opt, peerstore, upgrader) - tcp = TCP() - swarm.add_transport(tcp) - await swarm.listen(transport_opt[0]) - - # TODO enable support for other host type - # TODO routing unimplemented - host = BasicHost(swarm) - - return host diff --git a/network/__init__.py b/libp2p/network/__init__.py similarity index 100% rename from network/__init__.py rename to libp2p/network/__init__.py diff --git a/network/connection/__init__.py b/libp2p/network/connection/__init__.py similarity index 100% rename from network/connection/__init__.py rename to libp2p/network/connection/__init__.py diff --git a/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py similarity index 100% rename from network/connection/raw_connection.py rename to libp2p/network/connection/raw_connection.py diff --git a/network/connection/raw_connection_interface.py b/libp2p/network/connection/raw_connection_interface.py similarity index 99% rename from network/connection/raw_connection_interface.py rename to libp2p/network/connection/raw_connection_interface.py index 9f17ffe..2b20607 100644 --- a/network/connection/raw_connection_interface.py +++ b/libp2p/network/connection/raw_connection_interface.py @@ -2,6 +2,7 @@ from abc import ABC # pylint: disable=too-few-public-methods + class IRawConnection(ABC): """ A Raw Connection provides a Reader and a Writer diff --git a/network/multiaddr.py b/libp2p/network/multiaddr.py similarity index 100% rename from network/multiaddr.py rename to libp2p/network/multiaddr.py diff --git a/network/network_interface.py b/libp2p/network/network_interface.py similarity index 100% rename from network/network_interface.py rename to libp2p/network/network_interface.py diff --git a/network/stream/__init__.py b/libp2p/network/stream/__init__.py similarity index 100% rename from network/stream/__init__.py rename to libp2p/network/stream/__init__.py diff --git a/network/stream/net_stream.py b/libp2p/network/stream/net_stream.py similarity index 100% rename from network/stream/net_stream.py rename to libp2p/network/stream/net_stream.py diff --git a/network/stream/net_stream_interface.py b/libp2p/network/stream/net_stream_interface.py similarity index 99% rename from network/stream/net_stream_interface.py rename to libp2p/network/stream/net_stream_interface.py index 1263252..056f924 100644 --- a/network/stream/net_stream_interface.py +++ b/libp2p/network/stream/net_stream_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class INetStream(ABC): @abstractmethod diff --git a/network/swarm.py b/libp2p/network/swarm.py similarity index 97% rename from network/swarm.py rename to libp2p/network/swarm.py index 7a73209..cc7531b 100644 --- a/network/swarm.py +++ b/libp2p/network/swarm.py @@ -1,5 +1,7 @@ -from protocol_muxer.multiselect_client import MultiselectClient -from protocol_muxer.multiselect import Multiselect +from libp2p.protocol_muxer.multiselect_client import MultiselectClient +from libp2p.protocol_muxer.multiselect import Multiselect + + from .network_interface import INetwork from .stream.net_stream import NetStream from .connection.raw_connection import RawConnection diff --git a/peer/README.md b/libp2p/peer/README.md similarity index 100% rename from peer/README.md rename to libp2p/peer/README.md diff --git a/peer/__init__.py b/libp2p/peer/__init__.py similarity index 100% rename from peer/__init__.py rename to libp2p/peer/__init__.py diff --git a/peer/addrbook_interface.py b/libp2p/peer/addrbook_interface.py similarity index 99% rename from peer/addrbook_interface.py rename to libp2p/peer/addrbook_interface.py index 82a1668..13a97e8 100644 --- a/peer/addrbook_interface.py +++ b/libp2p/peer/addrbook_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IAddrBook(ABC): def __init__(self): @@ -44,4 +45,4 @@ class IAddrBook(ABC): """ :return: all of the peer IDs stored with addresses """ - \ No newline at end of file + diff --git a/peer/id.py b/libp2p/peer/id.py similarity index 100% rename from peer/id.py rename to libp2p/peer/id.py diff --git a/peer/peerdata.py b/libp2p/peer/peerdata.py similarity index 99% rename from peer/peerdata.py rename to libp2p/peer/peerdata.py index 5e008a0..c505bad 100644 --- a/peer/peerdata.py +++ b/libp2p/peer/peerdata.py @@ -1,5 +1,6 @@ from .peerdata_interface import IPeerData + class PeerData(IPeerData): def __init__(self): @@ -33,5 +34,6 @@ class PeerData(IPeerData): return self.metadata[key] raise PeerDataError("key not found") + class PeerDataError(KeyError): """Raised when a key is not found in peer metadata""" diff --git a/peer/peerdata_interface.py b/libp2p/peer/peerdata_interface.py similarity index 99% rename from peer/peerdata_interface.py rename to libp2p/peer/peerdata_interface.py index 907990c..cf4a292 100644 --- a/peer/peerdata_interface.py +++ b/libp2p/peer/peerdata_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IPeerData(ABC): @abstractmethod diff --git a/peer/peerinfo.py b/libp2p/peer/peerinfo.py similarity index 89% rename from peer/peerinfo.py rename to libp2p/peer/peerinfo.py index 42a5c2d..44cba56 100644 --- a/peer/peerinfo.py +++ b/libp2p/peer/peerinfo.py @@ -1,11 +1,12 @@ import multiaddr import multiaddr.util -from peer.id import id_b58_decode -from peer.peerdata import PeerData + +from .id import id_b58_decode +from .peerdata import PeerData class PeerInfo: - # pylint: disable=too-few-public-methods + # pylint: disable=too-few-public-methods def __init__(self, peer_id, peer_data): self.peer_id = peer_id self.addrs = peer_data.get_addrs() diff --git a/peer/peermetadata_interface.py b/libp2p/peer/peermetadata_interface.py similarity index 98% rename from peer/peermetadata_interface.py rename to libp2p/peer/peermetadata_interface.py index a8d7d9a..1badabd 100644 --- a/peer/peermetadata_interface.py +++ b/libp2p/peer/peermetadata_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IPeerMetadata(ABC): def __init__(self): @@ -22,4 +23,3 @@ class IPeerMetadata(ABC): :param val: value to associated with key :raise Exception: unsuccessful put """ - \ No newline at end of file diff --git a/peer/peerstore.py b/libp2p/peer/peerstore.py similarity index 99% rename from peer/peerstore.py rename to libp2p/peer/peerstore.py index ff055df..5a6a77b 100644 --- a/peer/peerstore.py +++ b/libp2p/peer/peerstore.py @@ -2,6 +2,7 @@ from .peerstore_interface import IPeerStore from .peerdata import PeerData from .peerinfo import PeerInfo + class PeerStore(IPeerStore): def __init__(self): @@ -83,5 +84,6 @@ class PeerStore(IPeerStore): output.append(key) return output + class PeerStoreError(KeyError): """Raised when peer ID is not found in peer store""" diff --git a/peer/peerstore_interface.py b/libp2p/peer/peerstore_interface.py similarity index 99% rename from peer/peerstore_interface.py rename to libp2p/peer/peerstore_interface.py index aad08c8..b368d8f 100644 --- a/peer/peerstore_interface.py +++ b/libp2p/peer/peerstore_interface.py @@ -1,7 +1,9 @@ from abc import abstractmethod + from .addrbook_interface import IAddrBook from .peermetadata_interface import IPeerMetadata + class IPeerStore(IAddrBook, IPeerMetadata): def __init__(self): diff --git a/protocol_muxer/__init__.py b/libp2p/protocol_muxer/__init__.py similarity index 100% rename from protocol_muxer/__init__.py rename to libp2p/protocol_muxer/__init__.py diff --git a/protocol_muxer/multiselect.py b/libp2p/protocol_muxer/multiselect.py similarity index 99% rename from protocol_muxer/multiselect.py rename to libp2p/protocol_muxer/multiselect.py index b2f4532..61b8fbe 100644 --- a/protocol_muxer/multiselect.py +++ b/libp2p/protocol_muxer/multiselect.py @@ -1,9 +1,11 @@ from .multiselect_muxer_interface import IMultiselectMuxer from .multiselect_communicator import MultiselectCommunicator + MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0" PROTOCOL_NOT_FOUND_MSG = "na" + class Multiselect(IMultiselectMuxer): """ Multiselect module that is responsible for responding to @@ -88,5 +90,6 @@ class Multiselect(IMultiselectMuxer): # is added return handshake_contents == MULTISELECT_PROTOCOL_ID + class MultiselectError(ValueError): """Raised when an error occurs in multiselect process""" diff --git a/protocol_muxer/multiselect_client.py b/libp2p/protocol_muxer/multiselect_client.py similarity index 99% rename from protocol_muxer/multiselect_client.py rename to libp2p/protocol_muxer/multiselect_client.py index c455877..a10063c 100644 --- a/protocol_muxer/multiselect_client.py +++ b/libp2p/protocol_muxer/multiselect_client.py @@ -1,17 +1,16 @@ from .multiselect_client_interface import IMultiselectClient from .multiselect_communicator import MultiselectCommunicator + MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0" PROTOCOL_NOT_FOUND_MSG = "na" + class MultiselectClient(IMultiselectClient): """ Client for communicating with receiver's multiselect module in order to select a protocol id to communicate over """ - - def __init__(self): - pass async def handshake(self, communicator): """ @@ -117,5 +116,6 @@ class MultiselectClient(IMultiselectClient): else: raise MultiselectClientError("unrecognized response: " + response) + class MultiselectClientError(ValueError): """Raised when an error occurs in protocol selection process""" diff --git a/protocol_muxer/multiselect_client_interface.py b/libp2p/protocol_muxer/multiselect_client_interface.py similarity index 99% rename from protocol_muxer/multiselect_client_interface.py rename to libp2p/protocol_muxer/multiselect_client_interface.py index 1c8d066..81d3c0f 100644 --- a/protocol_muxer/multiselect_client_interface.py +++ b/libp2p/protocol_muxer/multiselect_client_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IMultiselectClient(ABC): """ Client for communicating with receiver's multiselect diff --git a/protocol_muxer/multiselect_communicator.py b/libp2p/protocol_muxer/multiselect_communicator.py similarity index 99% rename from protocol_muxer/multiselect_communicator.py rename to libp2p/protocol_muxer/multiselect_communicator.py index bfdeabf..d7e0dd5 100644 --- a/protocol_muxer/multiselect_communicator.py +++ b/libp2p/protocol_muxer/multiselect_communicator.py @@ -1,12 +1,13 @@ from .multiselect_communicator_interface import IMultiselectCommunicator + class MultiselectCommunicator(IMultiselectCommunicator): """ Communicator helper class that ensures both the client and multistream module will follow the same multistream protocol, which is necessary for them to work """ - + def __init__(self, stream): self.stream = stream diff --git a/protocol_muxer/multiselect_communicator_interface.py b/libp2p/protocol_muxer/multiselect_communicator_interface.py similarity index 99% rename from protocol_muxer/multiselect_communicator_interface.py rename to libp2p/protocol_muxer/multiselect_communicator_interface.py index a9990fd..db6d27c 100644 --- a/protocol_muxer/multiselect_communicator_interface.py +++ b/libp2p/protocol_muxer/multiselect_communicator_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IMultiselectCommunicator(ABC): """ Communicator helper class that ensures both the client diff --git a/protocol_muxer/multiselect_muxer_interface.py b/libp2p/protocol_muxer/multiselect_muxer_interface.py similarity index 99% rename from protocol_muxer/multiselect_muxer_interface.py rename to libp2p/protocol_muxer/multiselect_muxer_interface.py index 0f08fec..39e5302 100644 --- a/protocol_muxer/multiselect_muxer_interface.py +++ b/libp2p/protocol_muxer/multiselect_muxer_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IMultiselectMuxer(ABC): """ Multiselect module that is responsible for responding to diff --git a/stream_muxer/__init__.py b/libp2p/stream_muxer/__init__.py similarity index 100% rename from stream_muxer/__init__.py rename to libp2p/stream_muxer/__init__.py diff --git a/stream_muxer/mplex/__init__.py b/libp2p/stream_muxer/mplex/__init__.py similarity index 100% rename from stream_muxer/mplex/__init__.py rename to libp2p/stream_muxer/mplex/__init__.py diff --git a/stream_muxer/mplex/constants.py b/libp2p/stream_muxer/mplex/constants.py similarity index 100% rename from stream_muxer/mplex/constants.py rename to libp2p/stream_muxer/mplex/constants.py diff --git a/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py similarity index 99% rename from stream_muxer/mplex/mplex.py rename to libp2p/stream_muxer/mplex/mplex.py index bd324f5..da806ce 100644 --- a/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -1,4 +1,5 @@ import asyncio + from .utils import encode_uvarint, decode_uvarint_from_stream from .mplex_stream import MplexStream from ..muxed_connection_interface import IMuxedConn diff --git a/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py similarity index 99% rename from stream_muxer/mplex/mplex_stream.py rename to libp2p/stream_muxer/mplex/mplex_stream.py index 1ec77b2..33883d5 100644 --- a/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -1,4 +1,5 @@ import asyncio + from .constants import HEADER_TAGS from ..muxed_stream_interface import IMuxedStream diff --git a/stream_muxer/mplex/utils.py b/libp2p/stream_muxer/mplex/utils.py similarity index 99% rename from stream_muxer/mplex/utils.py rename to libp2p/stream_muxer/mplex/utils.py index 94ffe84..2b3cddd 100644 --- a/stream_muxer/mplex/utils.py +++ b/libp2p/stream_muxer/mplex/utils.py @@ -1,6 +1,7 @@ import asyncio import struct + def encode_uvarint(number): """Pack `number` into varint bytes""" buf = b'' @@ -14,6 +15,7 @@ def encode_uvarint(number): break return buf + def decode_uvarint(buff, index): shift = 0 result = 0 @@ -27,6 +29,7 @@ def decode_uvarint(buff, index): return result, index + 1 + async def decode_uvarint_from_stream(reader): shift = 0 result = 0 diff --git a/stream_muxer/muxed_connection_interface.py b/libp2p/stream_muxer/muxed_connection_interface.py similarity index 100% rename from stream_muxer/muxed_connection_interface.py rename to libp2p/stream_muxer/muxed_connection_interface.py diff --git a/stream_muxer/muxed_stream_interface.py b/libp2p/stream_muxer/muxed_stream_interface.py similarity index 99% rename from stream_muxer/muxed_stream_interface.py rename to libp2p/stream_muxer/muxed_stream_interface.py index a2d85b5..b15dba1 100644 --- a/stream_muxer/muxed_stream_interface.py +++ b/libp2p/stream_muxer/muxed_stream_interface.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + class IMuxedStream(ABC): @abstractmethod diff --git a/stream_muxer/yamux/__init__.py b/libp2p/stream_muxer/yamux/__init__.py similarity index 100% rename from stream_muxer/yamux/__init__.py rename to libp2p/stream_muxer/yamux/__init__.py diff --git a/transport/__init__.py b/libp2p/transport/__init__.py similarity index 100% rename from transport/__init__.py rename to libp2p/transport/__init__.py diff --git a/transport/listener_interface.py b/libp2p/transport/listener_interface.py similarity index 100% rename from transport/listener_interface.py rename to libp2p/transport/listener_interface.py diff --git a/transport/tcp/tcp.py b/libp2p/transport/tcp/tcp.py similarity index 94% rename from transport/tcp/tcp.py rename to libp2p/transport/tcp/tcp.py index 92d098c..f066971 100644 --- a/transport/tcp/tcp.py +++ b/libp2p/transport/tcp/tcp.py @@ -2,9 +2,10 @@ import asyncio import multiaddr -from network.connection.raw_connection import RawConnection -from transport.listener_interface import IListener -from transport.transport_interface import ITransport +from libp2p.network.connection.raw_connection import RawConnection + +from ..listener_interface import IListener +from ..transport_interface import ITransport class TCP(ITransport): diff --git a/transport/transport_interface.py b/libp2p/transport/transport_interface.py similarity index 100% rename from transport/transport_interface.py rename to libp2p/transport/transport_interface.py diff --git a/transport/upgrader.py b/libp2p/transport/upgrader.py similarity index 88% rename from transport/upgrader.py rename to libp2p/transport/upgrader.py index 995e670..d8074b9 100644 --- a/transport/upgrader.py +++ b/libp2p/transport/upgrader.py @@ -1,7 +1,7 @@ -from stream_muxer.mplex.mplex import Mplex +from libp2p.stream_muxer.mplex.mplex import Mplex -class TransportUpgrader(): +class TransportUpgrader: # pylint: disable=no-self-use def __init__(self, secOpt, muxerOpt): diff --git a/setup.py b/setup.py index db80f6e..557ea86 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,9 @@ import setuptools classifiers = [ ( - "Programming Language :: Python :: %s" % x + "Programming Language :: Python :: %s" % version ) - for x in "3.5 3.6".split() + for version in "3.5 3.6".split() ] @@ -13,7 +13,7 @@ setuptools.setup( name="libp2p", description="libp2p implementation written in python", version="0.0.1", - license="MIT", + license="MIT/APACHE2.0", platforms=["unix", "linux", "osx"], classifiers=classifiers, install_requires=[ @@ -24,8 +24,8 @@ setuptools.setup( "click", "base58", "pymultihash", + "multiaddr", ], - packages="libp2p", + packages=["libp2p"], zip_safe=False, ) - diff --git a/tests/examples/test_chat.py b/tests/examples/test_chat.py index c25a451..9920d19 100644 --- a/tests/examples/test_chat.py +++ b/tests/examples/test_chat.py @@ -1,14 +1,9 @@ -#!/bin/env python3 -import asyncio -import sys - -import click -import multiaddr import pytest -from libp2p.libp2p import * -from peer.peerinfo import info_from_p2p_addr -from protocol_muxer.multiselect_client import MultiselectClientError +from libp2p import new_node +from libp2p.peer.peerinfo import info_from_p2p_addr +from libp2p.protocol_muxer.multiselect_client import MultiselectClientError + PROTOCOL_ID = '/chat/1.0.0' diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py index 20dc7f5..9d4e0a9 100644 --- a/tests/libp2p/test_libp2p.py +++ b/tests/libp2p/test_libp2p.py @@ -1,8 +1,8 @@ import multiaddr import pytest -from libp2p.libp2p import new_node -from peer.peerinfo import info_from_p2p_addr +from libp2p import new_node +from libp2p.peer.peerinfo import info_from_p2p_addr @pytest.mark.asyncio diff --git a/tests/network/test_connection.py b/tests/network/test_connection.py index 15ebb96..9b7602d 100644 --- a/tests/network/test_connection.py +++ b/tests/network/test_connection.py @@ -1,13 +1,14 @@ import asyncio import pytest + async def handle_echo(reader, writer): data = await reader.read(100) writer.write(data) await writer.drain() - writer.close() + @pytest.mark.asyncio # TODO: this test should develop out into a fuller test between MPlex # modules communicating with each other. diff --git a/tests/peer/test_addrbook.py b/tests/peer/test_addrbook.py index c67b74f..d88de34 100644 --- a/tests/peer/test_addrbook.py +++ b/tests/peer/test_addrbook.py @@ -1,7 +1,7 @@ import pytest -from peer.peerstore import PeerStoreError -from peer.peerstore import PeerStore +from libp2p.peer.peerstore import PeerStoreError +from libp2p.peer.peerstore import PeerStore # Testing methods from IAddrBook base class. diff --git a/tests/peer/test_peerid.py b/tests/peer/test_peerid.py index b2ecb19..7e702c0 100644 --- a/tests/peer/test_peerid.py +++ b/tests/peer/test_peerid.py @@ -1,5 +1,6 @@ from Crypto.PublicKey import RSA -from peer.id import id_from_private_key, id_from_public_key + +from libp2p.peer.id import id_from_private_key, id_from_public_key def test_id_from_private_key(): diff --git a/tests/peer/test_peerinfo.py b/tests/peer/test_peerinfo.py index cbed83d..d0d12a2 100644 --- a/tests/peer/test_peerinfo.py +++ b/tests/peer/test_peerinfo.py @@ -1,5 +1,6 @@ import multiaddr -from peer.peerinfo import info_from_p2p_addr + +from libp2p.peer.peerinfo import info_from_p2p_addr def test_info_from_p2p_addr(): diff --git a/tests/peer/test_peermetadata.py b/tests/peer/test_peermetadata.py index 3dd0544..2d5e79e 100644 --- a/tests/peer/test_peermetadata.py +++ b/tests/peer/test_peermetadata.py @@ -1,10 +1,12 @@ import pytest -from peer.peerstore import PeerStoreError -from peer.peerstore import PeerStore + +from libp2p.peer.peerstore import PeerStoreError +from libp2p.peer.peerstore import PeerStore # Testing methods from IPeerMetadata base class. + def test_get_empty(): with pytest.raises(PeerStoreError): store = PeerStore() diff --git a/tests/peer/test_peerstore.py b/tests/peer/test_peerstore.py index d76d39f..52029ba 100644 --- a/tests/peer/test_peerstore.py +++ b/tests/peer/test_peerstore.py @@ -1,4 +1,4 @@ -from peer.peerstore import PeerStore +from libp2p.peer.peerstore import PeerStore # Testing methods from IPeerStore base class. diff --git a/tests/protocol_muxer/test_protocol_muxer.py b/tests/protocol_muxer/test_protocol_muxer.py index 7ac4621..9eba3a4 100644 --- a/tests/protocol_muxer/test_protocol_muxer.py +++ b/tests/protocol_muxer/test_protocol_muxer.py @@ -1,7 +1,7 @@ import pytest -from libp2p.libp2p import new_node -from protocol_muxer.multiselect_client import MultiselectClientError +from libp2p import new_node +from libp2p.protocol_muxer.multiselect_client import MultiselectClientError # TODO: Add tests for multiple streams being opened on different # protocols through the same connection diff --git a/tests/transport/test_tcp.py b/tests/transport/test_tcp.py index 34b5b90..3822926 100644 --- a/tests/transport/test_tcp.py +++ b/tests/transport/test_tcp.py @@ -2,7 +2,7 @@ import asyncio import pytest -from transport.tcp.tcp import _multiaddr_from_socket +from libp2p.transport.tcp.tcp import _multiaddr_from_socket @pytest.mark.asyncio From d629a0c85ef64036305c52f0fbd56d38f001e9d2 Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 22:01:46 +0300 Subject: [PATCH 3/7] Fix examples/chat/chat.py. Extract test requirements to .travis.yaml file. --- .travis.yml | 4 ++-- examples/chat/chat.py | 9 ++------- setup.py | 3 --- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 40e47f4..56d4b96 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,11 @@ python: - "3.6" install: - - pip install codecov pytest pytest-cov + - pip install codecov pytest pytest-cov pytest-asyncio pylint - python setup.py develop script: - - py.test --cov=./libp2p tests/ + - pytest --cov=./libp2p tests/ - pylint --rcfile=.pylintrc libp2p tests after_success: diff --git a/examples/chat/chat.py b/examples/chat/chat.py index a820fce..83ac937 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -1,15 +1,10 @@ -#!/usr/bin/env python3 import asyncio import sys -from os.path import abspath, dirname import click -from libp2p.libp2p import * -from network.multiaddr import MultiAddr -from peer.peerinfo import info_from_p2p_addr - -sys.path.append(dirname(dirname(dirname(abspath(__file__))))) +from libp2p import new_node +from libp2p.peer.peerinfo import info_from_p2p_addr PROTOCOL_ID = '/chat/1.0.0' diff --git a/setup.py b/setup.py index 557ea86..420516d 100644 --- a/setup.py +++ b/setup.py @@ -17,10 +17,7 @@ setuptools.setup( platforms=["unix", "linux", "osx"], classifiers=classifiers, install_requires=[ - "pylint", - "pytest", "pycryptodome", - "pytest-asyncio", "click", "base58", "pymultihash", From 3ab741feb40d922252be35fb5ab8c0f7fdafc70e Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 22:13:44 +0300 Subject: [PATCH 4/7] Upgrade pip, using pytest>=3.6. --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 56d4b96..0690d6a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,9 @@ python: - "3.6" install: - - pip install codecov pytest pytest-cov pytest-asyncio pylint + - pip install --upgrade pip + - pip install "pytest>=3.6" + - pip install codecov pytest-cov pytest-asyncio pylint - python setup.py develop script: From 113021eacd50e203cd8686b03de53cc640e9de3f Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 22:30:18 +0300 Subject: [PATCH 5/7] Delete trailing whitespace issue, ignore previously untested folders for now. --- .travis.yml | 2 +- libp2p/peer/addrbook_interface.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0690d6a..d90f7aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: script: - pytest --cov=./libp2p tests/ - - pylint --rcfile=.pylintrc libp2p tests + - pylint --rcfile=.pylintrc libp2p tests --ignore libp2p/{kademlia,protocol_muxer} after_success: - codecov diff --git a/libp2p/peer/addrbook_interface.py b/libp2p/peer/addrbook_interface.py index 13a97e8..914d937 100644 --- a/libp2p/peer/addrbook_interface.py +++ b/libp2p/peer/addrbook_interface.py @@ -45,4 +45,3 @@ class IAddrBook(ABC): """ :return: all of the peer IDs stored with addresses """ - From 58743bb3734b7f8a3255fa7c5f4af08a49851168 Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 22:34:35 +0300 Subject: [PATCH 6/7] Unpack bash curly stuff. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d90f7aa..6d029ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: script: - pytest --cov=./libp2p tests/ - - pylint --rcfile=.pylintrc libp2p tests --ignore libp2p/{kademlia,protocol_muxer} + - pylint --rcfile=.pylintrc libp2p tests --ignore libp2p/kademlia --ignore libp2p/protocol_muxer after_success: - codecov From 597f94ddb2b9aca968ed5421cbdfb6c15c7e2704 Mon Sep 17 00:00:00 2001 From: Alexander Koshkin Date: Wed, 9 Jan 2019 23:10:36 +0300 Subject: [PATCH 7/7] Pylint --ignore-patterns seems to be broken. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6d029ca..3dc7a73 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: script: - pytest --cov=./libp2p tests/ - - pylint --rcfile=.pylintrc libp2p tests --ignore libp2p/kademlia --ignore libp2p/protocol_muxer + - pylint --rcfile=.pylintrc libp2p/!(kademlia|protocol_muxer) tests after_success: - codecov