Skip to content
Lattice

CircuitBreaker

Git Source

Inherits: ICircuitBreaker

Title: CircuitBreaker

Thin Diamond facet that exposes multi-key threshold-based circuit breaking.

All logic lives in CircuitBreakerLib. This contract is stateless and forwards every call to the library. Inherit this in your Diamond facet to add circuit-breaking.

Notes:

  • lattice-version: 0.1.0
  • lattice-source: Lattice original

Functions

isTripped

Returns whether the circuit breaker for key is currently tripped.

function isTripped(bytes32 key) public view virtual returns (bool);

Parameters

NameTypeDescription
keybytes32The circuit breaker key to query.

Returns

NameTypeDescription
<none>boolbool True if the circuit breaker is tripped, false otherwise.

getThreshold

Returns the configured threshold and window for key.

function getThreshold(bytes32 key) public view virtual returns (uint256 threshold, uint48 windowSeconds);

Parameters

NameTypeDescription
keybytes32The circuit breaker key to query.

Returns

NameTypeDescription
thresholduint256The trip threshold (0 means unconfigured).
windowSecondsuint48The rolling window duration in seconds.

getCumulative

Returns the current cumulative value and window start time for key.

function getCumulative(bytes32 key) public view virtual returns (uint256 cumulative, uint48 windowStart);

Parameters

NameTypeDescription
keybytes32The circuit breaker key to query.

Returns

NameTypeDescription
cumulativeuint256The accumulated value within the current window.
windowStartuint48The timestamp when the current window started.

setThreshold

Configures or updates the threshold and rolling window for key.

Requires DEFAULT_ADMIN_ROLE. Resets cumulative and tripped state. Reverts with CircuitBreakerInvalidThreshold if threshold == 0. Reverts with CircuitBreakerInvalidWindow if windowSeconds == 0.

function setThreshold(bytes32 key, uint256 threshold, uint48 windowSeconds) public virtual;

Parameters

NameTypeDescription
keybytes32The circuit breaker key to configure.
thresholduint256The trip threshold value (must be > 0).
windowSecondsuint48The rolling window duration in seconds (must be > 0).

recordObservation

Records an observation for key, rolling the window if necessary.

Requires DEFAULT_ADMIN_ROLE. If block.timestamp >= windowStart + windowSeconds, the window resets before accumulating. If the new cumulative value meets or exceeds the threshold the circuit trips and emits CircuitBreakerTripped. Reverts with CircuitBreakerNotConfigured if the key has not been set up. Reverts with CircuitBreakerTrippedError if the circuit is already tripped; call reset() to clear the tripped state before recording further observations.

function recordObservation(bytes32 key, uint256 value) public virtual;

Parameters

NameTypeDescription
keybytes32The circuit breaker key.
valueuint256The value to add to the current window's cumulative.

reset

Resets the tripped state and cumulative for key.

Requires DEFAULT_ADMIN_ROLE. Reverts with CircuitBreakerNotConfigured if the key has not been configured yet (windowSeconds == 0). Note: the tripped flag is sticky — it is NOT automatically cleared when the rolling window expires. An explicit reset() or setThreshold() call is required to untrip the circuit. This is intentional so that operational incidents are never silently resolved without a human action. Emits CircuitBreakerReset.

function reset(bytes32 key) public virtual;

Parameters

NameTypeDescription
keybytes32The circuit breaker key to reset.

exportSelectors

ERC-8153 selector export: this facet's cuttable selectors, tightly packed (4 bytes each).

Excludes exportSelectors() itself (0x0ef22643) - it is never cut into a diamond. Order matches forge inspect CircuitBreaker methodIdentifiers (alphabetical by signature); kept in exact parity by ExportSelectorsParityTest. Chunks: getCumulative(bytes32) 0x7e633690 getThreshold(bytes32) 0x42acf119 isTripped(bytes32) 0xcaac05fa recordObservation(bytes32,uint256) 0xef13ca9d reset(bytes32) 0xed3c7d40 setThreshold(bytes32,uint256,uint48) 0x802ec864

function exportSelectors() external pure virtual returns (bytes memory selectors);

Returns

NameTypeDescription
selectorsbytes