Skip to content

Commit

Permalink
feat: get vrf requests status in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt committed Jun 26, 2024
1 parent 4c5b958 commit fc3d52a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pragma/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pragma.core.abis import ABIS
from pragma.core.contract import Contract
from pragma.core.logger import get_stream_logger
from pragma.core.mixins import (
NonceMixin,
OracleMixin,
Expand All @@ -22,8 +23,7 @@
get_client_from_network,
)

logging.basicConfig()
logger = logging.getLogger(__name__)
logger = get_stream_logger()
logger.setLevel(logging.INFO)


Expand Down
4 changes: 4 additions & 0 deletions pragma/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@


def get_stream_logger():
global logger

logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(stdout)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
stream_handler.setFormatter(formatter)
if logger.hasHandlers():
logger.handlers.clear()
logger.addHandler(stream_handler)

return logger
4 changes: 2 additions & 2 deletions pragma/core/mixins/oracle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import collections
import logging
import time
from typing import List, Optional

Expand All @@ -13,10 +12,11 @@

from pragma.core.contract import Contract
from pragma.core.entry import Entry, FutureEntry, SpotEntry
from pragma.core.logger import get_stream_logger
from pragma.core.types import ASSET_MAPPING, AggregationMode, DataType, DataTypes
from pragma.core.utils import felt_to_str, str_to_felt

logger = logging.getLogger(__name__)
logger = get_stream_logger()

OracleResponse = collections.namedtuple(
"OracleResponse",
Expand Down
31 changes: 23 additions & 8 deletions pragma/core/mixins/randomness.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
import asyncio
import sys
from typing import List, Optional

Expand All @@ -8,14 +8,15 @@

from pragma.core.abis import ABIS
from pragma.core.contract import Contract
from pragma.core.logger import get_stream_logger
from pragma.core.randomness.utils import (
RandomnessRequest,
create_randomness,
felt_to_secret_key,
)
from pragma.core.types import RequestStatus

logger = logging.getLogger(__name__)
logger = get_stream_logger()

IGNORE_REQUEST_THRESHOLD = 30

Expand Down Expand Up @@ -137,7 +138,7 @@ async def submit_random(
try:
estimate_fee = await prepared_call.estimate_fee()
except ClientError as e:
print("Error while estimating fee: ", e)
logger.error("Error while estimating fee: ", e)
return None
if estimate_fee.overall_fee > callback_fee_limit:
logger.error(
Expand Down Expand Up @@ -352,6 +353,7 @@ async def handle_random(
block_number = await self.full_node_client.get_block_number()

min_block = max(min_block, block_number - IGNORE_REQUEST_THRESHOLD)
logger.info(f"Handle random job running with min_block: {min_block}")

sk = felt_to_secret_key(private_key)

Expand All @@ -369,6 +371,7 @@ async def handle_random(
continuation_token=continuation_token,
chunk_size=500,
)
logger.info(f"Got {len(event_list.events)} events")
for event in event_list.events:
index_to_split = 7
event.data.pop(index_to_split)
Expand All @@ -379,6 +382,13 @@ async def handle_random(
continuation_token = event_list.continuation_token
more_pages = continuation_token is not None

statuses = await asyncio.gather(
*[
self.get_request_status(event.caller_address, event.request_id)
for event in events
]
)

for event in events:
minimum_block_number = event.minimum_block_number
# Skip if block_number is less than minimum_block_number
Expand All @@ -388,13 +398,18 @@ async def handle_random(
minimum_block_number > block_number + 1
or minimum_block_number < block_number - IGNORE_REQUEST_THRESHOLD
):
logger.info(
f"Skipping event: {event.request_id} with min_block: {minimum_block_number}"
)
continue
request_id = event.request_id
status = await self.get_request_status(event.caller_address, request_id)
status = statuses.pop(0)
if status.variant != "RECEIVED":
logger.info(
f"Skipping event: {event.request_id} with status: {status.variant}"
)
continue

print(f"event {event}")
logger.info(f"Found event: {event}")

is_pending = minimum_block_number == block_number + 1

Expand Down Expand Up @@ -433,7 +448,7 @@ async def handle_random(
)

if invocation is None:
print("Failed to submit random")
logger.error("Failed to submit randomness")
continue

print(f"Submitted: {hex(invocation.hash)}\n\n")
logger.info(f"Submitted tx: {hex(invocation.hash)}\n\n")
4 changes: 2 additions & 2 deletions pragma/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from starknet_py.net.full_node_client import FullNodeClient

from pragma.core.logger import get_stream_logger
from pragma.core.utils import felt_to_str, str_to_felt

logging.basicConfig()
logger = logging.getLogger(__name__)
logger = get_stream_logger()
logger.setLevel(logging.INFO)


Expand Down
5 changes: 3 additions & 2 deletions pragma/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import inspect
import logging
from functools import wraps
from typing import TypeVar

from asgiref.sync import async_to_sync

logger = logging.getLogger(__name__)
from pragma.core.logger import get_stream_logger

logger = get_stream_logger()


def str_to_felt(text):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pragma-sdk"
version = "1.4.6"
version = "1.4.7"
authors = ["0xevolve <[email protected]>"]
description = "Core package for rollup-native Pragma Oracle"
readme = "README.md"
Expand Down Expand Up @@ -94,6 +94,7 @@ exclude = [
"node_modules",
"site-packages",
"venv",
"pragma-oracle",
]
# Same as Black.
line-length = 88
Expand Down
5 changes: 5 additions & 0 deletions stagecoach/jobs/randomness/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import boto3

from pragma.core.client import PragmaClient
from pragma.core.logger import get_stream_logger

START_BLOCK = int(os.environ.get("START_BLOCK", 0))
NETWORK = os.environ.get("NETWORK", "sepolia")
Expand All @@ -13,6 +14,8 @@
VRF_CONTRACT_ADDRESS = int(os.environ["VRF_CONTRACT_ADDRESS"], 16)
VRF_UPDATE_TIME_SECONDS = int(os.environ.get("VRF_UPDATE_TIME_SECONDS", 10))

logger = get_stream_logger()


def _get_pvt_key():
region_name = "eu-west-3"
Expand All @@ -29,6 +32,7 @@ def _get_pvt_key():

async def main():
admin_private_key = _get_pvt_key()
# admin_private_key = int(os.environ.get("ADMIN_PRIVATE_KEY"), 16)

client = PragmaClient(
network=NETWORK,
Expand All @@ -38,6 +42,7 @@ async def main():
client.init_randomness_contract(VRF_CONTRACT_ADDRESS)

while True:
logger.info("Checking for randomness requests...")
await client.handle_random(admin_private_key, START_BLOCK)
await asyncio.sleep(VRF_UPDATE_TIME_SECONDS)

Expand Down

0 comments on commit fc3d52a

Please sign in to comment.