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): L2 1559 no longer use block gas limit #14388

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 23 additions & 21 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
uint256 basefee;
EIP1559Config memory config = getEIP1559Config();
if (config.gasIssuedPerSecond != 0) {
(basefee, gasExcess) = _calcBasefee(
config,
block.timestamp - parentTimestamp,
uint32(block.gaslimit),
parentGasUsed
);
(basefee, gasExcess) = _calcBasefee({
config: config,
timeSinceParent: block.timestamp - parentTimestamp,
parentGasUsed: parentGasUsed
});
}

// On L2, basefee is not burnt, but sent to a treasury instead.
Expand Down Expand Up @@ -234,16 +233,17 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {

function getBasefee(
uint32 timeSinceParent,
uint32 gasLimit,
uint32 parentGasUsed
)
public
view
returns (uint256 _basefee)
{
(_basefee,) = _calcBasefee(
getEIP1559Config(), timeSinceParent, gasLimit, parentGasUsed
);
(_basefee,) = _calcBasefee({
config: getEIP1559Config(),
timeSinceParent: timeSinceParent,
parentGasUsed: parentGasUsed
});
}

function getCrossChainBlockHash(uint256 number)
Expand Down Expand Up @@ -321,36 +321,38 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, ICrossChainSync {
function _calcBasefee(
EIP1559Config memory config,
uint256 timeSinceParent,
uint32 gasLimit,
uint32 parentGasUsed
)
private
view
returns (uint256 _basefee, uint64 _gasExcess)
{
// Very important to cap _gasExcess uint64
unchecked {
uint32 parentGasUsedNet;
if (parentGasUsed > LibL2Consts.ANCHOR_GAS_COST) {
uint32 parentGasUsedNet;

if (parentGasUsed > LibL2Consts.ANCHOR_GAS_COST) {
unchecked {
parentGasUsedNet = parentGasUsed - LibL2Consts.ANCHOR_GAS_COST;
}

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

_basefee = Lib1559Math.calculatePrice({
xscale: config.xscale,
yscale: config.yscale,
xExcess: _gasExcess,
xPurchase: gasLimit
xExcess: gasExcess,
xPurchase: parentGasUsedNet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this adds an additional delay on the actual basefee that isn't really required, because this will set the basefee between the two points, but the parent block has already happened so the basefee needs to be at the end point uint256(gasExcess) + parentGasUsedNet already. I think the more accurate way to do this is to purchase 0 gas after already taking into account the parent gas ussed, which means calculating the derivative at the end point which is also easier/cheaper to calculate: basefee = eth_qty(_gasExcess) / (TARGET * ADJUSTMENT_QUOTIENT).

The current implementation additionally followed an approach very similar to the one proposed in https://ethresear.ch/t/make-eip-1559-more-like-an-amm-curve/9082 in option 2, purchasing an extra amount of gas for the max amount of gas possible in the block for safety and accuracy, but without implementing the refund because that would be very complex to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out this new commit, let me know if it addresses your concerns.

});
if (_basefee == 0) {
// To make sure when 1559 is enabled, the basefee is non-zero
// (geth never use 0 values for basefee)
_basefee = 1;
}

// Very important to cap _gasExcess uint64
unchecked {
uint256 issued = timeSinceParent * config.gasIssuedPerSecond;
uint256 excess = (uint256(gasExcess) + parentGasUsedNet).max(issued);
_gasExcess = uint64((excess - issued).min(type(uint64).max));
}
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/protocol/test/TaikoL1TestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ abstract contract TaikoL1TestBase is Test {
bytes memory txList = new bytes(txListSize);
TaikoData.BlockMetadataInput memory input = TaikoData.BlockMetadataInput({
beneficiary: proposer,
gasLimit: gasLimit,
txListHash: keccak256(txList),
txListByteStart: 0,
txListByteEnd: txListSize,
Expand Down
40 changes: 10 additions & 30 deletions packages/protocol/test/TaikoL2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract TestTaikoL2 is Test {
function testAnchorTxsBlocktimeConstant() external {
uint256 firstBasefee;
for (uint256 i = 0; i < 100; i++) {
uint256 basefee = _getBasefeeAndPrint(0, BLOCK_GAS_LIMIT);
uint256 basefee = _getBasefeeAndPrint2(0, BLOCK_GAS_LIMIT);
vm.fee(basefee);

if (firstBasefee == 0) {
Expand All @@ -60,7 +60,7 @@ contract TestTaikoL2 is Test {
uint256 prevBasefee;

for (uint256 i = 0; i < 32; i++) {
uint256 basefee = _getBasefeeAndPrint(0, BLOCK_GAS_LIMIT);
uint256 basefee = _getBasefeeAndPrint2(0, BLOCK_GAS_LIMIT);
vm.fee(basefee);

assertGe(basefee, prevBasefee);
Expand All @@ -78,7 +78,7 @@ contract TestTaikoL2 is Test {
uint256 prevBasefee;

for (uint256 i = 0; i < 30; i++) {
uint256 basefee = _getBasefeeAndPrint(0, BLOCK_GAS_LIMIT);
uint256 basefee = _getBasefeeAndPrint2(0, BLOCK_GAS_LIMIT);
vm.fee(basefee);

if (prevBasefee != 0) {
Expand All @@ -97,7 +97,7 @@ contract TestTaikoL2 is Test {

// calling anchor in the same block more than once should fail
function testAnchorTxsFailInTheSameBlock() external {
uint256 expectedBasefee = _getBasefeeAndPrint(0, BLOCK_GAS_LIMIT);
uint256 expectedBasefee = _getBasefeeAndPrint2(0, BLOCK_GAS_LIMIT);
vm.fee(expectedBasefee);

vm.prank(L2.GOLDEN_TOUCH_ADDRESS());
Expand All @@ -110,7 +110,7 @@ contract TestTaikoL2 is Test {

// calling anchor in the same block more than once should fail
function testAnchorTxsFailByNonTaikoL2Signer() external {
uint256 expectedBasefee = _getBasefeeAndPrint(0, BLOCK_GAS_LIMIT);
uint256 expectedBasefee = _getBasefeeAndPrint2(0, BLOCK_GAS_LIMIT);
vm.fee(expectedBasefee);
vm.expectRevert();
_anchor(BLOCK_GAS_LIMIT);
Expand All @@ -134,31 +134,14 @@ contract TestTaikoL2 is Test {

function testGetBasefee() external {
uint32 timeSinceParent = uint32(block.timestamp - L2.parentTimestamp());
assertEq(_getBasefeeAndPrint(timeSinceParent, 0, 0), 317_609_019);
assertEq(_getBasefeeAndPrint(timeSinceParent, 1, 0), 317_609_019);
assertEq(
_getBasefeeAndPrint(timeSinceParent, 1_000_000, 0), 320_423_332
);
assertEq(
_getBasefeeAndPrint(timeSinceParent, 5_000_000, 0), 332_018_053
);
assertEq(
_getBasefeeAndPrint(timeSinceParent, 10_000_000, 0), 347_305_199
);
assertEq(_getBasefeeAndPrint(timeSinceParent, 0), 538_808_482);

timeSinceParent = uint32(100 + block.timestamp - L2.parentTimestamp());
assertEq(_getBasefeeAndPrint(timeSinceParent, 0, 0), 54_544_902);
assertEq(_getBasefeeAndPrint(timeSinceParent, 1, 0), 54_544_902);
assertEq(_getBasefeeAndPrint(timeSinceParent, 1_000_000, 0), 55_028_221);
assertEq(_getBasefeeAndPrint(timeSinceParent, 5_000_000, 0), 57_019_452);
assertEq(
_getBasefeeAndPrint(timeSinceParent, 10_000_000, 0), 59_644_805
);
timeSinceParent += 1000;
assertEq(_getBasefeeAndPrint(timeSinceParent, 0), 538_808_482);
}

function _getBasefeeAndPrint(
uint32 timeSinceParent,
uint32 gasLimit,
uint32 parentGasUsed
)
private
Expand All @@ -175,12 +158,10 @@ contract TestTaikoL2 is Test {
Strings.toString(timeSinceParent),
", gasIssued=",
Strings.toString(gasIssued),
", gasLimit=",
Strings.toString(gasLimit),
", parentGasUsed=",
Strings.toString(parentGasUsed)
);
_basefee = L2.getBasefee(timeSinceParent, gasLimit, parentGasUsed);
_basefee = L2.getBasefee(timeSinceParent, parentGasUsed);
assertTrue(_basefee != 0);

_msg = string.concat(
Expand All @@ -194,7 +175,7 @@ contract TestTaikoL2 is Test {
console2.log(_msg);
}

function _getBasefeeAndPrint(
function _getBasefeeAndPrint2(
uint32 timeSinceNow,
uint32 gasLimit
)
Expand All @@ -203,7 +184,6 @@ contract TestTaikoL2 is Test {
{
return _getBasefeeAndPrint(
uint32(timeSinceNow + block.timestamp - L2.parentTimestamp()),
gasLimit,
gasLimit + ANCHOR_GAS_COST
);
}
Expand Down
8 changes: 2 additions & 6 deletions packages/protocol/test/genesis/GenerateGenesis.g.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract TestGenerateGenesis is Test, AddressResolver {
address private owner = configJSON.readAddress(".contractOwner");
address private admin = configJSON.readAddress(".contractAdmin");

uint32 public constant BLOCK_GAS_LIMIT = 30_000_000;
// uint32 public constant BLOCK_GAS_LIMIT = 30_000_000;

function testContractDeployment() public {
assertEq(block.chainid, 167);
Expand Down Expand Up @@ -107,11 +107,7 @@ contract TestGenerateGenesis is Test, AddressResolver {
for (uint32 i = 0; i < 300; i++) {
vm.roll(block.number + 1);
vm.warp(taikoL2.parentTimestamp() + 12);
vm.fee(
taikoL2.getBasefee(
12, BLOCK_GAS_LIMIT, i + LibL2Consts.ANCHOR_GAS_COST
)
);
vm.fee(taikoL2.getBasefee(12, i + LibL2Consts.ANCHOR_GAS_COST));

uint256 gasLeftBefore = gasleft();

Expand Down