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

perf: optimize BondedDisputeModule #14

Merged
merged 5 commits into from
Nov 12, 2023
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
2 changes: 1 addition & 1 deletion docs/src/content/modules/dispute/bonded_dispute_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The Bonded Dispute Module is a contract that allows users to dispute a proposed

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request.
- `decodeRequestData(bytes calldata _data)`: Returns the decoded data for a request.
- `disputeResponse(bytes32 _requestId, bytes32 _responseId, address _disputer, address _proposer)`: Starts a dispute.
- `onDisputeStatusChange(bytes32 _disputeId, IOracle.Dispute memory _dispute)`: Is a hook called by the oracle when a dispute status has been updated.
- `disputeEscalated(bytes32 _disputeId)`: Called by the oracle when a dispute has been escalated. Not implemented in this module.
Expand Down
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-a1d2cc55",
"@defi-wonderland/prophet-core-contracts": "0.0.0-1ae08a81",
"@defi-wonderland/solidity-utils": "0.0.0-3e9c8e8b",
"@openzeppelin/contracts": "^4.9.3",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
Expand Down
197 changes: 94 additions & 103 deletions solidity/contracts/modules/dispute/BondedDisputeModule.sol
Original file line number Diff line number Diff line change
@@ -1,115 +1,106 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// // solhint-disable-next-line no-unused-import
// import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
// solhint-disable-next-line no-unused-import
import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

// import {IBondedDisputeModule} from '../../../interfaces/modules/dispute/IBondedDisputeModule.sol';
import {IBondedDisputeModule} from '../../../interfaces/modules/dispute/IBondedDisputeModule.sol';

// contract BondedDisputeModule is Module, IBondedDisputeModule {
// constructor(IOracle _oracle) Module(_oracle) {}
contract BondedDisputeModule is Module, IBondedDisputeModule {
constructor(IOracle _oracle) Module(_oracle) {}

// /// @inheritdoc IModule
// function moduleName() external pure returns (string memory _moduleName) {
// return 'BondedDisputeModule';
// }
/// @inheritdoc IModule
function moduleName() external pure returns (string memory _moduleName) {
return 'BondedDisputeModule';
}

// /// @inheritdoc IBondedDisputeModule
// function decodeRequestData(bytes32 _requestId) public view returns (RequestParameters memory _params) {
// _params = abi.decode(requestData[_requestId], (RequestParameters));
// }
/// @inheritdoc IBondedDisputeModule
function decodeRequestData(bytes calldata _data) public view returns (RequestParameters memory _params) {
_params = abi.decode(_data, (RequestParameters));
}

// /// @inheritdoc IBondedDisputeModule
// function disputeEscalated(bytes32 _disputeId) external onlyOracle {}
/// @inheritdoc IBondedDisputeModule
function disputeResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response,

Check warning on line 26 in solidity/contracts/modules/dispute/BondedDisputeModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_response" is unused
IOracle.Dispute calldata _dispute
) external onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.disputeModuleData);

// /// @inheritdoc IBondedDisputeModule
// function disputeResponse(
// bytes32 _requestId,
// bytes32 _responseId,
// address _disputer,
// address _proposer
// ) external onlyOracle returns (IOracle.Dispute memory _dispute) {
// _dispute = IOracle.Dispute({
// disputer: _disputer,
// responseId: _responseId,
// proposer: _proposer,
// requestId: _requestId,
// status: IOracle.DisputeStatus.Active,
// createdAt: block.timestamp
// });
_params.accountingExtension.bond({
_bonder: _dispute.disputer,
_requestId: _dispute.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});

// RequestParameters memory _params = decodeRequestData(_requestId);
// _params.accountingExtension.bond({
// _bonder: _disputer,
// _requestId: _requestId,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
emit ResponseDisputed({
_requestId: _dispute.requestId,
_responseId: _dispute.responseId,
_disputeId: _getId(_dispute),
_dispute: _dispute,
_blockNumber: block.number
});
}

// emit ResponseDisputed(_requestId, _responseId, _disputer, _proposer);
// }
/// @inheritdoc IBondedDisputeModule
function onDisputeStatusChange(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,

Check warning on line 51 in solidity/contracts/modules/dispute/BondedDisputeModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_response" is unused
IOracle.Dispute calldata _dispute
) external onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.disputeModuleData);
IOracle.DisputeStatus _status = ORACLE.disputeStatus(_disputeId);

// /// @inheritdoc IBondedDisputeModule
// function onDisputeStatusChange(bytes32, /* _disputeId */ IOracle.Dispute memory _dispute) external onlyOracle {
// RequestParameters memory _params = decodeRequestData(_dispute.requestId);
// IOracle.DisputeStatus _status = _dispute.status;
// address _proposer = _dispute.proposer;
// address _disputer = _dispute.disputer;
if (_status == IOracle.DisputeStatus.NoResolution) {
// No resolution, we release both bonds
_params.accountingExtension.release({
_bonder: _dispute.disputer,
_requestId: _dispute.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});

// if (_status == IOracle.DisputeStatus.NoResolution) {
// // No resolution, we release both bonds
// _params.accountingExtension.release({
// _bonder: _disputer,
// _requestId: _dispute.requestId,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
_params.accountingExtension.release({
_bonder: _dispute.proposer,
_requestId: _dispute.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});
} else if (_status == IOracle.DisputeStatus.Won) {
// Disputer won, we pay the disputer and release their bond
_params.accountingExtension.pay({
_requestId: _dispute.requestId,
_payer: _dispute.proposer,
_receiver: _dispute.disputer,
_token: _params.bondToken,
_amount: _params.bondSize
});
_params.accountingExtension.release({
_bonder: _dispute.disputer,
_requestId: _dispute.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});
} else if (_status == IOracle.DisputeStatus.Lost) {
// Disputer lost, we pay the proposer and release their bond
_params.accountingExtension.pay({
_requestId: _dispute.requestId,
_payer: _dispute.disputer,
_receiver: _dispute.proposer,
_token: _params.bondToken,
_amount: _params.bondSize
});
_params.accountingExtension.release({
_bonder: _dispute.proposer,
_requestId: _dispute.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});
}

