Skip to content

Commit efad577

Browse files
authored
[CVAT] Move escrow validation to before handling (#2814)
* Remove extra webhook validation in endpoint * Do webhook validation before handling * Allow any escrow state for ExchangeOracleEventTypes.escrow_cleaned handling * Update test
1 parent 7df2674 commit efad577

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

packages/examples/cvat/recording-oracle/src/crons/process_exchange_oracle_webhooks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import logging
22

3+
from human_protocol_sdk.constants import Status as EscrowStatus
34
from sqlalchemy.orm import Session
45

56
import src.services.webhook as oracle_db_service
7+
from src.chain.escrow import validate_escrow
68
from src.chain.kvstore import get_exchange_oracle_url
79
from src.core.config import Config
810
from src.core.types import ExchangeOracleEventTypes, OracleWebhookTypes
@@ -35,12 +37,20 @@ def handle_exchange_oracle_event(webhook: Webhook, *, db_session: Session):
3537

3638
match webhook.event_type:
3739
case ExchangeOracleEventTypes.job_finished:
40+
validate_escrow(webhook.chain_id, webhook.escrow_address)
41+
3842
validate_results(
3943
escrow_address=webhook.escrow_address,
4044
chain_id=webhook.chain_id,
4145
db_session=db_session,
4246
)
4347
case ExchangeOracleEventTypes.escrow_cleaned:
48+
validate_escrow(
49+
webhook.chain_id,
50+
webhook.escrow_address,
51+
accepted_states=list(EscrowStatus.__members__.values()),
52+
)
53+
4454
clean_escrow(
4555
db_session, escrow_address=webhook.escrow_address, chain_id=webhook.chain_id
4656
)

packages/examples/cvat/recording-oracle/src/endpoints/webhook.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from fastapi import APIRouter, Header, HTTPException, Request
22

33
import src.services.webhook as oracle_db_service
4-
from src.chain.escrow import validate_escrow
54
from src.db import SessionLocal
65
from src.schemas.webhook import OracleWebhook, OracleWebhookResponse
76
from src.validators.signature import validate_oracle_webhook_signature
@@ -17,7 +16,6 @@ async def receive_oracle_webhook(
1716
) -> OracleWebhookResponse:
1817
try:
1918
sender = await validate_oracle_webhook_signature(request, human_signature, webhook)
20-
validate_escrow(webhook.chain_id, webhook.escrow_address)
2119

2220
with SessionLocal.begin() as session:
2321
webhook_id = oracle_db_service.inbox.create_webhook(

packages/examples/cvat/recording-oracle/src/handlers/validation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ def validate_results(
214214
):
215215
logger = get_function_logger(module_logger_name)
216216

217-
escrow.validate_escrow(chain_id=chain_id, escrow_address=escrow_address)
218-
219217
manifest = parse_manifest(escrow.get_escrow_manifest(chain_id, escrow_address))
220218

221219
validator = _TaskValidator(

packages/examples/cvat/recording-oracle/tests/integration/cron/test_process_exchange_oracle_webhooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_process_exchange_oracle_webhook_escrow_cleaned(self):
8282

8383
mock_storage_client = MagicMock(spec=StorageClient)
8484
with (
85-
patch("src.chain.escrow.get_escrow"),
85+
patch("src.crons.process_exchange_oracle_webhooks.validate_escrow"),
8686
patch("src.services.cloud.make_client", return_value=mock_storage_client),
8787
):
8888
process_incoming_exchange_oracle_webhooks()

0 commit comments

Comments
 (0)