-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[chore] sidecar send new block updates #1409
base: feat/emily-new-block-handler
Are you sure you want to change the base?
Changes from all commits
c430f0a
0d8cd47
fea6c24
2fc1844
96f6426
d59f387
5cde067
90a60ae
4fb0c2d
2ae0d65
e9cb9ee
635b02c
658aa45
4371242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
FROM python:3.12 AS emily-sidecar | ||
FROM python:3.12-slim AS emily-sidecar | ||
|
||
WORKDIR /app | ||
COPY ../../emily_sidecar /app | ||
# Install dependencies | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
# Set environment variables | ||
ENV FLASK_APP=app.py | ||
# Expose the Flask port | ||
# Expose the FastAPI port | ||
EXPOSE 20540 | ||
ENV EMILY_API_KEY=testApiKey | ||
ENV EMILY_CHAINSTATE_URL=http://emily-server:3031/chainstate | ||
ENV DEPLOYER_ADDRESS=SN3R84XZYA63QS28932XQF3G1J8R9PC3W76P9CSQS | ||
CMD ["gunicorn", "-c", "gunicorn_config.py", "wsgi:app"] | ||
ENV EMILY_ENDPOINT=http://emily-server:3031 | ||
CMD ["uvicorn", "--host", "0.0.0.0", "--port", "20540", "--workers", "4", "--timeout-keep-alive", "30", "--log-level" , "info", "main:app"] |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import logging | ||
import sys | ||
|
||
|
||
def setup_logging( | ||
level: int = logging.INFO, log_to_file: bool = False, log_file: str = "app.log" | ||
) -> None: | ||
"""Set up application-wide logging.""" | ||
handlers = [logging.StreamHandler(sys.stdout)] | ||
|
||
if log_to_file: | ||
handlers.append(logging.FileHandler(log_file)) | ||
|
||
logging.basicConfig( | ||
level=level, | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
handlers=handlers, | ||
) | ||
|
||
|
||
def silence_logging(): | ||
"""Disable logging for tests.""" | ||
logging.disable(logging.CRITICAL) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI, HTTPException | ||
from pydantic import BaseModel | ||
import requests | ||
|
||
import settings | ||
import logging_config | ||
|
||
# Initialize the FastAPI app | ||
app = FastAPI() | ||
|
||
# Set up logging when the app starts | ||
logging_config.setup_logging() | ||
|
||
# Get a logger instance | ||
logger = logging.getLogger(__name__) | ||
|
||
logger.info("Using Emily endpoint: %s", settings.EMILY_ENDPOINT) | ||
|
||
headers = {"Content-Type": "application/json", "x-api-key": settings.API_KEY} | ||
|
||
|
||
# The events received from the stacks-node contain many additional fields, | ||
# but we only validate these specific ones for logging purposes. | ||
# The `extra="allow"` argument permits extra fields in the request body | ||
# that are not explicitly defined in the model. | ||
class NewBlockEventModel(BaseModel, extra="allow"): | ||
block_height: int | ||
index_block_hash: str | ||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh nice, that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in 658aa45 |
||
|
||
|
||
@app.post("/new_block") | ||
async def handle_new_block(new_block_event: NewBlockEventModel) -> dict: | ||
try: | ||
resp = requests.post( | ||
settings.NEW_BLOCK_URL, | ||
headers=headers, | ||
json=new_block_event.model_dump_json(), | ||
) | ||
resp.raise_for_status() # This will raise an HTTPError if the response was an error | ||
except requests.RequestException as e: | ||
logger.error( | ||
"Failed to send new block event: block_height=%s, index_block_hash=%s, error=%s", | ||
new_block_event.block_height, | ||
new_block_event.index_block_hash, | ||
e, | ||
) | ||
# lets return an error so that the node will retry | ||
raise HTTPException(status_code=500, detail="Failed to send new_block event") | ||
logger.info( | ||
"Successfully processed new block event: block_height=%s, index_block_hash=%s", | ||
new_block_event.block_height, | ||
new_block_event.index_block_hash, | ||
) | ||
return {} | ||
|
||
|
||
@app.post("/attachments/new") | ||
async def handle_attachments() -> dict: | ||
return {} | ||
|
||
|
||
@app.get("/") | ||
def read_root() -> dict: | ||
return {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
flask==3.1.0 | ||
requests==2.32.3 | ||
marshmallow==3.23.1 | ||
gunicorn==23.0.0 | ||
fastapi==0.115.8 | ||
uvicorn==0.34.0 | ||
setuptools==75.8.0 | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, this is the way |
||
requests==2.32.3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
|
||
# Environment variables and configuration | ||
API_KEY = os.getenv("EMILY_API_KEY", "testApiKey") | ||
EMILY_ENDPOINT = os.getenv( | ||
"EMILY_ENDPOINT", "http://@host.docker.internal:3031" | ||
).removesuffix("/") | ||
NEW_BLOCK_URL = f"{EMILY_ENDPOINT}/new_block" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh we don't need the deployer in the side-car since filtering is getting done in Emily itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly, now the sidecar does the bare minimum