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: guarantee that rate limits are initialised #224

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions evm/src/NttManager/NttManagerState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ abstract contract NttManagerState is
}
__PausedOwnable_init(msg.sender, msg.sender);
__ReentrancyGuard_init();
_setOutboundLimit(TrimmedAmountLib.max(tokenDecimals_));
}

function _initialize() internal virtual override {
Expand Down Expand Up @@ -272,6 +273,8 @@ abstract contract NttManagerState is
_getPeersStorage()[peerChainId].peerAddress = peerContract;
_getPeersStorage()[peerChainId].tokenDecimals = decimals;

_setInboundLimit(TrimmedAmountLib.max(tokenDecimals_), peerChainId);

emit PeerUpdated(
peerChainId, oldPeer.peerAddress, oldPeer.tokenDecimals, peerContract, decimals
);
Expand Down
11 changes: 7 additions & 4 deletions evm/src/libraries/RateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
RateLimitParams storage rateLimitParams
) internal {
TrimmedAmount memory oldLimit = rateLimitParams.limit;
TrimmedAmount memory currentCapacity = _getCurrentCapacity(rateLimitParams);
if (oldLimit.isZero()) {
rateLimitParams.currentCapacity = limit;
} else {
TrimmedAmount memory currentCapacity = _getCurrentCapacity(rateLimitParams);
rateLimitParams.currentCapacity =
_calculateNewCurrentCapacity(limit, oldLimit, currentCapacity);
}
rateLimitParams.limit = limit;

rateLimitParams.currentCapacity =
_calculateNewCurrentCapacity(limit, oldLimit, currentCapacity);

rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
}

Expand Down
43 changes: 5 additions & 38 deletions evm/src/libraries/TrimmedAmount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ library TrimmedAmountLib {
}

function gt(TrimmedAmount memory a, TrimmedAmount memory b) internal pure returns (bool) {
// on initialization
if (isZero(b) && !isZero(a)) {
return true;
}
if (isZero(a) && !isZero(b)) {
return false;
}

if (a.decimals != b.decimals) {
revert NumberOfDecimalsNotEqual(a.decimals, b.decimals);
}
Expand All @@ -50,14 +42,6 @@ library TrimmedAmountLib {
}

function lt(TrimmedAmount memory a, TrimmedAmount memory b) internal pure returns (bool) {
// on initialization
if (isZero(b) && !isZero(a)) {
return false;
}
if (isZero(a) && !isZero(b)) {
return true;
}

if (a.decimals != b.decimals) {
revert NumberOfDecimalsNotEqual(a.decimals, b.decimals);
}
Expand All @@ -74,11 +58,6 @@ library TrimmedAmountLib {
TrimmedAmount memory a,
TrimmedAmount memory b
) internal pure returns (TrimmedAmount memory) {
// on initialization
if (isZero(b)) {
return a;
}

if (a.decimals != b.decimals) {
revert NumberOfDecimalsNotEqual(a.decimals, b.decimals);
}
Expand All @@ -90,15 +69,6 @@ library TrimmedAmountLib {
TrimmedAmount memory a,
TrimmedAmount memory b
) internal pure returns (TrimmedAmount memory) {
// on initialization
if (isZero(a)) {
return b;
}

if (isZero(b)) {
return a;
}

if (a.decimals != b.decimals) {
revert NumberOfDecimalsNotEqual(a.decimals, b.decimals);
}
Expand Down Expand Up @@ -134,14 +104,6 @@ library TrimmedAmountLib {
TrimmedAmount memory a,
TrimmedAmount memory b
) public pure returns (TrimmedAmount memory) {
// on initialization
if (isZero(a) && !isZero(b)) {
return a;
}
if (isZero(b) && !isZero(a)) {
return b;
}

if (a.decimals != b.decimals) {
revert NumberOfDecimalsNotEqual(a.decimals, b.decimals);
}
Expand Down Expand Up @@ -176,6 +138,11 @@ library TrimmedAmountLib {
);
}

function max(uint8 decimals) internal pure returns (TrimmedAmount memory) {
uint8 actualDecimals = minUint8(TRIMMED_DECIMALS, decimals);
return TrimmedAmount(type(uint64).max, actualDecimals);
}

/// @dev trim the amount to target decimals.
/// The actual resulting decimals is the minimum of TRIMMED_DECIMALS,
/// fromDecimals, and toDecimals. This ensures that no dust is
Expand Down
Loading