Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evm: Reduce contract size of Manager #177

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions evm/src/EndpointRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ abstract contract EndpointRegistry {

/// =============== STORAGE ===============================================

bytes32 public constant ENDPOINT_INFOS_SLOT =
bytes32 private constant ENDPOINT_INFOS_SLOT =
bytes32(uint256(keccak256("ntt.endpointInfos")) - 1);

bytes32 public constant ENDPOINT_BITMAP_SLOT =
bytes32 private constant ENDPOINT_BITMAP_SLOT =
bytes32(uint256(keccak256("ntt.endpointBitmap")) - 1);

bytes32 public constant ENABLED_ENDPOINTS_SLOT =
bytes32 private constant ENABLED_ENDPOINTS_SLOT =
bytes32(uint256(keccak256("ntt.enabledEndpoints")) - 1);

bytes32 public constant REGISTERED_ENDPOINTS_SLOT =
bytes32 private constant REGISTERED_ENDPOINTS_SLOT =
bytes32(uint256(keccak256("ntt.registeredEndpoints")) - 1);

bytes32 public constant NUM_REGISTERED_ENDPOINTS_SLOT =
bytes32 private constant NUM_REGISTERED_ENDPOINTS_SLOT =
bytes32(uint256(keccak256("ntt.numRegisteredEndpoints")) - 1);

function _getEndpointInfosStorage()
Expand Down
43 changes: 13 additions & 30 deletions evm/src/Manager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ contract Manager is
error EndpointAlreadyAttestedToMessage(bytes32 managerMessageHash);

address public immutable token;
address public immutable deployer;
address immutable deployer;
Mode public immutable mode;
uint16 public immutable chainId;
uint256 public immutable evmChainId;
uint256 immutable evmChainId;

enum Mode {
LOCKING,
Expand All @@ -69,15 +69,15 @@ contract Manager is

/// =============== STORAGE ===============================================

bytes32 public constant MESSAGE_ATTESTATIONS_SLOT =
bytes32 private constant MESSAGE_ATTESTATIONS_SLOT =
bytes32(uint256(keccak256("ntt.messageAttestations")) - 1);

bytes32 public constant MESSAGE_SEQUENCE_SLOT =
bytes32 private constant MESSAGE_SEQUENCE_SLOT =
bytes32(uint256(keccak256("ntt.messageSequence")) - 1);

bytes32 public constant SIBLINGS_SLOT = bytes32(uint256(keccak256("ntt.siblings")) - 1);
bytes32 private constant SIBLINGS_SLOT = bytes32(uint256(keccak256("ntt.siblings")) - 1);

bytes32 public constant THRESHOLD_SLOT = bytes32(uint256(keccak256("ntt.threshold")) - 1);
bytes32 private constant THRESHOLD_SLOT = bytes32(uint256(keccak256("ntt.threshold")) - 1);

/// =============== GETTERS/SETTERS ========================================

Expand Down Expand Up @@ -221,12 +221,6 @@ contract Manager is
}
}

/// @dev Override the [`renounceOwnership`] function to ensure
/// the manager ownership is not renounced.
function renounceOwnership() public view override onlyOwner {
revert CannotRenounceManagerOwnership(owner());
}

/// @dev This method should return an array of delivery prices corresponding to each endpoint.
function quoteDeliveryPrice(
uint16 recipientChain,
Expand Down Expand Up @@ -309,11 +303,11 @@ contract Manager is
}

function setOutboundLimit(uint256 limit) external onlyOwner {
_setOutboundLimit(nttNormalize(limit));
_setOutboundLimit(_nttNormalize(limit));
}

function setInboundLimit(uint256 limit, uint16 chainId_) external onlyOwner {
_setInboundLimit(nttNormalize(limit), chainId_);
_setInboundLimit(_nttNormalize(limit), chainId_);
}

