Skip to content

Commit

Permalink
refactor: "prover timeout" -> "dispute penalty time"
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 6, 2024
1 parent 2f300e6 commit 6fdaec3
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 70 deletions.
38 changes: 19 additions & 19 deletions packages/contracts-rfq/contracts/AdminV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
/// @notice The default cancel delay set during contract deployment.
uint256 public constant DEFAULT_CANCEL_DELAY = 1 days;

/// @notice The minimum prover timeout that can be set by the governor.
uint256 public constant MIN_PROVER_TIMEOUT = 1 minutes;
/// @notice The default prover timeout set during contract deployment.
uint256 public constant DEFAULT_PROVER_TIMEOUT = 30 minutes;
/// @notice The minimum dispute penalty time that can be set by the governor.
uint256 public constant MIN_DISPUTE_PENALTY_TIME = 1 minutes;
/// @notice The default dispute penalty time set during contract deployment.
uint256 public constant DEFAULT_DISPUTE_PENALTY_TIME = 30 minutes;

/// @notice The protocol fee rate taken on the origin amount deposited in the origin chain.
uint256 public protocolFeeRate;
Expand All @@ -72,7 +72,7 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
uint256 public cancelDelay;

/// @notice The timeout period that is used to temporarily disactivate a disputed prover.
uint256 public proverTimeout;
uint256 public disputePenaltyTime;

/// @notice A list of all provers ever added to the contract. Can hold up to 2^16-1 provers.
address[] private _allProvers;
Expand All @@ -86,7 +86,7 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
constructor(address defaultAdmin) {
_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
_setCancelDelay(DEFAULT_CANCEL_DELAY);
_setProverTimeout(DEFAULT_PROVER_TIMEOUT);
_setDisputePenaltyTime(DEFAULT_DISPUTE_PENALTY_TIME);
}

/// @inheritdoc IAdminV2
Expand Down Expand Up @@ -123,8 +123,8 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
}

/// @inheritdoc IAdminV2
function setProverTimeout(uint256 newProverTimeout) external onlyRole(GOVERNOR_ROLE) {
_setProverTimeout(newProverTimeout);
function setDisputePenaltyTime(uint256 newDisputePenaltyTime) external onlyRole(GOVERNOR_ROLE) {
_setDisputePenaltyTime(newDisputePenaltyTime);
}

/// @inheritdoc IAdminV2
Expand Down Expand Up @@ -195,20 +195,20 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
return id;
}

Check warning

Code scanning / Slither

Dangerous strict equalities Medium


