Repository navigation
feat: show the nightly from-genesis Platform sync on the network page #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ktechmidas
wants to merge
3
commits into
master
Choose a base branch
from
feat/genesis-sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
bb6a8b6
feat: show the nightly from-genesis Platform sync on the network page
ktechmidas 50482cb
fix(genesis-sync): show the internet bytes the sync host counted
ktechmidas 57c3023
fix(genesis-sync): stale by the page's clock, failed without a run re…
ktechmidas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: <script>\u0007\n' + 'x'.repeat(400), image: { ref: 'a b', version: '<b>', revision: 'zz' }, bytes: { platformRx: '5' } } }), now); | ||
| assert.equal(r.height, null); | ||
| assert.equal(r.progress, null); | ||
| assert.equal(r.onHold, true); | ||
| assert.equal(r.run.result, null); | ||
| assert.equal(r.run.reason.length, 300); | ||
| assert.match(r.run.reason, /^[\x20-\x7e]+$/); | ||
| assert.deepEqual(r.run.image, { ref: null, version: null, revision: null }); | ||
| assert.equal(r.run.bytes.platformRx, null); | ||
| }); | ||
|
|
||
| test('wrong network, schema, phase and old timestamps are refused', () => { | ||
| for (const bad of [report({ network: 'devnet-x' }), report({ schema: 2 }), report({ phase: 'dancing' }), report({ updatedAt: at(4) }), report({ updatedAt: at(-2) }), report({ updatedAt: 'yesterday' }), null]) | ||
| assert.throws(() => validatePlatformSyncReport(bad, now)); | ||
| }); | ||
|
|
||
| test('a stored report goes stale by the clock of the reader', () => { | ||
| const dataDir = mkdtempSync(join(tmpdir(), 'psync-')); | ||
| mkdirSync(join(dataDir, 'state')); | ||
| assert.equal(readGenesisSync(dataDir, 'testnet', now), null); | ||
| writeFileSync(join(dataDir, 'state', 'platform-sync-testnet.json'), JSON.stringify(validatePlatformSyncReport(report(), now))); | ||
| assert.equal(readGenesisSync(dataDir, 'testnet', now + 60_000).stale, false); | ||
| assert.equal(readGenesisSync(dataDir, 'testnet', now + 16 * 60_000).stale, true); | ||
| assert.equal(readGenesisSync(dataDir, 'mainnet', now), null); | ||
| assert.equal(readGenesisSync(dataDir, '../settings', now), null); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.