Skip to content

Commit

Permalink
test(medusa): rewrite prop-1 to assert
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJabberwock committed Nov 25, 2024
1 parent 8072e09 commit 1c33374
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
2 changes: 2 additions & 0 deletions test/invariants/handlers/HandlerEBORequestCreator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ contract HandlerEBORequestCreator is BaseHandler {
}

function handleCreateRequest(uint256 _epoch, uint256 _chainIdSeed) external {
_epoch = bound(_epoch, START_EPOCH, block.timestamp);

string memory chainId = _getRandomChainId(_chainIdSeed);
if (bytes(chainId).length == 0) return;

Expand Down
22 changes: 22 additions & 0 deletions test/invariants/helpers/Utils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ contract Utils {
}
}

function assertEq(bytes32 a, bytes32 b) internal {
assertEq(a, b, 'assertEq: a != b');
}

function assertEq(bytes32 a, bytes32 b, string memory reason) internal {
if (a != b) {
emit TestFailure(reason);
assert(false);
}
}

function assertNotEq(bytes32 a, bytes32 b) internal {
assertNotEq(a, b, 'assertNotEq: a == b');
}

function assertNotEq(bytes32 a, bytes32 b, string memory reason) internal {
if (a == b) {
emit TestFailure(reason);
assert(false);
}
}

function assertEq(bytes memory a, bytes memory b) internal {
assertEq(a, b, 'assertEq: a != b');
}
Expand Down
43 changes: 12 additions & 31 deletions test/invariants/properties/PropertyRequester.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,21 @@ contract PropertyRequester is HandlerParent {
constructor() {}

/// @custom:property-id 1
/// @custom:property Requester can always create a request as long as the same chainId/epoch isn't requested yet
function property_canAlwaysCreateRequest(uint256 _epoch, uint256 _chainIdSeed) external {
string memory chainId = _getRandomChainId(_chainIdSeed);
_epoch = bound(_epoch, START_EPOCH, block.timestamp);

if (bytes(chainId).length == 0) return;

// Prevent duplicate chainId for same epoch
if (_ghost_requestsPerEpochChainId[_epoch][chainId].length > 0) return;

// Create request via EBORequestCreator
try eboRequestCreator.createRequest(_epoch, chainId) {
// Get current request data
IOracle.Request memory requestData = eboRequestCreator.getRequestData();

// Build request module parameters
IEBORequestModule.RequestParameters memory requestParams =
abi.decode(requestData.requestModuleData, (IEBORequestModule.RequestParameters));
requestParams.epoch = _epoch;
requestParams.chainId = chainId;
requestData.requestModuleData = abi.encode(requestParams);
/// @custom:property There can only be one request finalized with response per chainId/epoch
function property_onlyOneRequestFinalizedWithResponse(uint256 _requestIdSeed) external {
bytes32 requestId = _getRandomRequestId(_requestIdSeed);
if (requestId == bytes32(0)) return;

// Calculate request ID using same logic as Oracle
bytes32 requestId = keccak256(abi.encode(requestData));
IOracle.Request memory requestData = _ghost_requestData[requestId];
IEBORequestModule.RequestParameters memory requestParams =
abi.decode(requestData.requestModuleData, (IEBORequestModule.RequestParameters));

// Track the request
_ghost_requests.push(requestId);
_ghost_requestsPerEpochChainId[_epoch][chainId].push(requestId);
_ghost_requestData[requestId] = requestData;
_ghost_validRequests[requestId] = true;
uint256 requestsPerEpochChainId = _ghost_requestsPerEpochChainId[requestParams.epoch][requestParams.chainId].length;
if (requestsPerEpochChainId < 2) return;

emit RequestCreated(requestId, _epoch, chainId);
} catch {
assertTrue(false, 'prop-1: create request reverted');
for (uint256 i; i < requestsPerEpochChainId - 1; ++i) {
requestId = _ghost_requestsPerEpochChainId[requestParams.epoch][requestParams.chainId][i];
assertEq(oracle.finalizedResponseId(requestId), 0, 'prop-1: same chainId/epoch request finalized with response');
}
}

Expand Down

0 comments on commit 1c33374

Please sign in to comment.