work on torgossip
This commit is contained in:
parent
2ffcc2e18e
commit
db5320124f
@ -12,6 +12,7 @@ from communicatorutils import uploadblocks
|
|||||||
from communicatorutils import announcenode, deniableinserts
|
from communicatorutils import announcenode, deniableinserts
|
||||||
from communicatorutils import netcheck
|
from communicatorutils import netcheck
|
||||||
import onionrpeers
|
import onionrpeers
|
||||||
|
from blockio import clean_expired_blocks
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
@ -69,3 +70,5 @@ def spawn_client_threads(shared_state: 'TooMany'):
|
|||||||
add_onionr_thread(
|
add_onionr_thread(
|
||||||
announcenode.announce_node, [shared_state], 600, 60)
|
announcenode.announce_node, [shared_state], 600, 60)
|
||||||
add_onionr_thread(onionrpeers.peer_cleanup, [], 300, 300)
|
add_onionr_thread(onionrpeers.peer_cleanup, [], 300, 300)
|
||||||
|
|
||||||
|
add_onionr_thread(clean_expired_blocks, [shared_state.get_by_string('SafeDB')], 120, 1)
|
||||||
|
@ -98,7 +98,7 @@ def client_funcs(shared_state, socket_pool):
|
|||||||
sleep(sleep_t)
|
sleep(sleep_t)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
download_blocks(socket_pool[peer], 0, 'txt')
|
download_blocks(block_db, socket_pool[peer], 0, 'txt')
|
||||||
except BrokenPipeError:
|
except BrokenPipeError:
|
||||||
del socket_pool[peer]
|
del socket_pool[peer]
|
||||||
|
|
||||||
|
@ -2,9 +2,13 @@ from typing import TYPE_CHECKING
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from kasten import Kasten
|
||||||
|
from kasten.generator import pack
|
||||||
|
|
||||||
import logger
|
import logger
|
||||||
from blockio import store_block, subprocvalidate
|
from blockio import store_block, subprocvalidate, list_all_blocks
|
||||||
import onionrblocks
|
import onionrblocks
|
||||||
|
from onionrblocks.exceptions import BlockExpired
|
||||||
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
||||||
from commands import GossipCommands
|
from commands import GossipCommands
|
||||||
|
|
||||||
@ -12,17 +16,43 @@ if TYPE_CHECKING:
|
|||||||
from socket import socket
|
from socket import socket
|
||||||
|
|
||||||
|
|
||||||
def download_blocks(sock: 'socket', offset: int, block_type: str):
|
def download_blocks(safe_db, sock: 'socket', offset: int, block_type: str):
|
||||||
sock.sendall(
|
sock.sendall(
|
||||||
str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode('utf-8') + str(offset).encode('utf-8') + b',' +
|
str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode('utf-8') +
|
||||||
block_type.encode('utf-8'))
|
str(offset).encode('utf-8') + b',' +
|
||||||
|
block_type.encode('utf-8'))
|
||||||
bl_hashs = sock.recv(600000)
|
bl_hashs = sock.recv(600000)
|
||||||
|
existing_blocks = list_all_blocks(safe_db)
|
||||||
|
existing_blocks_hashes = b''
|
||||||
|
for i in existing_blocks:
|
||||||
|
for x in i:
|
||||||
|
existing_blocks_hashes += int(x).to_bytes(1, 'little')
|
||||||
|
print('existing', existing_blocks_hashes)
|
||||||
hash = None
|
hash = None
|
||||||
for i in range(len(bl_hashs)//64):
|
for i in range(len(bl_hashs)//64):
|
||||||
hash = bl_hashs[:(i*64) + 64]
|
hash = bl_hashs[:(i*64) + 64]
|
||||||
|
if hash in existing_blocks_hashes:
|
||||||
|
continue
|
||||||
sock.sendall(
|
sock.sendall(
|
||||||
str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash)
|
str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash)
|
||||||
bl_content = sock.recv(10**6)
|
bl_content = sock.recv(10**6)
|
||||||
|
if bl_content == b'0':
|
||||||
|
existing_blocks.append(hash)
|
||||||
|
continue
|
||||||
print('got block', bl_content)
|
print('got block', bl_content)
|
||||||
|
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
|
||||||
|
@ -71,7 +71,6 @@ def on_init(api, data=None):
|
|||||||
hf.write(hs)
|
hf.write(hs)
|
||||||
logger.info("TorGossip server on " + b32encode(hs).lower().decode('utf-8'), terminal=True)
|
logger.info("TorGossip server on " + b32encode(hs).lower().decode('utf-8'), terminal=True)
|
||||||
|
|
||||||
|
|
||||||
Thread(target=start_server, daemon=True, args=[shared_state]).start()
|
Thread(target=start_server, daemon=True, args=[shared_state]).start()
|
||||||
Thread(target=start_client, daemon=True, args=[shared_state]).start()
|
Thread(target=start_client, daemon=True, args=[shared_state]).start()
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
def start_server(shared_state):
|
def start_server(shared_state):
|
||||||
|
|
||||||
bl = subprocgenerate.vdf_block(b"yep", "txt", 120)
|
bl = subprocgenerate.vdf_block(b"yep" + os.urandom(5), "txt", 6000)
|
||||||
store_block(Kasten(bl.id, bl.get_packed(), generator=AnonVDFGenerator), shared_state.get_by_string('SafeDB'))
|
store_block(Kasten(bl.id, bl.get_packed(), generator=AnonVDFGenerator), shared_state.get_by_string('SafeDB'))
|
||||||
print(bl.id)
|
print(bl.id)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user