Skip to content

Commit

Permalink
feat: Standalone Aztec Node and RPC Server (#2522)
Browse files Browse the repository at this point in the history
This PR addresses the comments of PR
[2486](#2486). The
feature set here is:

1. Some refactoring of packages dependencies and l1 contract address
configuration.
2. Entrypoints to start standalone instances of Aztec Node and Aztec RPC
Server.
3. Some initial terraform for Aztec Node deployments.

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [ ] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
PhilWindle authored Sep 26, 2023
1 parent 87b8080 commit 8e355bc
Show file tree
Hide file tree
Showing 69 changed files with 1,020 additions and 370 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
build/
.idea
cmake-build-debug
.bootstrapped
.terraform
.bootstrapped
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"nodemon": "^3.0.1",
"typedoc": "^0.25.1",
"typedoc-plugin-markdown": "^3.16.0",
"typescript": "^4.7.2"
"typescript": "^5.0.4"
},
"browserslist": {
"production": [
Expand Down
8 changes: 4 additions & 4 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8040,10 +8040,10 @@ typedoc@^0.25.1:
minimatch "^9.0.3"
shiki "^0.14.1"

typescript@^4.7.2:
version "4.9.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78"
integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==
typescript@^5.0.4:
version "5.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==

ua-parser-js@^0.7.30:
version "0.7.32"
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
const archiverStore = new MemoryArchiverStore();
const archiver = new Archiver(
publicClient,
config.rollupContract,
config.inboxContract,
config.registryContract,
config.contractDeploymentEmitterContract,
config.l1Contracts.rollupAddress,
config.l1Contracts.inboxAddress,
config.l1Contracts.registryAddress,
config.l1Contracts.contractDeploymentEmitterAddress,
config.searchStartBlock,
archiverStore,
config.archiverPollingIntervalMS,
Expand Down
27 changes: 19 additions & 8 deletions yarn-project/archiver/src/archiver/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { L1ContractAddresses } from '@aztec/ethereum';
import { EthAddress } from '@aztec/foundation/eth-address';
import { L1Addresses } from '@aztec/types';

/**
* There are 2 polling intervals used in this configuration. The first is the archiver polling interval, archiverPollingIntervalMS.
Expand All @@ -11,7 +11,7 @@ import { L1Addresses } from '@aztec/types';
/**
* The archiver configuration.
*/
export interface ArchiverConfig extends L1Addresses {
export interface ArchiverConfig {
/**
* The url of the Ethereum RPC node.
*/
Expand All @@ -36,6 +36,11 @@ export interface ArchiverConfig extends L1Addresses {
* Eth block from which we start scanning for L2Blocks.
*/
searchStartBlock: number;

/**
* The deployed L1 contract addresses
*/
l1Contracts: L1ContractAddresses;
}

/**
Expand All @@ -55,17 +60,23 @@ export function getConfigEnvVars(): ArchiverConfig {
INBOX_CONTRACT_ADDRESS,
REGISTRY_CONTRACT_ADDRESS,
} = process.env;
// Populate the relevant addresses for use by the archiver.
const addresses: L1ContractAddresses = {
rollupAddress: ROLLUP_CONTRACT_ADDRESS ? EthAddress.fromString(ROLLUP_CONTRACT_ADDRESS) : EthAddress.ZERO,
registryAddress: REGISTRY_CONTRACT_ADDRESS ? EthAddress.fromString(REGISTRY_CONTRACT_ADDRESS) : EthAddress.ZERO,
inboxAddress: INBOX_CONTRACT_ADDRESS ? EthAddress.fromString(INBOX_CONTRACT_ADDRESS) : EthAddress.ZERO,
outboxAddress: EthAddress.ZERO,
contractDeploymentEmitterAddress: CONTRACT_DEPLOYMENT_EMITTER_ADDRESS
? EthAddress.fromString(CONTRACT_DEPLOYMENT_EMITTER_ADDRESS)
: EthAddress.ZERO,
decoderHelperAddress: EthAddress.ZERO,
};
return {
rpcUrl: ETHEREUM_HOST || 'http://127.0.0.1:8545/',
archiverPollingIntervalMS: ARCHIVER_POLLING_INTERVAL_MS ? +ARCHIVER_POLLING_INTERVAL_MS : 1_000,
viemPollingIntervalMS: ARCHIVER_VIEM_POLLING_INTERVAL_MS ? +ARCHIVER_VIEM_POLLING_INTERVAL_MS : 1_000,
rollupContract: ROLLUP_CONTRACT_ADDRESS ? EthAddress.fromString(ROLLUP_CONTRACT_ADDRESS) : EthAddress.ZERO,
registryContract: REGISTRY_CONTRACT_ADDRESS ? EthAddress.fromString(REGISTRY_CONTRACT_ADDRESS) : EthAddress.ZERO,
inboxContract: INBOX_CONTRACT_ADDRESS ? EthAddress.fromString(INBOX_CONTRACT_ADDRESS) : EthAddress.ZERO,
contractDeploymentEmitterContract: CONTRACT_DEPLOYMENT_EMITTER_ADDRESS
? EthAddress.fromString(CONTRACT_DEPLOYMENT_EMITTER_ADDRESS)
: EthAddress.ZERO,
searchStartBlock: SEARCH_START_BLOCK ? +SEARCH_START_BLOCK : 0,
apiKey: API_KEY,
l1Contracts: addresses,
};
}
17 changes: 5 additions & 12 deletions yarn-project/archiver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ const log = createDebugLogger('aztec:archiver');
// eslint-disable-next-line require-await
async function main() {
const config = getConfigEnvVars();
const {
rpcUrl,
rollupContract,
inboxContract,
registryContract,
contractDeploymentEmitterContract,
searchStartBlock,
} = config;
const { rpcUrl, l1Contracts, searchStartBlock } = config;

const publicClient = createPublicClient({
chain: localhost,
Expand All @@ -35,10 +28,10 @@ async function main() {

const archiver = new Archiver(
publicClient,
rollupContract,
inboxContract,
registryContract,
contractDeploymentEmitterContract,
l1Contracts.rollupAddress,
l1Contracts.inboxAddress,
l1Contracts.registryAddress,
l1Contracts.contractDeploymentEmitterAddress,
searchStartBlock,
archiverStore,
);
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/aztec-node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ RUN yarn cache clean
RUN yarn workspaces focus --production > /dev/null

FROM node:18-alpine
COPY --from=builder /usr/src/yarn-project/aztec-node /usr/src/yarn-project/aztec-node
COPY --from=builder /usr/src /usr/src
WORKDIR /usr/src/yarn-project/aztec-node
ENTRYPOINT ["yarn"]
ENTRYPOINT ["yarn"]
CMD [ "start" ]
5 changes: 5 additions & 0 deletions yarn-project/aztec-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "dest/index.js",
"type": "module",
"exports": "./dest/index.js",
"bin": "./dest/bin/index.js",
"typedocOptions": {
"entryPoints": [
"./src/index.ts"
Expand All @@ -12,6 +13,7 @@
"tsconfig": "./tsconfig.json"
},
"scripts": {
"start": "node --no-warnings ./dest/bin",
"build": "yarn clean && tsc -b",
"build:dev": "tsc -b --watch",
"clean": "rm -rf ./dest .tsbuildinfo",
Expand All @@ -33,13 +35,16 @@
"dependencies": {
"@aztec/archiver": "workspace:^",
"@aztec/circuits.js": "workspace:^",
"@aztec/ethereum": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/l1-artifacts": "workspace:^",
"@aztec/merkle-tree": "workspace:^",
"@aztec/p2p": "workspace:^",
"@aztec/sequencer-client": "workspace:^",
"@aztec/types": "workspace:^",
"@aztec/world-state": "workspace:^",
"koa": "^2.14.2",
"koa-router": "^12.0.0",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
26 changes: 12 additions & 14 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Archiver } from '@aztec/archiver';
import {
CONTRACT_TREE_HEIGHT,
CircuitsWasm,
EthAddress,
Fr,
GlobalVariables,
HistoricBlockData,
L1_TO_L2_MSG_TREE_HEIGHT,
PRIVATE_DATA_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { L1ContractAddresses } from '@aztec/ethereum';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { createDebugLogger } from '@aztec/foundation/log';
import { InMemoryTxPool, P2P, createP2PClient } from '@aztec/p2p';
Expand Down Expand Up @@ -57,6 +57,7 @@ export const createMemDown = () => (memdown as any)() as MemDown<any, any>;
*/
export class AztecNodeService implements AztecNode {
constructor(
protected config: AztecNodeConfig,
protected p2pClient: P2P,
protected blockSource: L2BlockSource,
protected encryptedLogsSource: L2LogsSource,
Expand All @@ -83,7 +84,7 @@ export class AztecNodeService implements AztecNode {

// we identify the P2P transaction protocol by using the rollup contract address.
// this may well change in future
config.transactionProtocol = `/aztec/tx/${config.rollupContract.toString()}`;
config.transactionProtocol = `/aztec/tx/${config.l1Contracts.rollupAddress.toString()}`;

// create the tx pool and the p2p client, which will need the l2 block source
const p2pClient = await createP2PClient(config, new InMemoryTxPool(), archiver);
Expand All @@ -107,6 +108,7 @@ export class AztecNodeService implements AztecNode {
archiver,
);
return new AztecNodeService(
config,
p2pClient,
archiver,
archiver,
Expand All @@ -122,6 +124,14 @@ export class AztecNodeService implements AztecNode {
);
}

/**
* Method to return the currently deployed L1 contract addresses.
* @returns - The currently deployed L1 contract addresses.
*/
public getL1ContractAddresses(): Promise<L1ContractAddresses> {
return Promise.resolve(this.config.l1Contracts);
}

/**
* Method to determine if the node is ready to accept transactions.
* @returns - Flag indicating the readiness for tx submission.
Expand Down Expand Up @@ -173,18 +183,6 @@ export class AztecNodeService implements AztecNode {
return Promise.resolve(this.chainId);
}

/**
* Method to fetch the rollup contract address at the base-layer.
* @returns The rollup address.
*/
public getRollupAddress(): Promise<EthAddress> {
return this.blockSource.getRollupAddress();
}

public getRegistryAddress(): Promise<EthAddress> {
return this.blockSource.getRegistryAddress();
}

/**
* Get the extended contract data for this contract.
* @param contractAddress - The contract data address.
Expand Down
67 changes: 67 additions & 0 deletions yarn-project/aztec-node/src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env -S node --no-warnings
import { createDebugLogger } from '@aztec/foundation/log';

import http from 'http';
import Koa from 'koa';
import Router from 'koa-router';

import { AztecNodeConfig, AztecNodeService, getConfigEnvVars, createAztecNodeRpcServer } from '../index.js';

const { SERVER_PORT = 8081, API_PREFIX = '' } = process.env;

const logger = createDebugLogger('aztec:node');

/**
* Creates the node from provided config
*/
async function createAndDeployAztecNode() {
const aztecNodeConfig: AztecNodeConfig = { ...getConfigEnvVars() };

return await AztecNodeService.createAndSync(aztecNodeConfig);
}

/**
* Creates a router for helper API endpoints of the Aztec RPC Server.
* @param apiPrefix - The prefix to use for all api requests
* @returns - The router for handling status requests.
*/
export function createStatusRouter(apiPrefix: string) {
const router = new Router({ prefix: `${apiPrefix}` });
router.get('/status', (ctx: Koa.Context) => {
ctx.status = 200;
});
return router;
}

/**
* Create and start a new Aztec Node HTTP Server
*/
async function main() {
logger.info(`Setting up Aztec Node...`);

const aztecNode = await createAndDeployAztecNode();

const shutdown = async () => {
logger.info('Shutting down...');
await aztecNode.stop();
process.exit(0);
};

process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);

const rpcServer = createAztecNodeRpcServer(aztecNode);
const app = rpcServer.getApp(API_PREFIX);
const apiRouter = createStatusRouter(API_PREFIX);
app.use(apiRouter.routes());
app.use(apiRouter.allowedMethods());

const httpServer = http.createServer(app.callback());
httpServer.listen(+SERVER_PORT);
logger.info(`Aztec Node JSON-RPC Server listening on port ${SERVER_PORT}`);
}

main().catch(err => {
logger.error(err);
process.exit(1);
});
3 changes: 1 addition & 2 deletions yarn-project/aztec-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './aztec-node/config.js';
export * from './aztec-node/server.js';
export * from './rpc/http_rpc_server.js';
export * from './rpc/http_rpc_client.js';
export * from './aztec-node/http_rpc_server.js';
Loading

0 comments on commit 8e355bc

Please sign in to comment.