How snapshot-labs/snapshot-hub works
Project Type and Purpose
• This is a backend service (“hub”) for the Snapshot network. Its primary responsibilities are to store data in a MySQL database, serve APIs (including GraphQL endpoints) for querying and processing various operations (such as proposals, votes, spaces, etc.), and to forward and counter-sign messages using a stored private key.
• The README confirms that the service exposes its API endpoints (e.g., at /api) and requires configuration for database connection strings (HUB_DATABASE_URL and SEQ_DATABASE_URL) as well as a private key (RELAYER_PK) for signing messages.
File Structure Insights
• The top-level files (Dockerfile, docker-compose.yml, etc.) and various configuration files (e.g., .env.example, tsconfig.json, jest.config.ts) indicate that the project is built with Node.js and TypeScript and is containerized.
• The src directory is the heart of the application:
• src/index.ts is the likely entry point.
• The src/api.ts and src/eip4824.ts files suggest HTTP API-related logic and some specialized protocol or key functionality.
• The src/graphql directory is highly detailed and broken down into examples, helpers, and multiple operations. This points to a well-modularized GraphQL API that handles different domains (users, votes, proposals, spaces, etc.).
• The helpers under src/helpers (e.g., mysql, rateLimit, metrics, log) provide utility functions for database connectivity, rate limiting, logging and other cross-cutting concerns.
• The test directory, with end-to-end tests and setup/teardown scripts, reinforces that there’s an emphasis on ensuring the integrity of the system via comprehensive automated testing.
Main Components
• API Server:
• Main API entry point (src/index.ts) that initiates the server.
• GraphQL Module:
• Break out the GraphQL service logic into its own component, showing that it aggregates various operations (aliases, follows, leaderboards, proposals, votes, etc.). Point out that these operations represent the business logic for interacting with different data types within the Snapshot ecosystem.
• Database:
• Two distinct database connections: one for the main hub data (HUB_DATABASE_URL) and one for messages or sequencing (SEQ_DATABASE_URL).
• That schema initialization is driven by the SQL script in src/helpers/schema.sql.
• Message Signing Component:
• A component that utilizes the RELAYER_PK (private key) for counter-signing accepted messages, ensuring integrity and authenticity.
• External Communication:
• Since the hub forwards messages to peers on the Snapshot network, include an “external peers” component. This can be depicted as outbound interfaces that handle messaging (and possibly logging or error reporting, hinted by the Sentry release workflow).
• Auxiliary Services:
Relationships and Data Flow
• How incoming API requests (HTTP/GraphQL) are received by the API Server.
• The API server interacting with the GraphQL module to process operations.
• Data being read from and written to the MySQL databases.
• Outbound messages being forwarded to external Snapshot network peers.
• The signing process that uses the relayer’s private key before messages are sent out.
• Dependencies between the API layer, the GraphQL layer, and the helper modules (for tasks like logging, metrics, and rate-limiting).
Incorporation of Architectural Patterns
• Modular Architecture: Clearly separate concerns by isolating the API layer, GraphQL operations, database access, and cryptographic signing.
• Configuration & Environment Management: Note the use of environment variables (from .env) to configure the databases and key management.
• Testing and Continuous Integration: Include a section or a note indicating that there is an extensive testing suite (located in the test directory) and automated workflows in GitHub for linting, testing, and Docker image publishing.