use bytes for key of hashtable in replacement of hexlified string

This commit is contained in:
Kashiko Koibumi 2024-05-16 23:45:10 +09:00
parent ec91d9f20c
commit 1b3ce71f19
No known key found for this signature in database
GPG Key ID: 8F06E069E37C40C4
15 changed files with 130 additions and 160 deletions

View File

@ -1347,7 +1347,7 @@ class BMRPCDispatcher(object):
'Broadcasting inv for msg(API disseminatePreEncryptedMsg'
' command): %s', hexlify(inventoryHash))
queues.invQueue.put((toStreamNumber, inventoryHash))
return hexlify(inventoryHash).decode('ascii')
return hexlify(inventoryHash).decode()
@command('trashSentMessageByAckData')
def HandleTrashSentMessageByAckDAta(self, ackdata):

View File

@ -1769,7 +1769,7 @@ class MyForm(settingsmixin.SMainWindow):
if rect.width() > 20:
txt = "+"
fontSize = 15
font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Bold)
font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Weight.Bold)
fontMetrics = QtGui.QFontMetrics(font)
rect = fontMetrics.boundingRect(txt)
# draw text

View File

@ -172,10 +172,10 @@ class addressGenerator(StoppableThread):
config.set(address, 'payloadlengthextrabytes', str(
payloadLengthExtraBytes))
config.set(
address, 'privsigningkey', privSigningKeyWIF.decode('ascii'))
address, 'privsigningkey', privSigningKeyWIF.decode())
config.set(
address, 'privencryptionkey',
privEncryptionKeyWIF.decode('ascii'))
privEncryptionKeyWIF.decode())
config.save()
# The API and the join and create Chan functionality
@ -325,10 +325,10 @@ class addressGenerator(StoppableThread):
str(payloadLengthExtraBytes))
config.set(
address, 'privsigningkey',
privSigningKeyWIF.decode('ascii'))
privSigningKeyWIF.decode())
config.set(
address, 'privencryptionkey',
privEncryptionKeyWIF.decode('ascii'))
privEncryptionKeyWIF.decode())
config.save()
queues.UISignalQueue.put((
@ -340,14 +340,12 @@ class addressGenerator(StoppableThread):
shared.myECCryptorObjects[ripe] = \
highlevelcrypto.makeCryptor(
hexlify(potentialPrivEncryptionKey))
hex_ripe = hexlify(ripe).decode('ascii')
shared.myAddressesByHash[hex_ripe] = address
shared.myAddressesByHash[bytes(ripe)] = address
tag = highlevelcrypto.double_sha512(
encodeVarint(addressVersionNumber)
+ encodeVarint(streamNumber) + ripe
)[32:]
hex_tag = hexlify(tag).decode('ascii')
shared.myAddressesByTag[hex_tag] = address
shared.myAddressesByTag[bytes(tag)] = address
if addressVersionNumber == 3:
# If this is a chan address,
# the worker thread won't send out

View File

@ -141,10 +141,10 @@ class objectProcessor(threading.Thread):
# bypass nonce and time, retain object type/version/stream + body
readPosition = 16
hex_data = hexlify(data[readPosition:]).decode('ascii')
if hex_data in state.ackdataForWhichImWatching:
data_bytes = bytes(data[readPosition:])
if data_bytes in state.ackdataForWhichImWatching:
logger.info('This object is an acknowledgement bound for me.')
del state.ackdataForWhichImWatching[hex_data]
del state.ackdataForWhichImWatching[data_bytes]
sqlExecute(
"UPDATE sent SET status='ackreceived', lastactiontime=?"
" WHERE ackdata=?", int(time.time()), data[readPosition:])
@ -214,24 +214,25 @@ class objectProcessor(threading.Thread):
return logger.debug(
'The length of the requested hash is not 20 bytes.'
' Something is wrong. Ignoring.')
hex_hash = hexlify(requestedHash).decode('ascii')
logger.info(
'the hash requested in this getpubkey request is: %s',
hex_hash)
hexlify(requestedHash).decode())
requestedHash_bytes = bytes(requestedHash)
# if this address hash is one of mine
if hex_hash in shared.myAddressesByHash:
myAddress = shared.myAddressesByHash[hex_hash]
if requestedHash_bytes in shared.myAddressesByHash:
myAddress = shared.myAddressesByHash[requestedHash_bytes]
elif requestedAddressVersionNumber >= 4:
requestedTag = data[readPosition:readPosition + 32]
if len(requestedTag) != 32:
return logger.debug(
'The length of the requested tag is not 32 bytes.'
' Something is wrong. Ignoring.')
hex_tag = hexlify(requestedTag).decode('ascii')
logger.debug(
'the tag requested in this getpubkey request is: %s', hex_tag)
if hex_tag in shared.myAddressesByTag:
myAddress = shared.myAddressesByTag[hex_tag]
'the tag requested in this getpubkey request is: %s',
hexlify(requestedTag).decode())
requestedTag_bytes = bytes(requestedTag)
if requestedTag_bytes in shared.myAddressesByTag:
myAddress = shared.myAddressesByTag[requestedTag_bytes]
if myAddress == '':
logger.info('This getpubkey request is not for any of my keys.')
@ -421,13 +422,13 @@ class objectProcessor(threading.Thread):
' Sanity check failed.')
tag = data[readPosition:readPosition + 32]
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
if hex_tag not in state.neededPubkeys:
tag_bytes = bytes(tag)
if tag_bytes not in state.neededPubkeys:
return logger.info(
'We don\'t need this v4 pubkey. We didn\'t ask for it.')
# Let us try to decrypt the pubkey
toAddress = state.neededPubkeys[hex_tag][0]
toAddress = state.neededPubkeys[tag_bytes][0]
if protocol.decryptAndCheckPubkeyPayload(data, toAddress) == \
'successful':
# At this point we know that we have been waiting on this
@ -492,8 +493,7 @@ class objectProcessor(threading.Thread):
# This is a message bound for me.
# Look up my address based on the RIPE hash.
hex_ripe = hexlify(toRipe).decode('ascii')
toAddress = shared.myAddressesByHash[hex_ripe]
toAddress = shared.myAddressesByHash[bytes(toRipe)]
readPosition = 0
sendersAddressVersionNumber, sendersAddressVersionNumberLength = \
decodeVarint(decryptedData[readPosition:readPosition + 10])
@ -803,11 +803,11 @@ class objectProcessor(threading.Thread):
# of the sender's address to verify that it was
# encrypted by with their key rather than some
# other key.
toRipe = unhexlify(key)
toRipe = key
initialDecryptionSuccessful = True
logger.info(
'EC decryption successful using key associated'
' with ripe hash: %s', key)
' with ripe hash: %s', hexlify(key).decode())
except Exception as ex:
logger.debug(
'cryptorObject.decrypt Exception: {}'.format(ex))
@ -820,14 +820,14 @@ class objectProcessor(threading.Thread):
elif broadcastVersion == 5:
embeddedTag = data[readPosition:readPosition + 32]
readPosition += 32
hex_tag = hexlify(embeddedTag).decode('ascii')
if hex_tag not in shared.MyECSubscriptionCryptorObjects:
embeddedTag_bytes = bytes(embeddedTag)
if embeddedTag_bytes not in shared.MyECSubscriptionCryptorObjects:
logger.debug('We\'re not interested in this broadcast.')
return
# We are interested in this broadcast because of its tag.
# We're going to add some more data which is signed further down.
signedData = bytes(data[8:readPosition])
cryptorObject = shared.MyECSubscriptionCryptorObjects[hex_tag]
cryptorObject = shared.MyECSubscriptionCryptorObjects[embeddedTag_bytes]
try:
decryptedData = cryptorObject.decrypt(data[readPosition:])
logger.debug('EC decryption successful')
@ -1011,9 +1011,9 @@ class objectProcessor(threading.Thread):
encodeVarint(addressVersion) + encodeVarint(streamNumber)
+ ripe
)[32:]
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
if hex_tag in state.neededPubkeys:
del state.neededPubkeys[hex_tag]
tag_bytes = bytes(tag)
if tag_bytes in state.neededPubkeys:
del state.neededPubkeys[tag_bytes]
self.sendMessages(address)
@staticmethod

