Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 17 additions & 5 deletions src/refs/CoreRefV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,29 @@ abstract contract CoreRefV2 is ICoreRefV2, Pausable {
/// 3. call core and unlock the lock back to starting level
modifier globalLock(uint8 level) {
IGlobalReentrancyLock lock = globalReentrancyLock();
lock.lock(level);
_;
lock.unlock(level - 1);

if (address(lock) != address(0)) {
lock.lock(level);
_;
lock.unlock(level - 1);
} else {
_; /// if lock is not set, allow function execution without global reentrancy locks
}
}

/// @notice modifier to restrict function acces to a certain lock level
modifier isGlobalReentrancyLocked(uint8 level) {
IGlobalReentrancyLock lock = globalReentrancyLock();

require(lock.lockLevel() == level, "CoreRef: System not at lock level");
_;
if (address(lock) != address(0)) {
require(
lock.lockLevel() == level,
"CoreRef: System not at lock level"
);
_;
} else {
_; /// if lock is not set, allow function execution without global lock level
}
}

/// @notice callable only by the Volt Minter
Expand Down
11 changes: 11 additions & 0 deletions test/mock/MockCoreRefV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ contract MockCoreRefV2 is CoreRefV2 {

function testSystemState() public onlyVoltRole(VoltRoles.LOCKER) {}

function testSystemLocksToLevel1() public globalLock(1) {}

function testSystemLocksToLevel2() public globalLock(2) {}

/// invalid lock level, doesn't matter because the lock is disabled in test
function testSystemLocksToLevel3() public globalLock(3) {}

function testSystemLockLevel1() public isGlobalReentrancyLocked(1) {}

function testSystemLockLevel2() public isGlobalReentrancyLocked(2) {}

function testStateGovernorMinter()
public
hasAnyOfThreeRoles(
Expand Down
6 changes: 3 additions & 3 deletions test/unit/core/GlobalReentrancyLock.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ contract UnitTestGlobalReentrancyLock is Test {
assertTrue(core.isGovernor(address(core))); /// core contract is governor
assertTrue(core.isGovernor(addresses.governorAddress)); /// msg.sender of contract is governor

assertTrue(lock.isUnlocked()); /// core starts out unlocked
assertTrue(!lock.isLocked()); /// core starts out not locked
assertTrue(lock.isUnlocked()); /// lock starts out unlocked
assertTrue(!lock.isLocked()); /// lock starts out not locked
assertEq(lock.lastSender(), address(0));
assertEq(lock.lastBlockEntered(), 0);

Expand Down Expand Up @@ -219,7 +219,7 @@ contract UnitTestGlobalReentrancyLock is Test {

assertTrue(!lock.isLocked());
assertEq(lock.lockLevel(), 0);
assertTrue(lock.isUnlocked()); /// core is fully unlocked
assertTrue(lock.isUnlocked()); /// lock is fully unlocked
assertEq(toPrank, lock.lastSender());

vm.roll(block.number + 1);
Expand Down
33 changes: 32 additions & 1 deletion test/unit/refs/CoreRefV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {Test} from "@forge-std/Test.sol";
import {ICoreV2} from "@voltprotocol/core/ICoreV2.sol";
import {VoltRoles} from "@voltprotocol/core/VoltRoles.sol";
import {MockERC20} from "@test/mock/MockERC20.sol";
import {getCoreV2} from "@test/unit/utils/Fixtures.sol";
import {IPCVOracle} from "@voltprotocol/oracle/IPCVOracle.sol";
import {MockCoreRefV2} from "@test/mock/MockCoreRefV2.sol";
import {TestAddresses as addresses} from "@test/unit/utils/TestAddresses.sol";
import {getCoreV2} from "@test/unit/utils/Fixtures.sol";
import {IGlobalReentrancyLock} from "@voltprotocol/core/IGlobalReentrancyLock.sol";

contract UnitTestCoreRefV2 is Test {
ICoreV2 private core;
Expand Down Expand Up @@ -113,6 +114,36 @@ contract UnitTestCoreRefV2 is Test {
coreRef.testSystemState();
}

function _disableLock() private {
vm.prank(addresses.governorAddress);
core.setGlobalReentrancyLock(IGlobalReentrancyLock(address(0)));
}

function testLockLevel1LockDisabled() public {
_disableLock();
coreRef.testSystemLocksToLevel1();
}

function testLockLevel2LockDisabled() public {
_disableLock();
coreRef.testSystemLocksToLevel2();
}

function testLockLevel3LockDisabled() public {
_disableLock();
coreRef.testSystemLocksToLevel3();
}

function testSystemLockLevel1LockDisabled() public {
_disableLock();
coreRef.testSystemLockLevel1();
}

function testSystemLockLevel2LockDisabled() public {
_disableLock();
coreRef.testSystemLockLevel2();
}

function testGuardianAsGuardian() public {
vm.prank(addresses.guardianAddress);
coreRef.testGuardian();
Expand Down