From 3eba7b99f8640e54b089ec1a4a3dd1f996a44519 Mon Sep 17 00:00:00 2001 From: moebius <0xmoebius@tutanota.com> Date: Thu, 26 Oct 2023 11:10:51 -0300 Subject: [PATCH 1/5] perf: delete response improvement --- solidity/contracts/Oracle.sol | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/solidity/contracts/Oracle.sol b/solidity/contracts/Oracle.sol index e483326..7f3b5a3 100644 --- a/solidity/contracts/Oracle.sol +++ b/solidity/contracts/Oracle.sol @@ -206,8 +206,8 @@ contract Oracle is IOracle { /// @inheritdoc IOracle function deleteResponse(bytes32 _responseId) external { - Response memory _response = _responses[_responseId]; - Request memory _request = _requests[_response.requestId]; + Response storage _response = _responses[_responseId]; + Request storage _request = _requests[_response.requestId]; if (disputeOf[_responseId] != bytes32(0)) { revert Oracle_CannotDeleteWhileDisputing(_responseId); @@ -217,11 +217,10 @@ contract Oracle is IOracle { } _request.responseModule.deleteResponse(_response.requestId, _responseId, msg.sender); - - delete _responses[_responseId]; _responseIds[_response.requestId].remove(_responseId); emit ResponseDeleted(_response.requestId, msg.sender, _responseId); + delete _responses[_responseId]; } /// @inheritdoc IOracle From bbc46986be9d2a853a4fe062cd30abf0cd3b3d13 Mon Sep 17 00:00:00 2001 From: moebius <0xmoebius@tutanota.com> Date: Thu, 26 Oct 2023 11:41:28 -0300 Subject: [PATCH 2/5] perf: storage reads improvements --- solidity/contracts/Oracle.sol | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solidity/contracts/Oracle.sol b/solidity/contracts/Oracle.sol index 7f3b5a3..03bfef2 100644 --- a/solidity/contracts/Oracle.sol +++ b/solidity/contracts/Oracle.sol @@ -53,7 +53,7 @@ contract Oracle is IOracle { uint256 public totalRequestCount; /// @inheritdoc IOracle - function createRequest(NewRequest memory _request) external returns (bytes32 _requestId) { + function createRequest(NewRequest calldata _request) external returns (bytes32 _requestId) { _requestId = _createRequest(_request); } @@ -225,7 +225,7 @@ contract Oracle is IOracle { /// @inheritdoc IOracle function disputeResponse(bytes32 _requestId, bytes32 _responseId) external returns (bytes32 _disputeId) { - Request memory _request = _requests[_requestId]; + Request storage _request = _requests[_requestId]; if (_request.finalizedAt != 0) { revert Oracle_AlreadyFinalized(_requestId); } @@ -270,7 +270,7 @@ contract Oracle is IOracle { // Change the dispute status _dispute.status = DisputeStatus.Escalated; - Request memory _request = _requests[_dispute.requestId]; + Request storage _request = _requests[_dispute.requestId]; // Notify the dispute module about the escalation _request.disputeModule.disputeEscalated(_disputeId); @@ -285,7 +285,7 @@ contract Oracle is IOracle { /// @inheritdoc IOracle function resolveDispute(bytes32 _disputeId) external { - Dispute memory _dispute = _disputes[_disputeId]; + Dispute storage _dispute = _disputes[_disputeId]; if (_dispute.createdAt == 0) revert Oracle_InvalidDisputeId(_disputeId); // Revert if the dispute is not active nor escalated @@ -293,7 +293,7 @@ contract Oracle is IOracle { revert Oracle_CannotResolve(_disputeId); } - Request memory _request = _requests[_dispute.requestId]; + Request storage _request = _requests[_dispute.requestId]; if (address(_request.resolutionModule) == address(0)) { revert Oracle_NoResolutionModule(_disputeId); } @@ -306,7 +306,7 @@ contract Oracle is IOracle { /// @inheritdoc IOracle function updateDisputeStatus(bytes32 _disputeId, DisputeStatus _status) external { Dispute storage _dispute = _disputes[_disputeId]; - Request memory _request = _requests[_dispute.requestId]; + Request storage _request = _requests[_dispute.requestId]; if (msg.sender != address(_request.disputeModule) && msg.sender != address(_request.resolutionModule)) { revert Oracle_NotDisputeOrResolutionModule(msg.sender); } @@ -318,7 +318,7 @@ contract Oracle is IOracle { /// @inheritdoc IOracle function allowedModule(bytes32 _requestId, address _module) external view returns (bool _allowedModule) { - Request memory _request = _requests[_requestId]; + Request storage _request = _requests[_requestId]; _allowedModule = address(_request.requestModule) == _module || address(_request.responseModule) == _module || address(_request.disputeModule) == _module || address(_request.resolutionModule) == _module || address(_request.finalityModule) == _module; @@ -368,7 +368,7 @@ contract Oracle is IOracle { if (_request.finalizedAt != 0) { revert Oracle_AlreadyFinalized(_requestId); } - Response memory _response = _responses[_finalizedResponseId]; + Response storage _response = _responses[_finalizedResponseId]; if (_response.requestId != _requestId) { revert Oracle_InvalidFinalizedResponse(_finalizedResponseId); } @@ -434,7 +434,7 @@ contract Oracle is IOracle { * @param _request The request to be created * @return _requestId The id of the created request */ - function _createRequest(NewRequest memory _request) internal returns (bytes32 _requestId) { + function _createRequest(NewRequest calldata _request) internal returns (bytes32 _requestId) { uint256 _requestNonce = totalRequestCount++; _requestId = keccak256(abi.encodePacked(msg.sender, address(this), _requestNonce)); _requestIds[_requestNonce] = _requestId; From c6699c262d2f4657a8e343f610f9cee7fc65dc77 Mon Sep 17 00:00:00 2001 From: moebius <0xmoebius@tutanota.com> Date: Thu, 26 Oct 2023 11:53:14 -0300 Subject: [PATCH 3/5] perf: request struct packing --- solidity/contracts/Oracle.sol | 6 +++--- solidity/interfaces/IOracle.sol | 8 ++++---- solidity/test/unit/Oracle.t.sol | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/solidity/contracts/Oracle.sol b/solidity/contracts/Oracle.sol index 03bfef2..a7b5f66 100644 --- a/solidity/contracts/Oracle.sol +++ b/solidity/contracts/Oracle.sol @@ -378,7 +378,7 @@ contract Oracle is IOracle { } _finalizedResponses[_requestId] = _finalizedResponseId; - _request.finalizedAt = block.timestamp; + _request.finalizedAt = uint128(block.timestamp); _finalize(_requestId, _request); } @@ -406,7 +406,7 @@ contract Oracle is IOracle { } } } - _request.finalizedAt = block.timestamp; + _request.finalizedAt = uint128(block.timestamp); _finalize(_requestId, _request); } @@ -448,7 +448,7 @@ contract Oracle is IOracle { finalityModule: _request.finalityModule, requester: msg.sender, nonce: _requestNonce, - createdAt: block.timestamp, + createdAt: uint128(block.timestamp), finalizedAt: 0 }); diff --git a/solidity/interfaces/IOracle.sol b/solidity/interfaces/IOracle.sol index 51b2145..f0d38c5 100644 --- a/solidity/interfaces/IOracle.sol +++ b/solidity/interfaces/IOracle.sol @@ -198,15 +198,15 @@ interface IOracle { */ struct Request { bytes32 ipfsHash; + uint128 createdAt; + uint128 finalizedAt; + uint256 nonce; + address requester; IRequestModule requestModule; IResponseModule responseModule; IDisputeModule disputeModule; IResolutionModule resolutionModule; IFinalityModule finalityModule; - address requester; - uint256 nonce; - uint256 createdAt; - uint256 finalizedAt; } /** diff --git a/solidity/test/unit/Oracle.t.sol b/solidity/test/unit/Oracle.t.sol index e03d9fa..56c15a0 100644 --- a/solidity/test/unit/Oracle.t.sol +++ b/solidity/test/unit/Oracle.t.sol @@ -689,7 +689,7 @@ contract Unit_ProposeResponse is BaseTest { assertEq(_responseIds[1], _secondResponseId); } - function test_proposeResponseRevertsIfAlreadyFinalized(bytes calldata _responseData, uint256 _finalizedAt) public { + function test_proposeResponseRevertsIfAlreadyFinalized(bytes calldata _responseData, uint128 _finalizedAt) public { vm.assume(_finalizedAt > 0); // Create mock request From a2e6cfa24e5917b379733c8869d7f1bf9d389be4 Mon Sep 17 00:00:00 2001 From: moebius <0xmoebius@tutanota.com> Date: Thu, 26 Oct 2023 11:56:27 -0300 Subject: [PATCH 4/5] perf: more struct packing --- solidity/contracts/Oracle.sol | 2 +- solidity/interfaces/IOracle.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/solidity/contracts/Oracle.sol b/solidity/contracts/Oracle.sol index a7b5f66..c3bda7c 100644 --- a/solidity/contracts/Oracle.sol +++ b/solidity/contracts/Oracle.sol @@ -447,7 +447,7 @@ contract Oracle is IOracle { resolutionModule: _request.resolutionModule, finalityModule: _request.finalityModule, requester: msg.sender, - nonce: _requestNonce, + nonce: uint96(_requestNonce), createdAt: uint128(block.timestamp), finalizedAt: 0 }); diff --git a/solidity/interfaces/IOracle.sol b/solidity/interfaces/IOracle.sol index f0d38c5..6255388 100644 --- a/solidity/interfaces/IOracle.sol +++ b/solidity/interfaces/IOracle.sol @@ -200,7 +200,7 @@ interface IOracle { bytes32 ipfsHash; uint128 createdAt; uint128 finalizedAt; - uint256 nonce; + uint96 nonce; address requester; IRequestModule requestModule; IResponseModule responseModule; From 3ca26cd75e814dc5b87da3b0839b2261f55bb045 Mon Sep 17 00:00:00 2001 From: moebius <0xmoebius@tutanota.com> Date: Thu, 26 Oct 2023 12:06:48 -0300 Subject: [PATCH 5/5] fix: bound fuzz parameter --- solidity/test/integration/ResponseProposal.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solidity/test/integration/ResponseProposal.t.sol b/solidity/test/integration/ResponseProposal.t.sol index 2879711..2151fb3 100644 --- a/solidity/test/integration/ResponseProposal.t.sol +++ b/solidity/test/integration/ResponseProposal.t.sol @@ -98,7 +98,7 @@ contract Integration_ResponseProposal is IntegrationBase { } function test_proposeResponse_finalizedRequest(bytes memory _responseData, uint256 _timestamp) public { - vm.assume(_timestamp > _expectedDeadline + _baseDisputeWindow); + _timestamp = bound(_timestamp, _expectedDeadline + _baseDisputeWindow, type(uint128).max); vm.prank(proposer); bytes32 _responseId = oracle.proposeResponse(_requestId, _responseData);