From aaeaff674b036a09f3bec176800404faf883d7f2 Mon Sep 17 00:00:00 2001 From: JQQQ Date: Mon, 22 Jan 2024 11:54:19 +1300 Subject: [PATCH 1/3] Fix handle soroban height behind stellar --- packages/node/src/stellar/api.stellar.spec.ts | 13 +++++++ packages/node/src/stellar/api.stellar.ts | 34 +++++++------------ packages/node/src/stellar/soroban.server.ts | 10 ------ 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/packages/node/src/stellar/api.stellar.spec.ts b/packages/node/src/stellar/api.stellar.spec.ts index 4dd4ab5d..6c945162 100644 --- a/packages/node/src/stellar/api.stellar.spec.ts +++ b/packages/node/src/stellar/api.stellar.spec.ts @@ -84,4 +84,17 @@ describe('StellarApi', () => { const specName = stellarApi.getSpecName(); expect(specName).toEqual('Stellar'); }); + + it('handleError - soroban node been reset', async () => { + const error = new Error('start is after newest ledger'); + stellarApi.getAndWrapEvents = jest.fn(() => { + throw new Error('start is after newest ledger'); + }); + (stellarApi as any).fetchOperationsForLedger = jest.fn((seq: number) => [ + { type: { toString: () => 'invoke_host_function' } }, + ]); + await expect((stellarApi as any).fetchAndWrapLedger(100)).rejects.toThrow( + /access a ledger that is earlier than the latest ledger number/, + ); + }); }); diff --git a/packages/node/src/stellar/api.stellar.ts b/packages/node/src/stellar/api.stellar.ts index 23c43e22..c8b6e886 100644 --- a/packages/node/src/stellar/api.stellar.ts +++ b/packages/node/src/stellar/api.stellar.ts @@ -153,28 +153,10 @@ export class StellarApi implements ApiWrapper { } async getAndWrapEvents(height: number): Promise { - // If soroban network the latest ledger height behind processing height (due to network reset) - // We check if cached latestLedger behind height, if so get updated and check again. - if ( - !this.sorobanClient.latestLedger || - this.sorobanClient.latestLedger < height - ) { - const latestLedger = (await this.sorobanClient.getLatestLedger()) - .sequence; - this.sorobanClient.updateCacheLatestLedger(latestLedger); - if (this.sorobanClient.latestLedger < height) { - logger.warn( - `Error: Unable to fetch Soroban events at block height ${height} because the start is after the newest ledger. The latest ledger on Soroban is at ${latestLedger}. Please check the Soroban node. Subquery will treat it as if there are no events for this height.`, - ); - return []; - } - } - - const { events: events, latestLedger: latestLedger } = - await this.sorobanClient.getEvents({ - startLedger: height, - filters: [], - }); + const { events: events } = await this.sorobanClient.getEvents({ + startLedger: height, + filters: [], + }); return events.map((event) => { const wrappedEvent = { ...event, @@ -311,6 +293,14 @@ export class StellarApi implements ApiWrapper { try { eventsForSequence = await this.getAndWrapEvents(sequence); } catch (e) { + if (e.message === 'start is after newest ledger') { + const latestLedger = (await this.sorobanClient.getLatestLedger()) + .sequence; + throw new Error(`The requested events for ledger number ${sequence} is not available on the current soroban node. + This is because you're trying to access a ledger that is earlier than the latest ledger number ${latestLedger} stored in this node. + To resolve this issue, please check you endpoint node start height`); + } + if (e.message === 'start is before oldest ledger') { throw new Error(`The requested events for ledger number ${sequence} is not available on the current soroban node. This is because you're trying to access a ledger that is older than the oldest ledger stored in this node. diff --git a/packages/node/src/stellar/soroban.server.ts b/packages/node/src/stellar/soroban.server.ts index 23583cc9..ec573165 100644 --- a/packages/node/src/stellar/soroban.server.ts +++ b/packages/node/src/stellar/soroban.server.ts @@ -1,17 +1,14 @@ // Copyright 2020-2023 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 -import { getLogger } from '@subql/node-core'; import { SorobanRpcEventResponse } from '@subql/types-stellar'; import { compact, groupBy, last } from 'lodash'; import { Server, SorobanRpc } from 'soroban-client'; -const logger = getLogger('stellar-server'); const DEFAULT_PAGE_SIZE = 100; export class SorobanServer extends Server { private eventsCache: { [key: number]: SorobanRpc.GetEventsResponse } = {}; - latestLedger?: number; private async fetchEventsForSequence( sequence: number, @@ -85,16 +82,9 @@ export class SorobanServer extends Server { if (!eventExists) { this.eventsCache[ledger].events.push(event); } - this.updateCacheLatestLedger(response.latestLedger); }); } - updateCacheLatestLedger(latestLedger: number): void { - if (!this.latestLedger || this.latestLedger < latestLedger) { - this.latestLedger = latestLedger; - } - } - async getEvents( request: Server.GetEventsRequest, ): Promise { From 6e33206a5cc9a0487ae4b24eedf7779d76d22fb5 Mon Sep 17 00:00:00 2001 From: Jay Ji Date: Mon, 22 Jan 2024 12:46:31 +1300 Subject: [PATCH 2/3] Update packages/node/src/stellar/api.stellar.ts Co-authored-by: Scott Twiname --- packages/node/src/stellar/api.stellar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/stellar/api.stellar.ts b/packages/node/src/stellar/api.stellar.ts index c8b6e886..86c84ee1 100644 --- a/packages/node/src/stellar/api.stellar.ts +++ b/packages/node/src/stellar/api.stellar.ts @@ -297,7 +297,7 @@ export class StellarApi implements ApiWrapper { const latestLedger = (await this.sorobanClient.getLatestLedger()) .sequence; throw new Error(`The requested events for ledger number ${sequence} is not available on the current soroban node. - This is because you're trying to access a ledger that is earlier than the latest ledger number ${latestLedger} stored in this node. + This is because you're trying to access a ledger that is after the latest ledger number ${latestLedger} stored in this node. To resolve this issue, please check you endpoint node start height`); } From 1d99181c656fc4a570c69fcf9344d65957afe374 Mon Sep 17 00:00:00 2001 From: JQQQ Date: Mon, 22 Jan 2024 13:25:01 +1300 Subject: [PATCH 3/3] fix test --- packages/node/src/stellar/api.stellar.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/stellar/api.stellar.spec.ts b/packages/node/src/stellar/api.stellar.spec.ts index 6c945162..d85311f9 100644 --- a/packages/node/src/stellar/api.stellar.spec.ts +++ b/packages/node/src/stellar/api.stellar.spec.ts @@ -94,7 +94,7 @@ describe('StellarApi', () => { { type: { toString: () => 'invoke_host_function' } }, ]); await expect((stellarApi as any).fetchAndWrapLedger(100)).rejects.toThrow( - /access a ledger that is earlier than the latest ledger number/, + /access a ledger that is after the latest ledger number/, ); }); });