Deduplicate infoNumbers in transportinfo keydb

master
Kevin Froman 2020-08-17 19:03:21 -05:00
parent fb4dd07ba4
commit 0938bf0692
1 changed files with 20 additions and 11 deletions

View File

@ -19,11 +19,21 @@ from etc import onionrvalues
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
info_numbers = {
'address': 0,
'type': 1,
'knownPeer': 2,
'speed': 3,
'success': 4,
'powValue': 5,
'failure': 6,
'lastConnect': 7,
'trust': 8,
'introduced': 9}
def get_address_info(address, info): def get_address_info(address, info):
""" """Get info about an address from its database entry.
Get info about an address from its database entry
address text, 0 address text, 0
type int, 1 type int, 1
@ -36,24 +46,23 @@ def get_address_info(address, info):
trust 8 trust 8
introduced 9 introduced 9
""" """
conn = sqlite3.connect( conn = sqlite3.connect(
dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT) dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (address,) command = (address,)
infoNumbers = {'address': 0, 'type': 1, 'knownPeer': 2, 'speed': 3, 'success': 4, 'powValue': 5, 'failure': 6, 'lastConnect': 7, 'trust': 8, 'introduced': 9}
info = infoNumbers[info] info = info_numbers[info]
iterCount = 0 iter_count = 0
retVal = '' retVal = ''
for row in c.execute('SELECT * FROM adders WHERE address=?;', command): for row in c.execute('SELECT * FROM adders WHERE address=?;', command):
for i in row: for i in row:
if iterCount == info: if iter_count == info:
retVal = i retVal = i
break break
else: else:
iterCount += 1 iter_count += 1
conn.close() conn.close()
return retVal return retVal
@ -61,15 +70,15 @@ def get_address_info(address, info):
def set_address_info(address, key, data): def set_address_info(address, key, data):
"""Update an address for a key.""" """Update an address for a key."""
conn = sqlite3.connect( conn = sqlite3.connect(
dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT) dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (data, address) command = (data, address)
if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'failure', 'powValue', 'lastConnect', 'lastConnectAttempt', 'trust', 'introduced'): if key not in info_numbers.keys():
raise ValueError("Got invalid database key when setting address info, must be in whitelist") raise ValueError(
"Got invalid database key when setting address info, must be in whitelist")
else: else:
c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command) c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command)
conn.commit() conn.commit()