Skip to content

Commit

Permalink
feat: public request nonce (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito authored Aug 8, 2023
1 parent 25f62ee commit b461ebb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
12 changes: 6 additions & 6 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
26 changes: 15 additions & 11 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit b461ebb

Please sign in to comment.