Skip to content

Commit

Permalink
chore: address comments + minor test issue with toEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 5, 2023
1 parent d391150 commit cb42729
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
5 changes: 0 additions & 5 deletions yarn-project/acir-simulator/src/client/function_selectors.ts

This file was deleted.

10 changes: 7 additions & 3 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,9 @@ describe('Private Execution test suite', () => {
const result = await runSimulator({ args, abi: parentAbi });

expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(privateIncrement));
expect(oracle.getFunctionABI.mock.calls[0]).toEqual([childAddress, childSelector]);
expect(oracle.getFunctionABI.mock.calls[0].map(a => a.toBuffer())).toEqual(
[childAddress, childSelector].map(a => a.toBuffer()),
);
expect(oracle.getPortalContractAddress.mock.calls[0]).toEqual([childAddress]);
expect(result.nestedExecutions).toHaveLength(1);
expect(result.nestedExecutions[0].callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(privateIncrement));
Expand Down Expand Up @@ -671,7 +673,9 @@ describe('Private Execution test suite', () => {
const result = await runSimulator({ args, abi: parentAbi });

expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(argsHash);
expect(oracle.getFunctionABI.mock.calls[0]).toEqual([testAddress, testCodeGenSelector]);
expect(oracle.getFunctionABI.mock.calls[0].map(a => a.toBuffer())).toEqual(
[testAddress, testCodeGenSelector].map(a => a.toBuffer()),
);
expect(oracle.getPortalContractAddress.mock.calls[0]).toEqual([testAddress]);
expect(result.nestedExecutions).toHaveLength(1);
expect(result.nestedExecutions[0].callStackItem.publicInputs.returnValues[0]).toEqual(argsHash);
Expand Down Expand Up @@ -815,7 +819,7 @@ describe('Private Execution test suite', () => {
);

expect(result.enqueuedPublicFunctionCalls).toHaveLength(1);
expect(result.enqueuedPublicFunctionCalls[0]).toEqual(publicCallRequest);
expect(result.enqueuedPublicFunctionCalls[0].toBuffer()).toEqual(publicCallRequest.toBuffer());
expect(result.callStackItem.publicInputs.publicCallStack[0]).toEqual(publicCallRequestHash);
});
});
Expand Down
7 changes: 3 additions & 4 deletions yarn-project/acir-simulator/src/client/simulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CallContext, CircuitsWasm, FunctionData, TxContext } from '@aztec/circuits.js';
import { CallContext, CircuitsWasm, FunctionData, MAX_NOTE_FIELDS_LENGTH, TxContext } from '@aztec/circuits.js';
import { computeTxHash } from '@aztec/circuits.js/abis';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { ArrayType, FunctionSelector, FunctionType, encodeArguments } from '@aztec/foundation/abi';
Expand All @@ -14,7 +14,6 @@ import { PackedArgsCache } from '../packed_args_cache.js';
import { ClientTxExecutionContext } from './client_execution_context.js';
import { DBOracle, FunctionAbiWithDebugMetadata } from './db_oracle.js';
import { ExecutionResult } from './execution_result.js';
import { computeNoteHashAndNullifierSelector, computeNoteHashAndNullifierSignature } from './function_selectors.js';
import { PrivateFunctionExecution } from './private_execution.js';
import { UnconstrainedFunctionExecution } from './unconstrained_execution.js';

