|
| 1 | +import * as os from 'node:os' |
| 2 | +import * as path from 'node:path' |
| 3 | +import * as Instance from '../Instance.js' |
| 4 | +import { deepAssign, toArgs } from '../internal/utils.js' |
| 5 | +import { execa } from '../processes/execa.js' |
| 6 | + |
| 7 | +// `tempo-zone dev` derives the WS RPC (port + 1) and P2P (port + 2) ports from |
| 8 | +// `http.port`. Each instance occupies the compact block [port, port + 4]. |
| 9 | +export function command(parameters: tempoZone.Parameters): string[] { |
| 10 | + const { nodeArgs, port, ...rest } = parameters |
| 11 | + |
| 12 | + const datadir = path.join(os.tmpdir(), '.prool', `tempo-zone.${port}`) |
| 13 | + const defaultParameters = { |
| 14 | + datadir, |
| 15 | + http: { |
| 16 | + addr: '0.0.0.0', |
| 17 | + port: port!, |
| 18 | + }, |
| 19 | + l1: { |
| 20 | + rpcUrl: 'ws://localhost:8546', |
| 21 | + }, |
| 22 | + privateRpc: { |
| 23 | + port: port! + 3, |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + const args = deepAssign(defaultParameters, rest) |
| 28 | + |
| 29 | + return [ |
| 30 | + 'dev', |
| 31 | + ...toArgs(args, { |
| 32 | + arraySeparator: null, |
| 33 | + }), |
| 34 | + // Forwarded to `tempo-zone node`; keeps concurrent instances from colliding. |
| 35 | + '--', |
| 36 | + '--authrpc.port', |
| 37 | + String(port! + 4), |
| 38 | + '--ipcdisable', |
| 39 | + ...((nodeArgs as string[] | undefined) ?? []), |
| 40 | + ] |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Defines a Tempo Zone instance. |
| 45 | + * |
| 46 | + * Provisions a fresh zone against a Tempo dev L1 (`tempo-zone dev`) and runs |
| 47 | + * the zone node. Requires a reachable Tempo dev L1 websocket RPC (`l1.rpcUrl`). |
| 48 | + * |
| 49 | + * @example |
| 50 | + * ```ts |
| 51 | + * const instance = Instance.tempoZone({ |
| 52 | + * l1: { rpcUrl: 'ws://localhost:8546' }, |
| 53 | + * port: 9545, |
| 54 | + * }) |
| 55 | + * await instance.start() |
| 56 | + * // ... |
| 57 | + * await instance.stop() |
| 58 | + * ``` |
| 59 | + */ |
| 60 | +export const tempoZone = Instance.define( |
| 61 | + (parameters?: tempoZone.Parameters) => { |
| 62 | + const { binary = 'tempo-zone', log: log_, ...args } = parameters || {} |
| 63 | + |
| 64 | + const log = (() => { |
| 65 | + try { |
| 66 | + return JSON.parse(log_ as string) |
| 67 | + } catch { |
| 68 | + return log_ |
| 69 | + } |
| 70 | + })() |
| 71 | + const RUST_LOG = log && typeof log !== 'boolean' ? log : '' |
| 72 | + |
| 73 | + const name = 'tempo-zone' |
| 74 | + const process = execa({ name }) |
| 75 | + |
| 76 | + return { |
| 77 | + _internal: { |
| 78 | + args, |
| 79 | + get process() { |
| 80 | + return process._internal.process |
| 81 | + }, |
| 82 | + }, |
| 83 | + host: args.host ?? 'localhost', |
| 84 | + name, |
| 85 | + port: args.port ?? 9545, |
| 86 | + async start({ port = args.port }, options) { |
| 87 | + return await process.start( |
| 88 | + ($) => |
| 89 | + $({ |
| 90 | + env: { |
| 91 | + RUST_LOG, |
| 92 | + }, |
| 93 | + })`${[binary, ...command({ ...args, port })]}`, |
| 94 | + { |
| 95 | + ...options, |
| 96 | + // Resolve when the zone RPC server is listening (fires after provisioning). |
| 97 | + resolver({ process, reject, resolve }) { |
| 98 | + let stderr = '' |
| 99 | + process.stdout.on('data', (data) => { |
| 100 | + const message = data.toString() |
| 101 | + if (log) console.log(message) |
| 102 | + if (message.includes('RPC HTTP server started')) resolve() |
| 103 | + if (message.includes('shutting down')) reject('shutting down') |
| 104 | + }) |
| 105 | + process.stderr.on('data', (data) => { |
| 106 | + const message = data.toString() |
| 107 | + if (log) console.error(message) |
| 108 | + stderr += message |
| 109 | + }) |
| 110 | + // Provisioning failures (e.g. unreachable L1) exit non-zero before the RPC starts. |
| 111 | + process.once('exit', (code) => { |
| 112 | + if (code) reject(stderr || `exited with code ${code}`) |
| 113 | + }) |
| 114 | + }, |
| 115 | + }, |
| 116 | + ) |
| 117 | + }, |
| 118 | + async stop() { |
| 119 | + await process.stop() |
| 120 | + }, |
| 121 | + } |
| 122 | + }, |
| 123 | +) |
| 124 | + |
| 125 | +export declare namespace tempoZone { |
| 126 | + export type Parameters = { |
| 127 | + /** |
| 128 | + * Path or alias to the Tempo Zone binary. |
| 129 | + */ |
| 130 | + binary?: string | undefined |
| 131 | + /** |
| 132 | + * Directory for genesis.json, zone.json, node data, and logs. Wiped on start. |
| 133 | + */ |
| 134 | + datadir?: string | undefined |
| 135 | + /** |
| 136 | + * Dev provisioning options. |
| 137 | + */ |
| 138 | + dev?: |
| 139 | + | { |
| 140 | + /** |
| 141 | + * Dev private key (hex): L1 fee payer, factory deployer, portal admin, |
| 142 | + * and zone sequencer. |
| 143 | + */ |
| 144 | + key?: string | undefined |
| 145 | + /** |
| 146 | + * Initial TIP-20 token enabled on the portal. |
| 147 | + * @default pathUSD (0x20c0000000000000000000000000000000000000) |
| 148 | + */ |
| 149 | + token?: string | undefined |
| 150 | + } |
| 151 | + | undefined |
| 152 | + /** |
| 153 | + * Host the server will listen on. |
| 154 | + */ |
| 155 | + host?: string | undefined |
| 156 | + /** |
| 157 | + * Tempo L1 options. |
| 158 | + */ |
| 159 | + l1?: |
| 160 | + | { |
| 161 | + /** |
| 162 | + * Existing ZoneFactory address on the L1. Deploys the bundled factory |
| 163 | + * when omitted. |
| 164 | + */ |
| 165 | + factoryAddress?: string | undefined |
| 166 | + /** |
| 167 | + * Tempo L1 WebSocket RPC URL. |
| 168 | + * @default "ws://localhost:8546" |
| 169 | + */ |
| 170 | + rpcUrl?: string | undefined |
| 171 | + } |
| 172 | + | undefined |
| 173 | + /** |
| 174 | + * Rust log level configuration (sets RUST_LOG environment variable). |
| 175 | + * Can be a log level or a custom filter string. |
| 176 | + */ |
| 177 | + log?: |
| 178 | + | 'trace' |
| 179 | + | 'debug' |
| 180 | + | 'info' |
| 181 | + | 'warn' |
| 182 | + | 'error' |
| 183 | + | (string & {}) |
| 184 | + | boolean |
| 185 | + | undefined |
| 186 | + /** |
| 187 | + * Extra arguments forwarded to `tempo-zone node`. |
| 188 | + */ |
| 189 | + nodeArgs?: string[] | undefined |
| 190 | + /** |
| 191 | + * Port the server will listen on. |
| 192 | + */ |
| 193 | + port?: number | undefined |
| 194 | + /** |
| 195 | + * Private RPC options. |
| 196 | + */ |
| 197 | + privateRpc?: |
| 198 | + | { |
| 199 | + /** |
| 200 | + * Zone private RPC port. |
| 201 | + * @default `port + 3` |
| 202 | + */ |
| 203 | + port?: number | undefined |
| 204 | + } |
| 205 | + | undefined |
| 206 | + } & Record<string, unknown> |
| 207 | +} |
0 commit comments