diff --git a/scripts/lib/mp-sim-engine.js b/scripts/lib/mp-sim-engine.js index 3887e76..6a19f3c 100644 --- a/scripts/lib/mp-sim-engine.js +++ b/scripts/lib/mp-sim-engine.js @@ -6,8 +6,10 @@ // const engine = createEngine({workers: 2, heapMb: 1536, root, log}); // engine.load(code, [{nid, env?, begin?, lang, prefs, rng?}]) → [{t:'loaded', nid, epoch, day, lost, saved?} | // {t:'loaded', nid, err}], one per network, in order. env: the checkpoint envelope {s, uidN, rt, last, -// day} (s: the save text); begin: {name, home, preset, asn, mp, tut} builds a new network, checkpointed -// at once (saved). A network whose save does not load (or that cannot begin) answers err: broken (4.7). +// day} (s: the save text; rt: what the realm needs besides it to play on exactly as before, opaque +// here: route memory, R's routing point, key order, the sales desk's memory); begin: {name, home, +// preset, asn, mp, tut} builds a new network, checkpointed at once (saved). A network whose save +// does not load (or that cannot begin) answers err: broken (4.7). // engine.day(code, {d, speed, last, alone:[nid], nets:[nid], mute}) → {t:'dayDone', code, d, nets:{[nid]: {day, lost, // pause?, err?, retried?, ms}}}. Each network in sorted order; one that failed twice answers err and // its day as it was, and is broken from then on (no more days; actions answer 'stopped'). diff --git a/scripts/lib/mp-sim-realm.js b/scripts/lib/mp-sim-realm.js index d5a0a19..1e081f0 100644 --- a/scripts/lib/mp-sim-realm.js +++ b/scripts/lib/mp-sim-realm.js @@ -161,7 +161,8 @@ function srvSlimR(){const c=SIM_RTXT.get(R);if(c)return c;const o={}; const t=JSON.stringify(o,srvR6);SIM_RTXT.set(R,t);return t;} const SIM_RTXT=new WeakMap(); -/* ---- route memory (4.1): what compute() reads of yesterday's R; efg cut to the scenic circuits' keys ---- */ +/* ---- rt (4.1, 4.6): what a fresh realm needs besides the save to go on exactly as this one ---- */ +// The route memory: what compute() reads of the R before it (R.load, R.l2route, R.efg cut to the scenic circuits' keys). function srvRtOut(r){const efg={},E=r&&r.efg; if(E)for(const id in E){const t=E[id]&&E[id].t;if(!t||!t.g)continue;const g=[],v=[];for(const [k,s] of t.g)if(String(k).startsWith('l2:')){g.push([k,v.length]);v.push(+t.v[s]);}if(g.length)efg[id]={g,v};} return {load:(r&&r.load)||{},l2route:(r&&r.l2route)||{},efg};} @@ -170,10 +171,54 @@ function srvRtIn(rt){const r=emptyR();if(!rt||typeof rt!=='object')return r; if(rt.efg&&typeof rt.efg==='object'){const o={};for(const id of Object.keys(rt.efg)){const t=rt.efg[id];if(t&&Array.isArray(t.g)&&Array.isArray(t.v))o[id]={g:new Map(t.g),v:t.v};} Object.defineProperty(r,'efg',{value:rwEfgIn(o)});} return r;} -function srvRt(inp){const m=inp?JSON.parse(inp):{};return JSON.stringify(m.mark?(SIMR.markRt||srvRtOut(SIMR.mark?SIMR.mark.R:R)):srvRtOut(R));} -/* R itself, whole, for the fresh realm that replaces this one after a timeout: route memory and compute() give a close R - but not the same one (scenic paths settle over days, and S moved on since R's last routing), so the marked R travels - the way the route worker sends it (rwPack/rwUnpack), as text: Maps, Sets, typed arrays and non-finite numbers tagged. */ +/* The routing point. R after a day is the R of the day's last routing, which ran before the day's end changed S (bills, + moods, customers who left or signed) and started from the memory of the R before it. compute() of the saved S, even + from R's own memory, gives another R, and the next day reads it (eyeball growth, the peering share): a network + restored that way drifts from its first day on. So every compute() keeps, for the R it makes, S as it routed it (one + JSON per top-level key, 0.8 ms big) and the memory it started from. rt carries that point (pt: the memory, at: the + keys that changed since, gone: the keys S did not have then), and a fresh realm routes that S again from that memory: + the same R (srvReroute). Trials route and put R back: their points go with their R. */ +const SIM_RIN=new WeakMap(); +{const c0=compute;compute=function(){const k=S?srvKeyJson():null,rt=JSON.stringify(srvRtOut(R)),v=c0.apply(this,arguments);if(k)SIM_RIN.set(R,{k,rt});return v;};} +// The sales desk's memory (39-sales-auto.js): the offers it tried, which wait a week before it tries them again. It is +// not in S; rt carries the entries of offers still on the table (no other can count again). +function srvSlOut(){const t=UI.slTried;if(!t||t.s!==S||!t.m)return [];const ids=new Set(S.offers.map(o=>o.id));return [...t.m].filter(e=>ids.has(e[0]));} +function srvSlIn(sl){const m=new Map();if(Array.isArray(sl))for(const e of sl.slice(0,1000))if(Array.isArray(e)&&typeof e[0]==='string'&&Number.isInteger(e[1]))m.set(e[0],e[1]);UI.slTried=m.size?{s:S,m}:null;} +/* Key order: a reload gives some keys back with their keys in another order (a customer's defaults go last, the intern's + week tally comes back in its own order, and the week's report lists it in that order). At a checkpoint rt carries the + live text of each such key (ord, srvGate's round trip: ≈ 8 ms big, once per checkpoint), and a fresh realm takes it + only where it holds what sanitize() made of the save (srvSame): only the order comes from it. */ +function srvOrdOut(txt,K){const u0=uidN,r0=S.rng;let B; + try{B=migrate(JSON.parse(txt));}catch(e){srvLog('checkpoint: the save does not load: '+String(e&&e.message||e));return '{}';}finally{uidN=u0;S.rng=r0;} + const o=[];for(const k in K)if(srvOwn(B,k)&&JSON.stringify(B[k])!==K[k])o.push(JSON.stringify(k)+':'+K[k]);return '{'+o.join(',')+'}';} +function srvOrdIn(o){if(!o||typeof o!=='object'||Array.isArray(o))return;for(const k of Object.keys(o))if(srvOwn(S,k)&&srvSame(o[k],S[k]))S[k]=o[k];} +// rt as JSON text: R's own route memory, its routing point against S as K holds it (when R was routed here), ord when +// given, and the desk's memory sl. +function srvRtText(r,K,ord,sl){const p=K&&SIM_RIN.get(r);let t=JSON.stringify(srvRtOut(r)).slice(0,-1); + if(p){const at=[],gone=[];for(const k in p.k)if(K[k]!==p.k[k])at.push(JSON.stringify(k)+':'+p.k[k]);for(const k in K)if(!srvOwn(p.k,k))gone.push(k); + t+=',"pt":'+p.rt.slice(0,-1)+',"at":{'+at.join(',')+'},"gone":'+JSON.stringify(gone)+'}';} + return t+(ord?',"ord":'+ord:'')+',"sl":'+JSON.stringify(sl||[])+'}';} +const srvKeysOf=s=>{const o=JSON.parse(s),K={};for(const k of Object.keys(o)){const j=JSON.stringify(o[k]);if(j!==undefined)K[k]=j;}return K;}; +// The S of a routing point (pt), one JSON per key: the loaded S with the keys of at put back and those of gone left out. +function srvAtK(pt){if(!pt||typeof pt!=='object'||!pt.at||typeof pt.at!=='object'||Array.isArray(pt.at)||!Array.isArray(pt.gone))return null; + const K=srvKeyJson();for(const k of Object.keys(pt.at))K[k]=JSON.stringify(pt.at[k]);for(const k of pt.gone)if(typeof k==='string')delete K[k];return K;} +// R as the routing point gives it: that S routed from that memory, then the loaded S again (R's attacks found in it). +function srvReroute(pt){const K=srvAtK(pt);if(!K)return false;const S1=S,u0=uidN; + try{S=JSON.parse(srvJoin(K));R=srvRtIn(pt);compute();} + catch(e){srvLog('routing the checkpoint again failed: '+String(e&&e.stack||e));return false;} + finally{S=S1;uidN=u0;} + srvAttacks();return true;} +// R with its point, when it came whole (rpack): a checkpoint before its next routing still carries the point. +function srvPointIn(r,pt){const K=srvAtK(pt);if(K)SIM_RIN.set(r,{k:K,rt:JSON.stringify({load:pt.load||{},l2route:pt.l2route||{},efg:pt.efg||{}})});} +// R's attacks are its only references into S (rwPack): looked up again in a new S. +function srvAttacks(){if(!R||!Array.isArray(R.ddos))return;const as=ddosAll(S,false);for(const x of R.ddos)if(x.attack)x.attack=as.find(a=>a.id===x.attack.id)||null;} +// mark: rt of the marked R against the marked S (a day's mark has it at once); else of R against S now. +function srvRt(inp){const m=inp?JSON.parse(inp):{},k=SIMR.mark; + if(!m.mark)return srvRtText(R,srvKeyJson(),'',srvSlOut()); + return SIMR.markRt||(k?srvRtText(k.R,SIMR.markK||srvKeysOf(k.s),'',k.sl):srvRtText(R,null,'',[]));} +/* R itself, whole, for the fresh realm that replaces this one after a timeout (routing it again from its point costs a + routing; the old realm still answers), the way the route worker sends it (rwPack/rwUnpack), as text: Maps, Sets, typed + arrays and non-finite numbers tagged. A checkpoint carries the point instead: 20 KB gzip, where R is 375 KB. */ const SIM_TA=new Set(['Float64Array','Float32Array','Int32Array','Uint32Array','Int16Array','Uint16Array','Int8Array','Uint8Array','Uint8ClampedArray']); const srvEnc=(k,v)=>typeof v==='number'&&!Number.isFinite(v)?{__n:String(v)}:v instanceof Map?{__m:[...v]}:v instanceof Set?{__s:[...v]}:ArrayBuffer.isView(v)&&SIM_TA.has(v.constructor.name)?{__t:v.constructor.name,a:Array.from(v)}:v; const srvDec=(k,v)=>!v||typeof v!=='object'||Array.isArray(v)?v:'__n' in v?Number(v.__n):'__m' in v?new Map(v.__m):'__s' in v?new Set(v.__s):'__t' in v&&SIM_TA.has(v.__t)?new self[v.__t](v.a):v; @@ -183,18 +228,22 @@ function srvSetPrefs(p){if(!p||typeof p!=='object')return;for(const k of SIMT.PR function srvFresh(epoch){SIMR.epoch=String(epoch||'');SIMR.v=0;SIMR.seen=false;SIMR.fx=[];SIMR.acts=[];SIMR.floats=0;SIMR.rSent=new WeakRef(R);SIMR.base=srvKeyJson();SIMR.mark=null;SIMR.markK=null;SIMR.markRt=null; UI.sel=null;UI.confirm=null;UI.pending=null;UI.started=true;UI.pitched=true;UI.clockLocked=true;$('modal').hidden=true;} -/* srvLoad: a network from its save (a checkpoint, or the mark of a realm that timed out). The route memory goes in - before compute(), the way the route worker seeds its R: then the days to come are the ones the old realm had. A - realm that replaces one that timed out gets the old realm's R itself when it could still pack it (rpack). */ -function srvLoad(inp){const m=JSON.parse(inp);SIMR.strict=!!m.strict; +/* srvLoad: a network from its save (a checkpoint, or the mark of a realm that timed out) and its rt: S in its live key + order, R routed again from its routing point, the desk's memory; then the days to come are the ones the old realm + had. A realm that replaces one that timed out gets the old realm's R itself when it could still pack it (rpack); live: + the save is the old realm's own S as text (a mark), key order and all. An rt without a point (an older checkpoint) + seeds R with its memory before compute(), the way the route worker does: close, not always the same. */ +function srvLoad(inp){const m=JSON.parse(inp),rt=m.rt&&typeof m.rt==='object'?m.rt:{};SIMR.strict=!!m.strict; S=migrate(JSON.parse(m.save)); if(typeof S.rng!=='number')S.rng=(m.rng|0)||1; if(Number.isInteger(m.uidN)&&m.uidN>0)uidN=m.uidN; srvSetPrefs(m.prefs); // A test's pinned clock goes on where the realm this one replaces had it. if(self.__srvClock&&typeof m.clock==='number')__srvClock(m.clock); - if(typeof m.rpack==='string'&&m.rpack){const x=JSON.parse(m.rpack,srvDec);R=rwUnpack(x.o,x.p);UI.hotMap=hotByCity();} - else{R=srvRtIn(m.rt);compute();} + srvOrdIn(m.live?JSON.parse(m.save):rt.ord); + if(typeof m.rpack==='string'&&m.rpack){const x=JSON.parse(m.rpack,srvDec);R=rwUnpack(x.o,x.p);UI.hotMap=hotByCity();srvPointIn(R,rt.pt);} + else if(!srvReroute(rt.pt)){R=srvRtIn(rt);compute();} + srvSlIn(rt.sl); srvFresh(m.epoch); // Whatever a loaded network says before its first snapshot (days nobody watched) is older than that snapshot: the // first pull handles it like any (5.11). Only a new network's welcome waits for its first page (srvBegin). @@ -215,24 +264,26 @@ function srvBegin(inp){const m=JSON.parse(inp),b=m.begin||{};SIMR.strict=!!m.str function srvPrefs(inp){const m=JSON.parse(inp);srvSetPrefs(m.prefs);return '{}';} /* srvMark: the restore point of the next day (day) or action (keys: one JSON per top-level key, whose join is the same - string, kept for the action's own comparison). The worker keeps the string, uidN and the route memory too, to build a - fresh realm if this one times out. */ -function srvMark(inp){const m=inp?JSON.parse(inp):{};let s; - if(m.keys){SIMR.markK=srvKeyJson();s=srvJoin(SIMR.markK);}else{SIMR.markK=null;s=JSON.stringify(S);} - SIMR.mark={s,uidN,rng:S.rng,R,...srvClockNow(),fx:SIMR.fx.slice(),floats:SIMR.floats,ui:{sel:UI.sel,tab:UI.tab,left:UI.left,confirm:UI.confirm,pending:UI.pending}}; - // A copy: R.load and R.l2route are R's own objects, and a day that fails half-way may already have written to them. - const rtJ=m.day?JSON.stringify(srvRtOut(R)):'';SIMR.markRt=rtJ?JSON.parse(rtJ):null;const h=JSON.stringify({uidN,day:S.day,...srvClockNow()}); + string, kept for the action's own comparison). The worker keeps the string, uidN and a day's rt too, to build a fresh + realm if this one times out. */ +function srvMark(inp){const m=inp?JSON.parse(inp):{};let s,K=null; + if(m.keys||m.day){K=srvKeyJson();s=srvJoin(K);}else s=JSON.stringify(S); + SIMR.markK=m.keys?K:null;const sl=srvSlOut(); + SIMR.mark={s,uidN,rng:S.rng,R,sl,...srvClockNow(),fx:SIMR.fx.slice(),floats:SIMR.floats,ui:{sel:UI.sel,tab:UI.tab,left:UI.left,confirm:UI.confirm,pending:UI.pending}}; + // As text: nothing the day does reaches it. + const rtJ=m.day?srvRtText(R,K,'',sl):'';SIMR.markRt=rtJ||null;const h=JSON.stringify({uidN,day:S.day,...srvClockNow()}); return (rtJ?h.slice(0,-1)+',"rt":'+rtJ+'}':h)+'\\n'+s;} /* srvRestore: back to the mark, in place (after a throw: the finally blocks ran). S is parsed back only when it changed, so the S object stays on every normal path; R is the marked object, its attacks looked up again in a parsed - S. (The design rebuilt R from route memory after a failed day; that R is close, not the same: tests/mp-sim-determinism.) */ + S; the sales desk's memory is the marked one (a failed day may have added to it). (The design rebuilt R from route + memory after a failed day; that R is close, not the same: tests/mp-sim-determinism.) */ function srvRestore(inp){const k=SIMR.mark;if(!k)return '{"none":1}'; let parsed=false;if(JSON.stringify(S)!==k.s){S=JSON.parse(k.s);parsed=true;} uidN=k.uidN;S.rng=k.rng;if(self.__srvClock&&typeof k.clock==='number')__srvClock(k.clock); // R: the marked object. A day or an action routes into a new R (compute() never writes to the one it starts from), // so the marked one is still the R the network had: route memory and compute() would only give one close to it. - R=k.R;if(parsed&&R&&Array.isArray(R.ddos)){const as=ddosAll(S,false);for(const x of R.ddos)if(x.attack)x.attack=as.find(a=>a.id===x.attack.id)||null;} - UI.hotMap=hotByCity(); + R=k.R;if(parsed)srvAttacks(); + UI.hotMap=hotByCity();UI.slTried=k.sl.length?{s:S,m:new Map(k.sl)}:null; Object.assign(UI,k.ui);SIMR.fx=k.fx.slice();SIMR.floats=k.floats;SIMR.modal=null;SIMR.closed=false;$('modal').hidden=true; return JSON.stringify({parsed});} @@ -361,7 +412,9 @@ function srvSub(inp){const m=inp?JSON.parse(inp):{};return JSON.stringify({v:SIM // The one-shots a page says when a network opens (the courtesy route, Australia's prices): they change S, so the // server says them, as fx of the next update. function srvGreet(inp){bootNotes();return srvPull(inp);} -function srvSave(){const txt=JSON.stringify(packSave(S));return srvOut({uidN,rt:srvRtOut(R),day:S.day,lost:!!S.lost,...srvClockNow()},txt);} +// A checkpoint: the save and its rt, ord included. +function srvSave(){const txt=JSON.stringify(packSave(S)),K=srvKeyJson(),h=JSON.stringify({uidN,day:S.day,lost:!!S.lost,...srvClockNow()}); + return h.slice(0,-1)+',"rt":'+srvRtText(R,K,srvOrdOut(txt,K),srvSlOut())+'}\\n'+txt;} function srvCard(){return JSON.stringify({card:mpCard()});} `; diff --git a/scripts/lib/mp-sim-worker.js b/scripts/lib/mp-sim-worker.js index 5a225cd..e01860b 100644 --- a/scripts/lib/mp-sim-worker.js +++ b/scripts/lib/mp-sim-worker.js @@ -7,10 +7,11 @@ // unloads of a session wait for its day; actions for a network that is still loading wait for it. // // Failures. Every realm call has a time limit (a day 2 s, an action 1 s, a what-if 3 s, a pull or a save 1 s). Before -// a day or an action the realm is marked (its S as text, uidN, the dice, and for a day yesterday's route memory), and the -// worker keeps the mark. A call that throws has run its finally blocks: the realm goes back to the mark in place. A call -// that times out has not (vm skips them), so that realm is never used again: a fresh one is built from the mark, with a -// new epoch. A failed day runs once more; failing again, the network is broken at its state before the day. +// a day or an action the realm is marked (its S as text, uidN, the dice, and for a day its rt: route memory and routing +// point), and the worker keeps the mark. A call that throws has run its finally blocks: the realm goes back to the mark +// in place. A call that times out has not (vm skips them), so that realm is never used again: a fresh one is built from +// the mark, with a new epoch. A failed day runs once more; failing again, the network is broken at its state before +// the day. // // The worker → host replies are the shapes of tests/lib/mp-sim-shape.js checkWorker(), sent as {id, ok, reply} (or // {id, ok:false, error}) for the engine to match its promises; logs go up as {log:[level, text]}. @@ -44,27 +45,28 @@ function createCore(opts = {}) { } // A realm call: [header, ...parts]; a throw inside the realm comes back as {srvErr}, a timeout throws. const call = (e, name, input, ms) => e.realm.call(name, input, ms); - /* A fresh realm in place of one that timed out, from {s, uidN, rt}: same network, new epoch, versions from 0. R comes - from the old realm, which still answers after a timeout: the marked R (mark) or its current one, packed whole. Only - when it cannot is R routed again from the route memory, which gives a close R, not always the same. */ + /* A fresh realm in place of one that timed out, from {s, uidN, rt, live}: same network, new epoch, versions from 0. R + comes from the old realm, which still answers after a timeout: the marked R (mark) or its current one, packed whole. + When it cannot, R is routed again from rt's routing point. live: s is a mark, the realm's own S as text. */ function rebuild(e, from, mark = true) { let rpack = null; try { const s = e.realm && e.realm.raw('srvRPack', { mark }, T.load); if (typeof s === 'string' && s.startsWith('{"o"')) rpack = s; } catch (x) { /* gone */ } const r = build(e.nid, e.lang), epoch = epochOf(); - const [h] = r.call('srvLoad', { save: from.s, uidN: from.uidN, rt: from.rt, rpack, prefs: e.prefs, epoch, strict: !!opts.strict, clock: from.clock }, T.load); + const [h] = r.call('srvLoad', { save: from.s, uidN: from.uidN, rt: from.rt, rpack, live: !!from.live, prefs: e.prefs, epoch, strict: !!opts.strict, clock: from.clock }, T.load); if (h.srvErr) throw new Error('rebuilding the realm failed: ' + h.srvErr); e.realm = r; e.epoch = epoch; e.v = 0; e.day = h.day; e.lost = h.lost; log('warn', `[${e.nid}] realm rebuilt after a timeout (epoch ${epoch}${rpack ? '' : ', R routed again'})`); } - // The route memory of the old realm's mark, if it still answers; else the last day's. + // The rt of the old realm's mark, if it still answers; else the last day's, whose routing point belongs to that day's + // S: with another mark only its route memory counts (a close R). function markRt(e) { try { const [h] = call(e, 'srvRt', { mark: true }, 200); if (h && !h.srvErr && h.load) return h; } catch (x) { /* gone */ } - return e.dayMark ? e.dayMark.rt : null; + return e.dayMark ? (e.dayMark === e.mark ? e.dayMark.rt : { ...e.dayMark.rt, pt: undefined }) : null; } function mark(e, input) { const [h, s] = call(e, 'srvMark', input, T.mark); if (h.srvErr) throw new Error('mark failed: ' + h.srvErr); - const m = { s, uidN: h.uidN, rt: h.rt || null, day: h.day, clock: h.clock }; + const m = { s, uidN: h.uidN, rt: h.rt || null, day: h.day, clock: h.clock, live: true }; if (input.day) e.dayMark = m; e.mark = m; return m; diff --git a/tests/lib/mp-sim-play.js b/tests/lib/mp-sim-play.js index c653601..02b2e4a 100644 --- a/tests/lib/mp-sim-play.js +++ b/tests/lib/mp-sim-play.js @@ -9,7 +9,7 @@ // begin(nid, o) a new network's begin entry for engine.load() // RSIG R with its lazy parts read out, attacks by id (as tests/route-worker.test.js reads it) // checked(r) r, after checkWorker (and checkEvent of the upd/snap inside) found nothing -// differs(a, b), canon(s) the first differing top-level keys of two JSON texts; a JSON text with its keys sorted +// differs(a, b) the first differing top-level keys of two JSON texts (compare texts, never a deep-equal) 'use strict'; const fs = require('node:fs'); const path = require('node:path'); @@ -77,9 +77,4 @@ function differs(a, b) { .slice(0, 4).map((k) => `${k}: ${String(JSON.stringify(A2[k])).slice(0, 300)} ≠ ${String(JSON.stringify(B2[k])).slice(0, 300)}`).join('\n'); } -// JSON text with every object's keys sorted (arrays keep their order): two networks that differ only in key order are -// the same text. Compare these strings, never the parsed objects with a deep-equal. -const sortK = (v) => (Array.isArray(v) ? v.map(sortK) : v && typeof v === 'object' ? Object.keys(v).sort().reduce((o, k) => ((o[k] = sortK(v[k])), o), {}) : v); -const canon = (s) => JSON.stringify(sortK(JSON.parse(s))); - -module.exports = { CODE, PID, DEV, MP, T0, STEP, TEST, ROOMY, sim, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs, canon }; +module.exports = { CODE, PID, DEV, MP, T0, STEP, TEST, ROOMY, sim, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs }; diff --git a/tests/mp-sim-determinism.test.js b/tests/mp-sim-determinism.test.js index 1d18173..4746f08 100644 --- a/tests/mp-sim-determinism.test.js +++ b/tests/mp-sim-determinism.test.js @@ -2,23 +2,25 @@ // - Realm vs jsdom: the big network with its L2 customers on scenic routes, 60 days with actions in between, the // page's arm clicking as a player does (each click ending in after()) and rendering the HUD after every day: S and R // byte for byte the same. -// - Restore: 20 days straight, or 10, a checkpoint, a fresh realm and 10 more: the same network. -// - Retry: a day that fails once (a throw, and a timeout, both after the day's DDoS step with an attack on) leaves the -// same network as no failure. +// - Restore: a checkpoint after any day, loaded into a fresh realm, plays on byte for byte as the network that kept +// going, the scenic big network and the same one busy (intern, sales desk, failures, events, an attack). +// - Retry: a day that fails once (a throw, and a timeout, both after the day's DDoS step with an attack on, the networks +// busy) leaves the same network as no failure. // - Order: three networks of a session, in two different realm orders, each the same. // - Market and moved customers (3.2.0, 3.3.0): the market's settings and a customer moved to another fiber or pinned // to a path, then days of sales, against the page and under the strict gate; a checkpoint of it plays on the same. // - Timer audit and gate: days with failures, events, the intern on every order and the sales desk on auto, then every // sent button a rich crawl finds: no timer is ever scheduled on these paths, nothing changes after them, and no // action is rolled back by the sanitize gate (which also runs after every day here, SIM_STRICT). -// SIM_LONG=1 plays the audit for a year, as section 11 asks (a few minutes); by default it plays 60 days. +// SIM_LONG=1 plays the audit for a year, as section 11 asks, and checkpoints after every day to 30 (a few minutes); by +// default the audit plays 60 days. const test = require('node:test'); const assert = require('node:assert/strict'); const { createEngine } = require('../scripts/lib/mp-sim-engine'); const { crawl } = require('./lib/mp-sim-crawl'); const A = require('../scripts/lib/mp-sim-acts'); const { load } = require('./harness'); -const { CODE, MP, TEST, ROOMY, T0, STEP, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs, canon } = require('./lib/mp-sim-play'); +const { CODE, MP, TEST, ROOMY, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs } = require('./lib/mp-sim-play'); const LONG = process.env.SIM_LONG === '1'; /* Acts the gate refuses because the game leaves a key in a shape sanitize() repairs: game fixes for the integrator (the @@ -27,6 +29,10 @@ const LONG = process.env.SIM_LONG === '1'; const PENDING = { buyTE: 'te', mkBanana: 'monkey' }; const E = createEngine({ workers: 2, test: TEST, timeouts: ROOMY }); test.after(() => E.close()); +// The big network with everything that keeps state between days on: the intern on every order, the sales desk (rep) +// signing on its own, a failure and an event due. +const busy = (rep) => `S.lvl=Math.max(S.lvl,6);S.rules=makeRules('bgp');ACT.opsHire({id:'kevin'});for(const k in S.ops.on)S.ops.on[k]=true;S.ops.floor=0;closeModal(); + ACT.salesHire({id:'${rep}'});ACT.salesAuto();closeModal();S.nextFail=S.day+2;S.nextEvent=S.day+3;compute();`; const on = (X) => ({ day: (code, d, nets, o = {}) => X.day(code, { d, speed: 1, last: 1, alone: [], nets, ...o }).then(checked), realmS: async (code, nid) => (await X.eval(code, nid, 'return JSON.stringify(S)')).value, @@ -90,26 +96,64 @@ test('realm vs jsdom: 60 days of the scenic big network with actions in between, assert.deepEqual(P.errors, []); }); -test('restore: 20 days straight, or 10, a checkpoint, a fresh realm and 10 more, is the same network', async () => { - const code = 'R3ST0R', a = 'rsta0001', b = 'rstb0002', c = 'rstc0003'; - await loadBig(code, a); await loadBig(code, b); - for (let d = 1; d <= 10; d++) await day(code, d, [a, b]); - // b's checkpoint: the envelope a server writes (4.6), into a fresh realm; the network's clock goes on. - const sv = checked(await E.save(code, b)); - const env = { s: sv.txt, uidN: sv.uidN, rt: sv.rt, last: 10, day: sv.day }; - await E.unload(code, b); - checked((await E.load(code, [{ nid: c, env: JSON.parse(JSON.stringify(env)), lang: 'en', prefs: {} }]))[0]); - await E.eval(code, c, `__srvClock(${T0 + 10 * STEP})`); - for (let d = 11; d <= 20; d++) await day(code, d, [a, c]); - // The same play: every value the same. (Only the order of a customer's keys can differ: the save drops the defaults - // that sanitize() puts back, at the end. A page takes a snapshot of a reloaded realm anyway, a new epoch.) - // Compared as text, keys sorted: a deep-equal of two big networks that differ builds a diff of gigabytes. - const sa = canon(await realmS(code, a)), sc = canon(await realmS(code, c)); - assert.ok(sc === sa, `S after 20 days:\n${sc === sa ? '' : differs(sa, sc)}`); - const ra = canon(await realmR(code, a)), rc = canon(await realmR(code, c)); - assert.ok(rc === ra, `R after 20 days:\n${rc === ra ? '' : differs(ra, rc)}`); - assert.ok(JSON.parse(sa).biz.some((x) => x.route === 'scenic'), 'with scenic circuits'); - await E.unload(code); +/* A restart or an idle unload is invisible (4.6, 4.7, 11): a network checkpointed after any day and loaded into a fresh + realm is at once, and stays, the network that kept going, S and R byte for byte. Two of them: the scenic big network, + and the same busy (busy(), and an attack on). They need everything rt carries besides the save: R's routing point (R + after a day is the R of the day's last routing, made before the day's end changed S, so the saved S routes to another + R, which the next day reads; a checkpoint after day 10 passed by luck: a failure or event routed at that day's end), the + sales desk's tried offers, and the live order of keys a reload reorders (the intern's weekly report lists its tally + in it). Checkpoints after days 1, 2, 5 and 10 of each (2 and 10 right after an action), each played 10 days on; + SIM_LONG: after every day up to 30. Compared as text: a deep-equal of two big networks that differ builds a diff of + gigabytes. */ +test('restore: a checkpoint after any day, in a fresh realm, plays on byte for byte as the network that kept going', { timeout: LONG ? 1800e3 : 300e3 }, async (t) => { + const X = createEngine({ workers: 1, test: TEST, timeouts: ROOMY, heapMb: LONG ? 4096 : 1536 }); + t.after(() => X.close()); + const { day, realmS, realmR } = on(X); + const code = 'R3ST0R', H = 10, KS = LONG ? Array.from({ length: 30 }, (_, i) => i + 1) : [1, 2, 5, 10], N = Math.max(...KS) + H; + const bases = ['busy0000', 'scen0000']; + for (const nid of bases) checked((await X.load(code, [{ nid, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'en', prefs: {} }]))[0]); + assert.equal((await X.eval(code, bases[0], busy('eng') + 'const a=ddosAttack(true);a.until=S.day+6;return slP();')).value, 'eng'); + const same = async (what, a, b) => { + const sa = await realmS(code, a), sb = await realmS(code, b); + assert.ok(sa === sb, `${what}: S\n${sa === sb ? '' : differs(sa, sb)}`); + const ra = await realmR(code, a), rb = await realmR(code, b); + assert.ok(ra === rb, `${what}: R\n${ra === rb ? '' : differs(ra, rb)}`); + }; + let copies = []; + const carried = { point: 0, desk: 0, order: 0 }; + for (let d = 1; d <= N; d++) { + await day(code, d, [...bases, ...copies.map((c) => c.nid)]); + for (const c of copies.filter((x) => x.k + H === d)) { await same(`${c.nid}, ${H} days after its checkpoint`, c.base, c.nid); await X.unload(code, c.nid); } + copies = copies.filter((x) => x.k + H > d); + if (!KS.includes(d)) continue; + // Every other one right after an action and a what-if (a server also checkpoints a network after an action), sent + // to the network and to its copies alike. + if (d % 2 === 0) for (const b of bases) { + const city = (await X.eval(code, b, "return Object.keys(CITY).filter(c=>!S.pops[c]&&homeOk(c)).sort((x,y)=>kmBetween('fra',x)-kmBetween('fra',y))[0]")).value; + const lid = (await X.eval(code, b, 'return S.links[0].id')).value; + for (const nid of [b, ...copies.filter((c) => c.base === b).map((c) => c.nid)]) { + assert.equal(checked(await X.act(code, nid, msg('buildPop', { city }))).outcome, 'applied', `${nid}: a POP in ${city}`); + assert.ok(checked(await X.ask(code, nid, msg('linkCheck', { id: lid }))).modal, `${nid}: the what-if`); + } + } + for (const b of bases) { + // The envelope a server writes (4.6), through text, into a fresh realm; the network's clock goes on. + const sv = checked(await X.save(code, b)), nid = b.slice(0, 4) + String(d).padStart(4, '0'); + const env = JSON.parse(JSON.stringify({ s: sv.txt, uidN: sv.uidN, rt: sv.rt, last: d, day: sv.day })); + checked((await X.load(code, [{ nid, env, lang: 'en', prefs: {} }]))[0]); + await X.eval(code, nid, `__srvClock(${(await X.eval(code, b, 'return __srvClock()')).value});return 1;`); + await same(`${nid}, loaded from its checkpoint after day ${d}`, b, nid); + copies.push({ nid, base: b, k: d }); + const rt = env.rt; + if (rt.pt && rt.pt.at.biz) carried.point++; + if (rt.sl.length) carried.desk++; + if (rt.ord && rt.ord.ops) carried.order++; + } + } + // What it played: scenic circuits, and checkpoints that needed each part of rt. + assert.ok(JSON.parse(await realmS(code, bases[1])).biz.some((x) => x.route === 'scenic'), 'with scenic circuits'); + assert.ok(carried.point && carried.desk && carried.order, `rt carried each part: ${JSON.stringify(carried)}`); + await X.unload(code); }); test('retry: a day that throws, or times out, once after the DDoS step with an attack on, leaves the same network', async (t) => { @@ -120,6 +164,8 @@ test('retry: a day that throws, or times out, once after the DDoS step with an a const code = 'R3TRYX', clean = 'rtya0001', thrown = 'rtyb0002', slow = 'rtyc0003', nets = [clean, thrown, slow]; const L = await E.load(code, nets.map((nid) => ({ nid, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'en', prefs: {} }))); L.forEach(checked); + // Busy: what keeps state between days goes back to the mark too (the sales desk's tried offers live outside S). + for (const nid of nets) await E.eval(code, nid, busy('eng') + 'return 1;'); for (let d = 1; d <= 3; d++) await day(code, d, nets); // An attack that is still on during day 4 (the same one in each). for (const nid of nets) assert.ok((await E.eval(code, nid, 'const a=ddosAttack(true);a.until=S.day+3;return ddosAll().length')).value > 0, 'an attack is on'); @@ -243,14 +289,14 @@ test('market and moved customers: realm and page the same, the strict gate passe assert.ok(U.fx.some(([k, p]) => k === 'toast' && p.k === 'good'), 'the sales were told'); const sold = ev('S.stats.cmSold'); assert.ok(sold > 20 && ev("S.biz.find(b=>b.id==='k').pin.join()") === via, `the market sold (${sold}) and the circuit stayed pinned`); - // A checkpoint into a fresh realm (route memory, no route cache) plays on as the realm that kept going. + // A checkpoint into a fresh realm (its rt, no route cache) plays on byte for byte as the realm that kept going. const sv = checked(await S2.save(code, nid)); checked((await S2.load(code, [{ nid: nid2, env: { s: sv.txt, uidN: sv.uidN, rt: sv.rt }, lang: 'en', prefs: {} }]))[0]); await realm(nid2, `__srvClock(${await realm(nid, 'return __srvClock()')});return 1;`); await days(15, [nid, nid2], false); - const s1 = canon(await realm(nid, 'return JSON.stringify(S)')), s2 = canon(await realm(nid2, 'return JSON.stringify(S)')); + const s1 = await realm(nid, 'return JSON.stringify(S)'), s2 = await realm(nid2, 'return JSON.stringify(S)'); assert.ok(s1 === s2, `S after the checkpoint:\n${s1 === s2 ? '' : differs(s1, s2)}`); - assert.ok(canon(await realm(nid, `return ${RSIG}`)) === canon(await realm(nid2, `return ${RSIG}`)), 'R after the checkpoint'); + assert.ok(await realm(nid, `return ${RSIG}`) === await realm(nid2, `return ${RSIG}`), 'R after the checkpoint'); assert.ok(JSON.parse(s1).stats.cmSold > sold, 'it went on selling'); assert.deepEqual(await realm(nid, 'return [__SRV_T.n,__SRV_T.at]'), [0, []], 'no timer'); assert.deepEqual(P.errors, []); @@ -260,8 +306,7 @@ test(`timer audit and gate: ${LONG ? 365 : 60} days with everything on, then eve const S2 = createEngine({ workers: 1, test: TEST, strict: true, timeouts: ROOMY }); t.after(() => S2.close()); const code = 'AVD1T5', nid = 'audt0001'; - const SETUP = `S.lvl=Math.max(S.lvl,6);S.rules=makeRules('bgp');ACT.opsHire({id:'kevin'});for(const k in S.ops.on)S.ops.on[k]=true;S.ops.floor=0;closeModal(); - ACT.salesHire({id:'shark'});ACT.salesAuto();closeModal();S.nextFail=S.day+2;S.nextEvent=S.day+3;compute();`; + const SETUP = busy('shark'); checked((await S2.load(code, [{ nid, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'en', prefs: {} }]))[0]); await S2.eval(code, nid, SETUP + 'return 1;'); const timers = async () => (await S2.eval(code, nid, 'return [__SRV_T.n,__SRV_T.at]')).value;