From dfbc3ea78046823fc3c013fed805ef02e0f88d43 Mon Sep 17 00:00:00 2001 From: Stelios Daveas Date: Fri, 23 Aug 2024 13:12:22 +0300 Subject: [PATCH 1/6] chore(test): add case for validating parent hash --- test/RpcCompatibility.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index f787a854..95551d21 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -243,6 +243,19 @@ describe('RpcCompatibility', () => { checkBlock(block, false); checkBlockTimeStamp(parseInt(block.timestamp, 16), 12000); }); + + it('blocks have valid parent hashes', async () => { + const withTransaction = false; + const block = await provider.send('eth_getBlockByNumber', ['latest', withTransaction]); + const parentBlock = await provider.send('eth_getBlockByHash', [block.parentHash, withTransaction]); + + console.log('Block:', block); + console.log('Parent block:', parentBlock); + + checkBlock(parentBlock, withTransaction); + + expect(parentBlock.hash).to.equal(block.parentHash); + }); }); it('should support RPC method eth_blockNumber', async () => { From 75eeb36acf34872bc010efaf682d245903bb553d Mon Sep 17 00:00:00 2001 From: Stelios Daveas Date: Fri, 23 Aug 2024 14:12:44 +0300 Subject: [PATCH 2/6] chore: support parent hash validation for both latest and finalized tags --- test/RpcCompatibility.js | 59 +++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index 95551d21..9f615541 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -39,6 +39,14 @@ describe('RpcCompatibility', () => { expect(timeDifference).to.be.lessThanOrEqual(maxDifference); } + async function validParentHashes(tag) { + const withTransaction = false; + const block = await provider.send('eth_getBlockByNumber', ['latest', 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(); @@ -117,11 +125,19 @@ 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); + } + }); + + it('should return latest.number > finalized.number', async () => { + let isLarger = false; if (finalizedBlockNumber && finalizedBlockNumber.number !== null) { isLarger = finalizedBlockNumber.number >= blockNumber; @@ -130,17 +146,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'); }); }); @@ -244,17 +262,8 @@ describe('RpcCompatibility', () => { checkBlockTimeStamp(parseInt(block.timestamp, 16), 12000); }); - it('blocks have valid parent hashes', async () => { - const withTransaction = false; - const block = await provider.send('eth_getBlockByNumber', ['latest', withTransaction]); - const parentBlock = await provider.send('eth_getBlockByHash', [block.parentHash, withTransaction]); - - console.log('Block:', block); - console.log('Parent block:', parentBlock); - - checkBlock(parentBlock, withTransaction); - - expect(parentBlock.hash).to.equal(block.parentHash); + it('should have valid parent hashes', async () => { + validParentHashes('latest') }); }); From 334408a867eeced1ba3cef9e3495d0a253e27c39 Mon Sep 17 00:00:00 2001 From: Stelios Daveas Date: Fri, 23 Aug 2024 14:16:26 +0300 Subject: [PATCH 3/6] appease linter --- test/RpcCompatibility.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index 9f615541..2c8f71d5 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -263,7 +263,7 @@ describe('RpcCompatibility', () => { }); it('should have valid parent hashes', async () => { - validParentHashes('latest') + validParentHashes('latest'); }); }); From e7826e240de77061e00d257da6c8d6704c778211 Mon Sep 17 00:00:00 2001 From: Stelios Daveas <48155711+sdaveas@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:46:55 +0300 Subject: [PATCH 4/6] Update test/RpcCompatibility.js Co-authored-by: Milap Sheth --- test/RpcCompatibility.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index 2c8f71d5..fbf35e50 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -133,6 +133,7 @@ describe('RpcCompatibility', () => { finalizedBlockNumber = await provider.send('eth_getBlockByNumber', ['finalized', false]); } catch (error) { console.error('Failed to retrieve finalized block number:', error); + throw error; } }); From ece03d595ccb5e4d9570e7e0875413e156404757 Mon Sep 17 00:00:00 2001 From: Stelios Daveas <48155711+sdaveas@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:47:03 +0300 Subject: [PATCH 5/6] Update test/RpcCompatibility.js Co-authored-by: Milap Sheth --- test/RpcCompatibility.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index fbf35e50..37a01b2c 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -39,9 +39,9 @@ describe('RpcCompatibility', () => { expect(timeDifference).to.be.lessThanOrEqual(maxDifference); } - async function validParentHashes(tag) { + async function validParentHashes(blockTag) { const withTransaction = false; - const block = await provider.send('eth_getBlockByNumber', ['latest', withTransaction]); + 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); From 0ca08d8deed0fe5cce72a3f1f4ec309c6d7579c5 Mon Sep 17 00:00:00 2001 From: Stelios Daveas <48155711+sdaveas@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:47:31 +0300 Subject: [PATCH 6/6] Update test/RpcCompatibility.js Co-authored-by: Milap Sheth --- test/RpcCompatibility.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/RpcCompatibility.js b/test/RpcCompatibility.js index 37a01b2c..a07c584e 100644 --- a/test/RpcCompatibility.js +++ b/test/RpcCompatibility.js @@ -264,6 +264,7 @@ describe('RpcCompatibility', () => { }); it('should have valid parent hashes', async () => { + // Note: If chain doesn't have instant finality, reorgs could cause this to fail validParentHashes('latest'); }); });