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

refactor: remove match bytes #51

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 15 additions & 62 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,16 @@ contract Oracle is IOracle {
/// @inheritdoc IOracle
mapping(bytes32 _requestId => bytes32 _finalizedResponseId) public finalizedResponseId;

/**
* @notice The list of the response ids for each request
*/
mapping(bytes32 _requestId => bytes _responseIds) internal _responseIds;
/// @inheritdoc IOracle
mapping(bytes32 _requestId => mapping(address _module => bool _allowed)) public allowedModule;

/**
* @notice The list of the participants for each request
*/
mapping(bytes32 _requestId => bytes _participants) internal _participants;
/// @inheritdoc IOracle
mapping(bytes32 _requestId => mapping(address _user => bool _isParticipant)) public isParticipant;

/**
* @notice The list of the allowed modules for each request
* @notice The list of the response ids for each request
*/
mapping(bytes32 _requestId => bytes _allowedModules) internal _allowedModules;
mapping(bytes32 _requestId => bytes _responseIds) internal _responseIds;

/// @inheritdoc IOracle
uint256 public totalRequestCount;
Expand Down Expand Up @@ -128,8 +124,7 @@ contract Oracle is IOracle {
if (finalizedAt[_requestId] != 0) {
revert Oracle_AlreadyFinalized(_requestId);
}

_participants[_requestId] = abi.encodePacked(_participants[_requestId], _response.proposer);
isParticipant[_requestId][_response.proposer] = true;
IResponseModule(_request.responseModule).propose(_request, _response, msg.sender);
_responseIds[_requestId] = abi.encodePacked(_responseIds[_requestId], _responseId);
responseCreatedAt[_responseId] = block.timestamp;
Expand Down Expand Up @@ -167,8 +162,7 @@ contract Oracle is IOracle {
if (disputeOf[_responseId] != bytes32(0)) {
revert Oracle_ResponseAlreadyDisputed(_responseId);
}

_participants[_requestId] = abi.encodePacked(_participants[_requestId], msg.sender);
isParticipant[_requestId][msg.sender] = true;
disputeStatus[_disputeId] = DisputeStatus.Active;
disputeOf[_responseId] = _disputeId;
disputeCreatedAt[_disputeId] = block.timestamp;
Expand Down Expand Up @@ -261,45 +255,6 @@ contract Oracle is IOracle {
emit DisputeStatusUpdated(_disputeId, _dispute, _status);
}

/**
* @notice Matches a bytes32 value against an encoded list of values
*
* @param _sought The value to be matched
* @param _list The encoded list of values
* @param _chunkSize The size of each chunk in bytes
* @return _found Whether the value was found or not
*/
function _matchBytes(bytes32 _sought, bytes memory _list, uint256 _chunkSize) internal pure returns (bool _found) {
assembly {
let length := mload(_list)
let i := 0
let shiftBy := sub(256, mul(_chunkSize, 8))

// Iterate N-bytes chunks of the list
for {} lt(i, length) { i := add(i, _chunkSize) } {
// Load the value at index i
let _chunk := mload(add(add(_list, 0x20), i))

// Shift the value to the right and compare with _sought
if eq(shr(shiftBy, _chunk), _sought) {
// Set _found to true and return
_found := true
break
}
}
}
}

/// @inheritdoc IOracle
function allowedModule(bytes32 _requestId, address _module) external view returns (bool _isAllowed) {
_isAllowed = _matchBytes(bytes32(uint256(uint160(_module))), _allowedModules[_requestId], 20);
}

/// @inheritdoc IOracle
function isParticipant(bytes32 _requestId, address _user) external view returns (bool _isParticipant) {
_isParticipant = _matchBytes(bytes32(uint256(uint160(_user))), _participants[_requestId], 20);
}

/// @inheritdoc IOracle
function getResponseIds(bytes32 _requestId) public view returns (bytes32[] memory _ids) {
bytes memory _responses = _responseIds[_requestId];
Expand Down Expand Up @@ -432,16 +387,14 @@ contract Oracle is IOracle {
nonceToRequestId[_requestNonce] = _requestId;
requestCreatedAt[_requestId] = block.timestamp;

// solhint-disable-next-line func-named-parameters
_allowedModules[_requestId] = abi.encodePacked(
_request.requestModule,
_request.responseModule,
_request.disputeModule,
_request.resolutionModule,
_request.finalityModule
);
allowedModule[_requestId][_request.requestModule] = true;
allowedModule[_requestId][_request.responseModule] = true;
allowedModule[_requestId][_request.disputeModule] = true;
allowedModule[_requestId][_request.resolutionModule] = true;
allowedModule[_requestId][_request.finalityModule] = true;

isParticipant[_requestId][msg.sender] = true;

_participants[_requestId] = abi.encodePacked(_participants[_requestId], msg.sender);
IRequestModule(_request.requestModule).createRequest(_requestId, _request.requestModuleData, msg.sender);

emit RequestCreated(_requestId, _request, _ipfsHash);
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ contract MockOracle is Oracle {
constructor() Oracle() {}

function mock_addParticipant(bytes32 _requestId, address _participant) external {
_participants[_requestId] = abi.encodePacked(_participants[_requestId], _participant);
isParticipant[_requestId][_participant] = true;
}

function mock_addAllowedModule(bytes32 _requestId, address _module) external {
_allowedModules[_requestId] = abi.encodePacked(_allowedModules[_requestId], _module);
allowedModule[_requestId][_module] = true;
}

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