Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandemeusy committed Jun 7, 2024
1 parent f586354 commit 84713c2
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 103 deletions.
11 changes: 5 additions & 6 deletions ct-app/core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ def main(configfile: str = None):
params.from_env("SUBGRAPH_", "PG", "RABBITMQ_")
params.overrides("OVERRIDE_")


# Utils.stringArrayToGCP(
# params.gcp.bucket,
# Utils.generateFilename("", "startup", "csv"),
# [["header"], ["value"]],
# )
Utils.stringArrayToGCP(
params.gcp.bucket,
Utils.generateFilename("", "startup", "csv"),
[["header"], ["value"]],
)

# create the core and nodes instances
instance = Core()
Expand Down
5 changes: 4 additions & 1 deletion ct-app/core/components/baseclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Base:
"""
Base class for logging and printing messages with different colors.
"""

handler = logging.StreamHandler()
handler.setFormatter(formatter)

Expand All @@ -20,7 +21,9 @@ class Base:
@property
def print_prefix(self) -> str:
cls = self.__class__
raise NotImplementedError(f"print_prefix not implemented for class '{cls.__name__}'")
raise NotImplementedError(
f"print_prefix not implemented for class '{cls.__name__}'"
)

@classmethod
def class_prefix(cls) -> str:
Expand Down
9 changes: 4 additions & 5 deletions ct-app/core/components/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ async def wrapper(self, *args, **kwargs):
if not hasattr(self.params, "flags"):
self.error("No flags available")
return

if not hasattr(self.params.flags, self.class_prefix()):
raise AttributeError(f"Feature `{func.__name__}` not in config file")

class_flags = getattr(self.params.flags, self.class_prefix())

params_raw = dir(class_flags)
Expand Down Expand Up @@ -71,7 +71,7 @@ async def wrapper(self, *args, **kwargs):
if func_name_clean not in params_clean:
self.error(f"Feature `{func.__name__}` not regonized")
return

index = params_clean.index(func_name_clean)
delay = getattr(class_flags, params_raw[index])

Expand All @@ -85,11 +85,10 @@ async def wrapper(self, *args, **kwargs):

if delay == 0:
break

if delay is not None:
await asyncio.sleep(delay)


return wrapper

return decorator
2 changes: 1 addition & 1 deletion ct-app/core/components/environment_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def envvarWithPrefix(cls, prefix: str, type=str) -> dict[str, Any]:
key: type(v) for key, v in environ.items() if key.startswith(prefix)
}

return dict(sorted(var_dict.items()))
return dict(sorted(var_dict.items()))
20 changes: 12 additions & 8 deletions ct-app/core/components/graphql_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ async def _test_query(self, key: str, **kwargs) -> bool:

# call `self._execute(self._sku_query, vars)` with a timeout
try:
response = await asyncio.wait_for(self._execute(self._sku_query, vars), timeout=30)
response = await asyncio.wait_for(
self._execute(self._sku_query, vars), timeout=30
)
except asyncio.TimeoutError:
self.error("Query timeout occurred")
return False
Expand All @@ -81,10 +83,12 @@ async def _get(self, key: str, **kwargs) -> dict:
vars.update(kwargs)

try:
response = await asyncio.wait_for(self._execute(self._sku_query, vars), timeout=30)
response = await asyncio.wait_for(
self._execute(self._sku_query, vars), timeout=30
)
except asyncio.TimeoutError:
self.error("Timeout error while fetching data from subgraph.")
break
break
if response is None:
break

Expand Down Expand Up @@ -136,9 +140,10 @@ async def test(self, **kwargs):

if result is None:
return False

return result


class SafesProvider(GraphQLProvider):
def __init__(self, url: str):
super().__init__(url)
Expand Down Expand Up @@ -175,14 +180,13 @@ def __init__(self, url: str):
def print_prefix(self) -> str:
return "transaction-provider"


class RewardsProvider(GraphQLProvider):
def __init__(self, url: str):
super().__init__(url)
self._default_key = "accounts"
self._sku_query = self._load_query(
"core/subgraph_queries/rewards.graphql"
)
self._sku_query = self._load_query("core/subgraph_queries/rewards.graphql")

