2019-06-26 20:13:36 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
2019-08-09 20:07:32 +00:00
|
|
|
Process block metadata with relevant actions
|
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/>.
|
|
|
|
'''
|
2019-08-09 20:07:32 +00:00
|
|
|
|
2019-06-23 07:00:27 +00:00
|
|
|
from etc import onionrvalues
|
2019-09-21 23:49:24 +00:00
|
|
|
from onionrblocks import onionrblockapi
|
2019-08-09 20:07:32 +00:00
|
|
|
from .. import epoch, bytesconverter
|
|
|
|
from coredb import blockmetadb
|
|
|
|
import logger
|
2019-09-21 22:45:46 +00:00
|
|
|
from onionrplugins import onionrevents
|
2019-08-10 04:18:30 +00:00
|
|
|
import onionrexceptions
|
|
|
|
from onionrusers import onionrusers
|
2019-11-16 04:18:38 +00:00
|
|
|
from onionrutils import updater
|
2019-06-23 07:00:27 +00:00
|
|
|
|
2019-08-09 20:07:32 +00:00
|
|
|
def process_block_metadata(blockHash: str):
|
2019-06-23 07:00:27 +00:00
|
|
|
'''
|
|
|
|
Read metadata from a block and cache it to the block database
|
2019-08-09 20:07:32 +00:00
|
|
|
|
|
|
|
blockHash -> sha3_256 hex formatted hash of Onionr block
|
2019-06-23 07:00:27 +00:00
|
|
|
'''
|
2019-06-25 08:21:36 +00:00
|
|
|
curTime = epoch.get_rounded_epoch(roundS=60)
|
2019-07-19 19:49:56 +00:00
|
|
|
myBlock = onionrblockapi.Block(blockHash)
|
2019-06-23 07:00:27 +00:00
|
|
|
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
|
|
|
|
2019-06-25 23:07:35 +00:00
|
|
|
signer = bytesconverter.bytes_to_str(myBlock.signer)
|
2019-06-23 07:00:27 +00:00
|
|
|
valid = myBlock.verifySig()
|
2019-08-15 08:36:05 +00:00
|
|
|
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')))
|
2019-06-23 07:00:27 +00:00
|
|
|
|
|
|
|
try:
|
2019-08-09 20:07:32 +00:00
|
|
|
if len(blockType) <= onionrvalues.MAX_BLOCK_TYPE_LENGTH:
|
2019-07-17 16:58:40 +00:00
|
|
|
blockmetadb.update_block_info(blockHash, 'dataType', blockType)
|
2019-06-23 07:00:27 +00:00
|
|
|
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'))
|
2019-09-09 00:21:36 +00:00
|
|
|
# test that expire time is an integer of sane length (for epoch)
|
2019-09-22 08:28:38 +00:00
|
|
|
# doesn't matter if its too large because of the min() func below
|
2019-09-09 00:21:36 +00:00
|
|
|
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
|
2019-06-23 07:00:27 +00:00
|
|
|
finally:
|
2019-09-04 06:20:11 +00:00
|
|
|
expireTime = min(expireTime, curTime + onionrvalues.DEFAULT_EXPIRE)
|
2019-07-17 16:58:40 +00:00
|
|
|
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})
|