Skip to content

Commit c106d22

Browse files
authored
Merge pull request #54 from zixuanzh/lint
lgtm
2 parents b427291 + 4daba76 commit c106d22

18 files changed

+68
-86
lines changed

host/basic_host.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .host_interface import IHost
22

3+
34
# Upon host creation, host takes in options,
45
# including the list of addresses on which to listen.
56
# Host then parses these options and delegates to its Network instance,
@@ -51,7 +52,7 @@ def set_stream_handler(self, protocol_id, stream_handler):
5152
async def new_stream(self, peer_id, protocol_id):
5253
"""
5354
:param peer_id: peer_id that host is connecting
54-
:param proto_id: protocol id that stream runs on
55+
:param protocol_id: protocol id that stream runs on
5556
:return: true if successful
5657
"""
5758
# TODO: host should return a mux stream not a raw stream

host/host_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22

3+
34
class IHost(ABC):
45

56
@abstractmethod
@@ -36,9 +37,8 @@ def set_stream_handler(self, protocol_id, stream_handler):
3637
# protocol_id can be a list of protocol_ids
3738
# stream will decide which protocol_id to run on
3839
@abstractmethod
39-
def new_stream(self, context, peer_id, protocol_id):
40+
def new_stream(self, peer_id, protocol_id):
4041
"""
41-
:param context: a context instance
4242
:param peer_id: peer_id that host is connecting
4343
:param proto_id: protocol id that stream runs on
4444
:return: true if successful

libp2p/libp2p.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1+
from Crypto.PublicKey import RSA
12
from peer.peerstore import PeerStore
23
from network.swarm import Swarm
34
from host.basic_host import BasicHost
45
from transport.upgrader import TransportUpgrader
56
from transport.tcp.tcp import TCP
6-
from Crypto.PublicKey import RSA
77

8-
class Libp2p(object):
98

10-
def __init__(self, idOpt = None, \
11-
transportOpt = ["/ip4/127.0.0.1/tcp/8001"], \
12-
muxerOpt = ["mplex/6.7.0"], \
13-
secOpt = ["secio"], \
14-
peerstore = PeerStore()):
15-
16-
if idOpt:
17-
self.idOpt = idOpt
9+
class Libp2p():
10+
11+
def __init__(self, id_opt=None, transport_opt=["/ip4/127.0.0.1/tcp/8001"], \
12+
muxer_opt=["mplex/6.7.0"], sec_opt=["secio"], peerstore=PeerStore()):
13+
14+
if id_opt:
15+
self.id_opt = id_opt
1816
else:
1917
new_key = RSA.generate(2048, e=65537)
20-
self.idOpt = new_key.publickey().exportKey("PEM")
18+
self.id_opt = new_key.publickey().exportKey("PEM")
2119
self.private_key = new_key.exportKey("PEM")
22-
23-
self.transportOpt = transportOpt
24-
self.muxerOpt = muxerOpt
25-
self.secOpt = secOpt
20+
21+
self.transport_opt = transport_opt
22+
self.muxer_opt = muxer_opt
23+
self.sec_opt = sec_opt
2624
self.peerstore = peerstore
2725

2826
async def new_node(self):
2927

30-
upgrader = TransportUpgrader(self.secOpt, self.transportOpt)
31-
swarm = Swarm(self.idOpt, self.peerstore, upgrader)
28+
upgrader = TransportUpgrader(self.sec_opt, self.transport_opt)
29+
swarm = Swarm(self.id_opt, self.peerstore, upgrader)
3230
tcp = TCP()
3331
swarm.add_transport(tcp)
34-
await swarm.listen(self.transportOpt[0])
32+
await swarm.listen(self.transport_opt[0])
3533
host = BasicHost(swarm)
3634

3735
# TODO MuxedConnection currently contains all muxing logic (move to a Muxer)

muxer/mplex/muxed_connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .muxed_connection_interface import IMuxedConn
44
from .muxed_stream import MuxedStream
55

6+
67
class MuxedConn(IMuxedConn):
78
"""
89
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
@@ -47,6 +48,10 @@ async def read_buffer(self, stream_id):
4748
def open_stream(self, protocol_id, stream_id, peer_id, multi_addr):
4849
"""
4950
creates a new muxed_stream
51+
:param protocol_id: protocol_id of stream
52+
:param stream_id: stream_id of stream
53+
:param peer_id: peer_id that stream connects to
54+
:param multi_addr: multi_addr that stream connects to
5055
:return: a new stream
5156
"""
5257
stream = MuxedStream(stream_id, multi_addr, self)

