Skip to content

Commit

Permalink
refactor(protocol): refactor tier provider & address caching and add …
Browse files Browse the repository at this point in the history
…ComposeVerifier (#17960)

Co-authored-by: dantaik <[email protected]>
Co-authored-by: D <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2024
1 parent 80176a1 commit ee464ca
Show file tree
Hide file tree
Showing 47 changed files with 1,097 additions and 630 deletions.
596 changes: 345 additions & 251 deletions packages/protocol/contract_layout.md

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions packages/protocol/contracts/L1/tiers/ITierProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,3 @@ interface ITierProvider {
/// @return The tier id.
function getMinTier(address proposer, uint256 rand) external view returns (uint16);
}

/// @dev Tier ID cannot be zero!
library LibTiers {
/// @notice Optimistic tier ID.
uint16 public constant TIER_OPTIMISTIC = 100;

/// @notice SGX tier ID.
uint16 public constant TIER_SGX = 200;
uint16 public constant TIER_SGX2 = 200;

// @notice ZKVM risc0 tier ID
uint16 public constant TIER_ZKVM_RISC0 = 290;

/// @notice SGX + ZKVM tier ID.
uint16 public constant TIER_SGX_ZKVM = 300;

/// @notice Guardian tier ID with minority approval.
uint16 public constant TIER_GUARDIAN_MINORITY = 900;

/// @notice Guardian tier ID with majority approval.
uint16 public constant TIER_GUARDIAN = 1000;
}
34 changes: 34 additions & 0 deletions packages/protocol/contracts/L1/tiers/LibTiers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

/// @title LibTiers
/// @dev Tier ID cannot be zero!
/// @custom:security-contact [email protected]
library LibTiers {
/// @notice Optimistic tier ID.
uint16 public constant TIER_OPTIMISTIC = 100;

/// @notice SGX proof
uint16 public constant TIER_SGX = 200;

/// @notice TDX proof
uint16 public constant TIER_TDX = 200;

/// @notice Any TEE proof
uint16 public constant TIER_TEE_ANY = 200;

/// @notice Risc0's ZKVM proof
uint16 public constant TIER_ZKVM_RISC0 = 290;

/// @notice SP1's ZKVM proof
uint16 public constant TIER_ZKVM_SP1 = 290;

/// @notice Any ZKVM proof
uint16 public constant TIER_ZKVM_ANY = 290;

/// @notice Guardian tier ID with minority approval.
uint16 public constant TIER_GUARDIAN_MINORITY = 900;

/// @notice Guardian tier ID with majority approval.
uint16 public constant TIER_GUARDIAN = 1000;
}
144 changes: 67 additions & 77 deletions packages/protocol/contracts/L1/tiers/TierProviderBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.24;

import "../../common/LibStrings.sol";
import "./ITierProvider.sol";
import "./LibTiers.sol";

/// @title TierProviderBase
/// @custom:security-contact [email protected]
Expand All @@ -12,97 +13,86 @@ abstract contract TierProviderBase is ITierProvider {
/// service may be paused if gas prices are excessively high. Since block proving is
/// asynchronous, this grace period allows provers to defer submissions until gas
/// prices become more favorable, potentially reducing transaction costs.
uint16 public constant GRACE_PERIOD = 240; // 4 hours
uint16 public constant GRACE_PERIOD = 240; // minutes
uint96 public constant BOND_UNIT = 75 ether; // TAIKO tokens

/// @inheritdoc ITierProvider
/// @notice Each tier, except the top tier, has a validity bond that is 50 TAIKO higher than the
/// @notice Each tier, except the top tier, has a validity bond that is 75 TAIKO higher than the
/// previous tier. Additionally, each tier's contest bond is 6.5625 times its validity bond.
function getTier(
uint16 _tierId
)
public
pure
virtual
override
returns (ITierProvider.Tier memory)
{
function getTier(uint16 _tierId) public pure virtual returns (ITierProvider.Tier memory) {
if (_tierId == LibTiers.TIER_OPTIMISTIC) {
return ITierProvider.Tier({
verifierName: "",
validityBond: 100 ether, // TAIKO
contestBond: 656.25 ether, // = 100 TAIKO * 6.5625
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 15, // 15 minutes
maxBlocksToVerifyPerProof: 0
});
}

if (_tierId == LibTiers.TIER_SGX) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_SGX,
validityBond: 150 ether, // TAIKO
contestBond: 984.375 ether, // = 150 TAIKO * 6.5625
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 60, // 1 hour
maxBlocksToVerifyPerProof: 0
});
// cooldownWindow is 1440 minutes and provingWindow is 15 minutes
return _buildTier("", BOND_UNIT, 1440, 15);
}

if (_tierId == LibTiers.TIER_SGX2) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_SGX2,
validityBond: 150 ether, // TAIKO
contestBond: 984.375 ether, // = 150 TAIKO * 6.5625
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 60, // 1 hour
maxBlocksToVerifyPerProof: 0
});
}

