Onionr/src/coredb/blockmetadb/add.py

50 lines
1.6 KiB
Python
Raw Normal View History

2020-03-03 11:55:50 +00:00
"""Onionr - Private P2P Communication.
2019-06-26 05:26:50 +00:00
2020-03-03 11:55:50 +00:00
Add an entry to the block metadata database
"""
import sqlite3
import secrets
2020-11-02 01:31:11 +00:00
from onionrutils import epoch
from oldblocks import blockmetadata
2020-03-03 11:55:50 +00:00
from etc import onionrvalues
from .. import dbfiles
from onionrexceptions import BlockMetaEntryExists
"""
2019-06-26 05:26:50 +00:00
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-03-03 11:55:50 +00:00
"""
def add_to_block_DB(newHash, selfInsert=False, dataSaved=False):
2020-03-03 11:55:50 +00:00
"""
2019-06-22 22:54:41 +00:00
Add a hash value to the block db
Should be in hex format!
2020-03-03 11:55:50 +00:00
"""
2019-06-22 22:54:41 +00:00
if blockmetadata.has_block(newHash):
raise BlockMetaEntryExists
2020-11-02 01:31:11 +00:00
conn = sqlite3.connect(
dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
2019-06-22 22:54:41 +00:00
c = conn.cursor()
currentTime = epoch.get_epoch() + secrets.randbelow(61)
2019-06-22 22:54:41 +00:00
if selfInsert or dataSaved:
selfInsert = 1
else:
selfInsert = 0
data = (newHash, currentTime, '', selfInsert)
2020-11-02 01:31:11 +00:00
c.execute(
'INSERT INTO hashes (hash, dateReceived, dataType, dataSaved) VALUES(?, ?, ?, ?);', data)
2019-06-22 22:54:41 +00:00
conn.commit()
2020-03-03 11:55:50 +00:00
conn.close()