feat: implement v0.6.0 mutation engine and db_type runtime selection
Rust / build (push) Canceled after 0s
Rust / build (push) Canceled after 0s
This commit is contained in:
1 parent
217dc5f92b
commit
ba768da58e
27 files changed
+2615
-158
No files matched your search
+37
-4
@@ -1,10 +1,21 @@
|
||||
import init, { generate_keypair, sign_message, compute_next_hash, run_program } from './pkg/chronoseal_wasm.js';
|
||||
import init, {
|
||||
generate_keypair,
|
||||
sign_message,
|
||||
compute_next_hash,
|
||||
run_program,
|
||||
init_gene_state,
|
||||
preview_gene_commitment,
|
||||
commit_gene_preview,
|
||||
discard_gene_preview
|
||||
} from './pkg/chronoseal_wasm.js';
|
||||
import { collectEntropy } from './entropy.js';
|
||||
import { sendRequest } from './transport.js';
|
||||
|
||||
let session, prevHash, currentSalt, opcodesB64, lastTime;
|
||||
let minInterval = 12000;
|
||||
let maxInterval = 25000;
|
||||
let pendingMutationStep = 0;
|
||||
let pendingMutationOrderB64 = '';
|
||||
|
||||
export async function initHeartbeat() {
|
||||
await init();
|
||||
@@ -16,6 +27,11 @@ export async function initHeartbeat() {
|
||||
opcodesB64 = initResp.opcodes_b64;
|
||||
minInterval = initResp.heartbeat_min_interval_ms || 12000;
|
||||
maxInterval = initResp.heartbeat_max_interval_ms || 25000;
|
||||
if (!init_gene_state(initResp.gene_size || 512)) {
|
||||
throw new Error('Failed to initialize gene state');
|
||||
}
|
||||
pendingMutationStep = initResp.mutation_step;
|
||||
pendingMutationOrderB64 = initResp.mutation_order_b64;
|
||||
lastTime = performance.now();
|
||||
scheduleNext();
|
||||
}
|
||||
@@ -40,6 +56,10 @@ async function sendHeartbeat() {
|
||||
const timestamp = Date.now();
|
||||
const entropyData = { events: events.map(e => ({ x: e.x, y: e.y, t: e.t })) };
|
||||
const entropyJson = JSON.stringify(entropyData);
|
||||
const geneCommitment = preview_gene_commitment(pendingMutationOrderB64);
|
||||
if (!geneCommitment) {
|
||||
throw new Error('Unable to compute mutation commitment');
|
||||
}
|
||||
|
||||
const signable = {
|
||||
sessionId: session,
|
||||
@@ -47,11 +67,14 @@ async function sendHeartbeat() {
|
||||
timestamp: timestamp,
|
||||
entropyData: entropyData,
|
||||
stackState: JSON.parse(stackState),
|
||||
fingerprint: fingerprint
|
||||
fingerprint: fingerprint,
|
||||
mutationStep: pendingMutationStep,
|
||||
geneCommitment: geneCommitment
|
||||
};
|
||||
const msg = JSON.stringify(signable, Object.keys(signable).sort());
|
||||
const sig = sign_message(msg);
|
||||
if (!sig) {
|
||||
discard_gene_preview();
|
||||
console.error('Keypair not initialised — skipping heartbeat');
|
||||
return;
|
||||
}
|
||||
@@ -62,22 +85,32 @@ async function sendHeartbeat() {
|
||||
entropy_data: entropyData,
|
||||
stack_state: JSON.parse(stackState),
|
||||
fingerprint,
|
||||
mutation_step: pendingMutationStep,
|
||||
gene_commitment: geneCommitment,
|
||||
signature: sig
|
||||
});
|
||||
|
||||
if (resp.next_salt) {
|
||||
if (resp.next_salt && resp.next_mutation_step && resp.next_mutation_order_b64) {
|
||||
if (!commit_gene_preview()) {
|
||||
discard_gene_preview();
|
||||
throw new Error('Failed to commit local mutation preview');
|
||||
}
|
||||
// IMPORTANT: capture the salt that was active when this heartbeat was sent.
|
||||
// The server computes new_hash = H(prev, ts, entropy, stack, OLD_salt) and stores it,
|
||||
// then rotates to next_salt. We must mirror that using the same old salt, then rotate.
|
||||
const sentSalt = currentSalt;
|
||||
currentSalt = resp.next_salt;
|
||||
prevHash = compute_next_hash(prevHash, timestamp, entropyJson, stackState, sentSalt);
|
||||
pendingMutationStep = resp.next_mutation_step;
|
||||
pendingMutationOrderB64 = resp.next_mutation_order_b64;
|
||||
} else {
|
||||
discard_gene_preview();
|
||||
console.warn('Heartbeat rejected');
|
||||
}
|
||||
} catch (e) {
|
||||
discard_gene_preview();
|
||||
console.error(e);
|
||||
} finally {
|
||||
scheduleNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user