View File

@ -87,8 +87,7 @@ class singleWorker(StoppableThread):
tag = doubleHashOfAddressData[32:]
# We'll need this for when we receive a pubkey reply:
# it will be encrypted and we'll need to decrypt it.
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
state.neededPubkeys[hex_tag] = (
state.neededPubkeys[bytes(tag)] = (
toAddress,
highlevelcrypto.makeCryptor(
hexlify(privEncryptionKey))
@ -99,23 +98,20 @@ class singleWorker(StoppableThread):
'''SELECT ackdata FROM sent WHERE status = 'msgsent' AND folder = 'sent' ''')
for row in queryreturn:
ackdata, = row
self.logger.info('Watching for ackdata %s', hexlify(ackdata))
hex_ackdata = hexlify(ackdata).decode('ascii')
state.ackdataForWhichImWatching[hex_ackdata] = 0
self.logger.info('Watching for ackdata %s', hexlify(ackdata).decode())
state.ackdataForWhichImWatching[bytes(ackdata)] = 0
# Fix legacy (headerless) watched ackdata to include header
for hex_oldack in state.ackdataForWhichImWatching:
oldack = unhexlify(hex_oldack)
for oldack in state.ackdataForWhichImWatching:
if len(oldack) == 32:
# attach legacy header, always constant (msg/1/1)
newack = b'\x00\x00\x00\x02\x01\x01' + oldack
hex_newack = hexlify(newack).decode('ascii')
state.ackdataForWhichImWatching[hex_newack] = 0
state.ackdataForWhichImWatching[bytes(newack)] = 0
sqlExecute(
'''UPDATE sent SET ackdata=? WHERE ackdata=? AND folder = 'sent' ''',
newack, oldack
)
del state.ackdataForWhichImWatching[hex_oldack]
del state.ackdataForWhichImWatching[oldack]
# For the case if user deleted knownnodes
# but is still having onionpeer objects in inventory
@ -701,7 +697,7 @@ class singleWorker(StoppableThread):
ackdata,
tr._translate(
"MainWindow",
"Broadcast sent on %1"
"Broadcast sent on {0}"
).format(l10n.formatTimestamp()))
))
@ -798,9 +794,9 @@ class singleWorker(StoppableThread):
encodeVarint(toAddressVersionNumber)
+ encodeVarint(toStreamNumber) + toRipe
)[32:]
hex_tag = 'tag-' + hexlify(toTag).decode('ascii')
toTag_bytes = bytes(toTag)
if toaddress in state.neededPubkeys or \
hex_tag in state.neededPubkeys:
toTag_bytes in state.neededPubkeys:
# We already sent a request for the pubkey
sqlExecute(
'''UPDATE sent SET status='awaitingpubkey', '''
@ -841,8 +837,8 @@ class singleWorker(StoppableThread):
privEncryptionKey = doubleHashOfToAddressData[:32]
# The second half of the sha512 hash.
tag = doubleHashOfToAddressData[32:]
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
state.neededPubkeys[hex_tag] = (
tag_bytes = bytes(tag)
state.neededPubkeys[tag_bytes] = (
toaddress,
highlevelcrypto.makeCryptor(
hexlify(privEncryptionKey))
@ -865,7 +861,7 @@ class singleWorker(StoppableThread):
''' status='doingpubkeypow') AND '''
''' folder='sent' ''',
toaddress)
del state.neededPubkeys[hex_tag]
del state.neededPubkeys[tag_bytes]
break
# else:
# There was something wrong with this
@ -907,8 +903,7 @@ class singleWorker(StoppableThread):
# if we aren't sending this to ourselves or a chan
if not config.has_section(toaddress):
hex_ackdata = hexlify(ackdata).decode('ascii')
state.ackdataForWhichImWatching[hex_ackdata] = 0
state.ackdataForWhichImWatching[bytes(ackdata)] = 0
queues.UISignalQueue.put((
'updateSentItemStatusByAckdata', (
ackdata,
@ -976,7 +971,7 @@ class singleWorker(StoppableThread):
" device who requests that the"
" destination be included in the"
" message but this is disallowed in"
" your settings. %1"
" your settings. {0}"
).format(l10n.formatTimestamp()))
))
# if the human changes their setting and then
@ -1315,7 +1310,7 @@ class singleWorker(StoppableThread):
ackdata,
tr._translate(
"MainWindow",
"Message sent. Sent at %1"
"Message sent. Sent at {0}"
).format(l10n.formatTimestamp()))))
else:
# not sending to a chan or one of my addresses
@ -1418,11 +1413,11 @@ class singleWorker(StoppableThread):
privEncryptionKey = doubleHashOfAddressData[:32]
# Note that this is the second half of the sha512 hash.
tag = doubleHashOfAddressData[32:]
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
if hex_tag not in state.neededPubkeys:
tag_bytes = bytes(tag)
if tag_bytes not in state.neededPubkeys:
# We'll need this for when we receive a pubkey reply:
# it will be encrypted and we'll need to decrypt it.
state.neededPubkeys[hex_tag] = (
state.neededPubkeys[tag_bytes] = (
toAddress,
highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))
)

