diff --git a/oracle/explorer-merkle.test.js b/oracle/explorer-merkle.test.js index 71eafdb..3805e54 100644 --- a/oracle/explorer-merkle.test.js +++ b/oracle/explorer-merkle.test.js @@ -11,6 +11,7 @@ const { ethers } = require('ethers'); const merkle = require('./merkle'); const { evidenceAsOf } = require('./evidence-as-of'); const explorer = require('../site/assets/explorer.js'); +const did = require('../site/assets/did.js'); const T0 = 1_700_000_000; @@ -114,6 +115,36 @@ test('matchRoot says so when no reading reproduces the root', () => { assert.equal(explorer.matchRoot(list, 'not a root'), null); }); +test('matchRoot hashes each pair once across its cut-offs, so a root 10 payments behind a long list is found quickly', (t) => { + // Built here rather than by stored(), whose random wallet is slow a thousand times over. + const events = Object.freeze(Array.from({ length: 1000 }, (_, i) => Object.freeze({ + txHash: ethers.hexlify(ethers.randomBytes(32)), + payer: ethers.hexlify(ethers.randomBytes(20)), + amount: String(10n ** 18n * BigInt(i + 1)), + ts: (T0 + i * 60) * 1000, + success: [true, false, null][i % 3], + }))); + const list = events.map(served); + const committed = merkle.rootFor(events.slice(0, 990)); + // The oracle's leaves, which the first test holds to the browser's, passed as agents.js passes the + // ones it has built, so what is counted below is the trees alone. + const leaves = events.map((e) => merkle.leafFor(e)); + const keccak = did.keccak256; + let pairs = 0; + // Two 32-byte nodes are the only thing matchRoot hashes from exactly 64 bytes. + t.mock.method(did, 'keccak256', (bytes) => { if (bytes.length === 64) pairs++; return keccak(bytes); }); + + const found = explorer.matchRoot(list, committed, { leaves }); + assert.ok(found, 'found by trying each settlement time'); + assert.equal(found.events.length, 990); + assert.equal(found.cut, list[989].settledAt); + assert.equal(found.unset, false); + assert.deepEqual(found.leaves, leaves.slice(0, 990)); + // The cut-offs run from all 1,000 payments down to 990. Built one by one, those trees take about + // 12,000 pairs; sharing them, one tree's 999 and the few each later cut-off changes. + assert.ok(pairs < 1300, `${pairs} pairs hashed`); +}); + test('verifyEvidence calldata is what the contract ABI encodes', () => { const iface = new ethers.Interface(['function verifyEvidence(bytes32 didHash, bytes32 leaf, bytes32[] proof)']); const did = ethers.hexlify(ethers.randomBytes(32)); diff --git a/oracle/site-pages.test.js b/oracle/site-pages.test.js index 5c2b734..ea6fdf6 100644 --- a/oracle/site-pages.test.js +++ b/oracle/site-pages.test.js @@ -9,6 +9,8 @@ const path = require('node:path'); const vm = require('node:vm'); const explorer = require('../site/assets/explorer.js'); const demo = require('../site/assets/demo.js'); +const did = require('../site/assets/did.js'); +const lobby = require('../site/assets/lobby.js'); test('the demo does not number blocks from the start again after the log is cleared', () => { const sim = new demo.Simulator(null); @@ -42,3 +44,44 @@ test('a Suspended or PendingBond simulated agent with stake is not shown as Acti assert.deepEqual([...ctx.statusOf(agent(3), 0)], ['waiting', 'Not bonded yet']); assert.deepEqual([...ctx.statusOf(agent(0), 0)], ['live', 'Active']); }); + +test('the agents page reads the chain only for the agents it shows, 50 rows at a time and 60 in the lobby', async () => { + const source = fs.readFileSync(path.join(__dirname, '..', 'site', 'assets', 'agents.js'), 'utf8'); + const word = (n) => n.toString(16).padStart(64, '0'); + // 130 registrations, oldest first, as contractLogs gives them. + const logs = Array.from({ length: 130 }, (_, i) => ({ + topics: [explorer.EVENTS[0].topic, '0x' + word(i + 1), '0x' + word(0xabc), '0x' + word(0x1000 + i)], time: 1_700_000_000 + i, tx: null, + })); + const asked = []; + const X = { + ...explorer, + contractLogs: async () => ({ logs, truncated: false, tail: null }), + loadSimulation: async () => {}, + // The didHash each read is for. Every answer is zero words: an active agent with nothing yet. + ethCalls: async (calls) => { asked.push(...calls.map((c) => c.data.slice(-64))); return calls.map(() => '0x' + word(0).repeat(5)); }, + }; + let html = '', people = null, next = null; + const card = { querySelector: () => ({ set innerHTML(v) { html = v; } }), classList: { remove() {} }, scrollIntoView() {} }; + const ctx = vm.createContext({ + window: { SigvaraExplorer: X, SigvaraDid: did, SigvaraLobby: { ...lobby, startLobby: (_host, list) => { people = list; } } }, + document: { + getElementById: () => card, + addEventListener() {}, + querySelectorAll: (sel) => (sel === '[data-page]' ? [{ getAttribute: () => '1', addEventListener: (_ev, fn) => { next = fn; } }] : []), + }, + }); + vm.runInContext(source, ctx); + for (let i = 0; i < 50 && !people; i++) await new Promise(setImmediate); + + const rows = () => (html.match(/data-check="/g) || []).length; + assert.equal(people.length, 60, 'the lobby holds 60'); + assert.deepEqual([...new Set(asked)], Array.from({ length: 60 }, (_, i) => word(i + 1)), 'the first 60 agents, read once each'); + assert.equal(asked.length, 60 * 5); + assert.equal(rows(), 50); + assert.match(html, /Rows 1 to 50 of 130\./); + + await next(); + assert.equal(rows(), 50); + assert.match(html, /Rows 51 to 100 of 130\./); + assert.deepEqual([...new Set(asked.slice(300))], Array.from({ length: 40 }, (_, i) => word(i + 61)), 'only the rows not read before'); +}); diff --git a/site/agents.html b/site/agents.html index 59a815f..077cbe9 100644 --- a/site/agents.html +++ b/site/agents.html @@ -57,7 +57,7 @@
Every agent on this deployment, for fun. A look and a nickname come only from the address, so neither says how far to trust an agent: the scores below are for that. Awake, asleep and ghost follow its status on chain: active, suspended and slashed. A dashed ring marks a simulated agent, and a prop says which kind: a lunchbox for steady, a cup for mediocre, smoke for failing, and a hoop and mask for the Sybil ring. A hard hat is Sigvara's slash drill, a flag its demo agent, and a suitcase an agent from outside. The table below lists the same agents.
+The agents on this deployment, up to the first 60 to register, for fun. A look and a nickname come only from the address, so neither says how far to trust an agent: the scores below are for that. Awake, asleep and ghost follow its status on chain: active, suspended and slashed. A dashed ring marks a simulated agent, and a prop says which kind: a lunchbox for steady, a cup for mediocre, smoke for failing, and a hoop and mask for the Sybil ring. A hard hat is Sigvara's slash drill, a flag its demo agent, and a suitcase an agent from outside. The table below lists every agent.