Skip to content

Commit

Permalink
Allow use major versions of Aleph SDK (#27)
Browse files Browse the repository at this point in the history
* Problem: Having a strong dependency to an exact aleph-python-sdk library version creates a dependency hell.

Solution: Allow to use the same and major version of aleph-python-sdk library.

* Fix: Update entire code to major SDK version, cause the braking changes.

* Fix: Added mock for new message endpoint.

* Fix: Put correct dependency tag, to update minor and patch version, but not major.

* Fix: Requested by Mike, allow to update just the patch version, but not major or minor.

* Fix: Update latest VRF methods with new SDK interface.
  • Loading branch information
nesitor authored Jan 31, 2024
1 parent dfd2194 commit 8ffda9e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 31 deletions.
8 changes: 4 additions & 4 deletions deployment/deploy_vrf_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tempfile import TemporaryDirectory
from typing import Optional, Tuple, Dict

from aleph.sdk import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.chains.common import get_fallback_private_key
from aleph.sdk.chains.ethereum import ETHAccount
from aleph_message.models import ItemHash, ProgramMessage
Expand All @@ -30,7 +30,7 @@ def mksquashfs(path: Path, destination: Path) -> None:


async def upload_dir_as_volume(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
dir_path: Path,
channel: str,
volume_path: Optional[Path] = None,
Expand All @@ -52,7 +52,7 @@ async def upload_dir_as_volume(


async def deploy_python_program(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
code_volume_hash: ItemHash,
entrypoint: str,
venv_hash: ItemHash,
Expand Down Expand Up @@ -101,7 +101,7 @@ async def deploy_vrf(
account = ETHAccount(private_key)
channel = "vrf-tests"

async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as aleph_client:
# Upload the code and venv volumes
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package_dir =
# For more information, check out https://semver.org/.
install_requires =
aiohttp
aleph-sdk-python==0.7.0
aleph-sdk-python~=0.8.0
hexbytes
fastapi>=0.95.1
importlib-metadata; python_version<"3.8"
Expand Down
23 changes: 13 additions & 10 deletions src/aleph_vrf/coordinator/vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import aiohttp
from aleph.sdk.chains.ethereum import ETHAccount
from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.query.filters import MessageFilter
from aleph_message.models import ItemHash, MessageType, PostMessage
from aleph_message.status import MessageStatus
from hexbytes import HexBytes
Expand Down Expand Up @@ -78,7 +79,7 @@ async def prepare_executor_api_request(url: str) -> bool:


async def _generate_vrf(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
nb_executors: int,
nb_bytes: int,
vrf_function: ItemHash,
Expand Down Expand Up @@ -165,7 +166,7 @@ async def generate_vrf(
):
vrf_function = vrf_function or settings.FUNCTION

async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account,
api_server=aleph_api_server or settings.API_HOST,
# Avoid going through the VM connector on aleph.im CRNs
Expand Down Expand Up @@ -292,7 +293,7 @@ def generate_final_vrf(


async def publish_data(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
data: Union[VRFRequest, VRFResponse],
ref: str,
) -> ItemHash:
Expand All @@ -317,7 +318,7 @@ async def publish_data(


async def get_existing_vrf_message(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
request_id: str,
) -> Optional[PostMessage]:
channel = f"vrf_{request_id}"
Expand All @@ -328,9 +329,11 @@ async def get_existing_vrf_message(
)

messages = await aleph_client.get_messages(
message_type=MessageType.post,
channels=[channel],
refs=[ref],
message_filter=MessageFilter(
message_types=[MessageType.post],
channels=[channel],
refs=[ref],
)
)

if messages.messages:
Expand All @@ -343,7 +346,7 @@ async def get_existing_vrf_message(


async def get_existing_message(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
item_hash: ItemHash,
) -> Optional[PostMessage]:
logger.debug(
Expand All @@ -363,7 +366,7 @@ async def get_existing_message(


async def check_message_integrity(
aleph_client: AuthenticatedAlephClient, vrf_response: PublishedVRFResponse
aleph_client: AuthenticatedAlephHttpClient, vrf_response: PublishedVRFResponse
):
logger.debug(
f"Checking VRF response message on {aleph_client.api_server} for item_hash {vrf_response.message_hash}"
Expand Down
14 changes: 7 additions & 7 deletions src/aleph_vrf/executor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
logger = logging.getLogger(__name__)

logger.debug("import aleph_client")
from aleph.sdk.client import AlephClient, AuthenticatedAlephClient
from aleph.sdk.client import AlephHttpClient, AuthenticatedAlephHttpClient
from aleph.sdk.vm.app import AlephApp
from aleph_message.models import ItemHash, PostMessage
from aleph_message.status import MessageStatus
Expand Down Expand Up @@ -52,9 +52,9 @@
app = AlephApp(http_app=http_app)


async def authenticated_aleph_client() -> AuthenticatedAlephClient:
async def authenticated_aleph_client() -> AuthenticatedAlephHttpClient:
account = settings.aleph_account()
async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account,
api_server=settings.API_HOST,
# Avoid going through the VM connector on aleph.im CRNs
Expand All @@ -71,7 +71,7 @@ async def index():
}


async def _get_message(client: AlephClient, item_hash: ItemHash) -> PostMessage:
async def _get_message(client: AlephHttpClient, item_hash: ItemHash) -> PostMessage:
try:
return await client.get_message(item_hash=item_hash, message_type=PostMessage)
except MessageNotFoundError:
Expand All @@ -93,7 +93,7 @@ async def _get_message(client: AlephClient, item_hash: ItemHash) -> PostMessage:
async def receive_generate(
vrf_request_hash: ItemHash,
aleph_client: Annotated[
AuthenticatedAlephClient, Depends(authenticated_aleph_client)
AuthenticatedAlephHttpClient, Depends(authenticated_aleph_client)
],
) -> APIResponse[PublishedVRFRandomNumberHash]:
"""
Expand Down Expand Up @@ -152,7 +152,7 @@ async def receive_generate(
async def receive_publish(
message_hash: ItemHash,
aleph_client: Annotated[
AuthenticatedAlephClient, Depends(authenticated_aleph_client)
AuthenticatedAlephHttpClient, Depends(authenticated_aleph_client)
],
) -> APIResponse[PublishedVRFRandomNumber]:
"""
Expand Down Expand Up @@ -197,7 +197,7 @@ async def receive_publish(


async def publish_data(
aleph_client: AuthenticatedAlephClient,
aleph_client: AuthenticatedAlephHttpClient,
data: Union[VRFRandomNumberHash, VRFRandomNumber],
ref: str,
) -> ItemHash:
Expand Down
4 changes: 2 additions & 2 deletions tests/coordinator/test_integration_lib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Tuple, Dict, List

import pytest
from aleph.sdk import AlephClient
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
Expand Down Expand Up @@ -76,7 +76,7 @@ async def assert_aleph_message_matches_vrf_response(
) -> PostMessage:
assert vrf_response.message_hash

async with AlephClient(api_server=ccn_url) as client:
async with AlephHttpClient(api_server=ccn_url) as client:
message = await client.get_message(
vrf_response.message_hash, message_type=PostMessage
)
Expand Down
6 changes: 3 additions & 3 deletions tests/executor/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import aiohttp
import pytest
import pytest_asyncio
from aleph.sdk import AlephClient
from aleph.sdk.client import AlephHttpClient
from aleph_message.models import (
ItemType,
MessageType,
Expand Down Expand Up @@ -156,7 +156,7 @@ async def assert_aleph_message_matches_random_number_hash(
) -> PostMessage:
assert random_number_hash.message_hash

async with AlephClient(api_server=ccn_url) as client:
async with AlephHttpClient(api_server=ccn_url) as client:
message = await client.get_message(
random_number_hash.message_hash, message_type=PostMessage
)
Expand All @@ -182,7 +182,7 @@ async def assert_aleph_message_matches_random_number(
ccn_url: Any, # aiohttp does not expose its URL type
random_number: PublishedVRFRandomNumber,
) -> PostMessage:
async with AlephClient(api_server=ccn_url) as client:
async with AlephHttpClient(api_server=ccn_url) as client:
message = await client.get_message(
random_number.message_hash, message_type=PostMessage
)
Expand Down
6 changes: 3 additions & 3 deletions tests/malicious_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

logger = logging.getLogger(__name__)

from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.vm.app import AlephApp
from aleph_message.models import ItemHash

Expand All @@ -36,7 +36,7 @@
async def receive_generate(
vrf_request_hash: ItemHash,
aleph_client: Annotated[
AuthenticatedAlephClient, Depends(authenticated_aleph_client)
AuthenticatedAlephHttpClient, Depends(authenticated_aleph_client)
],
) -> APIResponse[PublishedVRFRandomNumberHash]:
from aleph_vrf.executor.main import receive_generate as real_receive_generate
Expand All @@ -50,7 +50,7 @@ async def receive_generate(
async def receive_publish(
message_hash: ItemHash,
aleph_client: Annotated[
AuthenticatedAlephClient, Depends(authenticated_aleph_client)
AuthenticatedAlephHttpClient, Depends(authenticated_aleph_client)
],
) -> APIResponse[PublishedVRFRandomNumber]:
from aleph_vrf.executor.main import receive_publish as real_receive_publish
Expand Down
25 changes: 24 additions & 1 deletion tests/mock_ccn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from aleph_message.models import ItemHash
from aleph_message.status import MessageStatus
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

logger = logging.getLogger(__name__)
Expand All @@ -22,6 +22,9 @@

@app.get("/api/v0/messages.json")
async def get_messages(hashes: Optional[str], page: int = 1, pagination: int = 20):
"""
Mock messages.json endpoint
"""
hashes = [ItemHash(h) for h in hashes.split(",")] if hashes is not None else []
messages = [MESSAGES[item_hash] for item_hash in hashes if item_hash in MESSAGES]
paginated_messages = messages[(page - 1) * pagination : page * pagination]
Expand All @@ -35,6 +38,26 @@ async def get_messages(hashes: Optional[str], page: int = 1, pagination: int = 2
}


class MessageResponse(BaseModel):
message: Dict[str, Any]
status: str


@app.get("/api/v0/messages/{item_hash}")
async def get_message(item_hash: str):
"""
Mock individual view message endpoint
"""
message = MESSAGES.get(ItemHash(item_hash))
if not message:
raise HTTPException(status_code=404, detail="Message not found")

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


class PubMessageRequest(BaseModel):
sync: bool = False
message_dict: Dict[str, Any] = Field(alias="message")
Expand Down

0 comments on commit 8ffda9e

Please sign in to comment.