WIP: Handling a new pylint design checker message #17

Draft
lee.miller wants to merge 4 commits from lee.miller/MiNode:lint into v0.3
8 changed files with 39 additions and 53 deletions

View File

@ -20,22 +20,19 @@ class ConnectionBase(threading.Thread):
Common code for the connection thread Common code for the connection thread
with minimum command handlers to reuse with minimum command handlers to reuse
""" """
def __init__( def __init__(self, host, port, s=None, server=False):
self, host, port, s=None, network='ip', server=False,
i2p_remote_dest=b''
):
self.host = host self.host = host
self.port = port self.port = port
self.network = network self.network = 'i2p' if port == 'i2p' else 'ip'
self.i2p_remote_dest = i2p_remote_dest
if self.network == 'i2p': self.host_print = (
self.host_print = self.i2p_remote_dest[:8].decode() self.host[:8].decode() if self.network == 'i2p' else self.host)
else:
self.host_print = self.host
super().__init__(name='Connection to {}:{}'.format(host, port)) super().__init__(name='Connection to {}:{}'.format(host, port))
self.s = s
self.server = server
self.send_queue = queue.Queue() self.send_queue = queue.Queue()
self.vectors_to_get = set() self.vectors_to_get = set()
@ -43,22 +40,13 @@ class ConnectionBase(threading.Thread):
self.vectors_requested = {} self.vectors_requested = {}
self.status = 'ready' self.status = 'connected' if bool(s) else 'ready'
self.tls = False
self.verack_received = False self.verack_received = False
self.verack_sent = False self.verack_sent = False
self.s = s
self.remote_version = None self.remote_version = None
self.server = server
if bool(s):
self.status = 'connected'
self.buffer_receive = b'' self.buffer_receive = b''
self.buffer_send = b'' self.buffer_send = b''
@ -238,7 +226,7 @@ class ConnectionBase(threading.Thread):
logging.debug('ssl.SSLError reason: %s', e.reason) logging.debug('ssl.SSLError reason: %s', e.reason)
shared.node_pool.discard((self.host, self.port)) shared.node_pool.discard((self.host, self.port))
return return
self.tls = True
logging.debug( logging.debug(
'Established TLS connection with %s:%s (%s)', 'Established TLS connection with %s:%s (%s)',
self.host_print, self.port, self.s.version()) self.host_print, self.port, self.s.version())

View File

@ -7,14 +7,15 @@ from .util import I2PThread
class I2PDialer(I2PThread): class I2PDialer(I2PThread):
def __init__( def __init__(
self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656 self, state, destination, nick=None, *, sam_host=None, sam_port=None
): ):
self.sam_host = sam_host # Initially 127.0.0.1:7656
self.sam_port = sam_port self.sam_host = sam_host or state.i2p_sam_host
self.sam_port = sam_port or state.i2p_sam_port
self.nick = nick
self.destination = destination self.destination = destination
self.nick = nick or state.i2p_session_nick
super().__init__(state, name='I2P Dial to {}'.format(self.destination)) super().__init__(state, name='I2P Dial to {}'.format(self.destination))
@ -27,9 +28,7 @@ class I2PDialer(I2PThread):
logging.debug('Connecting to %s', self.destination) logging.debug('Connecting to %s', self.destination)
self._connect() self._connect()
if not self.state.shutting_down and self.success: if not self.state.shutting_down and self.success:
c = self.state.connection( c = self.state.connection(self.destination, 'i2p', self.s, False)
self.destination, 'i2p', self.s, 'i2p',
False, self.destination)
c.start() c.start()
self.state.connections.add(c) self.state.connections.add(c)

View File

@ -45,8 +45,7 @@ class I2PListener(I2PThread):
logging.debug('Rejecting duplicate I2P connection.') logging.debug('Rejecting duplicate I2P connection.')
self.s.close() self.s.close()
else: else:
c = self.state.connection( c = self.state.connection(destination, 'i2p', self.s, True)
destination, 'i2p', self.s, 'i2p', True, destination)
c.start() c.start()
self.state.connections.add(c) self.state.connections.add(c)
c = None c = None

