Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix critical issues reported by Codacy #7309

Merged
merged 9 commits into from
Mar 3, 2023
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN pip3 install -r requirements/core-requirements.txt

# Copy the source code and set the working directory
COPY ./ tribler
WORKDIR tribler
WORKDIR /home/user/tribler

# Set the REST API port and expose it
ENV CORE_API_PORT=20100
Expand Down
6 changes: 3 additions & 3 deletions src/run_tribler_headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class IPPortAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
parsed = re.match(r"^([\d\.]+)\:(\d+)$", values)
if not parsed:
raise argparse.ArgumentError("Invalid address:port")
raise argparse.ArgumentError(self, "Invalid address:port")

ip, port = parsed.group(1), int(parsed.group(2))
try:
inet_aton(ip)
except:
raise argparse.ArgumentError("Invalid server address")
raise argparse.ArgumentError(self, "Invalid server address")

if not (0 < port < 65535):
raise argparse.ArgumentError("Invalid server port")
raise argparse.ArgumentError(self, "Invalid server port")
setattr(namespace, self.dest, values)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from __future__ import annotations

from asyncio import Future
from typing import TYPE_CHECKING

from ipv8.requestcache import RandomNumberCache

from tribler.core.components.bandwidth_accounting.db.transaction import BandwidthTransactionData

if TYPE_CHECKING:
from tribler.core.components.bandwidth_accounting.community.bandwidth_accounting_community import \
BandwidthAccountingCommunity


class BandwidthTransactionSignCache(RandomNumberCache):
"""
This cache keeps track of pending bandwidth transaction signature requests.
"""

def __init__(self, community: BandwidthAccountingCommunity, transaction: BandwidthTransactionData) -> None: # noqa: F821
def __init__(self, community: BandwidthAccountingCommunity, transaction: BandwidthTransactionData) -> None:
"""
Initialize the cache.
:param community: The bandwidth community associated with this cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BandwidthTransactionPayload(VariablePayload):
"timestamp", "request_id"]

@classmethod
def from_transaction(cls, transaction: BandwidthTransaction, request_id: int) -> BandwidthTransactionPayload: # noqa: F821
def from_transaction(cls, transaction, request_id: int) -> BandwidthTransactionPayload:
return BandwidthTransactionPayload(
transaction.sequence_number,
transaction.public_key_a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def is_valid(self) -> bool:
return True

@classmethod
def from_payload(cls, payload: BandwidthTransactionPayload) -> BandwidthTransaction: # noqa: F821
def from_payload(cls, payload: BandwidthTransactionPayload):
"""
Create a block according to a given payload.
This method can be used when receiving a block from the network.
Expand All @@ -99,10 +99,10 @@ def from_payload(cls, payload: BandwidthTransactionPayload) -> BandwidthTransact
payload.signature_a, payload.signature_b, payload.amount, payload.timestamp)

@classmethod
def from_db(cls, db_obj: BandwidthTransaction) -> BandwidthTransactionData: # noqa: F821
def from_db(cls, db_obj) -> BandwidthTransactionData:
"""
Return a BandwidthTransaction object from a database object.
:param db_obj: The database object to convert.
:param db_obj: The BandwidthTransaction object to convert.
:return A BandwidthTransaction object, based on the database object.
"""
return BandwidthTransactionData(db_obj.sequence_number, db_obj.public_key_a, db_obj.public_key_b,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,6 @@ def set_upload_mode(self, upload_mode):
self.config.set_upload_mode(upload_mode)
self.handle.set_upload_mode(upload_mode)

@check_handle()
def get_upload_mode(self):
return self.handle.status().upload_mode

@require_handle
def set_upload_mode(self, upload_mode):
self.handle.set_upload_mode(upload_mode)

drew2a marked this conversation as resolved.
Show resolved Hide resolved
@require_handle
def force_dht_announce(self):
self.handle.force_dht_announce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from binascii import unhexlify
from copy import deepcopy
from shutil import rmtree
from typing import Dict, List, Optional
from typing import Callable, Dict, List, Optional

from ipv8.taskmanager import TaskManager, task

Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(self,
self.default_alert_mask = lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | \
lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning | \
lt.alert.category_t.tracker_notification | lt.alert.category_t.debug_notification
self.session_stats_callback = None
self.session_stats_callback: Optional[Callable] = None
self.state_cb_count = 0

# Status of libtorrent session to indicate if it can safely close and no pending writes to disk exists.
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/core/components/tunnel/community/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class BandwidthTransactionPayload(VariablePayload):
"circuit_id", "base_amount"]

@classmethod
def from_transaction(cls, transaction: BandwidthTransaction, circuit_id: int, base_amount: int): # noqa: F821
def from_transaction(cls, transaction, circuit_id: int, base_amount: int):
"""
Create a transaction from the provided payload.
:param transaction: The transaction to convert to a payload.
:param transaction: The BandwidthTransaction to convert to a payload.
:param circuit_id: The circuit identifier to include in the payload.
:param base_amount: The base amount of bandwidth to payout.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from binascii import unhexlify
from collections import Counter
from distutils.version import LooseVersion
from typing import List, Optional
from typing import Callable, List, Optional

import async_timeout
from ipv8.messaging.anonymization.caches import CreateRequestCache
Expand Down Expand Up @@ -94,7 +94,8 @@ def __init__(self, *args, **kwargs):
self.download_states = {}
self.competing_slots = [(0, None)] * num_competing_slots # 1st tuple item = token balance, 2nd = circuit id
self.random_slots = [None] * num_random_slots
self.reject_callback = None # This callback is invoked with a tuple (time, balance) when we reject a circuit
# This callback is invoked with a tuple (time, balance) when we reject a circuit
self.reject_callback: Optional[Callable] = None
self.last_forced_announce = {}

if self.socks_servers:
Expand Down