Skip to content

Commit

Permalink
fix(Configurator): remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Oct 25, 2024
1 parent 1cfaf25 commit 1b7f36b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 104 deletions.
116 changes: 116 additions & 0 deletions src/governance/configurator/Configurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,153 @@ import { IConfigurator } from "../interfaces/IConfigurator.sol";
* @author M^0 Labs
*/
contract Configurator is IConfigurator {
/* ============ Struct ============ */

/**
* @notice Chain configuration.
* @param chainId Wormhole chain ID.
* @param isEvmChain Whether the chain is an EVM chain or not.
* @param isSpecialRelayingEnabled Whether special relaying is enabled or not.
* @param isWormholeRelayingEnabled Whether Wormhole relaying is enabled or not.
* @param portal Address of the Portal being configured.
* @param wormholeTransceiver Address of the WormholeTransceiver being configured.
*/
struct ChainConfig {
uint16 chainId;
bool isEvmChain;
bool isSpecialRelayingEnabled;
bool isWormholeRelayingEnabled;
bytes32 portal;
bytes32 wormholeTransceiver;
}

/* ============ Variables ============ */

/// @inheritdoc IConfigurator
address public immutable portal;

/// @inheritdoc IConfigurator
address public immutable wormholeTransceiver;

/* ============ Constructor ============ */

/**
* @dev Constructs the Configurator contract.
* @param portal_ Address of the Portal being configured.
* @param wormholeTransceiver_ Address of the WormholeTransceiver being configured.
*/
constructor(address portal_, address wormholeTransceiver_) {
if ((portal = portal_) == address(0)) revert ZeroPortal();
if ((wormholeTransceiver = wormholeTransceiver_) == address(0)) revert ZeroWormholeTransceiver();
}

/* ============ Interactive Functions ============ */

/// @inheritdoc IConfigurator
function execute() external virtual {}

/* ============ Internal Interactive Functions ============ */

/**
* @dev Sets the Portal's peer for the given Wormhole chain ID.
* @param peerChainId_ The Wormhole chain ID for which to set the Portal peer.
* @param peerPortal_ The address of the Portal peer.
*/
function _setPeerPortal(uint16 peerChainId_, bytes32 peerPortal_) internal {
INttManager(portal).setPeer(peerChainId_, peerPortal_, 6, 0);
}

/**
* @dev Sets the Wormhole transceiver's peer for the given Wormhole chain ID.
* @param peerChainId_ The Wormhole chain ID for which to set the Wormhole transceiver peer.
* @param peerWormholeTransceiver_ The address of the Wormhole transceiver peer.
*/
function _setPeerWormholeTransceiver(uint16 peerChainId_, bytes32 peerWormholeTransceiver_) internal {
IWormholeTransceiver(wormholeTransceiver).setWormholePeer(peerChainId_, peerWormholeTransceiver_);
}

/**
* @dev Sets the Wormhole relaying enabled flag for the given Wormhole chain ID.
* @param chainId_ The Wormhole chain ID to set the flag for.
* @param isRelayingEnabled_ Whether Wormhole relaying is enabled or not.
*/
function _setIsWormholeRelayingEnabled(uint16 chainId_, bool isRelayingEnabled_) internal {
IWormholeTransceiver(wormholeTransceiver).setIsWormholeRelayingEnabled(chainId_, isRelayingEnabled_);
}

/**
* @dev Sets the special relaying enabled flag for the given Wormhole chain ID.
* @param chainId_ The Wormhole chain ID to set the flag for.
* @param isRelayingEnabled_ Whether special relaying is enabled or not.
*/
function _setIsSpecialRelayingEnabled(uint16 chainId_, bool isRelayingEnabled_) internal {
IWormholeTransceiver(wormholeTransceiver).setIsSpecialRelayingEnabled(chainId_, isRelayingEnabled_);
}

/**
* @dev Sets the EVM chain flag for the given Wormhole chain ID.
* @param chainId_ The Wormhole chain ID to set the flag for.
* @param isEvm_ Whether the chain is an EVM chain or not.
*/
function _setIsWormholeEvmChain(uint16 chainId_, bool isEvm_) internal {
IWormholeTransceiver(wormholeTransceiver).setIsWormholeEvmChain(chainId_, isEvm_);
}

/**
* @dev Configures the Portal for the given target chains.
* @param targetConfigs_ The configuration for each target chain.
* @param sourceWormholeChainId_ The Wormhole chain ID of the source chain.
*/
function _configurePortal(ChainConfig[] memory targetConfigs_, uint16 sourceWormholeChainId_) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
_setPeerPortal(targetConfig_.chainId, targetConfig_.portal);
}
}
}

/**
* @dev Configures the Wormhole transceiver for the given target chains.
* @param targetConfigs_ The configuration for each target chain.
* @param sourceWormholeChainId_ The Wormhole chain ID of the source chain.
*/
function _configureWormholeTransceiver(
ChainConfig[] memory targetConfigs_,
uint16 sourceWormholeChainId_
) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
if (targetConfig_.isWormholeRelayingEnabled) {
_setIsWormholeRelayingEnabled(targetConfig_.chainId, true);
} else if (targetConfig_.isSpecialRelayingEnabled) {
_setIsSpecialRelayingEnabled(targetConfig_.chainId, true);
}

_setPeerWormholeTransceiver(targetConfig_.chainId, targetConfig_.wormholeTransceiver);

if (targetConfig_.isEvmChain) {
_setIsWormholeEvmChain(targetConfig_.chainId, true);
}
}
}
}

