Skip to content

Commit

Permalink
feat: request processing in Oracle (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-something authored Jul 7, 2023
1 parent bbb2f03 commit 88e224d
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 71 deletions.
76 changes: 68 additions & 8 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ contract Oracle is IOracle {

uint256 internal _responseNonce;

function createRequest(Request memory _request) external payable returns (bytes32 _requestId) {
function createRequest(NewRequest memory _request) external payable returns (bytes32 _requestId) {
uint256 _requestNonce = _nonce++;
_requestId = keccak256(abi.encodePacked(msg.sender, address(this), _requestNonce));
_requestIds[_requestNonce] = _requestId;
_request.nonce = _requestNonce;
_request.requester = msg.sender;
_request.createdAt = block.timestamp;
_requests[_requestId] = _request;

Request memory _storedRequest = Request({
ipfsHash: _request.ipfsHash,
requestModule: _request.requestModule,
responseModule: _request.responseModule,
disputeModule: _request.disputeModule,
resolutionModule: _request.resolutionModule,
finalityModule: _request.finalityModule,
requester: msg.sender,
nonce: _requestNonce,
createdAt: block.timestamp
});

_requests[_requestId] = _storedRequest;

_request.requestModule.setupRequest(_requestId, _request.requestModuleData);
_request.responseModule.setupRequest(_requestId, _request.responseModuleData);
Expand All @@ -49,7 +59,7 @@ contract Oracle is IOracle {
// TODO: [OPO-86] Same as `createRequest` but with multiple requests passed in as an array
function createRequests(bytes[] calldata _requestsData) external returns (bytes32[] memory _requestsIds) {}

function listRequests(uint256 _startFrom, uint256 _batchSize) external view returns (Request[] memory _list) {
function listRequests(uint256 _startFrom, uint256 _batchSize) external view returns (FullRequest[] memory _list) {
uint256 _totalRequestsCount = _nonce;

// If trying to collect unexisting requests only, return empty array
Expand All @@ -61,11 +71,35 @@ contract Oracle is IOracle {
_batchSize = _totalRequestsCount - _startFrom;
}

_list = new Request[](_batchSize);
_list = new FullRequest[](_batchSize);

uint256 _index;
while (_index < _batchSize) {
_list[_index] = _requests[_requestIds[_startFrom + _index]];
bytes32 _requestId = _requestIds[_startFrom + _index];

Request memory _storedRequest = _requests[_requestId];

_list[_index] = FullRequest({
requestModuleData: _storedRequest.requestModule.requestData(_requestId),
responseModuleData: _storedRequest.responseModule.requestData(_requestId),
disputeModuleData: _storedRequest.disputeModule.requestData(_requestId),
resolutionModuleData: address(_storedRequest.resolutionModule) == address(0)
? bytes('')
: _storedRequest.resolutionModule.requestData(_requestId),
finalityModuleData: address(_storedRequest.finalityModule) == address(0)
? bytes('')
: _storedRequest.finalityModule.requestData(_requestId),
ipfsHash: _storedRequest.ipfsHash,
requestModule: _storedRequest.requestModule,
responseModule: _storedRequest.responseModule,
disputeModule: _storedRequest.disputeModule,
resolutionModule: _storedRequest.resolutionModule,
finalityModule: _storedRequest.finalityModule,
requester: _storedRequest.requester,
nonce: _storedRequest.nonce,
createdAt: _storedRequest.createdAt,
requestId: _requestId
});

unchecked {
++_index;
Expand All @@ -87,6 +121,32 @@ contract Oracle is IOracle {
_request = _requests[_requestId];
}

function getFullRequest(bytes32 _requestId) external view returns (FullRequest memory _request) {
Request memory _storedRequest = _requests[_requestId];

_request = FullRequest({
requestModuleData: _storedRequest.requestModule.requestData(_requestId),
responseModuleData: _storedRequest.responseModule.requestData(_requestId),
disputeModuleData: _storedRequest.disputeModule.requestData(_requestId),
resolutionModuleData: address(_storedRequest.resolutionModule) == address(0)
? bytes('')
: _storedRequest.resolutionModule.requestData(_requestId),
finalityModuleData: address(_storedRequest.finalityModule) == address(0)
? bytes('')
: _storedRequest.finalityModule.requestData(_requestId),
ipfsHash: _storedRequest.ipfsHash,
requestModule: _storedRequest.requestModule,
responseModule: _storedRequest.responseModule,
disputeModule: _storedRequest.disputeModule,
resolutionModule: _storedRequest.resolutionModule,
finalityModule: _storedRequest.finalityModule,
requester: _storedRequest.requester,
nonce: _storedRequest.nonce,
createdAt: _storedRequest.createdAt,
requestId: _requestId
});
}

function getDispute(bytes32 _disputeId) external view returns (Dispute memory _dispute) {
_dispute = _disputes[_disputeId];
}
Expand Down
35 changes: 33 additions & 2 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,36 @@ interface IOracle {
error Oracle_CannotResolve(bytes32 _disputeId);
error Oracle_NoResolutionModule(bytes32 _disputeId);

// stored request
struct Request {
bytes32 ipfsHash;
IRequestModule requestModule;
IResponseModule responseModule;
IDisputeModule disputeModule;
IResolutionModule resolutionModule;
IFinalityModule finalityModule;
address requester;
uint256 nonce;
uint256 createdAt;
}

// Request as sent by users
struct NewRequest {
bytes requestModuleData;
bytes responseModuleData;
bytes disputeModuleData;
bytes resolutionModuleData;
bytes finalityModuleData;
bytes32 ipfsHash;
IRequestModule requestModule;
IResponseModule responseModule;
IDisputeModule disputeModule;
IResolutionModule resolutionModule;
IFinalityModule finalityModule;
}

// For offchain/getters
struct FullRequest {
bytes requestModuleData;
bytes responseModuleData;
bytes disputeModuleData;
Expand All @@ -35,6 +64,7 @@ interface IOracle {
address requester;
uint256 nonce;
uint256 createdAt;
bytes32 requestId;
}

struct Response {
Expand Down Expand Up @@ -69,7 +99,7 @@ interface IOracle {
* @param _request The request data
* @return _requestId The ID of the request, can be used to propose a response or query results
*/
function createRequest(IOracle.Request memory _request) external payable returns (bytes32 _requestId);
function createRequest(IOracle.NewRequest memory _request) external payable returns (bytes32 _requestId);

/**
* @notice Creates multiple requests, the same way as createRequest
Expand All @@ -82,6 +112,7 @@ interface IOracle {
function getDispute(bytes32 _disputeId) external view returns (Dispute memory _dispute);
function getResponse(bytes32 _responseId) external view returns (Response memory _response);
function getRequest(bytes32 _requestId) external view returns (Request memory _request);
function getFullRequest(bytes32 _requestId) external view returns (FullRequest memory _request);
function disputeOf(bytes32 _requestId) external view returns (bytes32 _disputeId);
function proposeResponse(bytes32 _requestId, bytes calldata _responseData) external returns (bytes32 _responseId);
function proposeResponse(
Expand All @@ -95,7 +126,7 @@ interface IOracle {
function getResponseIds(bytes32 _requestId) external view returns (bytes32[] memory _ids);
function resolveDispute(bytes32 _disputeId) external;
function updateDisputeStatus(bytes32 _disputeId, DisputeStatus _status) external;
function listRequests(uint256 _startFrom, uint256 _amount) external view returns (Request[] memory _list);
function listRequests(uint256 _startFrom, uint256 _amount) external view returns (FullRequest[] memory _list);
function listRequestIds(uint256 _startFrom, uint256 _batchSize) external view returns (bytes32[] memory _list);
function finalize(bytes32 _requestId, bytes32 _finalizedResponseId) external;
}
5 changes: 1 addition & 4 deletions solidity/test/integration/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract IntegrationOracle is IntegrationBase {
usdc.approve(address(_accountingExtension), _expectedReward);
_accountingExtension.deposit(usdc, _expectedReward);

IOracle.Request memory _request = IOracle.Request({
IOracle.NewRequest memory _request = IOracle.NewRequest({
requestModuleData: abi.encode(
_expectedUrl, _expectedMethod, _expectedBody, _accountingExtension, USDC_ADDRESS, _expectedReward
),
Expand All @@ -60,9 +60,6 @@ contract IntegrationOracle is IntegrationBase {
disputeModule: _disputeModule,
resolutionModule: _resolutionModule,
finalityModule: IFinalityModule(_callbackModule),
requester: address(0),
nonce: 0,
createdAt: block.timestamp,
ipfsHash: bytes32('QmR4uiJH654k3Ta2uLLQ8r')
});

Expand Down
19 changes: 0 additions & 19 deletions solidity/test/unit/BondEscalationResolutionModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -736,25 +736,6 @@ contract BondEscalationResolutionModule_UnitTest is Test {
});
}

function _getMockRequest(address _disputeModule) internal pure returns (IOracle.Request memory _request) {
_request = IOracle.Request({
requestModuleData: abi.encode(0),
responseModuleData: abi.encode(0),
disputeModuleData: abi.encode(0),
resolutionModuleData: abi.encode(0),
finalityModuleData: abi.encode(0),
ipfsHash: 0,
requestModule: IRequestModule(address(100)),
responseModule: IResponseModule(address(200)),
disputeModule: IDisputeModule(_disputeModule),
resolutionModule: IResolutionModule(address(400)),
finalityModule: IFinalityModule(address(500)),
requester: address(600),
nonce: 0,
createdAt: 0
});
}

function _createPledgers(
uint256 _numOfPledgers,
uint256 _amount
Expand Down
Loading

0 comments on commit 88e224d

Please sign in to comment.