Skip to content

Commit

Permalink
feat: improvments and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ashitakah committed Aug 6, 2024
1 parent ddde6e6 commit 6bf9f06
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 143 deletions.
94 changes: 54 additions & 40 deletions src/contracts/EBORequestCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,85 @@ contract EBORequestCreator is IEBORequestCreator {
IOracle public oracle;

/// @inheritdoc IEBORequestCreator
address public owner;
RequestData public requestData;

/// @inheritdoc IEBORequestCreator
address public pendingOwner;
address public arbitrator;

/// @inheritdoc IEBORequestCreator
address public pendingArbitrator;

/// @inheritdoc IEBORequestCreator
uint256 public reward;

EnumerableSet.Bytes32Set internal _chainIdsAllowed;
/// @inheritdoc IEBORequestCreator
mapping(string _chainId => mapping(uint256 _epoch => bytes32 _requestId)) public requestIdPerChainAndEpoch;

mapping(uint256 _epoch => EnumerableSet.Bytes32Set _chainIds) internal _epochChainIds;
/**
* @notice The set of chain ids allowed
*/
EnumerableSet.Bytes32Set internal _chainIdsAllowed;

constructor(IOracle _oracle, address _owner) {
constructor(IOracle _oracle, address _arbitrator) {
oracle = _oracle;
owner = _owner;
arbitrator = _arbitrator;
reward = 0;
}

/// @inheritdoc IEBORequestCreator
function setPendingOwner(address _pendingOwner) external onlyOwner {
pendingOwner = _pendingOwner;
function setPendingArbitrator(address _pendingArbitrator) external onlyArbitrator {
pendingArbitrator = _pendingArbitrator;

emit PendingOwnerSetted(_pendingOwner);
emit PendingArbitratorSetted(_pendingArbitrator);
}

/// @inheritdoc IEBORequestCreator
function acceptPendingOwner() external onlyPendingOwner {
address _oldOwner = owner;
owner = pendingOwner;
pendingOwner = address(0);
function acceptPendingArbitrator() external onlyPendingArbitrator {
address _oldArbitrator = arbitrator;
arbitrator = pendingArbitrator;
pendingArbitrator = address(0);

emit OwnerSetted(_oldOwner, owner);
emit ArbitratorSetted(_oldArbitrator, arbitrator);
}

/// @inheritdoc IEBORequestCreator
function createRequests(uint256 _epoch, string[] calldata _chainIds) external {
bytes32 _encodedChainId;

EnumerableSet.Bytes32Set storage _epochEncodedChainIds = _epochChainIds[_epoch];
RequestData memory _requestData = requestData;

for (uint256 _i; _i < _chainIds.length; _i++) {
_encodedChainId = _encodeChainId(_chainIds[_i]);
if (!_chainIdsAllowed.contains(_encodedChainId)) revert EBORequestCreator_ChainNotAdded();

if (!_epochEncodedChainIds.contains(_encodedChainId)) {
_epochChainIds[_epoch].add(_encodedChainId);

if (requestIdPerChainAndEpoch[_chainIds[_i]][_epoch] == bytes32(0)) {
// TODO: COMPLETE THE REQUEST CREATION WITH THE PROPER MODULES
IOracle.Request memory _request = IOracle.Request({
nonce: 0,
requester: msg.sender,
requestModule: address(0),
responseModule: address(0),
disputeModule: address(0),
resolutionModule: address(0),
finalityModule: address(0),
requestModuleData: '',
responseModuleData: '',
disputeModuleData: '',
resolutionModuleData: '',
finalityModuleData: ''
requester: address(this),
requestModule: _requestData.requestModule,
responseModule: _requestData.responseModule,
disputeModule: _requestData.disputeModule,
resolutionModule: _requestData.resolutionModule,
finalityModule: _requestData.finalityModule,
requestModuleData: _requestData.requestModuleData,
responseModuleData: _requestData.responseModuleData,
disputeModuleData: _requestData.disputeModuleData,
resolutionModuleData: _requestData.resolutionModuleData,
finalityModuleData: _requestData.finalityModuleData
});

bytes32 _requestId = oracle.createRequest(_request, bytes32(0));

requestIdPerChainAndEpoch[_chainIds[_i]][_epoch] = _requestId;

emit RequestCreated(_requestId, _epoch, _chainIds[_i]);
}
}
}

/// @inheritdoc IEBORequestCreator
function addChain(string calldata _chainId) external onlyOwner {
function addChain(string calldata _chainId) external onlyArbitrator {
bytes32 _encodedChainId = _encodeChainId(_chainId);
if (_chainIdsAllowed.contains(_encodedChainId)) {
revert EBORequestCreator_ChainAlreadyAdded();
Expand All @@ -93,7 +100,7 @@ contract EBORequestCreator is IEBORequestCreator {
}

/// @inheritdoc IEBORequestCreator
function removeChain(string calldata _chainId) external onlyOwner {
function removeChain(string calldata _chainId) external onlyArbitrator {
bytes32 _encodedChainId = _encodeChainId(_chainId);
if (!_chainIdsAllowed.contains(_encodedChainId)) {
revert EBORequestCreator_ChainNotAdded();
Expand All @@ -104,12 +111,19 @@ contract EBORequestCreator is IEBORequestCreator {
}

/// @inheritdoc IEBORequestCreator
function setReward(uint256 _reward) external onlyOwner {
function setReward(uint256 _reward) external onlyArbitrator {
uint256 _oldReward = reward;
reward = _reward;
emit RewardSet(_oldReward, _reward);
}

/// @inheritdoc IEBORequestCreator
function setRequestData(RequestData calldata _requestData) external onlyArbitrator {
requestData = _requestData;

emit RequestDataSet(_requestData);
}

/**
* @notice Encodes the chain id
* @dev The chain id is hashed to have a enumerable set to avoid duplicates
Expand All @@ -119,21 +133,21 @@ contract EBORequestCreator is IEBORequestCreator {
}

/**
* @notice Checks if the sender is the owner
* @notice Checks if the sender is the arbitrator
*/
modifier onlyOwner() {
if (msg.sender != owner) {
revert EBORequestCreator_OnlyOwner();
modifier onlyArbitrator() {

Check warning on line 138 in src/contracts/EBORequestCreator.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function order is incorrect, modifier definition can not go after internal pure function (line 131)
if (msg.sender != arbitrator) {
revert EBORequestCreator_OnlyArbitrator();
}
_;
}

/**
* @notice Checks if the sender is the pending owner
* @notice Checks if the sender is the pending arbitrator
*/
modifier onlyPendingOwner() {
if (msg.sender != pendingOwner) {
revert EBORequestCreator_OnlyPendingOwner();
modifier onlyPendingArbitrator() {
if (msg.sender != pendingArbitrator) {
revert EBORequestCreator_OnlyPendingArbitrator();
}

_;
Expand Down
103 changes: 81 additions & 22 deletions src/interfaces/IEBORequestCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ interface IEBORequestCreator {
//////////////////////////////////////////////////////////////*/

/**
* @notice Emitted when the pending owner is set
* @param _pendingOwner The address of the pending owner
* @notice Emitted when the pending arbitrator is set
* @param _pendingArbitrator The address of the pending arbitrator
*/
event PendingOwnerSetted(address _pendingOwner);
event PendingArbitratorSetted(address _pendingArbitrator);

/**
* @notice Emitted when the owner is set
* @param _oldOwner The old owner address
* @param _newOwner The new owner address
* @notice Emitted when the abitrator is set
* @param _oldArbitrator The old abitrator address
* @param _newArbitrator The new abitrator address
*/
event OwnerSetted(address _oldOwner, address _newOwner);
event ArbitratorSetted(address _oldArbitrator, address _newArbitrator);

/**
* @notice Emitted when a request is created
Expand Down Expand Up @@ -48,19 +48,25 @@ interface IEBORequestCreator {
*/
event RewardSet(uint256 _oldReward, uint256 _newReward);

/**
* @notice Emitted when a request data is set
* @param _requestData The request data
*/
event RequestDataSet(RequestData _requestData);

/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/

/**
* @notice Thrown when the caller is not the owner
* @notice Thrown when the caller is not the arbitrator
*/
error EBORequestCreator_OnlyOwner();
error EBORequestCreator_OnlyArbitrator();

/**
* @notice Thrown when the caller is not the pending owner
* @notice Thrown when the caller is not the pending arbitrator
*/
error EBORequestCreator_OnlyPendingOwner();
error EBORequestCreator_OnlyPendingArbitrator();

/**
* @notice hrown when the chain is already added
Expand All @@ -72,6 +78,23 @@ interface IEBORequestCreator {
*/
error EBORequestCreator_ChainNotAdded();

/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/

struct RequestData {

Check warning on line 85 in src/interfaces/IEBORequestCreator.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function order is incorrect, struct definition can not go after custom error definition (line 79)
address requestModule;
address responseModule;
address disputeModule;
address resolutionModule;
address finalityModule;
bytes requestModuleData;
bytes responseModuleData;
bytes disputeModuleData;
bytes resolutionModuleData;
bytes finalityModuleData;
}

/*///////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////*/
Expand All @@ -82,37 +105,67 @@ interface IEBORequestCreator {
function oracle() external view returns (IOracle _oracle);

/**
* @notice The owner of the contract
* @return _owner The owner
* @notice The request data
*/
function requestData()
external
view
returns (
address _requestModule,
address _responseModule,
address _disputeModule,
address _resolutionModule,
address _finalityModule,
bytes memory _requestModuleData,
bytes memory _responseModuleData,
bytes memory _disputeModuleData,
bytes memory _resolutionModuleData,
bytes memory _finalityModuleData
);

/**
* @notice The arbitrator of the contract
* @return _arbitrator The arbitrator
*/
function owner() external view returns (address _owner);
function arbitrator() external view returns (address _arbitrator);

/**
* @notice The pending owner of the contract
* @return _pendingOwner The pending owner
* @notice The pending arbitrator of the contract
* @return _pendingArbitrator The pending owner
*/
function pendingOwner() external view returns (address _pendingOwner);
function pendingArbitrator() external view returns (address _pendingArbitrator);

/**
* @notice The reward paid for each chain updated
* @return _reward The reward
*/
function reward() external view returns (uint256 _reward);

/**
* @notice The request id per chain and epoch
* @param _chainId The chain id
* @param _epoch The epoch
* @return _requestId The request id
*/
function requestIdPerChainAndEpoch(
string calldata _chainId,
uint256 _epoch
) external view returns (bytes32 _requestId);

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

/**
* @notice Set the pending owner
* @param _pendingOwner The address of the pending owner
* @notice Set the pending arbitrator
* @param _pendingArbitrator The address of the pending arbitrator
*/
function setPendingOwner(address _pendingOwner) external;
function setPendingArbitrator(address _pendingArbitrator) external;

/**
* @notice Accept the pending owner
* @notice Accept the pending arbitrator
*/
function acceptPendingOwner() external;
function acceptPendingArbitrator() external;

/**
* @notice Create requests
Expand All @@ -138,4 +191,10 @@ interface IEBORequestCreator {
* @param _reward The reward to set
*/
function setReward(uint256 _reward) external;

/**
* @notice Set the request data
* @param _requestData The request data to set
*/
function setRequestData(RequestData calldata _requestData) external;
}
37 changes: 37 additions & 0 deletions test/integration/CreateRequest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import './IntegrationBase.sol';

contract Integration_CreateRequest is IntegrationBase {
function test_createRequest() public {
string[] memory _chainIds = new string[](1);
_chainIds[0] = 'chainId1';

// Create a request without approving the chain id
vm.expectRevert(IEBORequestCreator.EBORequestCreator_ChainNotAdded.selector);
vm.prank(_user);
_eboRequestCreator.createRequests(1, _chainIds);

// Create a request with an approved chain id
vm.prank(_arbitrator);
_eboRequestCreator.addChain('chainId1');

bytes32 _requestId = keccak256('requestId');
// TODO: Remove the mock when the Oracle contract is implemented
vm.mockCall(address(_oracle), abi.encodeWithSelector(IOracle.createRequest.selector), abi.encode(_requestId));

vm.prank(_user);
_eboRequestCreator.createRequests(1, _chainIds);
assertEq(_eboRequestCreator.requestIdPerChainAndEpoch('chainId1', 1), _requestId);

// Remove the chain id
vm.prank(_arbitrator);
_eboRequestCreator.removeChain('chainId1');

// Create a request without approving the chain id
vm.expectRevert(IEBORequestCreator.EBORequestCreator_ChainNotAdded.selector);
vm.prank(_user);
_eboRequestCreator.createRequests(1, _chainIds);
}
}
Loading

0 comments on commit 6bf9f06

Please sign in to comment.