View File

@ -9,7 +9,6 @@ import re
import socket
import struct
import time
from binascii import hexlify
# magic imports!
import addresses
@ -111,16 +110,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
b"error", b"version", b"verack"):
logger.error(
'Received command %s before connection was fully'
' established, ignoring', self.command.decode('ascii', 'backslashreplace'))
' established, ignoring', self.command.decode('utf-8', 'replace'))
self.invalid = True
if not self.invalid:
try:
retval = getattr(
self, "bm_command_" + self.command.decode('ascii', 'backslashreplace').lower())()
self, "bm_command_" + self.command.decode('utf-8', 'replace').lower())()
except AttributeError as err:
logger.debug('command = {}, err = {}'.format(self.command, err))
logger.debug('command = {}, err = {}'.format(self.command.decode('utf-8', 'replace'), err))
# unimplemented command
logger.debug('unimplemented command %s', self.command.decode('ascii', 'backslashreplace'))
logger.debug('unimplemented command %s', self.command.decode('utf-8', 'replace'))
except BMProtoInsufficientDataError:
logger.debug('packet length too short, skipping')
except BMProtoExcessiveDataError:
@ -143,8 +142,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
# broken read, ignore
pass
else:
logger.debug('Closing due to invalid command %s', self.command.decode('ascii', 'backslashreplace'))
self.close_reason = "Invalid command %s" % self.command.decode('ascii', 'backslashreplace')
logger.debug('Closing due to invalid command %s', self.command.decode('utf-8', 'replace'))
self.close_reason = "Invalid command %s" % self.command.decode('utf-8', 'replace')
self.set_state("close")
return False
if retval:
@ -417,8 +416,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
else:
try:
hex_hash = hexlify(self.object.inventoryHash).decode('ascii')
del missingObjects[hex_hash]
del missingObjects[bytes(self.object.inventoryHash)]
except KeyError:
pass
@ -446,16 +444,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
"""Incoming addresses, process them"""
# not using services
for seenTime, stream, _, ip, port in self._decode_addr():
if (stream not in state.streamsInWhichIAmParticipating):
if (
stream not in state.streamsInWhichIAmParticipating
# FIXME: should check against complete list
or ip.decode('utf-8', 'replace').startswith('bootstrap')
):
continue
try:
if (
# FIXME: should check against complete list
ip.decode('ascii', 'backslashreplace').startswith('bootstrap')
):
continue
except UnicodeDecodeError:
pass
decodedIP = protocol.checkIPAddress(ip)
if (
decodedIP and time.time() - seenTime > 0
@ -532,7 +526,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
logger.debug(
'remote node incoming address: %s:%i',
self.destination.host, self.peerNode.port)
logger.debug('user agent: %s', self.userAgent.decode('utf-8', 'backslashreplace'))
logger.debug('user agent: %s', self.userAgent.decode('utf-8', 'replace'))
logger.debug('streams: [%s]', ','.join(map(str, self.streams)))
if not self.peerValidityChecks():
# ABORT afterwards
@ -540,7 +534,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.append_write_buf(protocol.CreatePacket(b'verack'))
self.verackSent = True
ua_valid = re.match(
r'^/[a-zA-Z]+:[0-9]+\.?[\w\s\(\)\./:;-]*/$', self.userAgent.decode('utf-8', 'backslashreplace'))
r'^/[a-zA-Z]+:[0-9]+\.?[\w\s\(\)\./:;-]*/$', self.userAgent.decode('utf-8', 'replace'))
if not ua_valid:
self.userAgent = b'/INVALID:0/'
if not self.isOutbound:
@ -659,8 +653,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
except KeyError:
pass
try:
hex_hash = hexlify(hashId).decode('ascii')
del missingObjects[hex_hash]
del missingObjects[bytes(hashId)]
except KeyError:
pass

