Skip to content

Commit

Permalink
Run forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominator008 committed Oct 10, 2023
1 parent bbcc51b commit 9377e1f
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 509 deletions.
108 changes: 30 additions & 78 deletions src/MultiBridgeMessageReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import "./libraries/Message.sol";
/// governance timelock contract.
/// @dev The contract only accepts messages from trusted bridge receiver adapters, each of which implements the
/// IMessageReceiverAdapter interface.
contract MultiBridgeMessageReceiver is
IMultiBridgeMessageReceiver,
ExecutorAware
{
contract MultiBridgeMessageReceiver is IMultiBridgeMessageReceiver, ExecutorAware {
/// @notice the id of the source chain that this contract can receive messages from
uint256 public immutable srcChainId;
/// @notice the global access control contract
Expand All @@ -40,8 +37,7 @@ contract MultiBridgeMessageReceiver is
address public governanceTimelock;

/// @notice maintains which bridge adapters have delivered each message
mapping(bytes32 msgId => mapping(address receiverAdapter => bool delivered))
public msgDeliveries;
mapping(bytes32 msgId => mapping(address receiverAdapter => bool delivered)) public msgDeliveries;

/// @notice count of bridge adapters that have delivered each message
mapping(bytes32 msgId => uint256 deliveryCount) public msgDeliveryCount;
Expand Down Expand Up @@ -77,12 +73,7 @@ contract MultiBridgeMessageReceiver is
////////////////////////////////////////////////////////////////*/

/// @notice sets the initial parameters
constructor(
uint256 _srcChainId,
address _gac,
address[] memory _receiverAdapters,
uint64 _quorum
) {
constructor(uint256 _srcChainId, address _gac, address[] memory _receiverAdapters, uint64 _quorum) {
if (_srcChainId == 0) {
revert Error.INVALID_SENDER_CHAIN_ID();
}
Expand All @@ -93,7 +84,7 @@ contract MultiBridgeMessageReceiver is
srcChainId = _srcChainId;
gac = IGAC(_gac);

for (uint256 i; i < _receiverAdapters.length; ) {
for (uint256 i; i < _receiverAdapters.length;) {
_updateReceiverAdapter(_receiverAdapters[i], true);
unchecked {
++i;
Expand All @@ -108,9 +99,7 @@ contract MultiBridgeMessageReceiver is

/// @notice receive messages from allowed bridge receiver adapters
/// @param _message is the crosschain message sent by the mma sender
function receiveMessage(
MessageLibrary.Message calldata _message
) external override onlyReceiverAdapter {
function receiveMessage(MessageLibrary.Message calldata _message) external override onlyReceiverAdapter {
if (_message.dstChainId != block.chainid) {
revert Error.INVALID_DST_CHAIN();
}
Expand Down Expand Up @@ -146,29 +135,20 @@ contract MultiBridgeMessageReceiver is

/// stores the message if the amb is the first one delivering the message
if (prevStoredHash == bytes32(0)) {
msgExecParamsHash[msgId] = MessageLibrary
.computeExecutionParamsHash(_message);
msgExecParamsHash[msgId] = MessageLibrary.computeExecutionParamsHash(_message);
}

string memory bridgeName = IMessageReceiverAdapter(msg.sender).name();
emit BridgeMessageReceived(
msgId,
bridgeName,
_message.nonce,
msg.sender
);
emit BridgeMessageReceived(msgId, bridgeName, _message.nonce, msg.sender);
}

/// @inheritdoc IMultiBridgeMessageReceiver
function scheduleMessageExecution(
bytes32 _msgId,
MessageLibrary.MessageExecutionParams calldata _execParams
) external override {
function scheduleMessageExecution(bytes32 _msgId, MessageLibrary.MessageExecutionParams calldata _execParams)
external
override
{
bytes32 execParamsHash = msgExecParamsHash[_msgId];
if (
MessageLibrary.computeExecutionParamsHash(_execParams) !=
execParamsHash
) {
if (MessageLibrary.computeExecutionParamsHash(_execParams) != execParamsHash) {
revert Error.EXEC_PARAMS_HASH_MISMATCH();
}

Expand All @@ -191,42 +171,32 @@ contract MultiBridgeMessageReceiver is

/// @dev queues the action on timelock for execution
IGovernanceTimelock(governanceTimelock).scheduleTransaction(
_execParams.target,
_execParams.value,
_execParams.callData
_execParams.target, _execParams.value, _execParams.callData
);

emit MessageExecutionScheduled(
_msgId,
_execParams.target,
_execParams.value,
_execParams.nonce,
_execParams.callData
_msgId, _execParams.target, _execParams.value, _execParams.nonce, _execParams.callData
);
}

/// @notice update the governance timelock contract.
/// @dev called by admin to update the timelock contract
function updateGovernanceTimelock(
address _governanceTimelock
) external onlyGlobalOwner {
function updateGovernanceTimelock(address _governanceTimelock) external onlyGlobalOwner {
if (_governanceTimelock == address(0)) {
revert Error.ZERO_GOVERNANCE_TIMELOCK();
}
address oldGovernanceTimelock = governanceTimelock;
governanceTimelock = _governanceTimelock;
emit GovernanceTimelockUpdated(
oldGovernanceTimelock,
_governanceTimelock
);
emit GovernanceTimelockUpdated(oldGovernanceTimelock, _governanceTimelock);
}

/// @notice Update bridge receiver adapters.
/// @dev called by admin to update receiver bridge adapters on all other chains
function updateReceiverAdapters(
address[] calldata _receiverAdapters,
bool[] calldata _operations
) external override onlyGlobalOwner {
function updateReceiverAdapters(address[] calldata _receiverAdapters, bool[] calldata _operations)
external
override
onlyGlobalOwner
{
_updateReceiverAdapters(_receiverAdapters, _operations);
_validateQuorum(quorum);
}
Expand Down Expand Up @@ -255,20 +225,16 @@ contract MultiBridgeMessageReceiver is
/// @return isExecutionScheduled is true if the message has been sent to the timelock for execution
/// @return msgCurrentVotes is the number of bridges that have delivered the message
/// @return successfulBridge is the list of bridges that have delivered the message
function getMessageInfo(
bytes32 _msgId
) public view returns (bool, uint256, string[] memory) {
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; ) {
for (uint256 i; i < executors.length;) {
if (msgDeliveries[_msgId][executors[i]]) {
successfulBridge[currIndex] = IMessageReceiverAdapter(
executors[i]
).name();
successfulBridge[currIndex] = IMessageReceiverAdapter(executors[i]).name();
++currIndex;
}

Expand All @@ -278,11 +244,7 @@ contract MultiBridgeMessageReceiver is
}
}

return (
isExecutionScheduled[_msgId],
msgCurrentVotes,
successfulBridge
);
return (isExecutionScheduled[_msgId], msgCurrentVotes, successfulBridge);
}

/*/////////////////////////////////////////////////////////////////
Expand All @@ -298,10 +260,7 @@ contract MultiBridgeMessageReceiver is
emit QuorumUpdated(oldValue, _quorum);
}

function _updateReceiverAdapters(
address[] memory _receiverAdapters,
bool[] memory _operations
) private {
function _updateReceiverAdapters(address[] memory _receiverAdapters, bool[] memory _operations) private {
uint256 len = _receiverAdapters.length;

if (len == 0) {
Expand All @@ -312,7 +271,7 @@ contract MultiBridgeMessageReceiver is
revert Error.ARRAY_LENGTH_MISMATCHED();
}

for (uint256 i; i < len; ) {
for (uint256 i; i < len;) {
_updateReceiverAdapter(_receiverAdapters[i], _operations[i]);

unchecked {
Expand All @@ -321,22 +280,15 @@ contract MultiBridgeMessageReceiver is
}
}

function _updateReceiverAdapter(
address _receiverAdapter,
bool _add
) private {
function _updateReceiverAdapter(address _receiverAdapter, bool _add) private {
if (_receiverAdapter == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}
bool success = _add
? _addTrustedExecutor(_receiverAdapter)
: _removeTrustedExecutor(_receiverAdapter);
bool success = _add ? _addTrustedExecutor(_receiverAdapter) : _removeTrustedExecutor(_receiverAdapter);

if (!success) {
// only fails because we are either attempting to add an existing adapter, or remove a non-existing adapter
revert Error.UPDATE_RECEIVER_ADAPTER_FAILED(
_add ? "adapter already added" : "adapter not found"
);
revert Error.UPDATE_RECEIVER_ADAPTER_FAILED(_add ? "adapter already added" : "adapter not found");
}

emit BridgeReceiverAdapterUpdated(_receiverAdapter, _add);
Expand Down
27 changes: 6 additions & 21 deletions src/interfaces/IMultiBridgeMessageReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ interface IMultiBridgeMessageReceiver {
/// @param nonce is the nonce of the message
/// @param receiverAdapter is the address of the receiver adapter that received the message
event BridgeMessageReceived(
bytes32 indexed msgId,
string indexed bridgeName,
uint256 nonce,
address receiverAdapter
bytes32 indexed msgId, string indexed bridgeName, uint256 nonce, address receiverAdapter
);

/// @notice emitted when a message has been queued for execution in the destination timelock contract.
Expand All @@ -24,20 +21,13 @@ interface IMultiBridgeMessageReceiver {
/// @param nonce is the nonce of the message
/// @param callData is the data that will be passed to the target address through low-level call
event MessageExecutionScheduled(
bytes32 indexed msgId,
address indexed target,
uint256 nativeValue,
uint256 nonce,
bytes callData
bytes32 indexed msgId, address indexed target, uint256 nativeValue, uint256 nonce, bytes callData
);

/// @notice emitted when receiver adapter of a specific bridge is updated.
/// @param receiverAdapter is the new receiver adapter address
/// @param add is true if the receiver adapter was added, false if removed
event BridgeReceiverAdapterUpdated(
address indexed receiverAdapter,
bool add
);
event BridgeReceiverAdapterUpdated(address indexed receiverAdapter, bool add);

/// @notice emitted when the quorum for message validity is updated.
/// @param oldQuorum is the old quorum value
Expand All @@ -57,18 +47,13 @@ interface IMultiBridgeMessageReceiver {
/// @notice Sends a message, that has achieved quorum and has not yet expired, to the governance timelock for eventual execution.
/// @param _msgId is the unique identifier of the message
/// @param _execParams are the params for message execution
function scheduleMessageExecution(
bytes32 _msgId,
MessageLibrary.MessageExecutionParams calldata _execParams
) external;
function scheduleMessageExecution(bytes32 _msgId, MessageLibrary.MessageExecutionParams calldata _execParams)
external;

/// @notice adds or removes bridge receiver adapters.
/// @param _receiverAdapters the list of receiver adapters to add or remove
/// @param _operations the list of operations to perform for corresponding receiver adapters, true for add, false for remove
function updateReceiverAdapters(
address[] calldata _receiverAdapters,
bool[] calldata _operations
) external;
function updateReceiverAdapters(address[] calldata _receiverAdapters, bool[] calldata _operations) external;

/// @notice updates the quorum for message validity, which is the number of bridges that must deliver the message for it to be considered valid.
/// @param _quorum is the new quorum value
Expand Down
67 changes: 25 additions & 42 deletions src/libraries/Message.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,37 @@ library MessageLibrary {

/// @notice computes the message id (32 byte hash of the encoded message parameters)
/// @param _message is the cross-chain message
function computeMsgId(
Message memory _message
) internal pure returns (bytes32) {
return
keccak256(
abi.encodePacked(
_message.srcChainId,
_message.dstChainId,
_message.nonce,
_message.target,
_message.nativeValue,
_message.expiration,
_message.callData
)
);
function computeMsgId(Message memory _message) internal pure returns (bytes32) {
return keccak256(
abi.encodePacked(
_message.srcChainId,
_message.dstChainId,
_message.nonce,
_message.target,
_message.nativeValue,
_message.expiration,
_message.callData
)
);
}

function extractExecutionParams(
Message memory _message
) internal pure returns (MessageExecutionParams memory) {
return
MessageExecutionParams({
target: _message.target,
callData: _message.callData,
value: _message.nativeValue,
nonce: _message.nonce,
expiration: _message.expiration
});
function extractExecutionParams(Message memory _message) internal pure returns (MessageExecutionParams memory) {
return MessageExecutionParams({
target: _message.target,
callData: _message.callData,
value: _message.nativeValue,
nonce: _message.nonce,
expiration: _message.expiration
});
}

function computeExecutionParamsHash(
MessageExecutionParams memory _params
) internal pure returns (bytes32) {
return
keccak256(
abi.encodePacked(
_params.target,
_params.callData,
_params.value,
_params.nonce,
_params.expiration
)
);
function computeExecutionParamsHash(MessageExecutionParams memory _params) internal pure returns (bytes32) {
return keccak256(
abi.encodePacked(_params.target, _params.callData, _params.value, _params.nonce, _params.expiration)
);
}

function computeExecutionParamsHash(
Message memory _message
) internal pure returns (bytes32) {
function computeExecutionParamsHash(Message memory _message) internal pure returns (bytes32) {
return computeExecutionParamsHash(extractExecutionParams(_message));
}
}
Loading

0 comments on commit 9377e1f

Please sign in to comment.