Skip to content

Commit

Permalink
Merge pull request #12 from LayerZero-Labs/improve
Browse files Browse the repository at this point in the history
minor improvements
  • Loading branch information
cowboyisaac authored Jan 23, 2024
2 parents ccfd0d3 + 727f2c5 commit 0990059
Show file tree
Hide file tree
Showing 81 changed files with 1,245 additions and 1,013 deletions.
18 changes: 10 additions & 8 deletions messagelib/contracts/ExecutorFeeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ contract ExecutorFeeLib is Ownable, IExecutorFeeLib {
bytes calldata _options
) internal pure returns (uint256 dstAmount, uint256 totalGas) {
if (_options.length == 0) {
revert NoOptions();
revert Executor_NoOptions();
}

uint256 cursor = 0;
bool ordered = false;
totalGas = _baseGas;

uint256 lzReceiveGas;
while (cursor < _options.length) {
(uint8 optionType, bytes calldata option, uint256 newCursor) = _options.nextExecutorOption(cursor);
cursor = newCursor;
Expand All @@ -121,31 +122,32 @@ contract ExecutorFeeLib is Ownable, IExecutorFeeLib {
(uint128 gas, uint128 value) = ExecutorOptions.decodeLzReceiveOption(option);

// endpoint v1 does not support lzReceive with value
if (_v1Eid && value > 0) revert UnsupportedOptionType(optionType);
if (_v1Eid && value > 0) revert Executor_UnsupportedOptionType(optionType);

dstAmount += value;
totalGas += gas;
lzReceiveGas += gas;
} else if (optionType == ExecutorOptions.OPTION_TYPE_NATIVE_DROP) {
(uint128 nativeDropAmount, ) = ExecutorOptions.decodeNativeDropOption(option);
dstAmount += nativeDropAmount;
} else if (optionType == ExecutorOptions.OPTION_TYPE_LZCOMPOSE) {
// endpoint v1 does not support lzCompose
if (_v1Eid) revert UnsupportedOptionType(optionType);
if (_v1Eid) revert Executor_UnsupportedOptionType(optionType);

(, uint128 gas, uint128 value) = ExecutorOptions.decodeLzComposeOption(option);
dstAmount += value;
totalGas += gas;
} else if (optionType == ExecutorOptions.OPTION_TYPE_ORDERED_EXECUTION) {
ordered = true;
} else {
revert UnsupportedOptionType(optionType);
revert Executor_UnsupportedOptionType(optionType);
}
}
if (cursor != _options.length) revert InvalidExecutorOptions(cursor);
if (dstAmount > _nativeCap) revert NativeAmountExceedsCap(dstAmount, _nativeCap);
if (cursor != _options.length) revert Executor_InvalidExecutorOptions(cursor);
if (dstAmount > _nativeCap) revert Executor_NativeAmountExceedsCap(dstAmount, _nativeCap);
if (lzReceiveGas == 0) revert Executor_ZeroLzReceiveGasProvided();
totalGas += lzReceiveGas;

if (ordered) {
// todo: finalize the premium for ordered
totalGas = (totalGas * 102) / 100;
}
}
Expand Down
4 changes: 2 additions & 2 deletions messagelib/contracts/MessageLibBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ abstract contract MessageLibBase {
address internal immutable endpoint;
uint32 internal immutable localEid;

error OnlyEndpoint();
error LZ_MessageLib_OnlyEndpoint();

modifier onlyEndpoint() {
if (endpoint != msg.sender) revert OnlyEndpoint();
if (endpoint != msg.sender) revert LZ_MessageLib_OnlyEndpoint();
_;
}

Expand Down
6 changes: 3 additions & 3 deletions messagelib/contracts/PriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract PriceFeed is ILayerZeroPriceFeed, OwnableUpgradeable, Proxied {
modifier onlyPriceUpdater() {
if (owner() != msg.sender) {
if (!priceUpdater[msg.sender]) {
revert OnlyPriceUpdater();
revert LZ_PriceFeed_OnlyPriceUpdater();
}
}
_;
Expand Down Expand Up @@ -102,7 +102,7 @@ contract PriceFeed is ILayerZeroPriceFeed, OwnableUpgradeable, Proxied {
uint256 _gas
) external payable returns (uint256, uint128, uint128, uint128) {
uint256 fee = getFee(_dstEid, _callDataSize, _gas);
if (msg.value < fee) revert InsufficientFee(msg.value, fee);
if (msg.value < fee) revert LZ_PriceFeed_InsufficientFee(msg.value, fee);
return _estimateFeeByEid(_dstEid, _callDataSize, _gas);
}

Expand Down Expand Up @@ -181,7 +181,7 @@ contract PriceFeed is ILayerZeroPriceFeed, OwnableUpgradeable, Proxied {
} else if (l2Eid == 20132) {
return 20121; // ethereum-goerli
}
revert UnknownL2Eid(l2Eid);
revert LZ_PriceFeed_UnknownL2Eid(l2Eid);
}

function _estimateFeeWithDefaultModel(
Expand Down
26 changes: 0 additions & 26 deletions messagelib/contracts/ReceiveLibBaseE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,4 @@ abstract contract ReceiveLibBaseE2 is MessageLibBase, ERC165, IMessageLib {
function messageLibType() external pure virtual override returns (MessageLibType) {
return MessageLibType.Receive;
}

// ========================= VIEW FUNCTIONS FOR OFFCHAIN ONLY =========================
// Not involved in any state transition function.
// ====================================================================================

/// @dev checks for endpoint verifiable and endpoint has payload hash
function _verifiable(
uint32 _srcEid,
address _receiver,
bytes calldata _packetHeader,
bytes32 _payloadHash
) internal view returns (bool) {
Origin memory origin = Origin(_srcEid, _packetHeader.sender(), _packetHeader.nonce());

// check endpoint verifiable
if (!ILayerZeroEndpointV2(endpoint).verifiable(origin, _receiver, address(this), _payloadHash)) return false;

// if endpoint.verifiable, also check if the payload hash matches
// endpoint allows re-verify, check if this payload has already been verified
if (
ILayerZeroEndpointV2(endpoint).inboundPayloadHash(_receiver, origin.srcEid, origin.sender, origin.nonce) ==
_payloadHash
) return false;

return true;
}
}
20 changes: 10 additions & 10 deletions messagelib/contracts/SendLibBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ abstract contract SendLibBase is MessageLibBase, Ownable {
event ExecutorConfigSet(address oapp, uint32 eid, ExecutorConfig config);
event TreasuryNativeFeeCapSet(uint256 newTreasuryNativeFeeCap);

error InvalidMessageSize(uint256 actual, uint256 max);
error InvalidAmount(uint256 requested, uint256 available);
error TransferFailed();
error InvalidExecutor();
error ZeroMessageSize();
error LZ_MessageLib_InvalidMessageSize(uint256 actual, uint256 max);
error LZ_MessageLib_InvalidAmount(uint256 requested, uint256 available);
error LZ_MessageLib_TransferFailed();
error LZ_MessageLib_InvalidExecutor();
error LZ_MessageLib_ZeroMessageSize();

constructor(
address _endpoint,
Expand All @@ -68,8 +68,8 @@ abstract contract SendLibBase is MessageLibBase, Ownable {
for (uint256 i = 0; i < _params.length; ++i) {
SetDefaultExecutorConfigParam calldata param = _params[i];

if (param.config.executor == address(0x0)) revert InvalidExecutor();
if (param.config.maxMessageSize == 0) revert ZeroMessageSize();
if (param.config.executor == address(0x0)) revert LZ_MessageLib_InvalidExecutor();
if (param.config.maxMessageSize == 0) revert LZ_MessageLib_ZeroMessageSize();

executorConfigs[DEFAULT_CONFIG][param.eid] = param.config;
}
Expand All @@ -80,7 +80,7 @@ abstract contract SendLibBase is MessageLibBase, Ownable {
function setTreasuryNativeFeeCap(uint256 _newTreasuryNativeFeeCap) external onlyOwner {
// assert the new value is no greater than the old value
if (_newTreasuryNativeFeeCap > treasuryNativeFeeCap)
revert InvalidAmount(_newTreasuryNativeFeeCap, treasuryNativeFeeCap);
revert LZ_MessageLib_InvalidAmount(_newTreasuryNativeFeeCap, treasuryNativeFeeCap);
treasuryNativeFeeCap = _newTreasuryNativeFeeCap;
emit TreasuryNativeFeeCapSet(_newTreasuryNativeFeeCap);
}
Expand All @@ -100,7 +100,7 @@ abstract contract SendLibBase is MessageLibBase, Ownable {

// ======================= Internal =======================
function _assertMessageSize(uint256 _actual, uint256 _max) internal pure {
if (_actual > _max) revert InvalidMessageSize(_actual, _max);
if (_actual > _max) revert LZ_MessageLib_InvalidMessageSize(_actual, _max);
}

function _payExecutor(
Expand Down Expand Up @@ -227,7 +227,7 @@ abstract contract SendLibBase is MessageLibBase, Ownable {
/// @dev authenticated by msg.sender only
function _debitFee(uint256 _amount) internal {
uint256 fee = fees[msg.sender];
if (_amount > fee) revert InvalidAmount(_amount, fee);
if (_amount > fee) revert LZ_MessageLib_InvalidAmount(_amount, fee);
unchecked {
fees[msg.sender] = fee - _amount;
}
Expand Down
8 changes: 4 additions & 4 deletions messagelib/contracts/SendLibBaseE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ abstract contract SendLibBaseE2 is SendLibBase, ERC165, ISendLib {
event NativeFeeWithdrawn(address worker, address receiver, uint256 amount);
event LzTokenFeeWithdrawn(address lzToken, address receiver, uint256 amount);

error NotTreasury();
error CannotWithdrawAltToken();
error LZ_MessageLib_NotTreasury();
error LZ_MessageLib_CannotWithdrawAltToken();

constructor(
address _endpoint,
Expand Down Expand Up @@ -75,10 +75,10 @@ abstract contract SendLibBaseE2 is SendLibBase, ERC165, ISendLib {
/// @dev E2 only
/// @dev treasury only function
function withdrawLzTokenFee(address _lzToken, address _to, uint256 _amount) external {
if (msg.sender != treasury) revert NotTreasury();
if (msg.sender != treasury) revert LZ_MessageLib_NotTreasury();

// lz token cannot be the same as the native token
if (ILayerZeroEndpointV2(endpoint).nativeToken() == _lzToken) revert CannotWithdrawAltToken();
if (ILayerZeroEndpointV2(endpoint).nativeToken() == _lzToken) revert LZ_MessageLib_CannotWithdrawAltToken();

Transfer.token(_lzToken, _to, _amount);

Expand Down
4 changes: 2 additions & 2 deletions messagelib/contracts/Treasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract Treasury is Ownable, ILayerZeroTreasury {
uint256 public lzTokenFee;
bool public lzTokenEnabled;

error LzTokenNotEnabled();
error LZ_Treasury_LzTokenNotEnabled();

function getFee(
address /*_sender*/,
Expand Down Expand Up @@ -65,7 +65,7 @@ contract Treasury is Ownable, ILayerZeroTreasury {

function _getFee(uint256 _totalFee, bool _payInLzToken) internal view returns (uint256) {
if (_payInLzToken) {
if (!lzTokenEnabled) revert LzTokenNotEnabled();
if (!lzTokenEnabled) revert LZ_Treasury_LzTokenNotEnabled();
return lzTokenFee;
} else {
return (_totalFee * nativeBP) / 10000;
Expand Down
6 changes: 3 additions & 3 deletions messagelib/contracts/Worker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract contract Worker is AccessControl, Pausable, IWorker {

modifier onlyAcl(address _sender) {
if (!hasAcl(_sender)) {
revert NotAllowed();
revert Worker_NotAllowed();
}
_;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ abstract contract Worker is AccessControl, Pausable, IWorker {
/// @param _to address to withdraw fee to
/// @param _amount amount to withdraw
function withdrawFee(address _lib, address _to, uint256 _amount) external onlyRole(ADMIN_ROLE) {
if (!hasRole(MESSAGE_LIB_ROLE, _lib)) revert OnlyMessageLib();
if (!hasRole(MESSAGE_LIB_ROLE, _lib)) revert Worker_OnlyMessageLib();
ISendLib(_lib).withdrawFee(_to, _amount);
emit Withdraw(_lib, _to, _amount);
}
Expand Down Expand Up @@ -162,6 +162,6 @@ abstract contract Worker is AccessControl, Pausable, IWorker {

/// @dev overrides AccessControl to disable renouncing of roles
function renounceRole(bytes32 /*role*/, address /*account*/) public pure override {
revert RoleRenouncingDisabled();
revert Worker_RoleRenouncingDisabled();
}
}
9 changes: 5 additions & 4 deletions messagelib/contracts/interfaces/IExecutorFeeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ interface IExecutorFeeLib {
uint16 defaultMultiplierBps;
}

error NoOptions();
error NativeAmountExceedsCap(uint256 amount, uint256 cap);
error UnsupportedOptionType(uint8 optionType);
error InvalidExecutorOptions(uint256 cursor);
error Executor_NoOptions();
error Executor_NativeAmountExceedsCap(uint256 amount, uint256 cap);
error Executor_UnsupportedOptionType(uint8 optionType);
error Executor_InvalidExecutorOptions(uint256 cursor);
error Executor_ZeroLzReceiveGasProvided();

function getFeeOnSend(
FeeParams calldata _params,
Expand Down
6 changes: 3 additions & 3 deletions messagelib/contracts/interfaces/ILayerZeroPriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ interface ILayerZeroPriceFeed {
ArbitrumPriceExt extend;
}

error OnlyPriceUpdater();
error InsufficientFee(uint256 provided, uint256 required);
error UnknownL2Eid(uint32 l2Eid);
error LZ_PriceFeed_OnlyPriceUpdater();
error LZ_PriceFeed_InsufficientFee(uint256 provided, uint256 required);
error LZ_PriceFeed_UnknownL2Eid(uint32 l2Eid);

function nativeTokenPriceUSD() external view returns (uint128);

Expand Down
6 changes: 3 additions & 3 deletions messagelib/contracts/interfaces/IWorker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interface IWorker {
event SetSupportedOptionTypes(uint32 dstEid, uint8[] optionTypes);
event Withdraw(address lib, address to, uint256 amount);

error NotAllowed();
error OnlyMessageLib();
error RoleRenouncingDisabled();
error Worker_NotAllowed();
error Worker_OnlyMessageLib();
error Worker_RoleRenouncingDisabled();

function setPriceFeed(address _priceFeed) external;

Expand Down
97 changes: 0 additions & 97 deletions messagelib/contracts/uln/LzExecutor.sol

This file was deleted.

Loading

0 comments on commit 0990059

Please sign in to comment.