View File

@ -6,7 +6,7 @@ from collections import namedtuple
from random import choice, expovariate, sample
from threading import RLock
from time import time
from binascii import hexlify, unhexlify
from binascii import hexlify
import network.connectionpool as connectionpool
import state
@ -53,8 +53,7 @@ class Dandelion: # pylint: disable=old-style-class
if not state.dandelion_enabled:
return
with self.lock:
hex_hash = hexlify(hashId).decode('ascii')
self.hashMap[hex_hash] = Stem(
self.hashMap[bytes(hashId)] = Stem(
self.getNodeStem(source),
stream,
self.poissonTimeout())
@ -65,34 +64,31 @@ class Dandelion: # pylint: disable=old-style-class
include streams, we only learn this after receiving the object)
"""
with self.lock:
hex_hash = hexlify(hashId).decode('ascii')
if hex_hash in self.hashMap:
self.hashMap[hex_hash] = Stem(
self.hashMap[hex_hash].child,
hashId_bytes = bytes(hashId)
if hashId_bytes in self.hashMap:
self.hashMap[hashId_bytes] = Stem(
self.hashMap[hashId_bytes].child,
stream,
self.poissonTimeout())
def removeHash(self, hashId, reason="no reason specified"):
"""Switch inventory vector from stem to fluff mode"""
hex_hash = hexlify(hashId).decode('ascii')
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
'%s entering fluff mode due to %s.', hex_hash, reason)
'%s entering fluff mode due to %s.', hexlify(hashId).decode(), reason)
with self.lock:
try:
del self.hashMap[hex_hash]
del self.hashMap[bytes(hashId)]
except KeyError:
pass
def hasHash(self, hashId):
"""Is inventory vector in stem mode?"""
hex_hash = hexlify(hashId).decode('ascii')
return hex_hash in self.hashMap
return bytes(hashId) in self.hashMap
def objectChildStem(self, hashId):
"""Child (i.e. next) node for an inventory vector during stem mode"""
hex_hash = hexlify(hashId).decode('ascii')
return self.hashMap[hex_hash].child
return self.hashMap[bytes(hashId)].child
def maybeAddStem(self, connection):
"""
@ -112,7 +108,7 @@ class Dandelion: # pylint: disable=old-style-class
}.items():
self.hashMap[k] = Stem(
connection, v.stream, self.poissonTimeout())
invQueue.put((v.stream, unhexlify(k), v.child))
invQueue.put((v.stream, k, v.child))
def maybeRemoveStem(self, connection):
"""
@ -173,7 +169,7 @@ class Dandelion: # pylint: disable=old-style-class
with self.lock:
deadline = time()
toDelete = [
[v.stream, unhexlify(k), v.child] for k, v in self.hashMap.items()
[v.stream, k, v.child] for k, v in self.hashMap.items()
if v.timeout < deadline
]

View File

@ -9,7 +9,6 @@ import protocol
import network.connectionpool as connectionpool
from .objectracker import missingObjects
from .threads import StoppableThread
from binascii import hexlify
class DownloadThread(StoppableThread):
@ -68,8 +67,7 @@ class DownloadThread(StoppableThread):
continue
payload.extend(chunk)
chunkCount += 1
hex_chunk = hexlify(chunk).decode('ascii')
missingObjects[hex_chunk] = now
missingObjects[bytes(chunk)] = now
if not chunkCount:
continue
payload[0:0] = addresses.encodeVarint(chunkCount)

View File

@ -3,7 +3,6 @@ Module for tracking objects
"""
import time
from threading import RLock
from binascii import hexlify
import state
import network.connectionpool as connectionpool
@ -82,27 +81,28 @@ class ObjectTracker(object):
def hasObj(self, hashid):
"""Do we already have object?"""
hashid_bytes = bytes(hashid)
if haveBloom:
return hashid in self.invBloom
return hashid in self.objectsNewToMe
return hashid_bytes in self.invBloom
return hashid_bytes in self.objectsNewToMe
def handleReceivedInventory(self, hashId):
"""Handling received inventory"""
hex_hash = hexlify(hashId).decode('ascii')
hashId_bytes = bytes(hashId)
if haveBloom:
self.invBloom.add(hex_hash)
self.invBloom.add(hashId_bytes)
try:
with self.objectsNewToThemLock:
del self.objectsNewToThem[hex_hash]
del self.objectsNewToThem[hashId_bytes]
except KeyError:
pass
if hex_hash not in missingObjects:
missingObjects[hex_hash] = time.time()
if hashId_bytes not in missingObjects:
missingObjects[hashId_bytes] = time.time()
self.objectsNewToMe[hashId] = True
def handleReceivedObject(self, streamNumber, hashid):
"""Handling received object"""
hex_hash = hexlify(hashid).decode('ascii')
hashid_bytes = bytes(hashid);
for i in connectionpool.pool.connections():
if not i.fullyEstablished:
continue
@ -113,7 +113,7 @@ class ObjectTracker(object):
not state.Dandelion.hasHash(hashid)
or state.Dandelion.objectChildStem(hashid) == i):
with i.objectsNewToThemLock:
i.objectsNewToThem[hex_hash] = time.time()
i.objectsNewToThem[hashid_bytes] = time.time()
# update stream number,
# which we didn't have when we just received the dinv
# also resets expiration of the stem mode
@ -122,7 +122,7 @@ class ObjectTracker(object):
if i == self:
try:
with i.objectsNewToThemLock:
del i.objectsNewToThem[hex_hash]
del i.objectsNewToThem[hashid_bytes]
except KeyError:
pass
self.objectsNewToMe.setLastObject()
@ -136,4 +136,4 @@ class ObjectTracker(object):
def addAddr(self, hashid):
"""WIP, should be moved to addrthread.py or removed"""
if haveBloom:
self.addrBloom.add(hashid)
self.addrBloom.add(bytes(hashid))

