Skip to content

feat: updated implementation Data Service API #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions backend-services/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@
!tests/__init__.py
!tests/data_service/__init__.py
!tests/data_service/operations/__init__.py
!tests/data_service/operations/helpers.py
!tests/data_service/operations/test_bookmark.py
!tox.ini
12 changes: 6 additions & 6 deletions backend-services/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Wallet Backend Services
# Trusted Compute Data Services

An HTTP server backend for the Nautilus Wallet powered by [FastAPI][fastapi] and
the [uvicorn ASGI server][uvicorn].
Expand Down Expand Up @@ -51,7 +51,7 @@ eval "$(pyenv init -)"
```

Make sure to log out (and back in of course) or, alternatively, restart your
machine. Once you are back in the `backend-services` sub-directory of the
machine. Once you are back in the `backend-services` subdirectory of the
project run

```shell
Expand All @@ -71,11 +71,11 @@ poetry install

Make sure the following environment variables have been set in your local `.env` file:

- `WALLET_DB_CONNECTION_STRING`
- `WALLET_DB_DATABASE_NAME`
- `WALLET_BOOKMARK_DB_COLLECTION_NAME`
- `PRIMARY_ORIGIN`
- `VAULT_DB_CONNECTION_STRING`
- `VAULT_DB_NAME`

For examples you may consult the [python-dotenv] documentation. Once this is done, a local instance of the server may be started on `localhost:8000` by running
For examples, you may consult the [python-dotenv] documentation. Once this is done, a local instance of the server may be started on `localhost:8000` by running

