Skip to content
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

Open
wants to merge 14 commits into
base: feat/emily-new-block-handler
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ services:
- "20540:20540"
environment:
EMILY_API_KEY: testApiKey
EMILY_CHAINSTATE_URL: http://emily-server:3031/chainstate
DEPLOYER_ADDRESS: SN3R84XZYA63QS28932XQF3G1J8R9PC3W76P9CSQS
EMILY_ENDPOINT: http://emily-server:3031
profiles:
- default
depends_on:
Expand Down
3 changes: 1 addition & 2 deletions docker/sbtc/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ services:
- "20540:20540"
environment:
EMILY_API_KEY: testApiKey
EMILY_CHAINSTATE_URL: http://emily-server:3031/chainstate
DEPLOYER_ADDRESS: SN3R84XZYA63QS28932XQF3G1J8R9PC3W76P9CSQS
EMILY_ENDPOINT: http://emily-server:3031
depends_on:
- emily-server

Expand Down
12 changes: 5 additions & 7 deletions docker/sbtc/emily-sidecar/Dockerfile
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"]
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

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"]
89 changes: 0 additions & 89 deletions emily_sidecar/app.py

This file was deleted.

14 changes: 0 additions & 14 deletions emily_sidecar/gunicorn_config.py

This file was deleted.

23 changes: 23 additions & 0 deletions emily_sidecar/logging_config.py
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)
63 changes: 63 additions & 0 deletions emily_sidecar/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
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}


class NewBlockEventModel(BaseModel, extra="allow"):
block_height: int
index_block_hash: str
Comment on lines +28 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, that extra field is critical because we expect more fields to be sent in event from the stacks node but pydantic ignores them by default. We should probably add a comment above here describing why it's necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 {"message": "Hello, World!"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably just return 200 Ok here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in 658aa45

7 changes: 3 additions & 4 deletions emily_sidecar/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is the way

8 changes: 8 additions & 0 deletions emily_sidecar/settings.py
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"
Loading
Loading