Skip to content

Commit

Permalink
chore: Split up protocol contract artifacts
Browse files Browse the repository at this point in the history
The protocol contracts package was loading all artifacts in a single
file, and then re-exporting from there. This means that whenever a
protocol contract was loaded, all other protocol contracts were fetched
and processed as well.

This breaks protocol contracts into granular exports, and adds a bundle
export that contains all for the few cases where we do need a single
function for loading all artifacts.
  • Loading branch information
spalladino committed Dec 16, 2024
1 parent 308c5ce commit 955b25d
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 91 deletions.
1 change: 1 addition & 0 deletions yarn-project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ivc-integration/artifacts
ivc-integration/src/types/
protocol-contracts/artifacts
protocol-contracts/src/protocol_contract_data.ts
protocol-contracts/src/protocol_contract_artifacts.ts
scripts/tmp
noir-contracts.js/src
noir-contracts.js/artifacts/
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ import { elapsed } from '@aztec/foundation/timer';
import { InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
import {
ContractClassRegisteredEvent,
ContractInstanceDeployedEvent,
PrivateFunctionBroadcastedEvent,
UnconstrainedFunctionBroadcastedEvent,
} from '@aztec/protocol-contracts';
} from '@aztec/protocol-contracts/class-registerer';
import { ContractInstanceDeployedEvent } from '@aztec/protocol-contracts/instance-deployer';
import { Attributes, type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';

import groupBy from 'lodash.groupby';
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/archiver/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { type Maybe } from '@aztec/foundation/types';
import { type DataStoreConfig } from '@aztec/kv-store/config';
import { createStore } from '@aztec/kv-store/lmdb';
import { TokenBridgeContractArtifact, TokenContractArtifact } from '@aztec/noir-contracts.js';
import { getCanonicalProtocolContract, protocolContractNames } from '@aztec/protocol-contracts';
import { protocolContractNames } from '@aztec/protocol-contracts';
import { getCanonicalProtocolContract } from '@aztec/protocol-contracts/bundle';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { type FunctionCall } from '@aztec/circuit-types';
import { type AztecAddress, Fr, FunctionSelector } from '@aztec/circuits.js';
import { FunctionType } from '@aztec/foundation/abi';
import { ProtocolContractAddress, ProtocolContractArtifact } from '@aztec/protocol-contracts';
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
import { getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice';

import { type L2AmountClaim } from '../utils/portal_manager.js';
import { FeeJuicePaymentMethod } from './fee_juice_payment_method.js';
Expand All @@ -23,7 +24,7 @@ export class FeeJuicePaymentMethodWithClaim extends FeeJuicePaymentMethod {
*/
override getFunctionCalls(): Promise<FunctionCall[]> {
const selector = FunctionSelector.fromNameAndParameters(
ProtocolContractArtifact.FeeJuice.functions.find(f => f.name === 'claim')!,
getCanonicalFeeJuice().artifact.functions.find(f => f.name === 'claim')!,
);

return Promise.resolve([
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
import { fileURLToPath } from '@aztec/aztec.js';
import { injectCommands as injectBuilderCommands } from '@aztec/builder';
import { injectCommands as injectWalletCommands } from '@aztec/cli-wallet';
import { injectCommands as injectContractCommands } from '@aztec/cli/contracts';
Expand All @@ -9,6 +8,7 @@ import { injectCommands as injectL1Commands } from '@aztec/cli/l1';
import { injectCommands as injectMiscCommands } from '@aztec/cli/misc';
import { injectCommands as injectPXECommands } from '@aztec/cli/pxe';
import { createConsoleLogger, createLogger } from '@aztec/foundation/log';
import { fileURLToPath } from '@aztec/foundation/url';

import { Command } from 'commander';
import { readFileSync } from 'fs';
Expand Down
1 change: 1 addition & 0 deletions yarn-project/cli/src/utils/aztec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FunctionType } from '@aztec/foundation/abi';
import { type EthAddress } from '@aztec/foundation/eth-address';
import { type LogFn, type Logger } from '@aztec/foundation/log';
import { type NoirPackageConfig } from '@aztec/foundation/noir';
// TODO HERE
import { RollupAbi } from '@aztec/l1-artifacts';
import { ProtocolContractAddress, protocolContractTreeRoot } from '@aztec/protocol-contracts';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/cli/src/utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Fr } from '@aztec/aztec.js/fields';
import { LogId } from '@aztec/aztec.js/log_id';
import { TxHash } from '@aztec/aztec.js/tx_hash';
import { type PXE } from '@aztec/circuit-types';
import { PublicKeys } from '@aztec/circuits.js';
import { PublicKeys } from '@aztec/circuits.js/types';
import { type LogFn } from '@aztec/foundation/log';

import { type Command, CommanderError, InvalidArgumentError, Option } from 'commander';
Expand Down
1 change: 1 addition & 0 deletions yarn-project/cli/src/utils/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type AztecAddress, type Fr } from '@aztec/circuits.js';
import { siloNullifier } from '@aztec/circuits.js/hash';
import { type LogFn } from '@aztec/foundation/log';
import { toHumanReadable } from '@aztec/foundation/serialize';
// TODO here as well
import { ProtocolContractAddress } from '@aztec/protocol-contracts';

export async function inspectBlock(pxe: PXE, blockNumber: number, log: LogFn, opts: { showTxs?: boolean } = {}) {
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/protocol-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "module",
"exports": {
".": "./dest/index.js",
"./bundle": "./dest/bundle/index.js",
"./*": "./dest/*/index.js"
},
"typedocOptions": {
Expand All @@ -20,7 +21,7 @@
"scripts": {
"build": "yarn clean && yarn generate && tsc -b",
"generate": "yarn generate:data",
"generate:data": "node --no-warnings --loader ts-node/esm src/scripts/generate_data.ts && run -T prettier -w src/protocol_contract_data.ts",
"generate:data": "node --no-warnings --loader ts-node/esm src/scripts/generate_data.ts && run -T prettier -w src/protocol_contract_data.ts src/protocol_contract_artifacts.ts",
"build:dev": "tsc -b --watch",
"build:ts": "tsc -b",
"clean": "rm -rf ./dest .tsbuildinfo ./artifacts",
Expand Down Expand Up @@ -99,4 +100,4 @@
"engines": {
"node": ">=18"
}
}
}
16 changes: 14 additions & 2 deletions yarn-project/protocol-contracts/src/auth-registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

import AuthRegistryJson from '../../artifacts/AuthRegistry.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

let protocolContract: ProtocolContract;

export const AuthRegistryArtifact = loadContractArtifact(AuthRegistryJson as NoirCompiledContract);

/** Returns the canonical deployment of the auth registry. */
export function getCanonicalAuthRegistry(): ProtocolContract {
return getCanonicalProtocolContract('AuthRegistry');
if (!protocolContract) {
protocolContract = makeProtocolContract('AuthRegistry', AuthRegistryArtifact);
}
return protocolContract;
}
36 changes: 36 additions & 0 deletions yarn-project/protocol-contracts/src/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getContractClassFromArtifact, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';

import { AuthRegistryArtifact } from '../auth-registry/index.js';
import { ContractClassRegistererArtifact } from '../class-registerer/index.js';
import { FeeJuiceArtifact } from '../fee-juice/index.js';
import { ContractInstanceDeployerArtifact } from '../instance-deployer/index.js';
import { MultiCallEntrypointArtifact } from '../multi-call-entrypoint/index.js';
import { type ProtocolContract } from '../protocol_contract.js';
import { ProtocolContractAddress, type ProtocolContractName, ProtocolContractSalt } from '../protocol_contract_data.js';
import { RouterArtifact } from '../router/index.js';

/** Returns the canonical deployment a given artifact. */
export function getCanonicalProtocolContract(name: ProtocolContractName): ProtocolContract {
const artifact = ProtocolContractArtifact[name];
const address = ProtocolContractAddress[name];
const salt = ProtocolContractSalt[name];
// TODO(@spalladino): This computes the contract class from the artifact twice.
const contractClass = getContractClassFromArtifact(artifact);
const instance = getContractInstanceFromDeployParams(artifact, { salt });
return {
instance: { ...instance, address },
contractClass,
artifact,
address,
};
}

export const ProtocolContractArtifact: Record<ProtocolContractName, ContractArtifact> = {
AuthRegistry: AuthRegistryArtifact,
ContractInstanceDeployer: ContractInstanceDeployerArtifact,
ContractClassRegisterer: ContractClassRegistererArtifact,
MultiCallEntrypoint: MultiCallEntrypointArtifact,
FeeJuice: FeeJuiceArtifact,
Router: RouterArtifact,
};
21 changes: 18 additions & 3 deletions yarn-project/protocol-contracts/src/class-registerer/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

import ContractClassRegistererJson from '../../artifacts/ContractClassRegisterer.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

export * from './contract_class_registered_event.js';
export * from './private_function_broadcasted_event.js';
export * from './unconstrained_function_broadcasted_event.js';

/** Returns the canonical deployment of the class registerer contract. */
export const ContractClassRegistererArtifact = loadContractArtifact(
ContractClassRegistererJson as NoirCompiledContract,
);

let protocolContract: ProtocolContract;

/** Returns the canonical deployment of the contract. */
export function getCanonicalClassRegisterer(): ProtocolContract {
return getCanonicalProtocolContract('ContractClassRegisterer');
if (!protocolContract) {
const artifact = ContractClassRegistererArtifact;
protocolContract = makeProtocolContract('ContractClassRegisterer', artifact);
}
return protocolContract;
}
18 changes: 15 additions & 3 deletions yarn-project/protocol-contracts/src/fee-juice/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

/** Returns the canonical deployment of the Fee Juice. */
import FeeJuiceJson from '../../artifacts/FeeJuice.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

export const FeeJuiceArtifact = loadContractArtifact(FeeJuiceJson as NoirCompiledContract);

let protocolContract: ProtocolContract;

/** Returns the canonical deployment of the contract. */
export function getCanonicalFeeJuice(): ProtocolContract {
return getCanonicalProtocolContract('FeeJuice');
if (!protocolContract) {
protocolContract = makeProtocolContract('FeeJuice', FeeJuiceArtifact);
}
return protocolContract;
}
5 changes: 0 additions & 5 deletions yarn-project/protocol-contracts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export * from './auth-registry/index.js';
export * from './class-registerer/index.js';
export * from './fee-juice/index.js';
export * from './instance-deployer/index.js';
export * from './multi-call-entrypoint/index.js';
export * from './protocol_contract.js';
export * from './protocol_contract_data.js';
export * from './protocol_contract_tree.js';
20 changes: 17 additions & 3 deletions yarn-project/protocol-contracts/src/instance-deployer/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

import ContractInstanceDeployerJson from '../../artifacts/ContractInstanceDeployer.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

export * from './contract_instance_deployed_event.js';

/** Returns the canonical deployment of the instance deployer contract. */
export const ContractInstanceDeployerArtifact = loadContractArtifact(
ContractInstanceDeployerJson as NoirCompiledContract,
);

let protocolContract: ProtocolContract;

/** Returns the canonical deployment of the contract. */
export function getCanonicalInstanceDeployer(): ProtocolContract {
return getCanonicalProtocolContract('ContractInstanceDeployer');
if (!protocolContract) {
protocolContract = makeProtocolContract('ContractInstanceDeployer', ContractInstanceDeployerArtifact);
}
return protocolContract;
}
23 changes: 23 additions & 0 deletions yarn-project/protocol-contracts/src/make_protocol_contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getContractClassFromArtifact, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';

import { type ProtocolContract } from './protocol_contract.js';
import { ProtocolContractAddress, type ProtocolContractName, ProtocolContractSalt } from './protocol_contract_data.js';

/**
* Returns the canonical deployment given its name and artifact.
* To be used internally within the protocol-contracts package.
*/
export function makeProtocolContract(name: ProtocolContractName, artifact: ContractArtifact): ProtocolContract {
const address = ProtocolContractAddress[name];
const salt = ProtocolContractSalt[name];
// TODO(@spalladino): This computes the contract class from the artifact twice.
const contractClass = getContractClassFromArtifact(artifact);
const instance = getContractInstanceFromDeployParams(artifact, { salt });
return {
instance: { ...instance, address },
contractClass,
artifact,
address,
};
}
19 changes: 16 additions & 3 deletions yarn-project/protocol-contracts/src/multi-call-entrypoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

export function getCanonicalMultiCallEntrypointContract(): ProtocolContract {
return getCanonicalProtocolContract('MultiCallEntrypoint');
import MultiCallEntrypointJson from '../../artifacts/MultiCallEntrypoint.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

export const MultiCallEntrypointArtifact = loadContractArtifact(MultiCallEntrypointJson as NoirCompiledContract);

let protocolContract: ProtocolContract;

/** Returns the canonical deployment of the contract. */
export function getCanonicalMultiCallEntrypoint(): ProtocolContract {
if (!protocolContract) {
protocolContract = makeProtocolContract('MultiCallEntrypoint', MultiCallEntrypointArtifact);
}
return protocolContract;
}
25 changes: 1 addition & 24 deletions yarn-project/protocol-contracts/src/protocol_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ import {
type ContractClassIdPreimage,
type ContractClassWithId,
type ContractInstanceWithAddress,
getContractClassFromArtifact,
getContractInstanceFromDeployParams,
} from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';

import {
ProtocolContractAddress,
ProtocolContractArtifact,
type ProtocolContractName,
ProtocolContractSalt,
} from './protocol_contract_data.js';
import { ProtocolContractAddress } from './protocol_contract_data.js';

/** Represents a canonical contract in the protocol. */
export interface ProtocolContract {
Expand All @@ -27,22 +20,6 @@ export interface ProtocolContract {
address: AztecAddress;
}

/** Returns the canonical deployment a given artifact. */
export function getCanonicalProtocolContract(name: ProtocolContractName): ProtocolContract {
const artifact = ProtocolContractArtifact[name];
const address = ProtocolContractAddress[name];
const salt = ProtocolContractSalt[name];
// TODO(@spalladino): This computes the contract class from the artifact twice.
const contractClass = getContractClassFromArtifact(artifact);
const instance = getContractInstanceFromDeployParams(artifact, { salt });
return {
instance: { ...instance, address },
contractClass,
artifact,
address,
};
}

export function isProtocolContract(address: AztecAddress) {
return Object.values(ProtocolContractAddress).some(a => a.equals(address));
}
18 changes: 15 additions & 3 deletions yarn-project/protocol-contracts/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
import { loadContractArtifact } from '@aztec/types/abi';
import { type NoirCompiledContract } from '@aztec/types/noir';

/** Returns the canonical deployment of the router. */
import RouterJson from '../../artifacts/Router.json' assert { type: 'json' };
import { makeProtocolContract } from '../make_protocol_contract.js';
import { type ProtocolContract } from '../protocol_contract.js';

export const RouterArtifact = loadContractArtifact(RouterJson as NoirCompiledContract);

let protocolContract: ProtocolContract;

/** Returns the canonical deployment of the contract. */
export function getCanonicalRouter(): ProtocolContract {
return getCanonicalProtocolContract('Router');
if (!protocolContract) {
protocolContract = makeProtocolContract('Router', RouterArtifact);
}
return protocolContract;
}
Loading

0 comments on commit 955b25d

Please sign in to comment.