Onionr/static-data/default-plugins/torgossip/commandhandlers.py

80 lines
2.0 KiB
Python
Raw Normal View History

2021-01-31 04:40:51 +00:00
"""Onionr - Private P2P Communication.
Handle commands for the torgossip server
"""
2021-02-01 06:59:53 +00:00
from typing import type_check_only
2021-01-31 04:40:51 +00:00
from onionrblocks import generators
from onionrblocks.generators import anonvdf
import blockio
2021-01-31 04:40:51 +00:00
import onionrblocks
from kasten import Kasten
"""
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/>.
"""
2021-02-01 06:59:53 +00:00
2021-01-31 04:40:51 +00:00
def put_block(safe_db, block):
2021-02-01 06:59:53 +00:00
#6
2021-01-31 04:40:51 +00:00
block_hash = block[:64]
data = block[64:]
try:
blockio.store_block(
Kasten(block_hash, data, onionrblocks.generators.AnonVDFGenerator),
safe_db)
except ValueError:
pass
except Exception as e:
return b"0"
return b"1"
def get_block(safe_db, block_hash) -> bytes:
2021-02-01 06:59:53 +00:00
#5
2021-01-31 04:40:51 +00:00
try:
return safe_db.get(block_hash)
except KeyError:
return b"0"
2021-02-01 06:59:53 +00:00
def list_blocks_by_type_and_offset(safe_db, type_and_offset):
#4
offset, block_type = type_and_offset.split(b',', 1)
try:
offset = int(offset)
except ValueError:
return b""
try:
return list_blocks_by_type(safe_db, block_type)[offset * 64:]
except KeyError:
return b"0"
def list_blocks_by_type(safe_db, block_type) -> bytes:
2021-01-31 04:40:51 +00:00
# 3
block_type = block_type.decode('utf-8')
2021-02-01 06:59:53 +00:00
try:
2021-01-31 04:40:51 +00:00
return safe_db.get('bl-' + block_type)
except KeyError:
2021-01-31 04:40:51 +00:00
return b"0"
def handle_check_block(safe_db, block_hash):
2021-01-31 04:40:51 +00:00
# 2
if block_hash in blockio.list_all_blocks(safe_db):
return int(1).to_bytes(1, 'little')
else:
return int(2).to_bytes(1, 'little')