Skip to content

Commit

Permalink
Improve naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ermyas committed Oct 4, 2023
1 parent 1b19216 commit 2acaad8
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 88 deletions.
6 changes: 3 additions & 3 deletions src/adapters/BaseSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ abstract contract BaseSenderAdapter is IMessageSenderAdapter {
////////////////////////////////////////////////////////////////*/

/// @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;
}
}
32 changes: 17 additions & 15 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 @@ -98,7 +100,7 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
}

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

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

Expand Down
40 changes: 20 additions & 20 deletions src/adapters/axelar/AxelarSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ contract AxelarSenderAdapter is BaseSenderAdapter {
////////////////////////////////////////////////////////////////*/

/// @dev sendMessage sends a message to Axelar.
/// @param _toChainId The ID of the destination chain.
/// @param _receiverChainId The ID of the destination chain.
/// @param _to The address of the contract on the destination chain that will receive the message.
/// @param _data The data to be included in the message.
function dispatchMessage(uint256 _toChainId, address _to, bytes calldata _data)
function dispatchMessage(uint256 _receiverChainId, address _to, bytes calldata _data)
external
payable
override
onlyMultiBridgeMessageSender
returns (bytes32 msgId)
{
address receiverAdapter = receiverAdapters[_toChainId];
address receiverAdapter = receiverAdapters[_receiverChainId];

if (receiverAdapter == address(0)) {
revert Error.ZERO_RECEIVER_ADAPTER();
}

string memory destinationChain = chainIdMap[_toChainId];
string memory destinationChain = chainIdMap[_receiverChainId];

if (bytes(destinationChain).length == 0) {
revert Error.INVALID_DST_CHAIN();
}

msgId = _getNewMessageId(_toChainId, _to);
msgId = _getNewMessageId(_receiverChainId, _to);
_callContract(destinationChain, receiverAdapter, msgId, _to, _data);

emit MessageDispatched(msgId, msg.sender, _toChainId, _to, _data);
emit MessageDispatched(msgId, msg.sender, _receiverChainId, _to, _data);
}

/// @dev maps the MMA chain id to bridge specific chain id
Expand All @@ -87,26 +87,26 @@ contract AxelarSenderAdapter is BaseSenderAdapter {
////////////////////////////////////////////////////////////////*/

/// @dev Sends a message to the IAxelarRelayer contract for relaying to the Axelar Network.
/// @param destinationChain The name of the destination chain.
/// @param receiverAdapter The address of the adapter on the destination chain that will receive the message.
/// @param msgId The ID of the message to be relayed.
/// @param multibridgeReceiver The address of the MultibridgeReceiver contract on the destination chain that will receive the message.
/// @param data The bytes data to pass to the contract on the destination chain.
/// @param _destinationChain The name of the destination chain.
/// @param _receiverAdapter The address of the adapter on the destination chain that will receive the message.
/// @param _msgId The ID of the message to be relayed.
/// @param _multibridgeReceiver The address of the MultibridgeReceiver contract on the destination chain that will receive the message.
/// @param _data The bytes data to pass to the contract on the destination chain.
function _callContract(
string memory destinationChain,
address receiverAdapter,
bytes32 msgId,
address multibridgeReceiver,
bytes calldata data
string memory _destinationChain,
address _receiverAdapter,
bytes32 _msgId,
address _multibridgeReceiver,
bytes calldata _data
) internal {
string memory receiverAdapterInString = StringAddressConversion.toString(receiverAdapter);
string memory receiverAdapterInString = StringAddressConversion.toString(_receiverAdapter);
bytes memory payload =
abi.encode(AdapterPayload(msgId, address(msg.sender), receiverAdapter, multibridgeReceiver, data));
abi.encode(AdapterPayload(_msgId, address(msg.sender), _receiverAdapter, _multibridgeReceiver, _data));

gasService.payNativeGasForContractCall{value: msg.value}(
msg.sender, destinationChain, receiverAdapterInString, payload, msg.sender
msg.sender, _destinationChain, receiverAdapterInString, payload, msg.sender
);

gateway.callContract(destinationChain, receiverAdapterInString, payload);
gateway.callContract(_destinationChain, receiverAdapterInString, payload);
}
}
16 changes: 8 additions & 8 deletions src/adapters/axelar/interfaces/IAxelarExecutable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
pragma solidity >=0.8.9;

interface IAxelarExecutable {
/// @param commandId is axelar specific message identifier
/// @param sourceChain is the identifier for source chain
/// @param sourceAddress is the message sender address on source chain
/// @param payload is the cross-chain message sent
/// @param _commandId is axelar specific message identifier
/// @param _sourceChainId is the identifier for source chain
/// @param _sourceAddress is the message sender address on source chain
/// @param _payload is the cross-chain message sent
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;
}
10 changes: 5 additions & 5 deletions src/adapters/axelar/interfaces/IAxelarGasService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ pragma solidity >=0.8.9;
interface IAxelarGasService {
// This is called on the source chain before calling the gateway to execute a remote contract.
function payNativeGasForContractCall(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
address refundAddress
address _sender,
string calldata _destinationChain,
string calldata _destinationAddress,
bytes calldata _payload,
address _refundAddress
) external payable;
}
20 changes: 10 additions & 10 deletions src/adapters/axelar/interfaces/IAxelarGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
pragma solidity >=0.8.9;

interface IAxelarGateway {
function callContract(string calldata destinationChain, string calldata contractAddress, bytes calldata payload)
function callContract(string calldata _destinationChain, string calldata _contractAddress, bytes calldata _payload)
external;

function isContractCallApproved(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
address contractAddress,
bytes32 payloadHash
bytes32 _commandId,
string calldata _sourceChain,
string calldata _sourceAddress,
address _contractAddress,
bytes32 _payloadHash
) external view returns (bool);

function validateContractCall(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes32 payloadHash
bytes32 _commandId,
string calldata _sourceChain,
string calldata _sourceAddress,
bytes32 _payloadHash
) external returns (bool);
}
28 changes: 14 additions & 14 deletions src/adapters/wormhole/WormholeReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "../BaseReceiverAdapter.sol";
contract WormholeReceiverAdapter is BaseReceiverAdapter, IWormholeReceiver {
string public constant name = "WORMHOLE";
address public immutable relayer;
uint16 public immutable senderChain;
uint16 public immutable senderChainId;

/*/////////////////////////////////////////////////////////////////
STATE VARIABLES
Expand All @@ -46,20 +46,20 @@ contract WormholeReceiverAdapter is BaseReceiverAdapter, IWormholeReceiver {
////////////////////////////////////////////////////////////////*/

/// @param _relayer is wormhole relayer.
/// @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.
/// note: https://docs.wormhole.com/wormhole/quick-start/cross-chain-dev/automatic-relayer
constructor(address _relayer, uint16 _senderChain, address _receiverGAC) BaseReceiverAdapter(_receiverGAC) {
constructor(address _relayer, uint16 _senderChainId, address _receiverGAC) BaseReceiverAdapter(_receiverGAC) {
if (_relayer == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}

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

relayer = _relayer;
senderChain = _senderChain;
senderChainId = _senderChainId;
}

/*/////////////////////////////////////////////////////////////////
Expand All @@ -68,34 +68,34 @@ contract WormholeReceiverAdapter is BaseReceiverAdapter, IWormholeReceiver {

/// @inheritdoc IWormholeReceiver
function receiveWormholeMessages(
bytes memory payload,
bytes memory _payload,
bytes[] memory,
bytes32 sourceAddress,
uint16 sourceChain,
bytes32 deliveryHash
bytes32 _sourceAddress,
uint16 _sourceChainId,
bytes32 _deliveryHash
) public payable override onlyRelayerContract {
/// @dev validate the caller (done in modifier)
/// @dev step-1: validate incoming chain id
if (sourceChain != senderChain) {
if (_sourceChainId != senderChainId) {
revert Error.INVALID_SENDER_CHAIN_ID();
}

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

/// 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-3: check for duplicate message
if (isMessageExecuted[msgId] || deliveryHashStatus[deliveryHash]) {
if (isMessageExecuted[msgId] || deliveryHashStatus[_deliveryHash]) {
revert MessageIdAlreadyExecuted(msgId);
}

isMessageExecuted[decodedPayload.msgId] = true;
deliveryHashStatus[deliveryHash] = true;
deliveryHashStatus[_deliveryHash] = true;

/// @dev step-4: validate the receive adapter
if (decodedPayload.receiverAdapter != address(this)) {
Expand Down
10 changes: 5 additions & 5 deletions src/adapters/wormhole/WormholeSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ contract WormholeSenderAdapter is BaseSenderAdapter {
////////////////////////////////////////////////////////////////*/

/// @notice sends a message via wormhole relayer
function dispatchMessage(uint256 _toChainId, address _to, bytes calldata _data)
function dispatchMessage(uint256 _receiverChainId, address _to, bytes calldata _data)
external
payable
override
onlyMultiBridgeMessageSender
returns (bytes32 msgId)
{
address receiverAdapter = receiverAdapters[_toChainId];
address receiverAdapter = receiverAdapters[_receiverChainId];

if (receiverAdapter == address(0)) {
revert Error.ZERO_RECEIVER_ADAPTER();
}

uint16 wormChainId = chainIdMap[_toChainId];
uint16 wormChainId = chainIdMap[_receiverChainId];

if (wormChainId == 0) {
revert Error.INVALID_DST_CHAIN();
}

msgId = _getNewMessageId(_toChainId, _to);
msgId = _getNewMessageId(_receiverChainId, _to);
bytes memory payload = abi.encode(AdapterPayload(msgId, msg.sender, receiverAdapter, _to, _data));

relayer.sendPayloadToEvm{value: msg.value}(
Expand All @@ -63,7 +63,7 @@ contract WormholeSenderAdapter is BaseSenderAdapter {
senderGAC.msgDeliveryGasLimit()
);

emit MessageDispatched(msgId, msg.sender, _toChainId, _to, _data);
emit MessageDispatched(msgId, msg.sender, _receiverChainId, _to, _data);
}

/// @dev maps the MMA chain id to bridge specific chain id
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/EIP5164/MessageDispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ interface MessageDispatcher {
* @dev The MessageDispatched event MUST be emitted by the MessageDispatcher when an individual message is dispatched.
*/
event MessageDispatched(
bytes32 indexed messageId, address indexed from, uint256 indexed toChainId, address to, bytes data
bytes32 indexed messageId, address indexed from, uint256 indexed receiverChainId, address to, bytes data
);
}
6 changes: 3 additions & 3 deletions src/interfaces/EIP5164/SingleMessageDispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import "./MessageDispatcher.sol";

/**
* @dev The SingleMessageDispatcher is an extension of MessageDispatcher that defines a method, dispatchMessage,
* for dispatching an individual message to be executed on the toChainId.
* for dispatching an individual message to be executed on the receiverChainId.
* More about SingleMessageDispatcher of EIP5164, see https://eips.ethereum.org/EIPS/eip-5164#singlemessagedispatcher.
*/
interface SingleMessageDispatcher is MessageDispatcher {
/**
* @dev A method for dispatching an individual message to be executed on the toChainId.
* @dev A method for dispatching an individual message to be executed on the receiver chain.
*/
function dispatchMessage(uint256 toChainId, address to, bytes calldata data)
function dispatchMessage(uint256 _receiverChainId, address _to, bytes calldata _data)
external
payable
returns (bytes32 messageId);
Expand Down
Loading

0 comments on commit 2acaad8

Please sign in to comment.