Skip to content

Commit

Permalink
Add sqlite functions while versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
cis-muzahid committed Apr 26, 2021
1 parent f5fba7d commit 49f9620
Show file tree
Hide file tree
Showing 19 changed files with 638 additions and 405 deletions.
26 changes: 20 additions & 6 deletions src/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Operations with addresses
"""
# pylint: disable=redefined-outer-name,inconsistent-return-statements
import sys
import hashlib
import logging
from binascii import hexlify, unhexlify
Expand Down Expand Up @@ -149,18 +150,31 @@ def encodeAddress(version, stream, ripe):
'Programming error in encodeAddress: The length of'
' a given ripe hash was not 20.'
)
if ripe[:2] == b'\x00\x00':
ripe = ripe[2:]
elif ripe[:1] == b'\x00':
ripe = ripe[1:]

if isinstance(ripe, str):
if ripe[:2] == '\x00\x00':
ripe = ripe[2:]
elif ripe[:1] == '\x00':
ripe = ripe[1:]
else:
if ripe[:2] == b'\x00\x00':
ripe = ripe[2:]
elif ripe[:1] == b'\x00':
ripe = ripe[1:]

This comment has been minimized.

Copy link
@g1itch

g1itch Jul 29, 2021

Collaborator

Bullshit!

elif version == 4:
if len(ripe) != 20:
raise Exception(
'Programming error in encodeAddress: The length of'
' a given ripe hash was not 20.')
ripe = ripe.lstrip(b'\x00')
ripe = ripe.lstrip('\x00')

storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe
if sys.version_info[0] == 3:
if isinstance(ripe, str):
storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe.encode('utf-8')
else:
storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe
else:
storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe

# Generate the checksum
sha = hashlib.new('sha512')
Expand Down
616 changes: 309 additions & 307 deletions src/bitmessagecli.py

Large diffs are not rendered by default.

134 changes: 85 additions & 49 deletions src/bmconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@

@Singleton
class BMConfigParser(SafeConfigParser):

"""
Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
with additional methods specific to bitmessage config.
Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
with additional methods specific to bitmessage config.
"""
# pylint: disable=too-many-ancestors

_temp = {}

def set(self, section, option, value=None):
if self._optcre is self.OPTCRE or value:
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError("option values must be strings")

This comment has been minimized.

Copy link
@g1itch
if not self.validate(section, option, value):
raise ValueError("Invalid value %s" % value)
Expand All @@ -73,20 +71,20 @@ def set(self, section, option, value=None):
def get(self, section, option, raw=False, vars=None):
if sys.version_info[0] == 3:
# pylint: disable=arguments-differ
try:
try:
if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get(
self, section, option)
self, section, option, raw=True, vars=vars)
try:
return self._temp[section][option]
except KeyError:
pass
return ConfigParser.ConfigParser.get(
self, section, option)
except ConfigParser.InterpolationError:
self, section, option, raw=True, vars=vars)
except ConfigParser.InterpolationError:
return ConfigParser.ConfigParser.get(
self, section, option)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
self, section, option, raw=True, vars=vars)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
try:
return BMConfigDefaults[section][option]
except (KeyError, ValueError, AttributeError):
Expand Down Expand Up @@ -122,6 +120,10 @@ def setTemp(self, section, option, value=None):
def safeGetBoolean(self, section, field):
"""Return value as boolean, False on exceptions"""
try:
# Used in the python2.7
# return self.getboolean(section, field)
# Used in the python3.5.2
# print(config, section, field)
return self.getboolean(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
ValueError, AttributeError):
Expand All @@ -131,7 +133,10 @@ def safeGetInt(self, section, field, default=0):
"""Return value as integer, default on exceptions,
0 if default missing"""
try:
return self.getint(section, field)
# Used in the python2.7
# return self.getint(section, field)
# Used in the python3.7.0
return int(self.get(section, field))
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
ValueError, AttributeError):
return default
Expand All @@ -145,43 +150,71 @@ def safeGet(self, section, option, default=None):
return default

def items(self, section, raw=False, variables=None):
# pylint: disable=signature-differs
"""Return section variables as parent,
but override the "raw" argument to always True"""
# pylint: disable=arguments-differ
return ConfigParser.ConfigParser.items(self, section, True, variables)

@staticmethod
def addresses():
"""Return a list of local bitmessage addresses (from section labels)"""
return [
x for x in BMConfigParser().sections() if x.startswith('BM-')]

def _reset(self):
"""Reset current config. There doesn't appear to be a built in
method for this"""
sections = self.sections()
for x in sections:
self.remove_section(x)

def read(self, filenames):
"""Read config and populate defaults"""
self._reset()
ConfigParser.ConfigParser.read(self, filenames)
for section in self.sections():
for option in self.options(section):
try:
if not self.validate(
section, option,
ConfigParser.ConfigParser.get(self, section, option)
):
try:
newVal = BMConfigDefaults[section][option]
except KeyError:
continue
ConfigParser.ConfigParser.set(
self, section, option, newVal)
except ConfigParser.InterpolationError:
continue
if sys.version_info[0] == 3:
@staticmethod
def addresses(hidden=False):
"""Return a list of local bitmessage addresses (from section labels)"""
return [x for x in BMConfigParser().sections() if x.startswith('BM-') and (
hidden or not BMConfigParser().safeGetBoolean(x, 'hidden'))]

def read(self, filenames):
ConfigParser.ConfigParser.read(self, filenames)
for section in self.sections():
for option in self.options(section):
try:
if not self.validate(
section, option,
self[section][option]
):
try:
newVal = BMConfigDefaults[section][option]
except ConfigParser.NoSectionError:
continue
except KeyError:
continue
ConfigParser.ConfigParser.set(
self, section, option, newVal)
except ConfigParser.InterpolationError:
continue

else:
@staticmethod
def addresses():
"""Return a list of local bitmessage addresses (from section labels)"""
return [
x for x in BMConfigParser().sections() if x.startswith('BM-')]

def _reset(self):
"""Reset current config. There doesn't appear to be a built in
method for this"""
sections = self.sections()
for x in sections:
self.remove_section(x)

def read(self, filenames):
"""Read config and populate defaults"""
self._reset()
ConfigParser.ConfigParser.read(self, filenames)
for section in self.sections():
for option in self.options(section):
try:
if not self.validate(
section, option,
ConfigParser.ConfigParser.get(self, section, option)
):
try:
newVal = BMConfigDefaults[section][option]
except KeyError:
continue
ConfigParser.ConfigParser.set(
self, section, option, newVal)
except ConfigParser.InterpolationError:
continue

def save(self):
"""Save the runtime config onto the filesystem"""
Expand All @@ -198,8 +231,8 @@ def save(self):
# The backup failed. This can happen if the file
# didn't exist before.
fileNameExisted = False
# write the file
with open(fileName, 'wb') as configfile:

with open(fileName, 'w') as configfile:
self.write(configfile)
# delete the backup
if fileNameExisted:
Expand All @@ -208,7 +241,11 @@ def save(self):
def validate(self, section, option, value):
"""Input validator interface (using factory pattern)"""
try:
return getattr(self, 'validate_%s_%s' % (section, option))(value)
if sys.version_info[0] == 3:
return getattr(self, 'validate_{}_{}'.format(
section, option))(value)
else:
return getattr(self, 'validate_%s_%s' % (section, option))(value)
except AttributeError:
return True

Expand All @@ -222,4 +259,3 @@ def validate_bitmessagesettings_maxoutboundconnections(value):
if value < 0 or value > 8:
return False
return True

59 changes: 40 additions & 19 deletions src/class_sqlThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@
import threading
import time

import helper_sql
import helper_startup
import paths
import queues
import state
import tr
from bmconfigparser import BMConfigParser
from debug import logger
# pylint: disable=attribute-defined-outside-init,protected-access
if sys.version_info[0] == 3:
from . import helper_sql
from . import helper_startup
from . import paths
from . import queues
from . import state
from . import tr
from .bmconfigparser import BMConfigParser
from .debug import logger
# pylint: disable=attribute-defined-outside-init,protected-access
from .addresses import encodeAddress
else:
import helper_sql
import helper_startup
import paths
import queues
import state
import tr
from bmconfigparser import BMConfigParser
from debug import logger
# pylint: disable=attribute-defined-outside-init,protected-access
from addresses import encodeAddress


class sqlThread(threading.Thread):
Expand All @@ -35,6 +48,9 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s

self.cur.execute('PRAGMA secure_delete = true')

# call create_function for encode address
self.create_function()

try:
self.cur.execute(
'''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,'''
Expand Down Expand Up @@ -325,6 +341,7 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s

# We'll also need a `sleeptill` field and a `ttl` field. Also we
# can combine the pubkeyretrynumber and msgretrynumber into one.

item = '''SELECT value FROM settings WHERE key='version';'''
parameters = ''
self.cur.execute(item, parameters)
Expand Down Expand Up @@ -358,16 +375,11 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s
logger.debug('In messages.dat database, adding address field to the pubkeys table.')
# We're going to have to calculate the address for each row in the pubkeys
# table. Then we can take out the hash field.
self.cur.execute('''ALTER TABLE pubkeys ADD address text DEFAULT '' ''')
self.cur.execute('''SELECT hash, addressversion FROM pubkeys''')
queryResult = self.cur.fetchall()
from addresses import encodeAddress
for row in queryResult:
addressHash, addressVersion = row
address = encodeAddress(addressVersion, 1, hash)
item = '''UPDATE pubkeys SET address=? WHERE hash=?;'''
parameters = (address, addressHash)
self.cur.execute(item, parameters)
self.cur.execute('''ALTER TABLE pubkeys ADD address text DEFAULT '' ;''')

# replica for loop to update hashed address
self.cur.execute('''UPDATE pubkeys SET address=(enaddr(pubkeys.addressversion, 1, hash)); ''')

# Now we can remove the hash field from the pubkeys table.
self.cur.execute(
'''CREATE TEMPORARY TABLE pubkeys_backup'''
Expand Down Expand Up @@ -622,3 +634,12 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s

helper_sql.sqlReturnQueue.put((self.cur.fetchall(), rowcount))
# helper_sql.sqlSubmitQueue.task_done()

def create_function(self):
# create_function
try:
self.conn.create_function("enaddr", 3, func=encodeAddress, deterministic=True)
except (TypeError, sqlite3.NotSupportedError) as err:
logger.debug(
"Got error while pass deterministic in sqlite create function {}, Passing 3 params".format(err))
self.conn.create_function("enaddr", 3, encodeAddress)
21 changes: 17 additions & 4 deletions src/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@
just import and log.
"""

import ConfigParser
# import ConfigParser
import sys
if sys.version_info[0] == 3:
# python 3
import configparser as ConfigParser
else:
# python 2
import ConfigParser

import logging
import logging.config
import os
import sys

import helper_startup
import state
if sys.version_info[0] == 3:
from . import helper_startup
from . import state
else:
import helper_startup
import state

helper_startup.loadConfig()

Expand Down Expand Up @@ -74,7 +86,7 @@ def configureLogging():
False,
'Loaded logger configuration from %s' % logging_config
)
except (OSError, ConfigParser.NoSectionError):
except (OSError, ConfigParser.NoSectionError, KeyError):
if os.path.isfile(logging_config):
fail_msg = \
'Failed to load logger configuration from %s, using default' \
Expand Down Expand Up @@ -149,6 +161,7 @@ def resetLogging():


# !

preconfigured, msg = configureLogging()
logger = logging.getLogger('default')
if msg:
Expand Down
Loading

0 comments on commit 49f9620

Please sign in to comment.