Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph Horizon: tests and internal review fixes #983

Merged
merged 9 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: add tests for DataServicePausable
Signed-off-by: Tomás Migone <tomas@edgeandnode.com>
tmigone committed Jun 12, 2024
commit da34d9bd7035a89d72878834c2de5de0a0162415
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ abstract contract DataServicePausable is Pausable, DataService, IDataServicePaus
* @param _pauseGuardian The address of the pause guardian
* @param _allowed The allowed status of the pause guardian
*/
function _setPauseGuardian(address _pauseGuardian, bool _allowed) internal whenNotPaused {
function _setPauseGuardian(address _pauseGuardian, bool _allowed) internal {
pauseGuardians[_pauseGuardian] = _allowed;
emit PauseGuardianSet(_pauseGuardian, _allowed);
}
3 changes: 3 additions & 0 deletions packages/horizon/test/GraphBase.t.sol
Original file line number Diff line number Diff line change
@@ -84,6 +84,9 @@ abstract contract GraphBaseTest is Utils, Constants {
vm.label({ account: address(escrow), newLabel: "PaymentsEscrow" });
vm.label({ account: address(staking), newLabel: "HorizonStaking" });
vm.label({ account: address(stakingExtension), newLabel: "HorizonStakingExtension" });

// Ensure caller is back to the original msg.sender
vm.stopPrank();
}

function deployProtocolContracts() private {
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

import "forge-std/Test.sol";
import {HorizonStakingSharedTest} from "../../shared/horizon-staking/HorizonStakingShared.t.sol";
import {DataServiceImpPausable} from "../implementations/DataServiceImpPausable.sol";
import {IDataServicePausable} from "./../../../contracts/data-service/interfaces/IDataServicePausable.sol";

contract DataServicePausableTest is HorizonStakingSharedTest {
DataServiceImpPausable dataService;

event Paused(address pauser);
event Unpaused(address unpauser);

function setUp() public override {
super.setUp();

dataService = new DataServiceImpPausable(address(controller));
}

modifier whenTheCallerIsAPauseGuardian() {
_assert_setPauseGuardian(address(this), true);
_;
}

function test_Pause_WhenTheProtocolIsNotPaused() external whenTheCallerIsAPauseGuardian {
_assert_pause();
}

function test_Pause_RevertWhen_TheProtocolIsPaused() external whenTheCallerIsAPauseGuardian {
_assert_pause();

vm.expectRevert(abi.encodeWithSignature("EnforcedPause()"));
dataService.pause();
assertEq(dataService.paused(), true);
}

function test_Pause_RevertWhen_TheCallerIsNotAPauseGuardian() external {
vm.expectRevert(abi.encodeWithSignature("DataServicePausableNotPauseGuardian(address)", address(this)));
dataService.pause();
assertEq(dataService.paused(), false);
}

function test_Unpause_WhenTheProtocolIsPaused() external whenTheCallerIsAPauseGuardian {
_assert_pause();
_assert_unpause();
}

function test_Unpause_RevertWhen_TheProtocolIsNotPaused() external whenTheCallerIsAPauseGuardian {
vm.expectRevert(abi.encodeWithSignature("ExpectedPause()"));
dataService.unpause();
assertEq(dataService.paused(), false);
}

function test_Unpause_RevertWhen_TheCallerIsNotAPauseGuardian() external {
_assert_setPauseGuardian(address(this), true);
_assert_pause();
_assert_setPauseGuardian(address(this), false);

vm.expectRevert(abi.encodeWithSignature("DataServicePausableNotPauseGuardian(address)", address(this)));
dataService.unpause();
assertEq(dataService.paused(), true);
}

function test_SetPauseGuardian_WhenSettingAPauseGuardian() external {
_assert_setPauseGuardian(address(this), true);
}

function test_SetPauseGuardian_WhenRemovingAPauseGuardian() external {
_assert_setPauseGuardian(address(this), true);
_assert_setPauseGuardian(address(this), false);
}

function _assert_pause() private {
vm.expectEmit();
emit Paused(address(this));
dataService.pause();
assertEq(dataService.paused(), true);
}

function _assert_unpause() private {
vm.expectEmit();
emit Unpaused(address(this));
dataService.unpause();
assertEq(dataService.paused(), false);
}

function _assert_setPauseGuardian(address pauseGuardian, bool allowed) private {
vm.expectEmit();
emit IDataServicePausable.PauseGuardianSet(pauseGuardian, allowed);
dataService.setPauseGuardian(pauseGuardian, allowed);
assertEq(dataService.pauseGuardians(pauseGuardian), allowed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DataServicePausableTest::pause_
├── when the caller is a pause guardian
│ ├── when the protocol is not paused
│ │ └── it should pause the contract
│ └── when the protocol is paused
│ └── it should revert
└── when the caller is not a pause guardian
└── it should revert

DataServicePausableTest::unpause_
├── when the caller is a pause guardian
│ ├── when the protocol is paused
│ │ └── it should unpause the contract
│ └── when the protocol is not paused
│ └── it should revert
└── when the caller is not a pause guardian
└── it should revert

DataServicePausableTest::setPauseGuardian_
├── when setting a pause guardian
│ └── it should emit an event
└── when removing a pause guardian
└── it should emit an event
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.26;

import { DataService } from "../../../contracts/data-service/DataService.sol";
import { DataServicePausable } from "../../../contracts/data-service/extensions/DataServicePausable.sol";
import { IGraphPayments } from "./../../../contracts/interfaces/IGraphPayments.sol";

contract DataServiceImpPausable is DataServicePausable {
uint32 public constant DELEGATION_RATIO = 100;
uint256 public constant PROVISION_TOKENS_MIN = 50;
uint256 public constant PROVISION_TOKENS_MAX = 5000;
uint32 public constant VERIFIER_CUT_MIN = 5;
uint32 public constant VERIFIER_CUT_MAX = 100000;
uint64 public constant THAWING_PERIOD_MIN = 15;
uint64 public constant THAWING_PERIOD_MAX = 76;

constructor(address controller) DataService(controller) initializer {
__DataService_init();
}

function register(address serviceProvider, bytes calldata data) external {}

function acceptProvision(address serviceProvider, bytes calldata data) external {}

function startService(address serviceProvider, bytes calldata data) external {}

function stopService(address serviceProvider, bytes calldata data) external {}

function collect(address serviceProvider, IGraphPayments.PaymentTypes feeType, bytes calldata data) external {}

function slash(address serviceProvider, bytes calldata data) external {}

function setPauseGuardian(address pauseGuardian, bool allowed) external {
_setPauseGuardian(pauseGuardian, allowed);
}
}