Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions oracle/explorer-merkle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
43 changes: 43 additions & 0 deletions oracle/site-pages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
});
10 changes: 5 additions & 5 deletions site/agents.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h1 class="u-h1-28">Agents</h1>
<h2>The lobby</h2>
<div class="ag-lobby" id="lobbyRoom"></div>
<div class="ag-pick" id="lobbyPick"><span class="netnote">Click an agent to see who it is.</span></div>
<p class="netnote">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.</p>
<p class="netnote">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.</p>
</div>

<div class="app-card" id="agentsCard">
Expand Down Expand Up @@ -86,9 +86,9 @@ <h2>Evidence check</h2>
</footer>

<script src="assets/did.js?v=32000f8"></script>
<script src="assets/explorer.js?v=scores1"></script>
<script src="assets/lobby.js?v=lobby3"></script>
<script src="assets/agents.js?v=lobby3"></script>
<script type="module" src="assets/lobby-scene.js?v=lobby3"></script>
<script src="assets/explorer.js?v=memo1"></script>
<script src="assets/lobby.js?v=lobby4"></script>
<script src="assets/agents.js?v=lobby4"></script>
<script type="module" src="assets/lobby-scene.js?v=lobby4"></script>
</body>
</html>
109 changes: 79 additions & 30 deletions site/assets/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function setCard(id, html) {
const plural = (n, one, many = one + "s") => `${n} ${n === 1 ? one : many}`;

// ---- the list ----
// Registering costs only gas, so however many agents there are, the table shows this many at a
// time and the chain is read only for the agents on screen, in the table or in the lobby.
const PAGE_ROWS = 50;
// The most agents the lobby draws (agents.html says 60 in its note).
const LOBBY_MAX = 60;
let agents = [];

async function loadAgents() {
Expand All @@ -32,36 +37,49 @@ async function loadAgents() {
+ `<p class="netnote">You can look an agent up by address on <a href="epochs">the epochs page</a>, which reads the chain directly.</p>`);
}
const seen = new Set();
// In the order they registered, as the logs come. Until an agent is read from the chain, its
// operator is the one that registered it, which is what puts it in a group for the filter.
agents = registrations.logs
.map((l) => ({ didHash: l.topics[1], address: X.topicAddr(l.topics[3]), time: l.time, tx: l.tx }))
.map((l) => ({ didHash: l.topics[1], operator: X.topicAddr(l.topics[2]), address: X.topicAddr(l.topics[3]), time: l.time, tx: l.tx }))
.filter((a) => X.isHash(a.didHash) && X.isAddr(a.address) && !seen.has(a.didHash) && seen.add(a.didHash));
if (!agents.length) return setCard("agentsCard", "<p>No agent has registered on this deployment yet.</p>");
lastHistory = registrations;

// The lobby's agents, which take in the table's first page.
try {
const reads = await X.ethCalls(agents.flatMap((a) => [
{ to: X.IDENTITY, data: X.SEL.identities + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.getTotalScore + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.getEarnedScore + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.evidenceRoots + X.pad32(a.didHash) },
{ to: X.STAKING, data: X.SEL.getStake + X.pad32(a.didHash) },
]));
agents.forEach((a, i) => {
const [ident, total, earned, root, stake] = reads.slice(i * 5, i * 5 + 5);
// The operator now, which a transfer can change since registration.
a.operator = X.wordAddr(ident, 0);
a.status = Number(BigInt(X.word(ident, 3)));
a.total = Number(BigInt(total));
a.earned = Number(BigInt(earned));
a.root = X.word(root, 0).toLowerCase();
a.stake = BigInt(stake);
});
await readChain(agents.slice(0, LOBBY_MAX));
} catch (e) {
return setCard("agentsCard", X.failed("the agents' scores from the chain", e));
}
renderAgents(registrations);
await renderAgents();
renderLobby();
}

