Skip to content
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

chore(test): add case for validating parent hash #287

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
sdaveas marked this conversation as resolved.
Show resolved Hide resolved
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');
sdaveas marked this conversation as resolved.
Show resolved Hide resolved
});
});

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