2022-03-26 23:26:55 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
2022-03-28 05:13:36 +00:00
|
|
|
Diffuse blocks we can for inbound edge peers that ask for them
|
2022-03-26 23:26:55 +00:00
|
|
|
|
|
|
|
doesn't apply for blocks in the gossip queue that are awaiting
|
|
|
|
descision to fluff or stem
|
|
|
|
|
|
|
|
"""
|
2022-07-25 19:37:39 +00:00
|
|
|
from asyncio import IncompleteReadError, wait_for, Queue, sleep
|
2022-03-26 23:26:55 +00:00
|
|
|
|
|
|
|
import traceback
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from asyncio import StreamWriter, StreamReader
|
|
|
|
from onionrblocks import Block
|
|
|
|
|
2022-06-26 05:34:49 +00:00
|
|
|
from ..constants import BLOCK_MAX_SIZE, BLOCK_SIZE_LEN
|
|
|
|
from ..constants import BLOCK_STREAM_OFFSET_DIGITS
|
2022-03-26 23:26:55 +00:00
|
|
|
|
|
|
|
import logger
|
2022-07-19 05:32:54 +00:00
|
|
|
import blockdb
|
2022-03-26 23:26:55 +00:00
|
|
|
from blockdb import get_blocks_after_timestamp, block_storage_observers
|
|
|
|
"""
|
|
|
|
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/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-03-28 05:13:36 +00:00
|
|
|
async def diffuse_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
|
2022-03-26 23:26:55 +00:00
|
|
|
"""stream blocks to a peer created since an offset
|
|
|
|
"""
|
2022-04-03 06:16:58 +00:00
|
|
|
time_offset = await wait_for(reader.readexactly(BLOCK_STREAM_OFFSET_DIGITS), 12)
|
2022-03-26 23:26:55 +00:00
|
|
|
time_offset = time_offset.decode('utf-8')
|
|
|
|
keep_writing = True
|
|
|
|
|
|
|
|
# Makes sure timestamp is positive and not weird
|
|
|
|
if not all(c in "0123456789" for c in time_offset):
|
|
|
|
raise ValueError("Invalid time offset")
|
|
|
|
time_offset = int(time_offset)
|
|
|
|
|
2022-04-05 06:17:40 +00:00
|
|
|
if time_offset - time() > -5:
|
2022-03-26 23:26:55 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Peer's specified time offset skewed too far into the future")
|
|
|
|
|
2022-03-28 05:13:36 +00:00
|
|
|
|
2022-03-26 23:26:55 +00:00
|
|
|
|
2022-06-05 20:11:53 +00:00
|
|
|
async def _send_block(block: 'Block'):
|
2022-03-26 23:26:55 +00:00
|
|
|
writer.write(block.id)
|
2022-04-04 05:48:30 +00:00
|
|
|
await writer.drain()
|
2022-04-03 06:16:58 +00:00
|
|
|
|
|
|
|
# we tell id above, they tell if they want the block
|
|
|
|
if int.from_bytes(await reader.readexactly(1), 'big') == 0:
|
|
|
|
return
|
|
|
|
|
2022-04-20 05:28:29 +00:00
|
|
|
# write block size
|
2022-03-26 23:26:55 +00:00
|
|
|
writer.write(
|
2022-06-26 05:34:49 +00:00
|
|
|
str(len(block.raw)).zfill(BLOCK_SIZE_LEN).encode('utf-8'))
|
2022-03-26 23:26:55 +00:00
|
|
|
await writer.drain()
|
|
|
|
writer.write(block.raw)
|
|
|
|
await writer.drain()
|
|
|
|
|
|
|
|
try:
|
2022-03-28 05:13:36 +00:00
|
|
|
# Send initial blocks from offset
|
2022-03-26 23:26:55 +00:00
|
|
|
for block in get_blocks_after_timestamp(time_offset):
|
|
|
|
if not keep_writing:
|
|
|
|
break
|
|
|
|
|
|
|
|
await _send_block(block)
|
|
|
|
|
|
|
|
try:
|
|
|
|
keep_writing = bool(
|
|
|
|
int.from_bytes(await reader.readexactly(1), 'big')
|
|
|
|
)
|
|
|
|
except IncompleteReadError:
|
|
|
|
keep_writing = False
|
2022-07-19 05:32:54 +00:00
|
|
|
time_offset = time()
|
2022-03-26 23:26:55 +00:00
|
|
|
|
2022-03-28 05:13:36 +00:00
|
|
|
# Diffuse blocks stored since we started this stream
|
2022-03-26 23:26:55 +00:00
|
|
|
while keep_writing:
|
2022-07-19 05:32:54 +00:00
|
|
|
bls = blockdb.get_blocks_after_timestamp(time_offset)
|
2022-07-25 19:37:39 +00:00
|
|
|
await sleep(1) # Must be here to avoid blocking the event loop
|
2022-07-19 05:32:54 +00:00
|
|
|
for bl in bls:
|
|
|
|
await _send_block(bl)
|
|
|
|
try:
|
|
|
|
keep_writing = bool(
|
|
|
|
int.from_bytes(await reader.readexactly(1), 'big')
|
|
|
|
)
|
|
|
|
except IncompleteReadError:
|
|
|
|
keep_writing = False
|
|
|
|
break
|
2022-07-30 20:41:11 +00:00
|
|
|
except ConnectionResetError:
|
|
|
|
pass
|
2022-03-26 23:26:55 +00:00
|
|
|
except Exception:
|
|
|
|
logger.warn(traceback.format_exc(), terminal=True)
|
|
|
|
|