From 8959d491b2d26b09035979e6f525c2dfed464003 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Wed, 31 Oct 2018 22:39:47 +0100 Subject: [PATCH 1/6] Implement raw connection --- connection/raw_connection.py | 10 ++++++++++ connection/raw_connection_interface.py | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/connection/raw_connection.py b/connection/raw_connection.py index e69de29b..e2782c8f 100644 --- a/connection/raw_connection.py +++ b/connection/raw_connection.py @@ -0,0 +1,10 @@ +from .raw_connection import IRawConnection + +class RawConnection(IRawConnection): + + def __init__(self, ip, port): + self.ip = ip + self.port = port + + async def open_connection(self): + self.reader, self.writer = await asyncio.open_connection(self.ip, self.port) diff --git a/connection/raw_connection_interface.py b/connection/raw_connection_interface.py index e69de29b..5e0f3945 100644 --- a/connection/raw_connection_interface.py +++ b/connection/raw_connection_interface.py @@ -0,0 +1,13 @@ +from abc import ABC, abstractmethod +import asyncio + +class IRawConnection(ABC): + + @abstractmethod + def __init__(self, ip, port): + pass + + @abstractmethod + async def open_connection(self): + pass + \ No newline at end of file From bab97f5648115869a76909ee28af2c3546945ce5 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Wed, 31 Oct 2018 22:40:01 +0100 Subject: [PATCH 2/6] Implement new stream --- network/network_interface.py | 3 ++- network/swarm.py | 29 +++++++++++++++++++++++++++-- network/walkthrough.txt | 4 ++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 network/walkthrough.txt diff --git a/network/network_interface.py b/network/network_interface.py index a7b6a697..51589ebd 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -15,9 +15,10 @@ class INetwork(ABC): pass @abstractmethod - def new_stream(self, peer_id): + def new_stream(self, peer_id, protocol_id): """ :param peer_id: peer_id of destination + :param protocol_id: protocol id :return: stream instance """ pass diff --git a/network/swarm.py b/network/swarm.py index 50f7b31b..6190e32d 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -1,10 +1,13 @@ from .network_interface import INetwork +from ..connection.muxed_connection import MuxedConnection +from ..connection.raw_connection import RawConnection class Swarm(INetwork): def __init__(self, my_peer_id, peer_store): self.my_peer_id = my_peer_id self.peer_store = peer_store + self.connections = {} def set_stream_handler(self, stream_handler): """ @@ -13,12 +16,34 @@ class Swarm(INetwork): """ pass - def new_stream(self, peer_id): + def new_stream(self, peer_id, protocol_id): """ :param peer_id: peer_id of destination + :param protocol_id: protocol id :return: stream instance """ - pass + + """ + Determine if a connection to peer_id already exists + If a connection to peer_id exists, then + c = existing connection, + otherwise c = new muxed connection to peer_id + s = c.open_stream(protocol_id) + return s + """ + muxed_connection = None + if peer_id in self.connections: + muxed_connection = self.connections[peer_id] + else: + addrs = self.peer_store.addrs(peer_id) + ip = addrs.get_protocol_value("ip") + port = addrs.get_protocol_value("port") + if len(addrs) > 0: + conn = RawConnection(ip, port) + muxed_connection = MuxedConnection(conn, True) + else: + raise Exception("No IP and port in addr") + return muxed_connection.open_stream(protocol_id, "") def listen(self, *args): """ diff --git a/network/walkthrough.txt b/network/walkthrough.txt new file mode 100644 index 00000000..3183d2a5 --- /dev/null +++ b/network/walkthrough.txt @@ -0,0 +1,4 @@ +host.go --> config.go + config.go: newNode --> swarm.go: newSwarm + newSwarm | initializes data stores + From ad7a449f6e36306921dba99b4329d9e1082acda7 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Wed, 31 Oct 2018 22:40:21 +0100 Subject: [PATCH 3/6] Modify stream to use connection --- stream/stream.py | 9 +++------ stream/stream_interface.py | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/stream/stream.py b/stream/stream.py index 161a5a79..4766cebd 100644 --- a/stream/stream.py +++ b/stream/stream.py @@ -3,17 +3,14 @@ import asyncio class Stream(IStream): - def __init__(self, peer_id, multi_addr): + def __init__(self, peer_id, multi_addr, connection): IStream.__init__(self, peer_id, multi_addr) self.peer_id = peer_id ip = multi_addr.get_protocol_value("ip4") port = multi_addr.get_protocol_value("tcp") - - self.open_connection(ip, port) - - async def open_connection(self, ip, port): - self.reader, self.writer = await asyncio.open_connection(ip, port) + self.reader = connection.reader + self.writer = connection.writer def protocol(self): """ diff --git a/stream/stream_interface.py b/stream/stream_interface.py index 124bb45b..2863df56 100644 --- a/stream/stream_interface.py +++ b/stream/stream_interface.py @@ -2,9 +2,10 @@ from abc import ABC, abstractmethod class IStream(ABC): - def __init__(self, peer_id, multi_addr): + def __init__(self, peer_id, multi_addr, connection): self.peer_id = peer_id self.multi_addr = multi_addr + self.connection = connection @abstractmethod def protocol(self): From 23153885897cef00a841870a7f1f6292dbab32b9 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Wed, 31 Oct 2018 23:08:47 +0100 Subject: [PATCH 4/6] fix connection --- connection/connection_interface.py | 27 -------------------------- connection/raw_connection.py | 14 ++++++++----- connection/raw_connection_interface.py | 7 +------ 3 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 connection/connection_interface.py diff --git a/connection/connection_interface.py b/connection/connection_interface.py deleted file mode 100644 index 813ea7d0..00000000 --- a/connection/connection_interface.py +++ /dev/null @@ -1,27 +0,0 @@ -from abc import ABC, abstractmethod - -class IConnection(ABC): - - @abstractmethod - def get_observed_addrs(self): - """ - retrieve observed addresses from underlying transport - :return: list of multiaddrs - """ - pass - - @abstractmethod - def get_peer_info(self): - """ - retrieve peer info object that the connection connects to - :return: a peer info object - """ - pass - - @abstractmethod - def set_peer_info(self, peer_info): - """ - :param peer_info: a peer info object that contains info of peer - :return: True if successful - """ - pass diff --git a/connection/raw_connection.py b/connection/raw_connection.py index e2782c8f..9c77edb3 100644 --- a/connection/raw_connection.py +++ b/connection/raw_connection.py @@ -1,10 +1,14 @@ -from .raw_connection import IRawConnection +import asyncio +from .raw_connection_interface import IRawConnection class RawConnection(IRawConnection): def __init__(self, ip, port): - self.ip = ip - self.port = port - + self.conn_ip = ip + self.conn_port = port + self.reader = None + self.writer = None + async def open_connection(self): - self.reader, self.writer = await asyncio.open_connection(self.ip, self.port) + self.reader, self.writer = \ + await asyncio.open_connection(self.conn_ip, self.conn_port) diff --git a/connection/raw_connection_interface.py b/connection/raw_connection_interface.py index 5e0f3945..30f0f32f 100644 --- a/connection/raw_connection_interface.py +++ b/connection/raw_connection_interface.py @@ -1,13 +1,8 @@ from abc import ABC, abstractmethod -import asyncio class IRawConnection(ABC): - @abstractmethod - def __init__(self, ip, port): - pass - @abstractmethod async def open_connection(self): pass - \ No newline at end of file + \ No newline at end of file From 92867c42a4b30d3fc9dc225c8facd7b8f97e9d3d Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Wed, 31 Oct 2018 23:13:51 +0100 Subject: [PATCH 5/6] fixed stream --- stream/stream.py | 10 ++++++---- stream/stream_interface.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/stream/stream.py b/stream/stream.py index 4766cebd..cce6ad6e 100644 --- a/stream/stream.py +++ b/stream/stream.py @@ -1,5 +1,5 @@ -from .stream_interface import IStream import asyncio +from .stream_interface import IStream class Stream(IStream): @@ -7,12 +7,14 @@ class Stream(IStream): IStream.__init__(self, peer_id, multi_addr) self.peer_id = peer_id - ip = multi_addr.get_protocol_value("ip4") - port = multi_addr.get_protocol_value("tcp") + stream_ip = multi_addr.get_protocol_value("ip4") + stream_port = multi_addr.get_protocol_value("tcp") self.reader = connection.reader self.writer = connection.writer + # TODO should construct protocol id from constructor + self.protocol_id = None - def protocol(self): + def get_protocol(self): """ :return: protocol id that stream runs on """ diff --git a/stream/stream_interface.py b/stream/stream_interface.py index 2863df56..6d63a2f0 100644 --- a/stream/stream_interface.py +++ b/stream/stream_interface.py @@ -8,7 +8,7 @@ class IStream(ABC): self.connection = connection @abstractmethod - def protocol(self): + def get_protocol(self): """ :return: protocol id that stream runs on """ From f77e7bee95609001138a5d93f0dbbc8f94e95b84 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Wed, 31 Oct 2018 23:31:52 +0100 Subject: [PATCH 6/6] lint network --- network/multiaddr.py | 3 ++- network/network_interface.py | 6 +----- network/swarm.py | 18 ++++++++---------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/network/multiaddr.py b/network/multiaddr.py index b1245802..77075a1e 100644 --- a/network/multiaddr.py +++ b/network/multiaddr.py @@ -88,7 +88,8 @@ class MultiAddr: Gives back a dictionary with access to transport information from this multiaddr. Example: MultiAddr('/ip4/127.0.0.1/tcp/4001').to_options() = { family: 'ipv4', host: '127.0.0.1', transport: 'tcp', port: '4001' } - :return: {{family: String, host: String, transport: String, port: String}} with None if field does not exist + :return: {{family: String, host: String, transport: String, port: String}} + with None if field does not exist """ options = dict() diff --git a/network/network_interface.py b/network/network_interface.py index 51589ebd..17a859b7 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -2,10 +2,6 @@ from abc import ABC, abstractmethod class INetwork(ABC): - def __init__(self, my_peer_id, peer_store): - self.my_peer_id = my_peer_id - self.peer_store = peer_store - @abstractmethod def set_stream_handler(self, stream_handler): """ @@ -29,4 +25,4 @@ class INetwork(ABC): :param *args: one or many multiaddrs to start listening on :return: True if at least one success """ - pass \ No newline at end of file + pass diff --git a/network/swarm.py b/network/swarm.py index 6190e32d..5d570e99 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -17,29 +17,27 @@ class Swarm(INetwork): pass def new_stream(self, peer_id, protocol_id): - """ - :param peer_id: peer_id of destination - :param protocol_id: protocol id - :return: stream instance - """ - """ Determine if a connection to peer_id already exists If a connection to peer_id exists, then - c = existing connection, + c = existing connection, otherwise c = new muxed connection to peer_id s = c.open_stream(protocol_id) return s + + :param peer_id: peer_id of destination + :param protocol_id: protocol id + :return: stream instance """ muxed_connection = None if peer_id in self.connections: muxed_connection = self.connections[peer_id] else: addrs = self.peer_store.addrs(peer_id) - ip = addrs.get_protocol_value("ip") - port = addrs.get_protocol_value("port") + stream_ip = addrs.get_protocol_value("ip") + stream_port = addrs.get_protocol_value("port") if len(addrs) > 0: - conn = RawConnection(ip, port) + conn = RawConnection(stream_ip, stream_port) muxed_connection = MuxedConnection(conn, True) else: raise Exception("No IP and port in addr")