Reduce sample to network smoke test
This commit is contained in:
1 parent
e24914e76a
commit
4f0f66cf19
3 files changed
+20
-101
No files matched your search
+14
-84
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
@@ -13,91 +14,20 @@ import {
|
||||
const sourceDirectory = dirname(fileURLToPath(import.meta.url));
|
||||
const topologyFile = resolve(sourceDirectory, 'universa.json');
|
||||
|
||||
async function withTimeout(promise, milliseconds, message) {
|
||||
let timeoutId;
|
||||
const timeout = new Promise((_, reject) => {
|
||||
timeoutId = setTimeout(() => reject(new Error(message)), milliseconds);
|
||||
});
|
||||
await unicryptoReady;
|
||||
|
||||
try {
|
||||
return await Promise.race([promise, timeout]);
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
const topologyData = JSON.parse(await readFile(topologyFile, 'utf8'));
|
||||
const topology = await Topology.load(topologyData);
|
||||
const privateKey = await PrivateKey.generate({ strength: 2048 });
|
||||
const network = new Network(privateKey, {
|
||||
topology,
|
||||
directConnection: true
|
||||
});
|
||||
|
||||
async function loadTopology() {
|
||||
const packed = JSON.parse(await readFile(topologyFile, 'utf8'));
|
||||
return Topology.load(packed);
|
||||
}
|
||||
console.log(`Connecting to ${topology.size()} Universa nodes from ${topologyFile}`);
|
||||
await network.connect();
|
||||
|
||||
async function testNetwork(privateKey) {
|
||||
const topology = await loadTopology();
|
||||
const network = new Network(privateKey, {
|
||||
topology,
|
||||
directConnection: true
|
||||
});
|
||||
const response = await network.command('sping');
|
||||
assert.equal(response?.sping, 'spong', 'unexpected network state response');
|
||||
|
||||
console.log(`Testing ${topology.size()} Universa nodes from ${topologyFile}`);
|
||||
await withTimeout(network.connect(), 15_000, 'Network connection timed out');
|
||||
|
||||
const nodes = Object.entries(network.topology.nodes);
|
||||
const results = await Promise.all(nodes.map(async ([id, node]) => {
|
||||
try {
|
||||
const connection = await withTimeout(
|
||||
network.nodeConnection(id),
|
||||
10_000,
|
||||
`${node.name} connection timed out`
|
||||
);
|
||||
const response = await connection.command('sping', {}, { timeout: 5_000 });
|
||||
if (response?.sping !== 'spong') throw new Error('unexpected sping response');
|
||||
console.log(`PASS ${node.name}: authenticated, sping=spong`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`FAIL ${node.name}: ${error instanceof Error ? error.message : error}`);
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
|
||||
const passed = results.filter(Boolean).length;
|
||||
const quorum = Math.max(1, Math.ceil(nodes.length * 0.4));
|
||||
if (passed < quorum) {
|
||||
throw new Error(`Network quorum failed: ${passed}/${nodes.length} nodes passed; ${quorum} required`);
|
||||
}
|
||||
|
||||
console.log(`Universa network is functioning: ${passed}/${nodes.length} nodes passed`);
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage: u-js-sample [options]
|
||||
|
||||
Options:
|
||||
--generate-key Generate a private key and print its short address
|
||||
--test-network Test every node in src/universa.json
|
||||
--help Show this help`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const options = new Set(process.argv.slice(2));
|
||||
if (options.size === 0 || options.has('--help')) return printHelp();
|
||||
|
||||
const unknown = [...options].find((option) => (
|
||||
option !== '--generate-key' && option !== '--test-network'
|
||||
));
|
||||
if (unknown) throw new Error(`Unknown option: ${unknown}`);
|
||||
|
||||
if (options.has('--generate-key') || options.has('--test-network')) {
|
||||
await unicryptoReady;
|
||||
const privateKey = await PrivateKey.generate({ strength: 2048 });
|
||||
console.log(`Universa short address: ${privateKey.publicKey.shortAddress58}`);
|
||||
|
||||
if (options.has('--test-network')) await testNetwork(privateKey);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await main();
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : error);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
console.log(`Universa network is functioning: ${network.size()} nodes, sping=spong`);
|
||||
Reference in new issue
Block a user