generated from defi-wonderland/solidity-foundry-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity =0.8.19; | ||
|
||
import {BaseGuard} from 'safe-contracts/base/GuardManager.sol'; | ||
import {Enum} from 'safe-contracts/common/Enum.sol'; | ||
import {IVerifierModule} from 'interfaces/IVerifierModule.sol'; | ||
|
||
/** | ||
* @title NeedsUpdateGuard | ||
* @notice This guard should prevent the safe from executing any transaction if an update is needed. | ||
* @notice An update is needed based on the safe owner's time inputed on how long an update is trusted. | ||
*/ | ||
contract NeedsUpdateGuard is BaseGuard { | ||
/** | ||
* @notice Emits when the owner changes the trustLatestUpdateForSeconds | ||
* @param _safe The address of the safe | ||
* @param _trustLatestUpdateForSeconds The new trustLatestUpdateForSeconds, how many seconds the safe trusts the last update | ||
*/ | ||
event TrustLatestUpdateForSecondsChanged(address indexed _safe, uint256 _trustLatestUpdateForSeconds); | ||
|
||
/** | ||
* @notice Throws if the safe needs an update | ||
*/ | ||
error NeedsUpdateGuard_NeedsUpdate(); | ||
|
||
/** | ||
* @notice The verifier module | ||
*/ | ||
IVerifierModule public immutable VERIFIER_MODULE; | ||
|
||
/** | ||
* @notice How many seconds the safe trusts the last update | ||
*/ | ||
mapping(address => uint256) public trustLatestUpdateForSeconds; | ||
|
||
constructor(IVerifierModule _verifierModule) { | ||
VERIFIER_MODULE = _verifierModule; | ||
} | ||
|
||
/** | ||
* @notice Guard hook that is called before a Safe transaction is executed | ||
* @dev This function should revert if the safe needs an update | ||
* @dev WARNING: This can brick the safe if the owner doesn't update the settings or change the trustLatestUpdateForSeconds | ||
*/ | ||
// solhint-disable no-unused-vars | ||
function checkTransaction( | ||
address _to, | ||
uint256 _value, | ||
bytes memory _data, | ||
Enum.Operation _operation, | ||
uint256 _safeTxGas, | ||
uint256 _baseGas, | ||
uint256 _gasPrice, | ||
address _gasToken, | ||
address payable _refundReceiver, | ||
bytes memory _signatures, | ||
address _msgSender | ||
) external { | ||
uint256 _lastVerifiedUpdateTimestamp = VERIFIER_MODULE.latestVerifiedSettingsTimestamp(_msgSender); | ||
uint256 _trustLatestUpdateForSeconds = trustLatestUpdateForSeconds[_msgSender]; | ||
|
||
if (_lastVerifiedUpdateTimestamp + _trustLatestUpdateForSeconds < block.timestamp) { | ||
revert NeedsUpdateGuard_NeedsUpdate(); | ||
} | ||
} | ||
|
||
/** | ||
* @notice Guard hook that is called after a Safe transaction is executed | ||
*/ | ||
function checkAfterExecution(bytes32 _txHash, bool _success) external {} | ||
|
||
/** | ||
* @notice Should update the safe's trustLatestUpdateForSeconds | ||
* @dev This function should be called by the safe | ||
* @param _trustLatestUpdateForSeconds The new trustLatestUpdateForSeconds, how many seconds the safe trusts the last verified update | ||
*/ | ||
function updateTrustLatestUpdateForSeconds(uint256 _trustLatestUpdateForSeconds) external { | ||
trustLatestUpdateForSeconds[msg.sender] = _trustLatestUpdateForSeconds; | ||
emit TrustLatestUpdateForSecondsChanged(msg.sender, _trustLatestUpdateForSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.4 <0.9.0; | ||
|
||
import {Test} from 'forge-std/Test.sol'; | ||
import {Enum} from 'safe-contracts/common/Enum.sol'; | ||
import {NeedsUpdateGuard} from 'contracts/NeedsUpdateGuard.sol'; | ||
import {IVerifierModule} from 'interfaces/IVerifierModule.sol'; | ||
|
||
abstract contract Base is Test { | ||
event TrustLatestUpdateForSecondsChanged(address indexed _safe, uint256 _trustLatestUpdateForSeconds); | ||
|
||
error NeedsUpdateGuard_NeedsUpdate(); | ||
|
||
address public safe; | ||
IVerifierModule public verifierModule; | ||
NeedsUpdateGuard public needsUpdateGuard; | ||
|
||
function setUp() public { | ||
safe = makeAddr('safe'); | ||
verifierModule = IVerifierModule(makeAddr('verifierModule')); | ||
needsUpdateGuard = new NeedsUpdateGuard(verifierModule); | ||
// Warp to 2022-01-01 00:00:00 UTC | ||
vm.warp(1_641_070_800); | ||
} | ||
} | ||
|
||
contract UnitNeedsUpdateGuard is Base { | ||
function testCheckTransaction(address _to, uint256 _value, bytes memory _data) public { | ||
// Set trustLatestUpdateForSeconds | ||
vm.prank(safe); | ||
needsUpdateGuard.updateTrustLatestUpdateForSeconds(200); | ||
|
||
// Mock latest verified settings timestamp to current timestamp - 100 | ||
uint256 _currentTimeStamp = block.timestamp; | ||
vm.mockCall( | ||
address(verifierModule), | ||
abi.encodeWithSelector(IVerifierModule.latestVerifiedSettingsTimestamp.selector, safe), | ||
abi.encode(_currentTimeStamp - 100) | ||
); | ||
|
||
vm.expectCall( | ||
address(verifierModule), abi.encodeWithSelector(IVerifierModule.latestVerifiedSettingsTimestamp.selector, safe) | ||
); | ||
|
||
vm.prank(safe); | ||
needsUpdateGuard.checkTransaction( | ||
_to, _value, _data, Enum.Operation.Call, 0, 0, 0, address(0), payable(0), '', safe | ||
); | ||
} | ||
|
||
function testCheckTransactionReverts(address _to, uint256 _value, bytes memory _data) public { | ||
// Set trustLatestUpdateForSeconds | ||
vm.prank(safe); | ||
needsUpdateGuard.updateTrustLatestUpdateForSeconds(200); | ||
|
||
// Mock latest verified settings timestamp to current timestamp - 1_000 to make the check fail | ||
uint256 _currentTimeStamp = block.timestamp; | ||
vm.mockCall( | ||
address(verifierModule), | ||
abi.encodeWithSelector(IVerifierModule.latestVerifiedSettingsTimestamp.selector, safe), | ||
abi.encode(_currentTimeStamp - 1000) | ||
); | ||
|
||
vm.expectCall( | ||
address(verifierModule), abi.encodeWithSelector(IVerifierModule.latestVerifiedSettingsTimestamp.selector, safe) | ||
); | ||
|
||
vm.expectRevert(abi.encodeWithSelector(NeedsUpdateGuard.NeedsUpdateGuard_NeedsUpdate.selector)); | ||
|
||
vm.prank(safe); | ||
needsUpdateGuard.checkTransaction( | ||
_to, _value, _data, Enum.Operation.Call, 0, 0, 0, address(0), payable(0), '', safe | ||
); | ||
} | ||
|
||
function testCheckAfterExecution(bytes32 _txHash) public { | ||
vm.prank(safe); | ||
needsUpdateGuard.checkAfterExecution(_txHash, true); | ||
} | ||
|
||
function testUpdateTrustLatestUpdateForSeconds(uint256 _newTrustLatestUpdateForSeconds) public { | ||
vm.expectEmit(true, true, true, true); | ||
emit TrustLatestUpdateForSecondsChanged(safe, _newTrustLatestUpdateForSeconds); | ||
|
||
vm.prank(safe); | ||
needsUpdateGuard.updateTrustLatestUpdateForSeconds(_newTrustLatestUpdateForSeconds); | ||
|
||
assertEq( | ||
needsUpdateGuard.trustLatestUpdateForSeconds(safe), | ||
_newTrustLatestUpdateForSeconds, | ||
'Should update trustLatestUpdateForSeconds' | ||
); | ||
} | ||
} |