-
Notifications
You must be signed in to change notification settings - Fork 4
Use finalized block head for selected EVM chains #88
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
Open
mncdg
wants to merge
2
commits into
develop
Choose a base branch
from
codex/polygon-finalized-block-reader
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 |
|---|---|---|
|
|
@@ -9,6 +9,15 @@ import { getRepositoryToken } from '@nestjs/typeorm'; | |
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { SupportedChainEntity } from '../../../../../entities/SupportedChainEntity'; | ||
|
|
||
| jest.mock('@debridge-finance/solana-utils', () => ({ | ||
| constants: { | ||
| SOLANA_CHAIN_ID: 7565164, | ||
| }, | ||
| crypto: { | ||
| hashSubmissionIdRaw: jest.fn().mockReturnValue('submission-id'), | ||
| }, | ||
| })); | ||
|
|
||
| /** | ||
| * Creates an AddNewEventsAction service with configurable mocks. | ||
| * | ||
|
|
@@ -19,6 +28,8 @@ import { SupportedChainEntity } from '../../../../../entities/SupportedChainEnti | |
| * @param lastEventBlockNumber - highest blockNumber in submissions table, null if no events | ||
| * @param events - raw events returned by getPastEvents (default: []) | ||
| * @param blockConfirmation - number of blocks subtracted from RPC head (default: 1) | ||
| * @param finalizedBlockNumber - optional finalized block number returned by RPC | ||
| * @param finalizedBlockError - optional error thrown by finalized block RPC call | ||
| */ | ||
| async function buildService(config: { | ||
| chainId: number; | ||
|
|
@@ -28,6 +39,8 @@ async function buildService(config: { | |
| lastEventBlockNumber?: number | null; | ||
| events?: any[]; | ||
| blockConfirmation?: number; | ||
| finalizedBlockNumber?: number; | ||
| finalizedBlockError?: Error; | ||
| }) { | ||
| const isSolana = config.isSolana ?? false; | ||
| const blockConfirmation = config.blockConfirmation ?? 1; | ||
|
|
@@ -38,6 +51,9 @@ async function buildService(config: { | |
| const getPastEventsMock = jest.fn().mockResolvedValue(events); | ||
| const processMock = jest.fn().mockResolvedValue({}); | ||
| const syncTransactionsMock = jest.fn().mockResolvedValue({}); | ||
| const getFinalizedBlockMock = config.finalizedBlockError | ||
| ? jest.fn().mockRejectedValue(config.finalizedBlockError) | ||
| : jest.fn().mockResolvedValue({ number: config.finalizedBlockNumber }); | ||
| const submissionsFindOneMock = jest.fn().mockResolvedValue( | ||
| lastEventBlockNumber !== null | ||
| ? { blockNumber: lastEventBlockNumber, chainFrom: config.chainId } | ||
|
|
@@ -52,6 +68,7 @@ async function buildService(config: { | |
| getPastEvents: getPastEventsMock, | ||
| })), | ||
| getBlockNumber: jest.fn().mockResolvedValue(config.rpcBlockNumber), | ||
| getBlock: getFinalizedBlockMock, | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -114,6 +131,7 @@ async function buildService(config: { | |
| web3: web3Mock, | ||
| updateMock, | ||
| getPastEventsMock, | ||
| getFinalizedBlockMock, | ||
| processMock, | ||
| syncTransactionsMock, | ||
| }; | ||
|
|
@@ -211,6 +229,69 @@ describe('AddNewEventsAction', () => { | |
| ); | ||
| expect(processMock).toBeCalledTimes(1); | ||
| }); | ||
|
|
||
| it('Polygon scans up to finalized block when it is ahead of confirmations fallback', async () => { | ||
| const { service, chainId, getPastEventsMock, getFinalizedBlockMock } = await buildService({ | ||
| chainId: 137, | ||
| latestBlock: 1000, | ||
| rpcBlockNumber: 1300, | ||
| blockConfirmation: 256, | ||
| finalizedBlockNumber: 1200, | ||
| }); | ||
|
|
||
| await service.action(chainId); | ||
|
|
||
| expect(getFinalizedBlockMock).toBeCalledWith('finalized'); | ||
| expect(getPastEventsMock).toBeCalledWith('Sent', { | ||
| fromBlock: 1000, | ||
| toBlock: 1200, | ||
| }); | ||
| }); | ||
|
|
||
| it('Polygon uses finalized block even when it is behind confirmations fallback', async () => { | ||
| const { service, chainId, getPastEventsMock } = await buildService({ | ||
| chainId: 137, | ||
| latestBlock: 1000, | ||
| rpcBlockNumber: 1300, | ||
| blockConfirmation: 256, | ||
| finalizedBlockNumber: 1020, | ||
| }); | ||
|
|
||
| await service.action(chainId); | ||
|
|
||
| expect(getPastEventsMock).toBeCalledWith('Sent', { | ||
| fromBlock: 1000, | ||
| toBlock: 1020, | ||
| }); | ||
| }); | ||
|
Comment on lines
+251
to
+266
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. To ensure that the scanner does not trigger a database rollback or perform redundant scans when the finalized block is behind the latest scanned block, we should add a dedicated unit test verifying this behavior. it('Polygon uses finalized block even when it is behind confirmations fallback', async () => {
const { service, chainId, getPastEventsMock } = await buildService({
chainId: 137,
latestBlock: 1000,
rpcBlockNumber: 1300,
blockConfirmation: 256,
finalizedBlockNumber: 1020,
});
await service.action(chainId);
expect(getPastEventsMock).toBeCalledWith('Sent', {
fromBlock: 1000,
toBlock: 1020,
});
});
it('Polygon does not roll back or scan when finalized block is behind latest scanned block', async () => {
const { service, chainId, getPastEventsMock, updateMock } = await buildService({
chainId: 137,
latestBlock: 1000,
rpcBlockNumber: 1300,
blockConfirmation: 256,
finalizedBlockNumber: 980,
});
await service.action(chainId);
expect(getPastEventsMock).not.toBeCalled();
expect(updateMock).not.toBeCalled();
}); |
||
|
|
||
| it('Polygon stops scanning when finalized block RPC fails', async () => { | ||
| const { service, chainId, getPastEventsMock, processMock, updateMock } = await buildService({ | ||
| chainId: 137, | ||
| latestBlock: 1000, | ||
| rpcBlockNumber: 1300, | ||
| blockConfirmation: 256, | ||
| finalizedBlockError: new Error('finalized is not supported'), | ||
| }); | ||
|
|
||
| await service.action(chainId); | ||
|
|
||
| expect(getPastEventsMock).not.toBeCalled(); | ||
| expect(processMock).not.toBeCalled(); | ||
| expect(updateMock).not.toBeCalled(); | ||
| }); | ||
|
|
||
| it('does not request finalized block for regular EVM chains', async () => { | ||
| const { service, chainId, getFinalizedBlockMock } = await buildService({ | ||
| chainId: 1, | ||
| latestBlock: 98, | ||
| rpcBlockNumber: 100, | ||
| }); | ||
|
|
||
| await service.action(chainId); | ||
|
|
||
| expect(getFinalizedBlockMock).not.toBeCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
|
|
@@ -231,7 +312,7 @@ describe('AddNewEventsAction', () => { | |
| // safeBlock = Math.max(50, 99) = 99 — picks toBlock, minimal rollback. | ||
| it('RPC slightly behind, last event below toBlock — uses toBlock', async () => { | ||
| const { service, chainId, updateMock, getPastEventsMock, processMock } = await buildService({ | ||
| chainId: 56, | ||
| chainId: 1, | ||
| latestBlock: 200, | ||
| rpcBlockNumber: 100, | ||
| lastEventBlockNumber: 50, | ||
|
|
@@ -281,7 +362,7 @@ describe('AddNewEventsAction', () => { | |
| // safeBlock = Math.max(0, 99) = 99 — toBlock is the only reference point. | ||
| it('no events in DB — falls back to toBlock', async () => { | ||
| const { service, chainId, updateMock } = await buildService({ | ||
| chainId: 137, | ||
| chainId: 42161, | ||
| latestBlock: 200, | ||
| rpcBlockNumber: 100, | ||
| lastEventBlockNumber: null, | ||
|
|
||
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.
When transitioning a chain to use finalized blocks, or if the finalized block temporarily lags behind the head, the finalized block number (
toBlock) can be less than the last scanned block (fromBlock).Under the current implementation, if
fromBlock > toBlock, the stale RPC protection logic on line 96 will be triggered, which rolls back thelatestBlockin the database tosafeBlock(the last event block ortoBlock). This causes the node to perform redundant scanning of already processed blocks once the finalized block catches up.Since finalized blocks are guaranteed not to reorg, we do not need to roll back the progress. Instead, we should cap
toBlockto be at leastfromBlockfor finalized chains, which safely pauses scanning without triggering a database rollback.