Skip to content

Commit

Permalink
[CVAT][Exchange Oracle] Refactor structure (#787)
Browse files Browse the repository at this point in the history
* Refactor Exchange Oracle structure
  • Loading branch information
flopez7 authored Aug 16, 2023
1 parent a8f9e5b commit 7beafd6
Show file tree
Hide file tree
Showing 62 changed files with 294 additions and 297 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,4 @@ build
dist

#cache
cache

#Postgres Data
db
cache
5 changes: 4 additions & 1 deletion packages/apps/job-launcher/server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ lerna-debug.log*
!.vscode/extensions.json

.env.development
.env.production
.env.production

#Postgres Data
db
9 changes: 5 additions & 4 deletions packages/examples/cvat/exchange-oracle/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from alembic import context

from src.config import Config
from src.core.config import Config

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -22,9 +22,10 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from src.database import Base
from src.modules.oracle_webhook.model import Webhook
from src.modules.cvat.model import Task, Job

from src.models.cvat import Task, Job
from src.models.webhook import Webhook
from src.db import Base

target_metadata = Base.metadata

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
blockchain-node:
build:
context: ./
dockerfile: blockchain-node.Dockerfile
dockerfile: dockerfiles/blockchain-node.Dockerfile
healthcheck:
# Using a magic nubmer of 28 here because this is a block number when blockchain-node container is ready to use
test: if [ $(( $(wget -q --post-data='{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' -O- http://blockchain-node:8545 | grep -o '"result":"[^"]*"' | awk -F'"' '{print $4}' ) )) -ge 28 ]; then exit 0; else exit 1; fi
Expand Down Expand Up @@ -70,7 +70,7 @@ services:
test:
build:
context: ./
dockerfile: test.Dockerfile
dockerfile: dockerfiles/test.Dockerfile
environment:
SQLALCHEMY_SILENCE_UBER_WARNING: 1
ENVIRONMENT: 'test'
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/cvat/exchange-oracle/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uvicorn
from src.config import Config
from src.core.config import Config

if __name__ == "__main__":
is_dev = Config.environment == "development"
Expand Down
7 changes: 3 additions & 4 deletions packages/examples/cvat/exchange-oracle/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
from fastapi import FastAPI

import src.log
from src.api import init_api
from src.error_handlers import setup_error_handlers
from src.cron import setup_cron_jobs
from src.config import Config
from src.handlers.error_handlers import setup_error_handlers
from src.crons import setup_cron_jobs
from src.core.config import Config


app = FastAPI(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
""" API endpoints """
from fastapi import APIRouter, FastAPI

from src.api_schema import ValidationErrorResponse, ResponseError, MetaResponse
from src.config import Config
from src.schemas import ValidationErrorResponse, ResponseError, MetaResponse
from src.core.config import Config

from src.modules.api import router
from src.api.cvat import router as cvat_router
from src.api.webhook import router as webhook_router


greet_router = APIRouter()
Expand Down Expand Up @@ -44,6 +45,7 @@ def init_api(app: FastAPI) -> FastAPI:
}

app.include_router(greet_router)
app.include_router(router, prefix="/webhook", responses=default_responses)
app.include_router(cvat_router, responses=default_responses)
app.include_router(webhook_router, responses=default_responses)

return app
19 changes: 19 additions & 0 deletions packages/examples/cvat/exchange-oracle/src/api/cvat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from fastapi import APIRouter, Header, Request
from src.handlers.webhook import cvat_webhook_handler
from src.schemas.cvat import CvatWebhook
from src.validators.signature import validate_cvat_signature

router = APIRouter()


@router.post(
"/cvat",
description="Consumes a webhook from a cvat",
)
async def cvat_webhook(
cvat_webhook: CvatWebhook,
request: Request,
x_signature_256: str = Header(),
):
await validate_cvat_signature(request, x_signature_256)
cvat_webhook_handler(cvat_webhook)
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from fastapi import APIRouter, HTTPException, Header, Request
from typing import Union

from src.modules.api_schema import OracleWebhook, OracleWebhookResponse, CvatWebhook
from src.database import SessionLocal

from src.modules.chain.escrow import validate_escrow
from src.validators.signature import validate_webhook_signature, validate_cvat_signature

from src.modules.cvat.handlers.webhook import cvat_webhook_handler
from src.modules.oracle_webhook.service import create_webhook
from src.modules.oracle_webhook.constants import OracleWebhookTypes
from fastapi import APIRouter, Header, HTTPException, Request
from src.chain.escrow import validate_escrow
from src.core.constants import OracleWebhookTypes
from src.db import SessionLocal
from src.schemas.webhook import OracleWebhook, OracleWebhookResponse
from src.services.webhook import create_webhook
from src.validators.signature import validate_webhook_signature

router = APIRouter()

Expand Down Expand Up @@ -39,16 +36,3 @@ async def job_launcher_webhook(
return OracleWebhookResponse(id=webhook_id)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))


@router.post(
"/cvat",
description="Consumes a webhook from a cvat",
)
async def cvat_webhook(
cvat_webhook: CvatWebhook,
request: Request,
x_signature_256: str = Header(),
):
await validate_cvat_signature(request, x_signature_256)
cvat_webhook_handler(cvat_webhook)
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import json

from web3 import Web3

from human_protocol_sdk.constants import Status
from human_protocol_sdk.escrow import EscrowClient
from human_protocol_sdk.storage import StorageClient

from src.modules.chain.web3 import get_web3
from src.chain.web3 import get_web3


def validate_escrow(chain_id: int, escrow_address: str) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from human_protocol_sdk.escrow import EscrowClient
from human_protocol_sdk.kvstore import KVStoreClient

from src.modules.chain.web3 import get_web3
from src.chain.web3 import get_web3


def get_recording_oracle_url(chain_id: int, escrow_address: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from eth_account.messages import encode_defunct

from src.constants import Networks
from src.config import Config
from src.core.constants import Networks
from src.core.config import Config


def get_web3(chain_id: Networks):
Expand Down
8 changes: 0 additions & 8 deletions packages/examples/cvat/exchange-oracle/src/constants.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from enum import Enum
from src.core.config import Config


class Networks(int, Enum):
polygon_mainnet = Config.polygon_mainnet.chain_id
polygon_mumbai = Config.polygon_mumbai.chain_id
localhost = Config.localhost.chain_id


class EventTypes(str, Enum):
Expand Down Expand Up @@ -34,3 +41,14 @@ class CvatLabelTypes(str, Enum):
class Providers(str, Enum):
aws = "AWS_S3_BUCKET"
gcs = "GOOGLE_CLOUD_STORAGE"


class OracleWebhookTypes(str, Enum):
job_launcher = "job_launcher"
recording_oracle = "recoring_oracle"


class OracleWebhookStatuses(str, Enum):
pending = "pending"
completed = "completed"
failed = "failed"
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from fastapi import FastAPI
from apscheduler.schedulers.background import BackgroundScheduler

from src.config import Config
from src.modules.cvat.jobs.track_completed import (
from src.core.config import Config
from src.crons.track_completed import (
track_completed_projects,
track_completed_tasks,
)
from src.modules.cvat.jobs.retrieve_annotations import retrieve_annotations
from src.modules.oracle_webhook.jobs.process_job_launcher_webhooks import (
from src.crons.retrieve_annotations import retrieve_annotations
from src.crons.process_job_launcher_webhooks import (
process_job_launcher_webhooks,
)
from src.modules.oracle_webhook.jobs.process_recording_oracle_webhooks import (
from src.crons.process_recording_oracle_webhooks import (
process_recording_oracle_webhooks,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import logging

from src.database import SessionLocal
from src.config import CronConfig
from src.db import SessionLocal
from src.core.config import CronConfig

from src.modules.cvat.job_flows import job_creation_process, revert_job_creation
from src.modules.chain.escrow import get_escrow_manifest, validate_escrow
from src.cvat.create_job import job_creation_process
from src.cvat.revert_job import revert_job_creation
from src.chain.escrow import get_escrow_manifest, validate_escrow

from src.modules.oracle_webhook.constants import OracleWebhookTypes
import src.modules.oracle_webhook.service as db_service
from src.core.constants import OracleWebhookTypes
import src.services.webhook as db_service


LOG_MODULE = "[cron][webhook][process_job_launcher_webhooks]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import httpx
import logging

from src.database import SessionLocal
from src.config import CronConfig
from src.db import SessionLocal
from src.core.config import CronConfig

from src.modules.chain.kvstore import get_recording_oracle_url
from src.chain.kvstore import get_recording_oracle_url

from src.modules.oracle_webhook.constants import OracleWebhookTypes
from src.modules.oracle_webhook.helpers import prepare_recording_oracle_webhook_body
from src.core.constants import OracleWebhookTypes
from src.utils.helpers import prepare_recording_oracle_webhook_body

import src.modules.oracle_webhook.service as db_service
import src.services.webhook as db_service


LOG_MODULE = "[cron][webhook][process_recording_oracle_webhooks]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import logging

from human_protocol_sdk.storage import StorageClient, Credentials

from src.database import SessionLocal
from src.config import CronConfig, StorageConfig

from src.modules.cvat.constants import ProjectStatuses
from src.modules.cvat.handlers.annotation import get_annotations_handler

from src.modules.oracle_webhook.constants import OracleWebhookTypes
from src.modules.oracle_webhook.helpers import prepare_signature

import src.modules.cvat.api_calls as cvat_api
import src.modules.cvat.service as cvat_db_service
import src.modules.oracle_webhook.service as oracle_db_service

import src.cvat.api_calls as cvat_api
import src.services.cvat as cvat_db_service
import src.services.webhook as oracle_db_service
from human_protocol_sdk.storage import Credentials, StorageClient
from src.core.config import CronConfig, StorageConfig
from src.core.constants import OracleWebhookTypes, ProjectStatuses
from src.db import SessionLocal
from src.handlers.annotation import get_annotations_handler
from src.utils.helpers import prepare_signature

LOG_MODULE = "[cron][cvat][retrieve_annotations]"
logger = logging.getLogger("app")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging

from src.database import SessionLocal
from src.config import CronConfig
from src.db import SessionLocal
from src.core.config import CronConfig

from src.modules.cvat.constants import ProjectStatuses, TaskStatuses, JobStatuses
import src.modules.cvat.service as cvat_service
from src.core.constants import ProjectStatuses, TaskStatuses, JobStatuses
import src.services.cvat as cvat_service

LOG_MODULE = "[cron][cvat]"
logger = logging.getLogger("app")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from http import HTTPStatus
from typing import Dict, List

from src.config import Config
from src.core.config import Config
from cvat_sdk.api_client import Configuration, ApiClient, models, exceptions

configuration = Configuration(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from src.database import SessionLocal
from src.db import SessionLocal

from src.modules.cvat.helpers import parse_manifest, compose_bucket_url
from src.utils.helpers import parse_manifest, compose_bucket_url

import src.modules.cvat.service as db_service
import src.modules.cvat.api_calls as cvat_api
import src.services.cvat as db_service
import src.cvat.api_calls as cvat_api


def job_creation_process(escrow_address: str, chain_id: int, manifest: dict) -> None:
Expand Down Expand Up @@ -31,15 +31,3 @@ def job_creation_process(escrow_address: str, chain_id: int, manifest: dict) ->

# Actual job creation on CVAT. Async process (will be created in DB once 'update:task' or 'update:job' webhook is received)
cvat_api.put_task_data(task.id, cloudstorage.id)


def revert_job_creation(escrow_address: str) -> None:
with SessionLocal.begin() as session:
project = db_service.get_project_by_escrow_address(session, escrow_address)
if project is not None:
if project.cvat_cloudstorage_id:
cvat_api.delete_cloustorage(project.cvat_cloudstorage_id)
if project.cvat_id:
cvat_api.delete_project(project.cvat_id)
db_service.delete_project(session, project.id)
session.commit()
Loading

0 comments on commit 7beafd6

Please sign in to comment.