Skip to content

Commit

Permalink
feat: allow to duplicate request id (#9)
Browse files Browse the repository at this point in the history
# 🤖 Linear

Closes GRT-78
  • Loading branch information
ashitakah authored Aug 16, 2024
1 parent 3cda558 commit b3966e3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/contracts/EBORequestCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ contract EBORequestCreator is IEBORequestCreator, Arbitrable {
if (_epoch > epochManager.currentEpoch() || START_EPOCH > _epoch) revert EBORequestCreator_InvalidEpoch();

bytes32 _encodedChainId;
bytes32 _requestId;

IOracle.Request memory _requestData = requestData;

for (uint256 _i; _i < _chainIds.length; _i++) {
_encodedChainId = _encodeChainId(_chainIds[_i]);
if (!_chainIdsAllowed.contains(_encodedChainId)) revert EBORequestCreator_ChainNotAdded();

if (requestIdPerChainAndEpoch[_chainIds[_i]][_epoch] == bytes32(0)) {
_requestId = requestIdPerChainAndEpoch[_chainIds[_i]][_epoch];

if (
_requestId == bytes32(0)
|| (ORACLE.finalizedAt(_requestId) > 0 && ORACLE.finalizedResponseId(_requestId) == bytes32(0))
) {
// TODO: CREATE REQUEST DATA
bytes32 _requestId = ORACLE.createRequest(_requestData, bytes32(0));
_requestId = ORACLE.createRequest(_requestData, bytes32(0));

requestIdPerChainAndEpoch[_chainIds[_i]][_epoch] = _requestId;

Expand Down
105 changes: 103 additions & 2 deletions test/unit/EBORequestCreator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ contract EBORequestCreator_Unit_CreateRequest is EBORequestCreator_Unit_BaseTest

eboRequestCreator.createRequests(_epoch + 1, new string[](1));
}

/**
* @notice Test if the request id exists skip the request creation
*/

function test_expectNotEmitRequestIdExists(
uint256 _epoch,
string calldata _chainId,
Expand All @@ -205,17 +205,85 @@ contract EBORequestCreator_Unit_CreateRequest is EBORequestCreator_Unit_BaseTest
eboRequestCreator.setChainIdForTest(_chainId);
eboRequestCreator.setRequestIdPerChainAndEpochForTest(_chainId, _epoch, _requestId);

vm.mockCall(address(oracle), abi.encodeWithSelector(IOracle.finalizedAt.selector, _requestId), abi.encode(0));

vm.expectCall(address(oracle), abi.encodeWithSelector(IOracle.createRequest.selector), 0);

string[] memory _chainIds = new string[](1);
_chainIds[0] = _chainId;

eboRequestCreator.createRequests(_epoch, _chainIds);

assertEq(eboRequestCreator.requestIdPerChainAndEpoch(_chainId, _epoch), _requestId);
}

/**
* @notice Test the create requests
* @notice Test if the request id skip the request creation because the response id is already finalized
*/
function test_expectNotEmitRequestIdHasResponse(
uint256 _epoch,
string calldata _chainId,
bytes32 _requestId,
uint96 _finalizedAt
) external happyPath(_epoch, new string[](1)) {
vm.assume(_finalizedAt > 0);
vm.assume(_requestId != bytes32(0));
eboRequestCreator.setChainIdForTest(_chainId);
eboRequestCreator.setRequestIdPerChainAndEpochForTest(_chainId, _epoch, _requestId);

vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.finalizedAt.selector, _requestId), abi.encode(_finalizedAt)
);

vm.mockCall(
address(oracle),
abi.encodeWithSelector(IOracle.finalizedResponseId.selector, _requestId),
abi.encode(bytes32(keccak256('response')))
);

vm.expectCall(address(oracle), abi.encodeWithSelector(IOracle.createRequest.selector), 0);

string[] memory _chainIds = new string[](1);
_chainIds[0] = _chainId;

eboRequestCreator.createRequests(_epoch, _chainIds);

assertEq(eboRequestCreator.requestIdPerChainAndEpoch(_chainId, _epoch), _requestId);
}

/**
* @notice Test if the request id skip because the request didn't finalize
*/
function test_expectNotEmitRequestIdExistsBlockNumber(
uint256 _epoch,
string calldata _chainId,
bytes32 _requestId,
uint96 _finalizedAt
) external happyPath(_epoch, new string[](1)) {
vm.assume(_finalizedAt == 0);
vm.assume(_requestId != bytes32(0));
eboRequestCreator.setChainIdForTest(_chainId);
eboRequestCreator.setRequestIdPerChainAndEpochForTest(_chainId, _epoch, _requestId);

vm.mockCall(address(oracle), abi.encodeWithSelector(IOracle.finalizedAt.selector, _requestId), abi.encode(0));

vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.finalizedResponseId.selector, _requestId), abi.encode(bytes32(0))
);

vm.expectCall(address(oracle), abi.encodeWithSelector(IOracle.createRequest.selector), 0);

string[] memory _chainIds = new string[](1);
_chainIds[0] = _chainId;

eboRequestCreator.createRequests(_epoch, _chainIds);

assertEq(eboRequestCreator.requestIdPerChainAndEpoch(_chainId, _epoch), _requestId);
}

/**
* @notice Test the create requests
*/
function test_emitCreateRequest(
uint256 _epoch,
string[] calldata _chainIds,

Check warning on line 289 in test/unit/EBORequestCreator.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "_chainIds" is unused

Check warning on line 289 in test/unit/EBORequestCreator.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "_chainIds" is unused

Check warning on line 289 in test/unit/EBORequestCreator.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "_chainIds" is unused
Expand All @@ -229,6 +297,39 @@ contract EBORequestCreator_Unit_CreateRequest is EBORequestCreator_Unit_BaseTest

eboRequestCreator.createRequests(_epoch, _cleanChainIds);
}

/**
* @notice Test the create requests because finalize with no response
*/
function test_emitCreateRequestWithNoResponse(
uint256 _epoch,
string calldata _chainId,
bytes32 _requestId,
uint96 _finalizedAt
) external happyPath(_epoch, new string[](1)) {
vm.assume(_finalizedAt > 0);
vm.assume(_requestId != bytes32(0));
eboRequestCreator.setChainIdForTest(_chainId);
eboRequestCreator.setRequestIdPerChainAndEpochForTest(_chainId, _epoch, _requestId);

vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.finalizedAt.selector, _requestId), abi.encode(_finalizedAt)
);

vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.finalizedResponseId.selector, _requestId), abi.encode(bytes32(0))
);

vm.mockCall(address(oracle), abi.encodeWithSelector(IOracle.createRequest.selector), abi.encode(_requestId));

string[] memory _chainIds = new string[](1);
_chainIds[0] = _chainId;

vm.expectEmit();
emit RequestCreated(_requestId, _epoch, _chainId);

eboRequestCreator.createRequests(_epoch, _chainIds);
}
}

contract EBORequestCreator_Unit_AddChain is EBORequestCreator_Unit_BaseTest {
Expand Down

0 comments on commit b3966e3

Please sign in to comment.