diff --git a/onionr/__init__.py b/onionr/__init__.py index c730f428..b33ded55 100755 --- a/onionr/__init__.py +++ b/onionr/__init__.py @@ -25,6 +25,9 @@ import locale locale.setlocale(locale.LC_ALL, '') +ran_as_script = False +if __name__ == "__main__": ran_as_script = True + # Import standard libraries import sys @@ -33,6 +36,7 @@ import sys try: from urllib3.contrib.socks import SOCKSProxyManager except ModuleNotFoundError: + # check here or else we get error when onionr runs with tor raise ModuleNotFoundError("You need the PySocks module (for use with socks5 proxy to use Tor)") # Onionr imports @@ -58,7 +62,7 @@ def onionr_main(): parser.register() return -if __name__ == "__main__": +if ran_as_script: onionr_main() # Cleanup standard out/err because Python refuses to do it itsself diff --git a/onionr/communicatorutils/uploadblocks/sessionmanager.py b/onionr/communicatorutils/uploadblocks/sessionmanager.py index 5605677c..1282cb79 100644 --- a/onionr/communicatorutils/uploadblocks/sessionmanager.py +++ b/onionr/communicatorutils/uploadblocks/sessionmanager.py @@ -67,8 +67,18 @@ class BlockUploadSessionManager: comm_inst: OnionrCommunicatorDaemon = self._too_many.get_by_string("OnionrCommunicatorDaemon") sessions_to_delete = [] if comm_inst.getUptime() < 120: return + onlinePeerCount = len(comm_inst.onlinePeers) + + # If we have no online peers right now, + if onlinePeerCount == 0: return + for session in self.sessions: - if (session.total_success_count / len(comm_inst.onlinePeers)) >= onionrvalues.MIN_BLOCK_UPLOAD_PEER_PERCENT: + # if over 50% of peers that were online for a session have become unavailable, don't kill sessions + if session.total_success_count > onlinePeerCount: + if onlinePeerCount / session.total_success_count >= 0.5: return + # Clean sessions if they have uploaded to enough online peers + if session.total_success_count <= 0: continue + if (session.total_success_count / onlinePeerCount) >= onionrvalues.MIN_BLOCK_UPLOAD_PEER_PERCENT: sessions_to_delete.append(session) for session in sessions_to_delete: self.sessions.remove(session) diff --git a/onionr/runtests/__init__.py b/onionr/runtests/__init__.py index 8c47eab3..d2a0a418 100644 --- a/onionr/runtests/__init__.py +++ b/onionr/runtests/__init__.py @@ -20,9 +20,9 @@ import logger from onionrutils import epoch -from . import uicheck, inserttest +from . import uicheck, inserttest, stresstest -RUN_TESTS = [uicheck.check_ui, inserttest.insert_bin_test] +RUN_TESTS = [uicheck.check_ui, inserttest.insert_bin_test, stresstest.stress_test_block_insert] class OnionrRunTestManager: def __init__(self): diff --git a/onionr/runtests/inserttest.py b/onionr/runtests/inserttest.py index 3d52d740..39bd0e00 100644 --- a/onionr/runtests/inserttest.py +++ b/onionr/runtests/inserttest.py @@ -1,11 +1,13 @@ import os +import time import onionrblocks import logger import coredb +from communicator import peeraction def _check_remote_node(testmanager): - return + return def insert_bin_test(testmanager): data = os.urandom(32) diff --git a/onionr/runtests/stresstest.py b/onionr/runtests/stresstest.py new file mode 100644 index 00000000..a145fd76 --- /dev/null +++ b/onionr/runtests/stresstest.py @@ -0,0 +1,16 @@ +import os + +import onionrblocks +import logger +import coredb +from onionrutils import epoch + +def stress_test_block_insert(testmanager): + start = epoch.get_epoch() + count = 100 + max_insert_speed = 120 + for x in range(count): onionrblocks.insert(os.urandom(32)) + speed = epoch.get_epoch() - start + if speed < max_insert_speed: + raise ValueError(f'{count} blocks inserted too fast, {max_insert_speed}, got {speed}') + logger.info(f'runtest stress block insertion: {count} blocks inserted in {speed}s') diff --git a/onionr/runtests/uicheck.py b/onionr/runtests/uicheck.py index 57f27cae..03f4560f 100644 --- a/onionr/runtests/uicheck.py +++ b/onionr/runtests/uicheck.py @@ -5,5 +5,5 @@ def check_ui(test_manager): result = localcommand.local_command(point) if not result: raise ValueError result = result.lower() - if not 'onionr' in result and not 'Onionr' in result: - raise ValueError + if not 'script' in result: + raise ValueError(f'uicheck failed on {point}')