Skip to content

Commit

Permalink
refactor: pass fn signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Dec 18, 2024
1 parent 921febb commit d5af95d
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 56 deletions.
8 changes: 4 additions & 4 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ export class Archiver implements ArchiveSource, Traceable {
return;
}

registerContractFunctionNames(address: AztecAddress, names: Record<string, string>): Promise<void> {
return this.store.registerContractFunctionName(address, names);
registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void> {
return this.store.registerContractFunctionSignatures(address, signatures);
}

getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
Expand Down Expand Up @@ -1073,8 +1073,8 @@ class ArchiverStoreHelper
getContractClassIds(): Promise<Fr[]> {
return this.store.getContractClassIds();
}
registerContractFunctionName(address: AztecAddress, names: Record<string, string>): Promise<void> {
return this.store.registerContractFunctionName(address, names);
registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void> {
return this.store.registerContractFunctionSignatures(address, signatures);
}
getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
return this.store.getContractFunctionName(address, selector);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/archiver/src/archiver/archiver_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export interface ArchiverDataStore {
// TODO: These function names are in memory only as they are for development/debugging. They require the full contract
// artifact supplied to the node out of band. This should be reviewed and potentially removed as part of
// the node api cleanup process.
registerContractFunctionName(address: AztecAddress, names: Record<string, string>): Promise<void>;
registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void>;
getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined>;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
type PrivateLog,
type UnconstrainedFunctionWithMembershipProof,
} from '@aztec/circuits.js';
import { type FunctionSelector } from '@aztec/foundation/abi';
import { FunctionSelector } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { createLogger } from '@aztec/foundation/log';
import { type AztecKVStore } from '@aztec/kv-store';
Expand Down Expand Up @@ -62,9 +62,14 @@ export class KVArchiverDataStore implements ArchiverDataStore {
return Promise.resolve(this.functionNames.get(selector.toString()));
}

registerContractFunctionName(_address: AztecAddress, names: Record<string, string>): Promise<void> {
for (const [selector, name] of Object.entries(names)) {
this.functionNames.set(selector, name);
registerContractFunctionSignatures(_address: AztecAddress, signatures: string[]): Promise<void> {
for (const sig of signatures) {
try {
const selector = FunctionSelector.fromSignature(sig);
this.functionNames.set(selector.toString(), sig.slice(0, sig.indexOf('(')));
} catch {
this.#log.warn(`Failed to parse signature: ${sig}. Ignoring`);
}
}

return Promise.resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
type PrivateLog,
type UnconstrainedFunctionWithMembershipProof,
} from '@aztec/circuits.js';
import { type FunctionSelector } from '@aztec/foundation/abi';
import { FunctionSelector } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { createLogger } from '@aztec/foundation/log';

Expand Down Expand Up @@ -734,9 +734,14 @@ export class MemoryArchiverStore implements ArchiverDataStore {
return Promise.resolve(this.functionNames.get(selector.toString()));
}

public registerContractFunctionName(_address: AztecAddress, names: Record<string, string>): Promise<void> {
for (const [selector, name] of Object.entries(names)) {
this.functionNames.set(selector, name);
public registerContractFunctionSignatures(_address: AztecAddress, signatures: string[]): Promise<void> {
for (const sig of signatures) {
try {
const selector = FunctionSelector.fromSignature(sig);
this.functionNames.set(selector.toString(), sig.slice(0, sig.indexOf('(')));
} catch {
this.#log.warn(`Failed to parse signature: ${sig}. Ignoring`);
}
}

return Promise.resolve();
Expand Down
13 changes: 5 additions & 8 deletions yarn-project/archiver/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
computePublicBytecodeCommitment,
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import { FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { FunctionType, decodeFunctionSignature } from '@aztec/foundation/abi';
import { createLogger } from '@aztec/foundation/log';
import { type Maybe } from '@aztec/foundation/types';
import { type DataStoreConfig } from '@aztec/kv-store/config';
Expand Down Expand Up @@ -47,14 +47,11 @@ async function registerProtocolContracts(store: KVArchiverDataStore) {
unconstrainedFunctions: [],
};

const functionNames: Record<string, string> = {};
for (const fn of contract.artifact.functions) {
if (fn.functionType === FunctionType.PUBLIC) {
functionNames[FunctionSelector.fromNameAndParameters(fn.name, fn.parameters).toString()] = fn.name;
}
}
const publicFunctionSignatures = contract.artifact.functions
.filter(fn => fn.functionType === FunctionType.PUBLIC)
.map(fn => decodeFunctionSignature(fn.name, fn.parameters));

await store.registerContractFunctionName(contract.address, functionNames);
await store.registerContractFunctionSignatures(contract.address, publicFunctionSignatures);
const bytecodeCommitment = computePublicBytecodeCommitment(contractClassPublic.packedBytecode);
await store.addContractClasses([contractClassPublic], [bytecodeCommitment], blockNumber);
await store.addContractInstances([contract.instance], blockNumber);
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,8 @@ export class AztecNodeService implements AztecNode, Traceable {
return this.contractDataSource.addContractClass(contractClass);
}

public registerContractFunctionNames(_address: AztecAddress, names: Record<string, string>): Promise<void> {
return this.contractDataSource.registerContractFunctionNames(_address, names);
public registerContractFunctionSignatures(_address: AztecAddress, signatures: string[]): Promise<void> {
return this.contractDataSource.registerContractFunctionSignatures(_address, signatures);
}

public flushTxs(): Promise<void> {
Expand Down
10 changes: 4 additions & 6 deletions yarn-project/circuit-types/src/interfaces/archiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,8 @@ describe('ArchiverApiSchema', () => {
expect(result).toBe(1n);
});

it('registerContractFunctionNames', async () => {
await context.client.registerContractFunctionNames(AztecAddress.random(), {
[FunctionSelector.random().toString()]: 'test_fn',
});
it('registerContractFunctionSignatures', async () => {
await context.client.registerContractFunctionSignatures(AztecAddress.random(), ['test()']);
});

it('getContract', async () => {
Expand Down Expand Up @@ -374,9 +372,9 @@ class MockArchiver implements ArchiverApi {
expect(address).toBeInstanceOf(AztecAddress);
return Promise.resolve(this.artifact);
}
registerContractFunctionNames(address: AztecAddress, names: Record<string, string>): Promise<void> {
registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void> {
expect(address).toBeInstanceOf(AztecAddress);
expect(names).toEqual(expect.any(Object));
expect(Array.isArray(signatures)).toBe(true);
return Promise.resolve();
}
getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
Expand Down
5 changes: 1 addition & 4 deletions yarn-project/circuit-types/src/interfaces/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export const ArchiverApiSchema: ApiSchemaFor<ArchiverApi> = {
getBytecodeCommitment: z.function().args(schemas.Fr).returns(schemas.Fr),
getContract: z.function().args(schemas.AztecAddress).returns(ContractInstanceWithAddressSchema.optional()),
getContractClassIds: z.function().args().returns(z.array(schemas.Fr)),
registerContractFunctionNames: z
.function()
.args(schemas.AztecAddress, z.record(z.string(), z.string()))
.returns(z.void()),
registerContractFunctionSignatures: z.function().args(schemas.AztecAddress, z.array(z.string())).returns(z.void()),
getL1ToL2Messages: z.function().args(schemas.BigInt).returns(z.array(schemas.Fr)),
getL1ToL2MessageIndex: z.function().args(schemas.Fr).returns(schemas.BigInt.optional()),
// TODO(#10007): Remove this method
Expand Down
10 changes: 4 additions & 6 deletions yarn-project/circuit-types/src/interfaces/aztec-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import { type L1ContractAddresses, L1ContractsNames } from '@aztec/ethereum';
import { type ContractArtifact, FunctionSelector } from '@aztec/foundation/abi';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { memoize } from '@aztec/foundation/decorators';
import { type JsonRpcTestContext, createJsonRpcTestSetup } from '@aztec/foundation/json-rpc/test';
import { fileURLToPath } from '@aztec/foundation/url';
Expand Down Expand Up @@ -223,10 +223,8 @@ describe('AztecNodeApiSchema', () => {
expect(response).toEqual(Object.fromEntries(ProtocolContractsNames.map(name => [name, expect.any(AztecAddress)])));
});

it('registerContractFunctionNames', async () => {
await context.client.registerContractFunctionNames(AztecAddress.random(), {
[FunctionSelector.random().toString()]: 'test_fn',
});
it('registerContractFunctionSignatures', async () => {
await context.client.registerContractFunctionSignatures(AztecAddress.random(), ['test()']);
});

it('getPrivateLogs', async () => {
Expand Down Expand Up @@ -506,7 +504,7 @@ class MockAztecNode implements AztecNode {
) as ProtocolContractAddresses,
);
}
registerContractFunctionNames(_address: AztecAddress, _names: Record<string, string>): Promise<void> {
registerContractFunctionSignatures(_address: AztecAddress, _signatures: string[]): Promise<void> {
return Promise.resolve();
}
getPrivateLogs(_from: number, _limit: number): Promise<PrivateLog[]> {
Expand Down
7 changes: 2 additions & 5 deletions yarn-project/circuit-types/src/interfaces/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export interface AztecNode
* @param aztecAddress
* @param artifact
*/
registerContractFunctionNames(address: AztecAddress, names: Record<string, string>): Promise<void>;
registerContractFunctionSignatures(address: AztecAddress, functionSignatures: string[]): Promise<void>;

/**
* Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
Expand Down Expand Up @@ -533,10 +533,7 @@ export const AztecNodeApiSchema: ApiSchemaFor<AztecNode> = {

getProtocolContractAddresses: z.function().returns(ProtocolContractAddressesSchema),

registerContractFunctionNames: z
.function()
.args(schemas.AztecAddress, z.record(z.string(), z.string()))
.returns(z.void()),
registerContractFunctionSignatures: z.function().args(schemas.AztecAddress, z.array(z.string())).returns(z.void()),

getPrivateLogs: z.function().args(z.number(), z.number()).returns(z.array(PrivateLog.schema)),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ export interface ContractDataSource {
/** Returns a function's name */
getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined>;
/** Registers a function names. Useful for debugging. */
registerContractFunctionNames(address: AztecAddress, names: Record<string, string>): Promise<void>;
registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void>;
}
13 changes: 5 additions & 8 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
EventSelector,
FunctionSelector,
FunctionType,
decodeFunctionSignature,
encodeArguments,
} from '@aztec/foundation/abi';
import { Fr, type Point } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -241,14 +242,10 @@ export class PXEService implements PXE {

await this.db.addContractArtifact(contractClassId, artifact);

const functionNames: Record<string, string> = {};
for (const fn of artifact.functions) {
if (fn.functionType === FunctionType.PUBLIC) {
functionNames[FunctionSelector.fromNameAndParameters(fn.name, fn.parameters).toString()] = fn.name;
}
}

await this.node.registerContractFunctionNames(instance.address, functionNames);
const publicFunctionSignatures = artifact.functions
.filter(fn => fn.functionType === FunctionType.PUBLIC)
.map(fn => decodeFunctionSignature(fn.name, fn.parameters));
await this.node.registerContractFunctionSignatures(instance.address, publicFunctionSignatures);

// TODO(#10007): Node should get public contract class from the registration event, not from PXE registration
await this.node.addContractClass({ ...contractClass, privateFunctions: [], unconstrainedFunctions: [] });
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/public/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class MockedAvmTestContractDataSource implements ContractDataSource {
return Promise.resolve(this.fnName);
}

registerContractFunctionNames(_address: AztecAddress, _names: Record<string, string>): Promise<void> {
registerContractFunctionSignatures(_address: AztecAddress, _signatures: string[]): Promise<void> {
return Promise.resolve();
}
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/txe/src/node/txe_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export class TXENode implements AztecNode {
* @param aztecAddress
* @param artifact
*/
registerContractFunctionNames(_address: AztecAddress, _names: Record<string, string>): Promise<void> {
registerContractFunctionSignatures(_address: AztecAddress, _signatures: string[]): Promise<void> {
throw new Error('TXE Node method addContractArtifact not implemented');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class TXEPublicContractDataSource implements ContractDataSource {
return Promise.resolve(func?.name);
}

registerContractFunctionNames(_address: AztecAddress, _names: Record<string, string>): Promise<void> {
registerContractFunctionSignatures(_address: AztecAddress, _signatures: []): Promise<void> {
return Promise.resolve();
}

Expand Down

0 comments on commit d5af95d

Please sign in to comment.