Skip to content

Commit

Permalink
perf: store participants in a bytes field (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent authored Oct 26, 2023
1 parent a5f05d5 commit d6ded22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
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/integration/IntegrationBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {TestConstants} from '../utils/TestConstants.sol';
// solhint-enable no-unused-import

contract IntegrationBase is DSTestPlus, TestConstants, Helpers {
uint256 public constant FORK_BLOCK = 756_611;
uint256 public constant FORK_BLOCK = 111_361_902;

uint256 internal _initialBalance = 100_000 ether;

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

0 comments on commit d6ded22

Please sign in to comment.