Skip to content

Commit

Permalink
refactor(protocol): improve mainnet gas efficiency with addresses cac…
Browse files Browse the repository at this point in the history
…hed (#17791)
  • Loading branch information
dantaik authored Jul 14, 2024
1 parent 2dd30ab commit b12227d
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 115 deletions.
8 changes: 1 addition & 7 deletions packages/protocol/contracts/common/AddressManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ contract AddressManager is EssentialContract, IAddressManager {

/// @inheritdoc IAddressManager
function getAddress(uint64 _chainId, bytes32 _name) external view override returns (address) {
address addr = _getOverride(_chainId, _name);
if (addr != address(0)) return addr;
else return __addresses[_chainId][_name];
return __addresses[_chainId][_name];
}

/// @notice Gets the address mapped to a specific chainId-name pair.
/// @dev Sub-contracts can override this method to avoid reading from storage.
function _getOverride(uint64 _chainId, bytes32 _name) internal pure virtual returns (address) { }

function _authorizePause(address, bool) internal pure override notImplemented { }
}
28 changes: 12 additions & 16 deletions packages/protocol/contracts/common/AddressResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ abstract contract AddressResolver is IAddressResolver, Initializable {
}

/// @inheritdoc IAddressResolver
function resolve(
bytes32 _name,
bool _allowZeroAddress
)
public
view
virtual
returns (address payable)
{
function resolve(bytes32 _name, bool _allowZeroAddress) public view virtual returns (address) {
return _resolve(uint64(block.chainid), _name, _allowZeroAddress);
}

Expand All @@ -64,7 +56,7 @@ abstract contract AddressResolver is IAddressResolver, Initializable {
public
view
virtual
returns (address payable)
returns (address)
{
return _resolve(_chainId, _name, _allowZeroAddress);
}
Expand All @@ -90,17 +82,21 @@ abstract contract AddressResolver is IAddressResolver, Initializable {
bytes32 _name,
bool _allowZeroAddress
)
private
internal
view
returns (address payable addr_)
returns (address addr_)
{
address _addressManager = addressManager;
if (_addressManager == address(0)) revert RESOLVER_INVALID_MANAGER();

addr_ = payable(IAddressManager(_addressManager).getAddress(_chainId, _name));
addr_ = _getAddress(_chainId, _name);

if (!_allowZeroAddress && addr_ == address(0)) {
revert RESOLVER_ZERO_ADDR(_chainId, _name);
}
}

function _getAddress(uint64 _chainId, bytes32 _name) internal view virtual returns (address) {
address _addressManager = addressManager;
if (_addressManager == address(0)) revert RESOLVER_INVALID_MANAGER();

return IAddressManager(_addressManager).getAddress(_chainId, _name);
}
}
10 changes: 2 additions & 8 deletions packages/protocol/contracts/common/IAddressResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ interface IAddressResolver {
/// @param _allowZeroAddress If set to true, does not throw if the resolved
/// address is `address(0)`.
/// @return Address associated with the given name.
function resolve(
bytes32 _name,
bool _allowZeroAddress
)
external
view
returns (address payable);
function resolve(bytes32 _name, bool _allowZeroAddress) external view returns (address);

/// @notice Resolves a name to its address deployed on a specified chain.
/// @param _chainId The chainId of interest.
Expand All @@ -38,5 +32,5 @@ interface IAddressResolver {
)
external
view
returns (address payable);
returns (address);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "./TaikoL1.sol";
import "../L1/TaikoL1.sol";

/// @title TaikoL1Hekla
/// @title HeklaTaikoL1
/// @dev Labeled in AddressResolver as "taiko"
/// @custom:security-contact [email protected]
contract TaikoL1Hekla is TaikoL1 {
contract HeklaTaikoL1 is TaikoL1 {
/// @inheritdoc ITaikoL1
function getConfig() public pure override returns (TaikoData.Config memory) {
return TaikoData.Config({
Expand Down
57 changes: 0 additions & 57 deletions packages/protocol/contracts/mainnet/L1RollupAddressManager.sol

This file was deleted.

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

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

/// @title LibAddressCache
/// @custom:security-contact [email protected]
library LibAddressCache {
function getAddress(
uint64 _chainId,
bytes32 _name
)
internal
pure
returns (bool found, address addr)
{
if (_chainId == 1) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return (true, 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800);
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return (true, 0x9e0a24964e5397B566c1ed39258e21aB5E35C77C);
}
if (_name == LibStrings.B_BRIDGE) {
return (true, 0xd60247c6848B7Ca29eDdF63AA924E53dB6Ddd8EC);
}
if (_name == LibStrings.B_TAIKO) {
return (true, 0x06a9Ab27c7e2255df1815E6CC0168d7755Feb19a);
}
if (_name == LibStrings.B_TIER_ROUTER) {
return (true, 0x6E997f1F22C40ba37F633B08f3b07E10Ed43155a);
}
if (_name == LibStrings.B_TIER_SGX) {
return (true, 0xb0f3186FC1963f774f52ff455DC86aEdD0b31F81);
}
if (_name == LibStrings.B_TIER_GUARDIAN_MINORITY) {
return (true, 0x579A8d63a2Db646284CBFE31FE5082c9989E985c);
}
if (_name == LibStrings.B_TIER_GUARDIAN) {
return (true, 0xE3D777143Ea25A6E031d1e921F396750885f43aC);
}
if (_name == LibStrings.B_AUTOMATA_DCAP_ATTESTATION) {
return (true, 0x8d7C954960a36a7596d7eA4945dDf891967ca8A3);
}
}
return (false, address(0));
}
}
15 changes: 15 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetProverSet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../team/proving/ProverSet.sol";
import "./LibAddressCache.sol";

/// @title MainnetProverSet
/// @notice See the documentation in {ProverSet}.
/// @custom:security-contact [email protected]
contract MainnetProverSet is ProverSet {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../common/AddressManager.sol";
import "../common/LibStrings.sol";
import "./LibAddressCache.sol";

/// @title MainnetRollupAddressManager
/// @notice See the documentation in {IAddressManager}.
/// @custom:security-contact [email protected]
contract MainnetRollupAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,17 @@ pragma solidity 0.8.24;
import "../common/AddressManager.sol";
import "../common/LibStrings.sol";

/// @title L1SharedAddressManager
/// @title MainnetSharedAddressManager
/// @notice See the documentation in {IAddressManager}.
/// @dev This contract shall NOT be used to upgrade existing implementation unless the name-address
/// registration becomes stable in 0xEf9EaA1dd30a9AA1df01c36411b5F082aA65fBaa.
/// @custom:security-contact [email protected]
contract L1SharedAddressManager is AddressManager {
/// @notice Gets the address mapped to a specific chainId-name pair.
/// @dev Sub-contracts can override this method to avoid reading from storage.
/// Some names are not cached as they are not used frequently or
/// its address is likely to change.
function _getOverride(
uint64 _chainId,
bytes32 _name
)
internal
pure
override
returns (address addr_)
{
contract MainnetSharedAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
if (_chainId == 1) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800;
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return 0x9e0a24964e5397B566c1ed39258e21aB5E35C77C;
if (_name == LibStrings.B_QUOTA_MANAGER) {
return 0x91f67118DD47d502B1f0C354D0611997B022f29E;
}
if (_name == LibStrings.B_BRIDGE) {
return 0xd60247c6848B7Ca29eDdF63AA924E53dB6Ddd8EC;
Expand All @@ -42,8 +28,8 @@ contract L1SharedAddressManager is AddressManager {
if (_name == LibStrings.B_ERC1155_VAULT) {
return 0xaf145913EA4a56BE22E120ED9C24589659881702;
}
if (_name == LibStrings.B_QUOTA_MANAGER) {
return 0x91f67118DD47d502B1f0C354D0611997B022f29E;
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return 0x9e0a24964e5397B566c1ed39258e21aB5E35C77C;
}
} else if (_chainId == 167_000) {
if (_name == LibStrings.B_BRIDGE) {
Expand All @@ -63,6 +49,6 @@ contract L1SharedAddressManager is AddressManager {
}
}

return address(0);
return super._getAddress(_chainId, _name);
}
}
15 changes: 15 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetTaikoL1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../L1/TaikoL1.sol";
import "./LibAddressCache.sol";

/// @title MainnetTaikoL1
/// @notice See the documentation in {TaikoL1}.
/// @custom:security-contact [email protected]
contract MainnetTaikoL1 is TaikoL1 {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
5 changes: 3 additions & 2 deletions packages/protocol/script/upgrade/UpgradeTaikoL1.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ pragma solidity 0.8.24;

import "forge-std/src/Script.sol";
import "forge-std/src/console2.sol";
import "../../contracts/L1/TaikoL1Hekla.sol";

import "../../contracts/hekla/HeklaTaikoL1.sol";
import "./UpgradeScript.s.sol";

contract UpgradeTaikoL1 is UpgradeScript {
function run() external setUp {
upgrade("TaikoL1", address(new TaikoL1Hekla()));
upgrade("HeklaTaikoL1", address(new HeklaTaikoL1()));
}
}

0 comments on commit b12227d

Please sign in to comment.