From d2a896697d83e1f99bdd649af8771fb73c54393d Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 7 Aug 2019 18:31:08 +0300 Subject: [PATCH] Used logger.isEnabledFor() to prevent unneeded calculations --- src/class_objectProcessor.py | 57 ++++++++++++++++++++---------------- src/network/bmproto.py | 2 +- src/network/dandelion.py | 7 +++-- src/network/tls.py | 41 ++++++++++++++++---------- 4 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 1a9c0d81..6ae46658 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -1,4 +1,5 @@ import hashlib +import logging import random import shared import threading @@ -24,10 +25,11 @@ import protocol import queues import state import tr -from debug import logger from fallback import RIPEMD160Hash import l10n +logger = logging.getLogger('default') + class objectProcessor(threading.Thread): """ @@ -316,13 +318,14 @@ class objectProcessor(threading.Thread): '\x04' + publicSigningKey + '\x04' + publicEncryptionKey) ripe = RIPEMD160Hash(sha.digest()).digest() - logger.debug( - 'within recpubkey, addressVersion: %s, streamNumber: %s' - '\nripe %s\npublicSigningKey in hex: %s' - '\npublicEncryptionKey in hex: %s', - addressVersion, streamNumber, hexlify(ripe), - hexlify(publicSigningKey), hexlify(publicEncryptionKey) - ) + if logger.isEnabledFor(logging.DEBUG): + logger.debug( + 'within recpubkey, addressVersion: %s, streamNumber: %s' + '\nripe %s\npublicSigningKey in hex: %s' + '\npublicEncryptionKey in hex: %s', + addressVersion, streamNumber, hexlify(ripe), + hexlify(publicSigningKey), hexlify(publicEncryptionKey) + ) address = encodeAddress(addressVersion, streamNumber, ripe) @@ -380,13 +383,14 @@ class objectProcessor(threading.Thread): sha.update(publicSigningKey + publicEncryptionKey) ripe = RIPEMD160Hash(sha.digest()).digest() - logger.debug( - 'within recpubkey, addressVersion: %s, streamNumber: %s' - '\nripe %s\npublicSigningKey in hex: %s' - '\npublicEncryptionKey in hex: %s', - addressVersion, streamNumber, hexlify(ripe), - hexlify(publicSigningKey), hexlify(publicEncryptionKey) - ) + if logger.isEnabledFor(logging.DEBUG): + logger.debug( + 'within recpubkey, addressVersion: %s, streamNumber: %s' + '\nripe %s\npublicSigningKey in hex: %s' + '\npublicEncryptionKey in hex: %s', + addressVersion, streamNumber, hexlify(ripe), + hexlify(publicSigningKey), hexlify(publicEncryptionKey) + ) address = encodeAddress(addressVersion, streamNumber, ripe) queryreturn = sqlQuery( @@ -579,17 +583,18 @@ class objectProcessor(threading.Thread): logger.debug('ECDSA verify failed') return logger.debug('ECDSA verify passed') - logger.debug( - 'As a matter of intellectual curiosity, here is the Bitcoin' - ' address associated with the keys owned by the other person:' - ' %s ..and here is the testnet address: %s. The other person' - ' must take their private signing key from Bitmessage and' - ' import it into Bitcoin (or a service like Blockchain.info)' - ' for it to be of any use. Do not use this unless you know' - ' what you are doing.', - helper_bitcoin.calculateBitcoinAddressFromPubkey(pubSigningKey), - helper_bitcoin.calculateTestnetAddressFromPubkey(pubSigningKey) - ) + if logger.isEnabledFor(logging.DEBUG): + logger.debug( + 'As a matter of intellectual curiosity, here is the Bitcoin' + ' address associated with the keys owned by the other person:' + ' %s ..and here is the testnet address: %s. The other person' + ' must take their private signing key from Bitmessage and' + ' import it into Bitcoin (or a service like Blockchain.info)' + ' for it to be of any use. Do not use this unless you know' + ' what you are doing.', + helper_bitcoin.calculateBitcoinAddressFromPubkey(pubSigningKey), + helper_bitcoin.calculateTestnetAddressFromPubkey(pubSigningKey) + ) # Used to detect and ignore duplicate messages in our inbox sigHash = hashlib.sha512( hashlib.sha512(signature).digest()).digest()[32:] diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 839630d8..6375f393 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -510,7 +510,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.timeOffset = self.timestamp - int(time.time()) logger.debug('remoteProtocolVersion: %i', self.remoteProtocolVersion) logger.debug('services: 0x%08X', self.services) - logger.debug('time offset: %i', self.timestamp - int(time.time())) + logger.debug('time offset: %i', self.timeOffset) logger.debug('my external IP: %s', self.sockNode.host) logger.debug( 'remote node incoming address: %s:%i', diff --git a/src/network/dandelion.py b/src/network/dandelion.py index eed3c6ff..0f68cc24 100644 --- a/src/network/dandelion.py +++ b/src/network/dandelion.py @@ -74,9 +74,10 @@ class Dandelion(): # pylint: disable=old-style-class def removeHash(self, hashId, reason="no reason specified"): """Switch inventory vector from stem to fluff mode""" - logger.debug( - "%s entering fluff mode due to %s.", - ''.join('%02x' % ord(i) for i in hashId), reason) + if logger.isEnabledFor(logging.DEBUG): + logger.debug( + '%s entering fluff mode due to %s.', + ''.join('%02x' % ord(i) for i in hashId), reason) with self.lock: try: del self.hashMap[hashId] diff --git a/src/network/tls.py b/src/network/tls.py index 52f17c29..d5c4e23a 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -39,12 +39,13 @@ else: sslProtocolCiphers = "AECDH-AES256-SHA" -class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instance-attributes +class TLSDispatcher(AdvancedDispatcher): """TLS functionality for classes derived from AdvancedDispatcher""" - # pylint: disable=too-many-arguments, super-init-not-called, unused-argument + # pylint: disable=too-many-instance-attributes + # pylint: disable=too-many-arguments,super-init-not-called,unused-argument def __init__( - self, address=None, sock=None, certfile=None, keyfile=None, - server_side=False, ciphers=sslProtocolCiphers + self, address=None, sock=None, certfile=None, keyfile=None, + server_side=False, ciphers=sslProtocolCiphers ): self.want_read = self.want_write = True if certfile is None: @@ -96,7 +97,10 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc @staticmethod def state_tls_handshake(): - """Do nothing while TLS handshake is pending, as during this phase we need to react to callbacks instead""" + """ + Do nothing while TLS handshake is pending, as during this phase + we need to react to callbacks instead + """ return False def writable(self): @@ -122,10 +126,11 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc except AttributeError: return AdvancedDispatcher.readable(self) - def handle_read(self): # pylint: disable=inconsistent-return-statements + def handle_read(self): # pylint: disable=inconsistent-return-statements """ - Handle reads for sockets during TLS handshake. Requires special treatment as during the handshake, buffers must - remain empty and normal reads must be ignored + Handle reads for sockets during TLS handshake. Requires special + treatment as during the handshake, buffers must remain empty + and normal reads must be ignored. """ try: # wait for write buffer flush @@ -147,10 +152,11 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc self.handle_close() return - def handle_write(self): # pylint: disable=inconsistent-return-statements + def handle_write(self): # pylint: disable=inconsistent-return-statements """ - Handle writes for sockets during TLS handshake. Requires special treatment as during the handshake, buffers - must remain empty and normal writes must be ignored + Handle writes for sockets during TLS handshake. Requires special + treatment as during the handshake, buffers must remain empty + and normal writes must be ignored. """ try: # wait for write buffer flush @@ -193,18 +199,23 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc if not (self.want_write or self.want_read): raise except socket.error as err: - if err.errno in asyncore._DISCONNECTED: # pylint: disable=protected-access + # pylint: disable=protected-access + if err.errno in asyncore._DISCONNECTED: self.handle_close() else: raise else: if sys.version_info >= (2, 7, 9): self.tlsVersion = self.sslSocket.version() - logger.debug("%s:%i: TLS handshake success, TLS protocol version: %s", - self.destination.host, self.destination.port, self.sslSocket.version()) + logger.debug( + '%s:%i: TLS handshake success, TLS protocol version: %s', + self.destination.host, self.destination.port, + self.tlsVersion) else: self.tlsVersion = "TLSv1" - logger.debug("%s:%i: TLS handshake success", self.destination.host, self.destination.port) + logger.debug( + '%s:%i: TLS handshake success', + self.destination.host, self.destination.port) # The handshake has completed, so remove this channel and... self.del_channel() self.set_socket(self.sslSocket)