Skip to content

Commit

Permalink
fix: id collision (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJabberwock authored Jul 24, 2024
1 parent ad40b65 commit c25103e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
18 changes: 12 additions & 6 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ contract Oracle is IOracle {
mapping(bytes32 _requestId => uint128 _finalizedAt) public finalizedAt;

/// @inheritdoc IOracle
mapping(bytes32 _id => uint128 _createdAt) public createdAt;
mapping(bytes32 _id => uint128 _requestCreatedAt) public requestCreatedAt;

/// @inheritdoc IOracle
mapping(bytes32 _id => uint128 _responseCreatedAt) public responseCreatedAt;

/// @inheritdoc IOracle
mapping(bytes32 _id => uint128 _disputeCreatedAt) public disputeCreatedAt;

/// @inheritdoc IOracle
mapping(bytes32 _responseId => bytes32 _disputeId) public disputeOf;
Expand Down Expand Up @@ -106,7 +112,7 @@ contract Oracle is IOracle {
}

// Can't propose the same response twice
if (createdAt[_responseId] != 0) {
if (responseCreatedAt[_responseId] != 0) {
revert Oracle_InvalidResponseBody();
}

Expand All @@ -117,7 +123,7 @@ contract Oracle is IOracle {
_participants[_response.requestId] = abi.encodePacked(_participants[_response.requestId], _response.proposer);
IResponseModule(_request.responseModule).propose(_request, _response, msg.sender);
_responseIds[_response.requestId] = abi.encodePacked(_responseIds[_response.requestId], _responseId);
createdAt[_responseId] = uint128(block.number);
responseCreatedAt[_responseId] = uint128(block.number);

emit ResponseProposed(_response.requestId, _responseId, _response, block.number);
}
Expand All @@ -134,7 +140,7 @@ contract Oracle is IOracle {
revert Oracle_InvalidDisputeBody();
}

if (_dispute.disputer != msg.sender || createdAt[_dispute.requestId] == 0) {
if (_dispute.disputer != msg.sender || requestCreatedAt[_dispute.requestId] == 0) {
revert Oracle_InvalidDisputeBody();
}

Expand All @@ -149,7 +155,7 @@ contract Oracle is IOracle {
_participants[_response.requestId] = abi.encodePacked(_participants[_response.requestId], msg.sender);
disputeStatus[_disputeId] = DisputeStatus.Active;
disputeOf[_dispute.responseId] = _disputeId;
createdAt[_disputeId] = uint128(block.number);
disputeCreatedAt[_disputeId] = uint128(block.number);

IDisputeModule(_request.disputeModule).disputeResponse(_request, _response, _dispute);

Expand Down Expand Up @@ -389,7 +395,7 @@ contract Oracle is IOracle {

_requestId = keccak256(abi.encode(_request));
nonceToRequestId[_requestNonce] = _requestId;
createdAt[_requestId] = uint128(block.number);
requestCreatedAt[_requestId] = uint128(block.number);

// solhint-disable-next-line func-named-parameters
_allowedModules[_requestId] = abi.encodePacked(
Expand Down
24 changes: 20 additions & 4 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,28 @@ interface IOracle {
function finalizedResponseId(bytes32 _requestId) external view returns (bytes32 _finalizedResponseId);

/**
* @notice The number of the block at which a request, response, or a dispute was created
* @notice The number of the block at which a request was created
*
* @param _id The request, response, or dispute id
* @return _createdAt The block number
* @param _id The request id
* @return _requestCreatedAt The block number
*/
function createdAt(bytes32 _id) external view returns (uint128 _createdAt);
function requestCreatedAt(bytes32 _id) external view returns (uint128 _requestCreatedAt);

/**
* @notice The number of the block at which a response was created
*
* @param _id The response id
* @return _responseCreatedAt The block number
*/
function responseCreatedAt(bytes32 _id) external view returns (uint128 _responseCreatedAt);

/**
* @notice The number of the block at which a dispute was created
*
* @param _id The dispute id
* @return _disputeCreatedAt The block number
*/
function disputeCreatedAt(bytes32 _id) external view returns (uint128 _disputeCreatedAt);

/**
* @notice The number of the block at which a request was finalized
Expand Down
18 changes: 9 additions & 9 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ contract MockOracle is Oracle {
nonceToRequestId[_nonce] = _requestId;
}

function mock_setCreatedAt(bytes32 _requestId, uint128 _createdAt) external {
createdAt[_requestId] = _createdAt;
function mock_setRequestCreatedAt(bytes32 _requestId, uint128 _requestCreatedAt) external {
requestCreatedAt[_requestId] = _requestCreatedAt;
}

function mock_setTotalRequestCount(uint256 _totalRequestCount) external {
Expand Down Expand Up @@ -160,7 +160,7 @@ contract Oracle_Unit_CreateRequest is BaseTest {
assertTrue(oracle.isParticipant(_requestId, requester));

// Check: Saves the number of the block
assertEq(oracle.createdAt(_requestId), block.number);
assertEq(oracle.requestCreatedAt(_requestId), block.number);

// Check: Sets allowedModules
assertTrue(oracle.allowedModule(_requestId, address(requestModule)));
Expand Down Expand Up @@ -261,7 +261,7 @@ contract Oracle_Unit_CreateRequests is BaseTest {
assertTrue(oracle.isParticipant(_requestsIds[_i], requester));

// Check: Saves the number of the block
assertEq(oracle.createdAt(_requestsIds[_i]), block.number);
assertEq(oracle.requestCreatedAt(_requestsIds[_i]), block.number);

// Check: Sets allowedModules
assertTrue(oracle.allowedModule(_requestsIds[_i], address(requestModule)));
Expand Down Expand Up @@ -503,7 +503,7 @@ contract Oracle_Unit_DisputeResponse is BaseTest {
_responseId = _getId(mockResponse);
_disputeId = _getId(mockDispute);

oracle.mock_setCreatedAt(_getId(mockRequest), uint128(block.number));
oracle.mock_setRequestCreatedAt(_getId(mockRequest), uint128(block.number));
}

/**
Expand Down Expand Up @@ -541,7 +541,7 @@ contract Oracle_Unit_DisputeResponse is BaseTest {
*/
function test_disputeResponse_revertIfProposerIsNotValid(address _otherProposer) public {
vm.assume(_otherProposer != proposer);
oracle.mock_setCreatedAt(_getId(mockRequest), 0);
oracle.mock_setRequestCreatedAt(_getId(mockRequest), 0);

// Check: revert?
vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
Expand All @@ -557,7 +557,7 @@ contract Oracle_Unit_DisputeResponse is BaseTest {
* @notice Reverts if the request doesn't exist
*/
function test_disputeResponse_revertIfInvalidRequest() public {
oracle.mock_setCreatedAt(_getId(mockRequest), 0);
oracle.mock_setRequestCreatedAt(_getId(mockRequest), 0);

// Check: revert?
vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
Expand Down Expand Up @@ -602,7 +602,7 @@ contract Oracle_Unit_UpdateDisputeStatus is BaseTest {
*/
function test_updateDisputeStatus() public {
bytes32 _requestId = _getId(mockRequest);
oracle.mock_setCreatedAt(_getId(mockRequest), uint128(block.number));
oracle.mock_setRequestCreatedAt(_getId(mockRequest), uint128(block.number));

// Try every initial status
for (uint256 _previousStatus; _previousStatus < uint256(type(IOracle.DisputeStatus).max); _previousStatus++) {
Expand Down Expand Up @@ -665,7 +665,7 @@ contract Oracle_Unit_UpdateDisputeStatus is BaseTest {

// Mock the dispute
oracle.mock_setDisputeOf(_getId(mockResponse), _getId(mockDispute));
oracle.mock_setCreatedAt(_getId(mockRequest), uint128(block.number));
oracle.mock_setRequestCreatedAt(_getId(mockRequest), uint128(block.number));

// Check: revert?
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_NotDisputeOrResolutionModule.selector, proposer));
Expand Down

0 comments on commit c25103e

Please sign in to comment.