Skip to content

Commit

Permalink
fix: nonce zero (#27)
Browse files Browse the repository at this point in the history
* fix: nonce zero

* fix: comments
  • Loading branch information
ashitakah authored Jul 12, 2024
1 parent 211e8fa commit 1e39da6
Show file tree
Hide file tree
Showing 2 changed files with 43 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 (_request.nonce == 0) _request.nonce = uint96(_requestNonce);

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

_requestId = keccak256(abi.encode(_request));
nonceToRequestId[_requestNonce] = _requestId;
Expand Down
37 changes: 37 additions & 0 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,43 @@ 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);

mockRequest.requestModuleData = _requestData;
mockRequest.responseModuleData = _responseData;
mockRequest.disputeModuleData = _disputeData;
mockRequest.requester = requester;
mockRequest.nonce = uint96(0);

bytes32 _theoreticalRequestId = _getId(mockRequest);
bytes32 _ipfsHash = keccak256(abi.encode(_theoreticalRequestId, uint96(0)));

// Generate requests batch
for (uint256 _i = 0; _i < _requestsAmount; _i++) {
_requests[_i] = mockRequest;
_precalculatedIds[_i] = _theoreticalRequestId;
_ipfsHashes[_i] = _ipfsHash;
}

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 1e39da6

Please sign in to comment.