Skip to content

Commit

Permalink
feat(protocol): allow tier configuration based on block numbers. (#17399
Browse files Browse the repository at this point in the history
)

Co-authored-by: Brecht Devos <[email protected]>
  • Loading branch information
dantaik and Brechtpd authored Jun 3, 2024
1 parent 0febafe commit 3e50e1c
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 64 deletions.
9 changes: 3 additions & 6 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
pragma solidity 0.8.24;

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../../common/IAddressResolver.sol";
import "../../common/LibStrings.sol";
import "../../libs/LibAddress.sol";
import "../../libs/LibNetwork.sol";
import "../hooks/IHook.sol";
import "../tiers/ITierProvider.sol";
import "./LibUtils.sol";

/// @title LibProposing
/// @notice A library for handling block proposals in the Taiko protocol.
Expand Down Expand Up @@ -155,8 +152,8 @@ library LibProposing {
meta_.difficulty = keccak256(abi.encodePacked(block.prevrandao, b.numBlocks, block.number));

// Use the difficulty as a random number
meta_.minTier = ITierProvider(_resolver.resolve(LibStrings.B_TIER_PROVIDER, false))
.getMinTier(uint256(meta_.difficulty));
meta_.minTier =
LibUtils.getTierProvider(_resolver, b.numBlocks).getMinTier(uint256(meta_.difficulty));

// Create the block that will be stored onchain
TaikoData.Block memory blk = TaikoData.Block({
Expand Down
11 changes: 2 additions & 9 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../common/IAddressResolver.sol";
import "../../common/LibStrings.sol";
import "../../verifiers/IVerifier.sol";
import "../tiers/ITierProvider.sol";
import "./LibUtils.sol";

/// @title LibProving
Expand Down Expand Up @@ -153,9 +148,7 @@ library LibProving {

// Retrieve the tier configurations. If the tier is not supported, the
// subsequent action will result in a revert.
ITierProvider tierProvider =
ITierProvider(_resolver.resolve(LibStrings.B_TIER_PROVIDER, false));

ITierProvider tierProvider = LibUtils.getTierProvider(_resolver, local.blockId);
local.tier = tierProvider.getTier(_proof.tier);
local.minTier = tierProvider.getTier(_meta.minTier);

Expand All @@ -166,7 +159,7 @@ library LibProving {
});

// Checks if only the assigned prover is permissioned to prove the block.
// The guardian prover is granted exclusive permission to prove only the first
// The assigned prover is granted exclusive permission to prove only the first
// transition.
if (
local.tier.contestBond != 0 && ts.contester == address(0) && local.tid == 1
Expand Down
19 changes: 19 additions & 0 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import "../../common/IAddressResolver.sol";
import "../../common/LibStrings.sol";
import "../../libs/LibMath.sol";
import "../tiers/ITierProvider.sol";
import "../tiers/ITierRouter.sol";
import "../TaikoData.sol";

/// @title LibUtils
Expand Down Expand Up @@ -117,4 +124,16 @@ 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));
}
}
13 changes: 4 additions & 9 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../common/IAddressResolver.sol";
import "../../common/LibStrings.sol";
import "../../signal/ISignalService.sol";
import "../tiers/ITierProvider.sol";
import "./LibUtils.sol";

