Skip to content

Commit

Permalink
chore: Introduce EventSelector class (AztecProtocol#3960)
Browse files Browse the repository at this point in the history
Fixes AztecProtocol#2632

---------

Co-authored-by: Jan Beneš <janbenes1234@gmail.com>
  • Loading branch information
spalladino and benesjan authored Jan 12, 2024
1 parent 54f4ba1 commit d925d85
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 168 deletions.
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MerkleTreeId, UnencryptedL2Log } from '@aztec/circuit-types';
import { RETURN_VALUES_LENGTH } from '@aztec/circuits.js';
import { FunctionSelector } from '@aztec/foundation/abi';
import { EventSelector, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
import { Fr, Point } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -269,7 +269,7 @@ export class Oracle {
const logPayload = Buffer.concat(message.map(charBuffer => Fr.fromString(charBuffer).toBuffer().subarray(-1)));
const log = new UnencryptedL2Log(
AztecAddress.fromString(contractAddress),
FunctionSelector.fromField(fromACVMField(eventSelector)), // TODO https://github.com/AztecProtocol/aztec-packages/issues/2632
EventSelector.fromField(fromACVMField(eventSelector)),
logPayload,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BlockHeader, FunctionSelector } from '@aztec/circuits.js';
import { EventSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -29,6 +30,7 @@ export function createAztecNodeClient(url: string, fetch = defaultFetch): AztecN
ExtendedUnencryptedL2Log,
ContractData,
Fr,
EventSelector,
FunctionSelector,
BlockHeader,
L2Block,
Expand Down
22 changes: 7 additions & 15 deletions yarn-project/circuit-types/src/logs/log_filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AztecAddress, FunctionSelector } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/circuits.js';
import { EventSelector } from '@aztec/foundation/abi';

import { TxHash } from '../tx/tx_hash.js';
import { LogId } from './log_id.js';
Expand All @@ -8,25 +9,16 @@ import { LogId } from './log_id.js';
* @remarks This filter is applied as an intersection of all it's params.
*/
export type LogFilter = {
/**
* Hash of a transaction from which to fetch the logs.
*/
/** Hash of a transaction from which to fetch the logs. */
txHash?: TxHash;
/**
* The block number from which to start fetching logs (inclusive).
*/
/** The block number from which to start fetching logs (inclusive). */
fromBlock?: number;
/** The block number until which to fetch logs (not inclusive). */
toBlock?: number;
/**
* Log id after which to start fetching logs.
*/
/** Log id after which to start fetching logs. */
afterLog?: LogId;
/** The contract address to filter logs by. */
contractAddress?: AztecAddress;
/**
* Selector of the event/log topic.
* TODO: https://github.com/AztecProtocol/aztec-packages/issues/2632
*/
selector?: FunctionSelector;
/** Selector of the event/log topic. */
selector?: EventSelector;
};
18 changes: 8 additions & 10 deletions yarn-project/circuit-types/src/logs/unencrypted_l2_log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AztecAddress, FunctionSelector } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/circuits.js';
import { EventSelector } from '@aztec/foundation/abi';
import { BufferReader, prefixBufferWithLength } from '@aztec/foundation/serialize';

import { randomBytes } from 'crypto';
Expand All @@ -17,17 +18,14 @@ export class UnencryptedL2Log {
* TODO: Optimize this once it makes sense.
*/
public readonly contractAddress: AztecAddress,
/**
* Selector of the event/log topic.
* TODO: https://github.com/AztecProtocol/aztec-packages/issues/2632
*/
public readonly selector: FunctionSelector,
/** Selector of the event/log topic. */
public readonly selector: EventSelector,
/** The data contents of the log. */
public readonly data: Buffer,
) {}

get length(): number {
return FunctionSelector.SIZE + this.data.length;
return EventSelector.SIZE + this.data.length;
}

/**
Expand Down Expand Up @@ -60,7 +58,7 @@ export class UnencryptedL2Log {
public static fromBuffer(buffer: Buffer | BufferReader): UnencryptedL2Log {
const reader = BufferReader.asReader(buffer);
const contractAddress = AztecAddress.fromBuffer(reader);
const selector = FunctionSelector.fromBuffer(reader);
const selector = EventSelector.fromBuffer(reader);
const data = reader.readBuffer();
return new UnencryptedL2Log(contractAddress, selector, data);
}
Expand All @@ -71,8 +69,8 @@ export class UnencryptedL2Log {
*/
public static random(): UnencryptedL2Log {
const contractAddress = AztecAddress.random();
const selector = new FunctionSelector(Math.floor(Math.random() * (2 ** (FunctionSelector.SIZE * 8) - 1)));
const dataLength = FunctionSelector.SIZE + Math.floor(Math.random() * 200);
const selector = new EventSelector(Math.floor(Math.random() * (2 ** (EventSelector.SIZE * 8) - 1)));
const dataLength = EventSelector.SIZE + Math.floor(Math.random() * 200);
const data = randomBytes(dataLength);
return new UnencryptedL2Log(contractAddress, selector, data);
}
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/cli/src/cmds/get_logs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AztecAddress, FunctionSelector, LogFilter, LogId, TxHash } from '@aztec/aztec.js';
import { AztecAddress, LogFilter, LogId, TxHash } from '@aztec/aztec.js';
import { EventSelector } from '@aztec/foundation/abi';
import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';

Expand All @@ -13,7 +14,7 @@ export async function getLogs(
toBlock: number,
afterLog: LogId,
contractAddress: AztecAddress,
selector: FunctionSelector,
selector: EventSelector,
rpcUrl: string,
follow: boolean,
debugLogger: DebugLogger,
Expand Down
137 changes: 0 additions & 137 deletions yarn-project/foundation/src/abi/function_selector.ts

This file was deleted.

2 changes: 1 addition & 1 deletion yarn-project/foundation/src/abi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * from './abi.js';
export * from './abi_coder.js';
export * from './encoder.js';
export * from './decoder.js';
export * from './function_selector.js';
export * from './selector.js';
export * from './utils.js';
Loading

0 comments on commit d925d85

Please sign in to comment.