Skip to content

Commit

Permalink
test: update multiple callbacks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xorsal committed Oct 14, 2024
1 parent 927488b commit 3cfca5a
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions solidity/test/integration/MultipleCallbacksModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,81 @@ pragma solidity ^0.8.19;
import './IntegrationBase.sol';

contract Integration_MultipleCallbackModule is IntegrationBase {
IProphetCallback internal _callback;
MultipleCallbacksModule internal _multipleCallbacksModule;
uint256 public constant CALLBACKS_AMOUNT = 255;
IProphetCallback public callback;
MultipleCallbacksModule public multipleCallbacksModule;

bytes32 internal _requestId;
uint256 internal _correctValue = 123;

function setUp() public override {
super.setUp();

_multipleCallbacksModule = new MultipleCallbacksModule(oracle);
mockRequest.finalityModule = address(_multipleCallbacksModule);
multipleCallbacksModule = new MultipleCallbacksModule(oracle);
mockRequest.finalityModule = address(multipleCallbacksModule);

_callback = new MockCallback();
callback = new MockCallback();
}

function test_finalizeExecutesCallback(bytes memory _data, uint8 _length) public {
address[] memory _targets = new address[](_length);
bytes[] memory _datas = new bytes[](_length);
for (uint256 _i; _i < _length; _i++) {
_targets[_i] = address(_callback);
_datas[_i] = _data;
}

function test_finalizeExecutesCallback() public {
(address[] memory _targets, bytes[] memory _datas) = _createCallbacksData(address(callback), CALLBACKS_AMOUNT);
mockRequest.finalityModuleData =
abi.encode(IMultipleCallbacksModule.RequestParameters({targets: _targets, data: _datas}));
_resetMockIds();

//
_deposit(_accountingExtension, requester, usdc, _expectedReward);
vm.prank(requester);
_accountingExtension.approveModule(address(_requestModule));
vm.prank(requester);
_requestId = oracle.createRequest(mockRequest, _ipfsHash);

//
_deposit(_accountingExtension, proposer, usdc, _expectedBondSize);
vm.prank(proposer);
_accountingExtension.approveModule(address(_responseModule));
mockResponse.response = abi.encode(proposer, _requestId, _correctValue);
vm.prank(proposer);
oracle.proposeResponse(mockRequest, mockResponse);
_setupRequest();

vm.warp(_expectedDeadline + _baseDisputeWindow);
for (uint256 _i; _i < _datas.length; _i++) {
vm.expectCall(address(_targets[_i]), abi.encodeCall(IProphetCallback.prophetCallback, (_datas[_i])));
}

vm.warp(block.timestamp + _expectedDeadline + _baseDisputeWindow);
oracle.finalize(mockRequest, mockResponse);
}

function test_callbacksNeverRevert(uint8 _length) public {
_callback = new MockFailCallback();
function test_callbacksNeverRevert() public {
callback = new MockFailCallback();

(address[] memory _targets, bytes[] memory _datas) = _createCallbacksData(address(callback), CALLBACKS_AMOUNT);

address[] memory _targets = new address[](_length);
bytes[] memory _datas = new bytes[](_length);
for (uint256 _i; _i < _length; _i++) {
_targets[_i] = address(_callback);
_datas[_i] = new bytes(0);
}
mockRequest.finalityModuleData =
abi.encode(IMultipleCallbacksModule.RequestParameters({targets: _targets, data: _datas}));

_setupRequest();

// expect call to every target with the expected data
for (uint256 _i; _i < _datas.length; _i++) {
vm.expectCall(address(_targets[_i]), abi.encodeCall(IProphetCallback.prophetCallback, (_datas[_i])));
}

vm.warp(block.timestamp + _expectedDeadline + _baseDisputeWindow);
oracle.finalize(mockRequest, mockResponse);
}

function _setupRequest() internal {
_resetMockIds();

_deposit(_accountingExtension, requester, usdc, _expectedReward);
vm.prank(requester);
vm.startPrank(requester);
_accountingExtension.approveModule(address(_requestModule));
vm.prank(requester);
_requestId = oracle.createRequest(mockRequest, _ipfsHash);
vm.stopPrank();

_deposit(_accountingExtension, proposer, usdc, _expectedBondSize);
vm.prank(proposer);
vm.startPrank(proposer);
_accountingExtension.approveModule(address(_responseModule));
mockResponse.response = abi.encode(proposer, _requestId, _correctValue);
vm.prank(proposer);
mockResponse.response = abi.encode(proposer, _requestId, bytes(''));
oracle.proposeResponse(mockRequest, mockResponse);
vm.stopPrank();
}

vm.warp(_expectedDeadline + _baseDisputeWindow);

oracle.finalize(mockRequest, mockResponse);
function _createCallbacksData(
address _target,
uint256 _length
) internal returns (address[] memory _targets, bytes[] memory _datas) {
_targets = new address[](_length);
_datas = new bytes[](_length);
for (uint256 _i; _i < _length; _i++) {
_targets[_i] = _target;
_datas[_i] = abi.encode(keccak256(abi.encode(_i)));
}
}
}

0 comments on commit 3cfca5a

Please sign in to comment.