Skip to content

Commit 5b222d5

Browse files
spalladinoclaude
andcommitted
refactor(stdlib): extract FeeProvider interface from GlobalVariableBuilder
Addresses PR #22116 review comment 5: the GlobalVariableBuilder interface was overloaded with both fee and global-variables concerns. FeeProvider is now a separate interface with its own implementation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 24d1871 commit 5b222d5

8 files changed

Lines changed: 70 additions & 28 deletions

File tree

yarn-project/aztec-node/src/aztec-node/server.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { L2LogsSource, MerkleTreeReadOperations, WorldStateSynchronizer } f
2525
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
2626
import { mockTx } from '@aztec/stdlib/testing';
2727
import { MerkleTreeId, PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
28+
import type { FeeProvider } from '@aztec/stdlib/tx';
2829
import {
2930
BlockHeader,
3031
GlobalVariables,
@@ -73,6 +74,7 @@ class TestAztecNodeService extends AztecNodeService {
7374
describe('aztec node', () => {
7475
let p2p: MockProxy<P2P>;
7576
let globalVariablesBuilder: MockProxy<GlobalVariableBuilder>;
77+
let feeProvider: MockProxy<FeeProvider>;
7678
let merkleTreeOps: MockProxy<MerkleTreeReadOperations>;
7779
let worldState: MockProxy<WorldStateSynchronizer>;
7880
let l2BlockSource: MockProxy<L2BlockSource>;
@@ -108,7 +110,8 @@ describe('aztec node', () => {
108110
p2p = mock<P2P>();
109111

110112
globalVariablesBuilder = mock<GlobalVariableBuilder>();
111-
globalVariablesBuilder.getCurrentMinFees.mockResolvedValue(new GasFees(0, BlockNumber.ZERO));
113+
feeProvider = mock<FeeProvider>();
114+
feeProvider.getCurrentMinFees.mockResolvedValue(new GasFees(0, BlockNumber.ZERO));
112115

113116
merkleTreeOps = mock<MerkleTreeReadOperations>();
114117
merkleTreeOps.findLeafIndices.mockImplementation((treeId: MerkleTreeId, _value: any[]) => {
@@ -196,6 +199,7 @@ describe('aztec node', () => {
196199
12345,
197200
rollupVersion.toNumber(),
198201
globalVariablesBuilder,
202+
feeProvider,
199203
epochCache,
200204
getPackageVersion() ?? '',
201205
new TestCircuitVerifier(),
@@ -738,6 +742,7 @@ describe('aztec node', () => {
738742
12345,
739743
rollupVersion.toNumber(),
740744
globalVariablesBuilder,
745+
feeProvider,
741746
epochCache,
742747
getPackageVersion() ?? '',
743748
new TestCircuitVerifier(),
@@ -927,6 +932,7 @@ describe('aztec node', () => {
927932
12345,
928933
rollupVersion.toNumber(),
929934
globalVariablesBuilder,
935+
feeProvider,
930936
epochCache,
931937
getPackageVersion() ?? '',
932938
new TestCircuitVerifier(),

yarn-project/aztec-node/src/aztec-node/server.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import {
3232
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
3333
import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
3434
import { createKeyStoreForProver } from '@aztec/prover-node/config';
35-
import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
35+
import {
36+
FeeProviderImpl,
37+
GlobalVariableBuilder,
38+
SequencerClient,
39+
type SequencerPublisher,
40+
} from '@aztec/sequencer-client';
3641
import { PublicProcessorFactory } from '@aztec/simulator/server';
3742
import {
3843
AttestationsBlockWatcher,
@@ -86,6 +91,7 @@ import type { NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreim
8691
import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
8792
import {
8893
type BlockHeader,
94+
type FeeProvider,
8995
type GlobalVariableBuilder as GlobalVariableBuilderInterface,
9096
type IndexedTxEffect,
9197
PublicSimulationOutput,
@@ -151,6 +157,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
151157
protected readonly l1ChainId: number,
152158
protected readonly version: number,
153159
protected readonly globalVariableBuilder: GlobalVariableBuilderInterface,
160+
protected readonly feeProvider: FeeProvider,
154161
protected readonly epochCache: EpochCacheInterface,
155162
protected readonly packageVersion: string,
156163
private peerProofVerifier: ClientProtocolCircuitVerifier,
@@ -478,13 +485,16 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
478485
})
479486
.catch(err => log.error('Failed to start p2p services after archiver sync', err));
480487

481-
const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, {
488+
const globalVariableBuilderConfig = {
482489
l1Contracts: config.l1Contracts,
483490
ethereumSlotDuration: config.ethereumSlotDuration,
484491
rollupVersion: BigInt(config.rollupVersion),
485492
l1GenesisTime,
486493
slotDuration: Number(slotDuration),
487-
});
494+
};
495+
496+
const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, globalVariableBuilderConfig);
497+
const feeProvider = new FeeProviderImpl(dateProvider, publicClient, globalVariableBuilderConfig);
488498

489499
// Validator enabled, create/start relevant service
490500
let sequencer: SequencerClient | undefined;
@@ -612,6 +622,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
612622
ethereumChain.chainInfo.id,
613623
config.rollupVersion,
614624
globalVariableBuilder,
625+
feeProvider,
615626
epochCache,
616627
packageVersion,
617628
peerProofVerifier,
@@ -765,12 +776,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
765776
}
766777

767778
public async getCurrentMinFees(): Promise<GasFees> {
768-
return await this.globalVariableBuilder.getCurrentMinFees();
779+
return await this.feeProvider.getCurrentMinFees();
769780
}
770781

771782
/** Returns predicted min fees for the current slot and next N slots. */
772783
public async getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
773-
return await this.globalVariableBuilder.getPredictedMinFees(manaUsage);
784+
return await this.feeProvider.getPredictedMinFees(manaUsage);
774785
}
775786

776787
public async getMaxPriorityFees(): Promise<GasFees> {

yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GasFees, ManaUsageEstimate } from '@aztec/stdlib/gas';
1212
import type {
1313
BuildCheckpointGlobalVariablesOpts,
1414
CheckpointGlobalVariables,
15+
FeeProvider,
1516
GlobalVariableBuilder as GlobalVariableBuilderInterface,
1617
} from '@aztec/stdlib/tx';
1718
import { GlobalVariables } from '@aztec/stdlib/tx';
@@ -25,33 +26,22 @@ export type GlobalVariableBuilderConfig = {
2526
rollupVersion: bigint;
2627
} & Pick<L1RollupConstants, 'slotDuration' | 'l1GenesisTime'>;
2728

28-
/**
29-
* Simple global variables builder.
30-
*/
31-
export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
32-
private log = createLogger('sequencer:global_variable_builder');
29+
/** Provides current and predicted fee information based on on-chain state. */
30+
export class FeeProviderImpl implements FeeProvider {
3331
private currentMinFees: Promise<GasFees> = Promise.resolve(new GasFees(0, 0));
3432
private currentL1BlockNumber: bigint | undefined = undefined;
3533

3634
private readonly rollupContract: RollupContract;
3735
private readonly feePredictor: FeePredictor;
3836
private readonly ethereumSlotDuration: number;
39-
private readonly aztecSlotDuration: number;
4037
private readonly l1GenesisTime: bigint;
4138

42-
private chainId: Fr;
43-
private version: Fr;
44-
4539
constructor(
4640
private readonly dateProvider: DateProvider,
4741
private readonly publicClient: ViemPublicClient,
4842
config: GlobalVariableBuilderConfig,
4943
) {
50-
this.version = new Fr(config.rollupVersion);
51-
this.chainId = new Fr(this.publicClient.chain!.id);
52-
5344
this.ethereumSlotDuration = config.ethereumSlotDuration;
54-
this.aztecSlotDuration = config.slotDuration;
5545
this.l1GenesisTime = config.l1GenesisTime;
5646

5747
this.rollupContract = new RollupContract(this.publicClient, config.l1Contracts.rollupAddress);
@@ -95,6 +85,36 @@ export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
9585
public getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
9686
return this.feePredictor.getPredictedMinFees(this.publicClient, manaUsage ?? ManaUsageEstimate.Target);
9787
}
88+
}
89+
90+
/**
91+
* Simple global variables builder.
92+
*/
93+
export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
94+
private log = createLogger('sequencer:global_variable_builder');
95+
96+
private readonly rollupContract: RollupContract;
97+
private readonly ethereumSlotDuration: number;
98+
private readonly aztecSlotDuration: number;
99+
private readonly l1GenesisTime: bigint;
100+
101+
private chainId: Fr;
102+
private version: Fr;
103+
104+
constructor(
105+
private readonly dateProvider: DateProvider,
106+
private readonly publicClient: ViemPublicClient,
107+
config: GlobalVariableBuilderConfig,
108+
) {
109+
this.version = new Fr(config.rollupVersion);
110+
this.chainId = new Fr(this.publicClient.chain!.id);
111+
112+
this.ethereumSlotDuration = config.ethereumSlotDuration;
113+
this.aztecSlotDuration = config.slotDuration;
114+
this.l1GenesisTime = config.l1GenesisTime;
115+
116+
this.rollupContract = new RollupContract(this.publicClient, config.l1Contracts.rollupAddress);
117+
}
98118

99119
/**
100120
* Simple builder of global variables.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { GlobalVariableBuilder, type GlobalVariableBuilderConfig } from './global_builder.js';
1+
export { FeeProviderImpl, GlobalVariableBuilder, type GlobalVariableBuilderConfig } from './global_builder.js';
22
export { FeePredictor } from './fee_predictor.js';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { ManaUsageEstimate } from '../gas/fee_math.js';
2+
import type { GasFees } from '../gas/gas_fees.js';
3+
4+
/** Provides current and predicted fee information for transaction pricing. */
5+
export interface FeeProvider {
6+
/** Returns the current minimum fees for inclusion in the next block. */
7+
getCurrentMinFees(): Promise<GasFees>;
8+
/** Returns predicted min fees for each slot in the prediction window. */
9+
getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]>;
10+
}

yarn-project/stdlib/src/tx/global_variable_builder.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import type { EthAddress } from '@aztec/foundation/eth-address';
44
import type { SlotNumber } from '@aztec/foundation/schemas';
55

66
import type { AztecAddress } from '../aztec-address/index.js';
7-
import type { ManaUsageEstimate } from '../gas/fee_math.js';
8-
import type { GasFees } from '../gas/gas_fees.js';
97
import type { UInt32 } from '../types/index.js';
108
import type { CheckpointGlobalVariables, GlobalVariables } from './global_variables.js';
119

@@ -25,11 +23,6 @@ export type BuildCheckpointGlobalVariablesOpts = {
2523
* Interface for building global variables for Aztec blocks.
2624
*/
2725
export interface GlobalVariableBuilder {
28-
getCurrentMinFees(): Promise<GasFees>;
29-
30-
/** Returns predicted min fees for the current slot and next N slots. */
31-
getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]>;
32-
3326
/**
3427
* Builds global variables for a given block.
3528
* @param blockNumber - The block number to build global variables for.

yarn-project/stdlib/src/tx/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export * from './validator/tx_validator.js';
2424
export * from './validator/empty_validator.js';
2525
export * from './validator/error_texts.js';
2626
export * from './capsule.js';
27+
export * from './fee_provider.js';
2728
export * from './global_variable_builder.js';
2829
export * from './hashed_values.js';
2930
export * from './indexed_tx_effect.js';

yarn-project/txe/src/state_machine/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getPackageVersion } from '@aztec/stdlib/update-checker';
1313

1414
import { TXEArchiver } from './archiver.js';
1515
import { DummyP2P } from './dummy_p2p_client.js';
16-
import { TXEGlobalVariablesBuilder } from './global_variable_builder.js';
16+
import { TXEFeeProvider, TXEGlobalVariablesBuilder } from './global_variable_builder.js';
1717
import { MockEpochCache } from './mock_epoch_cache.js';
1818
import { TXESynchronizer } from './synchronizer.js';
1919

@@ -56,6 +56,7 @@ export class TXEStateMachine {
5656
VERSION,
5757
CHAIN_ID,
5858
new TXEGlobalVariablesBuilder(),
59+
new TXEFeeProvider(),
5960
new MockEpochCache(),
6061
getPackageVersion() ?? '',
6162
new TestCircuitVerifier(),

0 commit comments

Comments
 (0)