Skip to content

Commit

Permalink
rebase to main
Browse files Browse the repository at this point in the history
  • Loading branch information
adu-web3 committed Sep 14, 2024
1 parent 853cbf7 commit bf1ceff
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 192 deletions.
2 changes: 1 addition & 1 deletion src/core/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ contract Bootstrap is
if (!isWhitelistedToken[token]) {
revert Errors.TokenNotWhitelisted(token);
}
if (token == VIRTUAL_STAKED_ETH_ADDRESS) {
if (token == VIRTUAL_NST_ADDRESS) {
revert Errors.NoTvlLimitForNativeRestaking();
}
IVault vault = _getVault(token);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ClientChainGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ contract ClientChainGateway is
// grave error, should never happen
revert Errors.TokenNotWhitelisted(token);
}
if (token == VIRTUAL_STAKED_ETH_ADDRESS) {
if (token == VIRTUAL_NST_ADDRESS) {
// not possible to set a TVL limit for native restaking
revert Errors.NoTvlLimitForNativeRestaking();
}
Expand Down
14 changes: 8 additions & 6 deletions src/core/ClientGatewayLzReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ abstract contract ClientGatewayLzReceiver is PausableUpgradeable, OAppReceiverUp
/// @inheritdoc OAppReceiverUpgradeable
// This function would call other functions inside this contract through low-level-call
// slither-disable-next-line reentrancy-no-eth
function _lzReceive(Origin calldata _origin, bytes calldata payload) internal virtual override whenNotPaused {
function _lzReceive(Origin calldata _origin, bytes calldata message) internal virtual override whenNotPaused {
if (_origin.srcEid != EXOCORE_CHAIN_ID) {
revert Errors.UnexpectedSourceChain(_origin.srcEid);
}

_verifyAndUpdateNonce(_origin.srcEid, _origin.sender, _origin.nonce);

Action act = Action(uint8(payload[0]));
Action act = Action(uint8(message[0]));
bytes calldata payload = message[1:];
if (act == Action.RESPOND) {
_handleResponse(payload);
_handleResponse(message);
} else {
bytes4 selector_ = _whiteListFunctionSelectors[act];
if (selector_ == bytes4(0)) {
revert Errors.UnsupportedRequest(act);
}

(bool success, bytes memory reason) =
address(this).call(abi.encodePacked(selector_, abi.encode(payload[1:])));
(bool success, bytes memory reason) = address(this).call(abi.encodePacked(selector_, abi.encode(payload)));
if (!success) {
revert Errors.RequestOrResponseExecuteFailed(act, _origin.nonce, reason);
}
Expand Down Expand Up @@ -215,7 +215,9 @@ abstract contract ClientGatewayLzReceiver is PausableUpgradeable, OAppReceiverUp
// like reentrancy.
// slither-disable-next-line reentrancy-no-eth
function afterReceiveAddWhitelistTokenRequest(bytes calldata payload) public onlyCalledFromThis whenNotPaused {
_validatePayloadLength(payload, ADD_TOKEN_WHITELIST_REQUEST_LENGTH, Action.REQUEST_ADD_WHITELIST_TOKEN);
if (payload.length != ADD_TOKEN_WHITELIST_REQUEST_LENGTH) {
revert Errors.InvalidMessageLength();
}
(address token, uint128 tvlLimit) = _decodeTokenUint128(payload);
isWhitelistedToken[token] = true;
whitelistTokens.push(token);
Expand Down
1 change: 1 addition & 0 deletions src/libraries/ActionAttributes.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {Action} from "../storage/GatewayStorage.sol";
Expand Down
15 changes: 5 additions & 10 deletions src/storage/GatewayStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ contract GatewayStorage {
/// @param nativeFee The native fee paid for the message.
event MessageSent(Action indexed act, bytes32 packetId, uint64 nonce, uint256 nativeFee);

/// @notice Emitted when a message is received and successfully executed.
/// @param act The action being performed.
/// @param nonce The nonce associated with the message.
event MessageExecuted(Action indexed act, uint64 nonce);

/// @notice Ensures the provided address is a valid exo Bech32 encoded address.
/// @param addressToValidate The address to check.
modifier isValidBech32Address(string calldata addressToValidate) {
Expand Down Expand Up @@ -86,14 +91,4 @@ contract GatewayStorage {
inboundNonce[srcChainId][srcAddress] = nonce;
}

/// @dev Validates the payload length, that it matches the expected length.
/// @param payload The payload to validate.
/// @param expectedLength The expected length of the payload.
/// @param action The action that the payload is for.
function _validatePayloadLength(bytes calldata payload, uint256 expectedLength, Action action) internal pure {
if (payload.length != expectedLength) {
revert InvalidRequestLength(action, expectedLength, payload.length);
}
}

}
19 changes: 7 additions & 12 deletions test/foundry/Delegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ contract DelegateTest is ExocoreDeployer {

/// clientGateway should emit MessageSent event
vm.expectEmit(true, true, true, true, address(clientGateway));
emit MessageSent(
Action.REQUEST_DELEGATE_TO, requestId, outboundNonces[clientChainId]++, requestNativeFee
);
emit MessageSent(Action.REQUEST_DELEGATE_TO, requestId, outboundNonces[clientChainId]++, requestNativeFee);

/// delegator call clientGateway to send delegation request
vm.startPrank(delegator.addr);
Expand All @@ -125,8 +123,7 @@ contract DelegateTest is ExocoreDeployer {
// 2. second layerzero relayers should watch the request message packet and relay the message to destination
// endpoint

bytes memory delegateResponsePayload =
abi.encodePacked(Action.RESPOND, outboundNonces[clientChainId] - 1, true);
bytes memory delegateResponsePayload = abi.encodePacked(Action.RESPOND, outboundNonces[clientChainId] - 1, true);
uint256 responseNativeFee = exocoreGateway.quote(clientChainId, delegateResponsePayload);
bytes32 responseId = generateUID(outboundNonces[exocoreChainId], false);

Expand Down Expand Up @@ -166,7 +163,7 @@ contract DelegateTest is ExocoreDeployer {
delegateAmount
);
vm.expectEmit(address(exocoreGateway));
emit MessageExecuted(GatewayStorage.Action.REQUEST_DELEGATE_TO, inboundNonces[exocoreChainId]++);
emit MessageExecuted(Action.REQUEST_DELEGATE_TO, inboundNonces[exocoreChainId]++);

/// relayer call layerzero endpoint to deliver request messages and generate response message
vm.startPrank(relayer.addr);
Expand Down Expand Up @@ -243,9 +240,7 @@ contract DelegateTest is ExocoreDeployer {

/// clientGateway should emit MessageSent event
vm.expectEmit(true, true, true, true, address(clientGateway));
emit MessageSent(
Action.REQUEST_UNDELEGATE_FROM, requestId, outboundNonces[clientChainId]++, requestNativeFee
);
emit MessageSent(Action.REQUEST_UNDELEGATE_FROM, requestId, outboundNonces[clientChainId]++, requestNativeFee);

/// delegator call clientGateway to send undelegation request
vm.startPrank(delegator.addr);
Expand Down Expand Up @@ -296,7 +291,7 @@ contract DelegateTest is ExocoreDeployer {
undelegateAmount
);
vm.expectEmit(address(exocoreGateway));
emit MessageExecuted(GatewayStorage.Action.REQUEST_UNDELEGATE_FROM, inboundNonces[exocoreChainId]++);
emit MessageExecuted(Action.REQUEST_UNDELEGATE_FROM, inboundNonces[exocoreChainId]++);

/// relayer call layerzero endpoint to deliver request messages and generate response message
vm.startPrank(relayer.addr);
Expand All @@ -321,12 +316,12 @@ contract DelegateTest is ExocoreDeployer {
/// event
vm.expectEmit(true, true, true, true, address(clientGateway));
emit RequestFinished(
GatewayStorage.Action.REQUEST_UNDELEGATE_FROM,
Action.REQUEST_UNDELEGATE_FROM,
outboundNonces[clientChainId] - 1, // request id
true
);
vm.expectEmit(address(clientGateway));
emit MessageExecuted(GatewayStorage.Action.RESPOND, inboundNonces[clientChainId]++);
emit MessageExecuted(Action.RESPOND, inboundNonces[clientChainId]++);

/// relayer should watch the response message and relay it back to client chain
vm.startPrank(relayer.addr);
Expand Down
34 changes: 10 additions & 24 deletions test/foundry/DepositThenDelegateTo.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,9 @@ contract DepositThenDelegateToTest is ExocoreDeployer {
);

vm.expectEmit(address(clientGateway));
<<<<<<< HEAD
emit MessageSent(
GatewayStorage.Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO,
requestId,
outboundNonces[clientChainId]++,
requestNativeFee
Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, requestId, outboundNonces[clientChainId]++, requestNativeFee
);
=======
emit MessageSent(Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, requestId, lzNonce, requestNativeFee);
>>>>>>> 68fba84 (feat: use ActionAttributes lib)

vm.startPrank(delegator);
clientGateway.depositThenDelegateTo{value: requestNativeFee}(
Expand All @@ -165,7 +158,7 @@ contract DepositThenDelegateToTest is ExocoreDeployer {
uint256 delegateAmount
) private {
bytes memory responsePayload =
abi.encodePacked(GatewayStorage.Action.RESPOND, outboundNonces[clientChainId] - 1, true, delegateAmount);
abi.encodePacked(Action.RESPOND, outboundNonces[clientChainId] - 1, true, delegateAmount);
uint256 responseNativeFee = exocoreGateway.quote(clientChainId, responsePayload);
bytes32 responseId = generateUID(outboundNonces[exocoreChainId], false);

Expand Down Expand Up @@ -205,14 +198,10 @@ contract DepositThenDelegateToTest is ExocoreDeployer {
);

vm.expectEmit(address(exocoreGateway));
emit MessageSent(GatewayStorage.Action.RESPOND, responseId, outboundNonces[exocoreChainId]++, responseNativeFee);
emit MessageSent(Action.RESPOND, responseId, outboundNonces[exocoreChainId]++, responseNativeFee);

vm.expectEmit(address(exocoreGateway));
emit DelegateResult(
true, bytes32(bytes20(address(restakeToken))), bytes32(bytes20(delegator)), operatorAddress, delegateAmount
);
vm.expectEmit(address(exocoreGateway));
emit MessageExecuted(GatewayStorage.Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, inboundNonces[exocoreChainId]++);
emit MessageExecuted(Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, inboundNonces[exocoreChainId]++);

vm.startPrank(relayer);
exocoreLzEndpoint.lzReceive(
Expand Down Expand Up @@ -243,11 +232,9 @@ contract DepositThenDelegateToTest is ExocoreDeployer {
assertEq(actualDelegateAmount, delegateAmount);

vm.expectEmit(true, true, true, true, address(clientGateway));
emit RequestFinished(
GatewayStorage.Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, outboundNonces[clientChainId] - 1, true
);
emit RequestFinished(Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, outboundNonces[clientChainId] - 1, true);
vm.expectEmit(address(clientGateway));
emit MessageExecuted(GatewayStorage.Action.RESPOND, inboundNonces[clientChainId]++);
emit MessageExecuted(Action.RESPOND, inboundNonces[clientChainId]++);

vm.startPrank(relayer);
clientChainLzEndpoint.lzReceive(
Expand All @@ -263,19 +250,18 @@ contract DepositThenDelegateToTest is ExocoreDeployer {
function _testFailureResponse(address delegator, address relayer, uint256 delegateAmount) private {
// we assume delegation failed for some reason
bool delegateSuccess = false;
bytes memory responsePayload = abi.encodePacked(
GatewayStorage.Action.RESPOND, outboundNonces[clientChainId] - 1, delegateSuccess, delegateAmount
);
bytes memory responsePayload =
abi.encodePacked(Action.RESPOND, outboundNonces[clientChainId] - 1, delegateSuccess, delegateAmount);
uint256 responseNativeFee = exocoreGateway.quote(clientChainId, responsePayload);
bytes32 responseId = generateUID(outboundNonces[exocoreChainId], false);

// request finished with successful deposit and failed delegation
vm.expectEmit(true, true, true, true, address(clientGateway));
emit RequestFinished(
GatewayStorage.Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, outboundNonces[clientChainId] - 1, delegateSuccess
Action.REQUEST_DEPOSIT_THEN_DELEGATE_TO, outboundNonces[clientChainId] - 1, delegateSuccess
);
vm.expectEmit(address(clientGateway));
emit MessageExecuted(GatewayStorage.Action.RESPOND, inboundNonces[clientChainId]++);
emit MessageExecuted(Action.RESPOND, inboundNonces[clientChainId]++);

vm.startPrank(relayer);
clientChainLzEndpoint.lzReceive(
Expand Down
Loading

0 comments on commit bf1ceff

Please sign in to comment.