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

feat: Create standalone instances of Aztec Node and RPC Server #2486

Closed
wants to merge 26 commits into from
Closed
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
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
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
23 changes: 15 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,19 @@ export function getConfigEnvVars(): ArchiverConfig {
INBOX_CONTRACT_ADDRESS,
REGISTRY_CONTRACT_ADDRESS,
} = process.env;
const addresses = new L1ContractAddresses(
ROLLUP_CONTRACT_ADDRESS ? EthAddress.fromString(ROLLUP_CONTRACT_ADDRESS) : EthAddress.ZERO,
REGISTRY_CONTRACT_ADDRESS ? EthAddress.fromString(REGISTRY_CONTRACT_ADDRESS) : EthAddress.ZERO,
INBOX_CONTRACT_ADDRESS ? EthAddress.fromString(INBOX_CONTRACT_ADDRESS) : EthAddress.ZERO,
undefined,
CONTRACT_DEPLOYMENT_EMITTER_ADDRESS ? EthAddress.fromString(CONTRACT_DEPLOYMENT_EMITTER_ADDRESS) : 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
4 changes: 2 additions & 2 deletions yarn-project/aztec-node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ 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", "start"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have been using entrypoint pretty liberally, but I'd push for having just yarn as entrypoint, and start as its command. Not really important though unless we try to make this consistent across the board.

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
68 changes: 68 additions & 0 deletions yarn-project/aztec-node/src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env -S node --no-warnings
import { createDebugLogger } from '@aztec/foundation/log';
import { createAztecNodeRpcServer } from '@aztec/types';

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

import { AztecNodeConfig, AztecNodeService, getConfigEnvVars } 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);
});
2 changes: 0 additions & 2 deletions yarn-project/aztec-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
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';
Loading