Skip to content

Commit

Permalink
feat: use number instead of string for appropriate fields in DB (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipHarald authored Oct 7, 2024
1 parent 82a3088 commit 9c27133
Show file tree
Hide file tree
Showing 27 changed files with 166 additions and 1,236 deletions.
1 change: 1 addition & 0 deletions packages/types/src/aztec/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./l2Block.js";
export * from "./l2Contract.js";
export * from "./l2TxEffect.js";
export { frNumberSchema } from "./utils.js";

export { type NodeInfo } from "@aztec/aztec.js";
31 changes: 19 additions & 12 deletions packages/types/src/aztec/l2Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { z } from "zod";
import { hexStringSchema } from "../general.js";
import { deepPartial } from "../utils.js";
import { chicmozL2TxEffectSchema } from "./l2TxEffect.js";
import { bufferSchema, frSchema } from "./utils.js";
import {
aztecAddressSchema,
bufferSchema,
ethAddressSchema,
frNumberSchema,
frSchema,
frTimestampSchema,
} from "./utils.js";

export const chicmozL2BlockSchema = z.object({
hash: hexStringSchema,
Expand All @@ -17,7 +24,7 @@ export const chicmozL2BlockSchema = z.object({
nextAvailableLeafIndex: z.number(),
}),
contentCommitment: z.object({
numTxs: frSchema,
numTxs: frNumberSchema,
txsEffectsHash: bufferSchema,
inHash: bufferSchema,
outHash: bufferSchema,
Expand All @@ -43,19 +50,19 @@ export const chicmozL2BlockSchema = z.object({
}),
}),
globalVariables: z.object({
chainId: z.string(),
version: z.string(),
blockNumber: frSchema,
slotNumber: frSchema,
timestamp: frSchema,
coinbase: z.string(),
feeRecipient: z.string(),
chainId: frNumberSchema,
version: frNumberSchema,
blockNumber: frNumberSchema,
slotNumber: frNumberSchema,
timestamp: frTimestampSchema,
coinbase: ethAddressSchema,
feeRecipient: aztecAddressSchema,
gasFees: z.object({
feePerDaGas: frSchema,
feePerL2Gas: frSchema,
feePerDaGas: frNumberSchema,
feePerL2Gas: frNumberSchema,
}),
}),
totalFees: frSchema,
totalFees: frNumberSchema,
}),
body: z.object({
txEffects: z.array(chicmozL2TxEffectSchema),
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/aztec/l2Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type ChicmozL2ContractClassRegisteredEvent = z.infer<
export const chicmozL2ContractInstanceDeluxeSchema = z.object({
...chicmozL2ContractInstanceDeployedEventSchema.shape,
...chicmozL2ContractClassRegisteredEventSchema.shape,
blockHeight: z.number().optional(),
blockHeight: chicmozL2BlockSchema.shape.height.optional(),
});

export type ChicmozL2ContractInstanceDeluxe = z.infer<
Expand Down
12 changes: 6 additions & 6 deletions packages/types/src/aztec/l2TxEffect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import { hexStringSchema } from "../general.js";
import { frSchema } from "./utils.js";
import { aztecAddressSchema, frNumberSchema, frSchema } from "./utils.js";

export const noteEncryptedLogEntrySchema = z.object({
data: z.string(),
Expand All @@ -13,7 +13,7 @@ export const encryptedLogEntrySchema = z.object({

export const unencryptedLogEntrySchema = z.object({
data: z.string(),
contractAddress: z.string(),
contractAddress: aztecAddressSchema,
});

export const chicmozL2TxEffectSchema = z.object({
Expand All @@ -25,16 +25,16 @@ export const chicmozL2TxEffectSchema = z.object({
z.object({ code: z.number() }),
),
hash: hexStringSchema,
transactionFee: frSchema,
transactionFee: frNumberSchema,
noteHashes: z.array(frSchema),
nullifiers: z.array(frSchema),
l2ToL1Msgs: z.array(frSchema),
publicDataWrites: z.array(
z.object({ leafIndex: frSchema, newValue: frSchema }),
),
noteEncryptedLogsLength: frSchema,
encryptedLogsLength: frSchema,
unencryptedLogsLength: frSchema,
noteEncryptedLogsLength: frNumberSchema,
encryptedLogsLength: frNumberSchema,
unencryptedLogsLength: frNumberSchema,
noteEncryptedLogs: z.object({
functionLogs: z.array(
z.object({
Expand Down
32 changes: 25 additions & 7 deletions packages/types/src/aztec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,41 @@ export type StringifiedAztecAddress = {
value: `0x${string}`;
};

const frToHexString = (val: unknown) => {
if (!val) return val;
else if ((val as StringifiedAztecFr).value)
return (val as StringifiedAztecFr).value;
else if ((val as AztecFr).toString) return (val as AztecFr).toString();
else return val;
};

export const frSchema = z.preprocess(
(val) => {
if (!val) return val;
else if ((val as StringifiedAztecFr).value)
return (val as StringifiedAztecFr).value;
else if ((val as AztecFr).toString) return (val as AztecFr).toString();
else return val;
},
frToHexString,
z
.string()
.length(66)
.regex(/^0x[0-9a-fA-F]+$/)
);

export const frNumberSchema = z.preprocess((val) => {
if (typeof val === "number") return val;
const v = frToHexString(val);
if (typeof v === "string") return parseInt(v, 16);
return val;
}, z.coerce.number());

export const frTimestampSchema = z.preprocess((val) => {
if (typeof val === "number") return val;
const v = frToHexString(val);
if (typeof v === "string" && v.startsWith("0x")) return parseInt(v, 16) * 1000;
return val;
}, z.coerce.number());

// NOTE: it's technically not the same as Fr but practically it is
export const aztecAddressSchema = frSchema;

export const ethAddressSchema = z.string().length(42).regex(/^0x[0-9a-fA-F]+$/);

export type StringifiedBuffer = {
type: "Buffer";
data: number[];
Expand Down
4 changes: 3 additions & 1 deletion services/aztec-listener/src/aztec/genesis-catchup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { onCatchupBlock } from "../event-handler/index.js";
import { logger } from "../logger.js";
import { getBlock } from "./network-client.js";

const ARTIFICIAL_WAIT = 1000;

export const startCatchup = async ({
from,
to,
Expand All @@ -18,7 +20,7 @@ export const startCatchup = async ({
await onCatchupBlock(blockRes);
if (NODE_ENV === "development") {
// NOTE: we are restarting our local cluster quiet often, so we shouldn't spam them unnecessarily
await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, ARTIFICIAL_WAIT));
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ CREATE TABLE IF NOT EXISTS "public_data_write" (
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tx_effect" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"tx_hash" varchar NOT NULL,
"hash" varchar NOT NULL,
"index" integer NOT NULL,
"revert_code" smallint NOT NULL,
"transaction_fee" varchar(66) NOT NULL,
"transaction_fee" bigint NOT NULL,
"note_hashes" jsonb NOT NULL,
"nullifiers" jsonb NOT NULL,
"l2_to_l1_msgs" jsonb NOT NULL,
"note_encrypted_logs_length" varchar(66) NOT NULL,
"encrypted_logs_length" varchar(66) NOT NULL,
"unencrypted_logs_length" varchar(66) NOT NULL
"note_encrypted_logs_length" bigint NOT NULL,
"encrypted_logs_length" bigint NOT NULL,
"unencrypted_logs_length" bigint NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tx_effect_to_logs" (
Expand All @@ -69,25 +69,25 @@ CREATE TABLE IF NOT EXISTS "tx_effect_to_public_data_write" (
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "content_commitment" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"num_txs" varchar(66) NOT NULL,
"num_txs" bigint NOT NULL,
"txs_effects_hash" "bytea" NOT NULL,
"in_hash" "bytea" NOT NULL,
"out_hash" "bytea" NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "gas_fees" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"fee_per_da_gas" varchar(66),
"fee_per_l2_gas" varchar(66)
"fee_per_da_gas" bigint,
"fee_per_l2_gas" bigint
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "global_variables" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"chain_id" varchar(66),
"version" varchar(66),
"block_number" varchar(66),
"slot_number" varchar(66),
"timestamp" varchar(66),
"chain_id" bigint NOT NULL,
"version" bigint NOT NULL,
"block_number" bigint NOT NULL,
"slot_number" bigint NOT NULL,
"timestamp" bigint NOT NULL,
"coinbase" varchar(42) NOT NULL,
"fee_recipient" varchar(66) NOT NULL,
"gas_fees_id" uuid NOT NULL
Expand All @@ -99,7 +99,7 @@ CREATE TABLE IF NOT EXISTS "header" (
"content_commitment_id" uuid NOT NULL,
"state_id" uuid NOT NULL,
"global_variables_id" uuid NOT NULL,
"total_fees" varchar(66) NOT NULL
"total_fees" bigint NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "l1_to_l2_message_tree" (
Expand Down
1 change: 0 additions & 1 deletion services/explorer-api/migrations/0001_brainy_venom.sql

This file was deleted.

42 changes: 21 additions & 21 deletions services/explorer-api/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "e5266a9a-a408-4d41-af8d-121243dede01",
"id": "1546764d-c77b-4d7b-b16d-f2ffbe453dd1",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -269,8 +269,8 @@
"notNull": true,
"default": "gen_random_uuid()"
},
"tx_hash": {
"name": "tx_hash",
"hash": {
"name": "hash",
"type": "varchar",
"primaryKey": false,
"notNull": true
Expand All @@ -289,7 +289,7 @@
},
"transaction_fee": {
"name": "transaction_fee",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
Expand All @@ -313,19 +313,19 @@
},
"note_encrypted_logs_length": {
"name": "note_encrypted_logs_length",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
"encrypted_logs_length": {
"name": "encrypted_logs_length",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
"unencrypted_logs_length": {
"name": "unencrypted_logs_length",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
}
Expand Down Expand Up @@ -451,7 +451,7 @@
},
"num_txs": {
"name": "num_txs",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
Expand Down Expand Up @@ -492,13 +492,13 @@
},
"fee_per_da_gas": {
"name": "fee_per_da_gas",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
},
"fee_per_l2_gas": {
"name": "fee_per_l2_gas",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
}
Expand All @@ -521,33 +521,33 @@
},
"chain_id": {
"name": "chain_id",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
"notNull": true
},
"version": {
"name": "version",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
"notNull": true
},
"block_number": {
"name": "block_number",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
"notNull": true
},
"slot_number": {
"name": "slot_number",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
"notNull": true
},
"timestamp": {
"name": "timestamp",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": false
"notNull": true
},
"coinbase": {
"name": "coinbase",
Expand Down Expand Up @@ -620,7 +620,7 @@
},
"total_fees": {
"name": "total_fees",
"type": "varchar(66)",
"type": "bigint",
"primaryKey": false,
"notNull": true
}
Expand Down
Loading

0 comments on commit 9c27133

Please sign in to comment.