View File

@ -487,8 +487,7 @@ def decryptAndCheckPubkeyPayload(data, address):
encryptedData = data[readPosition:]
# Let us try to decrypt the pubkey
hex_tag = 'tag-' + hexlify(tag).decode('ascii')
toAddress, cryptorObject = state.neededPubkeys[hex_tag]
toAddress, cryptorObject = state.neededPubkeys[bytes(tag)]
if toAddress != address:
logger.critical(
'decryptAndCheckPubkeyPayload failed due to toAddress'

View File

@ -39,12 +39,10 @@ class RandomTrackingDict(object):
return self.len
def __contains__(self, key):
hex_key = hexlify(key).decode('ascii')
return hex_key in self.dictionary
return bytes(key) in self.dictionary
def __getitem__(self, key):
hex_key = hexlify(key).decode('ascii')
return self.dictionary[hex_key][1]
return self.dictionary[bytes(key)][1]
def _swap(self, i1, i2):
with self.lock:
@ -52,30 +50,28 @@ class RandomTrackingDict(object):
key2 = self.indexDict[i2]
self.indexDict[i1] = key2
self.indexDict[i2] = key1
hex_key1 = hexlify(key1).decode('ascii')
hex_key2 = hexlify(key2).decode('ascii')
self.dictionary[hex_key1][0] = i2
self.dictionary[hex_key2][0] = i1
self.dictionary[bytes(key1)][0] = i2
self.dictionary[bytes(key2)][0] = i1
# for quick reassignment
return i2
def __setitem__(self, key, value):
with self.lock:
hex_key = hexlify(key).decode('ascii')
if hex_key in self.dictionary:
self.dictionary[hex_key][1] = value
key_bytes = bytes(key)
if key_bytes in self.dictionary:
self.dictionary[key_bytes][1] = value
else:
self.indexDict.append(key)
self.dictionary[hex_key] = [self.len, value]
self.dictionary[key_bytes] = [self.len, value]
self._swap(self.len, self.len - self.pendingLen)
self.len += 1
def __delitem__(self, key):
hex_key = hexlify(key).decode('ascii')
if hex_key not in self.dictionary:
key_bytes = bytes(key)
if key_bytes not in self.dictionary:
raise KeyError
with self.lock:
index = self.dictionary[hex_key][0]
index = self.dictionary[key_bytes][0]
# not pending
if index < self.len - self.pendingLen:
# left of pending part
@ -89,7 +85,7 @@ class RandomTrackingDict(object):
# operation can improve 4x, but it's already very fast so we'll
# ignore it for the time being
del self.indexDict[-1]
del self.dictionary[hex_key]
del self.dictionary[key_bytes]
self.len -= 1
def setMaxPending(self, maxPending):

View File

@ -114,13 +114,11 @@ def reloadMyAddressHashes():
if len(privEncryptionKey) == 64:
myECCryptorObjects[hashobj] = \
highlevelcrypto.makeCryptor(privEncryptionKey)
hex_hash = hexlify(hashobj).decode('ascii')
myAddressesByHash[hex_hash] = addressInKeysFile
myAddressesByHash[bytes(hashobj)] = addressInKeysFile
tag = highlevelcrypto.double_sha512(
encodeVarint(addressVersionNumber)
+ encodeVarint(streamNumber) + hashobj)[32:]
hex_tag = hexlify(tag).decode('ascii')
myAddressesByTag[hex_tag] = addressInKeysFile
myAddressesByTag[bytes(tag)] = addressInKeysFile
if not keyfileSecure:
fixSensitiveFilePermissions(os.path.join(
@ -151,8 +149,7 @@ def reloadBroadcastSendersForWhichImWatching():
encodeVarint(addressVersionNumber)
+ encodeVarint(streamNumber) + hashobj
).digest()[:32]
hex_hash = hexlify(hashobj).decode('ascii')
MyECSubscriptionCryptorObjects[hex_hash] = \
MyECSubscriptionCryptorObjects[bytes(hashobj)] = \
highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))
else:
doubleHashOfAddressData = highlevelcrypto.double_sha512(
@ -161,8 +158,7 @@ def reloadBroadcastSendersForWhichImWatching():
)
tag = doubleHashOfAddressData[32:]
privEncryptionKey = doubleHashOfAddressData[:32]
hex_tag = hexlify(tag).decode('ascii')
MyECSubscriptionCryptorObjects[hex_tag] = \
MyECSubscriptionCryptorObjects[bytes(tag)] = \
highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))

