Merge pull request #390 from bugout-dev/add-crawler-of-data

Add abi_hash on subscription level.
pull/402/head
Andrei-Dolgolev 2021-11-14 07:13:20 +02:00 zatwierdzone przez GitHub
commit b5e2fab926
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 31 dodań i 9 usunięć

Wyświetl plik

@ -379,15 +379,13 @@ def dashboards_abi_validation(
return True
def validate_abi_string(abi: str) -> None:
def validate_abi_json(abi: Any) -> None:
"""
Transform string to json and run validation
"""
try:
validate_abi(json.loads(abi))
except json.JSONDecodeError:
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
validate_abi(abi)
except ValueError as e:
raise MoonstreamHTTPException(status_code=400, detail=e)
except:

Wyświetl plik

@ -334,7 +334,7 @@ async def update_dashboard_handler(
return resource
@router.get("/{dashboard_id}/data_links", tags=["dashboards"])
@router.get("/{dashboard_id}/stats", tags=["dashboards"])
async def get_dashboard_data_links_handler(
request: Request, dashboard_id: str
) -> Dict[UUID, Any]:
@ -402,11 +402,12 @@ async def get_dashboard_data_links_handler(
for subscription in dashboard_subscriptions:
hash = subscription.resource_data["abi_hash"]
available_timescales = [timescale.value for timescale in data.TimeScale]
stats[subscription.id] = {}
for timescale in available_timescales:
try:
result_key = f'contracts_data/{subscription.resource_data["address"]}/v1/{timescale}.json'
result_key = f'contracts_data/{subscription.resource_data["address"]}/{hash}/v1/{timescale}.json'
stats_presigned_url = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": SMARTCONTRACTS_ABI_BUCKET, "Key": result_key},

Wyświetl plik

@ -1,6 +1,7 @@
"""
The Moonstream subscriptions HTTP API
"""
import hashlib
import logging
import json
from typing import List, Optional, Dict, Any
@ -13,7 +14,7 @@ from fastapi import APIRouter, Depends, Request, Form
from web3 import Web3
from ..actions import (
validate_abi_string,
validate_abi_json,
upload_abi_to_s3,
)
from ..admin import subscription_types
@ -112,10 +113,21 @@ async def add_subscription_handler(
if abi:
validate_abi_string(abi=abi)
try:
json_abi = json.loads(abi)
except json.JSONDecodeError:
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
validate_abi_json(json_abi)
update_resource = upload_abi_to_s3(resource=resource, abi=abi, update={})
abi_string = json.dumps(json_abi, sort_keys=True, indent=2)
hash = hashlib.md5(abi_string.encode("utf-8")).hexdigest()
update_resource["abi_hash"] = hash
try:
updated_resource: BugoutResource = bc.update_resource(
token=token,
@ -241,7 +253,16 @@ async def update_subscriptions_handler(
if abi:
validate_abi_string(abi=abi)
try:
json_abi = json.loads(abi)
except json.JSONDecodeError:
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
validate_abi_json(json_abi)
abi_string = json.dumps(json_abi, sort_keys=True, indent=2)
hash = hashlib.md5(abi_string.encode("utf-8")).hexdigest()
try:
subscription_resource: BugoutResource = bc.get_resource(
@ -264,6 +285,8 @@ async def update_subscriptions_handler(
resource=subscription_resource, abi=abi, update=update
)
update["abi_hash"] = hash
try:
resource: BugoutResource = bc.update_resource(
token=token,