Skip to content

Commit

Permalink
feat: provide IPFS hash to create requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Nov 22, 2023
1 parent 4fd09e6 commit 5fbc672
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
16 changes: 10 additions & 6 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ contract Oracle is IOracle {
uint256 public totalRequestCount;

/// @inheritdoc IOracle
function createRequest(Request calldata _request) external returns (bytes32 _requestId) {
_requestId = _createRequest(_request);
function createRequest(Request calldata _request, bytes32 _ipfsHash) external returns (bytes32 _requestId) {
_requestId = _createRequest(_request, _ipfsHash);
}

/// @inheritdoc IOracle
function createRequests(Request[] calldata _requestsData) external returns (bytes32[] memory _batchRequestsIds) {
function createRequests(
Request[] calldata _requestsData,
bytes32[] calldata _ipfsHashes
) external returns (bytes32[] memory _batchRequestsIds) {
uint256 _requestsAmount = _requestsData.length;
_batchRequestsIds = new bytes32[](_requestsAmount);

for (uint256 _i = 0; _i < _requestsAmount;) {
_batchRequestsIds[_i] = _createRequest(_requestsData[_i]);
_batchRequestsIds[_i] = _createRequest(_requestsData[_i], _ipfsHashes[_i]);
unchecked {
++_i;
}
Expand Down Expand Up @@ -372,9 +375,10 @@ contract Oracle is IOracle {
* @notice Stores a request in the contract and configures it in the modules
*
* @param _request The request to be created
* @param _ipfsHash The hashed IPFS CID of the metadata json
* @return _requestId The id of the created request
*/
function _createRequest(Request calldata _request) internal returns (bytes32 _requestId) {
function _createRequest(Request calldata _request, bytes32 _ipfsHash) internal returns (bytes32 _requestId) {
uint256 _requestNonce = totalRequestCount++;

// @audit what about removing nonces? or how we avoid nonce clashing?
Expand All @@ -396,7 +400,7 @@ contract Oracle is IOracle {
_participants[_requestId] = abi.encodePacked(_participants[_requestId], msg.sender);
IRequestModule(_request.requestModule).createRequest(_requestId, _request.requestModuleData, msg.sender);

emit RequestCreated(_requestId, _request, block.number);
emit RequestCreated(_requestId, _request, _ipfsHash, block.number);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ interface IOracle {
/**
* @notice Emitted when a request is created
* @param _requestId The id of the created request
* @param _request The request that has been created
* @param _ipfsHash The hashed IPFS CID of the metadata json
* @param _blockNumber The current block number
*/
event RequestCreated(bytes32 indexed _requestId, Request _request, uint256 _blockNumber);
event RequestCreated(bytes32 indexed _requestId, Request _request, bytes32 _ipfsHash, uint256 _blockNumber);

/**
* @notice Emitted when a response is proposed
Expand Down Expand Up @@ -279,17 +282,22 @@ interface IOracle {
*
* @dev The modules must be real contracts following the IModule interface
* @param _request The request data
* @param _ipfsHash The hashed IPFS CID of the metadata json
* @return _requestId The id of the request, can be used to propose a response or query results
*/
function createRequest(Request memory _request) external returns (bytes32 _requestId);
function createRequest(Request memory _request, bytes32 _ipfsHash) external returns (bytes32 _requestId);

/**
* @notice Creates multiple requests, the same way as createRequest
*
* @param _requestsData The array of calldata for each request
* @return _batchRequestsIds The array of request IDs
* @param _ipfsHashes The array of hashed IPFS CIDs of the metadata files
*/
function createRequests(Request[] calldata _requestsData) external returns (bytes32[] memory _batchRequestsIds);
function createRequests(
Request[] calldata _requestsData,
bytes32[] calldata _ipfsHashes
) external returns (bytes32[] memory _batchRequestsIds);

/**
* @notice Returns the list of request IDs
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/integration/ResponseDispute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract Integration_ResponseDispute is IntegrationBase {
});

vm.prank(requester);
_requestId = oracle.createRequest(_request);
_requestId = oracle.createRequest(_request, _ipfsHash);

IOracle.Response memory _response =
IOracle.Response({requestId: _requestId, response: abi.encode('testResponse'), proposer: proposer});
Expand Down Expand Up @@ -190,7 +190,7 @@ contract Integration_ResponseDispute is IntegrationBase {
// ipfsHash: _ipfsHash
// });
// vm.prank(requester);
// bytes32 _secondRequest = oracle.createRequest(_request);
// bytes32 _secondRequest = oracle.createRequest(_request, _ipfsHash);

// vm.prank(proposer);
// bytes32 _secondResponseId = oracle.proposeResponse(_secondRequest, _responseData, _responseModuleData);
Expand Down
15 changes: 10 additions & 5 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ contract BaseTest is Test, Helpers {
IResolutionModule public resolutionModule = IResolutionModule(_mockContract('resolutionModule'));
IFinalityModule public finalityModule = IFinalityModule(_mockContract('finalityModule'));

// Mock IPFS hash
bytes32 internal _ipfsHash = bytes32('QmR4uiJH654k3Ta2uLLQ8r');

// Events
event RequestCreated(bytes32 indexed _requestId, IOracle.Request _request, uint256 _blockNumber);
event RequestCreated(bytes32 indexed _requestId, IOracle.Request _request, bytes32 _ipfsHash, uint256 _blockNumber);
event ResponseProposed(
bytes32 indexed _requestId, bytes32 indexed _responseId, IOracle.Response _response, uint256 _blockNumber
);
Expand Down Expand Up @@ -150,11 +153,11 @@ contract Unit_CreateRequest is BaseTest {

// Check: emits RequestCreated event?
_expectEmit(address(oracle));
emit RequestCreated(_getId(mockRequest), mockRequest, block.number);
emit RequestCreated(_getId(mockRequest), mockRequest, _ipfsHash, block.number);

// Test: create the request
vm.prank(requester);
bytes32 _requestId = oracle.createRequest(mockRequest);
bytes32 _requestId = oracle.createRequest(mockRequest, _ipfsHash);

// Check: Adds the requester to the list of participants
assertTrue(oracle.isParticipant(_requestId, requester));
Expand Down Expand Up @@ -197,6 +200,7 @@ contract Unit_CreateRequests is BaseTest {
IOracle.Request[] memory _requests = new IOracle.Request[](_requestsAmount);
bytes32[] memory _precalculatedIds = new bytes32[](_requestsAmount);
bool _useResolutionAndFinality = _requestData.length % 2 == 0;
bytes32[] memory _ipfsHashes = new bytes32[](_requestsAmount);

// Generate requests batch
for (uint256 _i = 0; _i < _requestsAmount; _i++) {
Expand All @@ -209,14 +213,15 @@ contract Unit_CreateRequests is BaseTest {
bytes32 _theoreticalRequestId = _getId(mockRequest);
_requests[_i] = mockRequest;
_precalculatedIds[_i] = _theoreticalRequestId;
_ipfsHashes[_i] = keccak256(abi.encode(_theoreticalRequestId, mockRequest.nonce));

// Check: emits RequestCreated event?
_expectEmit(address(oracle));
emit RequestCreated(_theoreticalRequestId, mockRequest, block.number);
emit RequestCreated(_theoreticalRequestId, mockRequest, _ipfsHashes[_i], block.number);
}

vm.prank(requester);
bytes32[] memory _requestsIds = oracle.createRequests(_requests);
bytes32[] memory _requestsIds = oracle.createRequests(_requests, _ipfsHashes);

for (uint256 _i = 0; _i < _requestsIds.length; _i++) {
assertEq(_requestsIds[_i], _precalculatedIds[_i]);
Expand Down

0 comments on commit 5fbc672

Please sign in to comment.