// What the chain says now about each agent in `list` not read yet: its operator and status, its
// two scores, its evidence root and its stake.
async function readChain(list) {
const todo = list.filter((a) => !a.read);
if (!todo.length) return;
const reads = await X.ethCalls(todo.flatMap((a) => [
{ to: X.IDENTITY, data: X.SEL.identities + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.getTotalScore + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.getEarnedScore + X.pad32(a.didHash) },
{ to: X.REPUTATION, data: X.SEL.evidenceRoots + X.pad32(a.didHash) },
{ to: X.STAKING, data: X.SEL.getStake + X.pad32(a.didHash) },
]));
todo.forEach((a, i) => {
const [ident, total, earned, root, stake] = reads.slice(i * 5, i * 5 + 5);
// The operator now, which a transfer can change since registration.
a.operator = X.wordAddr(ident, 0);
a.status = Number(BigInt(X.word(ident, 3)));
a.total = Number(BigInt(total));
a.earned = Number(BigInt(earned));
a.root = X.word(root, 0).toLowerCase();
a.stake = BigInt(stake);
a.read = true;
});
}

// Which group an agent is in, for the badge and the filter.
function groupOf(a) {
const profile = X.simAgent(a.address);
Expand All @@ -85,27 +103,42 @@ function characterOf(a) {
};
}
let filter = "all";
let page = 0;
let lastHistory = null;
let tableSeq = 0;

// The committed root, linked to the payments it is over as the primary oracle serves them, which
// "Check evidence" rebuilds it from.
const rootLink = (a) => (X.same(a.root, X.ZERO32) ? X.rootCode(a.root)
: `<a href="${X.PRIMARY}/evidence/${a.didHash}" target="_blank" rel="noopener" title="${X.esc(a.root)}: the payments behind it, from the primary oracle"><code>${X.esc(X.short(a.root))}</code></a>`);

