2021-01-31 04:40:51 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Handle commands for the torgossip server
|
|
|
|
"""
|
2021-02-05 04:38:57 +00:00
|
|
|
import traceback
|
|
|
|
import base64
|
|
|
|
|
2021-01-27 05:34:31 +00:00
|
|
|
import blockio
|
2021-02-05 04:38:57 +00:00
|
|
|
import logger
|
2021-01-27 05:34:31 +00:00
|
|
|
|
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-02-05 04:38:57 +00:00
|
|
|
def announce_peer(peers: 'TorGossipPeers', peer_address):
|
|
|
|
"""command 8: accept a new peer"""
|
|
|
|
try:
|
|
|
|
peers.add_peer(peer_address)
|
|
|
|
except Exception as _:
|
|
|
|
logger.warn("Error accepting announced peer " + base64.b85encode(peer_address).decode('utf-8'), terminal=True)
|
|
|
|
logger.warn(traceback.format_exc(), terminal=True)
|
|
|
|
return b"0"
|
|
|
|
return b"1"
|
|
|
|
|
|
|
|
|
|
|
|
def peer_exchange(peers: 'TorGossipPeers', num_of_peers: bytes):
|
2021-02-05 02:19:23 +00:00
|
|
|
"""command 7: exchange a number of our top performing peers"""
|
|
|
|
num_of_peers = int(chr(int.from_bytes(num_of_peers, 'little')))
|
|
|
|
peers = peers.get_highest_score_peers(num_of_peers)
|
|
|
|
just_addresses = []
|
|
|
|
for i in peers:
|
|
|
|
just_addresses.append(i[0])
|
|
|
|
return b''.join(just_addresses)
|
2021-02-02 23:24:35 +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
|
2021-02-05 02:19:23 +00:00
|
|
|
except Exception as _: # noqa
|
2021-01-31 04:40:51 +00:00
|
|
|
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-01-27 05:34:31 +00:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2021-01-27 05:34:31 +00:00
|
|
|
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
|
|
|
|
2021-01-27 05:34:31 +00:00
|
|
|
try:
|
2021-01-31 04:40:51 +00:00
|
|
|
return safe_db.get('bl-' + block_type)
|
2021-01-27 05:34:31 +00:00
|
|
|
except KeyError:
|
2021-01-31 04:40:51 +00:00
|
|
|
return b"0"
|
|
|
|
|
2021-01-27 05:34:31 +00:00
|
|
|
|
|
|
|
def handle_check_block(safe_db, block_hash):
|
2021-01-31 04:40:51 +00:00
|
|
|
# 2
|
2021-01-27 05:34:31 +00:00
|
|
|
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')
|