-
Notifications
You must be signed in to change notification settings - Fork 302
feat(pulse): add getFirstActiveRequests function to retrieve active requests #2371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+267
−2
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
241b651
feat(pulse): Add getLastActiveRequests function to retrieve active re…
cctdaniel 165d20f
fix(pulse): Update gas usage comments for active requests function
cctdaniel 1c9bf3b
refactor(pulse): Rename getLastActiveRequests to getFirstActiveReques…
cctdaniel ce15f14
refactor(tests): Rename test functions for consistency in naming conv…
cctdaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ contract CustomErrorPulseConsumer is IPulseConsumer { | |
| } | ||
| } | ||
|
|
||
| contract PulseTest is Test, PulseEvents { | ||
| contract PulseTest is Test, PulseEvents, IPulseConsumer { | ||
| ERC1967Proxy public proxy; | ||
| PulseUpgradeable public pulse; | ||
| MockPulseConsumer public consumer; | ||
|
|
@@ -876,4 +876,207 @@ contract PulseTest is Test, PulseEvents { | |
| vm.prank(secondProvider); | ||
| pulse.executeCallback(sequenceNumber, updateData, priceIds); | ||
| } | ||
|
|
||
| function test_getFirstActiveRequests() public { | ||
| // Setup test data | ||
| ( | ||
| bytes32[] memory priceIds, | ||
| bytes[] memory updateData | ||
| ) = setupTestData(); | ||
| createTestRequests(priceIds); | ||
| completeRequests(updateData, priceIds); | ||
|
|
||
| testRequestScenarios(priceIds, updateData); | ||
| } | ||
|
|
||
| function setupTestData() | ||
| private | ||
| pure | ||
| returns (bytes32[] memory, bytes[] memory) | ||
| { | ||
| bytes32[] memory priceIds = new bytes32[](1); | ||
| priceIds[0] = bytes32(uint256(1)); | ||
|
|
||
| bytes[] memory updateData = new bytes[](1); | ||
| return (priceIds, updateData); | ||
| } | ||
|
|
||
| function createTestRequests(bytes32[] memory priceIds) private { | ||
| uint256 publishTime = block.timestamp; | ||
| for (uint i = 0; i < 5; i++) { | ||
| vm.deal(address(this), 1 ether); | ||
| pulse.requestPriceUpdatesWithCallback{value: 1 ether}( | ||
| publishTime, | ||
| priceIds, | ||
| 1000000 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function completeRequests( | ||
| bytes[] memory updateData, | ||
| bytes32[] memory priceIds | ||
| ) private { | ||
| // Create mock price feeds and setup Pyth response | ||
| PythStructs.PriceFeed[] memory priceFeeds = createMockPriceFeeds( | ||
| block.timestamp | ||
| ); | ||
| mockParsePriceFeedUpdates(priceFeeds); | ||
| updateData = createMockUpdateData(priceFeeds); | ||
|
|
||
| vm.deal(defaultProvider, 2 ether); // Increase ETH allocation to prevent OutOfFunds | ||
| vm.startPrank(defaultProvider); | ||
| pulse.executeCallback{value: 1 ether}(2, updateData, priceIds); | ||
| pulse.executeCallback{value: 1 ether}(4, updateData, priceIds); | ||
| vm.stopPrank(); | ||
| } | ||
|
|
||
| function testRequestScenarios( | ||
| bytes32[] memory priceIds, | ||
| bytes[] memory updateData | ||
| ) private { | ||
| // Test 1: Request more than available | ||
| checkMoreThanAvailable(); | ||
|
|
||
| // Test 2: Request exact number | ||
| checkExactNumber(); | ||
|
|
||
| // Test 3: Request fewer than available | ||
| checkFewerThanAvailable(); | ||
|
|
||
| // Test 4: Request zero | ||
| checkZeroRequest(); | ||
|
|
||
| // Test 5: Clear all and check empty | ||
| clearAllRequests(updateData, priceIds); | ||
| checkEmptyState(); | ||
| } | ||
|
Comment on lines
+934
to
+953
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a strong opinion but if these are different tests maybe they can be different tests? |
||
|
|
||
| // Split test scenarios into separate functions | ||
| function checkMoreThanAvailable() private { | ||
| (PulseState.Request[] memory requests, uint256 count) = pulse | ||
| .getFirstActiveRequests(10); | ||
| assertEq(count, 3, "Should find 3 active requests"); | ||
| assertEq(requests.length, 3, "Array should be resized to 3"); | ||
| assertEq( | ||
| requests[0].sequenceNumber, | ||
| 1, | ||
| "First request should be oldest" | ||
| ); | ||
| assertEq(requests[1].sequenceNumber, 3, "Second request should be #3"); | ||
| assertEq(requests[2].sequenceNumber, 5, "Third request should be #5"); | ||
| } | ||
|
|
||
| function checkExactNumber() private { | ||
| (PulseState.Request[] memory requests, uint256 count) = pulse | ||
| .getFirstActiveRequests(3); | ||
| assertEq(count, 3, "Should find 3 active requests"); | ||
| assertEq(requests.length, 3, "Array should match requested size"); | ||
| } | ||
|
|
||
| function checkFewerThanAvailable() private { | ||
| (PulseState.Request[] memory requests, uint256 count) = pulse | ||
| .getFirstActiveRequests(2); | ||
| assertEq(count, 2, "Should find 2 active requests"); | ||
| assertEq(requests.length, 2, "Array should match requested size"); | ||
| assertEq( | ||
| requests[0].sequenceNumber, | ||
| 1, | ||
| "First request should be oldest" | ||
| ); | ||
| assertEq(requests[1].sequenceNumber, 3, "Second request should be #3"); | ||
| } | ||
|
|
||
| function checkZeroRequest() private { | ||
| (PulseState.Request[] memory requests, uint256 count) = pulse | ||
| .getFirstActiveRequests(0); | ||
| assertEq(count, 0, "Should find 0 active requests"); | ||
| assertEq(requests.length, 0, "Array should be empty"); | ||
| } | ||
|
|
||
| function clearAllRequests( | ||
| bytes[] memory updateData, | ||
| bytes32[] memory priceIds | ||
| ) private { | ||
| vm.deal(defaultProvider, 3 ether); // Increase ETH allocation | ||
| vm.startPrank(defaultProvider); | ||
| pulse.executeCallback{value: 1 ether}(1, updateData, priceIds); | ||
| pulse.executeCallback{value: 1 ether}(3, updateData, priceIds); | ||
| pulse.executeCallback{value: 1 ether}(5, updateData, priceIds); | ||
| vm.stopPrank(); | ||
| } | ||
|
|
||
| function checkEmptyState() private { | ||
| (PulseState.Request[] memory requests, uint256 count) = pulse | ||
| .getFirstActiveRequests(10); | ||
| assertEq(count, 0, "Should find 0 active requests"); | ||
| assertEq(requests.length, 0, "Array should be empty"); | ||
| } | ||
|
|
||
| function test_getFirstActiveRequests_GasUsage() public { | ||
| // Setup test data | ||
| bytes32[] memory priceIds = new bytes32[](1); | ||
| priceIds[0] = bytes32(uint256(1)); | ||
| uint256 publishTime = block.timestamp; | ||
| uint256 callbackGasLimit = 1000000; | ||
|
|
||
| // Create mock price feeds and setup Pyth response | ||
| PythStructs.PriceFeed[] memory priceFeeds = createMockPriceFeeds( | ||
| publishTime | ||
| ); | ||
| mockParsePriceFeedUpdates(priceFeeds); | ||
| bytes[] memory updateData = createMockUpdateData(priceFeeds); | ||
|
|
||
| // Create 20 requests with some gaps | ||
| for (uint i = 0; i < 20; i++) { | ||
| vm.deal(address(this), 1 ether); | ||
| pulse.requestPriceUpdatesWithCallback{value: 1 ether}( | ||
| publishTime, | ||
| priceIds, | ||
| callbackGasLimit | ||
| ); | ||
|
|
||
| // Complete every third request to create gaps | ||
| if (i % 3 == 0) { | ||
| vm.deal(defaultProvider, 1 ether); | ||
| vm.prank(defaultProvider); | ||
| pulse.executeCallback{value: 1 ether}( | ||
| uint64(i + 1), | ||
| updateData, | ||
| priceIds | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Measure gas for different request counts | ||
| uint256 gas1 = gasleft(); | ||
| pulse.getFirstActiveRequests(5); | ||
| uint256 gas1Used = gas1 - gasleft(); | ||
|
|
||
| uint256 gas2 = gasleft(); | ||
| pulse.getFirstActiveRequests(10); | ||
| uint256 gas2Used = gas2 - gasleft(); | ||
|
|
||
| // Log gas usage for analysis | ||
| emit log_named_uint("Gas used for 5 requests", gas1Used); | ||
| emit log_named_uint("Gas used for 10 requests", gas2Used); | ||
|
|
||
| // Verify gas usage scales roughly linearly | ||
| // Allow 10% margin for other factors | ||
| assertApproxEqRel( | ||
| gas2Used, | ||
| gas1Used * 2, | ||
| 0.1e18, // 10% tolerance | ||
| "Gas usage should scale roughly linearly" | ||
| ); | ||
| } | ||
|
|
||
| // Mock implementation of pulseCallback | ||
| function pulseCallback( | ||
| uint64 sequenceNumber, | ||
| PythStructs.PriceFeed[] memory priceFeeds | ||
| ) external override { | ||
| // Just accept the callback, no need to do anything with the data | ||
| // This prevents the revert we're seeing | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.