function completeOutboundQueuedTransfer(uint64 messageSequence)
Expand Down Expand Up @@ -367,9 +361,9 @@ contract Manager is
{
NormalizedAmount memory normalizedAmount;
{
normalizedAmount = nttNormalize(amount);
normalizedAmount = _nttNormalize(amount);
// don't deposit dust that can not be bridged due to the decimal shift
uint256 newAmount = nttDenormalize(normalizedAmount);
uint256 newAmount = _nttDenormalize(normalizedAmount);
if (amount != newAmount) {
revert TransferAmountHasDust(amount, amount - newAmount);
}
Expand Down Expand Up @@ -560,7 +554,7 @@ contract Manager is
recipientChain, priceQuotes, instructions, enabledEndpoints, encodedManagerPayload
);

emit TransferSent(recipient, nttDenormalize(amount), recipientChain, sequence);
emit TransferSent(recipient, _nttDenormalize(amount), recipientChain, sequence);

// return the sequence number
return sequence;
Expand Down Expand Up @@ -620,7 +614,7 @@ contract Manager is
revert InvalidTargetChain(nativeTokenTransfer.toChain, chainId);
}

NormalizedAmount memory nativeTransferAmount = nttFixDecimals(nativeTokenTransfer.amount);
NormalizedAmount memory nativeTransferAmount = _nttFixDecimals(nativeTokenTransfer.amount);

address transferRecipient = fromWormholeFormat(nativeTokenTransfer.to);

Expand Down Expand Up @@ -671,7 +665,7 @@ contract Manager is
) internal {
// calculate proper amount of tokens to unlock/mint to recipient
// denormalize the amount
uint256 denormalizedAmount = nttDenormalize(amount);
uint256 denormalizedAmount = _nttDenormalize(amount);

emit TransferRedeemed(digest);

Expand Down Expand Up @@ -769,24 +763,13 @@ contract Manager is
return tokenDecimals;
}

// @dev Count the number of set bits in a uint64
function countSetBits(uint64 x) public pure returns (uint8 count) {
while (x != 0) {
x &= x - 1;
count++;
}

return count;
}

/// ============== INVARIANTS =============================================

