Skip to content

Commit

Permalink
fix(protocol): fix bug in adjustExcess and improve L2 basefee calcu…
Browse files Browse the repository at this point in the history
…lation (#17896)

Co-authored-by: dantaik <[email protected]>
  • Loading branch information
dantaik and dantaik authored Aug 13, 2024
1 parent 6fe7c44 commit 920bd68
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 137 deletions.
16 changes: 11 additions & 5 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ pragma solidity 0.8.24;
/// protocol.
/// @custom:security-contact [email protected]
library TaikoData {
/// @dev Struct that represneds L2 basefee configurations
struct BaseFeeConfig {
uint8 adjustmentQuotient;
uint8 sharingPctg;
uint32 gasIssuancePerSecond;
uint64 minGasExcess;
uint32 maxGasIssuancePerBlock;
}

/// @dev Struct holding Taiko configuration parameters. See {TaikoConfig}.
struct Config {
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -39,9 +48,7 @@ library TaikoData {
// ---------------------------------------------------------------------
// Group 5: Previous configs in TaikoL2
// ---------------------------------------------------------------------
uint8 basefeeAdjustmentQuotient;
uint8 basefeeSharingPctg;
uint32 gasIssuancePerSecond;
BaseFeeConfig baseFeeConfig;
// ---------------------------------------------------------------------
// Group 6: Others
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -123,8 +130,7 @@ library TaikoData {
uint32 blobTxListOffset;
uint32 blobTxListLength;
uint8 blobIndex;
uint8 basefeeAdjustmentQuotient;
uint32 gasIssuancePerSecond;
BaseFeeConfig baseFeeConfig;
}

/// @dev Struct representing transition to be proven.
Expand Down
18 changes: 11 additions & 7 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
emit StateVariablesUpdated(state.slotB);
}

modifier onlyRegisteredProposer() {
modifier onlyPermittedProposer() {
LibProposing.checkProposerPermission(this);
_;
}
Expand Down Expand Up @@ -76,7 +76,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
)
external
payable
onlyRegisteredProposer
onlyPermittedProposer
whenNotPaused
nonReentrant
emitEventForClient
Expand All @@ -97,7 +97,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes calldata _txList
)
external
onlyRegisteredProposer
onlyPermittedProposer
whenNotPaused
nonReentrant
emitEventForClient
Expand All @@ -112,7 +112,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
bytes[] calldata _txListArr
)
external
onlyRegisteredProposer
onlyPermittedProposer
whenNotPaused
nonReentrant
emitEventForClient
Expand Down Expand Up @@ -301,9 +301,13 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
livenessBond: 125e18, // 125 Taiko token
stateRootSyncInternal: 16,
maxAnchorHeightOffset: 64,
basefeeAdjustmentQuotient: 8,
basefeeSharingPctg: 75,
gasIssuancePerSecond: 5_000_000,
baseFeeConfig: TaikoData.BaseFeeConfig({
adjustmentQuotient: 8,
sharingPctg: 75,
gasIssuancePerSecond: 5_000_000,
minGasExcess: 1_340_000_000,
maxGasIssuancePerBlock: 600_000_000 // two minutes
}),
ontakeForkHeight: 374_400 // = 7200 * 52
});
}
Expand Down
3 changes: 1 addition & 2 deletions packages/protocol/contracts/L1/libs/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ library LibData {
blobTxListOffset: 0,
blobTxListLength: 0,
blobIndex: 0,
basefeeAdjustmentQuotient: 0,
gasIssuancePerSecond: 0
baseFeeConfig: TaikoData.BaseFeeConfig(0, 0, 0, 0, 0)
});
}
}
17 changes: 11 additions & 6 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ library LibProposing {
blobHash: 0, // to be initialized below
// To make sure each L2 block can be exexucated deterministiclly by the client
// without referering to its metadata on Ethereum, we need to encode
// config.basefeeSharingPctg into the extraData.
// config.sharingPctg into the extraData.
extraData: local.postFork
? _encodeGasConfigs(_config.basefeeSharingPctg)
? _encodeBaseFeeConfig(_config.baseFeeConfig)
: local.extraData,
coinbase: local.params.coinbase,
id: local.b.numBlocks,
Expand All @@ -182,8 +182,7 @@ library LibProposing {
blobTxListOffset: local.params.blobTxListOffset,
blobTxListLength: local.params.blobTxListLength,
blobIndex: local.params.blobIndex,
basefeeAdjustmentQuotient: _config.basefeeAdjustmentQuotient,
gasIssuancePerSecond: _config.gasIssuancePerSecond
baseFeeConfig: _config.baseFeeConfig
});
}

Expand Down Expand Up @@ -270,7 +269,13 @@ library LibProposing {
}
}

function _encodeGasConfigs(uint8 _basefeeSharingPctg) private pure returns (bytes32) {
return bytes32(uint256(_basefeeSharingPctg));
function _encodeBaseFeeConfig(
TaikoData.BaseFeeConfig memory _baseFeeConfig
)
private
pure
returns (bytes32)
{
return bytes32(uint256(_baseFeeConfig.sharingPctg));
}
}
14 changes: 11 additions & 3 deletions packages/protocol/contracts/L2/Lib1559Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ library Lib1559Math {
uint256 _gasTarget,
uint64 _gasExcess,
uint64 _gasIssuance,
uint32 _parentGasUsed
uint32 _parentGasUsed,
uint64 _minGasExcess
)
internal
pure
Expand All @@ -30,7 +31,7 @@ library Lib1559Math {
// value as this has already happened
uint256 excess = uint256(_gasExcess) + _parentGasUsed;
excess = excess > _gasIssuance ? excess - _gasIssuance : 1;
gasExcess_ = uint64(excess.min(type(uint64).max));
gasExcess_ = uint64(excess.max(_minGasExcess).min(type(uint64).max));

// The base fee per gas used by this block is the spot price at the
// bonding curve, regardless the actual amount of gas used by this
Expand Down Expand Up @@ -61,8 +62,15 @@ library Lib1559Math {
int256 lnRatio = FixedPointMathLib.lnWad(int256(ratio)); // may be negative

uint256 newGasExcess;

assembly {
newGasExcess := sdiv(add(mul(lnRatio, _newGasTarget), mul(ratio, _gasExcess)), f)
// compute x = (_newGasTarget * lnRatio + _gasExcess * ratio)
let x := add(mul(_newGasTarget, lnRatio), mul(_gasExcess, ratio))

// If x < 0, set newGasExcess to 0, otherwise calculate newGasExcess = x / f
switch slt(x, 0)
case 1 { newGasExcess := 0 }
default { newGasExcess := div(x, f) }
}

return uint64(newGasExcess.min(type(uint64).max));
Expand Down
Loading

0 comments on commit 920bd68

Please sign in to comment.