Onionr/src/oldblocks/blockmetadata/process.py

72 lines
3.1 KiB
Python
Raw Normal View History

2020-11-02 01:31:11 +00:00
"""Onionr - Private P2P Communication.
2019-06-26 20:13:36 +00:00
2020-11-02 01:31:11 +00:00
Process block metadata with relevant actions
"""
from etc import onionrvalues
from oldblocks import onionrblockapi
2020-11-02 01:31:11 +00:00
from onionrutils import epoch, bytesconverter
from coredb import blockmetadb
import logger
from onionrplugins import onionrevents
import onionrexceptions
from onionrusers import onionrusers
from onionrutils import updater
"""
2019-06-26 20:13:36 +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-11-02 01:31:11 +00:00
"""
def process_block_metadata(blockHash: str):
2020-11-02 01:31:11 +00:00
"""
Read metadata from a block and cache it to the block database.
2020-02-26 02:30:04 +00:00
2020-11-02 01:31:11 +00:00
blockHash -> sha3_256 hex formatted hash of Onionr block
"""
curTime = epoch.get_rounded_epoch(roundS=60)
2019-07-19 19:49:56 +00:00
myBlock = onionrblockapi.Block(blockHash)
if myBlock.isEncrypted:
myBlock.decrypt()
if (myBlock.isEncrypted and myBlock.decrypted) or (not myBlock.isEncrypted):
blockType = myBlock.getMetadata('type') # we would use myBlock.getType() here, but it is bugged with encrypted blocks
2019-06-26 04:48:24 +00:00
signer = bytesconverter.bytes_to_str(myBlock.signer)
valid = myBlock.verifySig()
if valid:
if myBlock.getMetadata('newFSKey') is not None:
try:
onionrusers.OnionrUser(signer).addForwardKey(myBlock.getMetadata('newFSKey'))
except onionrexceptions.InvalidPubkey:
logger.warn('%s has invalid forward secrecy key to add: %s' % (signer, myBlock.getMetadata('newFSKey')))
2020-02-26 02:30:04 +00:00
try:
if len(blockType) <= onionrvalues.MAX_BLOCK_TYPE_LENGTH:
blockmetadb.update_block_info(blockHash, 'dataType', blockType)
except TypeError:
logger.warn("Missing block information")
pass
# Set block expire time if specified
try:
2019-09-04 06:20:11 +00:00
expireTime = int(myBlock.getHeader('expire'))
# test that expire time is an integer of sane length (for epoch)
# doesn't matter if its too large because of the min() func below
if not len(str(expireTime)) < 20: raise ValueError('timestamp invalid')
except (ValueError, TypeError) as e:
2019-08-09 20:41:27 +00:00
expireTime = onionrvalues.DEFAULT_EXPIRE + curTime
finally:
2019-09-04 06:20:11 +00:00
expireTime = min(expireTime, curTime + onionrvalues.DEFAULT_EXPIRE)
blockmetadb.update_block_info(blockHash, 'expire', expireTime)
2019-09-04 06:20:11 +00:00
2019-11-16 04:18:38 +00:00
if blockType == 'update': updater.update_event(myBlock)
2019-07-27 02:42:55 +00:00
onionrevents.event('processblocks', data = {'block': myBlock, 'type': blockType, 'signer': signer, 'validSig': valid})