Skip to content

Commit

Permalink
fix(protocol): fix getTierProvider and apply additional gas optimizat…
Browse files Browse the repository at this point in the history
…ion (#17488)

Co-authored-by: Daniel Wang <dong77@gmail.com>
  • Loading branch information
dantaik and dong77 committed Jun 6, 2024
1 parent f58d22f commit daa7aa7
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ library TaikoData {
// ---------------------------------------------------------------------
// The max number of L2 blocks that can stay unsynced on L1
uint8 blockSyncThreshold;
bool checkEOAForCalldataDA;
}

/// @dev Struct representing prover fees per given tier
Expand Down
29 changes: 16 additions & 13 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
returns (TaikoData.BlockMetadata memory meta_, TaikoData.EthDeposit[] memory deposits_)
{
TaikoData.Config memory config = getConfig();
(meta_, deposits_) = LibProposing.proposeBlock(
state, config, this, _params, _txList, _checkEOAForCalldataDA()
);
IERC20 tko = IERC20(resolve(LibStrings.B_TAIKO_TOKEN, false));

(meta_, deposits_) = LibProposing.proposeBlock(state, tko, config, this, _params, _txList);

if (!state.slotB.provingPaused) {
LibVerifying.verifyBlocks(state, config, this, config.maxBlocksToVerifyPerProposal);
LibVerifying.verifyBlocks(state, tko, config, this, config.maxBlocksToVerifyPerProposal);
}
}

Expand All @@ -112,10 +112,10 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
if (_blockId != meta.id) revert L1_INVALID_BLOCK_ID();

TaikoData.Config memory config = getConfig();
IERC20 tko = IERC20(resolve(LibStrings.B_TAIKO_TOKEN, false));

uint8 maxBlocksToVerify = LibProving.proveBlock(state, config, this, meta, tran, proof);

LibVerifying.verifyBlocks(state, config, this, maxBlocksToVerify);
uint8 maxBlocksToVerify = LibProving.proveBlock(state, tko, config, this, meta, tran, proof);
LibVerifying.verifyBlocks(state, tko, config, this, maxBlocksToVerify);
}

/// @inheritdoc ITaikoL1
Expand All @@ -126,7 +126,13 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
nonReentrant
emitEventForClient
{
LibVerifying.verifyBlocks(state, getConfig(), this, _maxBlocksToVerify);
LibVerifying.verifyBlocks(
state,
IERC20(resolve(LibStrings.B_TAIKO_TOKEN, false)),
getConfig(),
this,
_maxBlocksToVerify
);
}

/// @inheritdoc ITaikoL1
Expand Down Expand Up @@ -223,7 +229,8 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
// read Taiko's gas limit to be 240_250_000.
blockMaxGasLimit: 240_000_000,
livenessBond: 250e18, // 250 Taiko token
blockSyncThreshold: 16
blockSyncThreshold: 16,
checkEOAForCalldataDA: true
});
}

Expand All @@ -238,8 +245,4 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
override
onlyFromOwnerOrNamed(LibStrings.B_CHAIN_WATCHDOG)
{ }

