import { MAX_CONTRACT_CLASS_LOGS_PER_TX, MAX_ENQUEUED_CALLS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_PRIVATE_LOGS_PER_TX, PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH, } from '@aztec/constants'; import { type FieldsOf, makeTuple } from '@aztec/foundation/array'; import { arraySerializedSizeOfNonEmpty } from '@aztec/foundation/collection'; import { Fr } from '@aztec/foundation/curves/bn254'; import { BufferReader, FieldReader, type Tuple, serializeToBuffer, serializeToFields, } from '@aztec/foundation/serialize'; import { inspect } from 'util'; import { PrivateLog } from '../logs/private_log.js'; import { ScopedL2ToL1Message } from '../messaging/l2_to_l1_message.js'; import { ScopedLogHash } from './log_hash.js'; import { PublicCallRequest } from './public_call_request.js'; export class PrivateToPublicAccumulatedData { constructor( public readonly noteHashes: Tuple, public readonly nullifiers: Tuple, public readonly l2ToL1Msgs: Tuple, public readonly privateLogs: Tuple, public readonly contractClassLogsHashes: Tuple, public readonly publicCallRequests: Tuple, ) {} getSize() { return ( arraySerializedSizeOfNonEmpty(this.noteHashes) + arraySerializedSizeOfNonEmpty(this.nullifiers) + arraySerializedSizeOfNonEmpty(this.l2ToL1Msgs) + arraySerializedSizeOfNonEmpty(this.privateLogs) + arraySerializedSizeOfNonEmpty(this.contractClassLogsHashes) + arraySerializedSizeOfNonEmpty(this.publicCallRequests) ); } static getFields(fields: FieldsOf) { return [ fields.noteHashes, fields.nullifiers, fields.l2ToL1Msgs, fields.privateLogs, fields.contractClassLogsHashes, fields.publicCallRequests, ] as const; } static fromFields(fields: Fr[] | FieldReader) { const reader = FieldReader.asReader(fields); return new this( reader.readFieldArray(MAX_NOTE_HASHES_PER_TX), reader.readFieldArray(MAX_NULLIFIERS_PER_TX), reader.readTuple(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message), reader.readTuple(MAX_PRIVATE_LOGS_PER_TX, PrivateLog), reader.readTuple(MAX_CONTRACT_CLASS_LOGS_PER_TX, ScopedLogHash), reader.readTuple(MAX_ENQUEUED_CALLS_PER_TX, PublicCallRequest), ); } static from(fields: FieldsOf) { return new PrivateToPublicAccumulatedData(...PrivateToPublicAccumulatedData.getFields(fields)); } static fromBuffer(buffer: Buffer | BufferReader) { const reader = BufferReader.asReader(buffer); return new PrivateToPublicAccumulatedData( reader.readTuple(MAX_NOTE_HASHES_PER_TX, Fr), reader.readTuple(MAX_NULLIFIERS_PER_TX, Fr), reader.readTuple(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message), reader.readTuple(MAX_PRIVATE_LOGS_PER_TX, PrivateLog), reader.readTuple(MAX_CONTRACT_CLASS_LOGS_PER_TX, ScopedLogHash), reader.readTuple(MAX_ENQUEUED_CALLS_PER_TX, PublicCallRequest), ); } toBuffer() { return serializeToBuffer(...PrivateToPublicAccumulatedData.getFields(this)); } toFields(): Fr[] { const fields = serializeToFields(...PrivateToPublicAccumulatedData.getFields(this)); if (fields.length !== PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH) { throw new Error( `Invalid number of fields for PrivateToPublicAccumulatedData. Expected ${PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH}, got ${fields.length}`, ); } return fields; } static empty() { return new PrivateToPublicAccumulatedData( makeTuple(MAX_NOTE_HASHES_PER_TX, Fr.zero), makeTuple(MAX_NULLIFIERS_PER_TX, Fr.zero), makeTuple(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message.empty), makeTuple(MAX_PRIVATE_LOGS_PER_TX, PrivateLog.empty), makeTuple(MAX_CONTRACT_CLASS_LOGS_PER_TX, ScopedLogHash.empty), makeTuple(MAX_ENQUEUED_CALLS_PER_TX, PublicCallRequest.empty), ); } [inspect.custom]() { return `PrivateToPublicAccumulatedData { noteHashes: [${this.noteHashes .filter(x => !x.isZero()) .map(x => inspect(x)) .join(', ')}], nullifiers: [${this.nullifiers .filter(x => !x.isZero()) .map(x => inspect(x)) .join(', ')}], l2ToL1Msgs: [${this.l2ToL1Msgs .filter(x => !x.isEmpty()) .map(x => inspect(x)) .join(', ')}], privateLogs: [${this.privateLogs .filter(x => !x.isEmpty()) .map(h => inspect(h)) .join(', ')}], contractClassLogsHashes: [${this.contractClassLogsHashes .filter(x => !x.isEmpty()) .map(h => inspect(h)) .join(', ')}], publicCallRequests: [${this.publicCallRequests .filter(x => !x.isEmpty()) .map(h => inspect(h)) .join(', ')}], }`; } }