Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Jan 15, 2025
1 parent 7cd8328 commit 9e046dc
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions target_chains/ethereum/contracts/forge-test/Pulse.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,19 @@ contract PulseTest is Test, PulseEvents {
PulseState.Request memory expectedRequest = PulseState.Request({
sequenceNumber: 1,
publishTime: publishTime,
priceIdsHash: keccak256(abi.encode(priceIds)),
priceIds: [
priceIds[0],
priceIds[1],
bytes32(0), // Fill remaining slots with zero
bytes32(0),
bytes32(0),
bytes32(0),
bytes32(0),
bytes32(0),
bytes32(0),
bytes32(0)
],
numPriceIds: 2,
callbackGasLimit: CALLBACK_GAS_LIMIT,
requester: address(consumer)
});
Expand All @@ -215,7 +227,10 @@ contract PulseTest is Test, PulseEvents {
PulseState.Request memory lastRequest = pulse.getRequest(1);
assertEq(lastRequest.sequenceNumber, expectedRequest.sequenceNumber);
assertEq(lastRequest.publishTime, expectedRequest.publishTime);
assertEq(lastRequest.priceIdsHash, expectedRequest.priceIdsHash);
assertEq(lastRequest.numPriceIds, expectedRequest.numPriceIds);
for (uint8 i = 0; i < lastRequest.numPriceIds; i++) {
assertEq(lastRequest.priceIds[i], expectedRequest.priceIds[i]);
}
assertEq(
lastRequest.callbackGasLimit,
expectedRequest.callbackGasLimit
Expand Down Expand Up @@ -627,17 +642,13 @@ contract PulseTest is Test, PulseEvents {
mockParsePriceFeedUpdates(priceFeeds);
bytes[] memory updateData = createMockUpdateData(priceFeeds);

// Calculate hashes for both arrays
bytes32 providedPriceIdsHash = keccak256(abi.encode(wrongPriceIds));
bytes32 storedPriceIdsHash = keccak256(abi.encode(priceIds));

// Should revert when trying to execute with wrong priceIds
vm.prank(updater);
vm.expectRevert(
abi.encodeWithSelector(
InvalidPriceIds.selector,
providedPriceIdsHash,
storedPriceIdsHash
wrongPriceIds[0],
priceIds[0]
)
);
pulse.executeCallback(sequenceNumber, updateData, wrongPriceIds);
Expand Down Expand Up @@ -679,4 +690,30 @@ contract PulseTest is Test, PulseEvents {
assertEq(consumer.lastSequenceNumber(), sequenceNumber);
assertEq(consumer.lastUpdater(), updater);
}

function testRevertOnTooManyPriceIds() public {
uint256 maxPriceIds = uint256(pulse.MAX_PRICE_IDS());
// Create array with MAX_PRICE_IDS + 1 price IDs
bytes32[] memory priceIds = new bytes32[](maxPriceIds + 1);
for (uint i = 0; i < priceIds.length; i++) {
priceIds[i] = bytes32(uint256(i + 1));
}

vm.deal(address(consumer), 1 gwei);
uint128 totalFee = calculateTotalFee();

vm.prank(address(consumer));
vm.expectRevert(
abi.encodeWithSelector(
TooManyPriceIds.selector,
maxPriceIds + 1,
maxPriceIds
)
);
pulse.requestPriceUpdatesWithCallback{value: totalFee}(
block.timestamp,
priceIds,
CALLBACK_GAS_LIMIT
);
}
}

0 comments on commit 9e046dc

Please sign in to comment.