function _checkEOAForCalldataDA() internal pure virtual returns (bool) {
return true;
}
}
22 changes: 13 additions & 9 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ library LibProposing {

/// @dev Proposes a Taiko L2 block.
/// @param _state Current TaikoData.State.
/// @param _tko The taiko token.
/// @param _config Actual TaikoData.Config.
/// @param _resolver Address resolver interface.
/// @param _data Encoded data bytes containing the block params.
/// @param _txList Transaction list bytes (if not blob).
/// @return meta_ The constructed block's metadata.
function proposeBlock(
TaikoData.State storage _state,
IERC20 _tko,
TaikoData.Config memory _config,
IAddressResolver _resolver,
bytes calldata _data,
bytes calldata _txList,
bool _checkEOAForCalldataDA
bytes calldata _txList
)
internal
returns (TaikoData.BlockMetadata memory meta_, TaikoData.EthDeposit[] memory deposits_)
Expand Down Expand Up @@ -132,7 +133,7 @@ library LibProposing {
// We cannot rely on `msg.sender != tx.origin` for EOA check, as it will break after EIP
// 7645: Alias ORIGIN to SENDER
if (
_checkEOAForCalldataDA
_config.checkEOAForCalldataDA
&& ECDSA.recover(meta_.blobHash, params.signature) != msg.sender
) {
revert L1_INVALID_SIG();
Expand All @@ -146,9 +147,13 @@ library LibProposing {
// number as the L2 mixHash.
meta_.difficulty = keccak256(abi.encodePacked(block.prevrandao, b.numBlocks, block.number));

// Use the difficulty as a random number
meta_.minTier =
LibUtils.getTierProvider(_resolver, b.numBlocks).getMinTier(uint256(meta_.difficulty));
{
ITierRouter tierRouter = ITierRouter(_resolver.resolve(LibStrings.B_TIER_ROUTER, false));
ITierProvider tierProvider = ITierProvider(tierRouter.getProvider(b.numBlocks));

// Use the difficulty as a random number
meta_.minTier = tierProvider.getMinTier(uint256(meta_.difficulty));
}

// Create the block that will be stored onchain
TaikoData.Block memory blk = TaikoData.Block({
Expand Down Expand Up @@ -176,8 +181,7 @@ library LibProposing {
}

{
IERC20 tko = IERC20(_resolver.resolve(LibStrings.B_TAIKO_TOKEN, false));
uint256 tkoBalance = tko.balanceOf(address(this));
uint256 tkoBalance = _tko.balanceOf(address(this));

// Run all hooks.
// Note that address(this).balance has been updated with msg.value,
Expand Down Expand Up @@ -207,7 +211,7 @@ library LibProposing {
// have increased by the same amount as _config.livenessBond (to prevent)
// multiple draining payments by a malicious proposer nesting the same
// hook.
if (tko.balanceOf(address(this)) != tkoBalance + _config.livenessBond) {
if (_tko.balanceOf(address(this)) != tkoBalance + _config.livenessBond) {
revert L1_LIVENESS_BOND_NOT_RECEIVED();
}
}
Expand Down
17 changes: 11 additions & 6 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ library LibProving {

/// @dev Proves or contests a block transition.
/// @param _state Current TaikoData.State.
/// @param _tko The taiko token.
/// @param _config Actual TaikoData.Config.
/// @param _resolver Address resolver interface.
/// @param _meta The block's metadata.
Expand All @@ -94,6 +95,7 @@ library LibProving {
/// @return The number of blocks to be verified with this transaction.
function proveBlock(
TaikoData.State storage _state,
IERC20 _tko,
TaikoData.Config memory _config,
IAddressResolver _resolver,
TaikoData.BlockMetadata memory _meta,
Expand Down Expand Up @@ -147,9 +149,13 @@ library LibProving {

// Retrieve the tier configurations. If the tier is not supported, the
// subsequent action will result in a revert.
ITierProvider tierProvider = LibUtils.getTierProvider(_resolver, local.blockId);
local.tier = tierProvider.getTier(_proof.tier);
local.minTier = tierProvider.getTier(_meta.minTier);
{
ITierRouter tierRouter = ITierRouter(_resolver.resolve(LibStrings.B_TIER_ROUTER, false));
ITierProvider tierProvider = ITierProvider(tierRouter.getProvider(local.blockId));

local.tier = tierProvider.getTier(_proof.tier);
local.minTier = tierProvider.getTier(_meta.minTier);
}

local.inProvingWindow = !LibUtils.isPostDeadline({
_tsTimestamp: ts.timestamp,
Expand Down Expand Up @@ -200,13 +206,12 @@ library LibProving {

local.isTopTier = local.tier.contestBond == 0;
local.sameTransition = _tran.blockHash == ts.blockHash && _tran.stateRoot == ts.stateRoot;
IERC20 tko = IERC20(_resolver.resolve(LibStrings.B_TAIKO_TOKEN, false));

if (_proof.tier > ts.tier) {
// Handles the case when an incoming tier is higher than the current transition's tier.
// Reverts when the incoming proof tries to prove the same transition
// (L1_ALREADY_PROVED).
_overrideWithHigherProof(blk, ts, _tran, _proof, local, tko);
_overrideWithHigherProof(blk, ts, _tran, _proof, local, _tko);

emit TransitionProved({
blockId: local.blockId,
Expand Down Expand Up @@ -252,7 +257,7 @@ library LibProving {

// _checkIfContestable(/*_state,*/ tier.cooldownWindow, ts.timestamp);
// Burn the contest bond from the prover.
tko.transferFrom(msg.sender, address(this), local.tier.contestBond);
_tko.transferFrom(msg.sender, address(this), local.tier.contestBond);

// We retain the contest bond within the transition, just in
// case this configuration is altered to a different value
Expand Down
12 changes: 0 additions & 12 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,4 @@ library LibUtils {
return block.timestamp >= deadline;
}
}

function getTierProvider(
IAddressResolver _resolver,
uint256 _blockId
)
internal
view
returns (ITierProvider)
{
ITierRouter tierRouter = ITierRouter(_resolver.resolve(LibStrings.B_TIER_ROUTER, false));
return ITierProvider(tierRouter.getProvider(_blockId));
}
}
21 changes: 12 additions & 9 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ library LibVerifying {
/// @dev Verifies up to N blocks.
function verifyBlocks(
TaikoData.State storage _state,
IERC20 _tko,
TaikoData.Config memory _config,
IAddressResolver _resolver,
uint64 _maxBlocksToVerify
Expand Down Expand Up @@ -93,9 +94,7 @@ library LibVerifying {
bytes32 blockHash = _state.transitions[slot][tid].blockHash;
bytes32 stateRoot;
uint64 numBlocksVerified;
ITierProvider tierProvider;

IERC20 tko = IERC20(_resolver.resolve(LibStrings.B_TAIKO_TOKEN, false));
ITierRouter tierRouter;

// Unchecked is safe:
// - assignment is within ranges
Expand All @@ -122,18 +121,21 @@ library LibVerifying {
// It's not possible to verify this block if either the
// transition is contested and awaiting higher-tier proof or if
// the transition is still within its cooldown period.
uint16 tier = ts.tier;

if (ts.contester != address(0)) {
break;
} else {
if (tierProvider == ITierProvider(address(0))) {
tierProvider = LibUtils.getTierProvider(_resolver, blockId);
if (tierRouter == ITierRouter(address(0))) {
tierRouter = ITierRouter(_resolver.resolve(LibStrings.B_TIER_ROUTER, false));
}

if (
!LibUtils.isPostDeadline(
ts.timestamp,
b.lastUnpausedAt,
tierProvider.getTier(ts.tier).cooldownWindow
ITierProvider(tierRouter.getProvider(blockId)).getTier(tier)
.cooldownWindow
)
) {
// If cooldownWindow is 0, the block can theoretically
Expand All @@ -149,7 +151,8 @@ library LibVerifying {
blockHash = ts.blockHash;
stateRoot = ts.stateRoot;

tko.transfer(ts.prover, ts.validityBond);
address prover = ts.prover;
_tko.transfer(prover, ts.validityBond);

// Note: We exclusively address the bonds linked to the
// transition used for verification. While there may exist
Expand All @@ -160,10 +163,10 @@ library LibVerifying {

emit BlockVerified({
blockId: blockId,
prover: ts.prover,
prover: prover,
blockHash: blockHash,
stateRoot: stateRoot,
tier: ts.tier
tier: tier
});

++blockId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ contract L1RollupAddressManager is AddressManager {
if (_name == LibStrings.B_AUTOMATA_DCAP_ATTESTATION) {
return 0x8d7C954960a36a7596d7eA4945dDf891967ca8A3;
}
if (_name == LibStrings.B_TIER_ROUTER) {
return 0xa8e5D3a2E2052bea7f10bE6a0386454b721d1f9F;
}
}
return address(0);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/deployments/mainnet-contract-logs-L1.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
- register `prover_set` to `0x500735343372Dd6c9B84dBc7a75babf4479742B9` @commit`fa481c1` @tx`0x02ed558762eae5f0a930ba4a1047a02d4a793ea48890268c32df04e882f138ff`
- upgraded from `0xF1cA1F1A068468E1dcF90dA6add185467de80943` to `0x8Af4669E3068Bae96b92cD73603f5D86beD07a9a` @commit`e79a367ad` @tx`0xe1ef58455de0b0331228e487d54720290ed8a73f709d2146bd43330d4a360bd3`
- register `tier_router` to `0xa8e5D3a2E2052bea7f10bE6a0386454b721d1f9F` and unregister `tier_provider` @tx`0x2c455ae888a23c232bb5c7603657eda010ffadc602a74e626332bc06eaaa3b78`
- todo:
- update the contract using latest L1RollupAddressManager.sol

#### taikoL1

Expand Down
5 changes: 1 addition & 4 deletions packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ contract TaikoL1_NoCooldown is TaikoL1 {
config.blockMaxProposals = 10;
config.blockRingBufferSize = 12;
config.livenessBond = 1e18; // 1 Taiko token
}

function _checkEOAForCalldataDA() internal pure override returns (bool) {
return false;
config.checkEOAForCalldataDA = false;
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/protocol/test/L1/TaikoL1LibProvingWithTiers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ contract TaikoL1Tiers is TaikoL1 {
config.blockMaxProposals = 10;
config.blockRingBufferSize = 12;
config.livenessBond = 1e18; // 1 Taiko token
}

function _checkEOAForCalldataDA() internal pure override returns (bool) {
return false;
config.checkEOAForCalldataDA = false;
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/protocol/test/L1/TaikoL1TestGroupBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract TaikoL1New is TaikoL1 {
config.maxBlocksToVerifyPerProposal = 0;
config.blockMaxProposals = 10;
config.blockRingBufferSize = 20;
}

function _checkEOAForCalldataDA() internal pure override returns (bool) {
return true;
config.checkEOAForCalldataDA = true;
}
}

Expand Down

0 comments on commit daa7aa7

Please sign in to comment.