import { MAX_NOTE_HASH_READ_REQUESTS_PER_TX, NOTE_HASH_TREE_HEIGHT } from '@aztec/constants'; import { makeTuple } from '@aztec/foundation/array'; import { Fr } from '@aztec/foundation/curves/bn254'; import type { BufferReader } from '@aztec/foundation/serialize'; import type { MembershipWitness } from '@aztec/foundation/trees'; import { PendingReadHint, ReadRequestAction, ReadRequestResetHints, SettledReadHint } from './read_request_hints.js'; type NoteHashLeafValue = Fr; export type NoteHashReadRequestHints = ReadRequestResetHints< typeof MAX_NOTE_HASH_READ_REQUESTS_PER_TX, PENDING, SETTLED, typeof NOTE_HASH_TREE_HEIGHT, NoteHashLeafValue >; export function noteHashReadRequestHintsFromBuffer( buffer: Buffer | BufferReader, numPending: PENDING, numSettled: SETTLED, ): NoteHashReadRequestHints { return ReadRequestResetHints.fromBuffer( buffer, MAX_NOTE_HASH_READ_REQUESTS_PER_TX, numPending, numSettled, NOTE_HASH_TREE_HEIGHT, Fr, ); } export class NoteHashReadRequestHintsBuilder { private hints: NoteHashReadRequestHints; public numPendingReadHints = 0; public numSettledReadHints = 0; constructor( public readonly maxPending: PENDING, public readonly maxSettled: SETTLED, ) { this.hints = new ReadRequestResetHints( makeTuple(MAX_NOTE_HASH_READ_REQUESTS_PER_TX, ReadRequestAction.skip), makeTuple(maxPending, () => PendingReadHint.nada(MAX_NOTE_HASH_READ_REQUESTS_PER_TX)), makeTuple(maxSettled, () => SettledReadHint.nada(MAX_NOTE_HASH_READ_REQUESTS_PER_TX, NOTE_HASH_TREE_HEIGHT, Fr.zero), ), ); } static empty(maxPending: PENDING, maxSettled: SETTLED) { return new NoteHashReadRequestHintsBuilder(maxPending, maxSettled).toHints(); } addPendingReadRequest(readRequestIndex: number, noteHashIndex: number) { if (this.numPendingReadHints === this.maxPending) { throw new Error('Cannot add more pending read request.'); } this.hints.readRequestActions[readRequestIndex] = ReadRequestAction.readAsPending(this.numPendingReadHints); this.hints.pendingReadHints[this.numPendingReadHints] = new PendingReadHint(readRequestIndex, noteHashIndex); this.numPendingReadHints++; } addSettledReadRequest( readRequestIndex: number, membershipWitness: MembershipWitness, value: NoteHashLeafValue, ) { if (this.numSettledReadHints === this.maxSettled) { throw new Error('Cannot add more settled read request.'); } this.hints.readRequestActions[readRequestIndex] = ReadRequestAction.readAsSettled(this.numSettledReadHints); this.hints.settledReadHints[this.numSettledReadHints] = new SettledReadHint( readRequestIndex, membershipWitness, value, ); this.numSettledReadHints++; } toHints() { return this.hints; } }