Skip to content

Commit

Permalink
feat: allow to duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ashitakah committed Aug 15, 2024
1 parent 34b2351 commit 5093ddd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/contracts/EBORequestCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ contract EBORequestCreator is IEBORequestCreator, Arbitrable {
/// @inheritdoc IEBORequestCreator
function createRequests(uint256 _epoch, string[] calldata _chainIds) external {
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)
|| (block.number > oracle.finalizedAt(_requestId) && 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
66 changes: 63 additions & 3 deletions test/unit/EBORequestCreator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ contract EBORequestCreator_Unit_CreateRequest is EBORequestCreator_Unit_BaseTest
}

/**
* @notice Test if the request id exists skip the request creation
* @notice Test if the request id skip because the block number is less than the finalized at
*/
function test_expectNotEmitRequestIdExists(
function test_expectNotEmitRequestIdExistsBlockNumber(
uint256 _epoch,
string calldata _chainId,
bytes32 _requestId
Expand All @@ -146,17 +146,51 @@ 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(block.number)
);

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

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

eboRequestCreator.createRequests(_epoch, _chainIds);
}

/**
* @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
) external happyPath(_epoch, new string[](1)) {
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(block.number - 1)
);

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);
}

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

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

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "_chainIds" is unused
Expand All @@ -170,6 +204,32 @@ 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
) external happyPath(_epoch, new string[](1)) {
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(block.number)
);

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

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

eboRequestCreator.createRequests(_epoch, _chainIds);
}
}

contract EBORequestCreator_Unit_AddChain is EBORequestCreator_Unit_BaseTest {
Expand Down

0 comments on commit 5093ddd

Please sign in to comment.