From 860783508a4d61bab40e15fd6a8907a17a17adf0 Mon Sep 17 00:00:00 2001 From: zorzal Date: Fri, 8 Nov 2024 09:22:35 -0500 Subject: [PATCH] feat: use library instead of free variables --- .../modules/dispute/BondEscalationModule.sol | 18 ++++++++++--- .../BondEscalationResolutionModule.sol | 20 ++++++++------ .../resolution/ERC20ResolutionModule.sol | 9 ++++--- .../PrivateERC20ResolutionModule.sol | 11 +++++--- solidity/contracts/utils/ModuleTypehash.sol | 26 +++++++++++++++++++ solidity/contracts/utils/Typehash.sol | 22 ---------------- .../dispute/BondEscalationModule.t.sol | 8 +++--- 7 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 solidity/contracts/utils/ModuleTypehash.sol delete mode 100644 solidity/contracts/utils/Typehash.sol diff --git a/solidity/contracts/modules/dispute/BondEscalationModule.sol b/solidity/contracts/modules/dispute/BondEscalationModule.sol index 12807347..08906b5f 100644 --- a/solidity/contracts/modules/dispute/BondEscalationModule.sol +++ b/solidity/contracts/modules/dispute/BondEscalationModule.sol @@ -7,10 +7,10 @@ import {FixedPointMathLib} from 'solmate/src/utils/FixedPointMathLib.sol'; import {IBondEscalationModule} from '../../../interfaces/modules/dispute/IBondEscalationModule.sol'; -import {_PLEDGE_AGAINST_DISPUTE_TYPEHASH, _PLEDGE_FOR_DISPUTE_TYPEHASH} from '../../utils/Typehash.sol'; - import {ModuleAccessController} from '../access/ModuleAccessController.sol'; +import {ModuleTypehash} from '../../utils/ModuleTypehash.sol'; + contract BondEscalationModule is ModuleAccessController, IBondEscalationModule { /// @inheritdoc IBondEscalationModule mapping(bytes32 _requestId => mapping(address _pledger => uint256 pledges)) public pledgesForDispute; @@ -218,7 +218,12 @@ contract BondEscalationModule is ModuleAccessController, IBondEscalationModule { AccessControl calldata _accessControl ) external - hasAccess(_request.accessModule, _PLEDGE_FOR_DISPUTE_TYPEHASH, abi.encode(_request, _dispute), _accessControl) + hasAccess( + _request.accessModule, + ModuleTypehash._PLEDGE_FOR_DISPUTE_TYPEHASH, + abi.encode(_request, _dispute), + _accessControl + ) { bytes32 _disputeId = _getId(_dispute); RequestParameters memory _params = _pledgeChecks(_request, _dispute, true); @@ -245,7 +250,12 @@ contract BondEscalationModule is ModuleAccessController, IBondEscalationModule { AccessControl calldata _accessControl ) external - hasAccess(_request.accessModule, _PLEDGE_AGAINST_DISPUTE_TYPEHASH, abi.encode(_request, _dispute), _accessControl) + hasAccess( + _request.accessModule, + ModuleTypehash._PLEDGE_AGAINST_DISPUTE_TYPEHASH, + abi.encode(_request, _dispute), + _accessControl + ) { bytes32 _disputeId = _getId(_dispute); RequestParameters memory _params = _pledgeChecks(_request, _dispute, false); diff --git a/solidity/contracts/modules/resolution/BondEscalationResolutionModule.sol b/solidity/contracts/modules/resolution/BondEscalationResolutionModule.sol index f4504884..63002a90 100644 --- a/solidity/contracts/modules/resolution/BondEscalationResolutionModule.sol +++ b/solidity/contracts/modules/resolution/BondEscalationResolutionModule.sol @@ -18,11 +18,7 @@ import {IBondEscalationResolutionModule} from '../../../interfaces/modules/resolution/IBondEscalationResolutionModule.sol'; import {ModuleAccessController} from '../access/ModuleAccessController.sol'; -import { - _CLAIM_PLEDGE_TYPEHASH, - _PLEDGE_AGAINST_DISPUTE_TYPEHASH, - _PLEDGE_FOR_DISPUTE_TYPEHASH -} from '../../utils/Typehash.sol'; +import {ModuleTypehash} from '../../utils/ModuleTypehash.sol'; contract BondEscalationResolutionModule is ModuleAccessController, IBondEscalationResolutionModule { using SafeERC20 for IERC20; @@ -75,7 +71,7 @@ contract BondEscalationResolutionModule is ModuleAccessController, IBondEscalati external hasAccess( _request.accessModule, - _PLEDGE_FOR_DISPUTE_TYPEHASH, + ModuleTypehash._PLEDGE_FOR_DISPUTE_TYPEHASH, abi.encode(_request, _dispute, _pledgeAmount), _accessControl ) @@ -99,7 +95,7 @@ contract BondEscalationResolutionModule is ModuleAccessController, IBondEscalati external hasAccess( _request.accessModule, - _PLEDGE_AGAINST_DISPUTE_TYPEHASH, + ModuleTypehash._PLEDGE_AGAINST_DISPUTE_TYPEHASH, abi.encode(_request, _dispute, _pledgeAmount), _accessControl ) @@ -162,7 +158,15 @@ contract BondEscalationResolutionModule is ModuleAccessController, IBondEscalati IOracle.Request calldata _request, IOracle.Dispute calldata _dispute, AccessControl calldata _accessControl - ) external hasAccess(_request.accessModule, _CLAIM_PLEDGE_TYPEHASH, abi.encode(_request, _dispute), _accessControl) { + ) + external + hasAccess( + _request.accessModule, + ModuleTypehash._CLAIM_PLEDGE_TYPEHASH, + abi.encode(_request, _dispute), + _accessControl + ) + { bytes32 _disputeId = _validateDispute(_request, _dispute); Escalation storage _escalation = escalations[_disputeId]; diff --git a/solidity/contracts/modules/resolution/ERC20ResolutionModule.sol b/solidity/contracts/modules/resolution/ERC20ResolutionModule.sol index e14ba003..9e187af0 100644 --- a/solidity/contracts/modules/resolution/ERC20ResolutionModule.sol +++ b/solidity/contracts/modules/resolution/ERC20ResolutionModule.sol @@ -11,7 +11,7 @@ import {IOracle} from '@defi-wonderland/prophet-core/solidity/interfaces/IOracle import {IERC20ResolutionModule} from '../../../interfaces/modules/resolution/IERC20ResolutionModule.sol'; -import {_CAST_VOTE_TYPEHASH, _CLAIM_VOTE_TYPEHASH} from '../../utils/Typehash.sol'; +import {ModuleTypehash} from '../../utils/ModuleTypehash.sol'; import {ModuleAccessController} from '../access/ModuleAccessController.sol'; contract ERC20ResolutionModule is ModuleAccessController, IERC20ResolutionModule { @@ -62,7 +62,7 @@ contract ERC20ResolutionModule is ModuleAccessController, IERC20ResolutionModule external hasAccess( _request.accessModule, - _CAST_VOTE_TYPEHASH, + ModuleTypehash._CAST_VOTE_TYPEHASH, abi.encode(_request, _dispute, _numberOfVotes, _numberOfVotes), _accessControl ) @@ -138,7 +138,10 @@ contract ERC20ResolutionModule is ModuleAccessController, IERC20ResolutionModule IOracle.Request calldata _request, IOracle.Dispute calldata _dispute, AccessControl calldata _accessControl - ) external hasAccess(_request.accessModule, _CLAIM_VOTE_TYPEHASH, abi.encode(_request, _dispute), _accessControl) { + ) + external + hasAccess(_request.accessModule, ModuleTypehash._CLAIM_VOTE_TYPEHASH, abi.encode(_request, _dispute), _accessControl) + { bytes32 _disputeId = _validateDispute(_request, _dispute); Escalation memory _escalation = escalations[_disputeId]; diff --git a/solidity/contracts/modules/resolution/PrivateERC20ResolutionModule.sol b/solidity/contracts/modules/resolution/PrivateERC20ResolutionModule.sol index 09dd4d8c..d71705ea 100644 --- a/solidity/contracts/modules/resolution/PrivateERC20ResolutionModule.sol +++ b/solidity/contracts/modules/resolution/PrivateERC20ResolutionModule.sol @@ -6,7 +6,7 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol'; import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet.sol'; -import {_COMMIT_VOTE_TYPEHASH, _REVEAL_VOTE_TYPEHASH} from '../../utils/Typehash.sol'; +import {ModuleTypehash} from '../../utils/ModuleTypehash.sol'; import {IModule, Module} from '@defi-wonderland/prophet-core/solidity/contracts/Module.sol'; import {IOracle} from '@defi-wonderland/prophet-core/solidity/interfaces/IOracle.sol'; @@ -59,7 +59,12 @@ contract PrivateERC20ResolutionModule is ModuleAccessController, IPrivateERC20Re AccessControl calldata _accessControl ) external - hasAccess(_request.accessModule, _COMMIT_VOTE_TYPEHASH, abi.encode(_request, _dispute, _commitment), _accessControl) + hasAccess( + _request.accessModule, + ModuleTypehash._COMMIT_VOTE_TYPEHASH, + abi.encode(_request, _dispute, _commitment), + _accessControl + ) { bytes32 _disputeId = _validateDispute(_request, _dispute); if (ORACLE.disputeStatus(_disputeId) != IOracle.DisputeStatus.Escalated) { @@ -90,7 +95,7 @@ contract PrivateERC20ResolutionModule is ModuleAccessController, IPrivateERC20Re external hasAccess( _request.accessModule, - _REVEAL_VOTE_TYPEHASH, + ModuleTypehash._REVEAL_VOTE_TYPEHASH, abi.encode(_request, _dispute, _numberOfVotes, _salt), _accessControl ) diff --git a/solidity/contracts/utils/ModuleTypehash.sol b/solidity/contracts/utils/ModuleTypehash.sol new file mode 100644 index 00000000..37f44ec3 --- /dev/null +++ b/solidity/contracts/utils/ModuleTypehash.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +library ModuleTypehash { + bytes32 internal constant _PLEDGE_FOR_DISPUTE_TYPEHASH = + keccak256('pledgeForDispute(Request _request, Dispute _dispute,IAccessController.AccessControl _accessControl)'); + + bytes32 internal constant _PLEDGE_AGAINST_DISPUTE_TYPEHASH = + keccak256('pledgeAgainstDispute(Request _request,Dispute _dispute,IAccessController.AccessControl _accessControl)'); + + bytes32 internal constant _CLAIM_PLEDGE_TYPEHASH = + keccak256('claimPledge(Request _request,Dispute _dispute,AccessControl _accessControl)'); + + bytes32 internal constant _CLAIM_VOTE_TYPEHASH = + keccak256('claimVote(Request _request,Dispute _dispute,AccessControl _accessControl)'); + + bytes32 internal constant _CAST_VOTE_TYPEHASH = + keccak256('castVote(Request _request,Dispute _dispute,uint256 _numberOfVotes,AccessControl _accessControl)'); + + bytes32 internal constant _COMMIT_VOTE_TYPEHASH = + keccak256('commitVote(Request _request,Dispute _dispute,bytes32 _commitment,AccessControl _accessControl)'); + + bytes32 internal constant _REVEAL_VOTE_TYPEHASH = keccak256( + 'revealVote(Request _request,Dispute _dispute,uint256 _numberOfVotes,bytes32 _salt,AccessControl _accessControl)' + ); +} diff --git a/solidity/contracts/utils/Typehash.sol b/solidity/contracts/utils/Typehash.sol deleted file mode 100644 index a64ddead..00000000 --- a/solidity/contracts/utils/Typehash.sol +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.19; - -bytes32 constant _PLEDGE_FOR_DISPUTE_TYPEHASH = - keccak256('pledgeForDispute(Request _request, Dispute _dispute,IAccessController.AccessControl _accessControl)'); - -bytes32 constant _PLEDGE_AGAINST_DISPUTE_TYPEHASH = - keccak256('pledgeAgainstDispute(Request _request,Dispute _dispute,IAccessController.AccessControl _accessControl)'); - -bytes32 constant _CLAIM_PLEDGE_TYPEHASH = - keccak256('claimPledge(Request _request,Dispute _dispute,AccessControl _accessControl)'); - -bytes32 constant _CLAIM_VOTE_TYPEHASH = - keccak256('claimVote(Request _request,Dispute _dispute,AccessControl _accessControl)'); - -bytes32 constant _CAST_VOTE_TYPEHASH = - keccak256('castVote(Request _request,Dispute _dispute,uint256 _numberOfVotes,AccessControl _accessControl)'); -bytes32 constant _COMMIT_VOTE_TYPEHASH = - keccak256('commitVote(Request _request,Dispute _dispute,bytes32 _commitment,AccessControl _accessControl)'); -bytes32 constant _REVEAL_VOTE_TYPEHASH = keccak256( - 'revealVote(Request _request,Dispute _dispute,uint256 _numberOfVotes,bytes32 _salt,AccessControl _accessControl)' -); diff --git a/solidity/test/unit/modules/dispute/BondEscalationModule.t.sol b/solidity/test/unit/modules/dispute/BondEscalationModule.t.sol index 1f747e91..1adf211a 100644 --- a/solidity/test/unit/modules/dispute/BondEscalationModule.t.sol +++ b/solidity/test/unit/modules/dispute/BondEscalationModule.t.sol @@ -18,9 +18,7 @@ import { BondEscalationModule, IBondEscalationModule } from '../../../../contracts/modules/dispute/BondEscalationModule.sol'; -import { - _PLEDGE_AGAINST_DISPUTE_TYPEHASH, _PLEDGE_FOR_DISPUTE_TYPEHASH -} from '../../../../contracts/utils/Typehash.sol'; +import {ModuleTypehash} from '../../../../contracts/utils/ModuleTypehash.sol'; import {IAccountingExtension} from '../../../../interfaces/extensions/IAccountingExtension.sol'; import {IBondEscalationAccounting} from '../../../../interfaces/extensions/IBondEscalationAccounting.sol'; import {IModuleAccessController} from '../../../../interfaces/modules/access/IModuleAccessController.sol'; @@ -1552,7 +1550,7 @@ contract BondEscalationModule_Unit_AccessControl is BaseTest { IAccessModule.AccessControlParameters({ sender: _caller, accessControl: _accessControl, - typehash: _PLEDGE_FOR_DISPUTE_TYPEHASH, + typehash: ModuleTypehash._PLEDGE_FOR_DISPUTE_TYPEHASH, typehashParams: abi.encode(mockRequest, _dispute) }) ) @@ -1647,7 +1645,7 @@ contract BondEscalationModule_Unit_AccessControl is BaseTest { IAccessModule.AccessControlParameters({ sender: _caller, accessControl: _accessControl, - typehash: _PLEDGE_AGAINST_DISPUTE_TYPEHASH, + typehash: ModuleTypehash._PLEDGE_AGAINST_DISPUTE_TYPEHASH, typehashParams: abi.encode(mockRequest, _dispute) }) )