@aztec/sequencer-client 0.0.1-commit.24de95ac → 0.0.1-commit.2606882
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +281 -21
- package/dest/client/index.d.ts +1 -1
- package/dest/client/sequencer-client.d.ts +26 -16
- package/dest/client/sequencer-client.d.ts.map +1 -1
- package/dest/client/sequencer-client.js +78 -37
- package/dest/config.d.ts +37 -8
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +139 -48
- package/dest/global_variable_builder/fee_predictor.d.ts +37 -0
- package/dest/global_variable_builder/fee_predictor.d.ts.map +1 -0
- package/dest/global_variable_builder/fee_predictor.js +128 -0
- package/dest/global_variable_builder/fee_provider.d.ts +21 -0
- package/dest/global_variable_builder/fee_provider.d.ts.map +1 -0
- package/dest/global_variable_builder/fee_provider.js +58 -0
- package/dest/global_variable_builder/global_builder.d.ts +24 -20
- package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
- package/dest/global_variable_builder/global_builder.js +44 -65
- package/dest/global_variable_builder/index.d.ts +4 -2
- package/dest/global_variable_builder/index.d.ts.map +1 -1
- package/dest/global_variable_builder/index.js +2 -0
- package/dest/index.d.ts +2 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -2
- package/dest/publisher/config.d.ts +55 -20
- package/dest/publisher/config.d.ts.map +1 -1
- package/dest/publisher/config.js +124 -34
- package/dest/publisher/index.d.ts +2 -1
- package/dest/publisher/index.d.ts.map +1 -1
- package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
- package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +58 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
- package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
- package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/index.js +2 -0
- package/dest/publisher/sequencer-bundle-simulator.d.ts +96 -0
- package/dest/publisher/sequencer-bundle-simulator.d.ts.map +1 -0
- package/dest/publisher/sequencer-bundle-simulator.js +198 -0
- package/dest/publisher/sequencer-publisher-factory.d.ts +15 -8
- package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-factory.js +28 -4
- package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
- package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-metrics.js +23 -86
- package/dest/publisher/sequencer-publisher.d.ts +137 -97
- package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher.js +1001 -416
- package/dest/sequencer/automine/automine_factory.d.ts +54 -0
- package/dest/sequencer/automine/automine_factory.d.ts.map +1 -0
- package/dest/sequencer/automine/automine_factory.js +84 -0
- package/dest/sequencer/automine/automine_sequencer.d.ts +151 -0
- package/dest/sequencer/automine/automine_sequencer.d.ts.map +1 -0
- package/dest/sequencer/automine/automine_sequencer.js +509 -0
- package/dest/sequencer/chain_state_overrides.d.ts +61 -0
- package/dest/sequencer/chain_state_overrides.d.ts.map +1 -0
- package/dest/sequencer/chain_state_overrides.js +98 -0
- package/dest/sequencer/checkpoint_proposal_job.d.ts +150 -0
- package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_proposal_job.js +1792 -0
- package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts +34 -0
- package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_proposal_job_metrics.js +72 -0
- package/dest/sequencer/checkpoint_voter.d.ts +34 -0
- package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_voter.js +106 -0
- package/dest/sequencer/config.d.ts +3 -2
- package/dest/sequencer/config.d.ts.map +1 -1
- package/dest/sequencer/errors.d.ts +1 -1
- package/dest/sequencer/errors.d.ts.map +1 -1
- package/dest/sequencer/events.d.ts +92 -0
- package/dest/sequencer/events.d.ts.map +1 -0
- package/dest/sequencer/events.js +1 -0
- package/dest/sequencer/index.d.ts +6 -2
- package/dest/sequencer/index.d.ts.map +1 -1
- package/dest/sequencer/index.js +5 -1
- package/dest/sequencer/metrics.d.ts +48 -4
- package/dest/sequencer/metrics.d.ts.map +1 -1
- package/dest/sequencer/metrics.js +257 -50
- package/dest/sequencer/sequencer.d.ts +175 -144
- package/dest/sequencer/sequencer.d.ts.map +1 -1
- package/dest/sequencer/sequencer.js +1022 -567
- package/dest/sequencer/timetable.d.ts +64 -16
- package/dest/sequencer/timetable.d.ts.map +1 -1
- package/dest/sequencer/timetable.js +154 -64
- package/dest/sequencer/types.d.ts +3 -0
- package/dest/sequencer/types.d.ts.map +1 -0
- package/dest/sequencer/types.js +1 -0
- package/dest/sequencer/utils.d.ts +14 -8
- package/dest/sequencer/utils.d.ts.map +1 -1
- package/dest/sequencer/utils.js +7 -4
- package/dest/test/index.d.ts +6 -7
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/mock_checkpoint_builder.d.ts +95 -0
- package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
- package/dest/test/mock_checkpoint_builder.js +231 -0
- package/dest/test/utils.d.ts +53 -0
- package/dest/test/utils.d.ts.map +1 -0
- package/dest/test/utils.js +105 -0
- package/package.json +32 -30
- package/src/client/sequencer-client.ts +105 -60
- package/src/config.ts +163 -57
- package/src/global_variable_builder/README.md +44 -0
- package/src/global_variable_builder/fee_predictor.ts +172 -0
- package/src/global_variable_builder/fee_provider.ts +75 -0
- package/src/global_variable_builder/global_builder.ts +63 -92
- package/src/global_variable_builder/index.ts +3 -1
- package/src/index.ts +5 -2
- package/src/publisher/config.ts +174 -51
- package/src/publisher/index.ts +3 -0
- package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
- package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +57 -0
- package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
- package/src/publisher/l1_tx_failed_store/index.ts +3 -0
- package/src/publisher/sequencer-bundle-simulator.ts +254 -0
- package/src/publisher/sequencer-publisher-factory.ts +45 -14
- package/src/publisher/sequencer-publisher-metrics.ts +19 -71
- package/src/publisher/sequencer-publisher.ts +795 -538
- package/src/sequencer/README.md +243 -0
- package/src/sequencer/automine/README.md +46 -0
- package/src/sequencer/automine/automine_factory.ts +145 -0
- package/src/sequencer/automine/automine_sequencer.ts +595 -0
- package/src/sequencer/chain_state_overrides.ts +162 -0
- package/src/sequencer/checkpoint_proposal_job.ts +1632 -0
- package/src/sequencer/checkpoint_proposal_job_metrics.ts +128 -0
- package/src/sequencer/checkpoint_voter.ts +119 -0
- package/src/sequencer/config.ts +2 -1
- package/src/sequencer/events.ts +76 -0
- package/src/sequencer/index.ts +5 -1
- package/src/sequencer/metrics.ts +330 -62
- package/src/sequencer/sequencer.ts +831 -748
- package/src/sequencer/timetable.ts +181 -81
- package/src/sequencer/types.ts +6 -0
- package/src/sequencer/utils.ts +18 -9
- package/src/test/index.ts +5 -6
- package/src/test/mock_checkpoint_builder.ts +323 -0
- package/src/test/utils.ts +185 -0
- package/dest/sequencer/block_builder.d.ts +0 -27
- package/dest/sequencer/block_builder.d.ts.map +0 -1
- package/dest/sequencer/block_builder.js +0 -130
- package/dest/tx_validator/nullifier_cache.d.ts +0 -14
- package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
- package/dest/tx_validator/nullifier_cache.js +0 -24
- package/dest/tx_validator/tx_validator_factory.d.ts +0 -17
- package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
- package/dest/tx_validator/tx_validator_factory.js +0 -53
- package/src/sequencer/block_builder.ts +0 -218
- package/src/tx_validator/nullifier_cache.ts +0 -30
- package/src/tx_validator/tx_validator_factory.ts +0 -132
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { type L1FeeData, MAX_FEE_ASSET_PRICE_MODIFIER_BPS, type RollupContract } from '@aztec/ethereum/contracts';
|
|
2
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { times } from '@aztec/foundation/collection';
|
|
4
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
+
import { getSlotAtNextL1Block, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
6
|
+
import {
|
|
7
|
+
FEE_ORACLE_LAG,
|
|
8
|
+
GasFees,
|
|
9
|
+
MIN_ETH_PER_FEE_ASSET,
|
|
10
|
+
ManaUsageEstimate,
|
|
11
|
+
computeExcessMana,
|
|
12
|
+
computeManaMinFee,
|
|
13
|
+
} from '@aztec/stdlib/gas';
|
|
14
|
+
|
|
15
|
+
/** Cached rollup state for fee prediction. Refreshed once per L1 block. */
|
|
16
|
+
type FeeOracleState = {
|
|
17
|
+
lastSlot: SlotNumber;
|
|
18
|
+
excessMana: bigint;
|
|
19
|
+
ethPerFeeAsset: bigint;
|
|
20
|
+
manaTarget: bigint;
|
|
21
|
+
manaLimit: bigint;
|
|
22
|
+
provingCostPerManaEth: bigint;
|
|
23
|
+
epochDuration: bigint;
|
|
24
|
+
/** Pre-resolved L1 fees for each slot in the prediction window. */
|
|
25
|
+
l1FeesBySlot: L1FeeData[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Predicts min fees for LAG upcoming slots based on the L1 oracle state.
|
|
30
|
+
* A new oracle update can activate at startSlot + LAG, so only the first LAG entries
|
|
31
|
+
* are guaranteed stable. Caches L1 queries per L1 block and recomputes predictions
|
|
32
|
+
* for each mana usage estimate.
|
|
33
|
+
*/
|
|
34
|
+
export class FeePredictor {
|
|
35
|
+
private cachedState: Promise<FeeOracleState> | undefined;
|
|
36
|
+
private cachedL1BlockNumber: bigint | undefined;
|
|
37
|
+
|
|
38
|
+
private readonly slotDuration: number;
|
|
39
|
+
private readonly l1GenesisTime: bigint;
|
|
40
|
+
private readonly ethereumSlotDuration: number;
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
private readonly rollupContract: RollupContract,
|
|
44
|
+
private readonly publicClient: { getBlockNumber: (opts?: { cacheTime?: number }) => Promise<bigint> },
|
|
45
|
+
private readonly dateProvider: DateProvider,
|
|
46
|
+
config: { slotDuration: number; l1GenesisTime: bigint; ethereumSlotDuration: number },
|
|
47
|
+
) {
|
|
48
|
+
this.slotDuration = config.slotDuration;
|
|
49
|
+
this.l1GenesisTime = config.l1GenesisTime;
|
|
50
|
+
this.ethereumSlotDuration = config.ethereumSlotDuration;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Returns predicted min fees for each slot in the prediction window. */
|
|
54
|
+
async getPredictedMinFees(manaUsage: ManaUsageEstimate): Promise<GasFees[]> {
|
|
55
|
+
const state = await this.getState();
|
|
56
|
+
return this.computePredictions(state, manaUsage);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Fetches and caches rollup state. Refreshes when L1 block number advances. */
|
|
60
|
+
private async getState(): Promise<FeeOracleState> {
|
|
61
|
+
const blockNumber = await this.publicClient.getBlockNumber({ cacheTime: 0 });
|
|
62
|
+
if (this.cachedL1BlockNumber === undefined || blockNumber > this.cachedL1BlockNumber) {
|
|
63
|
+
this.cachedL1BlockNumber = blockNumber;
|
|
64
|
+
this.cachedState = this.fetchState(blockNumber);
|
|
65
|
+
}
|
|
66
|
+
return this.cachedState!;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private async fetchState(blockNumber: bigint): Promise<FeeOracleState> {
|
|
70
|
+
// Pin all non-constant queries to this L1 block number for a consistent snapshot.
|
|
71
|
+
const opts = { blockNumber };
|
|
72
|
+
|
|
73
|
+
// Cached constants don't need pinning
|
|
74
|
+
const [manaTarget, manaLimit, provingCostPerManaEth, epochDuration] = await Promise.all([
|
|
75
|
+
this.rollupContract.getManaTarget(),
|
|
76
|
+
this.rollupContract.getManaLimit(),
|
|
77
|
+
this.rollupContract.getProvingCostPerMana(),
|
|
78
|
+
this.rollupContract.getEpochDuration(),
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
// First, compute the earliest possible nextSlot independently of the checkpoint, so we can
|
|
82
|
+
// evaluate pruneability at the prediction start timestamp instead of the current L1 block time.
|
|
83
|
+
// This avoids an epoch-boundary edge case where the effective parent differs between now and nextSlot.
|
|
84
|
+
const slotConfig = { slotDuration: this.slotDuration, l1GenesisTime: this.l1GenesisTime };
|
|
85
|
+
const currentSlot = await this.rollupContract.getSlotNumber(opts);
|
|
86
|
+
|
|
87
|
+
const slotAtNextL1Block = getSlotAtNextL1Block(BigInt(this.dateProvider.nowInSeconds()), {
|
|
88
|
+
l1GenesisTime: this.l1GenesisTime,
|
|
89
|
+
slotDuration: this.slotDuration,
|
|
90
|
+
ethereumSlotDuration: this.ethereumSlotDuration,
|
|
91
|
+
});
|
|
92
|
+
const preliminaryNextSlot = SlotNumber(Math.max(currentSlot, slotAtNextL1Block));
|
|
93
|
+
const nextSlotTimestamp = getTimestampForSlot(preliminaryNextSlot, slotConfig);
|
|
94
|
+
|
|
95
|
+
// Resolve the effective checkpoint at the prediction start timestamp
|
|
96
|
+
const lastCheckpoint = await this.rollupContract.getEffectivePendingCheckpoint(nextSlotTimestamp, opts);
|
|
97
|
+
const lastSlot = lastCheckpoint.slotNumber;
|
|
98
|
+
// Refine nextSlot: also account for the slot after the last checkpoint
|
|
99
|
+
const nextSlot = SlotNumber(Math.max(SlotNumber.add(lastSlot, 1), preliminaryNextSlot));
|
|
100
|
+
const feeHeader = lastCheckpoint.feeHeader;
|
|
101
|
+
|
|
102
|
+
const slotCount = FEE_ORACLE_LAG;
|
|
103
|
+
const timestamps = times(slotCount, i => getTimestampForSlot(SlotNumber.add(nextSlot, i), slotConfig));
|
|
104
|
+
const l1FeesBySlot = await Promise.all(timestamps.map(ts => this.rollupContract.getL1FeesAt(ts, opts)));
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
lastSlot,
|
|
108
|
+
excessMana: computeExcessMana(feeHeader.excessMana, feeHeader.manaUsed, manaTarget),
|
|
109
|
+
ethPerFeeAsset: feeHeader.ethPerFeeAsset,
|
|
110
|
+
manaTarget,
|
|
111
|
+
manaLimit,
|
|
112
|
+
provingCostPerManaEth,
|
|
113
|
+
epochDuration: BigInt(epochDuration),
|
|
114
|
+
l1FeesBySlot,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Computes per-slot fee predictions given cached state and a mana usage assumption. */
|
|
119
|
+
private computePredictions(state: FeeOracleState, manaUsage: ManaUsageEstimate): GasFees[] {
|
|
120
|
+
const assumedManaUsed = this.getAssumedManaUsed(state, manaUsage);
|
|
121
|
+
|
|
122
|
+
const result: GasFees[] = [];
|
|
123
|
+
let { excessMana } = state;
|
|
124
|
+
let { ethPerFeeAsset } = state;
|
|
125
|
+
|
|
126
|
+
// Slot 0: current state (next available slot after last checkpoint)
|
|
127
|
+
result.push(this.computeGasFees(state, excessMana, ethPerFeeAsset, state.l1FeesBySlot[0]));
|
|
128
|
+
|
|
129
|
+
// Slots 1..LAG-1: advance excessMana with the assumed mana usage per checkpoint,
|
|
130
|
+
// and decay ethPerFeeAsset by MAX_FEE_ASSET_PRICE_MODIFIER_BPS per slot for conservative estimates.
|
|
131
|
+
// Lower ethPerFeeAsset means higher fees in fee asset terms.
|
|
132
|
+
for (let i = 1; i < state.l1FeesBySlot.length; i++) {
|
|
133
|
+
excessMana = computeExcessMana(excessMana, assumedManaUsed, state.manaTarget);
|
|
134
|
+
const decayed = (ethPerFeeAsset * (10000n - MAX_FEE_ASSET_PRICE_MODIFIER_BPS)) / 10000n;
|
|
135
|
+
ethPerFeeAsset = decayed < MIN_ETH_PER_FEE_ASSET ? MIN_ETH_PER_FEE_ASSET : decayed;
|
|
136
|
+
result.push(this.computeGasFees(state, excessMana, ethPerFeeAsset, state.l1FeesBySlot[i]));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private getAssumedManaUsed(state: FeeOracleState, manaUsage: ManaUsageEstimate): bigint {
|
|
143
|
+
switch (manaUsage) {
|
|
144
|
+
case ManaUsageEstimate.None:
|
|
145
|
+
return 0n;
|
|
146
|
+
case ManaUsageEstimate.Target:
|
|
147
|
+
return state.manaTarget;
|
|
148
|
+
case ManaUsageEstimate.Limit:
|
|
149
|
+
return state.manaLimit;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private computeGasFees(
|
|
154
|
+
state: FeeOracleState,
|
|
155
|
+
excessMana: bigint,
|
|
156
|
+
ethPerFeeAsset: bigint,
|
|
157
|
+
l1Fees: L1FeeData,
|
|
158
|
+
): GasFees {
|
|
159
|
+
return new GasFees(
|
|
160
|
+
0,
|
|
161
|
+
computeManaMinFee({
|
|
162
|
+
l1BaseFee: l1Fees.baseFee,
|
|
163
|
+
l1BlobFee: l1Fees.blobFee,
|
|
164
|
+
manaTarget: state.manaTarget,
|
|
165
|
+
epochDuration: state.epochDuration,
|
|
166
|
+
provingCostPerManaEth: state.provingCostPerManaEth,
|
|
167
|
+
excessMana,
|
|
168
|
+
ethPerFeeAsset,
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
2
|
+
import type { ViemPublicClient } from '@aztec/ethereum/types';
|
|
3
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
+
import { getNextL1SlotTimestamp } from '@aztec/stdlib/epoch-helpers';
|
|
6
|
+
import { GasFees, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
7
|
+
import type { FeeProvider } from '@aztec/stdlib/tx';
|
|
8
|
+
|
|
9
|
+
import { FeePredictor } from './fee_predictor.js';
|
|
10
|
+
import type { GlobalVariableBuilderConfig } from './global_builder.js';
|
|
11
|
+
|
|
12
|
+
/** Provides current and predicted fee information based on on-chain state. */
|
|
13
|
+
export class FeeProviderImpl implements FeeProvider {
|
|
14
|
+
private currentMinFees: Promise<GasFees> = Promise.resolve(new GasFees(0, 0));
|
|
15
|
+
private currentL1BlockNumber: bigint | undefined = undefined;
|
|
16
|
+
|
|
17
|
+
private readonly rollupContract: RollupContract;
|
|
18
|
+
private readonly feePredictor: FeePredictor;
|
|
19
|
+
private readonly ethereumSlotDuration: number;
|
|
20
|
+
private readonly l1GenesisTime: bigint;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
private readonly dateProvider: DateProvider,
|
|
24
|
+
private readonly publicClient: ViemPublicClient,
|
|
25
|
+
config: GlobalVariableBuilderConfig,
|
|
26
|
+
) {
|
|
27
|
+
this.ethereumSlotDuration = config.ethereumSlotDuration;
|
|
28
|
+
this.l1GenesisTime = config.l1GenesisTime;
|
|
29
|
+
|
|
30
|
+
this.rollupContract = new RollupContract(this.publicClient, config.rollupAddress);
|
|
31
|
+
this.feePredictor = new FeePredictor(this.rollupContract, this.publicClient, this.dateProvider, {
|
|
32
|
+
slotDuration: config.slotDuration,
|
|
33
|
+
l1GenesisTime: config.l1GenesisTime,
|
|
34
|
+
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Computes the "current" min fees, e.g., the price that you currently should pay to get include in the next block
|
|
40
|
+
* @returns Min fees for the next block
|
|
41
|
+
*/
|
|
42
|
+
private async computeCurrentMinFees(): Promise<GasFees> {
|
|
43
|
+
// Since this might be called in the middle of a slot where a block might have been published,
|
|
44
|
+
// we need to fetch the last block written, and estimate the earliest timestamp for the next block.
|
|
45
|
+
// The timestamp of that last block will act as a lower bound for the next block.
|
|
46
|
+
|
|
47
|
+
const lastCheckpoint = await this.rollupContract.getPendingCheckpoint();
|
|
48
|
+
const earliestTimestamp = await this.rollupContract.getTimestampForSlot(
|
|
49
|
+
SlotNumber.fromBigInt(BigInt(lastCheckpoint.slotNumber) + 1n),
|
|
50
|
+
);
|
|
51
|
+
const nextEthTimestamp = getNextL1SlotTimestamp(this.dateProvider.nowInSeconds(), {
|
|
52
|
+
l1GenesisTime: this.l1GenesisTime,
|
|
53
|
+
ethereumSlotDuration: this.ethereumSlotDuration,
|
|
54
|
+
});
|
|
55
|
+
const timestamp = earliestTimestamp > nextEthTimestamp ? earliestTimestamp : nextEthTimestamp;
|
|
56
|
+
|
|
57
|
+
return new GasFees(0, await this.rollupContract.getManaMinFeeAt(timestamp, true));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public async getCurrentMinFees(): Promise<GasFees> {
|
|
61
|
+
// Get the current block number
|
|
62
|
+
const blockNumber = await this.publicClient.getBlockNumber();
|
|
63
|
+
|
|
64
|
+
// If the L1 block number has changed then chain a new promise to get the current min fees
|
|
65
|
+
if (this.currentL1BlockNumber === undefined || blockNumber > this.currentL1BlockNumber) {
|
|
66
|
+
this.currentL1BlockNumber = blockNumber;
|
|
67
|
+
this.currentMinFees = this.currentMinFees.then(() => this.computeCurrentMinFees());
|
|
68
|
+
}
|
|
69
|
+
return this.currentMinFees;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
|
|
73
|
+
return this.feePredictor.getPredictedMinFees(manaUsage ?? ManaUsageEstimate.Target);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,92 +1,58 @@
|
|
|
1
1
|
import {
|
|
2
|
-
type L1ContractsConfig,
|
|
3
|
-
type L1ReaderConfig,
|
|
4
2
|
RollupContract,
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
} from '@aztec/ethereum';
|
|
3
|
+
type SimulationOverridesPlan,
|
|
4
|
+
buildSimulationOverridesStateOverride,
|
|
5
|
+
} from '@aztec/ethereum/contracts';
|
|
6
|
+
import type { ViemPublicClient } from '@aztec/ethereum/types';
|
|
7
|
+
import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
8
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
8
9
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
9
|
-
import {
|
|
10
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
10
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
11
11
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
12
|
+
import { type L1RollupConstants, getNextL1SlotTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
12
13
|
import { GasFees } from '@aztec/stdlib/gas';
|
|
13
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
CheckpointGlobalVariables,
|
|
16
|
+
GlobalVariableBuilder as GlobalVariableBuilderInterface,
|
|
17
|
+
} from '@aztec/stdlib/tx';
|
|
14
18
|
import { GlobalVariables } from '@aztec/stdlib/tx';
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
/** Configuration for the GlobalVariableBuilder (excludes L1 client config). */
|
|
21
|
+
export type GlobalVariableBuilderConfig = {
|
|
22
|
+
rollupAddress: EthAddress;
|
|
23
|
+
ethereumSlotDuration: number;
|
|
24
|
+
rollupVersion: bigint;
|
|
25
|
+
} & Pick<L1RollupConstants, 'slotDuration' | 'l1GenesisTime'>;
|
|
17
26
|
|
|
18
27
|
/**
|
|
19
28
|
* Simple global variables builder.
|
|
20
29
|
*/
|
|
21
30
|
export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
|
|
22
|
-
private log = createLogger('sequencer:global_variable_builder');
|
|
23
|
-
private currentBaseFees: Promise<GasFees> = Promise.resolve(new GasFees(0, 0));
|
|
24
|
-
private currentL1BlockNumber: bigint | undefined = undefined;
|
|
25
|
-
|
|
26
31
|
private readonly rollupContract: RollupContract;
|
|
27
|
-
private readonly publicClient: ViemPublicClient;
|
|
28
32
|
private readonly ethereumSlotDuration: number;
|
|
33
|
+
private readonly aztecSlotDuration: number;
|
|
34
|
+
private readonly l1GenesisTime: bigint;
|
|
29
35
|
|
|
30
|
-
private chainId
|
|
31
|
-
private version
|
|
32
|
-
|
|
33
|
-
constructor(config: L1ReaderConfig & Pick<L1ContractsConfig, 'ethereumSlotDuration'>) {
|
|
34
|
-
const { l1RpcUrls, l1ChainId: chainId, l1Contracts } = config;
|
|
36
|
+
private chainId: Fr;
|
|
37
|
+
private version: Fr;
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
constructor(
|
|
40
|
+
private readonly dateProvider: DateProvider,
|
|
41
|
+
private readonly publicClient: ViemPublicClient,
|
|
42
|
+
config: GlobalVariableBuilderConfig,
|
|
43
|
+
) {
|
|
44
|
+
this.version = new Fr(config.rollupVersion);
|
|
45
|
+
this.chainId = new Fr(this.publicClient.chain!.id);
|
|
37
46
|
|
|
38
47
|
this.ethereumSlotDuration = config.ethereumSlotDuration;
|
|
48
|
+
this.aztecSlotDuration = config.slotDuration;
|
|
49
|
+
this.l1GenesisTime = config.l1GenesisTime;
|
|
39
50
|
|
|
40
|
-
this.
|
|
41
|
-
chain: chain.chainInfo,
|
|
42
|
-
transport: fallback(chain.rpcUrls.map(url => http(url))),
|
|
43
|
-
pollingInterval: config.viemPollingIntervalMS,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
this.rollupContract = new RollupContract(this.publicClient, l1Contracts.rollupAddress);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Computes the "current" base fees, e.g., the price that you currently should pay to get include in the next block
|
|
51
|
-
* @returns Base fees for the next block
|
|
52
|
-
*/
|
|
53
|
-
private async computeCurrentBaseFees(): Promise<GasFees> {
|
|
54
|
-
// Since this might be called in the middle of a slot where a block might have been published,
|
|
55
|
-
// we need to fetch the last block written, and estimate the earliest timestamp for the next block.
|
|
56
|
-
// The timestamp of that last block will act as a lower bound for the next block.
|
|
57
|
-
|
|
58
|
-
const lastBlock = await this.rollupContract.getBlock(await this.rollupContract.getBlockNumber());
|
|
59
|
-
const earliestTimestamp = await this.rollupContract.getTimestampForSlot(lastBlock.slotNumber + 1n);
|
|
60
|
-
const nextEthTimestamp = BigInt((await this.publicClient.getBlock()).timestamp + BigInt(this.ethereumSlotDuration));
|
|
61
|
-
const timestamp = earliestTimestamp > nextEthTimestamp ? earliestTimestamp : nextEthTimestamp;
|
|
62
|
-
|
|
63
|
-
return new GasFees(0, await this.rollupContract.getManaBaseFeeAt(timestamp, true));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public async getCurrentBaseFees(): Promise<GasFees> {
|
|
67
|
-
// Get the current block number
|
|
68
|
-
const blockNumber = await this.publicClient.getBlockNumber();
|
|
69
|
-
|
|
70
|
-
// If the L1 block number has changed then chain a new promise to get the current base fees
|
|
71
|
-
if (this.currentL1BlockNumber === undefined || blockNumber > this.currentL1BlockNumber) {
|
|
72
|
-
this.currentL1BlockNumber = blockNumber;
|
|
73
|
-
this.currentBaseFees = this.currentBaseFees.then(() => this.computeCurrentBaseFees());
|
|
74
|
-
}
|
|
75
|
-
return this.currentBaseFees;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public async getGlobalConstantVariables(): Promise<Pick<GlobalVariables, 'chainId' | 'version'>> {
|
|
79
|
-
if (!this.chainId) {
|
|
80
|
-
this.chainId = new Fr(this.publicClient.chain.id);
|
|
81
|
-
}
|
|
82
|
-
if (!this.version) {
|
|
83
|
-
this.version = new Fr(await this.rollupContract.getVersion());
|
|
84
|
-
}
|
|
85
|
-
return { chainId: this.chainId, version: this.version };
|
|
51
|
+
this.rollupContract = new RollupContract(this.publicClient, config.rollupAddress);
|
|
86
52
|
}
|
|
87
53
|
|
|
88
54
|
/**
|
|
89
|
-
* Simple builder of global variables
|
|
55
|
+
* Simple builder of global variables.
|
|
90
56
|
* @param blockNumber - The block number to build global variables for.
|
|
91
57
|
* @param coinbase - The address to receive block reward.
|
|
92
58
|
* @param feeRecipient - The address to receive fees.
|
|
@@ -94,36 +60,41 @@ export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
|
|
|
94
60
|
* @returns The global variables for the given block number.
|
|
95
61
|
*/
|
|
96
62
|
public async buildGlobalVariables(
|
|
97
|
-
blockNumber:
|
|
63
|
+
blockNumber: BlockNumber,
|
|
98
64
|
coinbase: EthAddress,
|
|
99
65
|
feeRecipient: AztecAddress,
|
|
100
|
-
|
|
66
|
+
maybeSlot?: SlotNumber,
|
|
101
67
|
): Promise<GlobalVariables> {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
68
|
+
const slot: SlotNumber =
|
|
69
|
+
maybeSlot ??
|
|
70
|
+
(await this.rollupContract.getSlotAt(
|
|
71
|
+
getNextL1SlotTimestamp(this.dateProvider.nowInSeconds(), {
|
|
72
|
+
l1GenesisTime: this.l1GenesisTime,
|
|
73
|
+
ethereumSlotDuration: this.ethereumSlotDuration,
|
|
74
|
+
}),
|
|
75
|
+
));
|
|
76
|
+
|
|
77
|
+
const checkpointGlobalVariables = await this.buildCheckpointGlobalVariables(coinbase, feeRecipient, slot);
|
|
78
|
+
return GlobalVariables.from({ blockNumber, ...checkpointGlobalVariables });
|
|
79
|
+
}
|
|
112
80
|
|
|
113
|
-
|
|
114
|
-
|
|
81
|
+
/** Builds global variables that are constant throughout a checkpoint. */
|
|
82
|
+
public async buildCheckpointGlobalVariables(
|
|
83
|
+
coinbase: EthAddress,
|
|
84
|
+
feeRecipient: AztecAddress,
|
|
85
|
+
slotNumber: SlotNumber,
|
|
86
|
+
simulationOverridesPlan?: SimulationOverridesPlan,
|
|
87
|
+
): Promise<CheckpointGlobalVariables> {
|
|
88
|
+
const { chainId, version } = this;
|
|
89
|
+
|
|
90
|
+
const timestamp = getTimestampForSlot(slotNumber, {
|
|
91
|
+
slotDuration: this.aztecSlotDuration,
|
|
92
|
+
l1GenesisTime: this.l1GenesisTime,
|
|
93
|
+
});
|
|
115
94
|
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
version,
|
|
119
|
-
blockNumber,
|
|
120
|
-
slotFr,
|
|
121
|
-
timestamp,
|
|
122
|
-
coinbase,
|
|
123
|
-
feeRecipient,
|
|
124
|
-
gasFees,
|
|
125
|
-
);
|
|
95
|
+
const stateOverride = await buildSimulationOverridesStateOverride(this.rollupContract, simulationOverridesPlan);
|
|
96
|
+
const gasFees = new GasFees(0, await this.rollupContract.getManaMinFeeAt(timestamp, true, stateOverride));
|
|
126
97
|
|
|
127
|
-
return
|
|
98
|
+
return { chainId, version, slotNumber, timestamp, coinbase, feeRecipient, gasFees };
|
|
128
99
|
}
|
|
129
100
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,12 +2,15 @@ export * from './client/index.js';
|
|
|
2
2
|
export * from './config.js';
|
|
3
3
|
export * from './publisher/index.js';
|
|
4
4
|
export {
|
|
5
|
-
|
|
5
|
+
AutomineSequencer,
|
|
6
|
+
type AutomineSequencerConstants,
|
|
7
|
+
type AutomineSequencerDeps,
|
|
8
|
+
createAutomineSequencer,
|
|
9
|
+
type CreateAutomineSequencerArgs,
|
|
6
10
|
Sequencer,
|
|
7
11
|
SequencerState,
|
|
8
12
|
type SequencerEvents,
|
|
9
13
|
} from './sequencer/index.js';
|
|
10
|
-
export * from './tx_validator/tx_validator_factory.js';
|
|
11
14
|
|
|
12
15
|
// Used by the node to simulate public parts of transactions. Should these be moved to a shared library?
|
|
13
16
|
// ISSUE(#9832)
|