-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathstart_node.ts
162 lines (142 loc) · 6.26 KB
/
start_node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { getInitialTestAccounts } from '@aztec/accounts/testing';
import { type AztecNodeConfig, aztecNodeConfigMappings, getConfigEnvVars } from '@aztec/aztec-node';
import { NULL_KEY } from '@aztec/ethereum';
import type { NamespacedApiHandlers } from '@aztec/foundation/json-rpc/server';
import type { LogFn } from '@aztec/foundation/log';
import { AztecNodeApiSchema, type PXE } from '@aztec/stdlib/interfaces/client';
import { P2PApiSchema } from '@aztec/stdlib/interfaces/server';
import {
type TelemetryClientConfig,
initTelemetryClient,
telemetryClientConfigMappings,
} from '@aztec/telemetry-client';
import { getGenesisValues } from '@aztec/world-state/testing';
import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';
import { createAztecNode, deployContractsToL1 } from '../../sandbox/index.js';
import { getL1Config } from '../get_l1_config.js';
import { extractNamespacedOptions, extractRelevantOptions } from '../util.js';
export async function startNode(
options: any,
signalHandlers: (() => Promise<void>)[],
services: NamespacedApiHandlers,
userLog: LogFn,
): Promise<{ config: AztecNodeConfig }> {
// options specifically namespaced with --node.<option>
const nodeSpecificOptions = extractNamespacedOptions(options, 'node');
// All options set from environment variables
const configFromEnvVars = getConfigEnvVars();
// Extract relevant options from command line arguments
const relevantOptions = extractRelevantOptions(options, aztecNodeConfigMappings, 'node');
// All options that are relevant to the Aztec Node
let nodeConfig: AztecNodeConfig = {
...configFromEnvVars,
...relevantOptions,
};
if (options.proverNode) {
userLog(`Running a Prover Node within a Node is not yet supported`);
process.exit(1);
}
const initialFundedAccounts = nodeConfig.testAccounts ? await getInitialTestAccounts() : [];
const { genesisBlockHash, genesisArchiveRoot, prefilledPublicData } = await getGenesisValues(
initialFundedAccounts.map(a => a.address),
);
// Deploy contracts if needed
if (nodeSpecificOptions.deployAztecContracts || nodeSpecificOptions.deployAztecContractsSalt) {
let account;
if (nodeSpecificOptions.publisherPrivateKey) {
account = privateKeyToAccount(nodeSpecificOptions.publisherPrivateKey);
} else if (options.l1Mnemonic) {
account = mnemonicToAccount(options.l1Mnemonic);
} else {
throw new Error('--node.publisherPrivateKey or --l1-mnemonic is required to deploy L1 contracts');
}
// REFACTOR: We should not be calling a method from sandbox on the prod start flow
await deployContractsToL1(nodeConfig, account!, undefined, {
assumeProvenThroughBlockNumber: nodeSpecificOptions.assumeProvenThroughBlockNumber,
salt: nodeSpecificOptions.deployAztecContractsSalt,
genesisBlockHash,
genesisArchiveRoot,
});
}
// If not deploying, validate that any addresses and config provided are correct.
else {
if (!nodeConfig.l1Contracts.registryAddress || nodeConfig.l1Contracts.registryAddress.isZero()) {
throw new Error('L1 registry address is required to start Aztec Node without --deploy-aztec-contracts option');
}
const { addresses, config } = await getL1Config(
nodeConfig.l1Contracts.registryAddress,
nodeConfig.l1RpcUrls,
nodeConfig.l1ChainId,
);
// TODO(#12272): will clean this up.
nodeConfig = {
...nodeConfig,
l1Contracts: {
...addresses,
slashFactoryAddress: nodeConfig.l1Contracts.slashFactoryAddress,
},
...config,
};
}
// if no publisher private key, then use l1Mnemonic
if (!options.archiver) {
// expect archiver url in node config
const archiverUrl = nodeConfig.archiverUrl;
if (!archiverUrl) {
userLog('Archiver Service URL is required to start Aztec Node without --archiver option');
throw new Error('Archiver Service URL is required to start Aztec Node without --archiver option');
}
nodeConfig.archiverUrl = archiverUrl;
}
if (!options.sequencer) {
nodeConfig.disableValidator = true;
} else {
const sequencerConfig = {
...configFromEnvVars,
...extractNamespacedOptions(options, 'sequencer'),
};
let account;
if (!sequencerConfig.publisherPrivateKey || sequencerConfig.publisherPrivateKey === NULL_KEY) {
if (sequencerConfig.validatorPrivateKey) {
sequencerConfig.publisherPrivateKey = sequencerConfig.validatorPrivateKey as `0x${string}`;
} else if (!options.l1Mnemonic) {
userLog(
'--sequencer.publisherPrivateKey or --l1-mnemonic is required to start Aztec Node with --sequencer option',
);
throw new Error('Private key or Mnemonic is required to start Aztec Node with --sequencer option');
} else {
account = mnemonicToAccount(options.l1Mnemonic);
const privKey = account.getHdKey().privateKey;
sequencerConfig.publisherPrivateKey = `0x${Buffer.from(privKey!).toString('hex')}`;
}
}
nodeConfig.publisherPrivateKey = sequencerConfig.publisherPrivateKey;
}
if (nodeConfig.p2pEnabled) {
// ensure bootstrapNodes is an array
if (nodeConfig.bootstrapNodes && typeof nodeConfig.bootstrapNodes === 'string') {
nodeConfig.bootstrapNodes = (nodeConfig.bootstrapNodes as string).split(',');
}
}
const telemetryConfig = extractRelevantOptions<TelemetryClientConfig>(options, telemetryClientConfigMappings, 'tel');
const telemetry = initTelemetryClient(telemetryConfig);
// Create and start Aztec Node
const node = await createAztecNode(nodeConfig, { telemetry }, { prefilledPublicData });
// Add node and p2p to services list
services.node = [node, AztecNodeApiSchema];
services.p2p = [node.getP2P(), P2PApiSchema];
// Add node stop function to signal handlers
signalHandlers.push(node.stop.bind(node));
// Add a PXE client that connects to this node if requested
let pxe: PXE | undefined;
if (options.pxe) {
const { addPXE } = await import('./start_pxe.js');
({ pxe } = await addPXE(options, signalHandlers, services, userLog, { node }));
}
// Add a txs bot if requested
if (options.bot) {
const { addBot } = await import('./start_bot.js');
await addBot(options, signalHandlers, services, { pxe, node, telemetry });
}
return { config: nodeConfig };
}