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

Fix handle soroban height behind stellar #42

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions packages/node/src/stellar/api.stellar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 after the latest ledger number/,
);
});
});
34 changes: 12 additions & 22 deletions packages/node/src/stellar/api.stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,10 @@ export class StellarApi implements ApiWrapper<StellarBlockWrapper> {
}

async getAndWrapEvents(height: number): Promise<SorobanEvent[]> {
// 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,
Expand Down Expand Up @@ -311,6 +293,14 @@ export class StellarApi implements ApiWrapper<StellarBlockWrapper> {
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 after 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.
Expand Down
10 changes: 0 additions & 10 deletions packages/node/src/stellar/soroban.server.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<SorobanRpc.GetEventsResponse> {
Expand Down
Loading