From 5dc319f4d8baf8cdbcc62c494167b05aaf5d97c6 Mon Sep 17 00:00:00 2001 From: JQQQ Date: Thu, 23 Nov 2023 16:22:08 +1300 Subject: [PATCH 1/3] fix getBestBlockHeight miss unfinalizedBlocks --- packages/node/src/ethereum/api.connection.ts | 8 ++- .../node/src/ethereum/api.ethereum.test.ts | 52 ++++++++++++++++--- packages/node/src/ethereum/api.ethereum.ts | 7 ++- .../node/src/ethereum/api.service.ethereum.ts | 7 ++- .../node/src/indexer/project.service.test.ts | 2 +- 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/packages/node/src/ethereum/api.connection.ts b/packages/node/src/ethereum/api.connection.ts index dbdf052b4e..9ce3df3cdb 100644 --- a/packages/node/src/ethereum/api.connection.ts +++ b/packages/node/src/ethereum/api.connection.ts @@ -49,8 +49,14 @@ export class EthereumApiConnection blockConfirmations: number, fetchBlocksBatches: GetFetchFunc, eventEmitter: EventEmitter2, + unfinalizedBlocks: boolean, ): Promise { - const api = new EthereumApi(endpoint, blockConfirmations, eventEmitter); + const api = new EthereumApi( + endpoint, + blockConfirmations, + eventEmitter, + unfinalizedBlocks, + ); await api.init(); diff --git a/packages/node/src/ethereum/api.ethereum.test.ts b/packages/node/src/ethereum/api.ethereum.test.ts index 2a7da07414..107b3af1a6 100644 --- a/packages/node/src/ethereum/api.ethereum.test.ts +++ b/packages/node/src/ethereum/api.ethereum.test.ts @@ -53,7 +53,12 @@ describe('Api.ethereum', () => { }; beforeEach(async () => { - ethApi = new EthereumApi(HTTP_ENDPOINT, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + HTTP_ENDPOINT, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(16258633); }); @@ -130,7 +135,12 @@ describe('Api.ethereum', () => { it('Null filter support', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -152,7 +162,12 @@ describe('Api.ethereum', () => { it('!null filter support for logs, expect to filter out', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); const filter_1: EthereumLogFilter = { topics: [ @@ -189,7 +204,12 @@ describe('Api.ethereum', () => { it('Null filter support, for undefined transaction.to', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(2847447); blockData.transactions[1].to = undefined; @@ -212,7 +232,12 @@ describe('Api.ethereum', () => { it('Should return all tx if filter.to is not defined', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -231,7 +256,12 @@ describe('Api.ethereum', () => { it('filter.to Should support only null not undefined', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -250,7 +280,12 @@ describe('Api.ethereum', () => { it('If transaction is undefined, with null filter, should be supported', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); + ethApi = new EthereumApi( + beamEndpoint, + BLOCK_CONFIRMATIONS, + eventEmitter, + false, + ); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -277,6 +312,7 @@ describe('Api.ethereum', () => { 'https://rpc.api.moonbeam.network', BLOCK_CONFIRMATIONS, eventEmitter, + false, ); await ethApi.init(); @@ -287,6 +323,7 @@ describe('Api.ethereum', () => { 'https://bsc-dataseed.binance.org', BLOCK_CONFIRMATIONS, eventEmitter, + false, ); await ethApi.init(); @@ -297,6 +334,7 @@ describe('Api.ethereum', () => { 'https://polygon.llamarpc.com', BLOCK_CONFIRMATIONS, eventEmitter, + false, ); await ethApi.init(); diff --git a/packages/node/src/ethereum/api.ethereum.ts b/packages/node/src/ethereum/api.ethereum.ts index 84aea68bc4..b15f6c0ff8 100644 --- a/packages/node/src/ethereum/api.ethereum.ts +++ b/packages/node/src/ethereum/api.ethereum.ts @@ -110,6 +110,7 @@ export class EthereumApi implements ApiWrapper { private endpoint: string, private blockConfirmations: number, private eventEmitter: EventEmitter2, + private unfinalizedBlocks: boolean, ) { const { hostname, protocol, searchParams } = new URL(endpoint); @@ -226,7 +227,11 @@ export class EthereumApi implements ApiWrapper { async getBestBlockHeight(): Promise { // Cronos "safe" tag doesn't currently work as indended const tag = - this.supportsFinalization && this.chainId !== 25 ? 'safe' : 'latest'; + !this.unfinalizedBlocks && + this.supportsFinalization && + this.chainId !== 25 + ? 'safe' + : 'latest'; return (await this.client.getBlock(tag)).number; } diff --git a/packages/node/src/ethereum/api.service.ethereum.ts b/packages/node/src/ethereum/api.service.ethereum.ts index de73ed95a0..a4e01175bf 100644 --- a/packages/node/src/ethereum/api.service.ethereum.ts +++ b/packages/node/src/ethereum/api.service.ethereum.ts @@ -10,7 +10,11 @@ import { NodeConfig, profilerWrap, } from '@subql/node-core'; -import { EthereumBlock, EthereumNetworkConfig, LightEthereumBlock } from '@subql/types-ethereum'; +import { + EthereumBlock, + EthereumNetworkConfig, + LightEthereumBlock, +} from '@subql/types-ethereum'; import { EthereumNodeConfig } from '../configure/NodeConfig'; import { SubqueryProject } from '../configure/SubqueryProject'; import { isOnlyEventHandlers } from '../utils/project'; @@ -71,6 +75,7 @@ export class EthereumApiService extends ApiService< this.nodeConfig.blockConfirmations, this.fetchBlocksBatches, this.eventEmitter, + this.nodeConfig.unfinalizedBlocks, ), //eslint-disable-next-line @typescript-eslint/require-await async (connection: EthereumApiConnection) => { diff --git a/packages/node/src/indexer/project.service.test.ts b/packages/node/src/indexer/project.service.test.ts index f4c2ca3982..cb70329e30 100644 --- a/packages/node/src/indexer/project.service.test.ts +++ b/packages/node/src/indexer/project.service.test.ts @@ -8,7 +8,7 @@ import { ProjectService } from './project.service'; const HTTP_ENDPOINT = 'https://eth.llamarpc.com'; const mockApiService = (): EthereumApiService => { - const ethApi = new EthereumApi(HTTP_ENDPOINT, 20, new EventEmitter2()); + const ethApi = new EthereumApi(HTTP_ENDPOINT, 20, new EventEmitter2(), false); // await ethApi.init(); From 76f4fb57fbaaf86e94dd0eae4833dc9a6ded7328 Mon Sep 17 00:00:00 2001 From: Jay Ji Date: Thu, 23 Nov 2023 16:26:40 +1300 Subject: [PATCH 2/3] Update packages/node/src/ethereum/api.ethereum.ts Co-authored-by: Scott Twiname --- packages/node/src/ethereum/api.ethereum.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/ethereum/api.ethereum.ts b/packages/node/src/ethereum/api.ethereum.ts index b15f6c0ff8..74bea7e727 100644 --- a/packages/node/src/ethereum/api.ethereum.ts +++ b/packages/node/src/ethereum/api.ethereum.ts @@ -110,7 +110,7 @@ export class EthereumApi implements ApiWrapper { private endpoint: string, private blockConfirmations: number, private eventEmitter: EventEmitter2, - private unfinalizedBlocks: boolean, + private unfinalizedBlocks = false, ) { const { hostname, protocol, searchParams } = new URL(endpoint); From 17299048f1a18ff38903ff792b8b2634595a7e02 Mon Sep 17 00:00:00 2001 From: JQQQ Date: Thu, 23 Nov 2023 16:28:56 +1300 Subject: [PATCH 3/3] rollback tests --- .../node/src/ethereum/api.ethereum.test.ts | 52 +++---------------- .../node/src/indexer/project.service.test.ts | 2 +- 2 files changed, 8 insertions(+), 46 deletions(-) diff --git a/packages/node/src/ethereum/api.ethereum.test.ts b/packages/node/src/ethereum/api.ethereum.test.ts index 107b3af1a6..2a7da07414 100644 --- a/packages/node/src/ethereum/api.ethereum.test.ts +++ b/packages/node/src/ethereum/api.ethereum.test.ts @@ -53,12 +53,7 @@ describe('Api.ethereum', () => { }; beforeEach(async () => { - ethApi = new EthereumApi( - HTTP_ENDPOINT, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(HTTP_ENDPOINT, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(16258633); }); @@ -135,12 +130,7 @@ describe('Api.ethereum', () => { it('Null filter support', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -162,12 +152,7 @@ describe('Api.ethereum', () => { it('!null filter support for logs, expect to filter out', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); const filter_1: EthereumLogFilter = { topics: [ @@ -204,12 +189,7 @@ describe('Api.ethereum', () => { it('Null filter support, for undefined transaction.to', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(2847447); blockData.transactions[1].to = undefined; @@ -232,12 +212,7 @@ describe('Api.ethereum', () => { it('Should return all tx if filter.to is not defined', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -256,12 +231,7 @@ describe('Api.ethereum', () => { it('filter.to Should support only null not undefined', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -280,12 +250,7 @@ describe('Api.ethereum', () => { it('If transaction is undefined, with null filter, should be supported', async () => { const beamEndpoint = 'https://rpc.api.moonbeam.network'; - ethApi = new EthereumApi( - beamEndpoint, - BLOCK_CONFIRMATIONS, - eventEmitter, - false, - ); + ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter); await ethApi.init(); blockData = await fetchBlock(2847447); const result = blockData.transactions.filter((tx) => { @@ -312,7 +277,6 @@ describe('Api.ethereum', () => { 'https://rpc.api.moonbeam.network', BLOCK_CONFIRMATIONS, eventEmitter, - false, ); await ethApi.init(); @@ -323,7 +287,6 @@ describe('Api.ethereum', () => { 'https://bsc-dataseed.binance.org', BLOCK_CONFIRMATIONS, eventEmitter, - false, ); await ethApi.init(); @@ -334,7 +297,6 @@ describe('Api.ethereum', () => { 'https://polygon.llamarpc.com', BLOCK_CONFIRMATIONS, eventEmitter, - false, ); await ethApi.init(); diff --git a/packages/node/src/indexer/project.service.test.ts b/packages/node/src/indexer/project.service.test.ts index cb70329e30..f4c2ca3982 100644 --- a/packages/node/src/indexer/project.service.test.ts +++ b/packages/node/src/indexer/project.service.test.ts @@ -8,7 +8,7 @@ import { ProjectService } from './project.service'; const HTTP_ENDPOINT = 'https://eth.llamarpc.com'; const mockApiService = (): EthereumApiService => { - const ethApi = new EthereumApi(HTTP_ENDPOINT, 20, new EventEmitter2(), false); + const ethApi = new EthereumApi(HTTP_ENDPOINT, 20, new EventEmitter2()); // await ethApi.init();