Expand Down Expand Up @@ -171,12 +170,12 @@ export class AcirSimulator {
let abi: FunctionAbiWithDebugMetadata | undefined = undefined;

// Brute force
for (let i = 1; i < 5; i++) {
for (let i = 0; i < MAX_NOTE_FIELDS_LENGTH; i++) {
const signature = `compute_note_hash_and_nullifier(Field,Field,Field,[Field;${i}])`;
const selector = FunctionSelector.fromSignature(signature);
try {
abi = await this.db.getFunctionABI(contractAddress, selector);
break;
if (abi !== undefined) break;
} catch (e) {
// ignore
}
Expand Down
5 changes: 1 addition & 4 deletions yarn-project/end-to-end/src/e2e_lending_contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { AztecRPCServer } from '@aztec/aztec-rpc';
import { AztecAddress, CheatCodes, Fr, Wallet, computeMessageSecretHash, sleep } from '@aztec/aztec.js';
import { AztecAddress, CheatCodes, Fr, Wallet, computeMessageSecretHash } from '@aztec/aztec.js';
import { CircuitsWasm, CompleteAddress } from '@aztec/circuits.js';
import { pedersenPlookupCommitInputs } from '@aztec/circuits.js/barretenberg';
import { DebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -88,7 +88,6 @@ describe('e2e_lending_contract', () => {
stableCoin: NativeTokenContract,
account: Account,
) => {
await sleep(5000);
logger('Fetching storage snapshot 📸 ');
const accountKey = await account.key();

Expand Down Expand Up @@ -294,7 +293,6 @@ describe('e2e_lending_contract', () => {
const receipt3 = await tx3.wait();
expect(receipt3.status).toBe(TxStatus.MINED);

// At this point we should have some values!????
const tx4 = stableCoin.methods.redeemShield(shieldAmount, secret, recipient).send({ origin: recipient });
const receipt4 = await tx4.wait();
expect(receipt4.status).toBe(TxStatus.MINED);
Expand Down Expand Up @@ -413,7 +411,6 @@ describe('e2e_lending_contract', () => {
// - increase the private debt.

logger('Borrow 🥸 : 🏦 -> 🍌');
logger(`Addresses: ${stableCoin.address}, ${lendingContract.address}, ${collateralAsset.address}, ${recipient}`);
const tx = lendingContract.methods
.borrow_private(account.secret, account.address, borrowAmount)
.send({ origin: recipient });
Expand Down
13 changes: 11 additions & 2 deletions yarn-project/foundation/src/abi/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ export class FunctionSignatureDecoder {
* @returns The function signature.
*/
public decode(): string {
const hmm = this.parameters.map(param => this.decodeParameter(param.type));
return `${this.name}(${hmm.join(',')})`;
return `${this.name}(${this.parameters.map(param => this.decodeParameter(param.type)).join(',')})`;
}
}

/**
* Decodes a function signature from the name and parameters.
* @param name - The name of the function-
* @param parameters - The parameters of the function.
* @returns - The function signature.
*/
export function decodeFunctionSignature(name: string, parameters: ABIParameter[]) {
return new FunctionSignatureDecoder(name, parameters).decode();
}
10 changes: 6 additions & 4 deletions yarn-project/foundation/src/abi/function_selector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ABIParameter, FunctionSignatureDecoder } from '@aztec/foundation/abi';
import { ABIParameter, decodeFunctionSignature } from '@aztec/foundation/abi';
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
import { keccak } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';

import { createDebugLogger } from '../log/logger.js';

/**
* A function selector is the first 4 bytes of the hash of a function signature.
*/
Expand All @@ -12,6 +14,7 @@ export class FunctionSelector {
* The size of the function selector in bytes.
*/
public static SIZE = 4;
protected logger = createDebugLogger('aztec:foundation:function-selector');

constructor(/** number representing the function selector */ public value: number) {
if (value > 2 ** (FunctionSelector.SIZE * 8) - 1) {
Expand Down Expand Up @@ -97,10 +100,9 @@ export class FunctionSelector {
* @returns A Buffer containing the 4-byte function selector.
*/
static fromNameAndParameters(name: string, parameters: ABIParameter[]) {
const signature = new FunctionSignatureDecoder(name, parameters).decode();
const signature = decodeFunctionSignature(name, parameters);
const selector = FunctionSelector.fromSignature(signature);
// If you are debugging, can be useful to uncomment the following line.
// console.log(`Function selector for ${signature} is ${selector}`);
selector.logger(`Function selector for ${signature} is ${selector}`);
return selector;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SerialQueue } from '@aztec/foundation/fifo';
import { createDebugLogger } from '@aztec/foundation/log';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { L2Block, L2BlockDownloader, L2BlockSource } from '@aztec/types';

import { MerkleTreeOperations, MerkleTrees } from '../index.js';
Expand Down Expand Up @@ -28,7 +28,7 @@ export class ServerWorldStateSynchroniser implements WorldStateSynchroniser {
private merkleTreeDb: MerkleTrees,
private l2BlockSource: L2BlockSource,
config: WorldStateConfig,
private log = createDebugLogger('aztec:world_state'),
private log: DebugLogger = createDebugLogger('aztec:world_state'),
) {
this.l2BlockDownloader = new L2BlockDownloader(
l2BlockSource,
Expand Down

0 comments on commit cb42729

Please sign in to comment.