if (_tierId == LibTiers.TIER_ZKVM_RISC0) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_ZKVM_RISC0,
validityBond: 250 ether, // TAIKO
contestBond: 1640.625 ether, // = 250 TAIKO * 6.5625
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 180, // 3 hours
maxBlocksToVerifyPerProof: 0
});
}
// TEE Tiers
if (_tierId == LibTiers.TIER_SGX) return _buildTeeTier(LibStrings.B_TIER_SGX);
if (_tierId == LibTiers.TIER_TDX) return _buildTeeTier(LibStrings.B_TIER_TDX);
if (_tierId == LibTiers.TIER_TEE_ANY) return _buildTeeTier(LibStrings.B_TIER_TEE_ANY);

if (_tierId == LibTiers.TIER_SGX_ZKVM) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_SGX_ZKVM,
validityBond: 300 ether, // TAIKO
contestBond: 1968.75 ether, // = 300 TAIKO * 6.5625
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 240, // 4 hours
maxBlocksToVerifyPerProof: 0
});
}
// ZKVM Tiers
if (_tierId == LibTiers.TIER_ZKVM_RISC0) return _buildZkTier(LibStrings.B_TIER_ZKVM_RISC0);
if (_tierId == LibTiers.TIER_ZKVM_SP1) return _buildZkTier(LibStrings.B_TIER_ZKVM_SP1);
if (_tierId == LibTiers.TIER_ZKVM_ANY) return _buildZkTier(LibStrings.B_TIER_ZKVM_ANY);

if (_tierId == LibTiers.TIER_GUARDIAN_MINORITY) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_GUARDIAN_MINORITY,
validityBond: 350 ether, // TAIKO
contestBond: 2296.875 ether, // = 350 TAIKO * 6.5625
cooldownWindow: GRACE_PERIOD + 240, // 4 hours
provingWindow: 2880, // 48 hours
maxBlocksToVerifyPerProof: 0
});
// cooldownWindow is 240 minutes and provingWindow is 2880 minutes
return _buildTier(LibStrings.B_TIER_GUARDIAN_MINORITY, BOND_UNIT * 4, 240, 2880);
}

if (_tierId == LibTiers.TIER_GUARDIAN) {
return ITierProvider.Tier({
verifierName: LibStrings.B_TIER_GUARDIAN,
validityBond: 0, // must be 0 for top tier
contestBond: 0, // must be 0 for top tier
cooldownWindow: 1440, // 24 hours
provingWindow: GRACE_PERIOD + 2880, // 48 hours
maxBlocksToVerifyPerProof: 0
});
// cooldownWindow is 1440 minutes and provingWindow is 2880 minutes
return _buildTier(LibStrings.B_TIER_GUARDIAN, 0, 1440, 2880);
}

revert TIER_NOT_FOUND();
}

/// @dev Builds a TEE tier with a specific verifier name.
/// @param _verifierName The name of the verifier.
/// @return A Tier struct with predefined parameters for TEE.
function _buildTeeTier(
bytes32 _verifierName
)
private
pure
returns (ITierProvider.Tier memory)
{
// cooldownWindow is 1440 minutes and provingWindow is 60 minutes
return _buildTier(_verifierName, BOND_UNIT * 2, 1440, 60);
}

/// @dev Builds a ZK tier with a specific verifier name.
/// @param _verifierName The name of the verifier.
/// @return A Tier struct with predefined parameters for ZK.
function _buildZkTier(bytes32 _verifierName) private pure returns (ITierProvider.Tier memory) {
// cooldownWindow is 1440 minutes and provingWindow is 180 minutes
return _buildTier(_verifierName, BOND_UNIT * 3, 1440, 180);
}