View File

@ -70,7 +70,7 @@ class FilesystemInventory(InventoryStorage):
os.makedirs(os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii')))
hexlify(hashval).decode()))
except OSError:
pass
try:
@ -78,7 +78,7 @@ class FilesystemInventory(InventoryStorage):
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii'),
hexlify(hashval).decode(),
FilesystemInventory.metadataFilename,
),
"w",
@ -87,12 +87,12 @@ class FilesystemInventory(InventoryStorage):
value.type,
value.stream,
value.expires,
hexlify(value.tag).decode('ascii')))
hexlify(value.tag).decode()))
with open(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii'),
hexlify(hashval).decode(),
FilesystemInventory.dataFilename,
),
"wb",
@ -119,7 +119,7 @@ class FilesystemInventory(InventoryStorage):
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii'),
hexlify(hashval).decode(),
FilesystemInventory.metadataFilename))
except IOError:
pass
@ -128,7 +128,7 @@ class FilesystemInventory(InventoryStorage):
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii'),
hexlify(hashval).decode(),
FilesystemInventory.dataFilename))
except IOError:
pass
@ -136,7 +136,7 @@ class FilesystemInventory(InventoryStorage):
os.rmdir(os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode('ascii')))
hexlify(hashval).decode()))
except IOError:
pass
@ -186,7 +186,7 @@ class FilesystemInventory(InventoryStorage):
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashId).decode('ascii'),
hexlify(hashId).decode(),
FilesystemInventory.dataFilename,
),
"r",
@ -202,7 +202,7 @@ class FilesystemInventory(InventoryStorage):
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashId).decode('ascii'),
hexlify(hashId).decode(),
FilesystemInventory.metadataFilename,
),
"r",