View File

@ -156,10 +156,7 @@ class Manager(threading.Thread):
if port == 'i2p' and shared.i2p_enabled: if port == 'i2p' and shared.i2p_enabled:
if shared.i2p_session_nick and host != shared.i2p_dest_pub: if shared.i2p_session_nick and host != shared.i2p_dest_pub:
try: try:
d = I2PDialer( d = I2PDialer(shared, host)
shared,
host, shared.i2p_session_nick,
shared.i2p_sam_host, shared.i2p_sam_port)
d.start() d.start()
hosts.add(d.destination) hosts.add(d.destination)
shared.i2p_dialers.add(d) shared.i2p_dialers.add(d)
@ -275,7 +272,7 @@ class Manager(threading.Thread):
dest_pub_raw = base64.b64decode( dest_pub_raw = base64.b64decode(
shared.i2p_dest_pub, altchars=b'-~') shared.i2p_dest_pub, altchars=b'-~')
obj = structure.Object( obj = structure.Object(
b'\x00' * 8, int(time.time() + 2 * 3600), int(time.time() + 2 * 3600),
shared.i2p_dest_obj_type, shared.i2p_dest_obj_version, shared.i2p_dest_obj_type, shared.i2p_dest_obj_version,
shared.stream, dest_pub_raw) shared.stream, object_payload=dest_pub_raw)
proofofwork.do_pow_and_publish(obj) proofofwork.do_pow_and_publish(obj)

View File

@ -101,9 +101,10 @@ def _payload_read_int(data):
class Version(): class Version():
"""The version message payload""" """The version message payload"""
def __init__( def __init__(
self, host, port, protocol_version=shared.protocol_version, self, host, port,
services=shared.services, nonce=shared.nonce, nonce=shared.nonce, services=shared.services,
user_agent=shared.user_agent, streams=None *, streams=None, user_agent=shared.user_agent,
protocol_version=shared.protocol_version,
): ):
self.host = host self.host = host
self.port = port self.port = port
@ -176,7 +177,8 @@ class Version():
raise ValueError('malformed Version message, wrong streams_count') raise ValueError('malformed Version message, wrong streams_count')
return cls( return cls(
host, port, protocol_version, services, nonce, user_agent, streams) host, port, nonce, services, streams=streams,
protocol_version=protocol_version, user_agent=user_agent)
class Inv(): class Inv():

View File

@ -39,8 +39,8 @@ def _worker(obj):
logging.debug( logging.debug(
'Finished doing POW, nonce: %s, time: %ss', nonce, time.time() - t) 'Finished doing POW, nonce: %s, time: %ss', nonce, time.time() - t)
obj = structure.Object( obj = structure.Object(
nonce, obj.expires_time, obj.object_type, obj.version, obj.expires_time, obj.object_type, obj.version, obj.stream_number,
obj.stream_number, obj.object_payload) object_payload=obj.object_payload, nonce=nonce)
logging.debug( logging.debug(
'Object vector is %s', base64.b16encode(obj.vector).decode()) 'Object vector is %s', base64.b16encode(obj.vector).decode())

View File

