54 lines
5.2 KiB
JavaScript
54 lines
5.2 KiB
JavaScript
// Controlul negativ al lantului ca martor (agent-ancora.mjs, proba-agent-ancora.mjs): fiecare paznic scos intr-o COPIE a dosarului
|
|
// trebuie sa inroseasca proba NUMITA, cu proba chiar rulata (rezumatul ei exista); pe copia neatinsa, verde. Trei stari: un tipar care
|
|
// nu apare exact o data sau o proba care nu ajunge la rezumat e STRICAT si se numara esec.
|
|
// node control-negativ-ancora.mjs iesire 0 = martorul verde si toate plantarile rosii pe proba lor
|
|
import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path';
|
|
import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url';
|
|
|
|
const AICI = path.dirname(fileURLToPath(import.meta.url));
|
|
const A = 'agent-ancora.mjs';
|
|
const PLANTARI = [
|
|
// [nume, tipar, inlocuire, proba (inceputul numelui ei)]
|
|
['un cap al altei ramuri primit', 'if (!e || e.hash !== st.hash) return', 'if (false) return', '5. o alta ramura a agentului'],
|
|
['un plic atins (statementHash nerefacut) primit', "if (!H32.test(String(env.statementHash)) || sha(JSON.stringify(st)) !== env.statementHash) return { ok: false, motiv: 'the statementHash does not match", "if (false) return { ok: false, motiv: 'the statementHash does not match", '1. CONTROL: un cap cu hash-ul schimbat'],
|
|
['un cap care numeste alt agent primit', "if (st.agentId !== ledger.agentId || st.session !== ledger.session", "if (false && st.session !== ledger.session", '1. CONTROL: un cap care numeste alt agent'],
|
|
['o finalitate FAILED citita ca moment', "if (f.state !== 'PASSED') return", 'if (false) return', '2. CONTROL: finalitate FAILED'],
|
|
['ISO si unix nu mai trebuie sa fie acelasi moment', "if (!m || Math.floor(Date.parse(m[1]) / 1000) !== Number(m[2])) return", 'if (!m) return', '2. CONTROL: ISO si unix'],
|
|
['plicul judecat de verificator nu mai trebuie sa fie acesta', 'if (v.statementHash && String(v.statementHash).toLowerCase() !== env.statementHash) {', 'if (false) {', '3. CONTROL: verificatorul a judecat alt statementHash'],
|
|
['momentul ancorei luat din plic, nu din certificat', 'anchors.push({ seq: m.seq, hash: m.hash, at: fs2.at,', 'anchors.push({ seq: m.seq, hash: m.hash, at: Number(env.notarization && env.notarization.firstSeen) || fs2.at,', '3. CONTROL: momentul ancorei e cel certificat'],
|
|
['finalitatea in asteptare data drept ancora', "if (fs2.stare !== 'PASSED') {", 'if (false) {', '3. CONTROL: finalitate in asteptare'],
|
|
['notarizarea pe 2800 fara confirmare', 'if (chainId === 2800 && !confirmMainnet) return', 'if (false) return', '6. un nod al lantului 2800'],
|
|
['notarul fara contract primit', "if (!cod || cod === '0x') return", 'if (false) return', '6. niciun contract la adresa notarului'],
|
|
['un plic atins notarizat', "if (sha(JSON.stringify(st)) !== envelope.statementHash) return { ok: false, cod: 1, motiv: 'REFUSED", "if (false) return { ok: false, cod: 1, motiv: 'REFUSED", '6. notarizarea refuza un cap atins'],
|
|
];
|
|
const FISIERE = [A, 'proba-agent-ancora.mjs', 'agent-policy.mjs', 'agent-ledger.mjs', 'agent-aprobare.mjs'];
|
|
function copie() { const t = fs.mkdtempSync(path.join(os.tmpdir(), 'aere-anc-ctl-')); for (const f of FISIERE) fs.copyFileSync(path.join(AICI, f), path.join(t, f)); return t; }
|
|
function ruleaza(t) {
|
|
return new Promise((resolve) => {
|
|
const c = spawn(process.execPath, [path.join(t, 'proba-agent-ancora.mjs')], { env: { ...process.env, AERE_VERIFY_PROOF: '' } }); let out = '';
|
|
const ceas = setTimeout(() => c.kill(), 120000);
|
|
c.stdout.on('data', (x) => { out += x; }); c.stderr.on('data', (x) => { out += x; });
|
|
c.on('close', (cod) => { clearTimeout(ceas); resolve({ cod, rulat: /agent-ancora: \d+\/\d+/.test(out), rosii: out.split('\n').filter((l) => l.startsWith(' RAU ')) }); });
|
|
});
|
|
}
|
|
async function planteaza([nume, din, inl, tinta]) {
|
|
const t = copie();
|
|
try {
|
|
const f = path.join(t, A); const src = fs.readFileSync(f, 'utf8');
|
|
if (src.split(din).length !== 2) return [false, ` STRICAT ${nume}: tiparul apare de ${src.split(din).length - 1} ori`];
|
|
fs.writeFileSync(f, src.replace(din, inl));
|
|
const r = await ruleaza(t);
|
|
if (!r.rulat) return [false, ` STRICAT ${nume}: proba nu a ajuns la rezumat (cod ${r.cod})`];
|
|
if (r.rosii.some((l) => l.startsWith(' RAU ' + tinta))) return [true, ` ROSU cum trebuia ${nume} (proba '${tinta}' pica)`];
|
|
return [false, ` CONTROL CAZUT ${nume}: proba '${tinta}' a ramas verde (${r.rosii.length} rosii altundeva)`];
|
|
} finally { fs.rmSync(t, { recursive: true, force: true }); }
|
|
}
|
|
let rele = 0;
|
|
const t0 = copie(); const m = await ruleaza(t0); fs.rmSync(t0, { recursive: true, force: true });
|
|
if (m.rulat && m.cod === 0 && !m.rosii.length) console.log(' OK martorul: copia neatinsa verde'); else { rele++; console.log(` STRICAT martorul nu e verde (cod ${m.cod}, ${m.rosii.length} rosii)`); }
|
|
const rez = new Array(PLANTARI.length); let i = 0;
|
|
await Promise.all(Array.from({ length: 4 }, async () => { while (i < PLANTARI.length) { const k = i++; rez[k] = await planteaza(PLANTARI[k]); } }));
|
|
for (const [bun, linie] of rez) { console.log(linie); if (!bun) rele++; }
|
|
console.log(rele ? `CONTROL NEGATIV: ${rele} probleme` : `DOVEDIT: martorul verde, ${PLANTARI.length} din ${PLANTARI.length} paznici scosi -> proba lor rosie`);
|
|
process.exitCode = rele ? 1 : 0;
|