function renderAgents(history = lastHistory) {
lastHistory = history;
const sorted = [...agents].sort((a, b) => b.total - a.total || b.earned - a.earned || a.time - b.time);
// The table, a page at a time in the order the agents registered: an order by score would need
// every agent's score read first.
async function renderAgents() {
const seq = ++tableSeq;
const listed = agents.filter((a) => filter === "all" || groupOf(a).key === filter);
const pages = Math.max(1, Math.ceil(listed.length / PAGE_ROWS));
page = Math.min(page, pages - 1);
const shown = listed.slice(page * PAGE_ROWS, (page + 1) * PAGE_ROWS);
try {
await readChain(shown);
} catch (e) {
if (seq === tableSeq) setCard("agentsCard", X.failed("the agents' scores from the chain", e));
return;
}
if (seq !== tableSeq) return;
const history = lastHistory;
const count = (key) => agents.filter((a) => groupOf(a).key === key).length;
const n = { all: agents.length, outside: count("outside"), sim: count("sim"), own: count("own") };
const active = agents.filter((a) => a.status === 0).length;
let html = `<p><b>${plural(agents.length, "agent")}</b> registered on this deployment, ${active} active.`
// A count of the active agents needs every agent's status, and only the agents shown are read.
const active = agents.every((a) => a.read) ? `, ${agents.filter((a) => a.status === 0).length} active` : "";
let html = `<p><b>${plural(agents.length, "agent")}</b> registered on this deployment${active}.`
+ (n.sim ? ` <b>${n.sim}</b> of them are Sigvara's <a href="simulation">testnet simulation</a>, which runs a simulated economy so the protocol can be watched working.` : "")
+ ` Both oracle operators are run by the Sigvara team (see <a href="operators">Operators</a>), so read these scores as the protocol working, not as outside demand.</p>`;
if (X.simUnknown()) html += '<p class="pill-warn">The list of the testnet simulation\'s agents could not be read, so this page cannot tell its agents from outside ones. Reload to try again.</p>';
const choices = [["all", "All"], ["outside", "Outside"], ["sim", "Simulated"], ["own", "Sigvara's own"]].filter(([k]) => k === "all" || n[k]);
html += `<div class="ex-filter" role="group" aria-label="Show agents">${choices.map(([k, label]) =>
`<button type="button" data-filter="${k}" aria-pressed="${filter === k}">${X.esc(label)} (${n[k]})</button>`).join("")}</div>`;
const shown = sorted.filter((a) => filter === "all" || groupOf(a).key === filter);
html += `<div class="ex-scroll"><table class="ex-table">
<thead><tr><th>Agent</th><th>Who</th><th>Status</th><th title="getTotalScore: the score other contracts read, which matures toward the earned score">Matured score</th><th title="getEarnedScore: the score as last finalized">Earned</th><th>Stake</th><th>Evidence root</th><th></th></tr></thead><tbody>`;
for (const a of shown) {
Expand All @@ -123,22 +156,38 @@ function renderAgents(history = lastHistory) {
</tr>`;
}
html += "</tbody></table></div>";
html += '<p class="netnote">The matured score is the total other contracts read (<code>getTotalScore</code>). It climbs toward the earned score, the one last finalized (<code>getEarnedScore</code>), at a bounded rate a day, and falls at once. <a href="docs/reputation">How scores work</a>. The agent links open its score epochs, and each evidence root links to the payments it commits to.</p>';
if (pages > 1) {
html += `<p>Rows ${page * PAGE_ROWS + 1} to ${page * PAGE_ROWS + shown.length} of ${listed.length}.`
+ ` <button class="btn btn-secondary btn-sm" type="button" data-page="-1"${page === 0 ? " disabled" : ""}>Previous</button>`
+ ` <button class="btn btn-secondary btn-sm" type="button" data-page="1"${page === pages - 1 ? " disabled" : ""}>Next</button></p>`;
}
html += `<p class="netnote">Agents are listed in the order they registered, ${PAGE_ROWS} to a page. The matured score is the total other contracts read (<code>getTotalScore</code>). It climbs toward the earned score, the one last finalized (<code>getEarnedScore</code>), at a bounded rate a day, and falls at once. <a href="docs/reputation">How scores work</a>. The agent links open its score epochs, and each evidence root links to the payments it commits to.</p>`;
if (history?.truncated) html += '<p class="pill-warn">The explorer has more registrations than this page reads, so the newest may be missing.</p>';
html += X.historyNote(history);
setCard("agentsCard", html);
for (const btn of document.querySelectorAll("[data-check]")) {
btn.addEventListener("click", () => checkEvidence(btn.getAttribute("data-check")));
}
for (const btn of document.querySelectorAll("[data-filter]")) {
btn.addEventListener("click", () => { filter = btn.getAttribute("data-filter"); renderAgents(); });
btn.addEventListener("click", () => { filter = btn.getAttribute("data-filter"); page = 0; renderAgents(); });
}
for (const btn of document.querySelectorAll("[data-page]")) {
btn.addEventListener("click", async () => {
btn.disabled = true;
page += Number(btn.getAttribute("data-page"));
await renderAgents();
// The buttons sit under the rows, so the new page is shown from its top.
$("agentsCard").scrollIntoView({ block: "start" });
});
}
}

// ---- the lobby ----
// The first LOBBY_MAX agents to register. They are the table's first rows, read with them, and a
// wave of new registrations cannot crowd them out of the room.
function renderLobby() {
$("lobbyCard").classList.remove("u-hidden");
const people = agents.map((a) => ({ ...characterOf(a), agent: a }));
const people = agents.slice(0, LOBBY_MAX).map((a) => ({ ...characterOf(a), agent: a }));
const onPick = (person) => showInLobby(person.agent);
// The room with depth (lobby-scene.js) is a module and may not have loaded yet, and needs WebGL.
// Until it is here, and wherever it cannot run, the flat room stands in.
Expand Down Expand Up @@ -225,7 +274,7 @@ async function renderCheck(a, evidence, pendingRead) {
} else if (X.same(a.root, X.ZERO32)) {
finalHtml = "none yet: no score with an evidence root has been finalized for this agent";
} else {
const m = X.matchRoot(list, a.root, { delays });
const m = X.matchRoot(list, a.root, { delays, leaves: rebuilt });
if (!m) {
finalHtml = `${X.rootCode(a.root)} <span class="pill-err">no reading of these payments reproduces it</span>. The oracle no longer lists the set this score was made on, or lists it differently.`;
} else {
Expand All @@ -239,7 +288,7 @@ async function renderCheck(a, evidence, pendingRead) {
if (BigInt(X.word(raw, 8)) === 1n) {
const proposedAt = Number(BigInt(X.word(raw, 7)));
const root = X.word(raw, 9).toLowerCase();
const m = bad.length ? null : X.matchRoot(list, root, { at: proposedAt, delays, evidenceCut });
const m = bad.length ? null : X.matchRoot(list, root, { at: proposedAt, delays, evidenceCut, leaves: rebuilt });
pendingHtml = `${X.rootCode(root)}, proposed ${X.esc(X.fmtWhen(proposedAt))}: ` + (m
? `<span class="pill-ok">reproduced</span> from ${describeMatch(m, list.length)}`
: '<span class="pill-err">not reproduced</span> from the payments settled by then');
Expand Down
Loading
Loading