Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev:frontend": "vite",
"dev:backend": "node server/index.js",
"build": "vite build",
"test": "node --test",
"start": "node server/index.js",
"lint": "eslint .",
"preview": "vite preview"
Expand Down
67 changes: 67 additions & 0 deletions server/difficultyAlert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { getDifficultyAlert } from '../src/utils/difficultyAlert.js';

function node(overrides = {}) {
const { status: statusOverrides = {}, ...nodeOverrides } = overrides;
return {
name: 'masternode-1',
lastUpdated: 1000,
...nodeOverrides,
status: {
coreServiceStatus: 'up',
coreHeight: 100,
coreDifficulty: 10,
coreBestBlockHash: '000abc',
coreBestBlockTime: 1000,
...statusOverrides,
},
};
}

test('getDifficultyAlert returns null when the best block is recent', () => {
const alert = getDifficultyAlert([node()], 1200 * 1000, {
staleBlockSeconds: 300,
difficultyThreshold: 1,
});
assert.equal(alert, null);
});

test('getDifficultyAlert returns null when stale block difficulty is below threshold', () => {
const alert = getDifficultyAlert([node({ status: { coreDifficulty: 0.1 } })], 2000 * 1000, {
staleBlockSeconds: 300,
difficultyThreshold: 1,
});
assert.equal(alert, null);
});

test('getDifficultyAlert reports stale high-difficulty tips', () => {
const alert = getDifficultyAlert([node()], 2000 * 1000, {
staleBlockSeconds: 300,
difficultyThreshold: 1,
});
assert.deepEqual(alert, {
type: 'testnet_difficulty_stall',
severity: 'info',
sourceNode: 'masternode-1',
height: 100,
bestBlockHash: '000abc',
bestBlockTime: 1000,
difficulty: 10,
ageSeconds: 1000,
});
});