/// @dev When we add new immutables, this function should be updated
function _checkImmutables() internal view override {
assert(this.token() == token);
assert(this.mode() == mode);
assert(this.chainId() == chainId);
assert(this.evmChainId() == evmChainId);
assert(this.rateLimitDuration() == rateLimitDuration);
RahulMaganti47 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
10 changes: 5 additions & 5 deletions evm/src/NttNormalizer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ abstract contract NttNormalizer {
return abi.decode(queriedDecimals, (uint8));
}

function nttNormalize(uint256 amount) public view returns (NormalizedAmount memory) {
function _nttNormalize(uint256 amount) internal view returns (NormalizedAmount memory) {
return amount.normalize(tokenDecimals);
}

function nttDenormalize(NormalizedAmount memory amount) public view returns (uint256) {
function _nttDenormalize(NormalizedAmount memory amount) internal view returns (uint256) {
return amount.denormalize(tokenDecimals);
}

/// @dev Shift decimals of `amount` to match the token decimals
function nttFixDecimals(NormalizedAmount memory amount)
public
function _nttFixDecimals(NormalizedAmount memory amount)
internal
view
returns (NormalizedAmount memory)
{
return nttNormalize(nttDenormalize(amount));
return _nttNormalize(_nttDenormalize(amount));
}
}
10 changes: 5 additions & 5 deletions evm/src/WormholeEndpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ contract WormholeEndpoint is Endpoint, IWormholeEndpoint, IWormholeReceiver {

/// =============== STORAGE ===============================================

bytes32 public constant WORMHOLE_CONSUMED_VAAS_SLOT =
bytes32 private constant WORMHOLE_CONSUMED_VAAS_SLOT =
bytes32(uint256(keccak256("whEndpoint.consumedVAAs")) - 1);

bytes32 public constant WORMHOLE_SIBLINGS_SLOT =
bytes32 private constant WORMHOLE_SIBLINGS_SLOT =
bytes32(uint256(keccak256("whEndpoint.siblings")) - 1);

bytes32 public constant WORMHOLE_RELAYING_ENABLED_CHAINS_SLOT =
bytes32 private constant WORMHOLE_RELAYING_ENABLED_CHAINS_SLOT =
bytes32(uint256(keccak256("whEndpoint.relayingEnabledChains")) - 1);

bytes32 public constant SPECIAL_RELAYING_ENABLED_CHAINS_SLOT =
bytes32 private constant SPECIAL_RELAYING_ENABLED_CHAINS_SLOT =
bytes32(uint256(keccak256("whEndpoint.specialRelayingEnabledChains")) - 1);

bytes32 public constant WORMHOLE_EVM_CHAIN_IDS =
bytes32 private constant WORMHOLE_EVM_CHAIN_IDS =
bytes32(uint256(keccak256("whEndpoint.evmChainIds")) - 1);

/// =============== GETTERS/SETTERS ========================================
Expand Down
4 changes: 0 additions & 4 deletions evm/src/interfaces/IRateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ interface IRateLimiter {
address recipient;
}

function getOutboundLimitParams() external view returns (RateLimitParams memory);

function getCurrentOutboundCapacity() external view returns (uint256);

function getOutboundQueuedTransfer(uint64 queueSequence)
external
view
returns (OutboundQueuedTransfer memory);

function getInboundLimitParams(uint16 chainId) external view returns (RateLimitParams memory);

function getCurrentInboundCapacity(uint16 chainId) external view returns (uint256);

function getInboundQueuedTransfer(bytes32 digest)
Expand Down
10 changes: 10 additions & 0 deletions evm/src/libraries/EndpointHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ function isFork(uint256 evmChainId) view returns (bool) {
function min(uint256 a, uint256 b) pure returns (uint256) {
return a < b ? a : b;
}

// @dev Count the number of set bits in a uint64
function countSetBits(uint64 x) pure returns (uint8 count) {
while (x != 0) {
x &= x - 1;
count++;
}

return count;
}
RahulMaganti47 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions evm/src/libraries/Implementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ abstract contract Implementation is Initializable, ERC1967Upgrade {
bool value;
}

bytes32 public constant MIGRATING_SLOT = bytes32(uint256(keccak256("ntt.migrating")) - 1);
bytes32 private constant MIGRATING_SLOT = bytes32(uint256(keccak256("ntt.migrating")) - 1);

bytes32 public constant MIGRATES_IMMUTABLES_SLOT =
bytes32 private constant MIGRATES_IMMUTABLES_SLOT =
bytes32(uint256(keccak256("ntt.migratesImmutables")) - 1);

function _getMigratingStorage() private pure returns (_Migrating storage $) {
Expand Down
12 changes: 0 additions & 12 deletions evm/src/libraries/PausableOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,4 @@ abstract contract PausableOwnable is PausableUpgradeable, OwnableUpgradeable {
$._pauser = newPauser;
emit PauserTransferred(oldPauser, newPauser);
}

/**
* @dev Leaves the contract without a Pauser
*/
function renouncePauser() public virtual onlyOwnerOrPauser {
// NOTE: Cannot renounce the pauser capability when the contract is in the `PAUSED` state
// the contract can never be `UNPAUSED`
if (isPaused()) {
revert CannotRenounceWhilePaused(pauser());
}
transferPauserCapability(address(0));
}
}
4 changes: 2 additions & 2 deletions evm/src/libraries/PausableUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ abstract contract PausableUpgradeable is Initializable {
event Paused(bool paused);
event NotPaused(bool notPaused);

bytes32 public constant PAUSE_SLOT = bytes32(uint256(keccak256("Pause.pauseFlag")) - 1);
bytes32 public constant PAUSER_ROLE_SLOT = bytes32(uint256(keccak256("Pause.pauseRole")) - 1);
bytes32 private constant PAUSE_SLOT = bytes32(uint256(keccak256("Pause.pauseFlag")) - 1);
bytes32 private constant PAUSER_ROLE_SLOT = bytes32(uint256(keccak256("Pause.pauseRole")) - 1);

function _getPauserStorage() internal pure returns (PauserStorage storage $) {
uint256 slot = uint256(PAUSER_ROLE_SLOT);
Expand Down
33 changes: 19 additions & 14 deletions evm/src/libraries/RateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {

/// =============== STORAGE ===============================================

bytes32 public constant OUTBOUND_LIMIT_PARAMS_SLOT =
bytes32 private constant OUTBOUND_LIMIT_PARAMS_SLOT =
bytes32(uint256(keccak256("ntt.outboundLimitParams")) - 1);

bytes32 public constant OUTBOUND_QUEUE_SLOT =
bytes32 private constant OUTBOUND_QUEUE_SLOT =
bytes32(uint256(keccak256("ntt.outboundQueue")) - 1);

bytes32 public constant INBOUND_LIMIT_PARAMS_SLOT =
bytes32 private constant INBOUND_LIMIT_PARAMS_SLOT =
bytes32(uint256(keccak256("ntt.inboundLimitParams")) - 1);

bytes32 public constant INBOUND_QUEUE_SLOT = bytes32(uint256(keccak256("ntt.inboundQueue")) - 1);
bytes32 private constant INBOUND_QUEUE_SLOT =
bytes32(uint256(keccak256("ntt.inboundQueue")) - 1);

function _getOutboundLimitParamsStorage() internal pure returns (RateLimitParams storage $) {
uint256 slot = uint256(OUTBOUND_LIMIT_PARAMS_SLOT);
Expand Down Expand Up @@ -90,12 +91,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
_setLimit(limit, _getOutboundLimitParamsStorage());
}

function getOutboundLimitParams() public pure returns (RateLimitParams memory) {
function _getOutboundLimitParams() internal pure returns (RateLimitParams memory) {
return _getOutboundLimitParamsStorage();
}

function getCurrentOutboundCapacity() public view returns (uint256) {
NormalizedAmount memory normalizedCapacity = _getCurrentCapacity(getOutboundLimitParams());
NormalizedAmount memory normalizedCapacity = _getCurrentCapacity(_getOutboundLimitParams());
uint8 decimals = _tokenDecimals();
return normalizedCapacity.denormalize(decimals);
}
Expand All @@ -112,13 +113,17 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
_setLimit(limit, _getInboundLimitParamsStorage()[chainId_]);
}

function getInboundLimitParams(uint16 chainId_) public view returns (RateLimitParams memory) {
function _getInboundLimitParams(uint16 chainId_)
internal
view
returns (RateLimitParams memory)
{
return _getInboundLimitParamsStorage()[chainId_];
}

function getCurrentInboundCapacity(uint16 chainId_) public view returns (uint256) {
NormalizedAmount memory normalizedCapacity =
_getCurrentCapacity(getInboundLimitParams(chainId_));
_getCurrentCapacity(_getInboundLimitParams(chainId_));
uint8 decimals = _tokenDecimals();
return normalizedCapacity.denormalize(decimals);
}
Expand Down Expand Up @@ -193,28 +198,28 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {

function _consumeOutboundAmount(NormalizedAmount memory amount) internal {
_consumeRateLimitAmount(
amount, _getCurrentCapacity(getOutboundLimitParams()), _getOutboundLimitParamsStorage()
amount, _getCurrentCapacity(_getOutboundLimitParams()), _getOutboundLimitParamsStorage()
);
}

function _backfillOutboundAmount(NormalizedAmount memory amount) internal {
_backfillRateLimitAmount(
amount, _getCurrentCapacity(getOutboundLimitParams()), _getOutboundLimitParamsStorage()
amount, _getCurrentCapacity(_getOutboundLimitParams()), _getOutboundLimitParamsStorage()
);
}

function _consumeInboundAmount(NormalizedAmount memory amount, uint16 chainId_) internal {
_consumeRateLimitAmount(
amount,
_getCurrentCapacity(getInboundLimitParams(chainId_)),
_getCurrentCapacity(_getInboundLimitParams(chainId_)),
_getInboundLimitParamsStorage()[chainId_]
);
}

function _backfillInboundAmount(NormalizedAmount memory amount, uint16 chainId_) internal {
_backfillRateLimitAmount(
amount,
_getCurrentCapacity(getInboundLimitParams(chainId_)),
_getCurrentCapacity(_getInboundLimitParams(chainId_)),
_getInboundLimitParamsStorage()[chainId_]
);
}
Expand Down Expand Up @@ -244,14 +249,14 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
view
returns (bool)
{
return _isAmountRateLimited(_getCurrentCapacity(getOutboundLimitParams()), amount);
return _isAmountRateLimited(_getCurrentCapacity(_getOutboundLimitParams()), amount);
}

function _isInboundAmountRateLimited(
NormalizedAmount memory amount,
uint16 chainId_
) internal view returns (bool) {
return _isAmountRateLimited(_getCurrentCapacity(getInboundLimitParams(chainId_)), amount);
return _isAmountRateLimited(_getCurrentCapacity(_getInboundLimitParams(chainId_)), amount);
}

function _isAmountRateLimited(
Expand Down
11 changes: 0 additions & 11 deletions evm/src/libraries/external/OwnableUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable, IOwna
}
}

/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
Expand Down
Loading
Loading