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

Code cleanup #96

Merged
merged 12 commits into from
Oct 4, 2023
34 changes: 17 additions & 17 deletions src/MultiBridgeMessageReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity >=0.8.9;
import "./interfaces/controllers/IGAC.sol";
import "./interfaces/adapters/IMessageReceiverAdapter.sol";
import "./interfaces/IMultiBridgeMessageReceiver.sol";
import "./interfaces/EIP5164/ExecutorAware.sol";
import "./libraries/EIP5164/ExecutorAware.sol";
import "./interfaces/controllers/IGovernanceTimelock.sol";

/// libraries
Expand Down Expand Up @@ -60,8 +60,8 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
_;
}

/// @notice A modifier used for restricting the caller to just the governance timelock contract
modifier onlyOwner() {
/// @notice Restricts the caller to the owner configured in GAC.
modifier onlyGlobalOwner() {
if (!gac.isGlobalOwner(msg.sender)) {
revert Error.CALLER_NOT_OWNER();
}
Expand Down Expand Up @@ -129,23 +129,23 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
}

/// @inheritdoc IMultiBridgeMessageReceiver
function executeMessage(bytes32 msgId) external override {
ExecutionData memory _execData = msgExecData[msgId];
function executeMessage(bytes32 _msgId) external override {
ExecutionData memory _execData = msgExecData[_msgId];

/// @dev validates if msg execution is not beyond expiration
if (block.timestamp > _execData.expiration) {
revert Error.MSG_EXECUTION_PASSED_DEADLINE();
}

/// @dev validates if msgId is already executed
if (isExecuted[msgId]) {
if (isExecuted[_msgId]) {
revert Error.MSG_ID_ALREADY_EXECUTED();
}

isExecuted[msgId] = true;
isExecuted[_msgId] = true;

/// @dev validates message quorum
if (msgDeliveryCount[msgId] < quorum) {
if (msgDeliveryCount[_msgId] < quorum) {
revert Error.QUORUM_NOT_ACHIEVED();
}

Expand All @@ -154,12 +154,12 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
_execData.target, _execData.value, _execData.callData
);

emit MessageExecuted(msgId, _execData.target, _execData.value, _execData.nonce, _execData.callData);
emit MessageExecuted(_msgId, _execData.target, _execData.value, _execData.nonce, _execData.callData);
}

/// @notice update the governance timelock contract.
/// @dev called by admin to update the timelock contract
function updateGovernanceTimelock(address _governanceTimelock) external onlyOwner {
function updateGovernanceTimelock(address _governanceTimelock) external onlyGlobalOwner {
if (_governanceTimelock == address(0)) {
revert Error.ZERO_GOVERNANCE_TIMELOCK();
}
Expand All @@ -171,7 +171,7 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
function updateReceiverAdapters(address[] calldata _receiverAdapters, bool[] calldata _operations)
external
override
onlyOwner
onlyGlobalOwner
{
_updateReceiverAdapters(_receiverAdapters, _operations);
}
Expand All @@ -182,7 +182,7 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
uint64 _newQuorum,
address[] calldata _receiverAdapters,
bool[] calldata _operations
) external override onlyOwner {
) external override onlyGlobalOwner {
/// @dev updates quorum here
_updateQuorum(_newQuorum);

Expand All @@ -191,7 +191,7 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
}

/// @notice Update power quorum threshold of message execution.
function updateQuorum(uint64 _quorum) external override onlyOwner {
function updateQuorum(uint64 _quorum) external override onlyGlobalOwner {
_updateQuorum(_quorum);
}

Expand All @@ -200,15 +200,15 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
////////////////////////////////////////////////////////////////*/

/// @notice view message info, return (executed, msgPower, delivered adapters)
function getMessageInfo(bytes32 msgId) public view returns (bool, uint256, string[] memory) {
uint256 msgCurrentVotes = msgDeliveryCount[msgId];
function getMessageInfo(bytes32 _msgId) public view returns (bool, uint256, string[] memory) {
uint256 msgCurrentVotes = msgDeliveryCount[_msgId];
string[] memory successfulBridge = new string[](msgCurrentVotes);

if (msgCurrentVotes != 0) {
uint256 currIndex;
address[] memory executors = getTrustedExecutors();
for (uint256 i; i < executors.length;) {
if (msgDeliveries[msgId][executors[i]]) {
if (msgDeliveries[_msgId][executors[i]]) {
successfulBridge[currIndex] = IMessageReceiverAdapter(executors[i]).name();
++currIndex;
}
Expand All @@ -219,7 +219,7 @@ contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAwar
}
}

return (isExecuted[msgId], msgCurrentVotes, successfulBridge);
return (isExecuted[_msgId], msgCurrentVotes, successfulBridge);
}

/*/////////////////////////////////////////////////////////////////
Expand Down
40 changes: 6 additions & 34 deletions src/MultiBridgeMessageSender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ contract MultiBridgeMessageSender {
MODIFIERS
////////////////////////////////////////////////////////////////*/

/// @dev checks if msg.sender is the owner configured in GAC
modifier onlyOwner() {
/// @notice Restricts the caller to the owner configured in GAC.
modifier onlyGlobalOwner() {
if (msg.sender != senderGAC.getGlobalOwner()) {
revert Error.CALLER_NOT_OWNER();
}
Expand All @@ -105,7 +105,7 @@ contract MultiBridgeMessageSender {

/// @dev checks if msg.sender is the authorised caller configured in GAC
modifier onlyCaller() {
if (msg.sender != senderGAC.getAuthorisedCaller()) {
if (msg.sender != senderGAC.authorisedCaller()) {
revert Error.INVALID_PRIVILEGED_CALLER();
}
_;
Expand Down Expand Up @@ -137,34 +137,6 @@ contract MultiBridgeMessageSender {
EXTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////*/

/// @notice Call a remote function on a destination chain by sending multiple copies of a cross-chain message
/// via all available bridges. This function can only be called by the authorised called configured in the GAC.
/// @dev a fee in native token may be required by each message bridge to send messages. Any native token fee remained
/// will be refunded back to a refund address defined in the _refundAddress parameter.
/// Caller needs to specify fees for each adapter when calling this function.
/// @param _dstChainId is the destination chainId
/// @param _target is the target execution point on the destination chain
/// @param _callData is the data to be sent to _target by low-level call(eg. address(_target).call(_callData))
/// @param _nativeValue is the value to be sent to _target by low-level call (eg. address(_target).call{value: _nativeValue}(_callData))
/// @param _expiration refers to the number of seconds that a message remains valid before it is considered stale and can no longer be executed.
/// @param _refundAddress refers to the refund address for any extra native tokens paid
/// @param _fees refers to the fees to pay to each adapter
function remoteCall(
uint256 _dstChainId,
address _target,
bytes calldata _callData,
uint256 _nativeValue,
uint256 _expiration,
address _refundAddress,
uint256[] calldata _fees
) external payable onlyCaller validateExpiration(_expiration) {
_remoteCall(
RemoteCallArgs(
_dstChainId, _target, _callData, _nativeValue, _expiration, _refundAddress, _fees, new address[](0)
)
);
}

/// @param _dstChainId is the destination chainId
/// @param _target is the target execution point on the destination chain
/// @param _callData is the data to be sent to _target by low-level call(eg. address(_target).call(_callData))
Expand Down Expand Up @@ -193,7 +165,7 @@ contract MultiBridgeMessageSender {

/// @notice Add bridge sender adapters
/// @param _additions are the adapter address to add, in ascending order with no duplicates
function addSenderAdapters(address[] calldata _additions) external onlyOwner {
function addSenderAdapters(address[] calldata _additions) external onlyGlobalOwner {
_checkAdaptersOrder(_additions);

address[] memory existings = senderAdapters;
Expand Down Expand Up @@ -249,7 +221,7 @@ contract MultiBridgeMessageSender {

/// @notice Remove bridge sender adapters
/// @param _removals are the adapter addresses to remove
function removeSenderAdapters(address[] calldata _removals) external onlyOwner {
function removeSenderAdapters(address[] calldata _removals) external onlyGlobalOwner {
_checkAdaptersOrder(_removals);

address[] memory existings = senderAdapters;
Expand Down Expand Up @@ -324,7 +296,7 @@ contract MultiBridgeMessageSender {
revert Error.INVALID_REFUND_ADDRESS();
}

mmaReceiver = senderGAC.getRemoteMultiBridgeMessageReceiver(_dstChainId);
mmaReceiver = senderGAC.remoteMultiBridgeMessageReceiver(_dstChainId);

if (mmaReceiver == address(0)) {
revert Error.ZERO_RECEIVER_ADAPTER();
Expand Down
13 changes: 4 additions & 9 deletions src/adapters/BaseSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract contract BaseSenderAdapter is IMessageSenderAdapter {
MODIFIER
////////////////////////////////////////////////////////////////*/
modifier onlyMultiBridgeMessageSender() {
if (msg.sender != senderGAC.getMultiBridgeMessageSender()) {
if (msg.sender != senderGAC.multiBridgeMessageSender()) {
revert Error.CALLER_NOT_MULTI_MESSAGE_SENDER();
}
_;
Expand Down Expand Up @@ -72,20 +72,15 @@ abstract contract BaseSenderAdapter is IMessageSenderAdapter {
}
}

/// @inheritdoc IMessageSenderAdapter
function getReceiverAdapter(uint256 _dstChainId) external view override returns (address) {
return receiverAdapters[_dstChainId];
}

/*/////////////////////////////////////////////////////////////////
HELPER FUNCTIONS
////////////////////////////////////////////////////////////////*/

/// @notice generates a new message id by incrementing nonce
/// @param _toChainId is the destination chainId.
/// @param _receiverChainId is the destination chainId.
/// @param _to is the contract address on the destination chain.
function _getNewMessageId(uint256 _toChainId, address _to) internal returns (bytes32 messageId) {
messageId = keccak256(abi.encodePacked(block.chainid, _toChainId, nonce, address(this), _to));
function _getNewMessageId(uint256 _receiverChainId, address _to) internal returns (bytes32 messageId) {
messageId = keccak256(abi.encodePacked(block.chainid, _receiverChainId, nonce, address(this), _to));
++nonce;
}
}
34 changes: 18 additions & 16 deletions src/adapters/axelar/AxelarReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
/*/////////////////////////////////////////////////////////////////
STATE VARIABLES
////////////////////////////////////////////////////////////////*/
string public senderChain;
string public senderChainId;

mapping(bytes32 => bool) public isMessageExecuted;
mapping(bytes32 => bool) public commandIdStatus;
Expand All @@ -36,19 +36,21 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
CONSTRUCTOR
////////////////////////////////////////////////////////////////*/
/// @param _gateway is axelar gateway contract address.
/// @param _senderChain is the chain id of the sender chain.
/// @param _senderChainId is the chain id of the sender chain.
/// @param _receiverGAC is global access controller.
constructor(address _gateway, string memory _senderChain, address _receiverGAC) BaseReceiverAdapter(_receiverGAC) {
constructor(address _gateway, string memory _senderChainId, address _receiverGAC)
BaseReceiverAdapter(_receiverGAC)
{
if (_gateway == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}

if (bytes(_senderChain).length == 0) {
if (bytes(_senderChainId).length == 0) {
revert Error.INVALID_SENDER_CHAIN_ID();
}

gateway = IAxelarGateway(_gateway);
senderChain = _senderChain;
senderChainId = _senderChainId;
}

/*/////////////////////////////////////////////////////////////////
Expand All @@ -58,32 +60,32 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
/// @dev accepts new cross-chain messages from axelar gateway
/// @inheritdoc IAxelarExecutable
function execute(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
bytes32 _commandId,
string calldata _sourceChainId,
string calldata _sourceAddress,
bytes calldata _payload
) external override {
/// @dev step-1: validate incoming chain id
if (keccak256(bytes(sourceChain)) != keccak256(bytes(senderChain))) {
if (keccak256(bytes(_sourceChainId)) != keccak256(bytes(senderChainId))) {
revert Error.INVALID_SENDER_CHAIN_ID();
}

/// @dev step-2: validate the source address
if (sourceAddress.toAddress() != senderAdapter) {
if (_sourceAddress.toAddress() != senderAdapter) {
revert Error.INVALID_SENDER_ADAPTER();
}

/// @dev step-3: validate the contract call
if (!gateway.validateContractCall(commandId, sourceChain, sourceAddress, keccak256(payload))) {
if (!gateway.validateContractCall(_commandId, _sourceChainId, _sourceAddress, keccak256(_payload))) {
revert Error.NOT_APPROVED_BY_GATEWAY();
}

/// decode the cross-chain payload
AdapterPayload memory decodedPayload = abi.decode(payload, (AdapterPayload));
AdapterPayload memory decodedPayload = abi.decode(_payload, (AdapterPayload));
bytes32 msgId = decodedPayload.msgId;

/// @dev step-4: check for duplicate message
if (commandIdStatus[commandId] || isMessageExecuted[msgId]) {
if (commandIdStatus[_commandId] || isMessageExecuted[msgId]) {
revert MessageIdAlreadyExecuted(msgId);
}

Expand All @@ -93,12 +95,12 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
}

/// @dev step-6: validate the destination
if (decodedPayload.finalDestination != receiverGAC.getMultiBridgeMessageReceiver()) {
if (decodedPayload.finalDestination != receiverGAC.multiBridgeMsgReceiver()) {
revert Error.INVALID_FINAL_DESTINATION();
}

isMessageExecuted[msgId] = true;
commandIdStatus[commandId] = true;
commandIdStatus[_commandId] = true;

MessageLibrary.Message memory _data = abi.decode(decodedPayload.data, (MessageLibrary.Message));

Expand Down
Loading
Loading