View File

@ -4,7 +4,6 @@ Sqlite Inventory
import sqlite3
import time
from threading import RLock
from binascii import hexlify, unhexlify
from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery
from .storage import InventoryItem, InventoryStorage
@ -30,21 +29,21 @@ class SqliteInventory(InventoryStorage):
def __contains__(self, hash_):
with self.lock:
hex_hash = hexlify(hash_).decode('ascii')
if hex_hash in self._objects:
hash_bytes = bytes(hash_)
if hash_bytes in self._objects:
return True
rows = sqlQuery(
'SELECT streamnumber FROM inventory WHERE hash=?', hash_)
if not rows:
return False
self._objects[hex_hash] = rows[0][0]
self._objects[hash_bytes] = rows[0][0]
return True
def __getitem__(self, hash_):
with self.lock:
hex_hash = hexlify(hash_).decode('ascii')
if hex_hash in self._inventory:
return self._inventory[hex_hash]
hash_bytes = bytes(hash_)
if hash_bytes in self._inventory:
return self._inventory[hash_bytes]
rows = sqlQuery(
'SELECT objecttype, streamnumber, payload, expirestime, tag'
' FROM inventory WHERE hash=?', hash_)
@ -55,16 +54,16 @@ class SqliteInventory(InventoryStorage):
def __setitem__(self, hash_, value):
with self.lock:
value = InventoryItem(*value)
hex_hash = hexlify(hash_).decode('ascii')
self._inventory[hex_hash] = value
self._objects[hex_hash] = value.stream
hash_bytes = bytes(hash_)
self._inventory[hash_bytes] = value
self._objects[hash_bytes] = value.stream
def __delitem__(self, hash_):
raise NotImplementedError
def __iter__(self):
with self.lock:
hashes = map(unhexlify, self._inventory.keys()[:])
hashes = [] + self._inventory.keys()
hashes += (x for x, in sqlQuery('SELECT hash FROM inventory'))
return hashes.__iter__()
@ -96,7 +95,7 @@ class SqliteInventory(InventoryStorage):
"""Return unexpired inventory vectors filtered by stream"""
with self.lock:
t = int(time.time())
hashes = [unhexlify(x) for x, value in self._inventory.items()
hashes = [x for x, value in self._inventory.items()
if value.stream == stream and value.expires > t]
hashes += (payload for payload, in sqlQuery(
'SELECT hash FROM inventory WHERE streamnumber=?'
@ -112,7 +111,7 @@ class SqliteInventory(InventoryStorage):
for objectHash, value in self._inventory.items():
sql.execute(
'INSERT INTO inventory VALUES (?, ?, ?, ?, ?, ?)',
unhexlify(objectHash), *value)
objectHash, *value)
self._inventory.clear()
def clean(self):

View File

@ -82,12 +82,12 @@ class TestAPIThread(TestPartialRun):
proofofwork.init()
self.assertEqual(
unhexlify(self.api.disseminatePreparedObject(
hexlify(sample_object_data).decode('ascii'))),
hexlify(sample_object_data).decode())),
calculateInventoryHash(sample_object_data))
update_object = b'\x00' * 8 + pack(
'>Q', int(time.time() + 7200)) + sample_object_data[16:]
invhash = unhexlify(self.api.disseminatePreEncryptedMsg(
hexlify(update_object).decode('ascii')
hexlify(update_object).decode()
))
obj_type, obj_stream, obj_data = state.Inventory[invhash][:3]
self.assertEqual(obj_type, 42)