Skip to content

Commit

Permalink
Fix: Fixed code quality issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
nesitor committed Jun 20, 2024
1 parent d00dfaf commit b3229e6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
14 changes: 10 additions & 4 deletions src/aleph_vrf/coordinator/executor_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
from aleph_message.models import ItemHash

from aleph_vrf.exceptions import AlephNetworkError, NotEnoughExecutors
from aleph_vrf.models import AlephExecutor, ComputeResourceNode, Executor, Node
from aleph_vrf.models import (
AlephExecutor,
ComputeResourceNode,
Executor,
Node,
VRFExecutor,
)
from aleph_vrf.settings import settings


Expand Down Expand Up @@ -98,7 +104,7 @@ def _get_unauthorized_nodes() -> List[str]:

return []

async def select_executors(self, nb_executors: int) -> List[Executor]:
async def select_executors(self, nb_executors: int) -> List[VRFExecutor]:
"""
Selects nb_executors compute resource nodes at random from the aleph.im network.
"""
Expand All @@ -116,10 +122,10 @@ async def select_executors(self, nb_executors: int) -> List[Executor]:
raise NotEnoughExecutors(requested=nb_executors, available=len(executors))
return random.sample(executors, nb_executors)

async def get_candidate_executors(self) -> List[Executor]:
async def get_candidate_executors(self) -> List[VRFExecutor]:
compute_nodes = self._list_compute_nodes()
blacklisted_nodes = self._get_unauthorized_nodes()
executors = [
executors: List[VRFExecutor] = [
AlephExecutor(node=node, vm_function=self.vm_function)
async for node in compute_nodes
if node.address not in blacklisted_nodes
Expand Down
8 changes: 7 additions & 1 deletion src/aleph_vrf/coordinator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

logger.debug("local imports")
from aleph_vrf.coordinator.vrf import generate_vrf
from aleph_vrf.models import APIError, APIResponse, PublishedVRFResponse, AlephExecutor, ComputeResourceNode
from aleph_vrf.models import (
AlephExecutor,
APIError,
APIResponse,
ComputeResourceNode,
PublishedVRFResponse,
)

logger.debug("imports done")

Expand Down
5 changes: 4 additions & 1 deletion src/aleph_vrf/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Generic, List, TypeVar
from typing import Generic, List, TypeAlias, TypeVar, Union

import fastapi
from aleph_message.models import ItemHash, PostMessage
Expand Down Expand Up @@ -46,6 +46,9 @@ class VRFRequest(BaseModel):
node_list_hash: str


VRFExecutor: TypeAlias = Union[Executor, AlephExecutor]


def get_vrf_request_from_message(message: PostMessage) -> VRFRequest:
content = message.content.content
try:
Expand Down
9 changes: 5 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
- https://docs.pytest.org/en/stable/fixture.html
- https://docs.pytest.org/en/stable/writing_plugins.html
"""

import multiprocessing
import os
import socket
from contextlib import contextmanager, ExitStack, AsyncExitStack
from contextlib import AsyncExitStack, ExitStack, contextmanager
from time import sleep
from typing import Union, Tuple, ContextManager
from typing import ContextManager, Tuple, Union

import aiohttp
import fastapi.applications
Expand All @@ -20,10 +21,10 @@
import uvicorn
from aleph.sdk.chains.common import generate_key
from hexbytes import HexBytes
from malicious_executor import app as malicious_executor_app
from mock_ccn import app as mock_ccn_app

from aleph_vrf.settings import settings
from mock_ccn import app as mock_ccn_app
from malicious_executor import app as malicious_executor_app


def wait_for_server(host: str, port: int, nb_retries: int = 10, wait_time: int = 0.1):
Expand Down
23 changes: 13 additions & 10 deletions tests/coordinator/test_integration_lib.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
from typing import Tuple, Dict, List
from typing import Dict, List, Tuple

import pytest
from aleph.sdk.client import AlephHttpClient
from aleph.sdk.chains.common import generate_key
from aleph.sdk.chains.ethereum import ETHAccount
from aleph_message.models import PostMessage, ItemHash
from aleph.sdk.client import AlephHttpClient
from aleph_message.models import ItemHash, PostMessage
from hexbytes import HexBytes

from aleph_vrf.coordinator.executor_selection import UsePredeterminedExecutors
from aleph_vrf.coordinator.vrf import generate_vrf, post_executor_api_request
from aleph_vrf.coordinator.vrf import send_generate_requests
from aleph_vrf.coordinator.vrf import (
generate_vrf,
post_executor_api_request,
send_generate_requests,
)
from aleph_vrf.exceptions import (
RandomNumberPublicationFailed,
HashValidationFailed,
RandomNumberGenerationFailed,
RandomNumberPublicationFailed,
)
from aleph_vrf.models import (
Executor,
Node,
VRFResponse,
PublishedVRFResponse,
PublishedVRFRandomNumberHash,
PublishedVRFRandomNumber,
PublishedVRFRandomNumberHash,
PublishedVRFResponse,
VRFResponse,
)
from aleph_vrf.utils import xor_all, verify
from aleph_vrf.utils import verify, xor_all


@pytest.fixture
Expand Down
14 changes: 7 additions & 7 deletions tests/executor/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import datetime as dt
from hashlib import sha256
from typing import Dict, Any, Union, Tuple
from typing import Any, Dict, Tuple, Union

import aiohttp
import pytest
import pytest_asyncio
from aleph.sdk.client import AlephHttpClient
from aleph_message.models import (
Chain,
ItemHash,
ItemType,
MessageType,
PostContent,
Chain,
ItemHash,
PostMessage,
)
from hexbytes import HexBytes

from aleph_vrf.models import (
VRFRequest,
PublishedVRFRandomNumber,
PublishedVRFRandomNumberHash,
VRFRandomNumber,
VRFRandomNumberHash,
VRFRequest,
VRFResponse,
VRFRandomNumber,
PublishedVRFRandomNumberHash,
PublishedVRFRandomNumber,
)
from aleph_vrf.types import Nonce, RequestId
from aleph_vrf.utils import verify
Expand Down
6 changes: 2 additions & 4 deletions tests/malicious_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
A malicious executor that returns an incorrect random number willingly.
"""


import logging
import sys

Expand All @@ -19,13 +18,12 @@
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.vm.app import AlephApp
from aleph_message.models import ItemHash

from fastapi import FastAPI, Depends
from fastapi import Depends, FastAPI

from aleph_vrf.models import (
APIResponse,
PublishedVRFRandomNumberHash,
PublishedVRFRandomNumber,
PublishedVRFRandomNumberHash,
)

http_app = FastAPI()
Expand Down
7 changes: 2 additions & 5 deletions tests/mock_ccn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import logging
from enum import Enum
from typing import Optional, Dict, Any, List
from typing import Any, Dict, List, Optional

from aleph_message.models import ItemHash
from aleph_message.status import MessageStatus
Expand Down Expand Up @@ -52,10 +52,7 @@ async def get_message(item_hash: str):
if not message:
raise HTTPException(status_code=404, detail="Message not found")

return MessageResponse(
message=message,
status="processed"
)
return MessageResponse(message=message, status="processed")


class PubMessageRequest(BaseModel):
Expand Down

0 comments on commit b3229e6

Please sign in to comment.