corrected onionrstorage formatting

master
Kevin Froman 2020-03-26 03:57:54 -05:00
parent 16ae16f042
commit 012ee33499
3 changed files with 28 additions and 21 deletions

View File

@ -1,8 +1,8 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
This file handles block storage, providing an abstraction for storing blocks between file system and database Handle block storage, providing an abstraction for
''' storing blocks between file system and database
"""
import sys import sys
import sqlite3 import sqlite3
import os import os
@ -10,12 +10,10 @@ from onionrutils import bytesconverter
from onionrutils import stringvalidators from onionrutils import stringvalidators
from coredb import dbfiles from coredb import dbfiles
import filepaths import filepaths
import onionrcrypto
import onionrexceptions import onionrexceptions
from onionrsetup import dbcreator
from onionrcrypto import hashers from onionrcrypto import hashers
from . import setdata from . import setdata
''' """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -28,7 +26,7 @@ from . import setdata
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/>.
''' """
DB_ENTRY_SIZE_LIMIT = 10000 # Will be a config option DB_ENTRY_SIZE_LIMIT = 10000 # Will be a config option

View File

@ -2,8 +2,11 @@
remove onionr block from meta db remove onionr block from meta db
""" """
import sys, sqlite3 import sys
import onionrexceptions, onionrstorage import sqlite3
import onionrexceptions
import onionrstorage
from onionrutils import stringvalidators from onionrutils import stringvalidators
from coredb import dbfiles from coredb import dbfiles
from onionrblocks import storagecounter from onionrblocks import storagecounter
@ -24,12 +27,10 @@ from onionrblocks import storagecounter
def remove_block(block): def remove_block(block):
""" """remove a block from this node (does not automatically blacklist).
remove a block from this node (does not automatically blacklist)
**You may want blacklist.addToDB(blockHash) **You may want blacklist.addToDB(blockHash)
""" """
if stringvalidators.validate_hash(block): if stringvalidators.validate_hash(block):
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
c = conn.cursor() c = conn.cursor()

View File

@ -5,7 +5,9 @@ Test Onionr as it is running
import sys import sys
import sqlite3 import sqlite3
import onionrstorage, onionrexceptions, onionrcrypto as crypto import onionrstorage
import onionrexceptions
import onionrcrypto as crypto
import filepaths import filepaths
from onionrblocks import storagecounter from onionrblocks import storagecounter
from coredb import dbfiles from coredb import dbfiles
@ -24,12 +26,16 @@ from onionrutils import blockmetadata, bytesconverter
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/>.
""" """
def set_data(data) -> str: def set_data(data) -> str:
"""Set the data assciated with a hash.""" """Set the data assciated with a hash."""
storage_counter = storagecounter.StorageCounter() storage_counter = storagecounter.StorageCounter()
data = data data = data
dataSize = sys.getsizeof(data) dataSize = sys.getsizeof(data)
nonce_hash = crypto.hashers.sha3_hash(bytesconverter.str_to_bytes(blockmetadata.fromdata.get_block_metadata_from_data(data)[2])) nonce_hash = crypto.hashers.sha3_hash(
bytesconverter.str_to_bytes(
blockmetadata.fromdata.get_block_metadata_from_data(data)[2]))
nonce_hash = bytesconverter.bytes_to_str(nonce_hash) nonce_hash = bytesconverter.bytes_to_str(nonce_hash)
if not type(data) is bytes: if not type(data) is bytes:
@ -39,15 +45,16 @@ def set_data(data)->str:
if type(dataHash) is bytes: if type(dataHash) is bytes:
dataHash = dataHash.decode() dataHash = dataHash.decode()
blockFileName = filepaths.block_data_location + dataHash + '.dat'
try: try:
onionrstorage.getData(dataHash) onionrstorage.getData(dataHash)
except onionrexceptions.NoDataAvailable: except onionrexceptions.NoDataAvailable:
if storage_counter.add_bytes(dataSize) != False: if storage_counter.add_bytes(dataSize) is not False:
onionrstorage.store(data, blockHash=dataHash) onionrstorage.store(data, blockHash=dataHash)
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
c = conn.cursor() c = conn.cursor()
c.execute("UPDATE hashes SET dataSaved=1 WHERE hash = ?;", (dataHash,)) c.execute(
"UPDATE hashes SET dataSaved=1 WHERE hash = ?;",
(dataHash,))
conn.commit() conn.commit()
conn.close() conn.close()
with open(filepaths.data_nonce_file, 'a') as nonceFile: with open(filepaths.data_nonce_file, 'a') as nonceFile:
@ -55,6 +62,7 @@ def set_data(data)->str:
else: else:
raise onionrexceptions.DiskAllocationReached raise onionrexceptions.DiskAllocationReached
else: else:
raise onionrexceptions.DataExists("Data is already set for " + dataHash) raise onionrexceptions.DataExists(
"Data is already set for " + dataHash)
return dataHash return dataHash