Onionr/static-data/default-plugins/torgossip/clientfuncs/__init__.py

64 lines
2.0 KiB
Python
Raw Normal View History

2021-02-08 07:32:31 +00:00
from typing import TYPE_CHECKING
2021-02-09 23:02:19 +00:00
import sys
import os
2021-02-16 05:01:01 +00:00
from kasten import Kasten
from kasten.generator import pack
2021-02-09 23:02:19 +00:00
import logger
2021-02-16 05:01:01 +00:00
from blockio import store_block, subprocvalidate, list_all_blocks
2021-02-09 23:02:19 +00:00
import onionrblocks
2021-02-16 05:01:01 +00:00
from onionrblocks.exceptions import BlockExpired
2021-02-09 23:02:19 +00:00
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from commands import GossipCommands
2021-02-08 07:32:31 +00:00
if TYPE_CHECKING:
from socket import socket
2021-02-20 00:41:46 +00:00
def download_blocks(
safe_db, sock: 'socket', offset: int, block_type: str) -> int:
2021-02-09 23:02:19 +00:00
sock.sendall(
2021-02-16 05:01:01 +00:00
str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode('utf-8') +
str(offset).encode('utf-8') + b',' +
block_type.encode('utf-8'))
2021-02-09 23:02:19 +00:00
bl_hashs = sock.recv(600000)
2021-02-16 05:01:01 +00:00
existing_blocks = list_all_blocks(safe_db)
existing_blocks_hashes = b''
2021-02-20 00:41:46 +00:00
downloaded_total = 0 # Including non-succesful
2021-02-16 05:01:01 +00:00
for i in existing_blocks:
for x in i:
existing_blocks_hashes += int(x).to_bytes(1, 'little')
print('existing', existing_blocks_hashes)
2021-02-09 23:02:19 +00:00
hash = None
for i in range(len(bl_hashs)//64):
2021-02-20 00:41:46 +00:00
downloaded_total += 1
2021-02-15 09:11:16 +00:00
hash = bl_hashs[:(i*64) + 64]
2021-02-16 05:01:01 +00:00
if hash in existing_blocks_hashes:
continue
2021-02-09 23:02:19 +00:00
sock.sendall(
2021-02-15 09:11:16 +00:00
str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash)
2021-02-09 23:02:19 +00:00
bl_content = sock.recv(10**6)
2021-02-16 05:01:01 +00:00
if bl_content == b'0':
existing_blocks.append(hash)
continue
2021-02-15 09:11:16 +00:00
print('got block', bl_content)
2021-02-16 05:01:01 +00:00
try:
store_block(
Kasten(
hash,
bl_content,
generator=onionrblocks.generators.AnonVDFGenerator),
safe_db
)
existing_blocks_hashes += hash
print('stored block!')
except BlockExpired:
print('Block expired', hash)
existing_blocks_hashes += hash
except ValueError:
#print('not storing dupe block')
existing_blocks_hashes += hash
2021-02-20 00:41:46 +00:00
return downloaded_total