Updated code quality binary operator line change & updated bare except warning code changes in class_addressGenerator.py

Added specific exceptions, Replaced BMConfigParser().get with BMConfigParser().safeGetInt & removed not useful exception handling

Added queue.Full variable to queues

Imported queue & configparser from six.moves and reverted queue.Full monkeypatch from queues.py
This commit is contained in:
kuldeep.k@cisinlabs.com 2021-08-16 19:59:02 +05:30
parent fe32bcac8b
commit 6365012fa4
No known key found for this signature in database
GPG Key ID: AF4FB299BF7C7C2A

View File

@ -17,6 +17,7 @@ from fallback import RIPEMD160Hash
from network import StoppableThread from network import StoppableThread
from pyelliptic import arithmetic from pyelliptic import arithmetic
from pyelliptic.openssl import OpenSSL from pyelliptic.openssl import OpenSSL
from six.moves import configparser, queue
class addressGenerator(StoppableThread): class addressGenerator(StoppableThread):
@ -27,8 +28,9 @@ class addressGenerator(StoppableThread):
def stopThread(self): def stopThread(self):
try: try:
queues.addressGeneratorQueue.put(("stopThread", "data")) queues.addressGeneratorQueue.put(("stopThread", "data"))
except: except queue.Full:
pass self.logger.error('addressGeneratorQueue is Full')
super(addressGenerator, self).stopThread() super(addressGenerator, self).stopThread()
def run(self): def run(self):
@ -61,35 +63,25 @@ class addressGenerator(StoppableThread):
command, addressVersionNumber, streamNumber, label, \ command, addressVersionNumber, streamNumber, label, \
numberOfAddressesToMake, deterministicPassphrase, \ numberOfAddressesToMake, deterministicPassphrase, \
eighteenByteRipe = queueValue eighteenByteRipe = queueValue
try:
numberOfNullBytesDemandedOnFrontOfRipeHash = \ numberOfNullBytesDemandedOnFrontOfRipeHash = \
BMConfigParser().getint( BMConfigParser().safeGetInt(
'bitmessagesettings', 'bitmessagesettings',
'numberofnullbytesonaddress' 'numberofnullbytesonaddress',
) 2 if eighteenByteRipe else 1
except: )
if eighteenByteRipe:
numberOfNullBytesDemandedOnFrontOfRipeHash = 2
else:
# the default
numberOfNullBytesDemandedOnFrontOfRipeHash = 1
elif len(queueValue) == 9: elif len(queueValue) == 9:
command, addressVersionNumber, streamNumber, label, \ command, addressVersionNumber, streamNumber, label, \
numberOfAddressesToMake, deterministicPassphrase, \ numberOfAddressesToMake, deterministicPassphrase, \
eighteenByteRipe, nonceTrialsPerByte, \ eighteenByteRipe, nonceTrialsPerByte, \
payloadLengthExtraBytes = queueValue payloadLengthExtraBytes = queueValue
try:
numberOfNullBytesDemandedOnFrontOfRipeHash = \ numberOfNullBytesDemandedOnFrontOfRipeHash = \
BMConfigParser().getint( BMConfigParser().safeGetInt(
'bitmessagesettings', 'bitmessagesettings',
'numberofnullbytesonaddress' 'numberofnullbytesonaddress',
) 2 if eighteenByteRipe else 1
except: )
if eighteenByteRipe:
numberOfNullBytesDemandedOnFrontOfRipeHash = 2
else:
# the default
numberOfNullBytesDemandedOnFrontOfRipeHash = 1
elif queueValue[0] == 'stopThread': elif queueValue[0] == 'stopThread':
break break
else: else:
@ -143,8 +135,8 @@ class addressGenerator(StoppableThread):
potentialPubSigningKey + potentialPubEncryptionKey) potentialPubSigningKey + potentialPubEncryptionKey)
ripe = RIPEMD160Hash(sha.digest()).digest() ripe = RIPEMD160Hash(sha.digest()).digest()
if ( if (
ripe[:numberOfNullBytesDemandedOnFrontOfRipeHash] == ripe[:numberOfNullBytesDemandedOnFrontOfRipeHash]
'\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash == '\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash
): ):
break break
self.logger.info( self.logger.info(
@ -248,12 +240,12 @@ class addressGenerator(StoppableThread):
while True: while True:
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1
potentialPrivSigningKey = hashlib.sha512( potentialPrivSigningKey = hashlib.sha512(
deterministicPassphrase + deterministicPassphrase
encodeVarint(signingKeyNonce) + encodeVarint(signingKeyNonce)
).digest()[:32] ).digest()[:32]
potentialPrivEncryptionKey = hashlib.sha512( potentialPrivEncryptionKey = hashlib.sha512(
deterministicPassphrase + deterministicPassphrase
encodeVarint(encryptionKeyNonce) + encodeVarint(encryptionKeyNonce)
).digest()[:32] ).digest()[:32]
potentialPubSigningKey = highlevelcrypto.pointMult( potentialPubSigningKey = highlevelcrypto.pointMult(
potentialPrivSigningKey) potentialPrivSigningKey)
@ -266,8 +258,8 @@ class addressGenerator(StoppableThread):
potentialPubSigningKey + potentialPubEncryptionKey) potentialPubSigningKey + potentialPubEncryptionKey)
ripe = RIPEMD160Hash(sha.digest()).digest() ripe = RIPEMD160Hash(sha.digest()).digest()
if ( if (
ripe[:numberOfNullBytesDemandedOnFrontOfRipeHash] == ripe[:numberOfNullBytesDemandedOnFrontOfRipeHash]
'\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash == '\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash
): ):
break break
@ -279,8 +271,8 @@ class addressGenerator(StoppableThread):
' at %s addresses per second before finding' ' at %s addresses per second before finding'
' one with the correct ripe-prefix.', ' one with the correct ripe-prefix.',
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix,
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix / numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix
(time.time() - startTime) / (time.time() - startTime)
) )
except ZeroDivisionError: except ZeroDivisionError:
# The user must have a pretty fast computer. # The user must have a pretty fast computer.
@ -320,7 +312,7 @@ class addressGenerator(StoppableThread):
try: try:
BMConfigParser().add_section(address) BMConfigParser().add_section(address)
addressAlreadyExists = False addressAlreadyExists = False
except: except configparser.DuplicateSectionError:
addressAlreadyExists = True addressAlreadyExists = True
if addressAlreadyExists: if addressAlreadyExists:
@ -369,8 +361,8 @@ class addressGenerator(StoppableThread):
hexlify(potentialPrivEncryptionKey)) hexlify(potentialPrivEncryptionKey))
shared.myAddressesByHash[ripe] = address shared.myAddressesByHash[ripe] = address
tag = hashlib.sha512(hashlib.sha512( tag = hashlib.sha512(hashlib.sha512(
encodeVarint(addressVersionNumber) + encodeVarint(addressVersionNumber)
encodeVarint(streamNumber) + ripe + encodeVarint(streamNumber) + ripe
).digest()).digest()[32:] ).digest()).digest()[32:]
shared.myAddressesByTag[tag] = address shared.myAddressesByTag[tag] = address
if addressVersionNumber == 3: if addressVersionNumber == 3:
@ -401,6 +393,6 @@ class addressGenerator(StoppableThread):
queues.apiAddressGeneratorReturnQueue.put(address) queues.apiAddressGeneratorReturnQueue.put(address)
else: else:
raise Exception( raise Exception(
"Error in the addressGenerator thread. Thread was" + "Error in the addressGenerator thread. Thread was"
" given a command it could not understand: " + command) + " given a command it could not understand: " + command)
queues.addressGeneratorQueue.task_done() queues.addressGeneratorQueue.task_done()