Skip to content

Commit

Permalink
chore(test): add case for validating parent hash (#287)
Browse files Browse the repository at this point in the history
Co-authored-by: Milap Sheth <milap@interoplabs.io>
  • Loading branch information
sdaveas and milapsheth authored Aug 23, 2024
1 parent be9f5c2 commit 9fa2f2e
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions test/RpcCompatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe('RpcCompatibility', () => {
expect(timeDifference).to.be.lessThanOrEqual(maxDifference);
}

async function validParentHashes(blockTag) {
const withTransaction = false;
const block = await provider.send('eth_getBlockByNumber', [blockTag, withTransaction]);
const parentBlock = await provider.send('eth_getBlockByHash', [block.parentHash, withTransaction]);

expect(parentBlock.hash).to.equal(block.parentHash);
}

before(async () => {
provider = ethers.provider;
[signer] = await ethers.getSigners();
Expand Down Expand Up @@ -117,11 +125,20 @@ describe('RpcCompatibility', () => {
await checkLog(filter);
});

it('supports finalized tag', async () => {
let isLarger = false;
describe('supports finalized tag', () => {
let finalizedBlockNumber;

try {
const finalizedBlockNumber = await provider.send('eth_getBlockByNumber', ['finalized', false]);
before(async () => {
try {
finalizedBlockNumber = await provider.send('eth_getBlockByNumber', ['finalized', false]);
} catch (error) {
console.error('Failed to retrieve finalized block number:', error);
throw error;
}
});

it('should return latest.number > finalized.number', async () => {
let isLarger = false;

if (finalizedBlockNumber && finalizedBlockNumber.number !== null) {
isLarger = finalizedBlockNumber.number >= blockNumber;
Expand All @@ -130,17 +147,19 @@ describe('RpcCompatibility', () => {
console.log('Achieved finality for the block instantly');
}
}
} catch (error) {
console.error('Failed to retrieve finalized block number:', error);
}

const fromBlock = isLarger ? blockNumber : 'finalized';
const toBlock = fromBlock === 'finalized' ? blockNumber : 'finalized';
const filter = {
fromBlock: isHardhat ? hexValue(0) : fromBlock,
toBlock,
};
await checkLog(filter);
const fromBlock = isLarger ? blockNumber : 'finalized';
const toBlock = fromBlock === 'finalized' ? blockNumber : 'finalized';
const filter = {
fromBlock: isHardhat ? hexValue(0) : fromBlock,
toBlock,
};
await checkLog(filter);
});
});

it('should have valid parent hash', async () => {
validParentHashes('finalized');
});
});

Expand Down Expand Up @@ -243,6 +262,11 @@ describe('RpcCompatibility', () => {
checkBlock(block, false);
checkBlockTimeStamp(parseInt(block.timestamp, 16), 12000);
});

it('should have valid parent hashes', async () => {
// Note: If chain doesn't have instant finality, reorgs could cause this to fail
validParentHashes('latest');
});
});

it('should support RPC method eth_blockNumber', async () => {
Expand Down

0 comments on commit 9fa2f2e

Please sign in to comment.