From 5fd7cbe0eb2a181832edc8df85e8860c62844462 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Sun, 21 Oct 2018 10:58:55 -0400 Subject: [PATCH 1/8] started on basic host --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e69de29b..54b18e24 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +pylint \ No newline at end of file From 15e6b565162a8dd45c7b8a9a166dda97416ec652 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 11:18:35 -0400 Subject: [PATCH 2/8] Minor add to gitignore: pycharm --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 894a44cc..ee759a47 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +# pycharm +.idea/ From d625af0001427f39b9152aa8e99003de870307e5 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 11:19:43 -0400 Subject: [PATCH 3/8] Adding pylint to our dependencies --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index e69de29b..cba07d79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,2 @@ +pylint + From 9e64202e5c41ce4648c2576a9ccc1cdbe979f7f0 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Sun, 21 Oct 2018 12:55:45 -0400 Subject: [PATCH 4/8] basic-host and remove context --- host/basic_host.py | 22 ++++++++++++---------- host/host_interface.py | 7 +------ network/network_interface.py | 6 ++---- network/stream.py | 3 +-- network/stream_interface.py | 3 +-- network/swarm.py | 6 ++---- requirements.txt | 1 - 7 files changed, 19 insertions(+), 29 deletions(-) diff --git a/host/basic_host.py b/host/basic_host.py index 5ea942a2..133571cc 100644 --- a/host/basic_host.py +++ b/host/basic_host.py @@ -8,23 +8,23 @@ from .host_interface import IHost class BasicHost(IHost): # default options constructor - def __init__(self, context, network): - self.context = context - self.network = network + def __init__(self, _network): + self.network = _network + # self.stream_handlers = {} def get_id(self): """ :return: peer_id of host """ - pass + return self.network.get_peer_id() def get_network(self): """ :return: network instance of host """ - pass + return self.network - def mux(self): + def get_mux(self): """ :return: mux instance of host """ @@ -37,15 +37,17 @@ class BasicHost(IHost): :param stream_handler: a stream handler function :return: true if successful """ - pass + return self.network.set_stream_handler(protocol_id, stream_handler) + # protocol_id can be a list of protocol_ids # stream will decide which protocol_id to run on - def new_stream(self, context, peer_id, protocol_id): + def new_stream(self, peer_id, protocol_id): """ - :param context: a context instance :param peer_id: peer_id that host is connecting :param proto_id: protocol id that stream runs on :return: true if successful """ - pass + stream = self.network.new_stream(peer_id) + stream.set_protocol(protocol_id) + return stream diff --git a/host/host_interface.py b/host/host_interface.py index 13ff83ba..eaca0f8b 100644 --- a/host/host_interface.py +++ b/host/host_interface.py @@ -2,11 +2,6 @@ from abc import ABC, abstractmethod class IHost(ABC): - # default options constructor - def __init__(self, context, network): - self.context = context - self.network = network - @abstractmethod def get_id(self): """ @@ -22,7 +17,7 @@ class IHost(ABC): pass @abstractmethod - def mux(self): + def get_mux(self): """ :return: mux instance of host """ diff --git a/network/network_interface.py b/network/network_interface.py index 46c2d570..f04c3a3a 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -2,8 +2,7 @@ from abc import ABC, abstractmethod class INetwork(ABC): - def __init__(self, context, my_peer_id, peer_store): - self.context = context + def __init__(self, my_peer_id, peer_store): self.my_peer_id = my_peer_id self.peer_store = peer_store @@ -16,9 +15,8 @@ class INetwork(ABC): pass @abstractmethod - def new_stream(self, context, peer_id): + def new_stream(self, peer_id): """ - :param context: context instance :param peer_id: peer_id of destination :return: stream instance """ diff --git a/network/stream.py b/network/stream.py index 42fc1f39..634e25fb 100644 --- a/network/stream.py +++ b/network/stream.py @@ -2,8 +2,7 @@ from .stream_interface import IStream class Stream(IStream): - def __init__(self, context, peer_id): - self.context = context + def __init__(self, peer_id): self.peer_id = peer_id def protocol(self): diff --git a/network/stream_interface.py b/network/stream_interface.py index f49deb5b..4649e732 100644 --- a/network/stream_interface.py +++ b/network/stream_interface.py @@ -2,8 +2,7 @@ from abc import ABC, abstractmethod class IStream(ABC): - def __init__(self, context, peer_id): - self.context = context + def __init__(self, peer_id): self.peer_id = peer_id @abstractmethod diff --git a/network/swarm.py b/network/swarm.py index b6db89a9..8da3cb5e 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -2,8 +2,7 @@ from .network_interface import INetwork class Swarm(INetwork): - def __init__(self, context, my_peer_id, peer_store): - self.context = context + def __init__(self, my_peer_id, peer_store): self.my_peer_id = my_peer_id self.peer_store = peer_store @@ -14,9 +13,8 @@ class Swarm(INetwork): """ pass - def new_stream(self, context, peer_id): + def new_stream(self, peer_id): """ - :param context: context instance :param peer_id: peer_id of destination :return: stream instance """ diff --git a/requirements.txt b/requirements.txt index 54b18e24..e69de29b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +0,0 @@ -pylint \ No newline at end of file From 5934eaffd0efb38816e5013aa4db377833e926bc Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 13:44:39 -0400 Subject: [PATCH 5/8] added listen to interface --- network/network_interface.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/network/network_interface.py b/network/network_interface.py index f04c3a3a..ae805d3c 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -21,3 +21,10 @@ class INetwork(ABC): :return: stream instance """ pass + + @abstractmethod + def listen(self, *args): + """ + :param *args: one or many multiaddrs to start listening on + :return: true if at least one success + """ From 15978eae11dd89f4ec506f2fcd552e9b2940839b Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 13:51:55 -0400 Subject: [PATCH 6/8] added function to swarm --- network/swarm.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/network/swarm.py b/network/swarm.py index 8da3cb5e..50f7b31b 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -19,3 +19,10 @@ class Swarm(INetwork): :return: stream instance """ pass + + def listen(self, *args): + """ + :param *args: one or many multiaddrs to start listening on + :return: true if at least one success + """ + pass From f538d36947384c8cdfd78ae473b73b4834e8b671 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 14:06:56 -0400 Subject: [PATCH 7/8] Basic multiaddr representation. --- network/multiaddr.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 network/multiaddr.py diff --git a/network/multiaddr.py b/network/multiaddr.py new file mode 100644 index 00000000..38c826d8 --- /dev/null +++ b/network/multiaddr.py @@ -0,0 +1,39 @@ +class Multiaddr: + + def __init__(self, addr): + if not addr[0] == "/": + raise MultiaddrValueError("Invalid input multiaddr.") + + addr = addr[1:] + protocol_map = dict() + split_addr = addr.split("/") + + if len(split_addr) == 0 or len(split_addr) % 2 != 0: + raise MultiaddrValueError("Invalid input multiaddr.") + + is_protocol = True + curr_protocol = "" + + for addr_part in split_addr: + if is_protocol: + curr_protocol = addr_part + else: + protocol_map[curr_protocol] = addr_part + is_protocol = not is_protocol + + self.protocol_map = protocol_map + self.addr = addr + + def get_protocols(self): + return list(self.protocol_map.keys()) + + def get_protocol_value(self, protocol): + if protocol not in self.protocol_map: + return None + + return self.protocol_map[protocol] + + +class MultiaddrValueError(ValueError): + """Raised when the input string to the Multiaddr constructor was invalid.""" + pass From d59a0907b4418c86a307dfc5e6d84f2d810a30b3 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 14:16:26 -0400 Subject: [PATCH 8/8] Updating with results from pylinter --- network/multiaddr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/multiaddr.py b/network/multiaddr.py index 38c826d8..e6d4a5cd 100644 --- a/network/multiaddr.py +++ b/network/multiaddr.py @@ -8,7 +8,7 @@ class Multiaddr: protocol_map = dict() split_addr = addr.split("/") - if len(split_addr) == 0 or len(split_addr) % 2 != 0: + if not split_addr or len(split_addr) % 2 != 0: raise MultiaddrValueError("Invalid input multiaddr.") is_protocol = True