/// @notice Internal logic to apply the prover timeout to a given prover. Will make the prover inactive
/// for `proverTimeout` seconds. No-op if the prover ID does not exist or prover is already inactive.
function _applyTimeoutPenalty(uint16 proverID) internal {
/// @notice Internal logic to apply the dispute penalty time to a given prover. Will make the prover inactive
/// for `disputePenaltyTime` seconds. No-op if the prover ID does not exist or prover is already inactive.
function _applyDisputePenaltyTime(uint16 proverID) internal {
// Check that the prover exists.
if (proverID == 0 || proverID > _allProvers.length) return;
address prover = _allProvers[proverID - 1];
ProverInfo storage $ = _proverInfos[prover];
// No-op if the prover is already inactive.
if ($.activeFromTimestamp == 0) return;
uint256 newActiveFromTimestamp = block.timestamp + proverTimeout;
uint256 newActiveFromTimestamp = block.timestamp + disputePenaltyTime;
// Update the activeFrom timestamp.
// Note: this is a storage write.
$.activeFromTimestamp = uint240(newActiveFromTimestamp);
emit ProverTimeoutApplied(prover, newActiveFromTimestamp);
emit DisputePenaltyTimeApplied(prover, newActiveFromTimestamp);
}

Check warning

Code scanning / Slither

Dead-code Warning

AdminV2._applyDisputePenaltyTime(uint16) is never used and should be removed

/// @notice Internal logic to set the cancel delay. Security checks are performed outside of this function.
Expand All @@ -220,12 +220,12 @@ contract AdminV2 is AccessControlEnumerable, IAdminV2, IAdminV2Errors {
emit CancelDelayUpdated(oldCancelDelay, newCancelDelay);
}

/// @notice Internal logic to set the prover timeout. Security checks are performed outside of this function.
/// @notice Internal logic to set the dispute penalty time. Security checks are performed outside of this function.
/// @dev This function is marked as private to prevent child contracts from calling it directly.
function _setProverTimeout(uint256 newProverTimeout) private {
if (newProverTimeout < MIN_PROVER_TIMEOUT) revert ProverTimeoutBelowMin();
uint256 oldProverTimeout = proverTimeout;
proverTimeout = newProverTimeout;
emit ProverTimeoutUpdated(oldProverTimeout, newProverTimeout);
function _setDisputePenaltyTime(uint256 newDisputePenaltyTime) private {
if (newDisputePenaltyTime < MIN_DISPUTE_PENALTY_TIME) revert DisputePenaltyTimeBelowMin();
uint256 oldDisputePenaltyTime = disputePenaltyTime;
disputePenaltyTime = newDisputePenaltyTime;
emit DisputePenaltyTimeUpdated(oldDisputePenaltyTime, newDisputePenaltyTime);
}
}
2 changes: 1 addition & 1 deletion packages/contracts-rfq/contracts/FastBridgeV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract FastBridgeV2 is AdminV2, MulticallTarget, IFastBridgeV2, IFastBridgeV2E

// Apply the timeout penalty to the prover that submitted the proof.
// Note: this is a no-op if the prover has already been removed.
_applyTimeoutPenalty(proverID);
_applyDisputePenaltyTime(proverID);

// Update status to REQUESTED and delete the disputed proof details.
// Note: these are storage writes.
Expand Down
10 changes: 5 additions & 5 deletions packages/contracts-rfq/contracts/interfaces/IAdminV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ pragma solidity ^0.8.4;

interface IAdminV2 {
event CancelDelayUpdated(uint256 oldCancelDelay, uint256 newCancelDelay);
event ProverTimeoutUpdated(uint256 oldProverTimeout, uint256 newProverTimeout);
event DisputePenaltyTimeUpdated(uint256 oldDisputePenaltyTime, uint256 newDisputePenaltyTime);
event FeeRateUpdated(uint256 oldFeeRate, uint256 newFeeRate);
event FeesSwept(address token, address recipient, uint256 amount);

event ProverAdded(address prover);
event ProverRemoved(address prover);
event ProverTimeoutApplied(address prover, uint256 inactiveUntilTimestamp);
event DisputePenaltyTimeApplied(address prover, uint256 inactiveUntilTimestamp);

/// @notice Allows the role admin to add a new prover to the contract.
function addProver(address prover) external;
Expand All @@ -21,10 +21,10 @@ interface IAdminV2 {
/// deadline during which a transaction can be permissionlessly cancelled if it hasn't been proven by any Relayer.
function setCancelDelay(uint256 newCancelDelay) external;

/// @notice Allows the governor to set the prover timeout. The prover timeout is the time period used to
/// temporarily deactivate a prover if its proof is disputed: prover will be inactive for the prover timeout
/// @notice Allows the governor to set the dispute penalty time. The dispute penalty time is the time period used to
/// temporarily deactivate a prover if its proof is disputed: prover will be inactive for the dispute penalty time
/// after the dispute is submitted.
function setProverTimeout(uint256 newProverTimeout) external;
function setDisputePenaltyTime(uint256 newDisputePenaltyTime) external;

/// @notice Allows the governor to set the protocol fee rate. The protocol fee is taken from the origin
/// amount and is only applied to completed and claimed transactions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface IAdminV2Errors {
error ProverAlreadyActive();
error ProverCapacityExceeded();
error ProverNotActive();
error ProverTimeoutBelowMin();
error DisputePenaltyTimeBelowMin();
}
42 changes: 21 additions & 21 deletions packages/contracts-rfq/test/FastBridgeV2.Management.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ contract FastBridgeV2ManagementTest is FastBridgeV2Test {
uint256 public constant MIN_CANCEL_DELAY = 1 hours;
uint256 public constant DEFAULT_CANCEL_DELAY = 1 days;

uint256 public constant MIN_PROVER_TIMEOUT = 1 minutes;
uint256 public constant DEFAULT_PROVER_TIMEOUT = 30 minutes;
uint256 public constant MIN_DISPUTE_PENALTY_TIME = 1 minutes;
uint256 public constant DEFAULT_DISPUTE_PENALTY_TIME = 30 minutes;

address public admin = makeAddr("Admin");
address public governorA = makeAddr("Governor A");
Expand All @@ -26,7 +26,7 @@ contract FastBridgeV2ManagementTest is FastBridgeV2Test {
event ProverRemoved(address prover);

event CancelDelayUpdated(uint256 oldCancelDelay, uint256 newCancelDelay);
event ProverTimeoutUpdated(uint256 oldProverTimeout, uint256 newProverTimeout);
event DisputePenaltyTimeUpdated(uint256 oldDisputePenaltyTime, uint256 newDisputePenaltyTime);
event FeeRateUpdated(uint256 oldFeeRate, uint256 newFeeRate);
event FeesSwept(address token, address recipient, uint256 amount);

Expand Down Expand Up @@ -65,9 +65,9 @@ contract FastBridgeV2ManagementTest is FastBridgeV2Test {
fastBridge.setCancelDelay(newCancelDelay);
}

function setProverTimeout(address caller, uint256 newProverTimeout) public {
function setDisputePenaltyTime(address caller, uint256 newDisputePenaltyTime) public {
vm.prank(caller);
fastBridge.setProverTimeout(newProverTimeout);
fastBridge.setDisputePenaltyTime(newDisputePenaltyTime);
}

function setProtocolFeeRate(address caller, uint256 newFeeRate) public {
Expand All @@ -94,7 +94,7 @@ contract FastBridgeV2ManagementTest is FastBridgeV2Test {

function test_defaultValues() public view {
assertEq(fastBridge.cancelDelay(), DEFAULT_CANCEL_DELAY);
assertEq(fastBridge.proverTimeout(), DEFAULT_PROVER_TIMEOUT);
assertEq(fastBridge.disputePenaltyTime(), DEFAULT_DISPUTE_PENALTY_TIME);
assertEq(fastBridge.protocolFeeRate(), 0);
}

Expand Down Expand Up @@ -273,32 +273,32 @@ contract FastBridgeV2ManagementTest is FastBridgeV2Test {
setCancelDelay(caller, 4 days);
}

// ════════════════════════════════════════════ SET PROVER TIMEOUT ═════════════════════════════════════════════════
// ═════════════════════════════════════════ SET DISPUTE PENALTY TIME ══════════════════════════════════════════════

function test_setProverTimeout() public {
function test_setDisputePenaltyTime() public {
vm.expectEmit(address(fastBridge));
emit ProverTimeoutUpdated(DEFAULT_PROVER_TIMEOUT, 1 days);
setProverTimeout(governor, 1 days);
assertEq(fastBridge.proverTimeout(), 1 days);
emit DisputePenaltyTimeUpdated(DEFAULT_DISPUTE_PENALTY_TIME, 1 days);
setDisputePenaltyTime(governor, 1 days);
assertEq(fastBridge.disputePenaltyTime(), 1 days);
}

function test_setProverTimeout_twice() public {
test_setProverTimeout();
function test_setDisputePenaltyTime_twice() public {
test_setDisputePenaltyTime();
vm.expectEmit(address(fastBridge));
emit ProverTimeoutUpdated(1 days, 2 days);
setProverTimeout(governor, 2 days);
assertEq(fastBridge.proverTimeout(), 2 days);
emit DisputePenaltyTimeUpdated(1 days, 2 days);
setDisputePenaltyTime(governor, 2 days);
assertEq(fastBridge.disputePenaltyTime(), 2 days);
}

function test_setProverTimeout_revertBelowMin() public {
vm.expectRevert(ProverTimeoutBelowMin.selector);
setProverTimeout(governor, MIN_PROVER_TIMEOUT - 1);
function test_setDisputePenaltyTime_revertBelowMin() public {
vm.expectRevert(DisputePenaltyTimeBelowMin.selector);
setDisputePenaltyTime(governor, MIN_DISPUTE_PENALTY_TIME - 1);
}

function test_setProverTimeout_revertNotGovernor(address caller) public {
function test_setDisputePenaltyTime_revertNotGovernor(address caller) public {
vm.assume(caller != governor);
expectUnauthorized(caller, fastBridge.GOVERNOR_ROLE());
setProverTimeout(caller, 1 days);
setDisputePenaltyTime(caller, 1 days);
}

// ═══════════════════════════════════════════ SET PROTOCOL FEE RATE ═══════════════════════════════════════════════
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts-rfq/test/FastBridgeV2.Src.Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract contract FastBridgeV2SrcBaseTest is FastBridgeV2Test {
uint256 public constant CLAIM_DELAY = 30 minutes;
// Use values different from the default to ensure it's being set correctly.
uint256 public constant PERMISSIONLESS_CANCEL_DELAY = 13.37 hours;
uint256 public constant PROVER_TIMEOUT = 4.2 minutes;
uint256 public constant DISPUTE_PENALTY_TIME = 4.2 minutes;

uint256 public constant LEFTOVER_BALANCE = 10 ether;
uint256 public constant INITIAL_PROTOCOL_FEES_TOKEN = 456_789;
Expand All @@ -35,7 +35,7 @@ abstract contract FastBridgeV2SrcBaseTest is FastBridgeV2Test {

fastBridge.grantRole(fastBridge.GOVERNOR_ROLE(), address(this));
fastBridge.setCancelDelay(PERMISSIONLESS_CANCEL_DELAY);
fastBridge.setProverTimeout(PROVER_TIMEOUT);
fastBridge.setDisputePenaltyTime(DISPUTE_PENALTY_TIME);
}

function mintTokens() public virtual override {
Expand Down
Loading

0 comments on commit 6fdaec3

Please sign in to comment.