Skip to content

Commit

Permalink
feat(protocol): reenable time as basefee calculation input (#17867)
Browse files Browse the repository at this point in the history
Co-authored-by: dantaik <dantaik@users.noreply.github.com>
Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent fe687b3 commit a1e217e
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 122 deletions.
6 changes: 3 additions & 3 deletions packages/protocol/contract_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
| __gap | uint256[49] | 202 | 0 | 1568 | contracts/L2/TaikoL2.sol:TaikoL2 |
| l2Hashes | mapping(uint256 => bytes32) | 251 | 0 | 32 | contracts/L2/TaikoL2.sol:TaikoL2 |
| publicInputHash | bytes32 | 252 | 0 | 32 | contracts/L2/TaikoL2.sol:TaikoL2 |
| gasExcess | uint64 | 253 | 0 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| parentGasExcess | uint64 | 253 | 0 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| lastSyncedBlock | uint64 | 253 | 8 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| __deprecated1 | uint64 | 253 | 16 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| __deprecated2 | uint64 | 253 | 24 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| parentTimestamp | uint64 | 253 | 16 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| parentGasTarget | uint64 | 253 | 24 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| l1ChainId | uint64 | 254 | 0 | 8 | contracts/L2/TaikoL2.sol:TaikoL2 |
| __gap | uint256[46] | 255 | 0 | 1472 | contracts/L2/TaikoL2.sol:TaikoL2 |

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ library TaikoData {
// ---------------------------------------------------------------------
uint8 basefeeAdjustmentQuotient;
uint8 basefeeSharingPctg;
uint32 blockGasIssuance;
uint32 gasIssuancePerSecond;
// ---------------------------------------------------------------------
// Group 6: Others
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -126,7 +126,7 @@ library TaikoData {
uint8 blobIndex;
uint8 basefeeAdjustmentQuotient;
uint8 basefeeSharingPctg;
uint32 blockGasIssuance;
uint32 gasIssuancePerSecond;
}

/// @dev Struct representing transition to be proven.
Expand Down
11 changes: 1 addition & 10 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,9 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {

/// @inheritdoc ITaikoL1
function getConfig() public pure virtual override returns (TaikoData.Config memory) {
// All hard-coded configurations:
// - treasury: the actual TaikoL2 address.
// - anchorGasLimit: 250_000 (based on internal devnet, its ~220_000
// after 256 L2 blocks)
return TaikoData.Config({
chainId: LibNetwork.TAIKO_MAINNET,
// If we have 1 block per 12 seconds, then each day there will be 86400/12=7200 blocks.
// We therefore use 7200 as the base unit to configure blockMaxProposals and
// blockRingBufferSize.
blockMaxProposals: 324_000, // = 7200 * 45
// We give 7200 * 5 = 36000 slots for verifeid blocks in case third party apps will use
// their data.
blockRingBufferSize: 360_000, // = 7200 * 50
maxBlocksToVerify: 16,
blockMaxGasLimit: 240_000_000,
Expand All @@ -310,7 +301,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
maxAnchorHeightOffset: 64,
basefeeAdjustmentQuotient: 8,
basefeeSharingPctg: 75,
blockGasIssuance: 20_000_000,
gasIssuancePerSecond: 5_000_000,
ontakeForkHeight: 374_400 // = 7200 * 52
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ library LibData {
blobIndex: 0,
basefeeAdjustmentQuotient: 0,
basefeeSharingPctg: 0,
blockGasIssuance: 0
gasIssuancePerSecond: 0
});
}
}
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ library LibProposing {
blobIndex: local.params.blobIndex,
basefeeAdjustmentQuotient: _config.basefeeAdjustmentQuotient,
basefeeSharingPctg: _config.basefeeSharingPctg,
blockGasIssuance: _config.blockGasIssuance
gasIssuancePerSecond: _config.gasIssuancePerSecond
});
}

Expand Down
63 changes: 36 additions & 27 deletions packages/protocol/contracts/L2/Lib1559Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ library Lib1559Math {
error EIP1559_INVALID_PARAMS();

function calc1559BaseFee(
uint32 _gasTarget,
uint8 _adjustmentQuotient,
uint256 _gasTarget,
uint64 _gasExcess,
uint64 _gasIssuance,
uint32 _parentGasUsed
Expand All @@ -37,42 +36,52 @@ library Lib1559Math {
// bonding curve, regardless the actual amount of gas used by this
// block, however, this block's gas used will affect the next
// block's base fee.
basefee_ = basefee(gasExcess_, uint256(_adjustmentQuotient) * _gasTarget);

// Always make sure basefee is nonzero, this is required by the node.
if (basefee_ == 0) basefee_ = 1;
basefee_ = basefee(gasExcess_, _gasTarget);
}

/// @dev eth_qty(excess_gas_issued) / (TARGET * ADJUSTMENT_QUOTIENT)
/// @param _gasExcess The gas excess value
/// @param _adjustmentFactor The product of gasTarget and adjustmentQuotient
function basefee(
uint256 _gasExcess,
uint256 _adjustmentFactor
/// @dev Returns the new gas excess that will keep the basefee the same.
/// `_newGasTarget * ln(_newGasTarget / _target) + _gasExcess * _newGasTarget / _target`
function adjustExcess(
uint64 _gasExcess,
uint64 _gasTarget,
uint64 _newGasTarget
)
internal
pure
returns (uint256)
returns (uint64)
{
if (_adjustmentFactor == 0) {
revert EIP1559_INVALID_PARAMS();
if (_gasTarget == 0) {
return _newGasTarget;
}
return _ethQty(_gasExcess, _adjustmentFactor) / FixedPointMathLib.WAD;

uint256 f = FixedPointMathLib.WAD;
uint256 ratio = f * _newGasTarget / _gasTarget;
if (ratio > uint256(type(int256).max)) revert EIP1559_INVALID_PARAMS();

int256 lnRatio = FixedPointMathLib.lnWad(int256(ratio)); // may be negative

uint256 newGasExcess;
assembly {
newGasExcess := sdiv(add(mul(lnRatio, _newGasTarget), mul(ratio, _gasExcess)), f)
}

return uint64(newGasExcess.min(type(uint64).max));
}

/// @dev exp(gas_qty / TARGET / ADJUSTMENT_QUOTIENT)
function _ethQty(
uint256 _gasExcess,
uint256 _adjustmentFactor
)
private
pure
returns (uint256)
{
uint256 input = _gasExcess * FixedPointMathLib.WAD / _adjustmentFactor;
/// @dev exp(_gasExcess / _gasTarget) / _gasTarget
function basefee(uint256 _gasExcess, uint256 _gasTarget) internal pure returns (uint256) {
uint256 fee = ethQty(_gasExcess, _gasTarget) / _gasTarget;
return fee == 0 ? 1 : fee;
}

/// @dev exp(_gasExcess / _gasTarget)
function ethQty(uint256 _gasExcess, uint256 _gasTarget) internal pure returns (uint256) {
if (_gasTarget == 0) revert EIP1559_INVALID_PARAMS();

uint256 input = FixedPointMathLib.WAD * _gasExcess / _gasTarget;
if (input > MAX_EXP_INPUT) {
input = MAX_EXP_INPUT;
}
return uint256(FixedPointMathLib.expWad(int256(input)));
return uint256(FixedPointMathLib.expWad(int256(input))) / FixedPointMathLib.WAD;
}
}
Loading

0 comments on commit a1e217e

Please sign in to comment.