diff --git a/README.md b/README.md index ac57806..08f614f 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ AWS account/region changes take effect after an agent restart. **Mainnet** is a deliberately limited observer board, not a mainnet-support-fleet monitor. A separately managed Evolution fullnode observer reports Core ChainLocks and stall state, Platform block progress and stall state, the public quorum-list-server, and the count of large PoSe bans. The `mainnet-support` EC2 fleet is not discovered or probed by this application, and no Mainnet operations are exposed. The observer report is token-scoped and contains only public chain/service facts. The web container reads `MAINNET_REPORT_TOKEN` from `/etc/dash-status/mainnet.env`; the observer host uses the same secret in `/etc/dash-mainnet-observer/env`. Install `deploy/dash-mainnet-observer.service` only on the dedicated observer/fullnode host. +**Genesis sync** is one tile on the Testnet and Mainnet pages. A separate host replays Platform from block 1 with the newest published Drive every night ([dashpay/infra, `platform-sync`](https://github.com/dashpay/infra/tree/master/platform-sync)) and posts where its node is and how the last replay ended to `/api/platform-sync/report`. The report is token-scoped (`PLATFORM_SYNC_REPORT_TOKEN`, read by the web container from `/etc/dash-status/mainnet.env`; the sync host keeps the same secret in `/etc/platform-sync/report-token`), schema-checked, and holds only public facts. A report older than 15 minutes shows as `no report`. + ## Infrastructure pages Visible to any account with access (any role). diff --git a/server/platform-sync.js b/server/platform-sync.js new file mode 100644 index 0000000..754c913 --- /dev/null +++ b/server/platform-sync.js @@ -0,0 +1,53 @@ +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; + +// The nightly from-genesis Platform sync (dashpay/infra, platform-sync) pushes +// where its node is and how the last replay ended. Public-data-only boundary: +// the result is served to anonymous callers, so only allowlisted, type-checked +// fields survive. No host name, path, container name or request id passes. +export const SYNC_NETWORKS = ['testnet', 'mainnet']; +const PHASES = ['idle', 'starting', 'syncing', 'verifying', 'following', 'failed']; +const RESULTS = ['passed', 'failed']; +const BYTES = ['internetRx', 'internetTx', 'platformRx', 'platformTx', 'coreRx', 'coreTx', 'image']; +const FRESH_MS = 180_000, STALE_MS = 15 * 60_000; + +const integer = (v) => (Number.isSafeInteger(v) && v >= 0 ? v : null); +const finite = (v) => (Number.isFinite(v) && v >= 0 ? v : null); +const boolean = (v) => (typeof v === 'boolean' ? v : null); +const enumeration = (v, allowed) => (allowed.includes(v) ? v : null); +const timestamp = (v) => (typeof v === 'string' && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z$/.test(v) && Number.isFinite(Date.parse(v)) ? new Date(v).toISOString() : null); +const match = (v, re) => (typeof v === 'string' && re.test(v) ? v : null); +// The reason can quote a log line. Printable ASCII only, and short. +const reason = (v) => (typeof v === 'string' ? v.replace(/[^\x20-\x7e]/g, ' ').slice(0, 300) : null); +const VERSION = /^[\w.+-]{1,40}$/, IMAGE = /^[\w./:@-]{1,200}$/, REVISION = /^[0-9a-f]{40}$/; + +const image = (i) => (i && typeof i === 'object' ? { ref: match(i.ref, IMAGE), version: match(i.version, VERSION), revision: match(i.revision, REVISION) } : null); + +function run(r) { + if (!r || typeof r !== 'object') return null; + return { result: enumeration(r.result, RESULTS), reason: reason(r.reason), startedAt: timestamp(r.startedAt), finishedAt: timestamp(r.finishedAt), + durationSeconds: integer(r.durationSeconds), height: integer(r.height), image: image(r.image), tenderdash: match(r.tenderdash, VERSION), + bytes: Object.fromEntries(BYTES.map((k) => [k, integer(r.bytes?.[k])])) }; +} + +export function validatePlatformSyncReport(input, now = Date.now()) { + if (!input || input.schema !== 1 || !SYNC_NETWORKS.includes(input.network)) throw new Error('platform-sync report for testnet or mainnet required'); + const at = timestamp(input.updatedAt); + if (!at || Date.parse(at) > now + 60_000 || now - Date.parse(at) > FRESH_MS) throw new Error('platform-sync report timestamp invalid or stale'); + const phase = enumeration(input.phase, PHASES); + if (!phase) throw new Error('platform-sync phase unknown'); + const progress = finite(input.progress); + return { network: input.network, generatedAt: at, phase, healthy: boolean(input.healthy), onHold: input.hold != null && input.hold !== false, + height: integer(input.height), referenceHeight: integer(input.referenceHeight), progress: progress !== null && progress <= 1 ? progress : null, + blocksPerSecond: finite(input.blocksPerSecond), etaSeconds: integer(input.etaSeconds), run: run(input.run), lastPassed: run(input.lastPassed) }; +} + +// Freshness is decided on every read: a stored report outlives the host that sent it. +export function readGenesisSync(dataDir, name, now = Date.now()) { + if (!SYNC_NETWORKS.includes(name)) return null; + try { + const report = JSON.parse(readFileSync(join(dataDir, 'state', `platform-sync-${name}.json`), 'utf8')); + const at = Date.parse(report.generatedAt); + return Number.isFinite(at) ? { ...report, stale: now - at > STALE_MS } : null; + } catch { return null; } +} diff --git a/server/platform-sync.test.js b/server/platform-sync.test.js new file mode 100644 index 0000000..9f2d1de --- /dev/null +++ b/server/platform-sync.test.js @@ -0,0 +1,58 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { readGenesisSync, validatePlatformSyncReport } from './platform-sync.js'; + +const now = Date.parse('2026-11-21T01:00:00Z'); +const at = (minutesAgo) => new Date(now - minutesAgo * 60_000).toISOString().replace('.000Z', 'Z'); +const report = (over = {}) => ({ schema: 1, network: 'testnet', updatedAt: at(0), phase: 'syncing', hold: null, height: 400000, referenceHeight: 720000, + progress: 0.5556, blocksPerSecond: 31.5, etaSeconds: 10158, healthy: null, + core: { height: 1595500, headers: 1595500, syncing: false }, disk: { sizeGiB: 117.6, usedGiB: 60, freeGiB: 57.6 }, + run: { id: 'gh-123-1-testnet', network: 'testnet', requestedImage: 'auto', result: null, reason: null, startedAt: at(200), + image: { ref: 'dashpay/drive:5.1.0-dev.2', id: 'sha256:abc', source: 'release', version: '5.1.0-dev.2', revision: 'a'.repeat(40), notes: ['x'] }, + tenderdash: '2.0.0-dev.3', restarts: { drive_abci: 0 }, bytes: { internetRx: 1700000000, internetTx: 30000, platformRx: 1500000000, platformTx: 20000, coreRx: 100, coreTx: 5, image: 62113044 } }, + lastPassed: { id: 'gh-100-1-testnet', finishedAt: at(1500), durationSeconds: 21000, height: 715000, image: { ref: 'dashpay/drive:5.1.0-dev.1', version: '5.1.0-dev.1' } }, + ...over }); + +test('a report keeps only the allowlisted public fields', () => { + const r = validatePlatformSyncReport(report(), now); + assert.deepEqual(Object.keys(r).sort(), ['blocksPerSecond', 'etaSeconds', 'generatedAt', 'healthy', 'height', 'lastPassed', 'network', 'onHold', 'phase', 'progress', 'referenceHeight', 'run']); + assert.deepEqual(r.run.image, { ref: 'dashpay/drive:5.1.0-dev.2', version: '5.1.0-dev.2', revision: 'a'.repeat(40) }); + assert.equal(r.run.id, undefined); + assert.equal(r.run.bytes.platformRx, 1500000000); + assert.equal(r.run.bytes.internetRx, 1700000000); + assert.equal(r.lastPassed.height, 715000); + assert.equal(r.onHold, false); + assert.equal(JSON.stringify(r).includes('gh-123'), false); +}); + +test('malformed values become null, not text on a public page', () => { + const r = validatePlatformSyncReport(report({ height: -1, progress: 7, hold: 'someone is reading the stores', + run: { result: 'exploded', reason: 'AppHash mismatch: