Skip to content

Commit

Permalink
refactor: use block.timestamp instead of block.number (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
xorsal authored Oct 2, 2024
1 parent 7dd25d3 commit 2d91875
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 86 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"package.json": "sort-package-json"
},
"dependencies": {
"@defi-wonderland/prophet-core": "0.0.0-0954a47f",
"@defi-wonderland/prophet-core": "0.0.0-4d28a0ec",
"@openzeppelin/contracts": "4.9.5",
"solmate": "https://github.com/transmissions11/solmate.git#bfc9c25865a274a7827fea5abf6e4fb64fc64e6c"
},
Expand Down
5 changes: 2 additions & 3 deletions solidity/contracts/modules/dispute/BondEscalationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
RequestParameters memory _params = decodeRequestData(_request.disputeModuleData);
BondEscalation storage _escalation = _escalations[_dispute.requestId];

if (block.number > ORACLE.responseCreatedAt(_dispute.responseId) + _params.disputeWindow) {
if (block.timestamp > ORACLE.responseCreatedAt(_dispute.responseId) + _params.disputeWindow) {
revert BondEscalationModule_DisputeWindowOver();
}

Expand All @@ -51,8 +51,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
_requestId: _dispute.requestId,
_responseId: _dispute.responseId,
_disputeId: _disputeId,
_dispute: _dispute,
_blockNumber: block.number
_dispute: _dispute
});

// Only the first dispute of a request should go through the bond escalation
Expand Down
3 changes: 1 addition & 2 deletions solidity/contracts/modules/dispute/BondedDisputeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ contract BondedDisputeModule is Module, IBondedDisputeModule {
_requestId: _dispute.requestId,
_responseId: _dispute.responseId,
_disputeId: _getId(_dispute),
_dispute: _dispute,
_blockNumber: block.number
_dispute: _dispute
});
}

Expand Down
3 changes: 1 addition & 2 deletions solidity/contracts/modules/dispute/CircuitResolverModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ contract CircuitResolverModule is Module, ICircuitResolverModule {
_requestId: _response.requestId,
_responseId: _dispute.responseId,
_disputeId: _getId(_dispute),
_dispute: _dispute,
_blockNumber: block.number
_dispute: _dispute
});

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ contract RootVerificationModule is Module, IRootVerificationModule {
_requestId: _response.requestId,
_responseId: _dispute.responseId,
_disputeId: _getId(_dispute),
_dispute: _dispute,
_blockNumber: block.number
_dispute: _dispute
});

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);
Expand Down
6 changes: 3 additions & 3 deletions solidity/contracts/modules/response/BondedResponseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract BondedResponseModule is Module, IBondedResponseModule {
});
}

emit ResponseProposed(_response.requestId, _response, block.number);
emit ResponseProposed(_response.requestId, _response);
}

