This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 82
fix: reject pending getLatestBlock requests when block tracker is stopped #320
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,7 +174,7 @@ | |
|
|
||
| await withPollingBlockTracker(async ({ blockTracker }) => { | ||
| expect(blockTracker.isRunning()).toBe(false); | ||
| blockTracker.getLatestBlock(); | ||
|
Check warning on line 177 in src/PollingBlockTracker.test.ts
|
||
| expect(blockTracker.isRunning()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
@@ -821,6 +821,87 @@ | |
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('should reject pending latest block request if block tracker is stopped before fetch completes on second getLatestBlock call', async () => { | ||
| const setTimeoutRecorder = recordCallsToSetTimeout(); | ||
| const blockTrackerOptions = { | ||
| pollingInterval: 100, | ||
| blockResetDuration: 200, | ||
| }; | ||
|
|
||
| await withPollingBlockTracker( | ||
| { | ||
| provider: { | ||
| stubs: [ | ||
| { | ||
| methodName: 'eth_blockNumber', | ||
| result: '0x0', | ||
| }, | ||
| { | ||
| methodName: 'eth_blockNumber', | ||
| result: '0x0', | ||
| }, | ||
| ], | ||
| }, | ||
| blockTracker: blockTrackerOptions, | ||
| }, | ||
| async ({ blockTracker }) => { | ||
| // Step 1: Start the block tracker | ||
| blockTracker.on('latest', EMPTY_FUNCTION); | ||
|
|
||
| // Step 2: Wait for the first block update to resolve | ||
| await new Promise((resolve) => { | ||
| blockTracker.on('sync', resolve); | ||
| }); | ||
| expect(blockTracker.getCurrentBlock()).toBe('0x0'); | ||
| expect(blockTracker.isRunning()).toBe(true); | ||
|
|
||
| // Clear the current block to force a new request for the next getLatestBlock | ||
| // When the block tracker stops, there may be two `setTimeout`s in | ||
| // play: one to go to the next iteration of the block tracker | ||
| // loop, another to expire the current block number cache. We don't | ||
| // know which one has been added first, so we have to find it. | ||
| blockTracker.removeAllListeners(); | ||
| await setTimeoutRecorder.nextMatchingDuration( | ||
| blockTrackerOptions.blockResetDuration, | ||
| ); | ||
| expect(blockTracker.getCurrentBlock()).toBeNull(); | ||
|
|
||
| // Restart the tracker for the second call | ||
| blockTracker.on('latest', EMPTY_FUNCTION); | ||
|
|
||
| // Step 3: Immediately after, call getLatestBlock | ||
| const secondBlockPromise = blockTracker.getLatestBlock(); | ||
|
|
||
| // Step 4: Immediately after, stop the block tracker | ||
| blockTracker.removeAllListeners(); | ||
|
|
||
| // Verify block tracker state | ||
| expect(blockTracker.isRunning()).toBe(false); | ||
| expect(blockTracker.getCurrentBlock()).toBeNull(); | ||
|
|
||
| // The call to getLatestBlock would then never resolve (should be rejected) | ||
| await expect(secondBlockPromise).rejects.toThrow( | ||
| 'Block tracker destroyed', | ||
| ); | ||
|
|
||
| // Verify that the block reset timeout is set up | ||
| expect( | ||
| setTimeoutRecorder.calls.some((call) => { | ||
| return call.duration === blockTrackerOptions.blockResetDuration; | ||
| }), | ||
| ).toBe(true); | ||
|
|
||
| // Wait for the block reset timeout to complete | ||
| await setTimeoutRecorder.nextMatchingDuration( | ||
| blockTrackerOptions.blockResetDuration, | ||
| ); | ||
|
|
||
| // Verify that the current block is still null after the timeout | ||
| expect(blockTracker.getCurrentBlock()).toBeNull(); | ||
|
Comment on lines
+888
to
+901
Contributor
Author
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 really tied to the test and thus can be removed |
||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to restart the block tracker to clear the cached currentBlock.