Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
Dominator008 committed Sep 13, 2023
1 parent 6e2eb83 commit 3a8b8b6
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 249 deletions.
2 changes: 1 addition & 1 deletion src/adapters/BaseSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract contract BaseSenderAdapter is IBridgeSenderAdapter {

modifier onlyGlobalOwner() {
if (!gac.isGlobalOwner(msg.sender)) {
revert Error.INVALID_PRIVILEGED_CALLER();
revert Error.CALLER_NOT_OWNER();
}
_;
}
Expand Down
11 changes: 2 additions & 9 deletions src/adapters/Celer/CelerReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,15 @@ contract CelerReceiverAdapter is IBridgeReceiverAdapter, IMessageReceiverApp {
////////////////////////////////////////////////////////////////*/

/// @inheritdoc IBridgeReceiverAdapter
function updateSenderAdapter(bytes memory _senderChain, address _senderAdapter) external override onlyGlobalOwner {
uint64 _senderChainDecoded = abi.decode(_senderChain, (uint64));

if (_senderChainDecoded == 0) {
revert Error.ZERO_CHAIN_ID();
}

function updateSenderAdapter(address _senderAdapter) external override onlyGlobalOwner {
if (_senderAdapter == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}

address oldAdapter = senderAdapter;
senderAdapter = _senderAdapter;
senderChain = _senderChainDecoded;

emit SenderAdapterUpdated(oldAdapter, _senderAdapter, _senderChain);
emit SenderAdapterUpdated(oldAdapter, _senderAdapter);
}

/// @dev accepts incoming messages from celer message bus
Expand Down
20 changes: 9 additions & 11 deletions src/adapters/Wormhole/WormholeReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ contract WormholeReceiverAdapter is IBridgeReceiverAdapter, IWormholeReceiver {
string public constant name = "wormhole";
address public immutable relayer;
IGAC public immutable gac;
uint16 public immutable senderChain = uint16(2); // Wormhole chain ID for Ethereum

/*/////////////////////////////////////////////////////////////////
STATE VARIABLES
////////////////////////////////////////////////////////////////*/
address public senderAdapter;
uint16 public senderChain;

mapping(uint256 => uint16) public chainIdMap;

Expand Down Expand Up @@ -67,22 +67,15 @@ contract WormholeReceiverAdapter is IBridgeReceiverAdapter, IWormholeReceiver {
////////////////////////////////////////////////////////////////*/

/// @inheritdoc IBridgeReceiverAdapter
function updateSenderAdapter(bytes memory _senderChain, address _senderAdapter) external override onlyGlobalOwner {
uint16 _senderChainDecoded = abi.decode(_senderChain, (uint16));

if (_senderChainDecoded == 0) {
revert Error.ZERO_CHAIN_ID();
}

function updateSenderAdapter(address _senderAdapter) external override onlyGlobalOwner {
if (_senderAdapter == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}

address oldAdapter = senderAdapter;
senderAdapter = _senderAdapter;
senderChain = _senderChainDecoded;

emit SenderAdapterUpdated(oldAdapter, _senderAdapter, _senderChain);
emit SenderAdapterUpdated(oldAdapter, _senderAdapter);
}

/// @dev maps the MMA chain id to bridge specific chain id
Expand Down Expand Up @@ -137,7 +130,12 @@ contract WormholeReceiverAdapter is IBridgeReceiverAdapter, IWormholeReceiver {
isMessageExecuted[decodedPayload.msgId] = true;
deliveryHashStatus[deliveryHash] = true;

/// @dev step-4: validate the destination
/// @dev step-4: validate the receive adapter
if (decodedPayload.receiverAdapter != address(this)) {
revert Error.INVALID_RECEIVER_ADAPTER();
}

/// @dev step-5: validate the destination
if (decodedPayload.finalDestination != gac.getMultiMessageReceiver(block.chainid)) {
revert Error.INVALID_FINAL_DESTINATION();
}
Expand Down
22 changes: 10 additions & 12 deletions src/adapters/axelar/AxelarReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IBridgeReceiverAdapter {
using StringAddressConversion for string;

string public constant name = "axelar";
string public constant senderChain = "ethereum";

IAxelarGateway public immutable gateway;
IGAC public immutable gac;
Expand All @@ -28,7 +29,6 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IBridgeReceiverAdapter {
STATE VARIABLES
////////////////////////////////////////////////////////////////*/
address public senderAdapter;
string public senderChain;

mapping(bytes32 => bool) public isMessageExecuted;
mapping(bytes32 => bool) public commandIdStatus;
Expand Down Expand Up @@ -56,22 +56,15 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IBridgeReceiverAdapter {
////////////////////////////////////////////////////////////////*/

/// @inheritdoc IBridgeReceiverAdapter
function updateSenderAdapter(bytes memory _senderChain, address _senderAdapter) external override onlyGlobalOwner {
string memory _senderChainDecoded = abi.decode(_senderChain, (string));

if (keccak256(abi.encode(_senderChainDecoded)) == keccak256(abi.encode(""))) {
revert Error.ZERO_CHAIN_ID();
}

function updateSenderAdapter(address _senderAdapter) external override onlyGlobalOwner {
if (_senderAdapter == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}

address oldAdapter = senderAdapter;
senderAdapter = _senderAdapter;
senderChain = _senderChainDecoded;

emit SenderAdapterUpdated(oldAdapter, _senderAdapter, _senderChain);
emit SenderAdapterUpdated(oldAdapter, _senderAdapter);
}

/// @dev accepts new cross-chain messages from axelar gateway
Expand All @@ -87,7 +80,7 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IBridgeReceiverAdapter {
revert Error.INVALID_SENDER_CHAIN_ID();
}

/// @dev step-2: validate the caller
/// @dev step-2: validate the contract call
if (!gateway.validateContractCall(commandId, sourceChain, sourceAddress, keccak256(payload))) {
revert Error.NOT_APPROVED_BY_GATEWAY();
}
Expand All @@ -106,7 +99,12 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IBridgeReceiverAdapter {
revert MessageIdAlreadyExecuted(msgId);
}

/// @dev step-5: validate the destination
/// @dev step-5: validate the receive adapter
if (decodedPayload.receiverAdapter != address(this)) {
revert Error.INVALID_RECEIVER_ADAPTER();
}

/// @dev step-6: validate the destination
if (decodedPayload.finalDestination != gac.getMultiMessageReceiver(block.chainid)) {
revert Error.INVALID_FINAL_DESTINATION();
}
Expand Down
5 changes: 2 additions & 3 deletions src/interfaces/IBridgeReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IBridgeReceiverAdapter is MessageExecutor {
/*/////////////////////////////////////////////////////////////////
EVENTS
////////////////////////////////////////////////////////////////*/
event SenderAdapterUpdated(address indexed oldSenderAdapter, address indexed newSenderAdapter, bytes senderChain);
event SenderAdapterUpdated(address indexed oldSenderAdapter, address indexed newSenderAdapter);

/*/////////////////////////////////////////////////////////////////
EXTERNAL FUNCTIONS
Expand All @@ -18,8 +18,7 @@ interface IBridgeReceiverAdapter is MessageExecutor {
function name() external view returns (string memory);

/// @dev allows global admin to update the sender adapter
/// @param _senderChain is the bridge native sender chain (ETH) as bytes
/// @param _senderAdapter is the bridge's sender adapter deployed to Ethereum
/// note: access controlled to be called by the global admin contract
function updateSenderAdapter(bytes memory _senderChain, address _senderAdapter) external;
function updateSenderAdapter(address _senderAdapter) external;
}
10 changes: 5 additions & 5 deletions src/libraries/Error.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ library Error {
/// @dev is thrown if the length of two arrays are mismatched
error ARRAY_LENGTH_MISMATCHED();

/// @dev is thrown if caller is not owner of the contract
/// @dev is thrown if caller is not the privileged caller
error INVALID_PRIVILEGED_CALLER();

/// @dev is thrown if caller is invalid receiver adapter
Expand All @@ -33,7 +33,7 @@ library Error {
error MSG_ID_ALREADY_EXECUTED();

/// @dev is thrown if message conflicts with current storage
error NEW_MESSAGE_CONFLITS_WITH_OLD_DATA();
error NEW_MESSAGE_CONFLICTS_WITH_OLD_DATA();

/// @dev is thrown if bridge adapter already delivered the message to multi message receiver
error DUPLICATE_MESSAGE_DELIVERY_BY_ADAPTER();
Expand Down Expand Up @@ -75,13 +75,13 @@ library Error {
/// @dev is thrown if caller is not multi message sender
error CALLER_NOT_MULTI_MESSAGE_SENDER();

/// @dev is thrown if sender chain is not allowed on reciever adapter
/// @dev is thrown if sender chain is not allowed on receiver adapter
error INVALID_SENDER_CHAIN_ID();

/// @dev is thrown if sender adapter is not allowed on reciever adapter
/// @dev is thrown if sender adapter is not allowed on receiver adapter
error INVALID_SENDER_ADAPTER();

/// @dev is thrown if final destination is not mma receiver on reciever adapter
/// @dev is thrown if final destination is not mma receiver on receiver adapter
error INVALID_FINAL_DESTINATION();

/// @dev is thrown if chain id is zero
Expand Down
4 changes: 2 additions & 2 deletions test/Setup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ abstract contract Setup is Test {
vm.selectFork(fork[chainId]);

WormholeReceiverAdapter(contractAddress[chainId]["WORMHOLE_RECEIVER_ADAPTER"]).updateSenderAdapter(
abi.encode(_wormholeChainId(1)), contractAddress[1]["WORMHOLE_SENDER_ADAPTER"]
contractAddress[1]["WORMHOLE_SENDER_ADAPTER"]
);

AxelarReceiverAdapter(contractAddress[chainId]["AXELAR_RECEIVER_ADAPTER"]).updateSenderAdapter(
abi.encode(_axelarChainId(1)), contractAddress[1]["AXELAR_SENDER_ADAPTER"]
contractAddress[1]["AXELAR_SENDER_ADAPTER"]
);

unchecked {
Expand Down
22 changes: 8 additions & 14 deletions test/unit-tests/adapters/BaseSenderAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import {AxelarSenderAdapter} from "src/adapters/axelar/AxelarSenderAdapter.sol";
contract AxelarSenderAdapterTest is Setup {
event ReceiverAdapterUpdated(uint256 dstChainId, address receiverAdapter);

uint256 constant SRC_CHAIN_ID = 1;
uint256 constant DST_CHAIN_ID = 137;

// Test base contract with Axelar adapter
AxelarSenderAdapter adapter;

Expand All @@ -30,29 +27,26 @@ contract AxelarSenderAdapterTest is Setup {
function test_update_receiver_adapter() public {
vm.startPrank(owner);

uint256[] memory dstChainIds = new uint256[](2);
dstChainIds[0] = 56;
dstChainIds[1] = DST_CHAIN_ID;
address[] memory receiverAdapters = new address[](2);
receiverAdapters[0] = address(42);
receiverAdapters[1] = address(43);

vm.expectEmit(true, true, true, true, address(adapter));
emit ReceiverAdapterUpdated(56, address(42));
emit ReceiverAdapterUpdated(BSC_CHAIN_ID, address(42));
vm.expectEmit(true, true, true, true, address(adapter));
emit ReceiverAdapterUpdated(DST_CHAIN_ID, address(43));
emit ReceiverAdapterUpdated(POLYGON_CHAIN_ID, address(43));

adapter.updateReceiverAdapter(dstChainIds, receiverAdapters);
adapter.updateReceiverAdapter(DST_CHAINS, receiverAdapters);

assertEq(adapter.receiverAdapters(56), address(42));
assertEq(adapter.receiverAdapters(DST_CHAIN_ID), address(43));
assertEq(adapter.receiverAdapters(BSC_CHAIN_ID), address(42));
assertEq(adapter.receiverAdapters(POLYGON_CHAIN_ID), address(43));
}

/// @dev only privileged caller can update receiver adapter
function test_update_receiver_adapter_only_privileged_caller() public {
/// @dev only global owner can update receiver adapter
function test_update_receiver_adapter_only_global_owner() public {
vm.startPrank(caller);

vm.expectRevert(Error.INVALID_PRIVILEGED_CALLER.selector);
vm.expectRevert(Error.CALLER_NOT_OWNER.selector);
adapter.updateReceiverAdapter(new uint256[](0), new address[](0));
}

Expand Down
Loading

0 comments on commit 3a8b8b6

Please sign in to comment.