/**
* @dev Converts an EVM address to a universal address.
* @param evmAddr_ The EVM address to convert.
* @return converted_ The universal address.
*/
function _toUniversalAddress(address evmAddr_) internal pure returns (bytes32 converted_) {
assembly ("memory-safe") {
converted_ := and(0xffffffffffffffffffffffffffffffffffffffff, evmAddr_)
}
}
}
62 changes: 10 additions & 52 deletions src/governance/configurator/MainnetConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import { Configurator } from "./Configurator.sol";
* @author M^0 Labs
*/
contract MainnetConfigurator is Configurator {
struct ChainConfig {
uint16 chainId;
bool isEvmChain;
bool isSpecialRelayingEnabled;
bool isWormholeRelayingEnabled;
bytes32 portal;
bytes32 wormholeTransceiver;
}

/// @dev Ethereum Mainnet Wormhole chain ID.
uint16 internal constant _MAINNET_WORMHOLE_CHAIN_ID = 2;

/// @dev Base Wormhole chain ID
uint16 internal constant _BASE_WORMHOLE_CHAIN_ID = 30;

/// @dev Optimism Wormhole chain ID.
uint16 internal constant _OPTIMISM_WORMHOLE_CHAIN_ID = 24;

/**
* @notice Constructs the MainnetConfigurator contract.
* @param portal_ The address of the Portal.
* @param wormholeTransceiver_ The address of the Wormhole transceiver.
*/
constructor(address portal_, address wormholeTransceiver_) Configurator(portal_, wormholeTransceiver_) {}

/// @inheritdoc IConfigurator
Expand Down Expand Up @@ -70,47 +71,4 @@ contract MainnetConfigurator is Configurator {
_configureWormholeTransceiver(mainnetConfig_, _OPTIMISM_WORMHOLE_CHAIN_ID);
}
}

function _configurePortal(ChainConfig[] memory targetConfigs_, uint16 sourceWormholeChainId_) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
_setPeerPortal(targetConfig_.chainId, targetConfig_.portal);
}
}
}

function _configureWormholeTransceiver(
ChainConfig[] memory targetConfigs_,
uint16 sourceWormholeChainId_
) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
if (targetConfig_.isWormholeRelayingEnabled) {
_setIsWormholeRelayingEnabled(targetConfig_.chainId, true);
} else if (targetConfig_.isSpecialRelayingEnabled) {
_setIsSpecialRelayingEnabled(targetConfig_.chainId, true);
}

_setPeerWormholeTransceiver(targetConfig_.chainId, targetConfig_.wormholeTransceiver);

if (targetConfig_.isEvmChain) {
_setIsWormholeEvmChain(targetConfig_.chainId, true);
}
}
}
}

function _toUniversalAddress(address evmAddr_) internal pure returns (bytes32 converted_) {
assembly ("memory-safe") {
converted_ := and(0xffffffffffffffffffffffffffffffffffffffff, evmAddr_)
}
}
}
62 changes: 10 additions & 52 deletions src/governance/configurator/SepoliaConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import { Configurator } from "./Configurator.sol";
* @author M^0 Labs
*/
contract SepoliaConfigurator is Configurator {
struct ChainConfig {
uint16 chainId;
bool isEvmChain;
bool isSpecialRelayingEnabled;
bool isWormholeRelayingEnabled;
bytes32 portal;
bytes32 wormholeTransceiver;
}

/// @dev Sepolia Wormhole chain ID.
uint16 internal constant _SEPOLIA_WORMHOLE_CHAIN_ID = 10002;

/// @dev Base Sepolia Wormhole chain ID.
uint16 internal constant _BASE_SEPOLIA_WORMHOLE_CHAIN_ID = 10004;

/// @dev Optimism Sepolia Wormhole chain ID.
uint16 internal constant _OPTIMISM_SEPOLIA_WORMHOLE_CHAIN_ID = 10005;

/**
* @notice Constructs the SepoliaConfigurator contract.
* @param portal_ The address of the Portal.
* @param wormholeTransceiver_ The address of the Wormhole transceiver.
*/
constructor(address portal_, address wormholeTransceiver_) Configurator(portal_, wormholeTransceiver_) {}

/// @inheritdoc IConfigurator
Expand Down Expand Up @@ -70,47 +71,4 @@ contract SepoliaConfigurator is Configurator {
_configureWormholeTransceiver(sepoliaConfig_, _OPTIMISM_SEPOLIA_WORMHOLE_CHAIN_ID);
}
}

function _configurePortal(ChainConfig[] memory targetConfigs_, uint16 sourceWormholeChainId_) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
_setPeerPortal(targetConfig_.chainId, targetConfig_.portal);
}
}
}

function _configureWormholeTransceiver(
ChainConfig[] memory targetConfigs_,
uint16 sourceWormholeChainId_
) internal {
for (uint256 i_; i_ < targetConfigs_.length; ++i_) {
ChainConfig memory targetConfig_ = targetConfigs_[i_];

if (targetConfig_.chainId == sourceWormholeChainId_) {
continue;
} else {
if (targetConfig_.isWormholeRelayingEnabled) {
_setIsWormholeRelayingEnabled(targetConfig_.chainId, true);
} else if (targetConfig_.isSpecialRelayingEnabled) {
_setIsSpecialRelayingEnabled(targetConfig_.chainId, true);
}

_setPeerWormholeTransceiver(targetConfig_.chainId, targetConfig_.wormholeTransceiver);

if (targetConfig_.isEvmChain) {
_setIsWormholeEvmChain(targetConfig_.chainId, true);
}
}
}
}

function _toUniversalAddress(address evmAddr_) internal pure returns (bytes32 converted_) {
assembly ("memory-safe") {
converted_ := and(0xffffffffffffffffffffffffffffffffffffffff, evmAddr_)
}
}
}

0 comments on commit 1b7f36b

Please sign in to comment.