2019-12-23 07:51:24 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-09-17 01:16:06 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
Manager for upload 'sessions'
|
2019-09-17 01:16:06 +00:00
|
|
|
"""
|
2019-12-23 07:51:24 +00:00
|
|
|
from typing import List, Union, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
2020-07-26 03:28:32 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-12-23 07:51:24 +00:00
|
|
|
from session import UploadSession
|
|
|
|
|
|
|
|
from onionrutils import bytesconverter
|
|
|
|
from etc import onionrvalues
|
|
|
|
from utils import reconstructhash
|
|
|
|
|
|
|
|
from . import session
|
2019-09-17 01:16:06 +00:00
|
|
|
"""
|
|
|
|
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/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class BlockUploadSessionManager:
|
2019-12-23 07:51:24 +00:00
|
|
|
"""Holds block UploadSession instances.
|
|
|
|
|
|
|
|
Optionally accepts iterable of sessions to added on init
|
|
|
|
Arguments: old_session: iterable of old UploadSession objects
|
|
|
|
"""
|
2019-09-17 01:16:06 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
def __init__(self, old_sessions: List = None):
|
2019-09-17 01:16:06 +00:00
|
|
|
if old_sessions is None:
|
|
|
|
self.sessions = []
|
|
|
|
else:
|
2019-12-23 07:51:24 +00:00
|
|
|
self.sessions = old_sessions
|
|
|
|
|
|
|
|
def add_session(self,
|
|
|
|
session_or_block: Union[str,
|
|
|
|
bytes,
|
|
|
|
session.UploadSession
|
|
|
|
]
|
|
|
|
) -> session.UploadSession:
|
|
|
|
"""Create (or add existing) block upload session.
|
|
|
|
|
|
|
|
from a str/bytes block hex hash, existing UploadSession
|
|
|
|
"""
|
|
|
|
if isinstance(session_or_block, session.UploadSession):
|
|
|
|
if session_or_block not in self.sessions:
|
2019-09-17 01:16:06 +00:00
|
|
|
self.sessions.append(session_or_block)
|
|
|
|
return session_or_block
|
|
|
|
try:
|
|
|
|
return self.get_session(session_or_block)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
# convert bytes hash to str
|
2019-12-23 07:51:24 +00:00
|
|
|
if isinstance(session_or_block, bytes):
|
|
|
|
session_or_block = bytesconverter.bytes_to_str(session_or_block)
|
2019-09-17 01:16:06 +00:00
|
|
|
# intentionally not elif
|
|
|
|
if isinstance(session_or_block, str):
|
|
|
|
new_session = session.UploadSession(session_or_block)
|
|
|
|
self.sessions.append(new_session)
|
|
|
|
return new_session
|
2019-12-23 07:51:24 +00:00
|
|
|
raise ValueError
|
2019-09-17 01:16:06 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
def get_session(self,
|
|
|
|
block_hash: Union[str, bytes]
|
|
|
|
) -> session.UploadSession:
|
|
|
|
block_hash = reconstructhash.deconstruct_hash(
|
|
|
|
bytesconverter.bytes_to_str(block_hash))
|
|
|
|
for sess in self.sessions:
|
|
|
|
if sess.block_hash == block_hash:
|
|
|
|
return sess
|
2019-09-17 01:16:06 +00:00
|
|
|
raise KeyError
|
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
def clean_session(self,
|
|
|
|
specific_session: Union[str, 'UploadSession'] = None):
|
|
|
|
|
|
|
|
comm_inst: 'OnionrCommunicatorDaemon' # type: ignore
|
|
|
|
comm_inst = self._too_many.get_by_string( # pylint: disable=E1101 type: ignore
|
2020-01-03 10:17:00 +00:00
|
|
|
"OnionrCommunicatorDaemon")
|
2020-07-26 03:28:32 +00:00
|
|
|
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string(
|
|
|
|
"DeadSimpleKV")
|
2019-09-17 01:16:06 +00:00
|
|
|
sessions_to_delete = []
|
2019-12-23 07:51:24 +00:00
|
|
|
if comm_inst.getUptime() < 120:
|
|
|
|
return
|
2020-07-26 03:28:32 +00:00
|
|
|
onlinePeerCount = len(kv.get('onlinePeers'))
|
2019-12-23 07:51:24 +00:00
|
|
|
|
2019-10-19 08:47:24 +00:00
|
|
|
# If we have no online peers right now,
|
2019-12-23 07:51:24 +00:00
|
|
|
if onlinePeerCount == 0:
|
|
|
|
return
|
2019-10-19 08:47:24 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
for sess in self.sessions:
|
|
|
|
# if over 50% of peers that were online for a session have
|
|
|
|
# become unavailable, don't kill sessions
|
|
|
|
if sess.total_success_count > onlinePeerCount:
|
|
|
|
if onlinePeerCount / sess.total_success_count >= 0.5:
|
|
|
|
return
|
2019-10-19 08:47:24 +00:00
|
|
|
# Clean sessions if they have uploaded to enough online peers
|
2019-12-23 07:51:24 +00:00
|
|
|
if sess.total_success_count <= 0:
|
|
|
|
continue
|
|
|
|
if (sess.total_success_count / onlinePeerCount) >= onionrvalues.MIN_BLOCK_UPLOAD_PEER_PERCENT:
|
|
|
|
sessions_to_delete.append(sess)
|
|
|
|
for sess in sessions_to_delete:
|
2020-01-03 10:17:00 +00:00
|
|
|
try:
|
|
|
|
self.sessions.remove(session)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-09-17 01:16:06 +00:00
|
|
|
# TODO cleanup to one round of search
|
2019-12-23 07:51:24 +00:00
|
|
|
# Remove the blocks from the sessions, upload list,
|
|
|
|
# and waitforshare list
|
2019-09-17 01:16:06 +00:00
|
|
|
try:
|
2019-12-23 07:51:24 +00:00
|
|
|
comm_inst.blocksToUpload.remove(
|
|
|
|
reconstructhash.reconstruct_hash(sess.block_hash))
|
2019-09-17 01:16:06 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
2019-12-23 07:51:24 +00:00
|
|
|
comm_inst.blocksToUpload.remove(sess.block_hash)
|
2019-09-17 01:16:06 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|