Skip to content

Commit

Permalink
fix: Make getCurrentBaseFees smarter on timing
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Nov 28, 2024
1 parent 3f172d0 commit 983dd17
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 0 additions & 2 deletions l1-contracts/test/fees/FeeRollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ contract FeeRollupTest is FeeModelTestPoints, DecoderBase {
Slot nextSlot = Slot.wrap(1);
Epoch nextEpoch = Epoch.wrap(1);

// We need the fee at the time just before the first slot

// Loop through all of the L1 metadata
for (uint256 i = 0; i < l1Metadata.length; i++) {
// Predict what the fee will be before we jump in time!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
* @returns Base fees for the expected next block
*/
public async getCurrentBaseFees(): Promise<GasFees> {
const timestamp = BigInt((await this.publicClient.getBlock()).timestamp + BigInt(this.ethereumSlotDuration));
// Since this might be called in the middle of a slot where a block might have been published,
// we need to fetch the last block written, and estimate the earliest timestamp for the next block.
// The timestamp of that last block will act as a lower bound for the next block.

const lastBlock = await this.rollupContract.read.getBlock([await this.rollupContract.read.getPendingBlockNumber()]);
const earliestTimestamp = await this.rollupContract.read.getTimestampForSlot([lastBlock.slotNumber + 1n]);
const nextEthTimestamp = BigInt((await this.publicClient.getBlock()).timestamp + BigInt(this.ethereumSlotDuration));
const timestamp = earliestTimestamp > nextEthTimestamp ? earliestTimestamp : nextEthTimestamp;

return new GasFees(Fr.ZERO, new Fr(await this.rollupContract.read.getManaBaseFeeAt([timestamp, true])));
}

Expand Down Expand Up @@ -82,6 +90,7 @@ export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
const slotFr = new Fr(slotNumber);
const timestampFr = new Fr(timestamp);

// We can skip much of the logic in getCurrentBaseFees since it we already check that we are not within a slot elsewhere.
const gasFees = new GasFees(Fr.ZERO, new Fr(await this.rollupContract.read.getManaBaseFeeAt([timestamp, true])));

const globalVariables = new GlobalVariables(
Expand Down

0 comments on commit 983dd17

Please sign in to comment.