Skip to content

Commit

Permalink
Fix: Solved more PR issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres D. Molins committed Sep 14, 2023
1 parent 33ef316 commit f296f4c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ WORKDIR /usr/src/aleph_vrf
COPY . .

RUN mkdir /opt/packages
RUN pip install -t /opt/packages -r requirements.txt
RUN pip install -t /opt/packages .
30 changes: 7 additions & 23 deletions src/aleph_vrf/coordinator/vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import json
import logging
import random
from enum import Enum
from hashlib import sha3_256
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Type, TypeVar, Union
from uuid import uuid4

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 pydantic import BaseModel
from pydantic.json import pydantic_encoder

from aleph_vrf.models import (
Expand Down Expand Up @@ -39,28 +39,20 @@
logger = logging.getLogger(__name__)


class VRFResponseModel(Enum):
ResponseHash = 1
RandomBytes = 2
M = TypeVar("M", bound=BaseModel)


async def post_node_vrf(
url: str, model: VRFResponseModel
) -> Union[Exception, VRFResponseHash, VRFRandomBytes]:
async def post_node_vrf(url: str, model: Type[M]) -> Union[Exception, M]:
async with aiohttp.ClientSession() as session:
async with session.post(url, timeout=60) as resp:
if resp.status != 200:
raise ValueError(f"VRF node request failed on {url}")

response = await resp.json()
data = response["data"]

await session.close()

if model == VRFResponseModel.ResponseHash:
return VRFResponseHash(**data)

return VRFRandomBytes(**data)
return model.parse_obj(response["data"])


async def _get_corechannel_aggregate() -> Dict[str, Any]:
Expand Down Expand Up @@ -175,11 +167,7 @@ async def send_generate_requests(
for node in selected_nodes:
nodes.append(node.address)
url = f"{node.address}/vm/{settings.FUNCTION}/{VRF_FUNCTION_GENERATE_PATH}/{request_item_hash}"
generate_tasks.append(
asyncio.create_task(
post_node_vrf(url, VRFResponseModel.ResponseHash)
)
)
generate_tasks.append(asyncio.create_task(post_node_vrf(url, VRFResponseHash)))

vrf_generated_responses = await asyncio.gather(
*generate_tasks, return_exceptions=True
Expand All @@ -202,11 +190,7 @@ async def send_publish_requests(
f"{node}/vm/{settings.FUNCTION}"
f"/{VRF_FUNCTION_PUBLISH_PATH}/{node_message_hash}"
)
publish_tasks.append(
asyncio.create_task(
post_node_vrf(url, VRFResponseModel.RandomBytes)
)
)
publish_tasks.append(asyncio.create_task(post_node_vrf(url, VRFRandomBytes)))

vrf_publish_responses = await asyncio.gather(*publish_tasks, return_exceptions=True)
return dict(zip(nodes, vrf_publish_responses))
Expand Down
2 changes: 0 additions & 2 deletions src/aleph_vrf/executor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ async def receive_publish(hash_message: str) -> APIResponse:

message_hash = await publish_data(response_bytes, ref, account)



response_bytes.message_hash = message_hash

return APIResponse(data=response_bytes)
Expand Down
9 changes: 7 additions & 2 deletions tests/coordinator/test_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ async def test_select_random_nodes(fixture_nodes_aggregate: Dict[str, Any], mock
assert len(nodes) == 3

with pytest.raises(ValueError) as exception:
resource_nodes = fixture_nodes_aggregate["data"]["corechannel"]["resource_nodes"]
resource_nodes = fixture_nodes_aggregate["data"]["corechannel"][
"resource_nodes"
]
await select_random_nodes(len(resource_nodes))
assert str(exception.value) == f"Not enough CRNs linked, only 3 available from 4 requested"
assert (
str(exception.value)
== f"Not enough CRNs linked, only 3 available from 4 requested"
)

0 comments on commit f296f4c

Please sign in to comment.