index.ts
Overview
`index.ts` is the Pulumi deployment entrypoint script responsible for provisioning the **Optimism** blockchain coinstack within the ShapeShift Unchained platform. It automates the configuration and deployment of the Optimism blockchain node services—primarily **daemon**, **op-node**, and **indexer**—onto a Kubernetes cluster.
This script reads configuration and environment files, enriches service definitions with specific parameters (such as environment variables, ports, health probes, and volume mounts), and then invokes a shared deployment function to create the necessary Kubernetes resources. It essentially translates high-level coinstack configuration into concrete Kubernetes StatefulSets, Services, and ConfigMaps for the Optimism network's node infrastructure.
Detailed Explanation
Imports
readFileSync from
'fs': Reads local files synchronously to load configuration or script contents.deployCoinstackfrompulumi/src/coinstack: Core deployment function that takes prepared service arguments and provisions Kubernetes resources.{ Outputs, CoinServiceArgs, getConfig } from
pulumi/src: Types and utilities to manage configuration and deployment arguments.defaultBlockbookServiceArgs from
blockbook/src/constants: Default configuration constants for Blockbook-based indexer services.
Exported Async Function (Default Export)
export = async (): Promise<Outputs> => { ... }
This asynchronous function is the Pulumi entrypoint. It returns a `Promise` resolving to deployment `Outputs`, which typically contain references to created Kubernetes resources and other deployment metadata.
Workflow:
Set Constants:
appName: Fixed to'unchained', representing the overarching application name.coinstack: Fixed to'optimism', defining the blockchain network being deployed.
Read Environment File:
Loads
../sample.envintosampleEnv, providing environment variables for the deployment.
Load Kubernetes Config:
Calls
getConfig()to asynchronously obtain:kubeconfig: Kubernetes cluster connection details.config: The Pulumi configuration object which includes StatefulSet service definitions.namespace: Kubernetes namespace in which to deploy.
Map and Customize Coin Services:
The script iterates over
config.statefulService?.services, mapping each service (daemon, op-node, indexer) to a customizedCoinServiceArgsobject:Daemon Service:
Adds a snapshot URL via environment variable.
Defines three ports:
daemon-rpc: 8545 (RPC endpoint)daemon-ws: 8546 (WebSocket endpoint with path prefix/websocket)daemon-auth: 8551 (authentication endpoint without ingress route)
Includes
configMapDatawith:jwt.hexfile content (for JWT authentication)evm.shscript content (helper script)
Defines volume mounts to inject these config files into the pod.
Configures Kubernetes health probes: startup, liveness, and readiness with tailored timeouts and thresholds.
Op-Node Service:
Sets environment variables for Layer 1 RPC and Beacon endpoints pointing to the Ethereum service within the cluster.
Opens port
op-node-rpcon 9545 without ingress route.Includes the
evm.shscript config data and volume mounts similar to daemon (including mountingjwt.hex).Defines startup, liveness, and readiness probes with specified timing parameters.
Indexer Service:
Merges the service with default Blockbook service arguments (standard for indexers).
Injects an
indexer-config.jsonfrom the local../indexer/config.jsonfile into a ConfigMap.
Fallback:
Throws an error for unsupported service names, enforcing strict service definitions.
Call
deployCoinstackwith Prepared Arguments:Passes the following to the deployment function:
appName:'unchained'coinServiceArgs: the fully mapped and customized array of service arguments.coinstack:'optimism'coinstackType:'node'(indicating this coinstack is a node-based blockchain)config,kubeconfig,namespace: Kubernetes deployment context.sampleEnv: environment variables file buffer.
Return Deployment Outputs:
The promise resolves with deployment outputs, which can be used for monitoring or further automation.
Important Types
CoinServiceArgsDescribes the configuration for a single service within the coinstack.
Includes fields like:
env: Environment variables object.ports: Port definitions with options like ingress routes and path prefixes.configMapData: Key-value pairs for ConfigMap files.volumeMounts: Kubernetes volume mount specifications.startupProbe,livenessProbe,readinessProbe: Kubernetes probe configurations.Additional metadata inherited from the service config.
OutputsRepresents the result of the deployment, typically including references to Kubernetes resources and endpoints.
Usage Example
This file is typically executed by the Pulumi CLI during infrastructure deployment:
pulumi up -s <stack-name>
Pulumi will invoke the exported async function, which will:
Read configurations.
Prepare service deployment arguments.
Deploy the Optimism coinstack services to Kubernetes.
Output deployment status and resource information.
Implementation Details and Algorithms
Service Configuration Mapping:
Uses a
switchstatement to differentiate service types and customize their deployment parameters.Reads local files synchronously (
jwt.hex, shell scripts, JSON configs) to embed into Kubernetes ConfigMaps.Defines Kubernetes health probes with specific timing to ensure pod readiness and resilience.
Sets environment variables for inter-service communication (e.g., L1 RPC endpoint for
op-node).
Deployment Invocation:
Calls a centralized
deployCoinstackfunction abstracting away Kubernetes resource creation.This promotes code reuse and consistency across different coinstacks.
Error Handling:
Throws explicit errors for unsupported service names, preventing silent misconfigurations.
Interaction with Other System Components
Pulumi Core Libraries (
pulumi/src):Provides deployment primitives and utility functions like
deployCoinstackandgetConfig.
Kubernetes Cluster:
Deployed resources include StatefulSets, Services, ConfigMaps, and potentially IngressRoutes.
Other Coinstack Services:
op-nodeservice depends on the Ethereum daemon running atethereum-svc.unchained.svc.cluster.local.
Scripts and Config Files:
Uses helper scripts (
evm.sh) and JWT keys (jwt.hex) mounted as ConfigMaps to configure pod startup and operation.
Blockbook Indexer:
Uses default Blockbook constants and configuration for the indexer service, enabling blockchain data indexing.
Mermaid Diagram: Flowchart of Service Configuration and Deployment in index.ts
flowchart TD
A[Start Deployment] --> B[Read sample.env File]
B --> C[Get Kubernetes Config & Namespace]
C --> D[Map Configured Services]
D --> E{Service Name?}
E -->|daemon| F[Configure Daemon Service Args]
E -->|op-node| G[Configure Op-Node Service Args]
E -->|indexer| H[Configure Indexer Service Args]
E -->|other| I[Throw Unsupported Service Error]
F --> J[Add Env Vars, Ports, Probes, VolumeMounts]
G --> J
H --> J
J --> K[Aggregate Service Arguments]
K --> L[Call deployCoinstack() with Args]
L --> M[Deploy Kubernetes Resources]
M --> N[Pods Start & Health Probes Run]
N --> O[Optimism Coinstack Running]
Summary
This file is a Pulumi deployment script for the Optimism blockchain coinstack.
It reads configuration and environment files to customize three main services:
daemon,op-node, andindexer.Each service is configured with specific ports, environment variables, probes, and volume mounts.
Deployment is delegated to a shared
deployCoinstackfunction that provisions Kubernetes resources.Errors are thrown for unknown service types to ensure deployment correctness.
The file integrates tightly with the Pulumi infrastructure-as-code framework and Kubernetes cluster.
It is part of the larger multi-blockchain coinstack architecture that standardizes blockchain node deployments.