@ -48,8 +48,8 @@ class VarInt():
class Object(): class Object():
"""The 'object' message payload""" """The 'object' message payload"""
def __init__( def __init__(
self, nonce, expires_time, object_type, version, self, expires_time, object_type, version, stream_number,
stream_number, object_payload *, object_payload, tag=None, nonce=b'\x00' * 8
): ):
self.nonce = nonce self.nonce = nonce
self.expires_time = expires_time self.expires_time = expires_time
@ -60,7 +60,7 @@ class Object():
self.vector = hashlib.sha512(hashlib.sha512( self.vector = hashlib.sha512(hashlib.sha512(
self.to_bytes()).digest()).digest()[:32] self.to_bytes()).digest()).digest()[:32]
self.tag = ( self.tag = tag or (
# broadcast from version 5 and pubkey/getpukey from version 4 # broadcast from version 5 and pubkey/getpukey from version 4
self.object_payload[:32] if object_type == 3 and version == 5 self.object_payload[:32] if object_type == 3 and version == 5
or (object_type in (0, 1) and version == 4) or (object_type in (0, 1) and version == 4)
@ -84,7 +84,8 @@ class Object():
payload[:stream_number_varint_length]).n payload[:stream_number_varint_length]).n
payload = payload[stream_number_varint_length:] payload = payload[stream_number_varint_length:]
return cls( return cls(
nonce, expires_time, object_type, version, stream_number, payload) expires_time, object_type, version, stream_number,
object_payload=payload, nonce=nonce)
def to_bytes(self): def to_bytes(self):
"""Serialize to bytes""" """Serialize to bytes"""

View File

@ -19,7 +19,7 @@ sample_addr_data = unhexlify(
# data for an object with expires_time 1697063939 # data for an object with expires_time 1697063939
# structure.Object( # structure.Object(
# b'\x00' * 8, expires_time, 42, 1, 2, b'HELLO').to_bytes() # expires_time, 42, 1, 2, object_payload=b'HELLO').to_bytes()
sample_object_data = unhexlify( sample_object_data = unhexlify(
'000000000000000000000000652724030000002a010248454c4c4f') '000000000000000000000000652724030000002a010248454c4c4f')
@ -135,13 +135,13 @@ class TestStructure(unittest.TestCase):
self.assertEqual(obj.object_payload, b'HELLO') self.assertEqual(obj.object_payload, b'HELLO')
obj = structure.Object( obj = structure.Object(
b'\x00' * 8, int(time.time() + 3000000), 42, 1, 1, b'HELLO') int(time.time() + 3000000), 42, 1, 1, object_payload=b'HELLO')
self.assertFalse(obj.is_valid()) self.assertFalse(obj.is_valid())
obj.expires_time = int(time.time() - 11000) obj.expires_time = int(time.time() - 11000)
self.assertFalse(obj.is_valid()) self.assertFalse(obj.is_valid())
obj = structure.Object( obj = structure.Object(
b'\x00' * 8, int(time.time() + 300), 42, 1, 2, b'HELLO') int(time.time() + 300), 42, 1, 2, object_payload=b'HELLO')
vector = obj.vector vector = obj.vector
proofofwork._worker(obj) # pylint: disable=protected-access proofofwork._worker(obj) # pylint: disable=protected-access
obj = shared.objects.popitem()[1] obj = shared.objects.popitem()[1]
@ -159,8 +159,8 @@ class TestStructure(unittest.TestCase):
"""Check the main proofofwork call and worker""" """Check the main proofofwork call and worker"""
shared.vector_advertise_queue = queue.Queue() shared.vector_advertise_queue = queue.Queue()
obj = structure.Object( obj = structure.Object(
b'\x00' * 8, int(time.time() + 300), 42, 1, int(time.time() + 300), 42, 1,
shared.stream, b'HELLO') shared.stream, object_payload=b'HELLO')
start_time = time.time() start_time = time.time()
proofofwork.do_pow_and_publish(obj) proofofwork.do_pow_and_publish(obj)
try: try:
@ -189,6 +189,6 @@ class TestStructure(unittest.TestCase):
self.fail("No nonce found in the queue") self.fail("No nonce found in the queue")
obj = structure.Object( obj = structure.Object(
nonce, obj.expires_time, obj.object_type, obj.version, obj.expires_time, obj.object_type, obj.version, obj.stream_number,
obj.stream_number, obj.object_payload) object_payload=obj.object_payload, nonce=nonce)
self.assertTrue(obj.is_valid()) self.assertTrue(obj.is_valid())