/// @dev Builds a generic tier with specified parameters.
/// @param _verifierName The name of the verifier.
/// @param _validityBond The validity bond amount.
/// @param _cooldownWindow The cooldown window duration in minutes.
/// @param _provingWindow The proving window duration in minutes.
/// @return A Tier struct with the provided parameters.
function _buildTier(
bytes32 _verifierName,
uint96 _validityBond,
uint16 _cooldownWindow,
uint16 _provingWindow
)
private
pure
returns (ITierProvider.Tier memory)
{
return ITierProvider.Tier({
verifierName: _verifierName,
validityBond: _validityBond,
contestBond: _validityBond / 10_000 * 65_625,
cooldownWindow: _cooldownWindow,
provingWindow: GRACE_PERIOD + _provingWindow,
maxBlocksToVerifyPerProof: 0
});
}
}
21 changes: 0 additions & 21 deletions packages/protocol/contracts/L1/tiers/TierProviderV3.sol

This file was deleted.

6 changes: 4 additions & 2 deletions packages/protocol/contracts/common/LibStrings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ library LibStrings {
bytes32 internal constant B_TIER_GUARDIAN_MINORITY = bytes32("tier_guardian_minority");
bytes32 internal constant B_TIER_ROUTER = bytes32("tier_router");
bytes32 internal constant B_TIER_SGX = bytes32("tier_sgx");
bytes32 internal constant B_TIER_SGX2 = bytes32("tier_sgx2");
bytes32 internal constant B_TIER_TDX = bytes32("tier_tdx");
bytes32 internal constant B_TIER_TEE_ANY = bytes32("tier_tee_any");
bytes32 internal constant B_TIER_ZKVM_RISC0 = bytes32("tier_zkvm_risc0");
bytes32 internal constant B_TIER_SGX_ZKVM = bytes32("tier_sgx_zkvm");
bytes32 internal constant B_TIER_ZKVM_SP1 = bytes32("tier_zkvm_sp1");
bytes32 internal constant B_TIER_ZKVM_ANY = bytes32("tier_zkvm_any");
bytes32 internal constant B_RISCZERO_GROTH16_VERIFIER = bytes32("risc0_groth16_verifier");
bytes32 internal constant B_WITHDRAWER = bytes32("withdrawer");
bytes32 internal constant H_RETURN_LIVENESS_BOND = keccak256("RETURN_LIVENESS_BOND");
Expand Down
55 changes: 0 additions & 55 deletions packages/protocol/contracts/mainnet/LibRollupAddressCache.sol

This file was deleted.

43 changes: 43 additions & 0 deletions packages/protocol/contracts/mainnet/addrcache/AddressCache.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

/// @title AddressCache
/// @custom:security-contact [email protected]
abstract contract AddressCache {
/// @notice This function retrieves the address associated with a given chain ID and name.
/// If the address is not found in the cache, it falls back to the provided function.
/// @param _chainId The chain ID for which the address is to be retrieved.
/// @param _name The name associated with the address to be retrieved.
/// @param _fallbackFunc The fallback function to be used if the address is not found in the
/// cache.
/// @return The address associated with the given chain ID and name.
function getAddress(
uint64 _chainId,
bytes32 _name,
function (uint64, bytes32) view returns (address) _fallbackFunc
)
internal
view
returns (address)
{
(bool found, address addr) = getCachedAddress(_chainId, _name);
return found ? addr : _fallbackFunc(_chainId, _name);
}

/// @notice This function retrieves the cached address associated with a given chain ID and
/// name.
/// @dev This function is virtual and should be overridden in derived contracts.
/// @param _chainId The chain ID for which the address is to be retrieved.
/// @param _name The name associated with the address to be retrieved.
/// @return found_ A boolean indicating whether the address was found in the cache.
/// @return addr_ The address associated with the given chain ID and name, if found in the
/// cache.
function getCachedAddress(
uint64 _chainId,
bytes32 _name
)
internal
pure
virtual
returns (bool found_, address addr_);
}
Loading

0 comments on commit ee464ca

Please sign in to comment.