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): add overridable getEIP1559Config() to TaikoL2 #13815

Merged
merged 5 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 32 additions & 19 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
uint64 ratio2x1x;
}

struct EIP1559Config {
uint128 yscale;
uint64 xscale;
uint64 gasIssuedPerSecond;
}

/*//////////////////////////////////////////////////////////////
STATE VARIABLES
//////////////////////////////////////////////////////////////*/
Expand All @@ -47,9 +53,7 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
// A hash to check the integrity of public inputs.
bytes32 public publicInputHash;

uint128 public yscale;
uint64 public xscale;
uint64 public gasIssuedPerSecond;
EIP1559Config private _eip1559Config;

uint64 public parentTimestamp;
uint64 public latestSyncedL1Height;
Expand Down Expand Up @@ -109,20 +113,21 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
) revert L2_INVALID_1559_PARAMS();

uint128 _xscale;
(_xscale, yscale) = Lib1559Math.calculateScales({
uint128 _yscale;
(_xscale, _yscale) = Lib1559Math.calculateScales({
xExcessMax: _param1559.gasExcessMax,
price: _param1559.basefee,
target: _param1559.gasTarget,
ratio2x1x: _param1559.ratio2x1x
});

if (_xscale == 0 || _xscale >= type(uint64).max || yscale == 0) {
if (_xscale == 0 || _xscale >= type(uint64).max || _yscale == 0) {
revert L2_INVALID_1559_PARAMS();
}
xscale = uint64(_xscale);
_eip1559Config.yscale = _yscale;
_eip1559Config.xscale = uint64(_xscale);
_eip1559Config.gasIssuedPerSecond = _param1559.gasIssuedPerSecond;

// basefee = _param1559.basefee;
gasIssuedPerSecond = _param1559.gasIssuedPerSecond;
gasExcess = _param1559.gasExcessMax / 2;
}

Expand Down Expand Up @@ -181,9 +186,10 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {

// Check EIP-1559 basefee
uint256 basefee;
if (gasIssuedPerSecond != 0) {
EIP1559Config memory config = getEIP1559Config();
if (config.gasIssuedPerSecond != 0) {
(basefee, gasExcess) = _calcBasefee(
block.timestamp - parentTimestamp, uint64(block.gaslimit), parentGasUsed
config, block.timestamp - parentTimestamp, uint64(block.gaslimit), parentGasUsed
);
}

Expand Down Expand Up @@ -216,7 +222,7 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
view
returns (uint256 _basefee)
{
(_basefee,) = _calcBasefee(timeSinceParent, gasLimit, parentGasUsed);
(_basefee,) = _calcBasefee(getEIP1559Config(), timeSinceParent, gasLimit, parentGasUsed);
}

function getCrossChainBlockHash(uint256 number) public view override returns (bytes32) {
Expand All @@ -239,6 +245,12 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
}
}

/// @dev Overide this funciton to return a constant EIP1559Config object
// to avoid reading from storage to reduce gas cost.
function getEIP1559Config() public view virtual returns (EIP1559Config memory) {
return _eip1559Config;
}

/*//////////////////////////////////////////////////////////////
PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -270,25 +282,26 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
}
}

function _calcBasefee(uint256 timeSinceParent, uint64 gasLimit, uint64 parentGasUsed)
private
view
returns (uint256 _basefee, uint64 _gasExcess)
{
function _calcBasefee(
EIP1559Config memory config,
uint256 timeSinceParent,
uint64 gasLimit,
uint64 parentGasUsed
) private view returns (uint256 _basefee, uint64 _gasExcess) {
// Very important to cap _gasExcess uint64
unchecked {
uint64 parentGasUsedNet = parentGasUsed > LibL2Consts.ANCHOR_GAS_COST
? parentGasUsed - LibL2Consts.ANCHOR_GAS_COST
: 0;

uint256 a = uint256(gasExcess) + parentGasUsedNet;
uint256 b = gasIssuedPerSecond * timeSinceParent;
uint256 b = config.gasIssuedPerSecond * timeSinceParent;
_gasExcess = uint64((a.max(b) - b).min(type(uint64).max));
}

_basefee = Lib1559Math.calculatePrice({
xscale: xscale,
yscale: yscale,
xscale: config.xscale,
yscale: config.yscale,
xExcess: _gasExcess,
xPurchase: gasLimit
});
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/TaikoL2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ contract TestTaikoL2 is Test {
private
returns (uint256 _basefee)
{
uint256 gasIssued = L2.gasIssuedPerSecond() * timeSinceParent;
uint256 gasIssued = L2.getEIP1559Config().gasIssuedPerSecond * timeSinceParent;
string memory _msg = string.concat(
"#",
Strings.toString(logIndex++),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,20 @@ struct EIP1559Params {
}
```

### publicInputHash

```solidity
bytes32 publicInputHash
```

### yscale

```solidity
uint128 yscale
```

### xscale
### EIP1559Config

```solidity
uint64 xscale
struct EIP1559Config {
uint128 yscale;
uint64 xscale;
uint64 gasIssuedPerSecond;
}
```

### gasIssuedPerSecond
### publicInputHash

```solidity
uint64 gasIssuedPerSecond
bytes32 publicInputHash
```

### parentTimestamp
Expand Down Expand Up @@ -214,6 +206,14 @@ block number.
function getBlockHash(uint256 number) public view returns (bytes32)
```

### getEIP1559Config

```solidity
function getEIP1559Config() public view virtual returns (struct TaikoL2.EIP1559Config)
```

_Overide this funciton to return a constant EIP1559Config object_

---

## title: ProxiedTaikoL2
Expand Down