test('getDifficultyAlert uses the highest reported core height', () => {
const alert = getDifficultyAlert([
node({ name: 'masternode-1', status: { coreHeight: 100, coreBestBlockTime: 1000 } }),
node({ name: 'masternode-2', status: { coreHeight: 101, coreBestBlockHash: '000def', coreBestBlockTime: 1100 } }),
], 2000 * 1000, {
staleBlockSeconds: 300,
difficultyThreshold: 1,
});

assert.equal(alert.sourceNode, 'masternode-2');
assert.equal(alert.height, 101);
assert.equal(alert.bestBlockHash, '000def');
});
4 changes: 4 additions & 0 deletions server/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export function parseDashCliStatus(blockchainJson, masternodeJson) {
const chain = JSON.parse(extractJsonBlob(blockchainJson));
result.network = chain.chain === 'test' ? 'testnet' : chain.chain;
result.coreHeight = chain.blocks || null;
result.coreDifficulty = Number.isFinite(chain.difficulty) ? chain.difficulty : null;
result.coreBestBlockHash = chain.bestblockhash || null;
result.coreBestBlockTime = Number.isFinite(chain.time) ? chain.time : null;
result.coreMedianTime = Number.isFinite(chain.mediantime) ? chain.mediantime : null;
result.coreServiceStatus = chain.initialblockdownload ? 'syncing' : 'up';
const progress = chain.verificationprogress;
if (progress != null) {
Expand Down
12 changes: 12 additions & 0 deletions server/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { parseHpStatus, parseTenderdashInfo } from './parser.js';
const blockchainJson = JSON.stringify({
chain: 'test',
blocks: 1000,
bestblockhash: '000000000abc',
difficulty: 42.5,
time: 1766450000,
mediantime: 1766449900,
initialblockdownload: false,
verificationprogress: 0.99995,
size_on_disk: 5 * 1024 ** 3,
Expand Down Expand Up @@ -105,6 +109,14 @@ test('parseHpStatus preserves coreSize and posePenalty from dash-cli JSON', () =
assert.equal(status.posePenalty, 0);
});

test('parseHpStatus preserves best block metadata from getblockchaininfo', () => {
const status = parseHpStatus(blockchainJson, masternodeJson, null, networkInfoJson);
assert.equal(status.coreDifficulty, 42.5);
assert.equal(status.coreBestBlockHash, '000000000abc');
assert.equal(status.coreBestBlockTime, 1766450000);
assert.equal(status.coreMedianTime, 1766449900);
});

// Contract boundary: scripts/dashmon-check.sh is responsible for unwrapping
// Tenderdash's JSON-RPC envelope ({jsonrpc, id, result}) and emitting a flat
// assembly object. parseTenderdashInfo only sees that post-unwrap blob -- it
Expand Down
5 changes: 5 additions & 0 deletions src/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useNodeData } from '../hooks/useNodeData';
import SummaryBar from './SummaryBar';
import NodeCard from './NodeCard';
import NodeDetail from './NodeDetail';
import DifficultyAlert from './DifficultyAlert';
import { getDifficultyAlert } from '../utils/difficultyAlert';

export default function Dashboard() {
const { nodes, proposer, loading } = useNodeData();
Expand All @@ -18,6 +20,7 @@ export default function Dashboard() {

const hpNodes = nodes.filter((n) => n.type === 'hp');
const mnNodes = nodes.filter((n) => n.type === 'mn');
const difficultyAlert = getDifficultyAlert(nodes);

return (
<div className="min-h-screen bg-gray-950 text-gray-100">
Expand All @@ -40,6 +43,8 @@ export default function Dashboard() {
</header>

<main className="max-w-7xl mx-auto px-4 py-6 space-y-8">
<DifficultyAlert alert={difficultyAlert} />

{/* HP Masternodes */}
{hpNodes.length > 0 && (
<section>
Expand Down
37 changes: 37 additions & 0 deletions src/components/DifficultyAlert.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function formatDuration(seconds) {
if (!Number.isFinite(seconds)) return 'unknown';
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (hours > 0 && minutes > 0) return `${hours}h ${minutes}m`;
if (hours > 0) return `${hours}h`;
return `${Math.max(1, minutes)}m`;
}

export default function DifficultyAlert({ alert }) {
if (!alert) return null;

const difficulty = alert.difficulty.toLocaleString(undefined, {
maximumFractionDigits: alert.difficulty >= 10 ? 0 : 6,
});
const tipAge = formatDuration(alert.ageSeconds);
const height = alert.height.toLocaleString();

return (
<div className="border border-sky-500/30 bg-sky-500/10 rounded-lg px-4 py-3">
<div className="flex flex-col gap-1 sm:flex-row sm:items-start sm:justify-between">
<div>
<div className="text-sm font-semibold text-sky-200">
Possible testnet difficulty pause
</div>
<div className="text-sm text-sky-100/80">
Best Core block {height} is {tipAge} old while difficulty is {difficulty}. This can happen after
ASIC mining spikes testnet difficulty; CPU miners may resume once min-difficulty rules allow it.
</div>
</div>
<div className="text-xs text-sky-200/70 font-mono sm:text-right shrink-0">
{alert.sourceNode}
</div>
</div>
</div>
);
}
2 changes: 2 additions & 0 deletions src/components/NodeDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export default function NodeDetail({ node, onClose }) {
<Row label="Status" value={s.coreServiceStatus}
highlight={s.coreServiceStatus === 'up' ? 'text-emerald-400' : 'text-amber-400'} />
<Row label="Height" value={s.coreHeight?.toLocaleString()} />
<Row label="Difficulty" value={s.coreDifficulty?.toLocaleString(undefined, { maximumFractionDigits: 6 })} />
<Row label="Sync Progress" value={s.coreSyncProgress} />
<Row label="Best Block Time" value={s.coreBestBlockTime ? new Date(s.coreBestBlockTime * 1000).toLocaleString() : null} />
<Row label="Size" value={s.coreSize} />
</div>

Expand Down
42 changes: 42 additions & 0 deletions src/utils/difficultyAlert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export const DEFAULT_DIFFICULTY_ALERT_THRESHOLD = 1;
export const DEFAULT_STALE_BLOCK_SECONDS = 45 * 60;

function isUsableNode(node) {
const status = node.status || {};
return status.coreServiceStatus === 'up'
&& Number.isFinite(status.coreHeight)
&& Number.isFinite(status.coreDifficulty)
&& Number.isFinite(status.coreBestBlockTime);
}

export function getDifficultyAlert(nodes, nowMs = Date.now(), options = {}) {
const difficultyThreshold = options.difficultyThreshold ?? DEFAULT_DIFFICULTY_ALERT_THRESHOLD;
const staleBlockSeconds = options.staleBlockSeconds ?? DEFAULT_STALE_BLOCK_SECONDS;

const bestNode = [...nodes]
.filter(isUsableNode)
.sort((a, b) => {
const heightDelta = b.status.coreHeight - a.status.coreHeight;
if (heightDelta !== 0) return heightDelta;
return (b.lastUpdated || 0) - (a.lastUpdated || 0);
})[0];

if (!bestNode) return null;

const status = bestNode.status;
const ageSeconds = Math.max(0, Math.floor(nowMs / 1000 - status.coreBestBlockTime));
if (ageSeconds < staleBlockSeconds || status.coreDifficulty < difficultyThreshold) {
return null;
}

return {
type: 'testnet_difficulty_stall',
severity: 'info',
sourceNode: bestNode.name,
height: status.coreHeight,
bestBlockHash: status.coreBestBlockHash,
bestBlockTime: status.coreBestBlockTime,
difficulty: status.coreDifficulty,
ageSeconds,
};
}