index.ts


Overview

The `index.ts` file is an asynchronous Pulumi deployment script tailored for the **Thorchain** coinstack within the ShapeShift Unchained platform. It defines and configures the core blockchain services—namely the **daemon node**, **indexer**, and **TimescaleDB**—and orchestrates their deployment into a Kubernetes cluster using Pulumi infrastructure-as-code.

This file serves as the entrypoint for deploying the Thorchain coinstack's stateful services by:

The end goal is to reliably deploy and manage the lifecycle of Thorchain's blockchain node infrastructure in a cloud-native environment, ensuring health monitoring, configuration injection, and optimized resource mounting.


Detailed Explanation of the Main Exported Function

Function Signature

export = async (): Promise<Outputs>

Parameters

The function takes no parameters directly but internally uses imported utilities and reads files relative to the project's directory structure.

Function Workflow and Steps

  1. Define Constants:

    • appName: Set to 'unchained' — a constant representing the application name.

    • coinstack: Set to 'thorchain' — identifies the blockchain coinstack being deployed.

  2. Read Environment File:

    • Loads the sample.env file from the Thorchain command directory to provide sample environment variables or configuration placeholders.

  3. Retrieve Kubernetes and Deployment Config:

    • Calls getConfig() to asynchronously fetch:

      • kubeconfig — Kubernetes cluster configuration.

      • config — deployment configuration, including stateful services and network settings.

      • namespace — target Kubernetes namespace for deployment.

  4. Map Stateful Services to Deployment Arguments:

    • Iterates through config.statefulService?.services to produce an array of CoinServiceArgs.

    • For each service (identified by service.name), the function:

      • Daemon Service:

        • Sets data directory to /root.

        • Defines environment variables such as CHAIN_ID and NET (network).

        • Configures two ports (daemon-api and daemon-rpc) with HTTP path prefixes for routing.

        • Injects a shell script tendermint.sh as a ConfigMap and mounts it inside the container.

        • Defines Kubernetes health probes (startup, liveness, readiness) with specific timing and thresholds.

      • Indexer Service:

        • Sets data directory to /blockstore.

        • Defines environment variables related to Midgard genesis configuration and blockstore URLs.

        • Opens port 8080 for the Midgard API.

        • Injects indexer-config.json as a ConfigMap and mounts it.

        • Enables a monitoring container sidecar.

        • Defines TCP socket-based health probes on port 8080.

      • TimescaleDB Service:

        • Sets data directory for PostgreSQL data.

        • Sets environment variables for PostgreSQL database credentials.

        • Opens port 5432 for PostgreSQL.

        • Mounts a shared memory volume (dshm).

        • Configures health probes using PostgreSQL readiness command pg_isready.

      • Throws an error if an unsupported service name is encountered.

  5. Define Additional Volumes:

    • Declares a memory-backed volume dshm to be used by TimescaleDB for shared memory.

  6. Invoke Deployment:

    • Calls deployCoinstack() with:

      • Application and coinstack names.

      • The constructed list of service deployment arguments.

      • Kubernetes configuration and namespace.

      • Loaded environment variables.

      • Volume definitions.

      • Type set as 'go' indicating the coinstack implementation language.

  7. Return Deployment Outputs:

    • Resolves with the outputs returned by deployCoinstack().


Key Types, Interfaces, and Utilities Used


Usage Example

This file acts as a Pulumi program entrypoint and is typically invoked by the Pulumi CLI during deployment:

pulumi up -s thorchain

Or programmatically from a Node.js environment that supports ES modules:

import thorchainDeploy from './index.ts';

(async () => {
  const outputs = await thorchainDeploy();
  console.log('Deployment outputs:', outputs);
})();

Important Implementation Details and Algorithms


Interaction with Other System Components


Mermaid Diagram: Flowchart of the Deployment Process in index.ts

flowchart TD
  A[Start Deployment] --> B[Read sample.env file]
  B --> C[Call getConfig() → kubeconfig, config, namespace]
  C --> D[Map config.statefulService.services to CoinServiceArgs]
  D --> E{Service.name}
  E -->|daemon| F[Configure daemon service args (env, ports, probes, mounts)]
  E -->|indexer| G[Configure indexer service args (env, ports, probes, mounts)]
  E -->|timescaledb| H[Configure timescaledb service args (env, ports, probes, mounts)]
  E -->|other| I[Throw unsupported service error]
  F & G & H --> J[Define volumes (e.g., dshm)]
  J --> K[Call deployCoinstack() with all args]
  K --> L[Deploy Kubernetes StatefulSets, Services, ConfigMaps]
  L --> M[Return deployment Outputs]

Summary

This modular approach aligns with the overall Multi-Blockchain Coinstacks architecture, facilitating consistent and maintainable blockchain infrastructure deployment across various networks.