2019-06-22 22:54:41 +00:00
|
|
|
import sys, sqlite3
|
2019-07-20 15:52:03 +00:00
|
|
|
import onionrstorage, onionrexceptions, onionrcrypto as crypto
|
2019-09-21 23:49:24 +00:00
|
|
|
import filepaths
|
|
|
|
from onionrblocks import storagecounter
|
2019-07-19 19:49:56 +00:00
|
|
|
from coredb import dbfiles
|
2019-09-05 09:40:31 +00:00
|
|
|
from onionrutils import blockmetadata, bytesconverter
|
2019-09-08 09:48:16 +00:00
|
|
|
def set_data(data)->str:
|
2019-06-22 22:54:41 +00:00
|
|
|
'''
|
|
|
|
Set the data assciated with a hash
|
|
|
|
'''
|
2019-07-19 19:49:56 +00:00
|
|
|
storage_counter = storagecounter.StorageCounter()
|
2019-06-22 22:54:41 +00:00
|
|
|
data = data
|
|
|
|
dataSize = sys.getsizeof(data)
|
2019-09-05 09:40:31 +00:00
|
|
|
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)
|
2019-06-22 22:54:41 +00:00
|
|
|
|
|
|
|
if not type(data) is bytes:
|
|
|
|
data = data.encode()
|
|
|
|
|
2019-07-20 15:52:03 +00:00
|
|
|
dataHash = crypto.hashers.sha3_hash(data)
|
2019-06-22 22:54:41 +00:00
|
|
|
|
|
|
|
if type(dataHash) is bytes:
|
|
|
|
dataHash = dataHash.decode()
|
2019-07-19 19:49:56 +00:00
|
|
|
blockFileName = filepaths.block_data_location + dataHash + '.dat'
|
2019-06-22 22:54:41 +00:00
|
|
|
try:
|
2019-07-19 19:49:56 +00:00
|
|
|
onionrstorage.getData(dataHash)
|
2019-06-22 22:54:41 +00:00
|
|
|
except onionrexceptions.NoDataAvailable:
|
2019-09-08 09:48:16 +00:00
|
|
|
if storage_counter.add_bytes(dataSize) != False:
|
2019-07-19 19:49:56 +00:00
|
|
|
onionrstorage.store(data, blockHash=dataHash)
|
|
|
|
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
|
2019-06-22 22:54:41 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
c.execute("UPDATE hashes SET dataSaved=1 WHERE hash = ?;", (dataHash,))
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
2019-07-19 19:49:56 +00:00
|
|
|
with open(filepaths.data_nonce_file, 'a') as nonceFile:
|
2019-09-05 09:40:31 +00:00
|
|
|
nonceFile.write(nonce_hash + '\n')
|
2019-06-22 22:54:41 +00:00
|
|
|
else:
|
|
|
|
raise onionrexceptions.DiskAllocationReached
|
|
|
|
else:
|
2019-06-29 18:18:31 +00:00
|
|
|
raise onionrexceptions.DataExists("Data is already set for " + dataHash)
|
2019-06-22 22:54:41 +00:00
|
|
|
|
|
|
|
return dataHash
|