Used logger.isEnabledFor() to prevent unneeded calculations

This commit is contained in:
Dmitri Bogomolov 2019-08-07 18:31:08 +03:00
parent 7a89109fc9
commit d2a896697d
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
4 changed files with 62 additions and 45 deletions

View File

@ -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,6 +318,7 @@ class objectProcessor(threading.Thread):
'\x04' + publicSigningKey + '\x04' + publicEncryptionKey)
ripe = RIPEMD160Hash(sha.digest()).digest()
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
'within recpubkey, addressVersion: %s, streamNumber: %s'
'\nripe %s\npublicSigningKey in hex: %s'
@ -380,6 +383,7 @@ class objectProcessor(threading.Thread):
sha.update(publicSigningKey + publicEncryptionKey)
ripe = RIPEMD160Hash(sha.digest()).digest()
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
'within recpubkey, addressVersion: %s, streamNumber: %s'
'\nripe %s\npublicSigningKey in hex: %s'
@ -579,6 +583,7 @@ class objectProcessor(threading.Thread):
logger.debug('ECDSA verify failed')
return
logger.debug('ECDSA verify passed')
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:'

View File

@ -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',

View File

@ -74,8 +74,9 @@ class Dandelion(): # pylint: disable=old-style-class
def removeHash(self, hashId, reason="no reason specified"):
"""Switch inventory vector from stem to fluff mode"""
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
"%s entering fluff mode due to %s.",
'%s entering fluff mode due to %s.',
''.join('%02x' % ord(i) for i in hashId), reason)
with self.lock:
try:

View File

@ -39,8 +39,9 @@ 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-instance-attributes
# pylint: disable=too-many-arguments,super-init-not-called,unused-argument
def __init__(
self, address=None, sock=None, certfile=None, keyfile=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):
@ -124,8 +128,9 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
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
@ -149,8 +154,9 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
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)