diff --git a/solidity/contracts/Oracle.sol b/solidity/contracts/Oracle.sol index dfd3d1bb..6de3d4f2 100644 --- a/solidity/contracts/Oracle.sol +++ b/solidity/contracts/Oracle.sol @@ -18,12 +18,12 @@ contract Oracle is IOracle { mapping(bytes32 _disputeId => Dispute) internal _disputes; - mapping(uint256 _nonce => bytes32 _id) internal _requestIds; - - uint256 internal _nonce; + mapping(uint256 _requestNumber => bytes32 _id) internal _requestIds; uint256 internal _responseNonce; + uint256 public totalRequestCount; + function createRequest(NewRequest memory _request) external payable returns (bytes32 _requestId) { _requestId = _createRequest(_request); } @@ -41,7 +41,7 @@ contract Oracle is IOracle { } function listRequests(uint256 _startFrom, uint256 _batchSize) external view returns (FullRequest[] memory _list) { - uint256 _totalRequestsCount = _nonce; + uint256 _totalRequestsCount = totalRequestCount; // If trying to collect unexisting requests only, return empty array if (_startFrom > _totalRequestsCount) { @@ -69,7 +69,7 @@ contract Oracle is IOracle { } function listRequestIds(uint256 _startFrom, uint256 _batchSize) external view returns (bytes32[] memory _list) { - return _requestIds.getSubset(_startFrom, _batchSize, _nonce); + return _requestIds.getSubset(_startFrom, _batchSize, totalRequestCount); } function getResponse(bytes32 _responseId) external view returns (Response memory _response) { @@ -221,7 +221,7 @@ contract Oracle is IOracle { } function _createRequest(NewRequest memory _request) internal returns (bytes32 _requestId) { - uint256 _requestNonce = _nonce++; + uint256 _requestNonce = totalRequestCount++; _requestId = keccak256(abi.encodePacked(msg.sender, address(this), _requestNonce)); _requestIds[_requestNonce] = _requestId; diff --git a/solidity/interfaces/IOracle.sol b/solidity/interfaces/IOracle.sol index 47fe5691..3455340a 100644 --- a/solidity/interfaces/IOracle.sol +++ b/solidity/interfaces/IOracle.sol @@ -132,4 +132,5 @@ interface IOracle { 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; + function totalRequestCount() external view returns (uint256 _count); } diff --git a/solidity/test/unit/Oracle.t.sol b/solidity/test/unit/Oracle.t.sol index 6e2f976f..2175c518 100644 --- a/solidity/test/unit/Oracle.t.sol +++ b/solidity/test/unit/Oracle.t.sol @@ -102,8 +102,7 @@ contract Oracle_UnitTest is Test { finalityModule = IFinalityModule(address(0)); } - // Read the slot 7 (internal var) which holds the nonce - uint256 _initialNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); + uint256 _initialNonce = oracle.totalRequestCount(); // Create the request IOracle.NewRequest memory _request = IOracle.NewRequest({ @@ -178,8 +177,7 @@ contract Oracle_UnitTest is Test { vm.prank(sender); bytes32 _requestId = oracle.createRequest(_request); - // Read the slot 7 (internal var) which holds the nonce - uint256 _newNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); + uint256 _newNonce = oracle.totalRequestCount(); // Check: correct request id returned? assertEq(_requestId, _theoricRequestId); @@ -210,7 +208,7 @@ contract Oracle_UnitTest is Test { bytes calldata _responseData, bytes calldata _disputeData ) public { - uint256 _initialNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); + uint256 _initialNonce = oracle.totalRequestCount(); uint256 _requestsAmount = 5; @@ -318,8 +316,7 @@ contract Oracle_UnitTest is Test { assertEq(_storedRequest.createdAt, block.timestamp); // should be set } - // Read the slot 7 (internal var) which holds the nonce - uint256 _newNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); + uint256 _newNonce = oracle.totalRequestCount(); assertEq(_newNonce, _initialNonce + _requestsAmount); } @@ -409,8 +406,8 @@ contract Oracle_UnitTest is Test { (bytes32[] memory _dummyRequestIds,) = _storeDummyRequests(1); bytes32 _requestId = _dummyRequestIds[0]; - // Get the current response nonce (8th slot) - uint256 _responseNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x8)))); + // Get the current response nonce (7th slot) + uint256 _responseNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); // Compute the response ID bytes32 _responseId = keccak256(abi.encodePacked(sender, address(oracle), _requestId, _responseNonce)); @@ -472,8 +469,8 @@ contract Oracle_UnitTest is Test { (bytes32[] memory _dummyRequestIds,) = _storeDummyRequests(1); bytes32 _requestId = _dummyRequestIds[0]; - // Get the current response nonce (8th slot) - uint256 _responseNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x8)))); + // Get the current response nonce (7th slot) + uint256 _responseNonce = uint256(vm.load(address(oracle), bytes32(uint256(0x7)))); // Compute the response ID bytes32 _responseId = keccak256(abi.encodePacked(_proposer, address(oracle), _requestId, _responseNonce)); @@ -829,6 +826,13 @@ contract Oracle_UnitTest is Test { oracle.finalize(_requestId, _responseId); } + function test_totalRequestCount(uint256 _requestsToAdd) public { + _requestsToAdd = bound(_requestsToAdd, 1, 10); + uint256 _initialCount = oracle.totalRequestCount(); + _storeDummyRequests(_requestsToAdd); + assert(oracle.totalRequestCount() == _initialCount + _requestsToAdd); + } + /** * @notice create mock requests and store them in the oracle *