Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: store participants in a bytes field #4

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet

contract Oracle is IOracle {
using EnumerableSet for EnumerableSet.Bytes32Set;
using EnumerableSet for EnumerableSet.AddressSet;

/// @inheritdoc IOracle
mapping(bytes32 _responseId => bytes32 _disputeId) public disputeOf;
Expand All @@ -33,7 +32,7 @@ contract Oracle is IOracle {
/**
* @notice The list of the participants for each request
*/
mapping(bytes32 _requestId => EnumerableSet.AddressSet _participants) internal _participants;
mapping(bytes32 _requestId => bytes _participants) internal _participants;

/**
* @notice The finalized response for each request
Expand Down Expand Up @@ -194,7 +193,7 @@ contract Oracle is IOracle {
}

_responseId = keccak256(abi.encodePacked(_proposer, address(this), _requestId, _responseNonce++));
_participants[_requestId].add(_proposer);
_participants[_requestId] = abi.encodePacked(_participants[_requestId], _proposer);
_responses[_responseId] = _request.responseModule.propose(_requestId, _proposer, _responseData, msg.sender);
_responseIds[_requestId].add(_responseId);

Expand Down Expand Up @@ -241,7 +240,7 @@ contract Oracle is IOracle {
}

_disputeId = keccak256(abi.encodePacked(msg.sender, _requestId, _responseId));
_participants[_requestId].add(msg.sender);
_participants[_requestId] = abi.encodePacked(_participants[_requestId], msg.sender);

Dispute memory _dispute =
_request.disputeModule.disputeResponse(_requestId, _responseId, msg.sender, _response.proposer);
Expand Down Expand Up @@ -326,8 +325,27 @@ contract Oracle is IOracle {
|| address(_request.finalityModule) == _module;
}

// @inheritdoc IOracle
function isParticipant(bytes32 _requestId, address _user) external view returns (bool _isParticipant) {
_isParticipant = _participants[_requestId].contains(_user);
bytes memory _requestParticipants = _participants[_requestId];

assembly {
let length := mload(_requestParticipants)
let i := 0

// Iterate 20-bytes chunks of the participants data
for {} lt(i, length) { i := add(i, 20) } {
// Load the participant at index i
let _participant := mload(add(add(_requestParticipants, 0x20), i))

// Shift the participant to the right by 96 bits and compare with _user
if eq(shr(96, _participant), _user) {
// Set _isParticipant to true and return
mstore(0x00, 1)
return(0x00, 32)
}
}
}
}

/// @inheritdoc IOracle
Expand Down Expand Up @@ -436,7 +454,7 @@ contract Oracle is IOracle {
});

_requests[_requestId] = _storedRequest;
_participants[_requestId].add(msg.sender);
_participants[_requestId] = abi.encodePacked(_participants[_requestId], msg.sender);

_request.requestModule.setupRequest(_requestId, _request.requestModuleData);
_request.responseModule.setupRequest(_requestId, _request.responseModuleData);
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ contract ForTest_Oracle is Oracle {
}

function forTest_addParticipant(bytes32 _requestId, address _participant) external {
_participants[_requestId].add(_participant);
_participants[_requestId] = abi.encodePacked(_participants[_requestId], _participant);
}

function forTest_setFinalizedResponseId(bytes32 _requestId, bytes32 _finalizedResponseId) external {
Expand Down
Loading