@layerzerolabs/utils-upgradeable-evm-contracts 4.0.2 → 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ import { RateLimiterUtils } from "./libs/RateLimiterUtils.sol";
9
9
  /**
10
10
  * @title RateLimiterBaseUpgradeable
11
11
  * @author LayerZero Labs (tinom.eth)
12
- * @custom:version 1.0.0
12
+ * @custom:version 1.1.0
13
13
  * @notice Abstract contract that provides toggleable rate limiting functionality for OApps.
14
14
  * Token bucket algorithm with linear decay.
15
15
  * @dev No public management functions are exposed by this contract, wrappers should be used with access control.
@@ -18,7 +18,7 @@ import { RateLimiterUtils } from "./libs/RateLimiterUtils.sol";
18
18
  * @dev Net rate limits offset outflow usage with inflows and vice versa, gross rate limits do not.
19
19
  * @dev Amounts are stored as `uint96`, any amounts larger than `type(uint96).max` need to be downscaled via the
20
20
  * `SCALE_DECIMALS` constructor parameter.
21
- * @dev When using global state, ID-specific configs are ignored, and the default config is always used.
21
+ * @dev When using global state, every ID shares the state and config of `GLOBAL_STATE_ID`.
22
22
  * @dev If `SCALE_DECIMALS` are applied, scaling rounding can favour the user in certain configurations. It is the
23
23
  * responsibility of the app to ensure the economic cost of running the rate-limited operation is greater than the
24
24
  * economic benefit of bypassing a unit of rate limit.
@@ -110,8 +110,8 @@ import { RateLimiterUtils } from "./libs/RateLimiterUtils.sol";
110
110
  * [ --------------- Extended 60 Second Window --------------- ]
111
111
  */
112
112
  abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
113
- /// @notice ID of the default rate limit configuration.
114
- uint256 public constant DEFAULT_ID = 0;
113
+ /// @notice ID of the rate limit state and configuration shared by every ID when using global state.
114
+ uint256 public constant GLOBAL_STATE_ID = 0;
115
115
 
116
116
  /// @notice Number of decimals to scale the rate limit amounts, usually 0.
117
117
  uint8 public immutable SCALE_DECIMALS;
@@ -164,25 +164,7 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
164
164
  * @param _useGlobalState Whether to use global rules for the rate limiter, instead of per-ID rules
165
165
  */
166
166
  function __RateLimiterBase_init_unchained(bool _useGlobalState) internal onlyInitializing {
167
- RateLimiterStorage storage $ = _getRateLimiterStorage();
168
- $.useGlobalStateFlag = _useGlobalState;
169
-
170
- /// @dev Default config is closed by default.
171
- $.rateLimits[DEFAULT_ID].configBitmap = RateLimiterUtils.encodeConfigBitmap(false, true, true, true, false);
172
- emit RateLimitConfigUpdated(
173
- DEFAULT_ID,
174
- RateLimitConfig({
175
- overrideDefaultConfig: false,
176
- outboundEnabled: true,
177
- inboundEnabled: true,
178
- netAccountingEnabled: true,
179
- addressExemptionEnabled: false,
180
- outboundLimit: 0,
181
- inboundLimit: 0,
182
- outboundWindow: 0,
183
- inboundWindow: 0
184
- })
185
- );
167
+ _getRateLimiterStorage().useGlobalStateFlag = _useGlobalState;
186
168
  }
187
169
 
188
170
  // ============ Public Getters ============
@@ -356,7 +338,7 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
356
338
  }
357
339
 
358
340
  /**
359
- * @notice Gets the rate limit state and configuration for a given ID, falling back to defaults if necessary.
341
+ * @notice Gets the rate limit state and configuration for a given ID.
360
342
  * @param _id ID of the rate limit
361
343
  * @return rateLimitState Rate limit state
362
344
  * @return rateLimitCache Rate limit configuration
@@ -382,59 +364,17 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
382
364
  {
383
365
  RateLimiterStorage storage $ = _getRateLimiterStorage();
384
366
 
385
- uint256 stateId;
386
- uint256 configId;
367
+ rateLimitState = $.rateLimits[$.useGlobalStateFlag ? GLOBAL_STATE_ID : _id];
368
+ rateLimitCache = rateLimitState;
387
369
 
388
- if ($.useGlobalStateFlag) {
389
- /// @dev If using global state, ID-specific config is ignored.
390
- stateId = DEFAULT_ID;
391
- configId = DEFAULT_ID;
392
- } else {
393
- stateId = _id;
394
- configId = RateLimiterUtils.decodeOverrideDefaultConfig($.rateLimits[_id].configBitmap) ? _id : DEFAULT_ID;
370
+ if (!RateLimiterUtils.decodeActive(rateLimitCache.configBitmap)) {
371
+ return (rateLimitState, rateLimitCache, true, true, false, false);
395
372
  }
396
373
 
397
- rateLimitState = $.rateLimits[stateId];
398
- rateLimitCache = _populateRateLimitCache(stateId, configId);
399
-
400
374
  (outboundEnabled, inboundEnabled, netAccountingEnabled, addressExemptionEnabled) = RateLimiterUtils
401
375
  .decodeConfigBitmapFlags(rateLimitCache.configBitmap);
402
376
  }
403
377
 
404
- /**
405
- * @notice Populates the rate limit cache for a given state and configuration ID.
406
- * @dev Avoids stack too deep error in `_getRateLimitStateAndConfig`.
407
- * @param _stateId State ID
408
- * @param _configId Configuration ID
409
- * @return rateLimitCache Rate limit cache
410
- */
411
- function _populateRateLimitCache(
412
- uint256 _stateId,
413
- uint256 _configId
414
- ) internal view virtual returns (RateLimit memory rateLimitCache) {
415
- RateLimiterStorage storage $ = _getRateLimiterStorage();
416
-
417
- (rateLimitCache.outboundUsage, rateLimitCache.inboundUsage, rateLimitCache.lastUpdated) = (
418
- $.rateLimits[_stateId].outboundUsage,
419
- $.rateLimits[_stateId].inboundUsage,
420
- $.rateLimits[_stateId].lastUpdated
421
- );
422
-
423
- (
424
- rateLimitCache.configBitmap,
425
- rateLimitCache.outboundLimit,
426
- rateLimitCache.inboundLimit,
427
- rateLimitCache.outboundWindow,
428
- rateLimitCache.inboundWindow
429
- ) = (
430
- $.rateLimits[_configId].configBitmap,
431
- $.rateLimits[_configId].outboundLimit,
432
- $.rateLimits[_configId].inboundLimit,
433
- $.rateLimits[_configId].outboundWindow,
434
- $.rateLimits[_configId].inboundWindow
435
- );
436
- }
437
-
438
378
  /**
439
379
  * @notice Calculates decayed usages and remaining capacities for a rate limit.
440
380
  * @param _rateLimit Rate limit state to calculate usages for
@@ -468,11 +408,16 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
468
408
  _rateLimit.inboundLimit,
469
409
  _rateLimit.inboundWindow
470
410
  );
411
+
412
+ if (!RateLimiterUtils.decodeActive(_rateLimit.configBitmap)) {
413
+ (outboundAvailableAmount, inboundAvailableAmount) = (0, 0);
414
+ }
471
415
  }
472
416
 
473
417
  /**
474
418
  * @notice Calculates decayed usage and remaining capacity for a rate limit direction.
475
419
  * @dev Treats 0-windows as 1-second windows.
420
+ * @dev Treats `type(uint32).max` windows as windows without decay.
476
421
  * @dev Decay can be stalled due to precision loss if `_limit` is not significantly larger than `_window`.
477
422
  * @param _lastUpdated Last updated timestamp
478
423
  * @param _amountInFlight Amount in flight
@@ -489,7 +434,9 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
489
434
  ) internal view virtual returns (uint256 currentUsage, uint256 availableAmount) {
490
435
  uint256 timeSinceLastUpdate = block.timestamp - _lastUpdated;
491
436
  unchecked {
492
- uint256 decay = (_limit * timeSinceLastUpdate) / (_window > 0 ? _window : 1);
437
+ uint256 decay = _window == type(uint32).max
438
+ ? 0
439
+ : (_limit * timeSinceLastUpdate) / (_window > 0 ? _window : 1);
493
440
  currentUsage = Math.saturatingSub(_amountInFlight, decay);
494
441
  availableAmount = Math.saturatingSub(_limit, currentUsage);
495
442
  }
@@ -549,7 +496,7 @@ abstract contract RateLimiterBaseUpgradeable is Initializable, IRateLimiter {
549
496
  RateLimit storage rateLimit = $.rateLimits[param.id];
550
497
 
551
498
  rateLimit.configBitmap = RateLimiterUtils.encodeConfigBitmap(
552
- param.config.overrideDefaultConfig,
499
+ param.config.active,
553
500
  param.config.outboundEnabled,
554
501
  param.config.inboundEnabled,
555
502
  param.config.netAccountingEnabled,
@@ -8,7 +8,7 @@ import { RateLimiterBaseUpgradeable } from "./RateLimiterBaseUpgradeable.sol";
8
8
  /**
9
9
  * @title RateLimiterRBACUpgradeable
10
10
  * @author LayerZero Labs (tinom.eth)
11
- * @custom:version 1.0.0
11
+ * @custom:version 1.1.0
12
12
  * @notice Abstract upgradeable contract that provides toggleable rate limiting functionality for OApps.
13
13
  * Token bucket algorithm with linear decay.
14
14
  * @dev Exposes public management functions through `AccessControl2StepUpgradeable`.
@@ -16,7 +16,7 @@ import { RateLimiterBaseUpgradeable } from "./RateLimiterBaseUpgradeable.sol";
16
16
  * @dev Net rate limits offset outflow usage with inflows and vice versa, gross rate limits do not.
17
17
  * @dev Amounts are stored as `uint96`, any amounts larger than `type(uint96).max` need to be downscaled via the
18
18
  * `SCALE_DECIMALS` constructor parameter.
19
- * @dev When using global state, ID-specific configs are ignored, and the default config is always used.
19
+ * @dev When using global state, every ID shares the state and config of `GLOBAL_STATE_ID`.
20
20
  */
