Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(protocol): improve mainnet gas efficiency with addresses cached #17791

Merged
merged 11 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
21 changes: 21 additions & 0 deletions packages/protocol/contracts/mainnet/CachedProverSet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

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

/// @title CachedProverSet
/// @notice See the documentation in {ProverSet}.
/// @custom:security-contact [email protected]
contract CachedProverSet is ProverSet {
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_TAIKO) {
return 0x06a9Ab27c7e2255df1815E6CC0168d7755Feb19a;
}
}
return super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@ pragma solidity 0.8.24;
import "../common/AddressManager.sol";
import "../common/LibStrings.sol";

/// @title L1RollupAddressManager
/// @title CachedRollupAddressManager
/// @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 0x579f40D0BE111b823962043702cabe6Aaa290780.
/// @custom:security-contact [email protected]
contract L1RollupAddressManager 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 CachedRollupAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
if (_chainId == 1) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800;
Expand Down Expand Up @@ -52,6 +38,6 @@ contract L1RollupAddressManager is AddressManager {
return 0x6E997f1F22C40ba37F633B08f3b07E10Ed43155a;
}
}
return address(0);
return super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@ pragma solidity 0.8.24;
import "../common/AddressManager.sol";
import "../common/LibStrings.sol";

/// @title L1SharedAddressManager
/// @title CachedSharedAddressManager
/// @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 CachedSharedAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
if (_chainId == 1) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800;
Expand Down Expand Up @@ -63,6 +49,6 @@ contract L1SharedAddressManager is AddressManager {
}
}

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

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

/// @title CachedTaikoL1
/// @notice See the documentation in {TaikoL1}.
/// @custom:security-contact [email protected]
contract CachedTaikoL1 is TaikoL1 {
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_TIER_ROUTER) {
return 0x6E997f1F22C40ba37F633B08f3b07E10Ed43155a;
}
}
return super._getAddress(_chainId, _name);
}
}