Add abi_hash to subscription.

pull/390/head
Andrey Dolgolev 2021-11-11 16:37:19 +02:00
rodzic 319b973053
commit f40a0fde1a
4 zmienionych plików z 30 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_string(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

@ -406,11 +406,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,

Wyświetl plik

@ -304,7 +304,6 @@ def days_stats(type, abi):
def crawlers_start(db_session):
token = os.getenv("MOONSTREAM_ADMIN_ACCESS_TOKEN")
# read all subscriptions
required_subscriptions: BugoutResources = bc.list_resources(