21
21
  abstract contract RateLimiterRBACUpgradeable is
22
22
  IRateLimiter,
@@ -4,31 +4,31 @@ pragma solidity ^0.8.22;
4
4
  /**
5
5
  * @title RateLimiterUtils
6
6
  * @author LayerZero Labs (tinom.eth)
7
- * @custom:version 1.0.0
7
+ * @custom:version 1.1.0
8
8
  * @notice Library for utility functions for the `RateLimiter` contract.
9
9
  */
10
10
  library RateLimiterUtils {
11
11
  uint24 private constant BIT_MASK = 1;
12
12
 
13
- uint24 private constant OVERRIDE_DEFAULT_CONFIG_BIT = 0;
13
+ uint24 private constant ACTIVE_BIT = 0;
14
14
  uint24 private constant OUTBOUND_ENABLED_BIT = 1;
15
15
  uint24 private constant INBOUND_ENABLED_BIT = 2;
16
16
  uint24 private constant NET_ACCOUNTING_BIT = 3;
17
17
  uint24 private constant ADDRESS_EXEMPTION_BIT = 4;
18
18
 
19
19
  /**
20
- * @notice Decodes the override default configuration flag from the configuration bitmap.
20
+ * @notice Decodes the active flag from the configuration bitmap.
21
21
  * @param _config Bitmap of the rate limit configuration
22
- * @return overrideDefaultConfig Whether to override the default configuration
22
+ * @return active Whether the configuration is active
23
23
  */
24
- function decodeOverrideDefaultConfig(uint24 _config) internal pure returns (bool overrideDefaultConfig) {
24
+ function decodeActive(uint24 _config) internal pure returns (bool active) {
25
25
  assembly {
26
- overrideDefaultConfig := and(shr(OVERRIDE_DEFAULT_CONFIG_BIT, _config), BIT_MASK)
26
+ active := and(shr(ACTIVE_BIT, _config), BIT_MASK)
27
27
  }
28
28
  }
29
29
 
30
30
  /**
31
- * @notice Decodes the configuration bitmap into its components, excluding the override default configuration flag.
31
+ * @notice Decodes the configuration bitmap into its components, excluding the active flag.
32
32
  * @param _config Bitmap of the rate limit configuration
33
33
  * @return outboundEnabled Whether outbound is enabled
34
34
  * @return inboundEnabled Whether inbound is enabled
@@ -52,7 +52,7 @@ library RateLimiterUtils {
52
52
 
53
53
  /**
54
54
  * @notice Encodes the configuration bitmap into its components.
55
- * @param _overrideDefaultConfig Whether to override the default configuration
55
+ * @param _active Whether the configuration is active
56
56
  * @param _outboundEnabled Whether outbound is enabled
57
57
  * @param _inboundEnabled Whether inbound is enabled
58
58
  * @param _netAccountingEnabled Whether net accounting is enabled
@@ -60,7 +60,7 @@ library RateLimiterUtils {
60
60
  * @return config Configuration bitmap
61
61
  */
62
62
  function encodeConfigBitmap(
63
- bool _overrideDefaultConfig,
63
+ bool _active,
64
64
  bool _outboundEnabled,
65
65
  bool _inboundEnabled,
66
66
  bool _netAccountingEnabled,
@@ -69,10 +69,7 @@ library RateLimiterUtils {
69
69
  assembly {
70
70
  config := or(
71
71
  or(
72
- or(
73
- shl(OVERRIDE_DEFAULT_CONFIG_BIT, _overrideDefaultConfig),
74
- shl(OUTBOUND_ENABLED_BIT, _outboundEnabled)
75
- ),
72
+ or(shl(ACTIVE_BIT, _active), shl(OUTBOUND_ENABLED_BIT, _outboundEnabled)),
76
73
  or(shl(INBOUND_ENABLED_BIT, _inboundEnabled), shl(NET_ACCOUNTING_BIT, _netAccountingEnabled))
77
74
  ),
78
75
  shl(ADDRESS_EXEMPTION_BIT, _addressExemptionEnabled)
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@layerzerolabs/utils-upgradeable-evm-contracts",
3
- "version": "4.0.2",
3
+ "version": "4.0.4",
4
4
  "private": false,
5
5
  "description": "LayerZero Labs upgradeable utility EVM contracts",
6
6
  "license": "MIT",
7
7
  "devDependencies": {
8
- "@layerzerolabs/solhint-configuration": "4.0.2",
8
+ "@layerzerolabs/solhint-configuration": "4.0.4",
9
9
  "@layerzerolabs/test-devtools-evm-foundry": "8.0.1",
10
10
  "@layerzerolabs/toolbox-foundry": "^0.1.13",
11
- "@layerzerolabs/utils-evm-contracts": "4.0.2",
12
- "@layerzerolabs/vm-tooling-evm": "4.0.2",
11
+ "@layerzerolabs/utils-evm-contracts": "4.0.4",
12
+ "@layerzerolabs/vm-tooling-evm": "4.0.4",
13
13
  "@openzeppelin/contracts": "^5.0.2",
14
14
  "@openzeppelin/contracts-upgradeable": "^5.0.2",
15
15
  "solhint": "^6.0.1"
@@ -45,17 +45,6 @@ contract RateLimiterBaseUpgradeableHarness is RateLimiterBaseUpgradeable {
45
45
  _inflow(_id, _to, _amount);
46
46
  }
47
47
 
48
- /// @dev Helper to bypass external visibility for permutation loop (try/catch needs external).
49
- function runPermutationWrapper(
50
- bool useDefaultConfig,
51
- bool useGlobalState,
52
- bool outboundEnabled,
53
- bool inboundEnabled,
54
- bool netAccounting,
55
- bool isExempt,
56
- bool isOutflow
57
- ) external {}
58
-
59
48
  function getRateLimitUsage(
60
49
  uint40 _lastUpdated,
61
50
  uint96 _amountInFlight,
@@ -74,23 +63,17 @@ contract RateLimiterBaseUpgradeableTest is Test {
74
63
  RateLimiterBaseUpgradeableHarness rateLimiter;
75
64
  /// @dev Separate instance for global state tests to avoid warm storage overlap or conflict.
76
65
  RateLimiterBaseUpgradeableHarness rateLimiterGlobal;
77
- /// @dev Separate instances for conflicting default config permutations.
78
- RateLimiterBaseUpgradeableHarness rateLimiterDefaultNet;
79
- RateLimiterBaseUpgradeableHarness rateLimiterDefaultDisabled;
80
66
 
81
67
  uint256 constant ID_1 = uint256(keccak256("ID_1"));
82
68
  uint256 constant ID_2 = uint256(keccak256("ID_2"));
83
- uint256 constant DEFAULT_ID = 0;
69
+ uint256 constant GLOBAL_STATE_ID = 0;
70
+ uint32 constant NO_DECAY_WINDOW = type(uint32).max;
71
+ uint256 constant YEAR = 365 days;
72
+ uint96 constant HUB_LEDGER_LIMIT = type(uint96).max;
84
73
 
85
74
  address alice = makeAddr("alice");
86
75
  address bob = makeAddr("bob");
87
76
 
88
- uint256 constant ID_SNAP_DEFAULT_OUTBOUND_GROSS = uint256(keccak256("ID_SNAP_DEFAULT_OUTBOUND_GROSS"));
89
- uint256 constant ID_SNAP_DEFAULT_INBOUND_GROSS = uint256(keccak256("ID_SNAP_DEFAULT_INBOUND_GROSS"));
90
- uint256 constant ID_SNAP_DEFAULT_BOTH_NET_OUT = uint256(keccak256("ID_SNAP_DEFAULT_BOTH_NET_OUT"));
91
- uint256 constant ID_SNAP_DEFAULT_BOTH_NET_IN = uint256(keccak256("ID_SNAP_DEFAULT_BOTH_NET_IN"));
92
- uint256 constant ID_SNAP_DEFAULT_DISABLED = uint256(keccak256("ID_SNAP_DEFAULT_DISABLED"));
93
-
94
77
  uint256 constant ID_SNAP_EXPLICIT_OUTBOUND_GROSS = uint256(keccak256("ID_SNAP_EXPLICIT_OUTBOUND_GROSS"));
95
78
  uint256 constant ID_SNAP_EXPLICIT_INBOUND_GROSS = uint256(keccak256("ID_SNAP_EXPLICIT_INBOUND_GROSS"));
96
79
  uint256 constant ID_SNAP_EXPLICIT_BOTH_NET_OUT = uint256(keccak256("ID_SNAP_EXPLICIT_BOTH_NET_OUT"));
@@ -99,24 +82,9 @@ contract RateLimiterBaseUpgradeableTest is Test {
99
82
  uint256 constant ID_SNAP_GLOBAL_OUTBOUND_GROSS = uint256(keccak256("ID_SNAP_GLOBAL_OUTBOUND_GROSS"));
100
83
  uint256 constant ID_SNAP_DISABLED = uint256(keccak256("ID_SNAP_DISABLED"));
101
84
 
102
- struct PermutationParams {
103
- /// @dev If true, `id` has `override=false`, config is on Default ID (only relevant if `useGlobalState=false`).
104
- bool useDefaultConfig;
105
- /// @dev If true, enable Global State flag.
106
- bool useGlobalState;
107
- bool outboundEnabled;
108
- bool inboundEnabled;
109
- bool netAccounting;
110
- bool isExempt;
111
- /// @dev True = Outflow, False = Inflow.
112
- bool isOutflow;
113
- }
114
-
115
85
  function setUp() public virtual {
116
86
  rateLimiter = _deployRateLimiter(0, false);
117
87
  rateLimiterGlobal = _deployRateLimiter(0, true);
118
- rateLimiterDefaultNet = _deployRateLimiter(0, false);
119
- rateLimiterDefaultDisabled = _deployRateLimiter(0, false);
120
88
 
121
89
  _setUpConfigs();
122
90
  }
@@ -142,33 +110,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
142
110
  uint96 limit = 1000;
143
111
  uint32 window = 1000;
144
112
 
145
- IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](2);
146
- configs[0] = _createConfig(DEFAULT_ID, false, true, true, false, false, limit, limit, window, window);
147
- configs[1] = _createConfig(ID_SNAP_DEFAULT_OUTBOUND_GROSS, false, false, false, false, false, 0, 0, 0, 0);
148
- rateLimiter.setRateLimitConfigs(configs);
149
-
150
- configs = new IRateLimiter.SetRateLimitConfigParam[](1);
151
- configs[0] = _createConfig(ID_SNAP_DEFAULT_INBOUND_GROSS, false, false, false, false, false, 0, 0, 0, 0);
152
- rateLimiter.setRateLimitConfigs(configs);
153
-
154
- configs = new IRateLimiter.SetRateLimitConfigParam[](3);
155
- configs[0] = _createConfig(DEFAULT_ID, false, true, true, true, false, limit, limit, window, window);
156
- configs[1] = _createConfig(ID_SNAP_DEFAULT_BOTH_NET_OUT, false, false, false, false, false, 0, 0, 0, 0);
157
- configs[2] = _createConfig(ID_SNAP_DEFAULT_BOTH_NET_IN, false, false, false, false, false, 0, 0, 0, 0);
158
- rateLimiterDefaultNet.setRateLimitConfigs(configs);
159
-
160
- IRateLimiter.SetRateLimitStateParam[] memory netStates = new IRateLimiter.SetRateLimitStateParam[](1);
161
- netStates[0] = _createState(ID_SNAP_DEFAULT_BOTH_NET_OUT, 0, 50, uint40(block.timestamp));
162
- rateLimiterDefaultNet.setRateLimitStates(netStates);
163
- netStates[0] = _createState(ID_SNAP_DEFAULT_BOTH_NET_IN, 50, 0, uint40(block.timestamp));
164
- rateLimiterDefaultNet.setRateLimitStates(netStates);
165
-
166
- configs = new IRateLimiter.SetRateLimitConfigParam[](2);
167
- configs[0] = _createConfig(DEFAULT_ID, false, false, false, false, false, limit, limit, window, window);
168
- configs[1] = _createConfig(ID_SNAP_DEFAULT_DISABLED, false, false, false, false, false, 0, 0, 0, 0);
169
- rateLimiterDefaultDisabled.setRateLimitConfigs(configs);
170
-
171
- configs = new IRateLimiter.SetRateLimitConfigParam[](1);
113
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
172
114
  configs[0] = _createConfig(
173
115
  ID_SNAP_EXPLICIT_OUTBOUND_GROSS,
174
116
  true,
@@ -248,9 +190,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
248
190
  exemptions[0] = IRateLimiter.SetRateLimitAddressExemptionParam({ user: alice, isExempt: true });
249
191
  rateLimiter.setRateLimitAddressExemptions(exemptions);
250
192
 
251
- configs = new IRateLimiter.SetRateLimitConfigParam[](2);
252
- configs[0] = _createConfig(DEFAULT_ID, false, true, true, false, false, limit, limit, window, window);
253
- configs[1] = _createConfig(ID_SNAP_GLOBAL_OUTBOUND_GROSS, false, false, false, false, false, 0, 0, 0, 0);
193
+ configs[0] = _createConfig(GLOBAL_STATE_ID, true, true, true, false, false, limit, limit, window, window);
254
194
  rateLimiterGlobal.setRateLimitConfigs(configs);
255
195
 
256
196
  configs[0] = _createConfig(ID_SNAP_DISABLED, true, false, true, false, false, limit, limit, window, window);
@@ -259,7 +199,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
259
199
 
260
200
  function _createConfig(
261
201
  uint256 _id,
262
- bool _overrideDefaultConfig,
202
+ bool _active,
263
203
  bool _outboundEnabled,
264
204
  bool _inboundEnabled,
265
205
  bool _netAccountingEnabled,
@@ -273,7 +213,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
273
213
  IRateLimiter.SetRateLimitConfigParam({
274
214
  id: _id,
275
215
  config: IRateLimiter.RateLimitConfig({
276
- overrideDefaultConfig: _overrideDefaultConfig,
216
+ active: _active,
277
217
  outboundEnabled: _outboundEnabled,
278
218
  inboundEnabled: _inboundEnabled,
279
219
  netAccountingEnabled: _netAccountingEnabled,
@@ -303,6 +243,40 @@ contract RateLimiterBaseUpgradeableTest is Test {
303
243
  });
304
244
  }
305
245
 
246
+ function _setUpHubLedger(
247
+ RateLimiterBaseUpgradeableHarness _limiter,
248
+ uint256 _id,
249
+ bool _outboundEnabled,
250
+ uint96 _outboundLimit,
251
+ uint32 _outboundWindow
252
+ ) internal {
253
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
254
+ configs[0] = _createConfig(
255
+ _id,
256
+ true,
257
+ _outboundEnabled,
258
+ true,
259
+ true,
260
+ false,
261
+ _outboundLimit,
262
+ HUB_LEDGER_LIMIT,
263
+ _outboundWindow,
264
+ NO_DECAY_WINDOW
265
+ );
266
+ _limiter.setRateLimitConfigs(configs);
267
+
268
+ IRateLimiter.SetRateLimitStateParam[] memory states = new IRateLimiter.SetRateLimitStateParam[](1);
269
+ states[0] = _createState(_id, 0, HUB_LEDGER_LIMIT, uint40(block.timestamp));
270
+ _limiter.setRateLimitStates(states);
271
+ }
272
+
273
+ function _inboundAvailable(
274
+ RateLimiterBaseUpgradeableHarness _limiter,
275
+ uint256 _id
276
+ ) internal view returns (uint256 inAvail) {
277
+ (, , , inAvail) = _limiter.getRateLimitUsages(_id);
278
+ }
279
+
306
280
  // ============ Constructor / Initialization Tests ============
307
281
 
308
282
  function test_constructor_Revert_InvalidScaledDecimals() public {
@@ -375,42 +349,42 @@ contract RateLimiterBaseUpgradeableTest is Test {
375
349
 
376
350
  // ============ Global State Tests ============
377
351
 
378
- function test_globalState_UsesDefaultIdForState() public {
352
+ function test_globalState_UsesGlobalStateIdForState() public {
379
353
  RateLimiterBaseUpgradeableHarness globalLimiter = _deployRateLimiter(0, true);
380
354
 
381
355
  uint96 limit = 1000;
382
356
  uint32 window = 1000;
383
357
 
384
358
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
385
- configs[0] = _createConfig(DEFAULT_ID, false, true, true, true, false, limit, limit, window, window);
359
+ configs[0] = _createConfig(GLOBAL_STATE_ID, true, true, true, true, false, limit, limit, window, window);
386
360
  globalLimiter.setRateLimitConfigs(configs);
387
361
 
388
362
  globalLimiter.outflow(ID_1, alice, 100);
389
363
 
390
- IRateLimiter.RateLimit memory defaultState = globalLimiter.rateLimits(DEFAULT_ID);
364
+ IRateLimiter.RateLimit memory globalState = globalLimiter.rateLimits(GLOBAL_STATE_ID);
391
365
  IRateLimiter.RateLimit memory id1State = globalLimiter.rateLimits(ID_1);
392
366
 
393
- assertEq(defaultState.outboundUsage, 100);
367
+ assertEq(globalState.outboundUsage, 100);
394
368
  assertEq(id1State.outboundUsage, 0);
395
369
  }
396
370
 
397
371
  function test_globalState_IgnoresIdSpecificConfig() public {
398
372
  RateLimiterBaseUpgradeableHarness globalLimiter = _deployRateLimiter(0, true);
399
373
 
400
- uint96 defaultLimit = 1000;
374
+ uint96 globalLimit = 1000;
401
375
  uint96 idSpecificLimit = 500;
402
376
  uint32 window = 1000;
403
377
 
404
378
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](2);
405
379
  configs[0] = _createConfig(
406
- DEFAULT_ID,
407
- false,
380
+ GLOBAL_STATE_ID,
381
+ true,
408
382
  true,
409
383
  true,
410
384
  true,
411
385
  false,
412
- defaultLimit,
413
- defaultLimit,
386
+ globalLimit,
387
+ globalLimit,
414
388
  window,
415
389
  window
416
390
  );
@@ -430,7 +404,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
430
404
 
431
405
  (, uint256 outAvail, , ) = globalLimiter.getRateLimitUsages(ID_1);
432
406
 
433
- assertEq(outAvail, defaultLimit);
407
+ assertEq(outAvail, globalLimit);
434
408
  }
435
409
 
436
410
  function test_globalState_SharesStateAcrossIds() public {
@@ -440,7 +414,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
440
414
  uint32 window = 1000;
441
415
 
442
416
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
443
- configs[0] = _createConfig(DEFAULT_ID, false, true, true, true, false, limit, limit, window, window);
417
+ configs[0] = _createConfig(GLOBAL_STATE_ID, true, true, true, true, false, limit, limit, window, window);
444
418
  globalLimiter.setRateLimitConfigs(configs);
445
419
 
446
420
  globalLimiter.outflow(ID_1, alice, 100);
@@ -483,7 +457,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
483
457
 
484
458
  function test_setRateLimitConfigs_Single() public {
485
459
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
486
- configs[0] = _createConfig(ID_1, false, false, false, true, true, 100, 200, 300, 400);
460
+ configs[0] = _createConfig(ID_1, true, false, false, true, true, 100, 200, 300, 400);
487
461
 
488
462
  vm.expectEmit(false, false, false, true);
489
463
  emit IRateLimiter.RateLimitConfigUpdated(ID_1, configs[0].config);
@@ -495,14 +469,14 @@ contract RateLimiterBaseUpgradeableTest is Test {
495
469
  assertEq(rl.inboundLimit, 200);
496
470
  assertEq(rl.outboundWindow, 300);
497
471
  assertEq(rl.inboundWindow, 400);
498
- // NetAccounting (bit 3) = 8, AddressExemption (bit 4) = 16 => 24 total.
499
- assertEq(rl.configBitmap, 24);
472
+ // Active (bit 0) = 1, NetAccounting (bit 3) = 8, AddressExemption (bit 4) = 16 => 25 total.
473
+ assertEq(rl.configBitmap, 25);
500
474
  }
501
475
 
502
476
  function test_setRateLimitConfigs_Multiple() public {
503
477
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](2);
504
478
  configs[0] = _createConfig(ID_1, true, true, true, true, true, 10, 10, 10, 10);
505
- configs[1] = _createConfig(ID_2, false, false, false, false, false, 20, 20, 20, 20);
479
+ configs[1] = _createConfig(ID_2, true, false, false, false, false, 20, 20, 20, 20);
506
480
 
507
481
  rateLimiter.setRateLimitConfigs(configs);
508
482
 
@@ -627,6 +601,26 @@ contract RateLimiterBaseUpgradeableTest is Test {
627
601
  assertEq(rl2.lastUpdated, block.timestamp);
628
602
  }
629
603
 
604
+ function test_checkpointRateLimits_NoDecayWindow() public {
605
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
606
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, NO_DECAY_WINDOW, NO_DECAY_WINDOW);
607
+ rateLimiter.setRateLimitConfigs(configs);
608
+
609
+ rateLimiter.outflow(ID_1, alice, 600);
610
+ rateLimiter.inflow(ID_1, alice, 400);
611
+
612
+ vm.warp(block.timestamp + 100 * YEAR);
613
+
614
+ uint256[] memory ids = new uint256[](1);
615
+ ids[0] = ID_1;
616
+ rateLimiter.checkpointRateLimits(ids);
617
+
618
+ IRateLimiter.RateLimit memory rl = rateLimiter.rateLimits(ID_1);
619
+ assertEq(rl.outboundUsage, 600);
620
+ assertEq(rl.inboundUsage, 400);
621
+ assertEq(rl.lastUpdated, block.timestamp);
622
+ }
623
+
630
624
  function test_checkpointRateLimits_Empty() public {
631
625
  uint256[] memory ids = new uint256[](0);
632
626
  rateLimiter.checkpointRateLimits(ids);
@@ -678,18 +672,110 @@ contract RateLimiterBaseUpgradeableTest is Test {
678
672
 
679
673
  // ============ Outflow/Inflow Tests ============
680
674
 
681
- function test_outflow_DefaultConfig_SpecificState_Outbound_Gross_NotExempt() public {
682
- rateLimiter.outflow(ID_SNAP_DEFAULT_OUTBOUND_GROSS, alice, 100);
675
+ function test_outflow_Revert_UnconfiguredId() public {
676
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
677
+ rateLimiter.outflow(ID_2, bob, 100);
678
+ }
683
679
 
684
- (uint256 usage, , , ) = rateLimiter.getRateLimitUsages(ID_SNAP_DEFAULT_OUTBOUND_GROSS);
685
- assertEq(usage, 100);
680
+ function test_inflow_Revert_UnconfiguredId() public {
681
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
682
+ rateLimiter.inflow(ID_2, bob, 100);
686
683
  }
687
684
 
688
- function test_inflow_DefaultConfig_SpecificState_Inbound_Gross_NotExempt() public {
689
- rateLimiter.inflow(ID_SNAP_DEFAULT_INBOUND_GROSS, alice, 100);
685
+ function test_outflow_Revert_UnconfiguredIdWithState() public {
686
+ IRateLimiter.SetRateLimitStateParam[] memory states = new IRateLimiter.SetRateLimitStateParam[](1);
687
+ states[0] = _createState(ID_2, 0, 50, uint40(block.timestamp));
688
+ rateLimiter.setRateLimitStates(states);
689
+
690
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
691
+ rateLimiter.outflow(ID_2, bob, 100);
692
+ }
690
693
 
691
- (, , uint256 usage, ) = rateLimiter.getRateLimitUsages(ID_SNAP_DEFAULT_INBOUND_GROSS);
692
- assertEq(usage, 100);
694
+ function test_outflow_Revert_UnconfiguredIdExemptAddress() public {
695
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
696
+ rateLimiter.outflow(ID_2, alice, 100);
697
+ }
698
+
699
+ function test_outflow_Revert_InactiveConfig() public {
700
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
701
+ configs[0] = _createConfig(ID_1, false, false, false, false, false, 1000, 1000, 1000, 1000);
702
+ rateLimiter.setRateLimitConfigs(configs);
703
+
704
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
705
+ rateLimiter.outflow(ID_1, bob, 100);
706
+
707
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
708
+ rateLimiter.inflow(ID_1, bob, 100);
709
+ }
710
+
711
+ function test_outflow_Revert_DeactivatedConfig() public {
712
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
713
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, 1000, 1000);
714
+ rateLimiter.setRateLimitConfigs(configs);
715
+ rateLimiter.outflow(ID_1, bob, 100);
716
+
717
+ configs[0] = _createConfig(ID_1, false, true, true, false, false, 1000, 1000, 1000, 1000);
718
+ rateLimiter.setRateLimitConfigs(configs);
719
+
720
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
721
+ rateLimiter.outflow(ID_1, bob, 100);
722
+ }
723
+
724
+ function test_outflow_ReactivatedConfigKeepsDecay() public {
725
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
726
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, 1000, 1000);
727
+ rateLimiter.setRateLimitConfigs(configs);
728
+ rateLimiter.outflow(ID_1, bob, 1000);
729
+
730
+ configs[0] = _createConfig(ID_1, false, true, true, false, false, 1000, 1000, 1000, 1000);
731
+ rateLimiter.setRateLimitConfigs(configs);
732
+
733
+ vm.warp(block.timestamp + 10 * 1000);
734
+ uint256[] memory ids = new uint256[](1);
735
+ ids[0] = ID_1;
736
+ rateLimiter.checkpointRateLimits(ids);
737
+ rateLimiter.outflow(ID_1, bob, 0);
738
+
739
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, 1000, 1000);
740
+ rateLimiter.setRateLimitConfigs(configs);
741
+
742
+ (, uint256 outAvail, , ) = rateLimiter.getRateLimitUsages(ID_1);
743
+ assertEq(outAvail, 1000);
744
+ rateLimiter.outflow(ID_1, bob, 1000);
745
+ }
746
+
747
+ function test_getRateLimitUsages_InactiveUsageDecays() public {
748
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
749
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, 1000, 1000);
750
+ rateLimiter.setRateLimitConfigs(configs);
751
+ rateLimiter.outflow(ID_1, bob, 1000);
752
+
753
+ configs[0] = _createConfig(ID_1, false, true, true, false, false, 1000, 1000, 1000, 1000);
754
+ rateLimiter.setRateLimitConfigs(configs);
755
+ vm.warp(block.timestamp + 500);
756
+
757
+ (uint256 outUsage, uint256 outAvail, , uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
758
+ assertEq(outUsage, 500);
759
+ assertEq(outAvail, 0);
760
+ assertEq(inAvail, 0);
761
+ }
762
+
763
+ function test_outflow_Revert_GlobalStateUnconfigured() public {
764
+ RateLimiterBaseUpgradeableHarness globalLimiter = _deployRateLimiter(0, true);
765
+
766
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
767
+ globalLimiter.outflow(ID_1, bob, 100);
768
+ }
769
+
770
+ function test_outflow_ConfiguredIdDoesNotOpenOtherIds() public {
771
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
772
+ configs[0] = _createConfig(ID_1, true, false, false, false, false, 0, 0, 0, 0);
773
+ rateLimiter.setRateLimitConfigs(configs);
774
+
775
+ rateLimiter.outflow(ID_1, bob, 100);
776
+
777
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 100));
778
+ rateLimiter.outflow(ID_2, bob, 100);
693
779
  }
694
780
 
695
781
  function test_outflow_ExplicitConfig_SpecificState_Outbound_Gross_NotExempt() public {
@@ -730,6 +816,173 @@ contract RateLimiterBaseUpgradeableTest is Test {
730
816
  assertEq(inUsage, 0);
731
817
  }
732
818
 
819
+ function test_outflow_NoDecayWindow() public {
820
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
821
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, NO_DECAY_WINDOW, NO_DECAY_WINDOW);
822
+ rateLimiter.setRateLimitConfigs(configs);
823
+
824
+ rateLimiter.outflow(ID_1, alice, 600);
825
+
826
+ vm.warp(block.timestamp + 100 * YEAR);
827
+
828
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 400, 401));
829
+ rateLimiter.outflow(ID_1, alice, 401);
830
+
831
+ rateLimiter.outflow(ID_1, alice, 400);
832
+
833
+ (uint256 outUsage, uint256 outAvail, , ) = rateLimiter.getRateLimitUsages(ID_1);
834
+ assertEq(outUsage, 1000);
835
+ assertEq(outAvail, 0);
836
+ }
837
+
838
+ function test_inflow_NoDecayWindow() public {
839
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
840
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, NO_DECAY_WINDOW, NO_DECAY_WINDOW);
841
+ rateLimiter.setRateLimitConfigs(configs);
842
+
843
+ rateLimiter.inflow(ID_1, alice, 600);
844
+
845
+ vm.warp(block.timestamp + 100 * YEAR);
846
+
847
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 400, 401));
848
+ rateLimiter.inflow(ID_1, alice, 401);
849
+
850
+ rateLimiter.inflow(ID_1, alice, 400);
851
+
852
+ (, , uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
853
+ assertEq(inUsage, 1000);
854
+ assertEq(inAvail, 0);
855
+ }
856
+
857
+ function test_inflow_HubLedger() public {
858
+ _setUpHubLedger(rateLimiter, ID_1, false, 0, 0);
859
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 0);
860
+
861
+ rateLimiter.outflow(ID_1, alice, 300);
862
+ rateLimiter.inflow(ID_1, alice, 100);
863
+ rateLimiter.outflow(ID_1, alice, 50);
864
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 250);
865
+
866
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 250, 251));
867
+ rateLimiter.inflow(ID_1, alice, 251);
868
+
869
+ rateLimiter.inflow(ID_1, alice, 250);
870
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 0);
871
+
872
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 1));
873
+ rateLimiter.inflow(ID_1, alice, 1);
874
+ }
875
+
876
+ function test_inflow_HubLedger_LongWarp() public {
877
+ _setUpHubLedger(rateLimiter, ID_1, false, 0, 0);
878
+ rateLimiter.outflow(ID_1, alice, 300);
879
+
880
+ vm.warp(block.timestamp + 100 * YEAR);
881
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 300);
882
+
883
+ vm.warp(block.timestamp + 10_000 * YEAR);
884
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 300);
885
+
886
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 300, 301));
887
+ rateLimiter.inflow(ID_1, alice, 301);
888
+
889
+ rateLimiter.inflow(ID_1, alice, 300);
890
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 0);
891
+ }
892
+
893
+ function test_inflow_HubLedger_DecayingOutboundNetLimit() public {
894
+ _setUpHubLedger(rateLimiter, ID_1, true, 500, 100);
895
+
896
+ rateLimiter.outflow(ID_1, alice, 500);
897
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 500);
898
+
899
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 1));
900
+ rateLimiter.outflow(ID_1, alice, 1);
901
+
902
+ vm.warp(block.timestamp + 50);
903
+ (uint256 outUsage, uint256 outAvail, , ) = rateLimiter.getRateLimitUsages(ID_1);
904
+ assertEq(outUsage, 250);
905
+ assertEq(outAvail, 250);
906
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 500);
907
+
908
+ rateLimiter.inflow(ID_1, alice, 200);
909
+ (outUsage, outAvail, , ) = rateLimiter.getRateLimitUsages(ID_1);
910
+ assertEq(outUsage, 50);
911
+ assertEq(outAvail, 450);
912
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 300);
913
+
914
+ vm.warp(block.timestamp + 100 * YEAR);
915
+ (outUsage, , , ) = rateLimiter.getRateLimitUsages(ID_1);
916
+ assertEq(outUsage, 0);
917
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 300);
918
+
919
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 300, 301));
920
+ rateLimiter.inflow(ID_1, alice, 301);
921
+
922
+ rateLimiter.inflow(ID_1, alice, 300);
923
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 0);
924
+ }
925
+
926
+ function test_inflow_HubLedger_WithScaleDecimals() public {
927
+ RateLimiterBaseUpgradeableHarness scaledLimiter = _deployRateLimiter(12, false);
928
+ _setUpHubLedger(scaledLimiter, ID_1, false, 0, 0);
929
+
930
+ scaledLimiter.outflow(ID_1, alice, 300_000_000e18 + 123_456e12);
931
+ scaledLimiter.inflow(ID_1, alice, 100_000_000e18);
932
+
933
+ uint256 headroom = 200_000_000e18 + 123_456e12;
934
+ assertEq(_inboundAvailable(scaledLimiter, ID_1), headroom);
935
+
936
+ vm.warp(block.timestamp + 100 * YEAR);
937
+ assertEq(_inboundAvailable(scaledLimiter, ID_1), headroom);
938
+
939
+ vm.expectRevert(
940
+ abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, headroom / 1e12, headroom / 1e12 + 1)
941
+ );
942
+ scaledLimiter.inflow(ID_1, alice, headroom + 1);
943
+
944
+ scaledLimiter.inflow(ID_1, alice, headroom);
945
+ assertEq(_inboundAvailable(scaledLimiter, ID_1), 0);
946
+ }
947
+
948
+ function test_inflow_HubLedger_MaxLimit() public {
949
+ _setUpHubLedger(rateLimiter, ID_1, false, 0, 0);
950
+
951
+ rateLimiter.outflow(ID_1, alice, HUB_LEDGER_LIMIT);
952
+ rateLimiter.outflow(ID_1, alice, 1);
953
+ assertEq(_inboundAvailable(rateLimiter, ID_1), HUB_LEDGER_LIMIT);
954
+
955
+ vm.expectRevert(
956
+ abi.encodeWithSelector(
957
+ IRateLimiter.RateLimitExceeded.selector,
958
+ HUB_LEDGER_LIMIT,
959
+ uint256(HUB_LEDGER_LIMIT) + 1
960
+ )
961
+ );
962
+ rateLimiter.inflow(ID_1, alice, uint256(HUB_LEDGER_LIMIT) + 1);
963
+
964
+ rateLimiter.inflow(ID_1, alice, HUB_LEDGER_LIMIT);
965
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 0);
966
+ assertEq(rateLimiter.rateLimits(ID_1).inboundUsage, HUB_LEDGER_LIMIT);
967
+
968
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 1));
969
+ rateLimiter.inflow(ID_1, alice, 1);
970
+ }
971
+
972
+ function test_outflow_HubLedger_PerIdIsolation() public {
973
+ _setUpHubLedger(rateLimiter, ID_1, false, 0, 0);
974
+ _setUpHubLedger(rateLimiter, ID_2, false, 0, 0);
975
+
976
+ rateLimiter.outflow(ID_1, alice, 300);
977
+ assertEq(_inboundAvailable(rateLimiter, ID_1), 300);
978
+ assertEq(_inboundAvailable(rateLimiter, ID_2), 0);
979
+
980
+ vm.expectRevert(abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, 0, 1));
981
+ rateLimiter.inflow(ID_2, alice, 1);
982
+
983
+ rateLimiter.inflow(ID_1, alice, 300);
984
+ }
985
+
733
986
  function test_outflow_Revert_RateLimitExceeded() public {
734
987
  IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
735
988
  configs[0] = _createConfig(ID_1, true, true, true, false, false, 100, 100, 100, 100);
@@ -760,8 +1013,87 @@ contract RateLimiterBaseUpgradeableTest is Test {
760
1013
  assertEq(outAvailAfter, 500);
761
1014
  }
762
1015
 
1016
+ function test_getRateLimitUsages_NoDecayWindow() public {
1017
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
1018
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, NO_DECAY_WINDOW, NO_DECAY_WINDOW);
1019
+ rateLimiter.setRateLimitConfigs(configs);
1020
+
1021
+ rateLimiter.outflow(ID_1, alice, 600);
1022
+ rateLimiter.inflow(ID_1, alice, 400);
1023
+
1024
+ vm.warp(block.timestamp + 100 * YEAR);
1025
+
1026
+ (uint256 outUsage, uint256 outAvail, uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
1027
+ assertEq(outUsage, 600);
1028
+ assertEq(outAvail, 400);
1029
+ assertEq(inUsage, 400);
1030
+ assertEq(inAvail, 600);
1031
+
1032
+ vm.warp(type(uint40).max);
1033
+
1034
+ (outUsage, outAvail, inUsage, inAvail) = rateLimiter.getRateLimitUsages(ID_1);
1035
+ assertEq(outUsage, 600);
1036
+ assertEq(outAvail, 400);
1037
+ assertEq(inUsage, 400);
1038
+ assertEq(inAvail, 600);
1039
+ }
1040
+
1041
+ function test_getRateLimitUsages_MaxDecayingWindow() public {
1042
+ uint32 window = NO_DECAY_WINDOW - 1;
1043
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
1044
+ configs[0] = _createConfig(ID_1, true, true, true, false, false, 1000, 1000, window, window);
1045
+ rateLimiter.setRateLimitConfigs(configs);
1046
+
1047
+ rateLimiter.outflow(ID_1, alice, 1000);
1048
+ rateLimiter.inflow(ID_1, alice, 1000);
1049
+
1050
+ vm.warp(block.timestamp + 100 * YEAR);
1051
+
1052
+ uint256 decay = (1000 * 100 * YEAR) / window;
1053
+ (uint256 outUsage, , uint256 inUsage, ) = rateLimiter.getRateLimitUsages(ID_1);
1054
+ assertEq(outUsage, 1000 - decay);
1055
+ assertEq(inUsage, 1000 - decay);
1056
+ assertGt(decay, 0);
1057
+ }
1058
+
1059
+ function test_getRateLimitUsages_Fuzz_NoDecayWindow(
1060
+ uint96 _outboundUsage,
1061
+ uint96 _inboundUsage,
1062
+ uint96 _limit,
1063
+ uint256 _elapsed
1064
+ ) public {
1065
+ _elapsed = bound(_elapsed, 0, type(uint40).max - block.timestamp);
1066
+
1067
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
1068
+ configs[0] = _createConfig(
1069
+ ID_1,
1070
+ true,
1071
+ true,
1072
+ true,
1073
+ false,
1074
+ false,
1075
+ _limit,
1076
+ _limit,
1077
+ NO_DECAY_WINDOW,
1078
+ NO_DECAY_WINDOW
1079
+ );
1080
+ rateLimiter.setRateLimitConfigs(configs);
1081
+
1082
+ IRateLimiter.SetRateLimitStateParam[] memory states = new IRateLimiter.SetRateLimitStateParam[](1);
1083
+ states[0] = _createState(ID_1, _outboundUsage, _inboundUsage, uint40(block.timestamp));
1084
+ rateLimiter.setRateLimitStates(states);
1085
+
1086
+ vm.warp(block.timestamp + _elapsed);
1087
+
1088
+ (uint256 outUsage, uint256 outAvail, uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
1089
+ assertEq(outUsage, _outboundUsage);
1090
+ assertEq(outAvail, _limit > _outboundUsage ? _limit - _outboundUsage : 0);
1091
+ assertEq(inUsage, _inboundUsage);
1092
+ assertEq(inAvail, _limit > _inboundUsage ? _limit - _inboundUsage : 0);
1093
+ }
1094
+
763
1095
  function test_getRateLimitUsages_OutboundDisabled() public view {
764
- // ID_SNAP_DISABLED: override=true, outbound=false, inbound=true, limit=1000, window=1000.
1096
+ // ID_SNAP_DISABLED: outbound=false, inbound=true, limit=1000, window=1000.
765
1097
  (uint256 outUsage, uint256 outAvail, uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(
766
1098
  ID_SNAP_DISABLED
767
1099
  );
@@ -773,7 +1105,7 @@ contract RateLimiterBaseUpgradeableTest is Test {
773
1105
  }
774
1106
 
775
1107
  function test_getRateLimitUsages_InboundDisabled() public view {
776
- // ID_SNAP_EXPLICIT_OUTBOUND_GROSS: override=true, outbound=true, inbound=false, limit=1000, window=1000.
1108
+ // ID_SNAP_EXPLICIT_OUTBOUND_GROSS: outbound=true, inbound=false, limit=1000, window=1000.
777
1109
  (uint256 outUsage, uint256 outAvail, uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(
778
1110
  ID_SNAP_EXPLICIT_OUTBOUND_GROSS
779
1111
  );
@@ -784,16 +1116,37 @@ contract RateLimiterBaseUpgradeableTest is Test {
784
1116
  assertEq(inAvail, type(uint256).max);
785
1117
  }
786
1118
 
787
- function test_getRateLimitUsages_BothDisabled() public view {
788
- // ID_SNAP_DEFAULT_DISABLED falls back to default which has outbound=false, inbound=false.
789
- (, uint256 outAvail, , uint256 inAvail) = rateLimiterDefaultDisabled.getRateLimitUsages(
790
- ID_SNAP_DEFAULT_DISABLED
791
- );
1119
+ function test_getRateLimitUsages_BothDisabled() public {
1120
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
1121
+ configs[0] = _createConfig(ID_1, true, false, false, false, false, 1000, 1000, 1000, 1000);
1122
+ rateLimiter.setRateLimitConfigs(configs);
1123
+
1124
+ (, uint256 outAvail, , uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
792
1125
 
793
1126
  assertEq(outAvail, type(uint256).max);
794
1127
  assertEq(inAvail, type(uint256).max);
795
1128
  }
796
1129
 
1130
+ function test_getRateLimitUsages_Inactive() public {
1131
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](1);
1132
+ configs[0] = _createConfig(ID_1, false, false, false, false, false, 1000, 1000, 1000, 1000);
1133
+ rateLimiter.setRateLimitConfigs(configs);
1134
+
1135
+ (, uint256 outAvail, , uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_1);
1136
+
1137
+ assertEq(outAvail, 0);
1138
+ assertEq(inAvail, 0);
1139
+ }
1140
+
1141
+ function test_getRateLimitUsages_Unconfigured() public view {
1142
+ (uint256 outUsage, uint256 outAvail, uint256 inUsage, uint256 inAvail) = rateLimiter.getRateLimitUsages(ID_2);
1143
+
1144
+ assertEq(outUsage, 0);
1145
+ assertEq(outAvail, 0);
1146
+ assertEq(inUsage, 0);
1147
+ assertEq(inAvail, 0);
1148
+ }
1149
+
797
1150
  function test_getRateLimitUsages_GloballyDisabled() public {
798
1151
  rateLimiter.setRateLimitGlobalConfig(
799
1152
  IRateLimiter.RateLimitGlobalConfig({ useGlobalState: false, isGloballyDisabled: true })
@@ -808,10 +1161,10 @@ contract RateLimiterBaseUpgradeableTest is Test {
808
1161
  function test_initial_state_closed() public {
809
1162
  RateLimiterBaseUpgradeableHarness freshLimiter = _deployRateLimiter(0, false);
810
1163
 
811
- IRateLimiter.RateLimit memory rl = freshLimiter.rateLimits(0);
1164
+ IRateLimiter.RateLimit memory rl = freshLimiter.rateLimits(GLOBAL_STATE_ID);
812
1165
 
813
- // Bitmap = encode(false, true, true, true, false) = 0 | 2 | 4 | 8 | 0 = 14.
814
- assertEq(rl.configBitmap, 14);
1166
+ // All-zero bitmap, closed in both directions.
1167
+ assertEq(rl.configBitmap, 0);
815
1168
  assertEq(rl.outboundLimit, 0);
816
1169
  assertEq(rl.inboundLimit, 0);
817
1170
 
@@ -99,7 +99,7 @@ contract RateLimiterRBACUpgradeableHandler is Test {
99
99
  configs[0] = IRateLimiter.SetRateLimitConfigParam({
100
100
  id: ID,
101
101
  config: IRateLimiter.RateLimitConfig({
102
- overrideDefaultConfig: true,
102
+ active: true,
103
103
  outboundEnabled: true,
104
104
  inboundEnabled: true,
105
105
  netAccountingEnabled: newNet,
@@ -407,7 +407,7 @@ contract RateLimiterRBACUpgradeableInvariantTest is StdInvariant, Test {
407
407
  IRateLimiter.RateLimit memory rl = limiter.rateLimits(handler.ID());
408
408
  uint256 bitmap = rl.configBitmap;
409
409
 
410
- assertTrue(bitmap & 1 != 0, "Override default config bit not set");
410
+ assertTrue(bitmap & 1 != 0, "Active bit not set");
411
411
  assertTrue(bitmap & 2 != 0, "Outbound enabled bit not set");
412
412
  assertTrue(bitmap & 4 != 0, "Inbound enabled bit not set");
413
413
 
@@ -442,3 +442,127 @@ contract RateLimiterRBACUpgradeableInvariantGlobalStateTest is RateLimiterRBACUp
442
442
  assertEq(inUsage, activeIn, "Global state: pristine ID should mirror active ID inbound usage");
443
443
  }
444
444
  }
445
+
446
+ contract RateLimiterRBACUpgradeableHubLedgerHandler is Test {
447
+ RateLimiterRBACUpgradeableFuzzHarness public limiter;
448
+
449
+ uint96 public constant LEDGER_LIMIT = type(uint96).max;
450
+ uint256 public constant ID_COUNT = 3;
451
+ address public constant USER = address(0x123);
452
+
453
+ mapping(uint256 id => uint256 netOutflow) public ghost_netOutflow;
454
+ bool public ghost_revertMismatch;
455
+
456
+ constructor(RateLimiterRBACUpgradeableFuzzHarness _limiter) {
457
+ limiter = _limiter;
458
+
459
+ IRateLimiter.SetRateLimitConfigParam[] memory configs = new IRateLimiter.SetRateLimitConfigParam[](ID_COUNT);
460
+ IRateLimiter.SetRateLimitStateParam[] memory states = new IRateLimiter.SetRateLimitStateParam[](ID_COUNT);
461
+ for (uint256 i = 0; i < ID_COUNT; i++) {
462
+ configs[i] = IRateLimiter.SetRateLimitConfigParam({
463
+ id: id(i),
464
+ config: IRateLimiter.RateLimitConfig({
465
+ active: true,
466
+ outboundEnabled: false,
467
+ inboundEnabled: true,
468
+ netAccountingEnabled: true,
469
+ addressExemptionEnabled: false,
470
+ outboundLimit: 0,
471
+ inboundLimit: LEDGER_LIMIT,
472
+ outboundWindow: 0,
473
+ inboundWindow: type(uint32).max
474
+ })
475
+ });
476
+ states[i] = IRateLimiter.SetRateLimitStateParam({
477
+ id: id(i),
478
+ state: IRateLimiter.RateLimitState({
479
+ outboundUsage: 0,
480
+ inboundUsage: LEDGER_LIMIT,
481
+ lastUpdated: uint40(block.timestamp)
482
+ })
483
+ });
484
+ }
485
+
486
+ vm.startPrank(msg.sender);
487
+ limiter.setRateLimitConfigs(configs);
488
+ limiter.setRateLimitStates(states);
489
+ vm.stopPrank();
490
+ }
491
+
492
+ function id(uint256 _index) public pure returns (uint256) {
493
+ return uint256(keccak256(abi.encode("HUB_LEDGER_ID", _index)));
494
+ }
495
+
496
+ function outflow(uint256 index, uint96 amount) public {
497
+ uint256 ledgerId = id(index % ID_COUNT);
498
+ amount = uint96(bound(amount, 1, LEDGER_LIMIT / 4));
499
+
500
+ try limiter.outflow(ledgerId, USER, amount) {
501
+ ghost_netOutflow[ledgerId] = Math.min(ghost_netOutflow[ledgerId] + amount, LEDGER_LIMIT);
502
+ } catch {
503
+ ghost_revertMismatch = true;
504
+ }
505
+ }
506
+
507
+ function inflow(uint256 index, uint96 amount, bool exact) public {
508
+ uint256 ledgerId = id(index % ID_COUNT);
509
+ uint256 netOutflow = ghost_netOutflow[ledgerId];
510
+ amount = exact ? uint96(netOutflow) : uint96(bound(amount, 1, LEDGER_LIMIT / 4));
511
+ bool shouldSucceed = amount <= netOutflow;
512
+
513
+ try limiter.inflow(ledgerId, USER, amount) {
514
+ if (!shouldSucceed) {
515
+ ghost_revertMismatch = true;
516
+ return;
517
+ }
518
+ ghost_netOutflow[ledgerId] = netOutflow - amount;
519
+ } catch (bytes memory reason) {
520
+ bytes memory expected = abi.encodeWithSelector(IRateLimiter.RateLimitExceeded.selector, netOutflow, amount);
521
+ if (shouldSucceed || keccak256(reason) != keccak256(expected)) ghost_revertMismatch = true;
522
+ }
523
+ }
524
+
525
+ function warp(uint256 seconds_) public {
526
+ seconds_ = bound(seconds_, 1, 100 * 365 days);
527
+ vm.warp(block.timestamp + seconds_);
528
+ }
529
+ }
530
+
531
+ contract RateLimiterRBACUpgradeableHubLedgerInvariantTest is StdInvariant, Test {
532
+ RateLimiterRBACUpgradeableFuzzHarness limiter;
533
+ RateLimiterRBACUpgradeableHubLedgerHandler handler;
534
+
535
+ function setUp() public {
536
+ RateLimiterRBACUpgradeableFuzzHarness impl = new RateLimiterRBACUpgradeableFuzzHarness(0);
537
+ TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
538
+ address(impl),
539
+ address(this),
540
+ abi.encodeWithSelector(RateLimiterRBACUpgradeableFuzzHarness.initialize.selector, false, address(this))
541
+ );
542
+ limiter = RateLimiterRBACUpgradeableFuzzHarness(address(proxy));
543
+ handler = new RateLimiterRBACUpgradeableHubLedgerHandler(limiter);
544
+
545
+ targetContract(address(handler));
546
+
547
+ bytes4[] memory selectors = new bytes4[](3);
548
+ selectors[0] = RateLimiterRBACUpgradeableHubLedgerHandler.outflow.selector;
549
+ selectors[1] = RateLimiterRBACUpgradeableHubLedgerHandler.inflow.selector;
550
+ selectors[2] = RateLimiterRBACUpgradeableHubLedgerHandler.warp.selector;
551
+ targetSelector(FuzzSelector({ addr: address(handler), selectors: selectors }));
552
+ }
553
+
554
+ /// @notice Inbound headroom must equal the ghost net outflow of each ID, capped at the ledger limit.
555
+ function invariant_HeadroomMatchesNetOutflow() public view {
556
+ for (uint256 i = 0; i < handler.ID_COUNT(); i++) {
557
+ uint256 ledgerId = handler.id(i);
558
+ (, , , uint256 inAvail) = limiter.getRateLimitUsages(ledgerId);
559
+ assertEq(inAvail, handler.ghost_netOutflow(ledgerId), "Headroom diverged from net outflow");
560
+ assertLe(inAvail, handler.LEDGER_LIMIT(), "Headroom exceeds ledger limit");
561
+ }
562
+ }
563
+
564
+ /// @notice Every outflow/inflow must succeed or revert exactly as predicted.
565
+ function invariant_RevertConsistency() public view {
566
+ assertFalse(handler.ghost_revertMismatch(), "Revert behavior diverged from predicted");
567
+ }
568
+ }
@@ -6,9 +6,9 @@ import { RateLimiterUtils } from "../contracts/rate-limiter/libs/RateLimiterUtil
6
6
 
7
7
  /// @dev Individual harnesses to prevent function selection costs in snapshots.
8
8
 
9
- contract DecodeOverrideDefaultConfigHarness {
10
- function decodeOverrideDefaultConfig(uint24 _config) external pure returns (bool overrideDefaultConfig) {
11
- return RateLimiterUtils.decodeOverrideDefaultConfig(_config);
9
+ contract DecodeActiveHarness {
10
+ function decodeActive(uint24 _config) external pure returns (bool active) {
11
+ return RateLimiterUtils.decodeActive(_config);
12
12
  }
13
13
  }
14
14
 
@@ -26,7 +26,7 @@ contract DecodeConfigBitmapFlagsHarness {
26
26
 
27
27
  contract EncodeConfigBitmapHarness {
28
28
  function encodeConfigBitmap(
29
- bool _overrideDefaultConfig,
29
+ bool _active,
30
30
  bool _outboundEnabled,
31
31
  bool _inboundEnabled,
32
32
  bool _netAccountingEnabled,
@@ -34,7 +34,7 @@ contract EncodeConfigBitmapHarness {
34
34
  ) external pure returns (uint24 config) {
35
35
  return
36
36
  RateLimiterUtils.encodeConfigBitmap(
37
- _overrideDefaultConfig,
37
+ _active,
38
38
  _outboundEnabled,
39
39
  _inboundEnabled,
40
40
  _netAccountingEnabled,
@@ -44,36 +44,36 @@ contract EncodeConfigBitmapHarness {
44
44
  }
45
45
 
46
46
  contract RateLimiterUtilsTest is Test {
47
- DecodeOverrideDefaultConfigHarness public decodeOverrideDefaultConfigHarness;
47
+ DecodeActiveHarness public decodeActiveHarness;
48
48
  DecodeConfigBitmapFlagsHarness public decodeConfigBitmapFlagsHarness;
49
49
  EncodeConfigBitmapHarness public encodeConfigBitmapHarness;
50
50
 
51
51
  function setUp() public {
52
- decodeOverrideDefaultConfigHarness = new DecodeOverrideDefaultConfigHarness();
52
+ decodeActiveHarness = new DecodeActiveHarness();
53
53
  decodeConfigBitmapFlagsHarness = new DecodeConfigBitmapFlagsHarness();
54
54
  encodeConfigBitmapHarness = new EncodeConfigBitmapHarness();
55
55
  }
56
56
 
57
57
  // ============ Encode/Decode Tests ============
58
58
 
59
- function test_decodeOverrideDefaultConfig_False() public view {
59
+ function test_decodeActive_False() public view {
60
60
  uint24 config = 0;
61
- bool overrideDefaultConfig = decodeOverrideDefaultConfigHarness.decodeOverrideDefaultConfig(config);
62
- assertFalse(overrideDefaultConfig);
61
+ bool active = decodeActiveHarness.decodeActive(config);
62
+ assertFalse(active);
63
63
  }
64
64
 
65
- function test_decodeOverrideDefaultConfig_True() public view {
65
+ function test_decodeActive_True() public view {
66
66
  uint24 config = 1;
67
- bool overrideDefaultConfig = decodeOverrideDefaultConfigHarness.decodeOverrideDefaultConfig(config);
68
- assertTrue(overrideDefaultConfig);
67
+ bool active = decodeActiveHarness.decodeActive(config);
68
+ assertTrue(active);
69
69
  }
70
70
 
71
- function test_decodeOverrideDefaultConfig_Fuzz(uint24 config) public pure {
72
- bool overrideDefaultConfig = RateLimiterUtils.decodeOverrideDefaultConfig(config);
71
+ function test_decodeActive_Fuzz(uint24 config) public pure {
72
+ bool active = RateLimiterUtils.decodeActive(config);
73
73
  if (config % 2 == 0) {
74
- assertFalse(overrideDefaultConfig);
74
+ assertFalse(active);
75
75
  } else {
76
- assertTrue(overrideDefaultConfig);
76
+ assertTrue(active);
77
77
  }
78
78
  }
79
79