Skip to content

Commit

Permalink
fix (protocol): add 4 PRs:
Browse files Browse the repository at this point in the history
  • Loading branch information
vikinatora committed Jan 22, 2025
1 parent 5b630e3 commit afd0c91
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 147 deletions.
5 changes: 3 additions & 2 deletions packages/protocol/contracts/layer2/based/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ contract TaikoL2 is EssentialContract, IBlockHash, TaikoL2Deprecated {
/// @return newGasExcess_ The new gas excess value.
function getBasefeeV2(
uint32 _parentGasUsed,
uint64 _blockTimestamp,
LibSharedData.BaseFeeConfig calldata _baseFeeConfig
)
public
Expand All @@ -201,7 +202,7 @@ contract TaikoL2 is EssentialContract, IBlockHash, TaikoL2Deprecated {
LibEIP1559.adjustExcess(parentGasTarget, newGasTarget, parentGasExcess);

uint64 gasIssuance =
uint64(block.timestamp - parentTimestamp) * _baseFeeConfig.gasIssuancePerSecond;
uint64(_blockTimestamp - parentTimestamp) * _baseFeeConfig.gasIssuancePerSecond;

if (
_baseFeeConfig.maxGasIssuancePerBlock != 0
Expand Down Expand Up @@ -292,7 +293,7 @@ contract TaikoL2 is EssentialContract, IBlockHash, TaikoL2Deprecated {
private
{
(uint256 basefee, uint64 newGasTarget, uint64 newGasExcess) =
getBasefeeV2(_parentGasUsed, _baseFeeConfig);
getBasefeeV2(_parentGasUsed, uint64(block.timestamp), _baseFeeConfig);

require(block.basefee == basefee || skipFeeCheck(), L2_BASEFEE_MISMATCH());

Expand Down
9 changes: 6 additions & 3 deletions packages/protocol/test/layer2/TaikoL2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ contract TaikoL2Tests is TaikoL2Test {
maxGasIssuancePerBlock: _maxGasIssuancePerBlock
});

(uint256 basefee_,,) = L2.getBasefeeV2(_parentGasUsed, baseFeeConfig);
(uint256 basefee_,,) =
L2.getBasefeeV2(_parentGasUsed, uint64(block.timestamp), baseFeeConfig);
assertTrue(basefee_ != 0, "basefee is 0");
}

Expand Down Expand Up @@ -156,15 +157,17 @@ contract TaikoL2Tests is TaikoL2Test {
vm.prank(L2.GOLDEN_TOUCH_ADDRESS());
L2.anchorV2(++anchorBlockId, anchorStateRoot, _parentGasUsed, baseFeeConfig);

(uint256 basefee, uint64 newGasTarget,) = L2.getBasefeeV2(_parentGasUsed, baseFeeConfig);
(uint256 basefee, uint64 newGasTarget,) =
L2.getBasefeeV2(_parentGasUsed, uint64(block.timestamp), baseFeeConfig);

assertTrue(basefee != 0, "basefee is 0");
assertEq(newGasTarget, L2.parentGasTarget());

// change the gas issuance to change the gas target
baseFeeConfig.gasIssuancePerSecond += 1;

(basefee, newGasTarget,) = L2.getBasefeeV2(_parentGasUsed, baseFeeConfig);
(basefee, newGasTarget,) =
L2.getBasefeeV2(_parentGasUsed, uint64(block.timestamp), baseFeeConfig);

assertTrue(basefee != 0, "basefee is 0");
assertTrue(newGasTarget != L2.parentGasTarget());
Expand Down
2 changes: 1 addition & 1 deletion packages/taiko-client/bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ab50d406172e879e7436c70a41bda674bfc6e441
c685e9d6d0b52be43fe8343a952fe24df268c20f
4 changes: 2 additions & 2 deletions packages/taiko-client/bindings/encoding/protocol_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var (
MaxBlocksToVerify: 16,
BlockMaxGasLimit: 240_000_000,
LivenessBond: livenessBond,
StateRootSyncInternal: 16,
StateRootSyncInternal: 4,
MaxAnchorHeightOffset: 64,
BaseFeeConfig: bindings.LibSharedDataBaseFeeConfig{
AdjustmentQuotient: 8,
Expand All @@ -85,7 +85,7 @@ var (
MinGasExcess: 1_340_000_000,
MaxGasIssuancePerBlock: 600_000_000,
},
OntakeForkHeight: 2,
OntakeForkHeight: 0,
}
UniFiTestnetProtocolConfig = &bindings.TaikoDataConfig{
ChainId: UniFiTestnetChainId,
Expand Down
29 changes: 0 additions & 29 deletions packages/taiko-client/bindings/gen_guardian_prover.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions packages/taiko-client/bindings/gen_sgx_verifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions packages/taiko-client/bindings/gen_taiko_l1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

241 changes: 199 additions & 42 deletions packages/taiko-client/bindings/gen_taiko_l2.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions packages/taiko-client/bindings/gen_tier_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 88 additions & 50 deletions packages/taiko-client/pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -281,66 +282,24 @@ func (c *Client) CalculateBaseFee(
currentTimestamp uint64,
) (*big.Int, error) {
var (
baseFeeInfo struct {
Basefee *big.Int
ParentGasExcess uint64
}
err error
baseFee *big.Int
err error
)

if isOntake {
var (
newGasTarget = uint64(baseFeeConfig.GasIssuancePerSecond) * uint64(baseFeeConfig.AdjustmentQuotient)
parentGasExcess uint64
)
parentGasTarget, err := c.TaikoL2.ParentGasTarget(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
)
if err != nil {
return nil, fmt.Errorf("failed to fetch parent gas target: %w", err)
}

if newGasTarget != parentGasTarget && parentGasTarget != 0 {
oldParentGasExcess, err := c.TaikoL2.ParentGasExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
)
if err != nil {
return nil, fmt.Errorf("failed to fetch old parent gas excess: %w", err)
}
if parentGasExcess, err = c.TaikoL2.AdjustExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
oldParentGasExcess,
parentGasTarget,
newGasTarget,
); err != nil {
return nil, fmt.Errorf("failed to adjust parent gas excess: %w", err)
}
} else {
if parentGasExcess, err = c.TaikoL2.ParentGasExcess(&bind.CallOpts{
BlockNumber: l2Head.Number, Context: ctx,
}); err != nil {
return nil, fmt.Errorf("failed to fetch parent gas excess: %w", err)
}
}
baseFeeInfo, err = c.TaikoL2.CalculateBaseFee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
*baseFeeConfig,
currentTimestamp-l2Head.Time,
parentGasExcess,
uint32(l2Head.GasUsed),
)
if err != nil {
if baseFee, err = c.calculateBaseFeeOntake(ctx, l2Head, currentTimestamp, baseFeeConfig); err != nil {
return nil, err
}
} else {
baseFeeInfo, err = c.TaikoL2.GetBasefee(
baseFeeInfo, err := c.TaikoL2.GetBasefee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
anchorBlockID.Uint64(),
uint32(l2Head.GasUsed),
)
if err != nil {
return nil, err
}
baseFee = baseFeeInfo.Basefee
}

if err != nil {
Expand All @@ -349,13 +308,13 @@ func (c *Client) CalculateBaseFee(

log.Info(
"Base fee information",
"fee", utils.WeiToGWei(baseFeeInfo.Basefee),
"fee", utils.WeiToGWei(baseFee),
"l2Head", l2Head.Number,
"anchorBlockID", anchorBlockID,
"isOntake", isOntake,
)

return baseFeeInfo.Basefee, nil
return baseFee, nil
}

// GetPoolContent fetches the transactions list from L2 execution engine's transactions pool with given
Expand All @@ -382,7 +341,7 @@ func (c *Client) GetPoolContent(
if err != nil {
return nil, err
}

baseFee, err := c.CalculateBaseFee(
ctx,
l2Head,
Expand Down Expand Up @@ -914,3 +873,82 @@ func (c *Client) WaitL1NewPendingTransaction(

return nil
}

// CalculateBaseFeeOnTake calculates the base fee after ontake fork from the L2 protocol.
func (c *Client) calculateBaseFeeOntake(
ctx context.Context,
l2Head *types.Header,
currentTimestamp uint64,
baseFeeConfig *bindings.LibSharedDataBaseFeeConfig,
) (*big.Int, error) {
var (
newGasTarget = uint64(baseFeeConfig.GasIssuancePerSecond) * uint64(baseFeeConfig.AdjustmentQuotient)
parentGasExcess uint64
)
parentGasTarget, err := c.TaikoL2.ParentGasTarget(&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to fetch parent gas target: %w", err)
}

if newGasTarget != parentGasTarget && parentGasTarget != 0 {
oldParentGasExcess, err := c.TaikoL2.ParentGasExcess(&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to fetch old parent gas excess: %w", err)
}
if parentGasExcess, err = c.TaikoL2.AdjustExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
oldParentGasExcess,
parentGasTarget,
newGasTarget,
); err != nil {
// If the `calculateBaseFee()` method is deprecated, we will use the new method `getBasefeeV2()`
// to calculate the base fee.
if strings.Contains(encoding.TryParsingCustomError(err).Error(), "L2_DEPRECATED_METHOD") {
baseFeeInfo, err := c.TaikoL2.GetBasefeeV2(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
uint32(l2Head.GasUsed),
currentTimestamp,
*baseFeeConfig,
)
if err != nil {
return nil, fmt.Errorf("failed to calculate base fee by GetBasefeeV2: %w", err)
}
return baseFeeInfo.Basefee, nil
}

return nil, fmt.Errorf("failed to adjust parent gas excess: %w", err)
}
} else {
if parentGasExcess, err = c.TaikoL2.ParentGasExcess(&bind.CallOpts{
BlockNumber: l2Head.Number, Context: ctx,
}); err != nil {
return nil, fmt.Errorf("failed to fetch parent gas excess: %w", err)
}
}

baseFeeInfo, err := c.TaikoL2.CalculateBaseFee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
*baseFeeConfig,
currentTimestamp-l2Head.Time,
parentGasExcess,
uint32(l2Head.GasUsed),
)
if err != nil {
// If the `calculateBaseFee()` method is deprecated, we will use the new method `getBasefeeV2()`
// to calculate the base fee.
if strings.Contains(encoding.TryParsingCustomError(err).Error(), "L2_DEPRECATED_METHOD") {
baseFeeInfo, err := c.TaikoL2.GetBasefeeV2(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
uint32(l2Head.GasUsed),
currentTimestamp,
*baseFeeConfig,
)
if err != nil {
return nil, fmt.Errorf("failed to calculate base fee by GetBasefeeV2: %w", err)
}
return baseFeeInfo.Basefee, nil
}
}

return baseFeeInfo.Basefee, nil
}

0 comments on commit afd0c91

Please sign in to comment.