Add dedublication.

pull/342/head
Andrey Dolgolev 2021-11-09 14:33:10 +02:00
rodzic 4838497aa5
commit 1e3421b5e3
2 zmienionych plików z 67 dodań i 63 usunięć

Wyświetl plik

@ -6,6 +6,7 @@ import uuid
import boto3 # type: ignore
from bugout.data import BugoutSearchResults
from bugout.exceptions import BugoutResponseException
from bugout.journal import SearchOrder
from ens.utils import is_valid_ens_name # type: ignore
from eth_utils.address import is_address # type: ignore
@ -14,7 +15,10 @@ from moonstreamdb.models import (
)
from sqlalchemy import text
from sqlalchemy.orm import Session
from web3._utils.validation import validate_abi
from .middleware import MoonstreamHTTPException
from . import data
from .reporter import reporter
from .settings import ETHERSCAN_SMARTCONTRACTS_BUCKET
@ -25,6 +29,8 @@ from .settings import (
BUGOUT_REQUEST_TIMEOUT_SECONDS,
MOONSTREAM_ADMIN_ACCESS_TOKEN,
MOONSTREAM_DATA_JOURNAL_ID,
AWS_S3_SMARTCONTRACTS_ABI_BUCKET,
AWS_S3_SMARTCONTRACTS_ABI_PREFIX,
)
from web3 import Web3
@ -263,3 +269,55 @@ def check_api_status():
)
return crawl_types_timestamp
def validate_abi_string(abi: str) -> 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.")
except ValueError as e:
raise MoonstreamHTTPException(status_code=400, detail=e)
except:
raise MoonstreamHTTPException(
status_code=400, detail="Error on abi valiadation."
)
def upload_abi_to_s3(
resource: BugoutResource,
abi: str,
update: Dict[str, Any],
) -> Dict[str, Any]:
"""
Uploading ABI to s3 bucket. Return object for updating resource.
"""
s3_client = boto3.client("s3")
bucket = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
result_bytes = abi.encode("utf-8")
result_key = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{resource.resource_data['address']}/{resource.id}/abi.json"
s3_client.put_object(
Body=result_bytes,
Bucket=bucket,
Key=result_key,
ContentType="application/json",
Metadata={"Moonstream": "Abi data"},
)
update["abi"] = True
update["bucket"] = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
update[
"s3_path"
] = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{resource.resource_data['address']}/{resource.id}/abi.json"
return update

Wyświetl plik

@ -11,8 +11,11 @@ from bugout.data import BugoutResource, BugoutResources
from bugout.exceptions import BugoutResponseException
from fastapi import APIRouter, Depends, Request, Form
from web3 import Web3
from web3._utils.validation import validate_abi
from ..actions import (
validate_abi_string,
upload_abi_to_s3,
)
from ..admin import subscription_types
from .. import data
from ..middleware import MoonstreamHTTPException
@ -109,39 +112,9 @@ async def add_subscription_handler(
if abi:
try:
validate_abi(json.loads(abi))
except json.JSONDecodeError:
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
except ValueError as e:
raise MoonstreamHTTPException(status_code=400, detail=e)
except:
raise MoonstreamHTTPException(
status_code=400, detail="Error on abi valiadation."
)
s3_client = boto3.client("s3")
validate_abi_string(abi=abi)
bucket = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
result_bytes = abi.encode("utf-8")
result_key = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{resource.resource_data['address']}/{resource.id}/abi.json"
s3_client.put_object(
Body=result_bytes,
Bucket=bucket,
Key=result_key,
ContentType="application/json",
Metadata={"Moonstream": "Abi data"},
)
update_resource: Dict[str, Any] = {}
update_resource["abi"] = True
update_resource["bucket"] = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
update_resource[
"s3_path"
] = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{resource.resource_data['address']}/{resource.id}/abi.json"
update_resource = upload_abi_to_s3(resource=resource, abi=abi, update={})
try:
updated_resource: BugoutResource = bc.update_resource(
@ -268,16 +241,7 @@ async def update_subscriptions_handler(
if abi:
try:
validate_abi(json.loads(abi))
except json.JSONDecodeError:
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
except ValueError as e:
raise MoonstreamHTTPException(status_code=400, detail=e)
except:
raise MoonstreamHTTPException(
status_code=400, detail="Error on abi valiadation."
)
validate_abi_string(abi=abi)
try:
subscription_resource: BugoutResource = bc.get_resource(
@ -296,28 +260,10 @@ async def update_subscriptions_handler(
detail="Subscription already have ABI. For add a new ABI create new subscription.",
)
s3_client = boto3.client("s3")
bucket = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
result_bytes = abi.encode("utf-8")
result_key = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{subscription_resource.resource_data['address']}/{subscription_resource.id}/abi.json"
s3_client.put_object(
Body=result_bytes,
Bucket=bucket,
Key=result_key,
ContentType="application/json",
Metadata={"Moonstream": "Abi data"},
update = upload_abi_to_s3(
resource=subscription_resource, abi=abi, update=update
)
update["abi"] = True
update["bucket"] = AWS_S3_SMARTCONTRACTS_ABI_BUCKET
update[
"s3_path"
] = f"{AWS_S3_SMARTCONTRACTS_ABI_PREFIX}/{subscription_resource.resource_data['address']}/{subscription_resource.id}/abi.json"
try:
resource: BugoutResource = bc.update_resource(
token=token,