@property
def print_prefix(self) -> str:
return "rewards-provider"
return "rewards-provider"
16 changes: 4 additions & 12 deletions ct-app/core/components/hoprd_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
SendMessageBodyRequest,
TagQueryRequest,
)
from hoprd_sdk.api import (
AccountApi,
ChannelsApi,
MessagesApi,
NetworkApi,
NodeApi,
)
from hoprd_sdk.api import AccountApi, ChannelsApi, MessagesApi, NetworkApi, NodeApi
from hoprd_sdk.rest import ApiException
from requests import Response
from urllib3.exceptions import MaxRetryError
Expand Down Expand Up @@ -142,9 +136,7 @@ async def open_channel(self, peer_address: str, amount: str):
"""
body = OpenChannelBodyRequest(amount, peer_address)

is_ok, response = await self.__call_api(
ChannelsApi, "open_channel", body=body
)
is_ok, response = await self.__call_api(ChannelsApi, "open_channel", body=body)

return response.channel_id if is_ok else None

Expand Down Expand Up @@ -335,7 +327,7 @@ async def messages_pop(self, tag: int = MESSAGE_TAG) -> bool:
"""
body = TagQueryRequest(tag=tag)
_, response = await self.__call_api(MessagesApi, "pop", body=body)

return response

async def messages_pop_all(self, tag: int = MESSAGE_TAG) -> list:
Expand All @@ -350,7 +342,7 @@ async def messages_pop_all(self, tag: int = MESSAGE_TAG) -> list:

async def node_info(self):
_, response = await self.__call_api(NodeApi, "info")

return response

async def ticket_price(self) -> int:
Expand Down
2 changes: 1 addition & 1 deletion ct-app/core/components/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Parameters(Base):
"""
Class that represents a set of parameters that can be accessed and modified. The parameters are stored in a dictionary and can be accessed and modified using the dot notation. The parameters can be loaded from environment variables with a specified prefix.
"""

def __init__(self):
super().__init__()

Expand Down Expand Up @@ -41,7 +42,6 @@ def overrides(self, prefix: str):
else:
raise KeyError(f"Key {key} not found in parameters")


def from_env(self, *prefixes: list[str]):
for prefix in prefixes:
subparams_name = prefix.lower()
Expand Down
13 changes: 9 additions & 4 deletions ct-app/core/components/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import csv
from os import path
import random
import time
from datetime import datetime, timedelta
from os import path
from typing import Any

from aiohttp import ClientSession
from google.cloud import storage

from core.model.address import Address
from core.model.peer import Peer
from core.model.subgraph_entry import SubgraphEntry
from core.model.topology_entry import TopologyEntry
from google.cloud import storage