muxer/mplex/muxed_connection_interface.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ def is_closed(self):
2323
pass
2424

2525
@abstractmethod
26-
def open_stream(self, protocol_id, stream_name):
26+
def open_stream(self, protocol_id, stream_id, peer_id, multi_addr):
2727
"""
2828
creates a new muxed_stream
29-
:param protocol_id: id to be associated with stream
30-
:param stream_name: name as part of identifier
29+
:param protocol_id: protocol_id of stream
30+
:param stream_id: stream_id of stream
31+
:param peer_id: peer_id that stream connects to
32+
:param multi_addr: multi_addr that stream connects to
3133
:return: a new stream
3234
"""
3335
pass

muxer/mplex/muxed_stream.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
from .muxed_stream_interface import IMuxedStream
32
from .constants import HEADER_TAGS
43

@@ -15,12 +14,12 @@ def __init__(self, stream_id, initiator, muxed_conn):
1514
:param initiator: boolean if this is an initiator
1615
:param muxed_conn: muxed connection of this muxed_stream
1716
"""
18-
self.id = stream_id
17+
self.stream_id = stream_id
1918
self.initiator = initiator
2019
self.muxed_conn = muxed_conn
2120

22-
# self.read_deadline = None
23-
# self.write_deadline = None
21+
self.read_deadline = None
22+
self.write_deadline = None
2423

2524
self.local_closed = False
2625
self.remote_closed = False
@@ -33,22 +32,22 @@ def get_flag(self, action):
3332
"""
3433
if self.initiator:
3534
return HEADER_TAGS[action]
36-
else:
37-
return HEADER_TAGS[action] - 1
35+
36+
return HEADER_TAGS[action] - 1
3837

3938
async def read(self):
4039
"""
4140
read messages associated with stream from buffer til end of file
4241
:return: bytes of input
4342
"""
44-
return await self.muxed_conn.read_buffer(self.id)
43+
return await self.muxed_conn.read_buffer(self.stream_id)
4544

4645
async def write(self, data):
4746
"""
4847
write to stream
4948
:return: number of bytes written
5049
"""
51-
return await self.muxed_conn.send_message(self.get_flag("MESSAGE"), data, self.id)
50+
return await self.muxed_conn.send_message(self.get_flag("MESSAGE"), data, self.stream_id)
5251

5352
def close(self):
5453
"""
@@ -59,8 +58,8 @@ def close(self):
5958
if self.local_closed and self.remote_closed:
6059
return True
6160

62-
self.muxed_conn.send_message(self.get_flag("CLOSE"), None, self.id)
63-
self.muxed_conn.streams.pop(self.id)
61+
self.muxed_conn.send_message(self.get_flag("CLOSE"), None, self.stream_id)
62+
self.muxed_conn.streams.pop(self.stream_id)
6463

6564
self.local_closed = True
6665
self.remote_closed = True

muxer/mplex/smux_multiplex.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from .muxed_stream import MuxedStream
21
from .muxed_connection import MuxedConn
32

3+
44
class Multiplex(object):
55
"""
6+
muxing logic currently lives in MuxedConn
67
reference: https://github.com/whyrusleeping/go-smux-multiplex/blob/master/multiplex.go
78
"""
89
def __init__(self, conn, initiator):
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import asyncio
21
from .raw_connection_interface import IRawConnection
32

3+
44
class RawConnection(IRawConnection):
55

66
def __init__(self, ip, port, reader, writer):
@@ -12,15 +12,3 @@ def __init__(self, ip, port, reader, writer):
1212

1313
def close(self):
1414
self.writer.close()
15-
16-
# def __init__(self, ip, port):
17-
# self.conn_ip = ip
18-
# self.conn_port = port
19-
# self.reader, self.writer = self.open_connection()
20-
21-
# async def open_connection(self):
22-
# """
23-
# opens a connection on self.ip and self.port
24-
# :return: a raw connection
25-
# """
26-
# return await asyncio.open_connection(self.conn_ip, self.conn_port)
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
from abc import ABC, abstractmethod
1+
from abc import ABC
2+
23

34
class IRawConnection(ABC):
45
"""
56
A Raw Connection provides a Reader and a Writer
6-
open_connection should return such a connection
77
"""
8-
9-
# @abstractmethod
10-
# async def open_connection(self):
11-
# """
12-
# opens a connection on ip and port
13-
# :return: a raw connection
14-
# """
15-
# pass

network/network_interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22

3+
34
class INetwork(ABC):
45

56
@abstractmethod

0 commit comments

Comments
 (0)