Skip to content

Commit

Permalink
fix: update sandbox setup
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 3, 2024
1 parent efb2c07 commit a997085
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 16 deletions.
4 changes: 3 additions & 1 deletion yarn-project/aztec.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@
"@aztec/circuits.js": "workspace:^",
"@aztec/ethereum": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/l1-artifacts": "workspace:^",
"@aztec/protocol-contracts": "workspace:^",
"@aztec/types": "workspace:^",
"axios": "^1.7.2",
"tslib": "^2.4.0"
"tslib": "^2.4.0",
"viem": "^2.7.15"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { ContractDeployer } from './deployment/index.js';

export {
AztecAddressLike,
Watcher,
CheatCodes,
EthAddressLike,
EthCheatCodes,
Expand Down
14 changes: 14 additions & 0 deletions yarn-project/aztec.js/src/utils/cheat_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export class EthCheatCodes {
return await (await fetch(this.rpcUrl, content)).json();
}

/**
* Get the auto mine status of the underlying chain
* @returns True if automine is on, false otherwise
*/
public async isAutoMining(): Promise<boolean> {
try {
const res = await this.rpcCall('anvil_getAutomine', []);
return res.result;
} catch (err) {
this.logger.error(`Calling "anvil_getAutomine" failed with:`, err);
}
return false;
}

/**
* Get the current blocknumber
* @returns The current block number
Expand Down
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './cheat_codes.js';
export * from './authwit.js';
export * from './pxe.js';
export * from './account.js';
export * from './watcher.js';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { RollupAbi } from '@aztec/l1-artifacts';
import { type GetContractReturnType, type HttpTransport, type PublicClient, getAddress, getContract } from 'viem';
import type * as chains from 'viem/chains';

/**
* Represents a watcher for a rollup contract. It periodically checks if a slot is filled and mines if necessary.
*/
export class Watcher {
private rollup: GetContractReturnType<typeof RollupAbi, PublicClient<HttpTransport, chains.Chain>>;

Expand All @@ -27,13 +30,20 @@ export class Watcher {
this.logger.info(`Watcher created for rollup at ${rollupAddress}`);
}

start() {
async start() {
if (this.filledRunningPromise) {
throw new Error('Watcher already watching for filled slot');
}
this.filledRunningPromise = new RunningPromise(() => this.mineIfSlotFilled(), 1000);
this.filledRunningPromise.start();
this.logger.info(`Watcher started`);

const isAutoMining = await this.cheatcodes.isAutoMining();

if (isAutoMining) {
this.filledRunningPromise = new RunningPromise(() => this.mineIfSlotFilled(), 1000);
this.filledRunningPromise.start();
this.logger.info(`Watcher started`);
} else {
this.logger.info(`Watcher not started because not auto mining`);
}
}

async stop() {
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec.js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
{
"path": "../foundation"
},
{
"path": "../l1-artifacts"
},
{
"path": "../protocol-contracts"
},
Expand Down
1 change: 1 addition & 0 deletions yarn-project/aztec/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge
const { aztecNodeConfig, node, pxe, stop } = await createSandbox({
enableGas: sandboxOptions.enableGas,
l1Mnemonic: options.l1Mnemonic,
l1RpcUrl: options.l1RpcUrl,
});

// Deploy test accounts by default
Expand Down
18 changes: 16 additions & 2 deletions yarn-project/aztec/src/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S node --no-warnings
import { type AztecNodeConfig, AztecNodeService, getConfigEnvVars } from '@aztec/aztec-node';
import { SignerlessWallet } from '@aztec/aztec.js';
import { EthCheatCodes, SignerlessWallet, Watcher } from '@aztec/aztec.js';
import { DefaultMultiCallEntrypoint } from '@aztec/aztec.js/entrypoint';
import { type AztecNode } from '@aztec/circuit-types';
import { deployCanonicalAuthRegistry, deployCanonicalKeyRegistry, deployCanonicalL2FeeJuice } from '@aztec/cli/misc';
Expand Down Expand Up @@ -166,8 +166,21 @@ export async function createSandbox(config: Partial<SandboxConfig> = {}) {
aztecNodeConfig.validatorPrivateKey = `0x${Buffer.from(privKey!).toString('hex')}`;
}

let watcher: Watcher | undefined = undefined;
if (!aztecNodeConfig.p2pEnabled) {
await deployContractsToL1(aztecNodeConfig, hdAccount);
const l1ContractAddresses = await deployContractsToL1(aztecNodeConfig, hdAccount);

const chain = aztecNodeConfig.l1RpcUrl
? createEthereumChain(aztecNodeConfig.l1RpcUrl, aztecNodeConfig.l1ChainId)
: { chainInfo: localAnvil };

const publicClient = createPublicClient({
chain: chain.chainInfo,
transport: httpViemTransport(aztecNodeConfig.l1RpcUrl),
});

watcher = new Watcher(new EthCheatCodes(aztecNodeConfig.l1RpcUrl), l1ContractAddresses.rollupAddress, publicClient);
await watcher.start();
}

const client = await createAndStartTelemetryClient(getTelemetryClientConfig());
Expand All @@ -191,6 +204,7 @@ export async function createSandbox(config: Partial<SandboxConfig> = {}) {
const stop = async () => {
await pxe.stop();
await node.stop();
await watcher?.stop();
};

return { node, pxe, aztecNodeConfig, stop };
Expand Down
1 change: 0 additions & 1 deletion yarn-project/end-to-end/src/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './fixtures.js';
export * from './logging.js';
export * from './utils.js';
export * from './watcher.js';
6 changes: 3 additions & 3 deletions yarn-project/end-to-end/src/fixtures/snapshot_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
type PXE,
SignerlessWallet,
type Wallet,
Watcher,
} from '@aztec/aztec.js';
import { deployInstance, registerContractClass } from '@aztec/aztec.js/deployment';
import { DefaultMultiCallEntrypoint } from '@aztec/aztec.js/entrypoint';
Expand All @@ -41,7 +42,6 @@ import { getACVMConfig } from './get_acvm_config.js';
import { getBBConfig } from './get_bb_config.js';
import { setupL1Contracts } from './setup_l1_contracts.js';
import { deployCanonicalAuthRegistry, deployCanonicalKeyRegistry, getPrivateKeyFromIndex } from './utils.js';
import { Watcher } from './watcher.js';

export type SubsystemsContext = {
anvil: Anvil;
Expand Down Expand Up @@ -346,7 +346,7 @@ async function setupFromFresh(
deployL1ContractsValues.l1ContractAddresses.rollupAddress,
deployL1ContractsValues.publicClient,
);
watcher.start();
await watcher.start();

logger.verbose('Creating pxe...');
const pxeConfig = getPXEServiceConfig();
Expand Down Expand Up @@ -424,7 +424,7 @@ async function setupFromState(statePath: string, logger: Logger): Promise<Subsys
aztecNodeConfig.l1Contracts.rollupAddress,
publicClient,
);
watcher.start();
await watcher.start();

logger.verbose('Creating aztec node...');
const telemetry = await createAndStartTelemetryClient(getTelemetryConfig());
Expand Down
7 changes: 2 additions & 5 deletions yarn-project/end-to-end/src/fixtures/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type SentTx,
SignerlessWallet,
type Wallet,
Watcher,
createAztecNodeClient,
createDebugLogger,
createPXEClient,
Expand Down Expand Up @@ -87,7 +88,6 @@ import { MNEMONIC } from './fixtures.js';
import { getACVMConfig } from './get_acvm_config.js';
import { getBBConfig } from './get_bb_config.js';
import { isMetricsLoggingRequested, setupMetricsLogger } from './logging.js';
import { Watcher } from './watcher.js';

export { deployAndInitializeTokenAndBridgeContracts } from '../shared/cross_chain_test_harness.js';

Expand Down Expand Up @@ -456,10 +456,7 @@ export async function setup(
deployL1ContractsValues.publicClient,
);

// If we are NOT using wall time, we should start the watcher to jump in time as needed.
if (!opts.l1BlockTime) {
watcher.start();
}
await watcher.start();

const wallets = numberOfAccounts > 0 ? await createAccounts(pxe, numberOfAccounts) : [];
const cheatCodes = CheatCodes.create(config.l1RpcUrl, pxe!);
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ __metadata:
"@aztec/circuits.js": "workspace:^"
"@aztec/ethereum": "workspace:^"
"@aztec/foundation": "workspace:^"
"@aztec/l1-artifacts": "workspace:^"
"@aztec/protocol-contracts": "workspace:^"
"@aztec/types": "workspace:^"
"@jest/globals": ^29.5.0
Expand All @@ -204,6 +205,7 @@ __metadata:
tslib: ^2.4.0
typescript: ^5.0.4
util: ^0.12.5
viem: ^2.7.15
webpack: ^5.88.2
webpack-cli: ^5.1.4
languageName: unknown
Expand Down

0 comments on commit a997085

Please sign in to comment.