Skip to content

Commit

Permalink
Fix: replace usages of __str__()
Browse files Browse the repository at this point in the history
Problem: `__str__()` is not meant to be used directly in typical Python code.

Solution: remove calls from within f-strings as they are redundant
anyway and replace the remaining calls by calls to `str()`.
  • Loading branch information
odesenfans committed Sep 6, 2023
1 parent 641bc3b commit 4391e62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/aleph_vrf/coordinator/vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import aiohttp
from aleph.sdk.chains.ethereum import ETHAccount
from aleph.sdk.client import AuthenticatedAlephClient
from aleph_message.models import ItemHash
from aleph_message.status import MessageStatus

from aleph_vrf.models import (
Expand Down Expand Up @@ -84,7 +85,7 @@ async def generate_vrf(account: ETHAccount) -> VRFResponse:
nodes=sha3_256(selected_nodes),
)

ref = f"vrf_{vrf_request.request_id.__str__()}_request"
ref = f"vrf_{vrf_request.request_id}_request"

request_item_hash = await publish_data(vrf_request, ref, account)

Expand All @@ -104,7 +105,7 @@ async def generate_vrf(account: ETHAccount) -> VRFResponse:
vrf_request,
)

ref = f"vrf_{vrf_response.request_id.__str__()}"
ref = f"vrf_{vrf_response.request_id}"

response_item_hash = await publish_data(vrf_response, ref, account)

Expand Down Expand Up @@ -200,8 +201,8 @@ def generate_final_vrf(

async def publish_data(
data: Union[VRFRequest, VRFResponse], ref: str, account: ETHAccount
):
channel = f"vrf_{data.request_id.__str__()}"
) -> ItemHash:
channel = f"vrf_{data.request_id}"

async with AuthenticatedAlephClient(account=account, api_server=API_HOST) as client:
message, status = await client.create_post(
Expand All @@ -214,4 +215,4 @@ async def publish_data(
if status != MessageStatus.PROCESSED:
raise ValueError(f"Message could not be processed for ref {ref}")

return message.item_hash.__str__()
return message.item_hash
20 changes: 11 additions & 9 deletions src/aleph_vrf/executor/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
from typing import Dict, Union

from aleph_message.models import ItemHash

logger = logging.getLogger(__name__)

logger.debug("import aleph_client")
Expand Down Expand Up @@ -61,7 +63,7 @@ async def receive_generate(vrf_request: str, request: Request) -> APIResponse:
generation_request.num_bytes, generation_request.nonce
)
SAVED_GENERATED_BYTES[
generation_request.execution_id.__str__()
str(generation_request.execution_id)
] = generated_bytes

response_hash = VRFResponseHash(
Expand All @@ -76,8 +78,8 @@ async def receive_generate(vrf_request: str, request: Request) -> APIResponse:

ref = (
f"vrf"
f"_{response_hash.request_id.__str__()}"
f"_{response_hash.execution_id.__str__()}"
f"_{response_hash.request_id}"
f"_{response_hash.execution_id}"
f"_{GENERATE_MESSAGE_REF_PATH}"
)

Expand All @@ -99,13 +101,13 @@ async def receive_publish(hash_message: str, request: Request) -> APIResponse:
message = await client.get_message(item_hash=hash_message)
response_hash = generate_response_hash_from_message(message)

if not SAVED_GENERATED_BYTES[response_hash.execution_id.__str__()]:
if not SAVED_GENERATED_BYTES[str(response_hash.execution_id)]:
raise ValueError(
f"Random bytes not existing for execution {response_hash.execution_id}"
)

random_bytes: bytes = SAVED_GENERATED_BYTES[
response_hash.execution_id.__str__()
str(response_hash.execution_id)
]

response_bytes = VRFRandomBytes(
Expand All @@ -118,7 +120,7 @@ async def receive_publish(hash_message: str, request: Request) -> APIResponse:
random_number=bytes_to_int(random_bytes),
)

ref = f"vrf_{response_hash.request_id.__str__()}_{response_hash.execution_id.__str__()}"
ref = f"vrf_{response_hash.request_id}_{response_hash.execution_id}"

message_hash = await publish_data(response_bytes, ref, account)

Expand All @@ -129,8 +131,8 @@ async def receive_publish(hash_message: str, request: Request) -> APIResponse:

async def publish_data(
data: Union[VRFResponseHash, VRFRandomBytes], ref: str, account: ETHAccount
):
channel = f"vrf_{data.request_id.__str__()}"
) -> ItemHash:
channel = f"vrf_{data.request_id}"

async with AuthenticatedAlephClient(account=account, api_server=API_HOST) as client:
message, status = await client.create_post(
Expand All @@ -145,4 +147,4 @@ async def publish_data(
f"Message could not be processed for request {data.request_id} and execution_id {data.execution_id}"
)

return message.item_hash.__str__()
return message.item_hash

0 comments on commit 4391e62

Please sign in to comment.