// _params.accountingExtension.release({
// _bonder: _proposer,
// _requestId: _dispute.requestId,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// } else if (_status == IOracle.DisputeStatus.Won) {
// // Disputer won, we pay the disputer and release their bond
// _params.accountingExtension.pay({
// _requestId: _dispute.requestId,
// _payer: _proposer,
// _receiver: _disputer,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// _params.accountingExtension.release({
// _bonder: _disputer,
// _requestId: _dispute.requestId,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// } else if (_status == IOracle.DisputeStatus.Lost) {
// // Disputer lost, we pay the proposer and release their bond
// _params.accountingExtension.pay({
// _requestId: _dispute.requestId,
// _payer: _disputer,
// _receiver: _proposer,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// _params.accountingExtension.release({
// _bonder: _proposer,
// _requestId: _dispute.requestId,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// }

// emit DisputeStatusChanged({
// _requestId: _dispute.requestId,
// _responseId: _dispute.responseId,
// _disputer: _disputer,
// _proposer: _proposer,
// _status: _status
// });
// }
// }
emit DisputeStatusChanged({_disputeId: _disputeId, _dispute: _dispute, _status: _status});
}
}
131 changes: 64 additions & 67 deletions solidity/interfaces/modules/dispute/IBondedDisputeModule.sol
Original file line number Diff line number Diff line change
@@ -1,75 +1,72 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
// import {IDisputeModule} from
// '@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/dispute/IDisputeModule.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
import {IDisputeModule} from
'@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/dispute/IDisputeModule.sol';

// import {IAccountingExtension} from '../../extensions/IAccountingExtension.sol';
import {IAccountingExtension} from '../../extensions/IAccountingExtension.sol';

// /*
// * @title BondedDisputeModule
// * @notice Module allowing users to dispute a proposed response
// * by bonding tokens. According to the result of the dispute,
// * the tokens are either returned to the disputer or to the proposer.
// */
// interface IBondedDisputeModule is IDisputeModule {
// /*///////////////////////////////////////////////////////////////
// STRUCTS
// //////////////////////////////////////////////////////////////*/
/*
* @title BondedDisputeModule
* @notice Module allowing users to dispute a proposed response
* by bonding tokens. According to the result of the dispute,
* the tokens are either returned to the disputer or to the proposer.
*/
interface IBondedDisputeModule is IDisputeModule {
/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Parameters of the request as stored in the module
// * @param ipfsHash The hash of the CID from IPFS
// * @param requestModule The address of the request module
// * @param responseModule The address of the response module
// */
// struct RequestParameters {
// IAccountingExtension accountingExtension;
// IERC20 bondToken;
// uint256 bondSize;
// }
/**
* @notice Parameters of the request as stored in the module
* @param ipfsHash The hash of the CID from IPFS
* @param requestModule The address of the request module
* @param responseModule The address of the response module
*/
struct RequestParameters {
IAccountingExtension accountingExtension;
IERC20 bondToken;
uint256 bondSize;
}

// /*///////////////////////////////////////////////////////////////
// LOGIC
// //////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Returns the decoded data for a request
// * @param _requestId The ID of the request
// * @return _params The struct containing the parameters for the request
// */
// function decodeRequestData(bytes32 _requestId) external view returns (RequestParameters memory _params);
/**
* @notice Returns the decoded data for a request
* @param _data The encoded request parameters
* @return _params The struct containing the parameters for the request
*/
function decodeRequestData(bytes calldata _data) external view returns (RequestParameters memory _params);

// /**
// * @notice Called by the oracle when a dispute has been made on a response.
// * Bonds the tokens of the disputer.
// * @param _requestId The ID of the request whose response is disputed
// * @param _responseId The ID of the response being disputed
// * @param _disputer The address of the user who disputed the response
// * @param _proposer The address of the user who proposed the disputed response
// * @return _dispute The dispute on the proposed response
// */
// function disputeResponse(
// bytes32 _requestId,
// bytes32 _responseId,
// address _disputer,
// address _proposer
// ) external returns (IOracle.Dispute memory _dispute);
/**
* @notice Called by the oracle when a dispute has been made on a response
*
* @dev Bonds the tokens of the disputer
* @param _request The request a dispute has been submitted for
* @param _response The response that is being disputed
* @param _dispute The dispute that is being submitted
*/
function disputeResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external;

// /**
// * @notice Called by the oracle when a dispute status has been updated.
// * According to the result of the dispute, bonds are released to the proposer or
// * paid to the disputer.
// * @param _disputeId The ID of the dispute being updated
// * @param _dispute The dispute object
// */
// function onDisputeStatusChange(bytes32 _disputeId, IOracle.Dispute memory _dispute) external;

// /**
// * @notice Called by the oracle when a dispute has been escalated. Not implemented in this module
// * @param _disputeId The ID of the dispute being escalated
// */
// function disputeEscalated(bytes32 _disputeId) external;
// }
/**
* @notice Called by the oracle when a dispute status has been updated
*
* @dev According to the result of the dispute, bonds are released to the proposer or paid to the disputer
* @param _disputeId The ID of the dispute being updated
* @param _dispute The dispute object
*/
function onDisputeStatusChange(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external;
}
Loading
Loading