/// @title LibVerifying
Expand Down Expand Up @@ -99,7 +94,7 @@ library LibVerifying {
bytes32 blockHash = _state.transitions[slot][tid].blockHash;
bytes32 stateRoot;
uint64 numBlocksVerified;
address tierProvider;
ITierProvider tierProvider;

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

Expand Down Expand Up @@ -131,15 +126,15 @@ library LibVerifying {
if (ts.contester != address(0)) {
break;
} else {
if (tierProvider == address(0)) {
tierProvider = _resolver.resolve(LibStrings.B_TIER_PROVIDER, false);
if (tierProvider == ITierProvider(address(0))) {
tierProvider = LibUtils.getTierProvider(_resolver, blockId);
}

if (
!LibUtils.isPostDeadline(
ts.timestamp,
b.lastUnpausedAt,
ITierProvider(tierProvider).getTier(ts.tier).cooldownWindow
tierProvider.getTier(ts.tier).cooldownWindow
)
) {
// If cooldownWindow is 0, the block can theoretically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.24;
import "./TierProviderBase.sol";

/// @title DevnetTierProvider
/// @dev Labeled in AddressResolver as "tier_provider"
/// @custom:security-contact [email protected]
contract DevnetTierProvider is TierProviderBase {
/// @inheritdoc ITierProvider
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/tiers/ITierProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ITierProvider {
function getTierIds() external view returns (uint16[] memory);

/// @dev Determines the minimal tier for a block based on a random input.
/// @param rand (Semi) random number.
/// @param rand A pseudo-random number.
/// @return The tier id.
function getMinTier(uint256 rand) external view returns (uint16);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/protocol/contracts/L1/tiers/ITierRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

/// @title ITierRouter
/// @notice Defines interface to return an ITierProvider
/// @custom:security-contact [email protected]
interface ITierRouter {
/// @dev Returns the address of the TierProvider for a given block.
/// @param blockId ID of the block.
/// @return The address of the corresponding TierProvider.
function getProvider(uint256 blockId) external view returns (address);
}
1 change: 0 additions & 1 deletion packages/protocol/contracts/L1/tiers/TierProviderV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.24;
import "./TierProviderBase.sol";

/// @title TierProviderV2
/// @dev Labeled in AddressResolver as "tier_provider"
/// @custom:security-contact [email protected]
contract TierProviderV2 is TierProviderBase {
/// @inheritdoc ITierProvider
Expand Down
1 change: 0 additions & 1 deletion packages/protocol/contracts/L1/tiers/TierProviderV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.24;
import "./TierProviderBase.sol";

/// @title TierProviderV3
/// @dev Labeled in AddressResolver as "tier_provider"
/// @custom:security-contact [email protected]
contract TierProviderV3 is TierProviderBase {
/// @inheritdoc ITierProvider
Expand Down
14 changes: 14 additions & 0 deletions packages/protocol/contracts/L1/tiers/TierRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "./ITierRouter.sol";

/// @title TierRouter
/// @dev Labeled in AddressResolver as "tier_router"
/// @custom:security-contact [email protected]
contract TierRouter is ITierRouter {
/// @inheritdoc ITierRouter
function getProvider(uint256 /*_blockId*/ ) external pure returns (address) {
return 0x4cffe56C947E26D07C14020499776DB3e9AE3a23;
}
}
2 changes: 1 addition & 1 deletion packages/protocol/contracts/common/LibStrings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ library LibStrings {
bytes32 internal constant B_TAIKO_TOKEN = bytes32("taiko_token");
bytes32 internal constant B_TIER_GUARDIAN = bytes32("tier_guardian");
bytes32 internal constant B_TIER_GUARDIAN_MINORITY = bytes32("tier_guardian_minority");
bytes32 internal constant B_TIER_PROVIDER = bytes32("tier_provider");
bytes32 internal constant B_TIER_ROUTER = bytes32("tier_router");
bytes32 internal constant B_TIER_SGX = bytes32("tier_sgx");
bytes32 internal constant B_TIER_SGX_ZKVM = bytes32("tier_sgx_zkvm");
bytes32 internal constant B_WITHDRAWER = bytes32("withdrawer");
Expand Down
29 changes: 0 additions & 29 deletions packages/protocol/script/DeployTaikoTokenOnL2.s.sol

This file was deleted.

4 changes: 2 additions & 2 deletions packages/protocol/script/L2PostGenesisConfig.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface USDCProxy {
// forge script --rpc-url https://rpc.mainnet.taiko.xyz script/L2PostGenesisConfig.s.sol
contract L2PostGenesisConfig is Script {
// All following addresses are L2 addresses
address public bridgedTKO = 0xA9d23408b9bA935c230493c40C73824Df71A0975; // TODO
address public bridgedUSDC = 0x07d83526730c7438048D55A4fc0b850e2aaB6f0b; // TODO
address public bridgedTKO = 0xA9d23408b9bA935c230493c40C73824Df71A0975;
address public bridgedUSDC = 0x07d83526730c7438048D55A4fc0b850e2aaB6f0b;

address public erc20Vault = 0x1670000000000000000000000000000000000002;
address public bridge = 0x1670000000000000000000000000000000000001;
Expand Down
5 changes: 3 additions & 2 deletions packages/protocol/test/L1/TaikoL1TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ abstract contract TaikoL1TestBase is TaikoTest {
function deployTaikoL1() internal virtual returns (TaikoL1 taikoL1);

function tierProvider() internal view returns (ITierProvider) {
return ITierProvider(L1.resolve(LibStrings.B_TIER_PROVIDER, false));
ITierRouter tierRouter = ITierRouter(L1.resolve(LibStrings.B_TIER_ROUTER, false));
return ITierProvider(tierRouter.getProvider(0));
}

function setUp() public virtual {
Expand Down Expand Up @@ -98,7 +99,7 @@ abstract contract TaikoL1TestBase is TaikoTest {
registerAddress("taiko", address(L1));
registerAddress("tier_sgx", address(sv));
registerAddress("tier_guardian", address(gp));
registerAddress("tier_provider", address(cp));
registerAddress("tier_router", address(cp));
registerAddress("signal_service", address(ss));
registerL2Address("taiko", address(L2));
registerL2Address("signal_service", address(L2SS));
Expand Down
10 changes: 8 additions & 2 deletions packages/protocol/test/L1/TestTierProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ pragma solidity 0.8.24;

import "../../contracts/common/LibStrings.sol";
import "../../contracts/L1/tiers/ITierProvider.sol";
import "../../contracts/L1/tiers/ITierRouter.sol";

/// @title TestTierProvider
/// @dev Labeled in AddressResolver as "tier_provider"
/// @dev Labeled in AddressResolver as "tier_router"
/// @custom:security-contact [email protected]
contract TestTierProvider is ITierProvider {
contract TestTierProvider is ITierProvider, ITierRouter {
uint256[50] private __gap;

/// @inheritdoc ITierRouter
function getProvider(uint256) external view returns (address) {
return address(this);
}
/// @inheritdoc ITierProvider

function getTier(uint16 _tierId) public pure override returns (ITierProvider.Tier memory) {
if (_tierId == LibTiers.TIER_OPTIMISTIC) {
return ITierProvider.Tier({
Expand Down

0 comments on commit 3e50e1c

Please sign in to comment.