Skip to content

Commit

Permalink
feat(protocol): add overridable getEIP1559Config() to TaikoL2 (#13815)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored May 25, 2023
1 parent b1dcb59 commit e15a9c1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
53 changes: 32 additions & 21 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,14 +53,12 @@ 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;
uint64 public gasExcess;
uint64 public __reserved1;
uint64 private __reserved1;

uint256[45] private __gap;

Expand Down Expand Up @@ -108,21 +112,20 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
|| _param1559.ratio2x1x == 0
) revert L2_INVALID_1559_PARAMS();

uint128 _xscale;
(_xscale, yscale) = Lib1559Math.calculateScales({
(uint128 xscale, uint128 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 +184,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 +220,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 +243,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 +280,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
12 changes: 7 additions & 5 deletions packages/protocol/utils/generate_genesis/taikoL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,13 @@ async function generateContractConfigs(
]),
]
)}`,
yscale: ethers.BigNumber.from(param1559.yscale),
xscale: ethers.BigNumber.from(param1559.xscale),
gasIssuedPerSecond: ethers.BigNumber.from(
param1559.gasIssuedPerSecond
),
_eip1559Config: {
yscale: ethers.BigNumber.from(param1559.yscale),
xscale: ethers.BigNumber.from(param1559.xscale),
gasIssuedPerSecond: ethers.BigNumber.from(
param1559.gasIssuedPerSecond
)
},
parentTimestamp: Math.floor(new Date().getTime() / 1000),
gasExcess: ethers.BigNumber.from(param1559.gasExcess),
// AddressResolver
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
### EIP1559Config

```solidity
uint128 yscale
```

### xscale

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

### gasIssuedPerSecond
### publicInputHash

```solidity
uint64 gasIssuedPerSecond
bytes32 publicInputHash
```

### parentTimestamp
Expand All @@ -67,12 +59,6 @@ uint64 latestSyncedL1Height
uint64 gasExcess
```

### \_\_reserved1

```solidity
uint64 __reserved1
```

### Anchored

```solidity
Expand Down Expand Up @@ -214,6 +200,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

0 comments on commit e15a9c1

Please sign in to comment.