Skip to content

Commit

Permalink
chore: Delete old serialization methods
Browse files Browse the repository at this point in the history
Cleans up several toJSON and fromJSON methods that are no longer needed
since we moved to schemas. Also drops hex serialization for most complex
entities, such as circuit inputs and outputs, and replaces it with
base64.
  • Loading branch information
spalladino committed Nov 13, 2024
1 parent cebcc3d commit d5d0773
Show file tree
Hide file tree
Showing 133 changed files with 908 additions and 1,155 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, L2Block, type TxEffect, type TxHash, TxReceipt } from '@aztec/circuit-types';
import { Body, L2Block, L2BlockHash, type TxEffect, type TxHash, TxReceipt } from '@aztec/circuit-types';
import { AppendOnlyTreeSnapshot, type AztecAddress, Header, INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { type AztecKVStore, type AztecMap, type AztecSingleton, type Range } from '@aztec/kv-store';
Expand Down Expand Up @@ -199,7 +199,7 @@ export class BlockStore {
TxReceipt.statusFromRevertCode(tx.revertCode),
'',
tx.transactionFee.toBigInt(),
block.data.hash().toBuffer(),
L2BlockHash.fromField(block.data.hash()),
block.data.number,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type GetUnencryptedLogsResponse,
type InboxLeaf,
type L2Block,
L2BlockHash,
type L2BlockL2Logs,
type LogFilter,
LogId,
Expand Down Expand Up @@ -353,7 +354,7 @@ export class MemoryArchiverStore implements ArchiverDataStore {
TxReceipt.statusFromRevertCode(txEffect.revertCode),
'',
txEffect.transactionFee.toBigInt(),
block.data.hash().toBuffer(),
L2BlockHash.fromField(block.data.hash()),
block.data.number,
),
);
Expand Down
12 changes: 10 additions & 2 deletions yarn-project/archiver/src/test/mock_l2_block_source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { L2Block, type L2BlockSource, type L2Tips, type TxHash, TxReceipt, TxStatus } from '@aztec/circuit-types';
import {
L2Block,
L2BlockHash,
type L2BlockSource,
type L2Tips,
type TxHash,
TxReceipt,
TxStatus,
} from '@aztec/circuit-types';
import { EthAddress, type Header } from '@aztec/circuits.js';
import { DefaultL1ContractsConfig } from '@aztec/ethereum';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -138,7 +146,7 @@ export class MockL2BlockSource implements L2BlockSource {
TxStatus.SUCCESS,
'',
txEffect.transactionFee.toBigInt(),
block.hash().toBuffer(),
L2BlockHash.fromField(block.hash()),
block.number,
),
);
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/aztec-node/src/aztec-node/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ describe('aztec node', () => {
maxBlockNumber: new MaxBlockNumber(true, new Fr(1)),
getSize: () => 1,
toBuffer: () => Fr.ZERO.toBuffer(),
toString: () => Fr.ZERO.toString(),
};

validMaxBlockNumberMetadata.data.rollupValidationRequests = {
maxBlockNumber: new MaxBlockNumber(true, new Fr(5)),
getSize: () => 1,
toBuffer: () => Fr.ZERO.toBuffer(),
toString: () => Fr.ZERO.toString(),
};

lastBlockNumber = 3;
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/bot/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class BotFactory {
this.log.info(`Initializing account at ${account.getAddress().toString()}`);
const sentTx = account.deploy();
const txHash = await sentTx.getTxHash();
this.log.info(`Sent tx with hash ${txHash.to0xString()}`);
this.log.info(`Sent tx with hash ${txHash.toString()}`);
if (this.config.flushSetupTransactions) {
this.log.verbose('Flushing transactions');
await this.node!.flushTxs();
Expand Down Expand Up @@ -117,7 +117,7 @@ export class BotFactory {
this.log.info(`Deploying token contract at ${address.toString()}`);
const sentTx = deploy.send(deployOpts);
const txHash = await sentTx.getTxHash();
this.log.info(`Sent tx with hash ${txHash.to0xString()}`);
this.log.info(`Sent tx with hash ${txHash.toString()}`);
if (this.config.flushSetupTransactions) {
this.log.verbose('Flushing transactions');
await this.node!.flushTxs();
Expand Down Expand Up @@ -164,7 +164,7 @@ export class BotFactory {
}
const sentTx = new BatchCall(token.wallet, calls).send();
const txHash = await sentTx.getTxHash();
this.log.info(`Sent tx with hash ${txHash.to0xString()}`);
this.log.info(`Sent tx with hash ${txHash.toString()}`);
if (this.config.flushSetupTransactions) {
this.log.verbose('Flushing transactions');
await this.node!.flushTxs();
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/circuit-types/src/auth_witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';
import { hexSchemaFor } from '@aztec/foundation/schemas';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';

/**
* An authentication witness. Used to authorize an action by a user.
Expand Down Expand Up @@ -37,12 +38,11 @@ export class AuthWitness {
}

toString() {
return '0x' + this.toBuffer().toString('hex');
return bufferToHex(this.toBuffer());
}

static fromString(str: string) {
const hex = str.replace(/^0x/, '');
return AuthWitness.fromBuffer(Buffer.from(hex, 'hex'));
return AuthWitness.fromBuffer(hexToBuffer(str));
}

static random() {
Expand Down
7 changes: 2 additions & 5 deletions yarn-project/circuit-types/src/body.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type ZodFor } from '@aztec/foundation/schemas';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { computeUnbalancedMerkleRoot } from '@aztec/foundation/trees';

Expand All @@ -16,18 +17,14 @@ export class Body {
});
}

static get schema() {
static get schema(): ZodFor<Body> {
return z
.object({
txEffects: z.array(TxEffect.schema),
})
.transform(({ txEffects }) => new Body(txEffects));
}

toJSON() {
return { txEffects: this.txEffects };
}

/**
* Serializes a block body
* @returns A serialized L2 block body.
Expand Down
16 changes: 0 additions & 16 deletions yarn-project/circuit-types/src/interfaces/nullifier_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,4 @@ export class NullifierMembershipWitness {
public toFields(): Fr[] {
return [new Fr(this.index), ...this.leafPreimage.toFields(), ...this.siblingPath.toFields()];
}

public toJSON() {
return {
index: '0x' + this.index.toString(16),
leafPreimage: this.leafPreimage.toJSON(),
siblingPath: this.siblingPath.toString(),
};
}

static fromJSON(json: any): NullifierMembershipWitness {
return new NullifierMembershipWitness(
BigInt(json.index),
NullifierLeafPreimage.fromJSON(json.leafPreimage),
SiblingPath.fromString<typeof NULLIFIER_TREE_HEIGHT>(json.siblingPath),
);
}
}
15 changes: 4 additions & 11 deletions yarn-project/circuit-types/src/l2_block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AppendOnlyTreeSnapshot, Header, STRING_ENCODING } from '@aztec/circuits.js';
import { AppendOnlyTreeSnapshot, Header } from '@aztec/circuits.js';
import { sha256, sha256ToField } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';

import { z } from 'zod';

Expand Down Expand Up @@ -31,14 +32,6 @@ export class L2Block {
.transform(({ archive, header, body }) => new L2Block(archive, header, body));
}

toJSON() {
return {
archive: this.archive,
header: this.header,
body: this.body,
};
}

/**
* Deserializes a block from a buffer
* @returns A deserialized L2 block.
Expand Down Expand Up @@ -66,15 +59,15 @@ export class L2Block {
* @returns Deserialized L2 block.
*/
static fromString(str: string): L2Block {
return L2Block.fromBuffer(Buffer.from(str, STRING_ENCODING));
return L2Block.fromBuffer(hexToBuffer(str));
}

/**
* Serializes a block to a string.
* @returns A serialized L2 block as a string.
*/
toString(): string {
return this.toBuffer().toString(STRING_ENCODING);
return bufferToHex(this.toBuffer());
}

/**
Expand Down
15 changes: 1 addition & 14 deletions yarn-project/circuit-types/src/logs/encrypted_l2_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,10 @@ export class EncryptedL2Log {

static get schema() {
return z
.object({ data: schemas.BufferHex, maskedContractAddress: schemas.Fr })
.object({ data: schemas.Buffer, maskedContractAddress: schemas.Fr })
.transform(({ data, maskedContractAddress }) => new EncryptedL2Log(data, maskedContractAddress));
}

/** Returns a JSON-friendly representation of the log. */
public toJSON(): object {
return {
data: this.data.toString('hex'),
maskedContractAddress: this.maskedContractAddress.toString(),
};
}

/** Converts a plain JSON object into an instance. */
public static fromJSON(obj: any) {
return new EncryptedL2Log(Buffer.from(obj.data, 'hex'), Fr.fromString(obj.maskedContractAddress));
}

/**
* Deserializes log from a buffer.
* @param buffer - The buffer containing the log.
Expand Down
14 changes: 1 addition & 13 deletions yarn-project/circuit-types/src/logs/encrypted_l2_note_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ export class EncryptedL2NoteLog {
return this.data;
}

/** Returns a JSON-friendly representation of the log. */
public toJSON(): object {
return { data: this.data.toString('hex') };
}

static get schema() {
return z
.object({ data: schemas.HexString })
.transform(({ data }) => new EncryptedL2NoteLog(Buffer.from(data, 'hex')));
}

/** Converts a plain JSON object into an instance. */
public static fromJSON(obj: any) {
return new EncryptedL2NoteLog(Buffer.from(obj.data, 'hex'));
return z.object({ data: schemas.Buffer }).transform(({ data }) => new EncryptedL2NoteLog(data));
}

/**
Expand Down
24 changes: 0 additions & 24 deletions yarn-project/circuit-types/src/logs/event_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,13 @@ export class EventMetadata<T> {
};
}

/**
* Serializes the metadata to a JSON-friendly format
*/
public toJSON() {
return {
type: 'event_metadata', // TODO(palla/schemas): Remove this type property
eventSelector: this.eventSelector,
abiType: this.abiType,
fieldNames: this.fieldNames,
};
}

static get schema() {
return z
.object({
eventSelector: schemas.EventSelector,
abiType: AbiTypeSchema,
fieldNames: z.array(z.string()),
type: z.literal('event_metadata').optional(),
})
.transform(obj => new EventMetadata(obj));
}

/**
* Creates an EventMetadata instance from a JSON representation
*/
public static fromJSON(json: any): EventMetadata<any> {
return new EventMetadata({
eventSelector: EventSelector.fromString(json.eventSelector),
abiType: json.abiType,
fieldNames: json.fieldNames,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader } from '@aztec/foundation/serialize';
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
import { type FieldsOf } from '@aztec/foundation/types';

import isEqual from 'lodash.isequal';
Expand All @@ -22,10 +23,6 @@ export class ExtendedUnencryptedL2Log {
return new ExtendedUnencryptedL2Log(LogId.random(), UnencryptedL2Log.random());
}

toJSON() {
return { id: this.id, log: this.log };
}

static get schema() {
return z
.object({
Expand All @@ -52,7 +49,7 @@ export class ExtendedUnencryptedL2Log {
* @returns A string containing the serialized log.
*/
public toString(): string {
return this.toBuffer().toString('hex');
return bufferToHex(this.toBuffer());
}

/**
Expand Down Expand Up @@ -92,7 +89,6 @@ export class ExtendedUnencryptedL2Log {
* @returns An `ExtendedUnencryptedL2Log` object.
*/
public static fromString(data: string): ExtendedUnencryptedL2Log {
const buffer = Buffer.from(data, 'hex');
return ExtendedUnencryptedL2Log.fromBuffer(buffer);
return ExtendedUnencryptedL2Log.fromBuffer(hexToBuffer(data));
}
}
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/logs/function_l2_logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function shouldBehaveLikeFunctionL2Logs(
it('can encode L2Logs to JSON and back', () => {
const l2Logs = FunctionL2Logs.random(3);

const buffer = JSON.stringify(l2Logs.toJSON());
const recovered = FunctionL2Logs.fromJSON(JSON.parse(buffer));
const buffer = JSON.stringify(l2Logs);
const recovered = FunctionL2Logs.schema.parse(JSON.parse(buffer));

expect(recovered).toEqual(l2Logs);
});
Expand Down
Loading

0 comments on commit d5d0773

Please sign in to comment.