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

chore(e2e): Initial e2e test for CLI #1576

Merged
merged 3 commits into from
Aug 16, 2023
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
14 changes: 14 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,18 @@ jobs:
command: ./scripts/cond_run_script end-to-end $JOB_NAME ./scripts/run_tests_local e2e_public_token_contract.test.ts
working_directory: yarn-project/end-to-end

e2e-cli:
machine:
image: ubuntu-2004:202010-01
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Test"
command: ./scripts/cond_run_script end-to-end $JOB_NAME ./scripts/run_tests_local e2e_cli.test.ts
working_directory: yarn-project/end-to-end

e2e-p2p:
docker:
- image: aztecprotocol/alpine-build-image
Expand Down Expand Up @@ -1225,6 +1237,7 @@ workflows:
- e2e-non-contract-account: *e2e_test
- e2e-multiple-accounts-1-enc-key: *e2e_test
- e2e-public-token-contract: *e2e_test
- e2e-cli: *e2e_test
- e2e-cross-chain-messaging: *e2e_test
- e2e-public-cross-chain-messaging: *e2e_test
- e2e-public-to-private-messaging: *e2e_test
Expand All @@ -1249,6 +1262,7 @@ workflows:
- e2e-non-contract-account
- e2e-multiple-accounts-1-enc-key
- e2e-public-token-contract
- e2e-cli
- e2e-cross-chain-messaging
- e2e-public-cross-chain-messaging
- e2e-public-to-private-messaging
Expand Down
2 changes: 2 additions & 0 deletions build_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@
"archiver",
"aztec-node",
"aztec-rpc",
"aztec-sandbox",
"aztec.js",
"circuits.js",
"cli",
"ethereum",
"foundation",
"l1-artifacts",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"main": "./dest/index.js",
"bin": {
"aztec-cli": "./dest/index.js"
"aztec-cli": "./dest/bin/index.js"
},
"typedocOptions": {
"entryPoints": [
Expand Down
20 changes: 20 additions & 0 deletions yarn-project/aztec-cli/src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env -S node --no-warnings
import { createDebugLogger } from '@aztec/aztec.js';
import { createConsoleLogger } from '@aztec/foundation/log';

import { getProgram } from '../index.js';

const debugLogger = createDebugLogger('aztec:cli');
const log = createConsoleLogger();

/** CLI main entrypoint */
async function main() {
const program = getProgram(log, debugLogger);
await program.parseAsync(process.argv);
}

main().catch(err => {
log(`Error in command execution`);
log(err);
process.exit(1);
});
36 changes: 18 additions & 18 deletions yarn-project/aztec-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env -S node --no-warnings
import {
AztecAddress,
Contract,
Expand All @@ -13,15 +12,18 @@ import {
} from '@aztec/aztec.js';
import { StructType } from '@aztec/foundation/abi';
import { JsonStringify } from '@aztec/foundation/json-rpc';
import { createConsoleLogger, createDebugLogger } from '@aztec/foundation/log';
import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { compileContract } from '@aztec/noir-compiler/cli';
import { SchnorrAccountContractAbi } from '@aztec/noir-contracts/artifacts';
import { CompleteAddress, ContractData, L2BlockL2Logs, PrivateKey, TxHash } from '@aztec/types';

import { Command } from 'commander';
import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import { mnemonicToAccount } from 'viem/accounts';

import { encodeArgs, parseStructString } from './cli_encoder.js';
import { encodeArgs, parseStructString } from './encoding.js';
import {
deployAztecContracts,
getAbiFunction,
Expand All @@ -33,25 +35,29 @@ import {

const accountCreationSalt = Fr.ZERO;

const debugLogger = createDebugLogger('aztec:cli');
const log = createConsoleLogger();
const stripLeadingHex = (hex: string) => {
if (hex.length > 2 && hex.startsWith('0x')) {
return hex.substring(2);
}
return hex;
};

const program = new Command();

program.name('aztec-cli').description('CLI for interacting with Aztec.').version('0.1.0');

const { ETHEREUM_HOST, AZTEC_RPC_HOST, PRIVATE_KEY, API_KEY } = process.env;

/**
* Main function for the Aztec CLI.
* Returns commander program that defines the CLI.
* @param log - Console logger.
* @param debugLogger - Debug logger.
* @returns The CLI.
*/
async function main() {
export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
const program = new Command();

const packageJsonPath = resolve(dirname(fileURLToPath(import.meta.url)), '../package.json');
const version: string = JSON.parse(readFileSync(packageJsonPath).toString()).version;

program.name('aztec-cli').description('CLI for interacting with Aztec.').version(version);

program
.command('deploy-l1-contracts')
.description('Deploys all necessary Ethereum contracts for Aztec.')
Expand Down Expand Up @@ -474,11 +480,5 @@ async function main() {

compileContract(program, 'compile', log);

await program.parseAsync(process.argv);
return program;
}

main().catch(err => {
log(`Error in command execution`);
log(err);
process.exit(1);
});
2 changes: 1 addition & 1 deletion yarn-project/aztec-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DebugLogger, LogFn } from '@aztec/foundation/log';
import fs from 'fs';
import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';

import { encodeArgs } from './cli_encoder.js';
import { encodeArgs } from './encoding.js';

/**
* Helper type to dynamically import contracts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server';
import {
AztecRPC,
CompleteAddress,
ContractData,
ContractDataAndBytecode,
Expand All @@ -16,15 +17,15 @@ import {

import { foundry } from 'viem/chains';

import { AztecRPCServer, EthAddress } from '../index.js';
import { EthAddress } from '../index.js';

export const localAnvil = foundry;

/**
* Wraps an instance of the Aztec RPC Server implementation to a JSON RPC HTTP interface.
* @returns A new instance of the HTTP server.
*/
export function getHttpRpcServer(aztecRpcServer: AztecRPCServer): JsonRpcServer {
export function getHttpRpcServer(aztecRpcServer: AztecRPC): JsonRpcServer {
const generatedRpcServer = new JsonRpcServer(
aztecRpcServer,
{
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/aztec-sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "@aztec/aztec-sandbox",
"version": "0.0.0",
"type": "module",
"exports": "./dest/index.js",
"exports": {
".": "./dest/index.js",
"./http": "./dest/server.js"
},
"typedocOptions": {
"entryPoints": [
"./src/index.ts"
Expand Down
15 changes: 3 additions & 12 deletions yarn-project/aztec-sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { AztecNodeConfig, AztecNodeService, getConfigEnvVars } from '@aztec/aztec-node';
import { createAztecRPCServer, getHttpRpcServer, getConfigEnvVars as getRpcConfigEnvVars } from '@aztec/aztec-rpc';
import { createAztecRPCServer, getConfigEnvVars as getRpcConfigEnvVars } from '@aztec/aztec-rpc';
import { deployInitialSandboxAccounts } from '@aztec/aztec.js';
import { PrivateKey } from '@aztec/circuits.js';
import { deployL1Contracts } from '@aztec/ethereum';
import { createDebugLogger } from '@aztec/foundation/log';
import { retryUntil } from '@aztec/foundation/retry';

import http from 'http';
import { HDAccount, createPublicClient, http as httpViemTransport } from 'viem';
import { mnemonicToAccount } from 'viem/accounts';
import { foundry } from 'viem/chains';

import { createApiRouter } from './routes.js';
import { startHttpRpcServer } from './server.js';
import { github, splash } from './splash.js';

const { SERVER_PORT = 8080, MNEMONIC = 'test test test test test test test test test test test junk' } = process.env;
Expand Down Expand Up @@ -84,15 +83,7 @@ async function main() {
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);

const rpcServer = getHttpRpcServer(aztecRpcServer);

const app = rpcServer.getApp();
const apiRouter = createApiRouter(deployedL1Contracts);
app.use(apiRouter.routes());
app.use(apiRouter.allowedMethods());

const httpServer = http.createServer(app.callback());
httpServer.listen(SERVER_PORT);
startHttpRpcServer(aztecRpcServer, deployedL1Contracts, SERVER_PORT);
logger.info(`Aztec JSON RPC listening on port ${SERVER_PORT}`);
const accountStrings = [`Initial Accounts:\n\n`];

Expand Down
31 changes: 31 additions & 0 deletions yarn-project/aztec-sandbox/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getHttpRpcServer } from '@aztec/aztec-rpc';
import { DeployL1Contracts } from '@aztec/ethereum';
import { AztecRPC } from '@aztec/types';

import http from 'http';

import { createApiRouter } from './routes.js';

/**
* Creates an http server that forwards calls to the rpc server and starts it on the given port.
* @param aztecRpcServer - RPC server that answers queries to the created HTTP server.
* @param deployedL1Contracts - Info on L1 deployed contracts.
* @param port - Port to listen in.
* @returns A running http server.
*/
export function startHttpRpcServer(
aztecRpcServer: AztecRPC,
deployedL1Contracts: DeployL1Contracts,
port: string | number,
): http.Server {
const rpcServer = getHttpRpcServer(aztecRpcServer);

const app = rpcServer.getApp();
const apiRouter = createApiRouter(deployedL1Contracts);
app.use(apiRouter.routes());
app.use(apiRouter.allowedMethods());

const httpServer = http.createServer(app.callback());
httpServer.listen(port);
return httpServer;
}
5 changes: 5 additions & 0 deletions yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
"@aztec/archiver": "workspace:^",
"@aztec/aztec-node": "workspace:^",
"@aztec/aztec-rpc": "workspace:^",
"@aztec/aztec-sandbox": "workspace:^",
"@aztec/aztec.js": "workspace:^",
"@aztec/circuits.js": "workspace:^",
"@aztec/cli": "workspace:^",
"@aztec/ethereum": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/l1-artifacts": "workspace:^",
Expand All @@ -51,11 +53,13 @@
"koa": "^2.14.2",
"koa-static": "^5.0.0",
"levelup": "^5.1.1",
"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",
"puppeteer": "^20.9.0",
"string-argv": "^0.3.2",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tslib": "^2.4.0",
Expand All @@ -64,6 +68,7 @@
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.1.4",
"@types/lodash.compact": "^3.0.7",
"concurrently": "^7.6.0"
},
"files": [
Expand Down
74 changes: 74 additions & 0 deletions yarn-project/end-to-end/src/e2e_cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { AztecAddress, AztecRPCServer } from '@aztec/aztec-rpc';
import { startHttpRpcServer } from '@aztec/aztec-sandbox/http';
import { createDebugLogger } from '@aztec/aztec.js';
import { getProgram } from '@aztec/cli';
import { DebugLogger } from '@aztec/foundation/log';
import { AztecRPC } from '@aztec/types';

import stringArgv from 'string-argv';
import { format } from 'util';

import { setup } from './fixtures/utils.js';

const HTTP_PORT = 9009;

// Spins up a new http server wrapping the set up rpc server, and tests cli commands against it
describe('cli', () => {
let cli: ReturnType<typeof getProgram>;
let http: ReturnType<typeof startHttpRpcServer>;
let debug: DebugLogger;
let aztecNode: AztecNodeService | undefined;
let aztecRpcServer: AztecRPC;

// All logs emitted by the cli will be collected here, and reset between tests
const logs: string[] = [];

beforeAll(async () => {
debug = createDebugLogger('aztec:e2e_cli');
const context = await setup(2);
debug(`Environment set up`);
const { deployL1ContractsValues } = context;
({ aztecNode, aztecRpcServer } = context);
http = startHttpRpcServer(aztecRpcServer, deployL1ContractsValues, HTTP_PORT);
debug(`HTTP RPC server started in port ${HTTP_PORT}`);
const log = (...args: any[]) => {
logs.push(format(...args));
debug(...args);
};
cli = getProgram(log, debug);
});

afterAll(async () => {
http.close();
await aztecNode?.stop();
await (aztecRpcServer as AztecRPCServer).stop();
});

beforeEach(() => {
logs.splice(0);
});

// Run a command on the CLI
const run = (cmd: string) =>
cli.parseAsync(stringArgv(cmd, 'node', 'dest/bin/index.js').concat(['--rpc-url', `http://localhost:${HTTP_PORT}`]));

// Returns first match across all logs collected so far
const findInLogs = (regex: RegExp) => {
for (const log of logs) {
const match = regex.exec(log);
if (match) return match;
}
};

it('creates an account', async () => {
const accountsBefore = await aztecRpcServer.getAccounts();
await run(`create-account`);
const newAddress = findInLogs(/Address:\s+(?<address>0x[a-fA-F0-9]+)/)?.groups?.address;
expect(newAddress).toBeDefined();

const accountsAfter = await aztecRpcServer.getAccounts();
const expectedAccounts = [...accountsBefore.map(a => a.address), AztecAddress.fromString(newAddress!)];
expect(accountsAfter.map(a => a.address)).toEqual(expectedAccounts);
});
});
6 changes: 6 additions & 0 deletions yarn-project/end-to-end/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
{
"path": "../aztec-rpc"
},
{
"path": "../aztec-sandbox"
},
{
"path": "../aztec.js"
},
{
"path": "../circuits.js"
},
{
"path": "../aztec-cli"
},
{
"path": "../ethereum"
},
Expand Down
Loading