```shell
poetry run uvicorn web_asgi.main:app
Expand Down
27 changes: 24 additions & 3 deletions backend-services/src/data_service/operations/datapool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
from odmantic import ObjectId

from common.types import WalletAddress
from data_service.schema.actions import CreateDatapool, DeleteDatapool
from data_service.schema.actions import CreateDatapool, UpdateDataPool, DeleteDatapool
from data_service.schema.entities import Datapool, DatapoolList
from data_service.schema.types import Engine


async def create_datapool(engine: Engine, params: CreateDatapool) -> Datapool:
async def create_datapool(engine: Engine, params: CreateDatapool) -> None:
"""
Create a new datapool.
"""
new_datapool = Datapool(
creator_wallet_id=params.creator_wallet_id,
name=params.name,
description=params.description,
datapool_schema=params.datapool_schema,
datapool_hash=params.datapool_hash,
smart_contract_id=params.smart_contract_id,
smart_contract_address=params.smart_contract_address,
Expand All @@ -23,7 +24,27 @@ async def create_datapool(engine: Engine, params: CreateDatapool) -> Datapool:
created=params.created,
)
await engine.save(new_datapool)
return new_datapool


async def update_datapool(engine: Engine, params: UpdateDataPool) -> None:
"""
Update existing datapool
"""
existing_datapool = await engine.find_one(
Datapool,
(UpdateDataPool.application_id == params.application_id),
)

if existing_datapool is None:
raise HTTPException(404)

if existing_datapool:
existing_datapool.sealed_data = (params.sealed_data,)
existing_datapool.contribution_token_id = (params.contribution_token_id,)
existing_datapool.ref_contributors = (params.ref_contributors,)
await engine.save(existing_datapool)
else:
pass


async def delete_datapool(engine: Engine, params: DeleteDatapool) -> None:
Expand Down
38 changes: 0 additions & 38 deletions backend-services/src/data_service/operations/dataschema.py

This file was deleted.

51 changes: 0 additions & 51 deletions backend-services/src/data_service/operations/dataset.py

This file was deleted.

35 changes: 35 additions & 0 deletions backend-services/src/data_service/operations/wasm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from fastapi import HTTPException
from odmantic import ObjectId

from data_service.schema.actions import CreateWasmBinary, DeleteWasmBinary
from data_service.schema.entities import WasmBinary, WasmBinaryList
from data_service.schema.types import Engine


async def create_wasm_binary(engine: Engine, params: CreateWasmBinary) -> None:
"""
Store a new WASM Binary.
"""
new_wasm_binary = WasmBinary(name=params.name, wasm_binary=params.wasm_binary)
await engine.save(new_wasm_binary)


async def get_wasm_binary(engine: Engine, name: str) -> WasmBinaryList:
"""
Retrieve a WASM BInary.
"""
return await engine.find(WasmBinary, WasmBinary.name == name)


async def delete_wasm_binary(engine: Engine, params: DeleteWasmBinary) -> None:
"""
Delete a specified WASM Binary.
"""
# XXX: assumes `params.id` is a 24 character hex string
id_to_delete = ObjectId(params.delete_id)
existing_wasm_binary = await engine.find_one(
WasmBinary, WasmBinary.id == id_to_delete
)
if existing_wasm_binary is None:
raise HTTPException(404)
await engine.delete(existing_wasm_binary)
68 changes: 18 additions & 50 deletions backend-services/src/data_service/schema/actions.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,34 @@
from datetime import datetime

from pydantic import BaseModel, validator

from common.types import WalletAddress


class CreateDataset(BaseModel):
class CreateDatapool(BaseModel):
"""
Datset creation parameters.
Datapool creation parameters.
"""

wallet_id: WalletAddress
data_pool_id: str
data_schema_id: str
application_id: str
creator_wallet_id: WalletAddress
name: str
description: str
num_of_rows: int
data_pool_position: int
created: datetime


class DeleteDataset(BaseModel):
"""
Dataset deletion parameters.
"""

delete_id: str

@validator("delete_id")
@classmethod
def valid_object_id_hex_representation(cls: type, v: str) -> str:
int(v, 16)
if len(v) != 24:
raise AssertionError(
f"expected a 24 character hexadecimal string but '{v}' has length {len(v)}"
)
return v
datapool_schema: str
sealed_data: str
ref_drt_id: list[str]
contribution_token_id: str
append_token_id: str
ref_contributors: list[str]


class CreateDatapool(BaseModel):
class UpdateDataPool(BaseModel):
"""
Datpool creation parameters.
Datapool update parameters
"""

creator_wallet_id: WalletAddress
name: str
description: str
datapool_hash: str
smart_contract_id: str
smart_contract_address: str
application_id: str
sealed_data: str
total_rows: int
created: datetime
contribution_token_id: str
ref_contributors: str


class DeleteDatapool(BaseModel):
Expand All @@ -72,21 +49,12 @@ def valid_object_id_hex_representation(cls: type, v: str) -> str:
return v


class CreateDataschema(BaseModel):
"""
Schema creation parameters.
"""

class CreateWasmBinary(BaseModel):
name: str
data_schema: str
created: datetime

wasm_binary: str

class DeleteDataschema(BaseModel):
"""
Datapool deletion parameters.
"""

class DeleteWasmBinary(BaseModel):
delete_id: str

@validator("delete_id")
Expand Down
49 changes: 19 additions & 30 deletions backend-services/src/data_service/schema/entities.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,45 @@
from datetime import datetime
from typing import TypeAlias

from odmantic import Model

from common.types import WalletAddress


class Dataset(Model):
class Datapool(Model):
"""
An address on the ledger dataseted by the user.
"""

wallet_id: WalletAddress
data_pool_id: str
data_schema_id: str
application_id: str
creator_wallet_id: WalletAddress
name: str
description: str
num_of_rows: int
data_pool_position: int
created: datetime
datapool_schema: str
sealed_data: str
ref_drt_id: list[str]
contribution_token_id: str
append_token_id: str
ref_contributors: list[str]


DatasetList: TypeAlias = list[Dataset]
DatapoolList: TypeAlias = list[Datapool]


class Datapool(Model):
"""
An address on the ledger dataseted by the user.
"""

creator_wallet_id: WalletAddress
class Drt(Model):
asset_id: str
name: str
description: str
datapool_hash: str
smart_contract_id: str
smart_contract_address: str
sealed_data: str
total_rows: int
created: datetime
url_binary: str
price: float
amount_created: int


DatapoolList: TypeAlias = list[Datapool]
DrtList: TypeAlias = list[Drt]


class Dataschema(Model):
"""
A JSON schema for a dataset of datapool.
"""

class WasmBinary(Model):
name: str
data_schema: str
created: datetime
wasm_binary: str


DataschemaList: TypeAlias = list[Dataschema]
WasmBinaryList: TypeAlias = list[WasmBinary]
Loading