Skip to content

Commit

Permalink
fix: nonce zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ashitakah committed Jul 9, 2024
1 parent 4d4a448 commit 941cb73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,14 @@ contract Oracle is IOracle {
* @param _ipfsHash The hashed IPFS CID of the metadata json
* @return _requestId The id of the created request
*/
function _createRequest(Request calldata _request, bytes32 _ipfsHash) internal returns (bytes32 _requestId) {
function _createRequest(Request memory _request, bytes32 _ipfsHash) internal returns (bytes32 _requestId) {
uint256 _requestNonce = totalRequestCount++;

if (_requestNonce != _request.nonce || msg.sender != _request.requester) revert Oracle_InvalidRequestBody();
if (msg.sender != _request.requester || (_requestNonce != _request.nonce && _request.nonce != 0)) {
revert Oracle_InvalidRequestBody();
}

if (_request.nonce == 0) _request.nonce = uint96(_requestNonce);

_requestId = keccak256(abi.encode(_request));
nonceToRequestId[_requestNonce] = _requestId;
Expand Down
35 changes: 35 additions & 0 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,41 @@ contract Oracle_Unit_CreateRequests is BaseTest {
uint256 _newNonce = oracle.totalRequestCount();
assertEq(_newNonce, _initialNonce + _requestsAmount);
}

/**
* @notice Test creation of requests in batch mode with nonce 0.
*/
function test_createRequestsWithNonceZero(
bytes calldata _requestData,
bytes calldata _responseData,
bytes calldata _disputeData
) public {
uint256 _initialNonce = oracle.totalRequestCount();
uint256 _requestsAmount = 5;
IOracle.Request[] memory _requests = new IOracle.Request[](_requestsAmount);
bytes32[] memory _precalculatedIds = new bytes32[](_requestsAmount);
bytes32[] memory _ipfsHashes = new bytes32[](_requestsAmount);

// Generate requests batch
for (uint256 _i = 0; _i < _requestsAmount; _i++) {
mockRequest.requestModuleData = _requestData;
mockRequest.responseModuleData = _responseData;
mockRequest.disputeModuleData = _disputeData;
mockRequest.requester = requester;
mockRequest.nonce = uint96(0);

bytes32 _theoreticalRequestId = _getId(mockRequest);
_requests[_i] = mockRequest;
_precalculatedIds[_i] = _theoreticalRequestId;
_ipfsHashes[_i] = keccak256(abi.encode(_theoreticalRequestId, mockRequest.nonce));
}

vm.prank(requester);
oracle.createRequests(_requests, _ipfsHashes);

uint256 _newNonce = oracle.totalRequestCount();
assertEq(_newNonce, _initialNonce + _requestsAmount);
}
}

contract Oracle_Unit_ListRequestIds is BaseTest {
Expand Down

0 comments on commit 941cb73

Please sign in to comment.