Skip to content

Commit

Permalink
refactor: simplify global variable builder setup
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Aug 14, 2024
1 parent 69bde53 commit 6799199
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 164 deletions.
10 changes: 2 additions & 8 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,7 @@ import { getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice';
import { getCanonicalInstanceDeployer } from '@aztec/protocol-contracts/instance-deployer';
import { getCanonicalKeyRegistryAddress } from '@aztec/protocol-contracts/key-registry';
import { getCanonicalMultiCallEntrypointAddress } from '@aztec/protocol-contracts/multi-call-entrypoint';
import {
AggregateTxValidator,
DataTxValidator,
type GlobalVariableBuilder,
SequencerClient,
getGlobalVariableBuilder,
} from '@aztec/sequencer-client';
import { AggregateTxValidator, DataTxValidator, GlobalVariableBuilder, SequencerClient } from '@aztec/sequencer-client';
import { PublicProcessorFactory, WASMSimulator, createSimulationProvider } from '@aztec/simulator';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
Expand Down Expand Up @@ -192,7 +186,7 @@ export class AztecNodeService implements AztecNode {
sequencer,
ethereumChain.chainInfo.id,
config.version,
getGlobalVariableBuilder(config),
new GlobalVariableBuilder(config),
store,
txValidator,
telemetry,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/sequencer-client/src/client/sequencer-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type WorldStateSynchronizer } from '@aztec/world-state';

import { BlockBuilderFactory } from '../block_builder/index.js';
import { type SequencerClientConfig } from '../config.js';
import { getGlobalVariableBuilder } from '../global_variable_builder/index.js';
import { GlobalVariableBuilder } from '../global_variable_builder/index.js';
import { getL1Publisher } from '../publisher/index.js';
import { Sequencer, type SequencerConfig } from '../sequencer/index.js';
import { TxValidatorFactory } from '../tx_validator/tx_validator_factory.js';
Expand Down Expand Up @@ -41,7 +41,7 @@ export class SequencerClient {
telemetryClient: TelemetryClient,
) {
const publisher = getL1Publisher(config, telemetryClient);
const globalsBuilder = getGlobalVariableBuilder(config);
const globalsBuilder = new GlobalVariableBuilder(config);
const merkleTreeDb = worldStateSynchronizer.getLatest();

const publicProcessorFactory = new PublicProcessorFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,47 @@ import {
GasFees,
GlobalVariables,
} from '@aztec/circuits.js';
import { type L1ReaderConfig, createEthereumChain } from '@aztec/ethereum';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { RollupAbi } from '@aztec/l1-artifacts';

import {
type GetContractReturnType,
type HttpTransport,
type PublicClient,
createPublicClient,
getAddress,
getContract,
http,
} from 'viem';
import type * as chains from 'viem/chains';

/**
* Reads values from L1 state that is used for the global values.
* Simple global variables builder.
*/
export interface L1GlobalReader {
/**
* Fetches the version of the rollup contract.
* @returns The version of the rollup contract.
*/
getVersion(): Promise<bigint>;
/**
* Gets the chain id.
* @returns The chain id.
*/
getChainId(): Promise<bigint>;

/**
* Gets the current L1 time.
* @returns The current L1 time.
*/
getL1CurrentTime(): Promise<bigint>;
export class GlobalVariableBuilder {
private log = createDebugLogger('aztec:sequencer:global_variable_builder');

/**
* Gets the current slot.
* @returns The current slot.
*/
getCurrentSlot(): Promise<bigint>;
private rollupContract: GetContractReturnType<typeof RollupAbi, PublicClient<HttpTransport, chains.Chain>>;
private publicClient: PublicClient<HttpTransport, chains.Chain>;

/**
* Get the slot for a specific timestamp.
* @param timestamp - The timestamp to get the slot for.
*/
getSlotAt(timestamp: readonly [bigint]): Promise<bigint>;
constructor(config: L1ReaderConfig) {
const { l1RpcUrl, l1ChainId: chainId, l1Contracts } = config;

/**
* Gets the timestamp for a slot
* @param slot - The slot to get the timestamp for.
* @returns The timestamp for the slot.
*/
getTimestampForSlot(slot: readonly [bigint]): Promise<bigint>;
}
const chain = createEthereumChain(l1RpcUrl, chainId);

/**
* Builds global variables from L1 state.
*/
export interface GlobalVariableBuilder {
/**
* Builds global variables.
* @param blockNumber - The block number to build global variables for.
* @param coinbase - The address to receive block reward.
* @param feeRecipient - The address to receive fees.
* @returns The global variables for the given block number.
*/
buildGlobalVariables(blockNumber: Fr, coinbase: EthAddress, feeRecipient: AztecAddress): Promise<GlobalVariables>;
}
this.publicClient = createPublicClient({
chain: chain.chainInfo,
transport: http(chain.rpcUrl),
});

/**
* Simple test implementation of a builder that uses the minimum time possible for the global variables.
* Also uses a "hack" to make use of the warp cheatcode that manipulates time on Aztec.
*/
export class SimpleTestGlobalVariableBuilder implements GlobalVariableBuilder {
private log = createDebugLogger('aztec:sequencer:simple_test_global_variable_builder');
constructor(private readonly reader: L1GlobalReader) {}
this.rollupContract = getContract({
address: getAddress(l1Contracts.rollupAddress.toString()),
abi: RollupAbi,
client: this.publicClient,
});
}

/**
* Simple builder of global variables that use the minimum time possible.
Expand All @@ -83,18 +59,18 @@ export class SimpleTestGlobalVariableBuilder implements GlobalVariableBuilder {
coinbase: EthAddress,
feeRecipient: AztecAddress,
): Promise<GlobalVariables> {
// Not just the current slot, the slot of the next block.
const ts = (await this.reader.getL1CurrentTime()) + BigInt(ETHEREUM_SLOT_DURATION);
const version = new Fr(await this.rollupContract.read.VERSION());
const chainId = new Fr(this.publicClient.chain.id);

const ts = (await this.publicClient.getBlock()).timestamp;

const slot = await this.reader.getSlotAt([ts]);
const timestamp = await this.reader.getTimestampForSlot([slot]);
// Not just the current slot, the slot of the next block.
const slot = await this.rollupContract.read.getSlotAt([ts + BigInt(ETHEREUM_SLOT_DURATION)]);
const timestamp = await this.rollupContract.read.getTimestampForSlot([slot]);

const slotFr = new Fr(slot);
const timestampFr = new Fr(timestamp);

const version = new Fr(await this.reader.getVersion());
const chainId = new Fr(await this.reader.getChainId());

const gasFees = GasFees.default();
const globalVariables = new GlobalVariables(
chainId,
Expand Down
15 changes: 0 additions & 15 deletions yarn-project/sequencer-client/src/global_variable_builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
import { type L1ReaderConfig } from '@aztec/ethereum';

import { type GlobalVariableBuilder, SimpleTestGlobalVariableBuilder } from './global_builder.js';
import { ViemReader } from './viem-reader.js';

export { SimpleTestGlobalVariableBuilder as SimpleGlobalVariableBuilder } from './global_builder.js';
export { GlobalVariableBuilder } from './global_builder.js';

/**
* Returns a new instance of the global variable builder.
* @param config - Configuration to initialize the builder.
* @returns A new instance of the global variable builder.
*/
export function getGlobalVariableBuilder(config: L1ReaderConfig): GlobalVariableBuilder {
return new SimpleTestGlobalVariableBuilder(new ViemReader(config));
}

This file was deleted.

3 changes: 1 addition & 2 deletions yarn-project/sequencer-client/src/publisher/l1-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { type TelemetryClient } from '@aztec/telemetry-client';

import pick from 'lodash.pick';

import { type L2BlockReceiver } from '../receiver.js';
import { type PublisherConfig } from './config.js';
import { L1PublisherMetrics } from './l1-publisher-metrics.js';

Expand Down Expand Up @@ -151,7 +150,7 @@ export type L1SubmitProofArgs = {
*
* Adapted from https://github.com/AztecProtocol/aztec2-internal/blob/master/falafel/src/rollup_publisher.ts.
*/
export class L1Publisher implements L2BlockReceiver {
export class L1Publisher {
private interruptibleSleep = new InterruptibleSleep();
private sleepTimeMs: number;
private interrupted = false;
Expand Down
11 changes: 0 additions & 11 deletions yarn-project/sequencer-client/src/receiver.ts

This file was deleted.

0 comments on commit 6799199

Please sign in to comment.