Private Decentralized Communication Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

61 lines
2.3 KiB

  1. """Onionr - Private P2P Communication.
  2. Test Onionr as it is running
  3. """
  4. import sys
  5. import sqlite3
  6. import onionrstorage, onionrexceptions, onionrcrypto as crypto
  7. import filepaths
  8. from onionrblocks import storagecounter
  9. from coredb import dbfiles
  10. from onionrutils import blockmetadata, bytesconverter
  11. """
  12. This program is free software: you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation, either version 3 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. """
  23. def set_data(data)->str:
  24. """Set the data assciated with a hash."""
  25. storage_counter = storagecounter.StorageCounter()
  26. data = data
  27. dataSize = sys.getsizeof(data)
  28. nonce_hash = crypto.hashers.sha3_hash(bytesconverter.str_to_bytes(blockmetadata.fromdata.get_block_metadata_from_data(data)[2]))
  29. nonce_hash = bytesconverter.bytes_to_str(nonce_hash)
  30. if not type(data) is bytes:
  31. data = data.encode()
  32. dataHash = crypto.hashers.sha3_hash(data)
  33. if type(dataHash) is bytes:
  34. dataHash = dataHash.decode()
  35. blockFileName = filepaths.block_data_location + dataHash + '.dat'
  36. try:
  37. onionrstorage.getData(dataHash)
  38. except onionrexceptions.NoDataAvailable:
  39. if storage_counter.add_bytes(dataSize) != False:
  40. onionrstorage.store(data, blockHash=dataHash)
  41. conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
  42. c = conn.cursor()
  43. c.execute("UPDATE hashes SET dataSaved=1 WHERE hash = ?;", (dataHash,))
  44. conn.commit()
  45. conn.close()
  46. with open(filepaths.data_nonce_file, 'a') as nonceFile:
  47. nonceFile.write(nonce_hash + '\n')
  48. else:
  49. raise onionrexceptions.DiskAllocationReached
  50. else:
  51. raise onionrexceptions.DataExists("Data is already set for " + dataHash)
  52. return dataHash