Skip to content

Commit

Permalink
feat: validateParameters (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmoebius authored Dec 15, 2023
1 parent 5c3fc07 commit 480dba7
Show file tree
Hide file tree
Showing 30 changed files with 413 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"package.json": "sort-package-json"
},
"dependencies": {
"@defi-wonderland/prophet-core-contracts": "0.0.0-49fc8fa6",
"@defi-wonderland/prophet-core-contracts": "0.0.0-6ac0f50d",
"@openzeppelin/contracts": "^4.9.3",
"solmate": "https://github.com/transmissions11/solmate.git#bfc9c25865a274a7827fea5abf6e4fb64fc64e6c"
},
Expand Down
13 changes: 13 additions & 0 deletions solidity/contracts/modules/dispute/BondEscalationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,17 @@ contract BondEscalationModule is Module, IBondEscalationModule {
function getEscalation(bytes32 _requestId) public view returns (BondEscalation memory _escalation) {
_escalation = _escalations[_requestId];
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& _params.bondSize != 0 && _params.bondEscalationDeadline != 0 && _params.maxNumberOfEscalations != 0
&& _params.tyingBuffer != 0 && _params.disputeWindow != 0;
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/dispute/BondedDisputeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,16 @@ contract BondedDisputeModule is Module, IBondedDisputeModule {

emit DisputeStatusChanged({_disputeId: _disputeId, _dispute: _dispute, _status: _status});
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& _params.bondSize != 0;
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/dispute/CircuitResolverModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ contract CircuitResolverModule is Module, ICircuitResolverModule {

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& _params.bondSize != 0 && address(_params.verifier) != address(0) && _params.callData.length != 0;
}
}
13 changes: 13 additions & 0 deletions solidity/contracts/modules/dispute/RootVerificationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ contract RootVerificationModule is Module, IRootVerificationModule {

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& address(_params.treeVerifier) != address(0) && _params.bondSize != 0 && _params.treeData.length != 0
&& _params.leavesToInsert.length != 0;
}
}
11 changes: 11 additions & 0 deletions solidity/contracts/modules/finality/CallbackModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ contract CallbackModule is Module, ICallbackModule {
emit Callback(_response.requestId, _params.target, _params.data);
emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.target) != address(0) && _params.data.length != 0;
}
}
25 changes: 25 additions & 0 deletions solidity/contracts/modules/finality/MultipleCallbacksModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,29 @@ contract MultipleCallbacksModule is Module, IMultipleCallbacksModule {

emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = true;

for (uint256 _i; _i < _params.targets.length; ++_i) {
if (_params.targets[_i] == address(0)) {
_valid = false;
break;
}
}

for (uint256 _i; _i < _params.data.length; ++_i) {
if (_params.data[_i].length == 0) {
_valid = false;
break;
}
}
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/request/ContractCallRequestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ contract ContractCallRequestModule is Module, IContractCallRequestModule {

emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.paymentToken) != address(0)
&& _params.target != address(0) && _params.paymentAmount != 0 && _params.functionSelector != bytes4(0);
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/request/HttpRequestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ contract HttpRequestModule is Module, IHttpRequestModule {

emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.paymentToken) != address(0)
&& _params.paymentAmount != 0 && bytes(_params.url).length != 0 && bytes(_params.body).length != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ contract SparseMerkleTreeRequestModule is Module, ISparseMerkleTreeRequestModule

emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.paymentToken) != address(0)
&& address(_params.treeVerifier) != address(0) && _params.paymentAmount != 0 && _params.treeData.length != 0
&& _params.leavesToInsert.length != 0;
}
}
11 changes: 11 additions & 0 deletions solidity/contracts/modules/resolution/ArbitratorModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ contract ArbitratorModule is Module, IArbitratorModule {

emit DisputeResolved(_dispute.requestId, _disputeId, _status);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = _params.arbitrator != address(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,17 @@ contract BondEscalationResolutionModule is Module, IBondEscalationResolutionModu
_resolution: _resolution
});
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& _params.percentageDiff != 0 && _params.pledgeThreshold != 0 && _params.timeUntilDeadline != 0
&& _params.timeToBreakInequality != 0;
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/resolution/ERC20ResolutionModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,16 @@ contract ERC20ResolutionModule is Module, IERC20ResolutionModule {
function getVoters(bytes32 _disputeId) external view returns (address[] memory __voters) {
__voters = _voters[_disputeId].values();
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.votingToken) != address(0)
&& _params.minVotesForQuorum != 0 && _params.timeUntilDeadline != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,16 @@ contract PrivateERC20ResolutionModule is Module, IPrivateERC20ResolutionModule {
) external view returns (bytes32 _commitment) {
_commitment = keccak256(abi.encode(msg.sender, _disputeId, _numberOfVotes, _salt));
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.votingToken) != address(0)
&& _params.minVotesForQuorum != 0 && _params.committingTimeWindow != 0 && _params.revealingTimeWindow != 0;
}
}
12 changes: 12 additions & 0 deletions solidity/contracts/modules/response/BondedResponseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ contract BondedResponseModule is Module, IBondedResponseModule {

emit RequestFinalized(_response.requestId, _response, _finalizer);
}

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters)
external
pure
override(Module, IModule)
returns (bool _valid)
{
RequestParameters memory _params = decodeRequestData(_encodedParameters);
_valid = address(_params.accountingExtension) != address(0) && address(_params.bondToken) != address(0)
&& _params.bondSize != 0 && _params.disputeWindow != 0 && _params.deadline != 0;
}
}
15 changes: 15 additions & 0 deletions solidity/test/unit/modules/dispute/BondEscalationModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ contract BondEscalationModule_Unit_ModuleData is BaseTest {
assertEq(_params.tyingBuffer, _decodedParams.tyingBuffer);
assertEq(_params.disputeWindow, _decodedParams.disputeWindow);
}

