Skip to content

Commit

Permalink
feat(protocol): measure gas per block using a debug event (#18470)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Nov 9, 2024
1 parent 3481b68 commit e84e472
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/protocol/contracts/layer1/based/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {

uint256[50] private __gap;

/// @dev Emitted to assist with future gas optimizations.
/// @param isProposeBlock True if measuring gas for proposing a block, false if measuring gas
/// for proving a block.
/// @param gasUsed The average gas used per block, including verifications.
/// @param batchSize The number of blocks proposed or proved.
event DebugGasPerBlock(bool isProposeBlock, uint256 gasUsed, uint256 batchSize);

error L1_FORK_HEIGHT_ERROR();

modifier whenProvingNotPaused() {
Expand All @@ -37,6 +44,16 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
emit StateVariablesUpdated(state.slotB);
}

modifier measureGasUsed(bool _isProposeBlock, uint256 _batchSize) {
uint256 gas = gasleft();
_;
unchecked {
if (_batchSize > 0) {
emit DebugGasPerBlock(_isProposeBlock, gas - gasleft() / _batchSize, _batchSize);
}
}
}

/// @notice Initializes the contract.
/// @param _owner The owner of this contract. msg.sender will be used if this value is zero.
/// @param _rollupAddressManager The address of the {AddressManager} contract.
Expand Down Expand Up @@ -73,6 +90,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes calldata _txList
)
external
measureGasUsed(true, 1)
whenNotPaused
nonReentrant
emitEventForClient
Expand All @@ -88,6 +106,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes[] calldata _txListArr
)
external
measureGasUsed(true, _paramsArr.length)
whenNotPaused
nonReentrant
emitEventForClient
Expand All @@ -103,6 +122,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes calldata _input
)
external
measureGasUsed(false, 1)
whenNotPaused
whenProvingNotPaused
nonReentrant
Expand All @@ -118,6 +138,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes calldata _batchProof
)
external
measureGasUsed(false, _blockIds.length)
whenNotPaused
whenProvingNotPaused
nonReentrant
Expand Down

0 comments on commit e84e472

Please sign in to comment.