Work on normal gossip diffusion

This commit is contained in:
Kevin F 2022-03-28 00:13:36 -05:00
parent 5858b0aca3
commit dae99dc2f7
5 changed files with 58 additions and 9 deletions

4
src/blockdb/dbpath.py Normal file
View File

@ -0,0 +1,4 @@
from utils import identifyhome
block_db_path = identifyhome.identify_home() + 'blockdata'

View File

@ -70,6 +70,9 @@ def gossip_client():
get_new_peers,
1200, initial_sleep=5)
# Start a new thread to stream blocks from peers
dandelion_phase = DandelionPhase(DANDELION_EPOCH_LENGTH)
while True:

View File

@ -0,0 +1,39 @@
"""Onionr - Private P2P Communication.
Download blocks that are being diffused
doesn't apply for blocks in the gossip queue that are awaiting
descision to fluff or stem
"""
from random import SystemRandom
if TYPE_CHECKING:
from socket import socket
from typing import TYPE_CHECKING, List
from ..peerset import gossip_peer_set
"""
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/>.
"""
def stream_from_peers():
peer_stream_sockets: List[socket] = []
sys_rand = SystemRandom()
while True:
peers = sys_rand.sample(gossip_peer_set, min(3, len(gossip_peer_set)))

View File

@ -21,7 +21,7 @@ from filepaths import gossip_server_socket_file
from ..commands import GossipCommands
from ..peerset import gossip_peer_set
from .acceptstem import accept_stem_blocks
from .streamblocks import stream_blocks
from .diffuseblocks import stream_blocks
"""
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

View File

@ -1,6 +1,6 @@
"""Onionr - Private P2P Communication.
Stream blocks we can for inbound edge peers that ask for them
Diffuse blocks we can for inbound edge peers that ask for them
doesn't apply for blocks in the gossip queue that are awaiting
descision to fluff or stem
@ -37,7 +37,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
async def stream_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
async def diffuse_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
"""stream blocks to a peer created since an offset
"""
time_offset = await wait_for(reader.read(8), 12)
@ -54,6 +54,7 @@ async def stream_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
"Peer's specified time offset skewed too far into the future")
newly_stored_blocks = queue.Queue()
def _add_to_queue(bl):
newly_stored_blocks.put_nowait(bl)
block_storage_observers.append(
@ -70,6 +71,7 @@ async def stream_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
await writer.drain()
try:
# Send initial blocks from offset
for block in get_blocks_after_timestamp(time_offset):
if not keep_writing:
break
@ -83,14 +85,15 @@ async def stream_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
except IncompleteReadError:
keep_writing = False
# Diffuse blocks stored since we started this stream
while keep_writing:
await _send_block(newly_stored_blocks.get())
try:
keep_writing = bool(
int.from_bytes(await reader.readexactly(1), 'big')
)
except IncompleteReadError:
keep_writing = False
try:
keep_writing = bool(
int.from_bytes(await reader.readexactly(1), 'big')
)
except IncompleteReadError:
keep_writing = False
except Exception:
logger.warn(traceback.format_exc(), terminal=True)