/// @inheritdoc IBondedResponseModule
Expand All @@ -75,14 +75,14 @@ contract BondedResponseModule is Module, IBondedResponseModule {

bool _isModule = ORACLE.allowedModule(_response.requestId, _finalizer);

if (!_isModule && block.number < _params.deadline) {
if (!_isModule && block.timestamp < _params.deadline) {
revert BondedResponseModule_TooEarlyToFinalize();
}

uint256 _responseCreatedAt = ORACLE.responseCreatedAt(_getId(_response));

if (_responseCreatedAt != 0) {
if (!_isModule && block.number < _responseCreatedAt + _params.disputeWindow) {
if (!_isModule && block.timestamp < _responseCreatedAt + _params.disputeWindow) {
revert BondedResponseModule_TooEarlyToFinalize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ interface IBondedResponseModule is IResponseModule {
*
* @param _requestId The ID of the request that the response was proposed
* @param _response The proposed response
* @param _blockNumber The number of the block in which the response was proposed
*/
event ResponseProposed(bytes32 indexed _requestId, IOracle.Response _response, uint256 indexed _blockNumber);
event ResponseProposed(bytes32 indexed _requestId, IOracle.Response _response);

/**
* @notice Emitted when an uncalled response is released
Expand Down
6 changes: 3 additions & 3 deletions solidity/test/integration/BondEscalation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ contract Integration_BondEscalation is IntegrationBase {
assertEq(_bondEscalationAccounting.balanceOf(disputer, usdc), 0, 'Mismatch: Disputer balance');

// Step 8: Finalize request and check balances again
vm.roll(_expectedDeadline + 1 days);
vm.warp(_expectedDeadline + 1 days);
oracle.finalize(mockRequest, mockResponse);

// Test: The requester has no balance because he has paid the proposer
Expand Down Expand Up @@ -248,7 +248,7 @@ contract Integration_BondEscalation is IntegrationBase {
_responseId = oracle.proposeResponse(mockRequest, _thirdResponse);

// Step 11: It goes undisputed for three days, therefore it's deemed correct and final
vm.roll(_expectedDeadline + 1);
vm.warp(_expectedDeadline + 1);
oracle.finalize(mockRequest, _thirdResponse);

// Test: The requester has paid out the reward
Expand Down Expand Up @@ -283,7 +283,7 @@ contract Integration_BondEscalation is IntegrationBase {

// Step 12: Two days after the deadline, the resolution module says that Another proposer's answer was correct
// So Another proposer gets paid Disputer's bond
vm.roll(_expectedDeadline + 2 days);
vm.warp(_expectedDeadline + 2 days);
_mockArbitrator.setAnswer(IOracle.DisputeStatus.Lost);
oracle.resolveDispute(mockRequest, _secondResponse, _secondDispute);

Expand Down
10 changes: 5 additions & 5 deletions solidity/test/integration/Finalization.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract Integration_Finalization is IntegrationBase {
_proposeResponse();

// Traveling to the end of the dispute window
vm.roll(_expectedDeadline + 1 + _baseDisputeWindow);
vm.warp(_expectedDeadline + 1 + _baseDisputeWindow);

// Check: all external calls are made?
vm.expectCall(address(_mockCallback), abi.encodeWithSelector(IProphetCallback.prophetCallback.selector, _calldata));
Expand Down Expand Up @@ -69,13 +69,13 @@ contract Integration_Finalization is IntegrationBase {
/**
* @notice Finalizing a request with a ongoing dispute reverts.
*/
function test_revertFinalizeInDisputeWindow(uint256 _block) public {
_block = bound(_block, block.number, _expectedDeadline - _baseDisputeWindow - 1);
function test_revertFinalizeInDisputeWindow(uint256 _timestamp) public {
_timestamp = bound(_timestamp, block.timestamp, _expectedDeadline - _baseDisputeWindow - 1);

_createRequest();
_proposeResponse();

vm.roll(_block);
vm.warp(_timestamp);

// Check: reverts if called during the dispute window?
vm.expectRevert(IBondedResponseModule.BondedResponseModule_TooEarlyToFinalize.selector);
Expand All @@ -96,7 +96,7 @@ contract Integration_Finalization is IntegrationBase {
_proposeResponse();

// Traveling to the end of the dispute window
vm.roll(_expectedDeadline + 1 + _baseDisputeWindow);
vm.warp(_expectedDeadline + 1 + _baseDisputeWindow);

vm.expectCall(address(_mockCallback), abi.encodeWithSelector(IProphetCallback.prophetCallback.selector, _calldata));
vm.prank(_finalizer);
Expand Down
5 changes: 3 additions & 2 deletions solidity/test/integration/IntegrationBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ contract IntegrationBase is DSTestPlus, TestConstants, Helpers {
uint256 internal _expectedReward = 30 ether;
uint256 internal _expectedDeadline;
uint256 internal _expectedCallbackValue = 42;
uint256 internal _baseDisputeWindow = 120; // blocks
uint256 internal _baseDisputeWindow = 120 * BLOCK_TIME;
bytes32 internal _ipfsHash = bytes32('QmR4uiJH654k3Ta2uLLQ8r');
uint256 internal _blocksDeadline = 600;
uint256 internal _timestampDeadline = _blocksDeadline * BLOCK_TIME;

function setUp() public virtual {
vm.createSelectFork(vm.rpcUrl('optimism'), FORK_BLOCK);
Expand Down Expand Up @@ -135,7 +136,7 @@ contract IntegrationBase is DSTestPlus, TestConstants, Helpers {
vm.stopPrank();

// Set the expected deadline
_expectedDeadline = block.timestamp + BLOCK_TIME * _blocksDeadline;
_expectedDeadline = block.timestamp + _timestampDeadline;

// Configure the mock request
mockRequest.requestModuleData = abi.encode(
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/integration/Payments.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract Integration_Payments is IntegrationBase {
assertEq(_accountingExtension.bondedAmountOf(proposer, usdc, _requestId), _bondSize);

// Warp to finalization time.
vm.roll(_expectedDeadline + _baseDisputeWindow);
vm.warp(_expectedDeadline + _baseDisputeWindow);

// Finalize request/response
oracle.finalize(mockRequest, mockResponse);
Expand Down Expand Up @@ -66,7 +66,7 @@ contract Integration_Payments is IntegrationBase {
assertEq(_accountingExtension.bondedAmountOf(proposer, weth, _requestId), _bondSize);

// Warp to finalization time.
vm.roll(_expectedDeadline + _baseDisputeWindow);
vm.warp(_expectedDeadline + _baseDisputeWindow);
// Finalize request/response.
oracle.finalize(mockRequest, mockResponse);

Expand Down
8 changes: 4 additions & 4 deletions solidity/test/integration/RequestCreation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ contract Integration_RequestCreation is IntegrationBase {
// Check: saved the correct nonce?
assertEq(oracle.nonceToRequestId(mockRequest.nonce), _requestId);

// Check: saved the correct creation block?
assertEq(oracle.requestCreatedAt(_requestId), block.number);
// Check: saved the correct creation timestamp?
assertEq(oracle.requestCreatedAt(_requestId), block.timestamp);

// Check: saved the allowed modules?
assertTrue(oracle.allowedModule(_requestId, mockRequest.requestModule));
Expand All @@ -60,8 +60,8 @@ contract Integration_RequestCreation is IntegrationBase {
// Check: saved the correct nonce?
assertEq(oracle.nonceToRequestId(mockRequest.nonce), _requestId);

// Check: saved the correct creation block?
assertEq(oracle.requestCreatedAt(_requestId), block.number);
// Check: saved the correct creation timestamp?
assertEq(oracle.requestCreatedAt(_requestId), block.timestamp);

// Check: saved the allowed modules?
assertTrue(oracle.allowedModule(_requestId, mockRequest.requestModule));
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/integration/ResponseDispute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract Integration_ResponseDispute is IntegrationBase {
assertEq(oracle.disputeOf(_getId(mockResponse)), _disputeId);

// Check: creation time is correct?
assertEq(oracle.disputeCreatedAt(_disputeId), block.number);
assertEq(oracle.disputeCreatedAt(_disputeId), block.timestamp);
}

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ contract Integration_ResponseDispute is IntegrationBase {
* @notice Disputing a finalized response should revert
*/
function test_disputeResponse_alreadyFinalized() public {
vm.roll(_expectedDeadline + _baseDisputeWindow);
vm.warp(_expectedDeadline + _baseDisputeWindow);
oracle.finalize(mockRequest, mockResponse);

vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_AlreadyFinalized.selector, _getId(mockRequest)));
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/integration/ResponseProposal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ contract Integration_ResponseProposal is IntegrationBase {
bytes32[] memory _getResponseIds = oracle.getResponseIds(_requestId);
assertEq(_getResponseIds[0], _getId(mockResponse));

// Check: the creation block is correct?
assertEq(oracle.responseCreatedAt(_getId(mockResponse)), block.number);
// Check: the creation timestamp is correct?
assertEq(oracle.responseCreatedAt(_getId(mockResponse)), block.timestamp);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/integration/RootVerification.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ contract Integration_RootVerification is IntegrationBase {
vm.prank(proposer);
oracle.proposeResponse(mockRequest, mockResponse);

vm.roll(_expectedDeadline + _baseDisputeWindow);
vm.warp(_expectedDeadline + _baseDisputeWindow);

oracle.finalize(mockRequest, mockResponse);
}
Expand Down
15 changes: 5 additions & 10 deletions solidity/test/unit/modules/dispute/BondEscalationModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ contract BaseTest is Test, Helpers {
bytes32 indexed _requestId, bytes32 indexed _disputeId, IBondEscalationModule.BondEscalationStatus _status
);
event ResponseDisputed(
bytes32 indexed _requestId,
bytes32 indexed _responseId,
bytes32 indexed _disputeId,
IOracle.Dispute _dispute,
uint256 _blockNumber
bytes32 indexed _requestId, bytes32 indexed _responseId, bytes32 indexed _disputeId, IOracle.Dispute _dispute
);
event DisputeStatusChanged(bytes32 indexed _disputeId, IOracle.Dispute _dispute, IOracle.DisputeStatus _status);

Expand Down Expand Up @@ -430,7 +426,7 @@ contract BondEscalationModule_Unit_DisputeResponse is BaseTest {
mockDispute.responseId = _responseId;

// Warp to a time after the disputeWindow is over.
vm.roll(block.number + _disputeWindow + 1);
vm.warp(block.timestamp + _disputeWindow + 1);

// Mock and expect IOracle.responseCreatedAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.responseCreatedAt, (_responseId)), abi.encode(1));
Expand All @@ -448,7 +444,7 @@ contract BondEscalationModule_Unit_DisputeResponse is BaseTest {
uint256 _timestamp,
IBondEscalationModule.RequestParameters memory _params
) public assumeFuzzable(address(_params.accountingExtension)) {
_timestamp = bound(_timestamp, 1, type(uint128).max);
_timestamp = bound(_timestamp, 1, 365 days);
// Set deadline to timestamp so we are still in the bond escalation period
_params.bondEscalationDeadline = _timestamp - 1;
_params.disputeWindow = _timestamp + 1;
Expand Down Expand Up @@ -522,8 +518,7 @@ contract BondEscalationModule_Unit_DisputeResponse is BaseTest {
_requestId: _requestId,
_responseId: _responseId,
_disputeId: _disputeId,
_dispute: mockDispute,
_blockNumber: block.number
_dispute: mockDispute
});

// Check: is the event emitted?
Expand Down Expand Up @@ -578,7 +573,7 @@ contract BondEscalationModule_Unit_DisputeResponse is BaseTest {

// Check: is the event emitted?
vm.expectEmit(true, true, true, true, address(bondEscalationModule));
emit ResponseDisputed(_requestId, _responseId, _disputeId, mockDispute, block.number);
emit ResponseDisputed(_requestId, _responseId, _disputeId, mockDispute);

vm.prank(address(oracle));
bondEscalationModule.disputeResponse(mockRequest, mockResponse, mockDispute);
Expand Down
8 changes: 2 additions & 6 deletions solidity/test/unit/modules/dispute/BondedDisputeModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ contract BaseTest is Test, Helpers {

event DisputeStatusChanged(bytes32 indexed _disputeId, IOracle.Dispute _dispute, IOracle.DisputeStatus _status);
event ResponseDisputed(
bytes32 indexed _requestId,
bytes32 indexed _responseId,
bytes32 indexed _disputeId,
IOracle.Dispute _dispute,
uint256 _blockNumber
bytes32 indexed _requestId, bytes32 indexed _responseId, bytes32 indexed _disputeId, IOracle.Dispute _dispute
);

/**
Expand Down Expand Up @@ -259,7 +255,7 @@ contract BondedDisputeModule_Unit_DisputeResponse is BaseTest {

// Expect the event
vm.expectEmit(true, true, true, true, address(bondedDisputeModule));
emit ResponseDisputed(_requestId, _getId(mockResponse), _getId(mockDispute), mockDispute, block.number);
emit ResponseDisputed(_requestId, _getId(mockResponse), _getId(mockDispute), mockDispute);

// Test: call disputeResponse
vm.prank(address(oracle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ contract BaseTest is Test, Helpers {
// Events
event DisputeStatusChanged(bytes32 indexed _disputeId, IOracle.Dispute _dispute, IOracle.DisputeStatus _status);
event ResponseDisputed(
bytes32 indexed _requestId,
bytes32 indexed _responseId,
bytes32 indexed _disputeId,
IOracle.Dispute _dispute,
uint256 _blockNumber
bytes32 indexed _requestId, bytes32 indexed _responseId, bytes32 indexed _disputeId, IOracle.Dispute _dispute
);

/**
Expand Down Expand Up @@ -217,8 +213,7 @@ contract CircuitResolverModule_Unit_DisputeResponse is BaseTest {
_requestId: mockResponse.requestId,
_responseId: mockDispute.responseId,
_disputeId: _getId(mockDispute),
_dispute: mockDispute,
_blockNumber: block.number
_dispute: mockDispute
});

vm.prank(address(oracle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ contract BaseTest is Test, Helpers {

// Events
event ResponseDisputed(
bytes32 indexed _requestId,
bytes32 indexed _responseId,
bytes32 indexed _disputeId,
IOracle.Dispute _dispute,
uint256 _blockNumber
bytes32 indexed _requestId, bytes32 indexed _responseId, bytes32 indexed _disputeId, IOracle.Dispute _dispute
);

/**
Expand Down Expand Up @@ -266,8 +262,7 @@ contract RootVerificationModule_Unit_DisputeResponse is BaseTest {
_requestId: mockDispute.requestId,
_responseId: mockDispute.responseId,
_disputeId: _getId(mockDispute),
_dispute: mockDispute,
_blockNumber: block.number
_dispute: mockDispute
});

vm.prank(address(oracle));
Expand Down
Loading

0 comments on commit 2d91875

Please sign in to comment.