from .baseclass import Base
from .channelstatus import ChannelStatus
Expand Down Expand Up @@ -44,6 +43,7 @@ async def httpPOST(
:param data: The data to be sent.
:returns: A tuple containing the status code and the response.
"""

async def post(session: ClientSession, url: str, data: dict, timeout: int):
async with session.post(url, json=data, timeout=timeout) as response:
status = response.status
Expand Down Expand Up @@ -77,7 +77,12 @@ def mergeDataSources(
# if safe is None:
# safe = SubgraphEntry(address, "0.000015", "0x0", "10000")

if peer is None or topo is None or safe is None or safe.wxHoprBalance is None:
if (
peer is None
or topo is None
or safe is None
or safe.wxHoprBalance is None
):
continue

peer.safe_address = safe.safe_address
Expand Down
22 changes: 11 additions & 11 deletions ct-app/core/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
import random
import time
from typing import Any
from copy import deepcopy
import random
from typing import Any

from database import Utils as DBUtils
from database.database_connection import DatabaseConnection
Expand All @@ -13,10 +13,10 @@
from .components.decorators import connectguard, flagguard, formalin
from .components.graphql_providers import (
ProviderError,
RewardsProvider,
SafesProvider,
StakingProvider,
wxHOPRTransactionProvider,
RewardsProvider
)
from .components.hoprd_api import HoprdAPI
from .components.lockedvar import LockedVar
Expand Down Expand Up @@ -118,9 +118,7 @@ def api(self) -> HoprdAPI:

@property
async def network_nodes_addresses(self) -> list[Address]:
return await asyncio.gather(
*[node.address.get() for node in self.nodes]
)
return await asyncio.gather(*[node.address.get() for node in self.nodes])

@property
def subgraph_type(self) -> SubgraphType:
Expand All @@ -141,7 +139,7 @@ def wxhopr_txs_subgraph_url(self) -> str:
@property
def rewards_subgraph_url(self) -> str:
return self._rewards_subgraph_url(self.subgraph_type)

@subgraph_type.setter
def subgraph_type(self, value: SubgraphType):
if value != self.subgraph_type:
Expand Down Expand Up @@ -303,7 +301,7 @@ async def apply_economic_model(self):
if not ready:
self.warning("Not enough data to apply economic model.")
return

eligibles = Utils.mergeDataSources(topology, peers, registered_nodes)
self.debug(f"Merged topology and subgraph data ({len(eligibles)} entries).")

Expand Down Expand Up @@ -353,7 +351,9 @@ async def apply_economic_model(self):

for peer in eligibles:
peer.economic_model = deepcopy(model)
peer.economic_model.coefficients.c += redeemed_rewards.get(peer.address.address,0.0)
peer.economic_model.coefficients.c += redeemed_rewards.get(
peer.address.address, 0.0
)
peer.max_apr = self.params.economicModel.maxAPRPercentage

self.debug("Assigned economic model to eligible nodes.")
Expand All @@ -379,7 +379,7 @@ async def apply_economic_model(self):
PEER_SAFE_COUNT.labels(peer.address.id).set(peer.safe_address_count)
PEER_TF_STAKE.labels(peer.address.id).set(peer.transformed_stake)
PEER_VERSION.labels(peer.address.id, str(peer.version)).set(1)

@flagguard
@formalin("Distributing rewards")
@connectguard
Expand Down Expand Up @@ -421,7 +421,7 @@ async def distribute_rewards(self):
t: tuple[dict[str, dict[str, Any]], int] = await self.multiple_attempts_sending(
peers, self.params.distribution.maxIterations
)
rewards, iterations = t # trick for typehinting tuple unpacking
rewards, iterations = t # trick for typehinting tuple unpacking
self.info("Distribution completed.")

self.debug(rewards)
Expand Down
11 changes: 6 additions & 5 deletions ct-app/core/model/economic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, formula: str, condition: str):
def fromParameters(cls, parameters: Parameters):
return cls(parameters.formula, parameters.condition)


class Equations:
def __init__(self, f_x: Equation, g_x: Equation):
self.f_x = f_x
Expand Down Expand Up @@ -74,7 +75,7 @@ def period(self):
@property
def distribution_per_period(self):
return self._distribution_per_period

@property
def ticket_price(self):
return self._ticket_price
Expand Down Expand Up @@ -108,7 +109,7 @@ def ticket_price(self, value):
def winning_probability(self, value):
self._winning_probability = value
TICKET_WINNING_PROB.set(value)

@classmethod
def fromParameters(cls, parameters: Parameters):
return cls(
Expand Down Expand Up @@ -154,10 +155,10 @@ def delay_between_distributions(self):
@classmethod
def fromParameters(cls, parameters: Parameters):
return EconomicModel(
Equations.fromParameters(parameters.equations),
Coefficients.fromParameters(parameters.coefficients),
Equations.fromParameters(parameters.equations),
Coefficients.fromParameters(parameters.coefficients),
Budget.fromParameters(parameters.budget),
)

def __repr__(self):
return f"EconomicModel({self.equations}, {self.coefficients}, {self.budget})"
2 changes: 1 addition & 1 deletion ct-app/core/model/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def message_count_for_reward(self):
budget = self.economic_model.budget
denominator = budget.ticket_price * budget.winning_probability

return round(self.protocol_reward_per_distribution / denominator)
return round(self.protocol_reward_per_distribution / denominator)

@property
def apr_percentage(self):
Expand Down
1 change: 1 addition & 0 deletions ct-app/core/model/subgraph_url.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core.components.parameters import Parameters

from .subgraph_type import SubgraphType


Expand Down
Loading

0 comments on commit 84713c2

Please sign in to comment.