Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export event selector and replace function selector with event selector where appropriate #7095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/api/abi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { type ContractArtifact, type FunctionArtifact, FunctionSelector } from '@aztec/foundation/abi';
export { type ContractArtifact, type FunctionArtifact, EventSelector, FunctionSelector } from '@aztec/foundation/abi';
export { loadContractArtifact, contractArtifactToBuffer, contractArtifactFromBuffer } from '@aztec/types/abi';
export { type NoirCompiledContract } from '@aztec/types/noir';
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
CheatCodes,
EthAddressLike,
EthCheatCodes,
EventSelectorLike,
FieldLike,
FunctionSelectorLike,
WrappedFieldLike,
Expand Down
13 changes: 11 additions & 2 deletions yarn-project/aztec.js/src/utils/abi_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type AztecAddress, type EthAddress, type Fr, type FunctionSelector } from '@aztec/circuits.js';
import {
type AztecAddress,
type EthAddress,
type EventSelector,
type Fr,
type FunctionSelector,
} from '@aztec/circuits.js';

/** Any type that can be converted into a field for a contract call. */
export type FieldLike = Fr | Buffer | bigint | number | { /** Converts to field */ toField: () => Fr };
Expand All @@ -9,8 +15,11 @@ export type EthAddressLike = { /** Wrapped address */ address: FieldLike } | Eth
/** Any type that can be converted into an AztecAddress Aztec.nr struct. */
export type AztecAddressLike = { /** Wrapped address */ address: FieldLike } | AztecAddress;

/** Any type that can be converted into an FunctionSelector Aztec.nr struct. */
/** Any type that can be converted into a FunctionSelector Aztec.nr struct. */
export type FunctionSelectorLike = FieldLike | FunctionSelector;

/** Any type that can be converted into an EventSelector Aztec.nr struct. */
export type EventSelectorLike = FieldLike | EventSelector;

/** Any type that can be converted into a struct with a single `inner` field. */
export type WrappedFieldLike = { /** Wrapped value */ inner: FieldLike } | FieldLike;
15 changes: 8 additions & 7 deletions yarn-project/builder/src/contract-interface-gen/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ function generateEvents(events: any[] | undefined) {
`;

const fieldNames = event.fields.map((field: any) => `"${field.name}"`);
const eventType = `${eventName}: {decode: (payload: L1EventPayload | undefined) => ${eventName} | undefined, functionSelector: FunctionSelector, fieldNames: string[] }`;
const eventType = `${eventName}: {decode: (payload: L1EventPayload | undefined) => ${eventName} | undefined, eventSelector: EventSelector, fieldNames: string[] }`;

const eventImpl = `${eventName}: {
decode: this.decodeEvent(${event.fields.length}, FunctionSelector.fromSignature('${eventName}(${event.fields
decode: this.decodeEvent(${event.fields.length}, EventSelector.fromSignature('${eventName}(${event.fields
.map(() => 'Field')
.join(',')})'), [${fieldNames}]),
functionSelector: FunctionSelector.fromSignature('${eventName}(${event.fields.map(() => 'Field').join(',')})'),
eventSelector: EventSelector.fromSignature('${eventName}(${event.fields.map(() => 'Field').join(',')})'),
fieldNames: [${fieldNames}],
}`;

Expand All @@ -276,21 +276,21 @@ function generateEvents(events: any[] | undefined) {
eventDefs: eventsMetadata.map(({ eventDef }) => eventDef).join('\n'),
events: `
// Partial application is chosen is to avoid the duplication of so much codegen.
private static decodeEvent<T>(fieldsLength: number, functionSelector: FunctionSelector, fields: string[]): (payload: L1EventPayload | undefined) => T | undefined {
private static decodeEvent<T>(fieldsLength: number, eventSelector: EventSelector, fields: string[]): (payload: L1EventPayload | undefined) => T | undefined {
return (payload: L1EventPayload | undefined): T | undefined => {
if (payload === undefined) {
return undefined;
}
if (
!functionSelector.equals(
FunctionSelector.fromField(payload.eventTypeId),
!eventSelector.equals(
EventSelector.fromField(payload.eventTypeId),
)
) {
return undefined;
}
if (payload.event.items.length !== fieldsLength) {
throw new Error(
'Something is weird here, we have matching FunctionSelectors, but the actual payload has mismatched length',
'Something is weird here, we have matching EventSelectors, but the actual payload has mismatched length',
);
}

Expand Down Expand Up @@ -351,6 +351,7 @@ import {
EthAddressLike,
FieldLike,
Fr,
EventSelector,
FunctionSelector,
FunctionSelectorLike,
L1EventPayload,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type PartialAddress,
type Point,
} from '@aztec/circuits.js';
import { type ContractArtifact, type FunctionSelector } from '@aztec/foundation/abi';
import { type ContractArtifact, type EventSelector } from '@aztec/foundation/abi';
import {
type ContractClassWithId,
type ContractInstanceWithAddress,
Expand Down Expand Up @@ -397,7 +397,7 @@ export interface PXE {
*/
export interface EventMetadata<T> {
decode(payload: L1EventPayload): T | undefined;
functionSelector: FunctionSelector;
eventSelector: EventSelector;
fieldNames: string[];
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/structs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ export * from './tx_request.js';
export * from './validation_requests.js';
export * from './verification_key.js';

export { FunctionSelector } from '@aztec/foundation/abi';
export { EventSelector, FunctionSelector } from '@aztec/foundation/abi';
export * from '@aztec/foundation/aztec-address';
export * from '@aztec/foundation/fields';
10 changes: 8 additions & 2 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ import {
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import { computeNoteHashNonce, siloNullifier } from '@aztec/circuits.js/hash';
import { type ContractArtifact, type DecodedReturn, FunctionSelector, encodeArguments } from '@aztec/foundation/abi';
import {
type ContractArtifact,
type DecodedReturn,
EventSelector,
FunctionSelector,
encodeArguments,
} from '@aztec/foundation/abi';
import { type Fq, Fr, type Point } from '@aztec/foundation/fields';
import { SerialQueue } from '@aztec/foundation/fifo';
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -853,7 +859,7 @@ export class PXEService implements PXE {
if (visibleEvent.payload === undefined) {
return undefined;
}
if (!FunctionSelector.fromField(visibleEvent.payload.eventTypeId).equals(eventMetadata.functionSelector)) {
if (!EventSelector.fromField(visibleEvent.payload.eventTypeId).equals(eventMetadata.eventSelector)) {
return undefined;
}
if (visibleEvent.payload.event.items.length !== eventMetadata.fieldNames.length) {
Expand Down
Loading