Skip to content

Commit

Permalink
Merge pull request #237 from lidofinance/fix/reorg-edge-cases
Browse files Browse the repository at this point in the history
test: covering reorg edge cases
  • Loading branch information
infloop authored Jan 17, 2024
2 parents 84e02ae + 0f318e1 commit 92538d3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/jobs/keys-update/staking-module-updater.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class StakingModuleUpdaterService {
// get full block data by hashes
const currentBlock = await this.executionProvider.getFullBlock(currentBlockHash);
const prevBlock = await this.executionProvider.getFullBlock(prevBlockHash);
// prevBlock is a direct child of currentBlock
// prevBlock is a direct parent of currentBlock
// there's no need to check deeper as we get the currentBlock by tag
if (currentBlock.parentHash === prevBlock.hash) return false;
// different hash but same number
Expand All @@ -178,9 +178,15 @@ export class StakingModuleUpdaterService {
}),
);
// compare hash from the first block
if (blocks[0].hash !== prevBlockHash) return true;
if (blocks[0].hash !== prevBlockHash) {
updaterState.isReorgDetected = true;
return true;
}
// compare hash from the last block
if (blocks[blocks.length - 1].hash !== currentBlockHash) return true;
if (blocks[blocks.length - 1].hash !== currentBlockHash) {
updaterState.isReorgDetected = true;
return true;
}
// check the integrity of the blockchain
for (let i = 1; i < blocks.length; i++) {
const previousBlock = blocks[i - 1];
Expand Down
58 changes: 57 additions & 1 deletion src/jobs/keys-update/test/detect-reorg.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,63 @@ describe('detect reorg', () => {
number: Number(blockHashOrBlockTag),
hash: `0x${blockHashOrBlockTag}`,
timestamp: 1,
parentHash: `0xSORRY`,
parentHash: blockHashOrBlockTag === 100 ? `0xSORRY` : `0x${Number(blockHashOrBlockTag) - 1}`,
} as any),
);

expect(await updaterService.isReorgDetected(updaterState, '0x1', '0x100')).toBeTruthy();
expect(updaterState.isReorgDetected).toBeTruthy();
});

it("first block from range response doesn't match with first block", async () => {
const updaterState: UpdaterState = {
lastChangedBlockHash: '0x1',
isReorgDetected: false,
};

jest
.spyOn(executionProviderService, 'getFullBlock')
.mockImplementationOnce(async () => ({ number: 100, hash: '0x100', timestamp: 1, parentHash: '0x99' } as any));

jest
.spyOn(executionProviderService, 'getFullBlock')
.mockImplementationOnce(async () => ({ number: 1, hash: '0x1', timestamp: 1, parentHash: '0x0' } as any));

jest.spyOn(executionProviderService, 'getFullBlock').mockImplementation(
async (blockHashOrBlockTag: string | number) =>
({
number: Number(blockHashOrBlockTag),
hash: blockHashOrBlockTag === 1 ? `0xSORRY` : `0x${Number(blockHashOrBlockTag) - 1}`,
timestamp: 1,
parentHash: `0x${Number(blockHashOrBlockTag) - 1}`,
} as any),
);

expect(await updaterService.isReorgDetected(updaterState, '0x1', '0x100')).toBeTruthy();
expect(updaterState.isReorgDetected).toBeTruthy();
});

it("last block from range response doesn't match with last block", async () => {
const updaterState: UpdaterState = {
lastChangedBlockHash: '0x1',
isReorgDetected: false,
};

jest
.spyOn(executionProviderService, 'getFullBlock')
.mockImplementationOnce(async () => ({ number: 100, hash: '0x100', timestamp: 1, parentHash: '0x99' } as any));

jest
.spyOn(executionProviderService, 'getFullBlock')
.mockImplementationOnce(async () => ({ number: 1, hash: '0x1', timestamp: 1, parentHash: '0x0' } as any));

jest.spyOn(executionProviderService, 'getFullBlock').mockImplementation(
async (blockHashOrBlockTag: string | number) =>
({
number: Number(blockHashOrBlockTag),
hash: blockHashOrBlockTag === 100 ? `0xSORRY` : `0x${Number(blockHashOrBlockTag) - 1}`,
timestamp: 1,
parentHash: `0x${Number(blockHashOrBlockTag) - 1}`,
} as any),
);

Expand Down

0 comments on commit 92538d3

Please sign in to comment.