Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): contribute L1 basefee to L2 basefee calculation #17732

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/protocol/contract_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
| __deprecated1 | uint64 | 253 | 16 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| __deprecated2 | uint64 | 253 | 24 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| l1ChainId | uint64 | 254 | 0 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| avgL1BaseFee | uint64 | 254 | 8 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| __gap | uint256[46] | 255 | 0 | 1472 | contracts/L2/TaikoL2.sol:TaikoL2 |

## SignalService
Expand Down
41 changes: 39 additions & 2 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ contract TaikoL2 is EssentialContract {
/// @notice Golden touch address is the only address that can do the anchor transaction.
address public constant GOLDEN_TOUCH_ADDRESS = 0x0000777735367b36bC9B61C50022d9D0700dB4Ec;

uint256 public constant ONTAKE_FORK_HEIGHT = 324_512 * 2;
uint256 public constant L1_BASE_FEE_FACTOR = 200;

/// @notice Mapping from L2 block numbers to their block hashes. All L2 block hashes will
/// be saved in this mapping.
mapping(uint256 blockId => bytes32 blockHash) public l2Hashes;
Expand All @@ -44,7 +47,9 @@ contract TaikoL2 is EssentialContract {
uint64 private __deprecated2; // was __currentBlockTimestamp

/// @notice The L1's chain ID.
/// @dev Slot 4.
uint64 public l1ChainId;
uint64 public avgL1BaseFee;

uint256[46] private __gap;

Expand All @@ -54,6 +59,7 @@ contract TaikoL2 is EssentialContract {
event Anchored(bytes32 parentHash, uint64 gasExcess);

error L2_BASEFEE_MISMATCH();
error L1_FORK_ERROR();
error L2_INVALID_L1_CHAIN_ID();
error L2_INVALID_L2_CHAIN_ID();
error L2_INVALID_PARAM();
Expand Down Expand Up @@ -116,6 +122,33 @@ contract TaikoL2 is EssentialContract {
)
external
nonReentrant
{
if (block.number >= ONTAKE_FORK_HEIGHT) revert L1_FORK_ERROR();
_anchor(_l1BlockHash, _l1StateRoot, _l1BlockId, _parentGasUsed, 0);
}

function anchor2(
bytes32 _l1BlockHash,
bytes32 _l1StateRoot,
uint64 _l1BlockId,
uint32 _parentGasUsed,
uint64 _l1BaseFee
)
external
nonReentrant
{
if (block.number < ONTAKE_FORK_HEIGHT) revert L1_FORK_ERROR();
_anchor(_l1BlockHash, _l1StateRoot, _l1BlockId, _parentGasUsed, _l1BaseFee);
}

function _anchor(
bytes32 _l1BlockHash,
bytes32 _l1StateRoot,
uint64 _l1BlockId,
uint32 _parentGasUsed,
uint64 _l1BaseFee
)
private
{
if (
_l1BlockHash == 0 || _l1StateRoot == 0 || _l1BlockId == 0
Expand All @@ -137,8 +170,10 @@ contract TaikoL2 is EssentialContract {
revert L2_PUBLIC_INPUT_HASH_MISMATCH();
}

avgL1BaseFee = uint64((uint256(avgL1BaseFee) * 7 + _l1BaseFee) / 8);

// Verify the base fee per gas is correct
(uint256 _basefee, uint64 _gasExcess) = getBasefee(_l1BlockId, _parentGasUsed);
(uint256 _basefee, uint64 _gasExcess) = getBasefee(_l1BlockId, _parentGasUsed, avgL1BaseFee);

if (!skipFeeCheck() && block.basefee != _basefee) {
revert L2_BASEFEE_MISMATCH();
Expand Down Expand Up @@ -191,7 +226,8 @@ contract TaikoL2 is EssentialContract {
/// @return gasExcess_ The new gasExcess value.
function getBasefee(
uint64 _l1BlockId,
uint32 _parentGasUsed
uint32 _parentGasUsed,
uint256 _avgL1BaseFee
)
public
view
Expand All @@ -207,6 +243,7 @@ contract TaikoL2 is EssentialContract {
gasIssuance,
_parentGasUsed
);
basefee_ += (_avgL1BaseFee / L1_BASE_FEE_FACTOR);
}

/// @notice Retrieves the block hash for the given L2 block number.
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L2/TaikoL2NoFeeCheck.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ contract TestTaikoL2NoFeeCheck is TaikoTest {

vm.prank(L2.GOLDEN_TOUCH_ADDRESS());
_anchorSimulation(currentGasUsed, l1Height);
(uint256 currentBaseFee,) = L2.getBasefee(l1Height, currentGasUsed);
(uint256 currentBaseFee,) = L2.getBasefee(l1Height, currentGasUsed, 0);
allBaseFee += currentBaseFee;
console2.log("Actual gas in L2 block is:", currentGasUsed);
console2.log("L2block to baseFee is:", i, ":", currentBaseFee);
Expand Down