Skip to content

Commit

Permalink
chore: Remove unneeded lodash imports (AztecProtocol#3863)
Browse files Browse the repository at this point in the history
This removes uses of:
- `compact`
- `upperfirst`
- `uniq`
- `flatten`
- `flatmap`
- `every`

And removes from package.json as they were not used:
- `partition`
- `zip`
- `zipwith`

After this cleanup, we are still using:
- `camelcase`
- `capitalize`
- `chunk`
- `clonedeepwith`
- `isequal`
- `omit`
- `pick`
- `times`

Out of those, `times` is the easiest to replace, but it's annoyingly
verbose, so I preferred to keep it. If we really want to remove the
dependency for some reason, we can re-write it and place it in
`foundation`.

```ts
// With lodash
times(5, i => new Address(i))

// Without lodash
Array(5).fill(0).map((_, i) => new Address(i))

// Alternative
[...Array(5).keys()].map(i => new Address(i))
```

Fixes AztecProtocol#3813
  • Loading branch information
spalladino authored Jan 8, 2024
1 parent afd7b6d commit 53fec42
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 171 deletions.
2 changes: 0 additions & 2 deletions noir/tooling/noir_codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"dependencies": {
"@noir-lang/types": "workspace:*",
"glob": "^10.3.10",
"lodash": "^4.17.21",
"ts-command-line-args": "^2.5.1"
},
"files": [
Expand Down Expand Up @@ -40,7 +39,6 @@
"devDependencies": {
"@noir-lang/noir_js": "workspace:*",
"@types/chai": "^4",
"@types/lodash": "^4",
"@types/mocha": "^10.0.1",
"@types/node": "^20.6.2",
"@types/prettier": "^3",
Expand Down
5 changes: 1 addition & 4 deletions noir/tooling/noir_codegen/src/utils/glob.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { sync as globSync } from 'glob';
import _ from 'lodash';
const { flatten, uniq } = _;

export function glob(cwd: string, patternsOrFiles: string[]): string[] {
const matches = patternsOrFiles.map((p) => globSync(p, { ignore: 'node_modules/**', absolute: true, cwd }));

return uniq(flatten(matches));
return [...new Set(matches.flat())];
}
9 changes: 0 additions & 9 deletions noir/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3980,15 +3980,13 @@ __metadata:
"@noir-lang/noir_js": "workspace:*"
"@noir-lang/types": "workspace:*"
"@types/chai": ^4
"@types/lodash": ^4
"@types/mocha": ^10.0.1
"@types/node": ^20.6.2
"@types/prettier": ^3
chai: ^4.3.8
eslint: ^8.50.0
eslint-plugin-prettier: ^5.0.0
glob: ^10.3.10
lodash: ^4.17.21
mocha: ^10.2.0
prettier: 3.0.3
ts-command-line-args: ^2.5.1
Expand Down Expand Up @@ -5340,13 +5338,6 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash@npm:^4":
version: 4.14.202
resolution: "@types/lodash@npm:4.14.202"
checksum: a91acf3564a568c6f199912f3eb2c76c99c5a0d7e219394294213b3f2d54f672619f0fde4da22b29dc5d4c31457cd799acc2e5cb6bd90f9af04a1578483b6ff7
languageName: node
linkType: hard

"@types/lru-cache@npm:^5.1.0":
version: 5.1.1
resolution: "@types/lru-cache@npm:5.1.1"
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/aztec.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@
"@aztec/ethereum": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/types": "workspace:^",
"lodash.every": "^4.6.0",
"lodash.partition": "^4.6.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@types/lodash.every": "^4.6.7",
"@types/lodash.partition": "^4.6.0",
"@types/node": "^18.7.23",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
Expand Down
4 changes: 1 addition & 3 deletions yarn-project/aztec.js/src/contract/sent_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { FieldsOf } from '@aztec/circuits.js';
import { retryUntil } from '@aztec/foundation/retry';
import { ExtendedNote, GetUnencryptedLogsResponse, PXE, TxHash, TxReceipt, TxStatus } from '@aztec/types';

import every from 'lodash.every';

/** Options related to waiting for a tx. */
export type WaitOpts = {
/** The maximum time (in seconds) to wait for the transaction to be mined. Defaults to 60. */
Expand Down Expand Up @@ -126,7 +124,7 @@ export class SentTx {
// Check if all sync blocks on the PXE Service are greater or equal than the block in which the tx was mined
const { blocks, notes } = await this.pxe.getSyncStatus();
const targetBlock = txReceipt.blockNumber!;
const areNotesSynced = blocks >= targetBlock && every(notes, block => block >= targetBlock);
const areNotesSynced = blocks >= targetBlock && Object.values(notes).every(block => block >= targetBlock);
return areNotesSynced ? txReceipt : undefined;
},
'isMined',
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
"@types/levelup": "^5.1.2",
"@types/lodash.every": "^4.6.7",
"@types/lodash.times": "^4.3.7",
"@types/lodash.zip": "^4.2.7",
"@types/lodash.zipwith": "^4.2.7",
"@types/memdown": "^3.0.3",
"@types/node": "^18.7.23",
"buffer": "^6.0.3",
Expand All @@ -64,8 +62,6 @@
"lodash.compact": "^3.0.1",
"lodash.every": "^4.6.0",
"lodash.times": "^4.3.2",
"lodash.zip": "^4.2.0",
"lodash.zipwith": "^4.2.0",
"memdown": "^6.1.1",
"process": "^0.11.10",
"puppeteer": "^21.3.4",
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/noir-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
"fs-extra": "^11.1.1",
"lodash.camelcase": "^4.3.0",
"lodash.capitalize": "^4.2.1",
"lodash.compact": "^3.0.1",
"lodash.times": "^4.3.2",
"lodash.upperfirst": "^4.3.1",
"memfs": "^4.6.0",
"pako": "^2.1.0",
"semver": "^7.5.4",
Expand All @@ -69,9 +67,7 @@
"@types/jest": "^29.5.0",
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.capitalize": "^4.2.7",
"@types/lodash.compact": "^3.0.7",
"@types/lodash.times": "^4.3.7",
"@types/lodash.upperfirst": "^4.3.7",
"@types/node": "^18.7.23",
"@types/pako": "^2.0.0",
"@types/semver": "^7.5.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
isFunctionSelectorStruct,
} from '@aztec/foundation/abi';

import compact from 'lodash.compact';

/**
* Returns the corresponding typescript type for a given Noir type.
* @param type - The input Noir type.
Expand Down Expand Up @@ -168,8 +166,7 @@ function generateAbiStatement(name: string, artifactImportPath: string) {
* @returns The corresponding ts code.
*/
export function generateTypescriptContractInterface(input: ContractArtifact, artifactImportPath?: string) {
// `compact` removes all falsey values from an array
const methods = compact(input.functions.filter(f => f.name !== 'constructor').map(generateMethod));
const methods = input.functions.filter(f => f.name !== 'constructor').map(generateMethod);
const deploy = artifactImportPath && generateDeploy(input);
const ctor = artifactImportPath && generateConstructor(input.name);
const at = artifactImportPath && generateAt(input.name);
Expand Down
11 changes: 4 additions & 7 deletions yarn-project/noir-compiler/src/contract-interface-gen/noir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {

import camelCase from 'lodash.camelcase';
import capitalize from 'lodash.capitalize';
import compact from 'lodash.compact';
import times from 'lodash.times';
import upperFirst from 'lodash.upperfirst';

/**
* Returns whether this function type corresponds to a private call.
Expand Down Expand Up @@ -41,7 +39,8 @@ function generateCallStatement(selector: FunctionSelector, functionType: Functio
* @returns A capitalized camelcase string.
*/
function toPascalCase(str: string) {
return upperFirst(camelCase(str));
const camel = camelCase(str);
return camel[0].toUpperCase() + camel.slice(1);
}

/**
Expand Down Expand Up @@ -281,10 +280,8 @@ ${contractImpl}
*/
export function generateNoirContractInterface(artifact: ContractArtifact) {
// We don't allow calling a constructor, internal fns, or unconstrained fns from other contracts
const methods = compact(
artifact.functions.filter(
f => f.name !== 'constructor' && !f.isInternal && f.functionType !== FunctionType.UNCONSTRAINED,
),
const methods = artifact.functions.filter(
f => f.name !== 'constructor' && !f.isInternal && f.functionType !== FunctionType.UNCONSTRAINED,
);
const paramStructs = methods.flatMap(m => collectStructs(m.parameters, [m.name])).map(generateStruct);
const privateContractStruct = generateContractStruct(artifact.name, 'private', methods);
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/pxe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"koa": "^2.14.2",
"koa-router": "^12.0.0",
"lodash.omit": "^4.5.0",
"lodash.partition": "^4.6.0",
"lodash.times": "^4.3.2",
"sha3": "^2.1.4",
"tslib": "^2.4.0",
Expand All @@ -55,7 +54,6 @@
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@types/lodash.omit": "^4.5.7",
"@types/lodash.partition": "^4.6.0",
"@types/lodash.times": "^4.3.7",
"@types/node": "^18.7.23",
"jest": "^29.5.0",
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/sequencer-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@aztec/types": "workspace:^",
"@aztec/world-state": "workspace:^",
"lodash.chunk": "^4.2.0",
"lodash.flatmap": "^4.5.0",
"lodash.pick": "^4.4.0",
"lodash.times": "^4.3.2",
"tslib": "^2.4.0",
Expand All @@ -54,7 +53,6 @@
"@types/jest": "^29.5.0",
"@types/levelup": "^5.1.2",
"@types/lodash.chunk": "^4.2.7",
"@types/lodash.flatmap": "^4.5.7",
"@types/lodash.pick": "^4.4.7",
"@types/lodash.times": "^4.3.7",
"@types/memdown": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import { MerkleTreeOperations, MerkleTrees } from '@aztec/world-state';

import { MockProxy, mock } from 'jest-mock-extended';
import { default as levelup } from 'levelup';
import flatMap from 'lodash.flatmap';
import times from 'lodash.times';
import { type MemDown, default as memdown } from 'memdown';

Expand Down Expand Up @@ -127,16 +126,16 @@ describe('sequencer/solo_block_builder', () => {

// Updates the expectedDb trees based on the new commitments, contracts, and nullifiers from these txs
const updateExpectedTreesFromTxs = async (txs: ProcessedTx[]) => {
const newContracts = flatMap(txs, tx => tx.data.end.newContracts.map(n => computeContractLeaf(n)));
const newContracts = txs.flatMap(tx => tx.data.end.newContracts.map(n => computeContractLeaf(n)));
for (const [tree, leaves] of [
[MerkleTreeId.NOTE_HASH_TREE, flatMap(txs, tx => tx.data.end.newCommitments.map(l => l.value.toBuffer()))],
[MerkleTreeId.NOTE_HASH_TREE, txs.flatMap(tx => tx.data.end.newCommitments.map(l => l.value.toBuffer()))],
[MerkleTreeId.CONTRACT_TREE, newContracts.map(x => x.toBuffer())],
] as const) {
await expectsDb.appendLeaves(tree, leaves);
}
await expectsDb.batchInsert(
MerkleTreeId.NULLIFIER_TREE,
flatMap(txs, tx => tx.data.end.newNullifiers.map(x => x.value.toBuffer())),
txs.flatMap(tx => tx.data.end.newNullifiers.map(x => x.value.toBuffer())),
NULLIFIER_SUBTREE_HEIGHT,
);
for (const tx of txs) {
Expand Down Expand Up @@ -218,16 +217,16 @@ describe('sequencer/solo_block_builder', () => {
await updateArchive();
rootRollupOutput.endArchiveSnapshot = await getTreeSnapshot(MerkleTreeId.ARCHIVE);

const newNullifiers = flatMap(txs, tx => tx.data.end.newNullifiers);
const newCommitments = flatMap(txs, tx => tx.data.end.newCommitments);
const newContracts = flatMap(txs, tx => tx.data.end.newContracts).map(cd => computeContractLeaf(cd));
const newContractData = flatMap(txs, tx => tx.data.end.newContracts).map(
n => new ContractData(n.contractAddress, n.portalContractAddress),
);
const newPublicDataWrites = flatMap(txs, tx =>
const newNullifiers = txs.flatMap(tx => tx.data.end.newNullifiers);
const newCommitments = txs.flatMap(tx => tx.data.end.newCommitments);
const newContracts = txs.flatMap(tx => tx.data.end.newContracts).map(cd => computeContractLeaf(cd));
const newContractData = txs
.flatMap(tx => tx.data.end.newContracts)
.map(n => new ContractData(n.contractAddress, n.portalContractAddress));
const newPublicDataWrites = txs.flatMap(tx =>
tx.data.end.publicDataUpdateRequests.map(t => new PublicDataWrite(t.leafSlot, t.newValue)),
);
const newL2ToL1Msgs = flatMap(txs, tx => tx.data.end.newL2ToL1Msgs);
const newL2ToL1Msgs = txs.flatMap(tx => tx.data.end.newL2ToL1Msgs);
const newEncryptedLogs = new L2BlockL2Logs(txs.map(tx => tx.encryptedLogs || new TxL2Logs([])));
const newUnencryptedLogs = new L2BlockL2Logs(txs.map(tx => tx.unencryptedLogs || new TxL2Logs([])));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import { ContractData, L2Block, L2BlockL2Logs, MerkleTreeId, PublicDataWrite, Tx
import { MerkleTreeOperations } from '@aztec/world-state';

import chunk from 'lodash.chunk';
import flatMap from 'lodash.flatmap';

import { VerificationKeys } from '../mocks/verification_keys.js';
import { RollupProver } from '../prover/index.js';
Expand Down Expand Up @@ -133,16 +132,16 @@ export class SoloBlockBuilder implements BlockBuilder {
} = circuitsOutput;

// Collect all new nullifiers, commitments, and contracts from all txs in this block
const newNullifiers = flatMap(txs, tx => tx.data.end.newNullifiers);
const newCommitments = flatMap(txs, tx => tx.data.end.newCommitments);
const newContracts = flatMap(txs, tx => tx.data.end.newContracts).map(cd => computeContractLeaf(cd));
const newContractData = flatMap(txs, tx => tx.data.end.newContracts).map(
n => new ContractData(n.contractAddress, n.portalContractAddress),
);
const newPublicDataWrites = flatMap(txs, tx =>
const newNullifiers = txs.flatMap(tx => tx.data.end.newNullifiers);
const newCommitments = txs.flatMap(tx => tx.data.end.newCommitments);
const newContracts = txs.flatMap(tx => tx.data.end.newContracts).map(cd => computeContractLeaf(cd));
const newContractData = txs
.flatMap(tx => tx.data.end.newContracts)
.map(n => new ContractData(n.contractAddress, n.portalContractAddress));
const newPublicDataWrites = txs.flatMap(tx =>
tx.data.end.publicDataUpdateRequests.map(t => new PublicDataWrite(t.leafSlot, t.newValue)),
);
const newL2ToL1Msgs = flatMap(txs, tx => tx.data.end.newL2ToL1Msgs);
const newL2ToL1Msgs = txs.flatMap(tx => tx.data.end.newL2ToL1Msgs);

// Consolidate logs data from all txs
const encryptedLogsArr: TxL2Logs[] = [];
Expand Down
Loading

0 comments on commit 53fec42

Please sign in to comment.