index.ts
Overview
This file is the Pulumi deployment entrypoint script for the **Bitcoin Cash (bitcoincash) coinstack** within the Unchained multi-blockchain infrastructure platform. It orchestrates the deployment of blockchain node services—specifically the daemon node and the indexer service—into a Kubernetes cluster using Pulumi infrastructure-as-code.
The script performs the following key functions:
Reads environment and service configuration files.
Fetches Kubernetes context and deployment configuration.
Maps blockchain-specific services (daemon and indexer) to deployment parameters including ports, environment variables, probes, and ConfigMaps.
Invokes a shared deployment function (
deployCoinstack) that provisions the necessary Kubernetes resources such as StatefulSets, Services, and ConfigMaps based on these parameters.Returns deployment outputs for further integration or inspection.
This file is tailored specifically for the Bitcoin Cash coinstack, configuring its services with blockchain-appropriate ports, readiness probes, and indexer configuration data.
Detailed Explanation
Imports
import { readFileSync } from 'fs'
import { deployCoinstack } from '../../../../pulumi/src/coinstack'
import { Outputs, CoinServiceArgs, getConfig } from '../../../../pulumi/src'
import { defaultBlockbookServiceArgs } from '../../../packages/blockbook/src/constants'
readFileSync (fs): Used to synchronously read configuration files such as environment variables and indexer config JSON.
deployCoinstack: Core Pulumi utility function that launches the Kubernetes deployment for the defined coinstack services.
Outputs, CoinServiceArgs, getConfig: Types and utility function from the Pulumi core library for configuration retrieval and typing.
defaultBlockbookServiceArgs: Provides default configuration parameters for Blockbook-based indexer services, standardizing indexer deployment.
Exported Async Deployment Function
export = async (): Promise<Outputs> => {
...
}
This is the main entrypoint function executed by Pulumi during deployment.
Returns: A
Promiseresolving toOutputswhich contains metadata and references to the deployed Kubernetes resources.Usage: Invoked automatically by Pulumi when running
pulumi upin the Bitcoin Cash coinstack directory.
Internal Function Workflow
Define Constants
const appName = 'unchained'
const coinstack = 'bitcoincash'
appName: Identifier for the application or platform.coinstack: The blockchain identifier used to select deployment parameters and resource naming.
Read Sample Environment File
const sampleEnv = readFileSync('../sample.env')
Reads a sample environment file alongside the deployment directory, typically containing environment variables or secrets used by the deployed pods.
Fetch Kubernetes Configuration
const { kubeconfig, config, namespace } = await getConfig()
Retrieves the Kubernetes context (
kubeconfig), deployment-specific configuration (config), and target namespace for resource creation.This function abstracts config loading and context setup, enabling environment-aware deployments.
Map Stateful Services to Deployment Arguments
const coinServiceArgs = config.statefulService?.services?.map((service): CoinServiceArgs => {
switch (service.name) {
case 'daemon':
return {
...service,
ports: { 'daemon-rpc': { port: 8332 } },
env: {
NETWORK: config.network,
},
readinessProbe: { periodSeconds: 30, failureThreshold: 10 },
}
case 'indexer':
return {
...service,
...defaultBlockbookServiceArgs,
configMapData: { 'indexer-config.json': readFileSync('../indexer/config.json').toString() },
}
default:
throw new Error(`no support for coin service: ${service.name}`)
}
})
Iterates over the configured stateful services for the coinstack.
For each service, returns a
CoinServiceArgsobject describing how Pulumi should deploy it.Daemon Node Service:
Opens port 8332 labeled as
daemon-rpc(Bitcoin Cash RPC port).Injects environment variable
NETWORKfrom the global config.Sets a readiness probe to check pod health every 30 seconds, allowing 10 failures before marking unhealthy.
Indexer Service:
Extends default Blockbook indexer arguments.
Loads
indexer-config.jsoninto a ConfigMap to provide indexer runtime configuration.
Throws an error for any unsupported service name, ensuring deployment correctness.
Invoke Deployment
return deployCoinstack({
appName,
coinServiceArgs,
coinstack,
coinstackType: 'node',
config,
kubeconfig,
namespace,
sampleEnv,
})
Calls the shared deployment routine, passing all required parameters:
appName: platform identifier.coinServiceArgs: array of service deployment argument objects.coinstack: blockchain identifier.coinstackType: set to'node'indicating a standard node-based coinstack.config,kubeconfig,namespace: environment and Kubernetes context.sampleEnv: environment variables for pods.
deployCoinstackhandles actual Kubernetes resource creation using Pulumi SDK.
Key Types
CoinServiceArgs: Interface describing deployment parameters for a blockchain service, including:
name: service name (e.g., daemon, indexer).ports: network ports exposed by the service.env: environment variables injected into the container.readinessProbe,livenessProbe,startupProbe: Kubernetes probe configurations.configMapData: key-value pairs for ConfigMaps mounted into pods.
Outputs: Deployment output metadata returned by Pulumi after resource creation.
Important Implementation Details
The file assumes the presence of:
A
sample.envfile one directory above for environment variables.An
indexer/config.jsonfile containing indexer configuration.A Pulumi shared library providing
deployCoinstackand utility types.
The use of
defaultBlockbookServiceArgsstandardizes indexer deployment, ensuring consistent behavior across UTXO-based indexers.The readiness probe on the daemon service improves cluster resilience by preventing traffic routing to unready pods.
The
switchstatement enforces strict service naming, preventing accidental misconfiguration.The deployment function is asynchronous to allow awaiting configuration fetches and Pulumi resource creation.
Interaction with Other System Components
Pulumi Core Library (
pulumi/src): Provides utility functions (getConfig), types (Outputs,CoinServiceArgs), and deployment primitives (deployCoinstack).Pulumi Coinstack Deployment (
pulumi/src/coinstack): Contains the actual logic to create Kubernetes resources (StatefulSets, Services, ConfigMaps) based on the supplied service arguments.Blockbook Indexer Package (
packages/blockbook): Provides default configuration constants for indexer services used here.Kubernetes Cluster: Target runtime environment where the Bitcoin Cash node daemon and indexer services are deployed.
Environment and Config Files: External JSON and ENV files (
sample.env,indexer/config.json) provide customized runtime parameters for pods.
Usage Example
Assuming the deployment environment is set up, running Pulumi in this coinstack directory will invoke this script:
pulumi up
This triggers:
Reading environment and config files.
Mapping configured services (daemon and indexer) to deployment arguments.
Deploying these services into the Kubernetes cluster namespace with the specified ports and probes.
Outputting deployment metadata for verification.
Mermaid Diagram: Flowchart of index.ts Deployment Workflow
flowchart TD
A[Start Deployment] --> B[Read sample.env file]
B --> C[Fetch Kubernetes config & namespace via getConfig()]
C --> D[Map stateful services from config]
D --> E{Service name?}
E -->|daemon| F[Configure daemon service args: ports, env, readinessProbe]
E -->|indexer| G[Configure indexer service args: defaultBlockbookServiceArgs, configMapData]
E -->|other| H[Throw error for unsupported service]
F --> I[Aggregate service args]
G --> I
I --> J[Call deployCoinstack() with all deployment args]
J --> K[Pulumi provisions Kubernetes StatefulSets, Services, ConfigMaps]
K --> L[Pods start and Kubernetes monitors health probes]
L --> M[Deployment completes, return Outputs]
Summary
This `index.ts` file is the Pulumi deployment script for the Bitcoin Cash coinstack within the Unchained platform. It reads configuration files and Kubernetes context, maps blockchain services (daemon and indexer) to deployment parameters including ports and probes, and calls a shared deployment function to provision Kubernetes resources. It ensures the Bitcoin Cash node and its indexer are deployed consistently and reliably, using best practices such as readiness probes and ConfigMaps for configuration. This file exemplifies the modular, reusable design pattern used across multi-blockchain coinstacks in the Unchained ecosystem.