/**
* @notice Test that the validateParameters function correctly checks the parameters
*/
function test_validateParameters(IBondEscalationModule.RequestParameters calldata _params) public {
if (
address(_params.accountingExtension) == address(0) || address(_params.bondToken) == address(0)
|| _params.bondSize == 0 || _params.bondEscalationDeadline == 0 || _params.maxNumberOfEscalations == 0
|| _params.tyingBuffer == 0 || _params.disputeWindow == 0
) {
assertFalse(bondEscalationModule.validateParameters(abi.encode(_params)));
} else {
assertTrue(bondEscalationModule.validateParameters(abi.encode(_params)));
}
}
}

contract BondEscalationModule_Unit_EscalateDispute is BaseTest {
Expand Down
20 changes: 17 additions & 3 deletions solidity/test/unit/modules/dispute/BondedDisputeModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ contract BaseTest is Test, Helpers {
}
}

contract BondedResponseModule_Unit_ModuleData is BaseTest {
contract BondedDisputeModule_Unit_ModuleData is BaseTest {
/**
* @notice Test that the decodeRequestData function returns the correct values
*/
Expand All @@ -76,9 +76,23 @@ contract BondedResponseModule_Unit_ModuleData is BaseTest {
function test_moduleNameReturnsName() public {
assertEq(bondedDisputeModule.moduleName(), 'BondedDisputeModule');
}

/**
* @notice Test that the validateParameters function correctly checks the parameters
*/
function test_validateParameters(IBondedDisputeModule.RequestParameters calldata _params) public {
if (
address(_params.accountingExtension) == address(0) || address(_params.bondToken) == address(0)
|| _params.bondSize == 0
) {
assertFalse(bondedDisputeModule.validateParameters(abi.encode(_params)));
} else {
assertTrue(bondedDisputeModule.validateParameters(abi.encode(_params)));
}
}
}

contract BondedResponseModule_Unit_OnDisputeStatusChange is BaseTest {
contract BondedDisputeModule_Unit_OnDisputeStatusChange is BaseTest {
/**
* @notice Dispute lost by disputer
*/
Expand Down Expand Up @@ -221,7 +235,7 @@ contract BondedResponseModule_Unit_OnDisputeStatusChange is BaseTest {
}
}

contract BondedResponseModule_Unit_DisputeResponse is BaseTest {
contract BondedDisputeModule_Unit_DisputeResponse is BaseTest {
/**
* @notice Test if dispute response returns the correct status
*/
Expand Down
14 changes: 14 additions & 0 deletions solidity/test/unit/modules/dispute/CircuitResolverModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ contract CircuitResolverModule_Unit_ModuleData is BaseTest {
function test_moduleName() public {
assertEq(circuitResolverModule.moduleName(), 'CircuitResolverModule');
}

/**
* @notice Test that the validateParameters function correctly checks the parameters
*/
function test_validateParameters(ICircuitResolverModule.RequestParameters calldata _params) public {
if (
address(_params.accountingExtension) == address(0) || address(_params.bondToken) == address(0)
|| _params.bondSize == 0 || address(_params.verifier) == address(0) || _params.callData.length == 0
) {
assertFalse(circuitResolverModule.validateParameters(abi.encode(_params)));
} else {
assertTrue(circuitResolverModule.validateParameters(abi.encode(_params)));
}
}
}

contract CircuitResolverModule_Unit_DisputeResponse is BaseTest {
Expand Down
15 changes: 15 additions & 0 deletions solidity/test/unit/modules/dispute/RootVerificationModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ contract RootVerificationModule_Unit_ModuleData is BaseTest {
assertEq(address(_params.bondToken), _randomToken, 'Mismatch: decoded token');
assertEq(_params.bondSize, _bondSize, 'Mismatch: decoded bond size');
}

/**
* @notice Test that the validateParameters function correctly checks the parameters
*/
function test_validateParameters(IRootVerificationModule.RequestParameters calldata _params) public {
if (
address(_params.accountingExtension) == address(0) || address(_params.bondToken) == address(0)
|| address(_params.treeVerifier) == address(0) || _params.bondSize == 0 || _params.treeData.length == 0
|| _params.leavesToInsert.length == 0
) {
assertFalse(rootVerificationModule.validateParameters(abi.encode(_params)));
} else {
assertTrue(rootVerificationModule.validateParameters(abi.encode(_params)));
}
}
}

contract RootVerificationModule_Unit_DisputeResponse is BaseTest {
Expand Down
11 changes: 11 additions & 0 deletions solidity/test/unit/modules/finality/CallbackModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ contract CallbackModule_Unit_ModuleData is BaseTest {
assertEq(_params.target, _target);
assertEq(_params.data, _data);
}

/**
* @notice Test that the validateParameters function correctly checks the parameters
*/
function test_validateParameters(ICallbackModule.RequestParameters calldata _params) public {
if (address(_params.target) == address(0) || _params.data.length == 0) {
assertFalse(callbackModule.validateParameters(abi.encode(_params)));
} else {
assertTrue(callbackModule.validateParameters(abi.encode(_params)));
}
}
}

contract CallbackModule_Unit_FinalizeRequest is BaseTest {
Expand Down
Loading

0 comments on commit 480dba7

Please sign in to comment.