v3 : vote par la chaîne, corrections de la relecture de 74f96be (interface)

- rien n'est parti (nœud muet avant la signature, refus, fenêtre fermée avant) :
  la page lâche le bulletin ; signé, ou montré en code Ğecko, il est gardé et
  le serveur le confirme sans la page ; le refus de la chaîne se dit (« chain ») ;
- le reçu : téléchargeable depuis la fenêtre de vote, et la page « Tes
  bulletins » (/bulletins) qui garde l'historique et vérifie un reçu dans le
  navigateur, sans le serveur (lib/receipt.ts, mêmes vecteurs que le moteur) ;
- le panier ne dit plus « Tes votes comptent dès l'envoi » quand le vote est fermé ;
- @noble/hashes déclaré (utilisé par e2e/chain-mock.mjs et le reçu) ;
- chaîne simulée : finalité, pages, filtres, nœud qui tombe ; contrôles C32, C33.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yvv
2026-10-31 03:11:54 +01:00
co-authored by Claude Opus 5.5
parent e63e2ec283
commit 51c9a4771d
19 changed files with 846 additions and 48 deletions
+37 -8
View File
@@ -6,11 +6,15 @@
// each one is included in a new block, as the transfer and the remark of the
// real `utility.batchAll` would be;
// - `/v1/graphql`, the « indexer »: answers the server's query
// `txComments(condition: {hash})` in the shape of indexer.duniter.org.
// `squidStatus { height } txComments(condition: {hash}, filter, first, after)`
// in the shape of indexer.duniter.org: the last final block (here, every
// block is final), the comments in the order of the chain, filtered (author,
// or a transfer to an account of at least an amount) and served page by page.
//
// And `/control/*` for the tests: `hide` (what is included next stays unseen by
// the indexer, as an indexer late on the chain), `show` (stop hiding), `release`
// (the indexer catches up), `silent` (the indexer stops answering) and `state`.
// the indexer, as an indexer behind the chain), `show` (stop hiding), `release`
// (the indexer catches up), `silent` (the indexer stops answering), `node`
// (`?down=1`: the node stops answering, before anything is sent) and `state`.
//
// Listens on an ephemeral port on 127.0.0.1, written to the file given as argument.
@@ -27,7 +31,7 @@ const IDENTITIES = {
g1QBoGEqZuxL77pF4rMa25f1cH6HAxwgBq66zZu76JPqsaVgj: { index: 9005, name: 'Eve', isMember: true },
};
const state = { block: 3_400_000, comments: [], hiding: false, silent: false, submitted: 0 };
const state = { block: 3_400_000, comments: [], hiding: false, silent: false, nodeDown: false, submitted: 0 };
const hex = (bytes) => Buffer.from(bytes).toString('hex');
const remarkHash = (remark) => `0x${hex(blake2b(Buffer.from(remark, 'utf8'), { dkLen: 32 }))}`;
@@ -49,6 +53,20 @@ async function read(req) {
}
}
/** What the real indexer's `filter` keeps, for the two filters the server asks. */
function kept(c, filter) {
if (!filter) return true;
const author = filter.authorId?.equalTo;
if (author && c.author.id !== author) return false;
const some = filter.transfersByCommentId?.some;
if (!some) return true;
const min = BigInt(some.amount?.greaterThanOrEqualTo ?? '0');
return c.transfersByCommentId.nodes.some((t) => (!some.toId?.equalTo || t.toId === some.toId.equalTo) && BigInt(t.amount) >= min);
}
/** The size of a page: `first: N` in the query, as the server writes it. */
const pageSize = (query) => Number(/first:\s*(\d+)/.exec(query)?.[1] ?? 100);
/** One signed extrinsic of the test mode: a new block, its transfer and its remark. */
function include(fields) {
state.block += 1;
@@ -70,7 +88,9 @@ const server = createServer(async (req, res) => {
if (req.method === 'OPTIONS') return send(res, 204, undefined, true);
if (url.pathname === '/rpc' && req.method === 'POST') {
if (state.nodeDown) return send(res, 503, { error: 'unavailable' }, true);
const body = await read(req);
if (body.method === 'system_health') return send(res, 200, { jsonrpc: '2.0', id: body.id, result: { peers: 8, isSyncing: false, shouldHavePeers: true } }, true);
if (body.method !== 'author_submitExtrinsic') return send(res, 200, { jsonrpc: '2.0', id: body.id, error: { code: -32601, message: 'Method not found' } }, true);
let fields;
try {
@@ -90,11 +110,19 @@ const server = createServer(async (req, res) => {
const hash = body.variables?.hash;
// Only the query of the ballots; the others (size of the web of trust) get nothing, as from a dead indexer.
if (state.silent || !hash || !String(body.query ?? '').includes('txComments')) return send(res, 502, { error: 'unavailable' });
const nodes = state.comments
.filter((c) => !c.hidden && remarkHash(c.remark) === hash)
.sort((a, b) => a.blockNumber - b.blockNumber)
const all = state.comments
.filter((c) => !c.hidden && remarkHash(c.remark) === hash && kept(c, body.variables?.filter))
.sort((a, b) => a.blockNumber - b.blockNumber || a.id.localeCompare(b.id))
.map(({ hidden: _, ...c }) => c);
return send(res, 200, { data: { txComments: { nodes } } });
const start = Number(/^at:(\d+)$/.exec(body.variables?.after ?? '')?.[1] ?? 0);
const nodes = all.slice(start, start + pageSize(String(body.query)));
const end = start + nodes.length;
return send(res, 200, {
data: {
squidStatus: { height: state.block },
txComments: { pageInfo: { hasNextPage: end < all.length, endCursor: `at:${end}` }, nodes },
},
});
}
if (url.pathname.startsWith('/control/') && req.method === 'POST') {
@@ -105,6 +133,7 @@ const server = createServer(async (req, res) => {
state.hiding = false;
for (const c of state.comments) c.hidden = false;
} else if (what === 'silent') state.silent = url.searchParams.get('on') !== '0';
else if (what === 'node') state.nodeDown = url.searchParams.get('down') === '1';
else if (what !== 'state') return send(res, 404, { error: 'unknown' }, true);
return send(res, 200, { block: state.block, submitted: state.submitted, hidden: state.comments.filter((c) => c.hidden).length }, true);
}