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

Bug fixes #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
};

await expect(
generateAbis([ds], PROJECT_PATH, undefined as any, undefined as any, undefined as any)

Check warning on line 40 in packages/common-starknet/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 40 in packages/common-starknet/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 40 in packages/common-starknet/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
).rejects.toThrow(/Asset: "zkLend" not found in project/);
).rejects.toThrow('Error: Asset zkLend, file ./abis/xxx.json does not exist');
});

it('render correct codegen from ejs', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import {
IBlock,
IStoreModelProvider,
} from '@subql/node-core';
import { StarknetBlock } from '@subql/types-starknet';
import {
StarknetProjectDs,
SubqueryProject,
} from '../../configure/SubqueryProject';
import { isFullBlock } from '../../starknet/block.starknet';
import { IndexerManager } from '../indexer.manager';
import { BlockContent, getBlockSize } from '../types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,16 @@ describe('buildDictionaryV1QueryEntries', () => {
const result = buildDictionaryV1QueryEntries([ds]);

expect(result).toEqual([
[
{
conditions: [
{
field: 'type',
matcher: 'equalTo',
value: 'L1_HANDLER',
},
],
entity: 'calls',
},
],
{
conditions: [
{
field: 'type',
matcher: 'equalTo',
value: 'L1_HANDLER',
},
],
entity: 'calls',
},
]);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ProjectService extends BaseProjectService<
protected async getBlockTimestamp(height: number): Promise<Date> {
const block = await this.apiService.unsafeApi.api.getBlock(height);

return new Date(block.timestamp * 1000); // TODO test and make sure its in MS not S
return new Date(block.timestamp * 1000);
}

protected onProjectChange(project: SubqueryProject): void | Promise<void> {
Expand Down
7 changes: 5 additions & 2 deletions packages/node/src/indexer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { isFullBlock } from '../starknet/block.starknet';
export type BlockContent = StarknetBlock | LightStarknetBlock;

export function getBlockSize(block: BlockContent): number {
// TODO. not sure if this is the right way to determine the block size
return isFullBlock(block) ? (block as StarknetBlock).transactions.length : 0;
return isFullBlock(block)
? block.transactions
.map((tx) => tx.receipt.execution_resources.steps)
.reduce((sum, steps) => sum + steps, 0)
: block.transactions.length;
}
4 changes: 1 addition & 3 deletions packages/node/src/indexer/unfinalizedBlocks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import { BlockContent } from './types';

@Injectable()
export class UnfinalizedBlocksService extends BaseUnfinalizedBlocksService<BlockContent> {
private supportsFinalization?: boolean;
private startupCheck = true;

constructor(
private readonly apiService: ApiService,
nodeConfig: NodeConfig,
Expand All @@ -38,6 +35,7 @@ export class UnfinalizedBlocksService extends BaseUnfinalizedBlocksService<Block

protected async getHeaderForHash(hash: string): Promise<Header> {
const block = await this.apiService.api.getBlockByHeightOrHash(hash);

return starknetBlockToHeader(formatBlock(block));
}

Expand Down
7 changes: 0 additions & 7 deletions packages/node/src/starknet/api.service.starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ describe('ApiService', () => {
).resolves.toHaveLength(4);
});

it('can fetch block events with height', async () => {
//https://starkscan.co/block/500000#events
await expect(apiService.api.fetchBlockLogs(500000)).resolves.toHaveLength(
637,
);
});

it('can get the finalized height', async () => {
const height = (await apiService.api.getFinalizedBlock()).block_number;

Expand Down
10 changes: 5 additions & 5 deletions packages/node/src/starknet/api.starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ describe('Api.starknet', () => {
expect(blockData.logs[0].transaction.calldata.length).toBeGreaterThan(1);
});

it('should have the ability to get receipts via transactions from all types', async () => {
expect(await blockData.transactions[0].receipt?.()).toBeDefined();
it('should have the ability to get receipts via transactions from all types', () => {
expect(blockData.transactions[0].receipt).toBeDefined();

expect(typeof blockData.transactions[0].receipt).toEqual('function');
expect(typeof blockData.logs[0].transaction.receipt).toEqual('function');
expect(typeof blockData.transactions[0].receipt).toEqual('object');
expect(typeof blockData.logs[0].transaction.receipt).toEqual('object');
expect(typeof blockData.logs[0].transaction.from).toEqual('string');
expect(typeof blockData.transactions[1].logs![0].transaction.from).toEqual(
'string',
);
expect(
typeof blockData.transactions[1].logs![0].transaction.receipt,
).toEqual('function');
).toEqual('object');
});

//https://starkscan.co/tx/0x0153a20567e66728f3c4ba60913a6011b9c73db9ea4d960e959923ed5afd8a24
Expand Down
167 changes: 64 additions & 103 deletions packages/node/src/starknet/api.starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@

import assert from 'assert';
import fs from 'fs';
import http from 'http';
import https from 'https';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { BlockWithTxs, SPEC } from '@starknet-io/types-js';
import { getLogger, IBlock, timeout } from '@subql/node-core';
import { BlockWithTxs } from '@starknet-io/types-js';
import { getLogger, IBlock } from '@subql/node-core';
import {
ApiWrapper,
StarknetBlock,
StarknetRuntimeDatasource,
IStarknetEndpointConfig,
LightStarknetBlock,
StarknetLogRaw,
StarknetResult,
StarknetTransaction,
StarknetLog,
StarknetContractCall,
isFulfilledBlock,
} from '@subql/types-starknet';
import {
AbiInterfaces,
ProviderInterface,
RpcProvider,
RpcProviderOptions,
TransactionReceipt,
events,
CallData,
Abi,
AbiEntry,
FunctionAbi,
transaction,
} from 'starknet';
import { SPEC } from 'starknet-types-07';
import SafeStarknetProvider from './safe-api';
import {
hexEq,
Expand All @@ -40,7 +38,6 @@ import {
formatBlock,
formatBlockUtil,
formatLog,
formatReceipt,
formatTransaction,
reverseToRawLog,
} from './utils.starknet';
Expand All @@ -50,8 +47,6 @@ const { version: packageVersion } = require('../../package.json');

const logger = getLogger('api.starknet');

const DEFAULT_EVENT_CHUNK_SIZE = 1000;

async function loadAssets(
ds: StarknetRuntimeDatasource,
): Promise<Record<string, string>> {
Expand Down Expand Up @@ -111,7 +106,7 @@ export class StarknetApi implements ApiWrapper {
},
batch: this.config?.batchSize ?? false,
};
searchParams.forEach((value, name, searchParams) => {
searchParams.forEach((value, name) => {
(connection.headers as any)[name] = value;
});
this.client = new RpcProvider(connection);
Expand Down Expand Up @@ -162,22 +157,26 @@ export class StarknetApi implements ApiWrapper {

/***
* Get the latest block (with its header)
* we will try to get the latest block, if it is rejected, we will get the latest accepted block
* @returns {Promise<BLOCK_HEADER>}
*/
async getFinalizedBlock(): Promise<SPEC.BLOCK_HEADER> {
async getFinalizedBlock(): Promise<Partial<SPEC.BLOCK_HEADER>> {
// we can not direct use this.client.getBlockLatestAccepted(), because its return missing parent block hash,but we still can retrieve the block number.
// In the meantime we also fetch the latest block, if it is rejected, we will get the latest accepted and fetch more details
const [latestBlock, latestAccepted] = await Promise.all([
this.client.getBlock('latest'),
await this.getFinalizedBlockHeight(),
this.getFinalizedBlockHeight(),
]);
// @ts-ignore
let b: SPEC.BLOCK_HEADER = latestBlock;

// If latest block been reject, we need to fetch the latest accepted block
if (latestBlock.status === 'REJECTED') {
b = (await this.getBlockByHeightOrHash(
latestAccepted,
)) as SPEC.BLOCK_WITH_TXS;
const block = await this.getBlockByHeightOrHash(latestAccepted);
return {
block_hash: block.block_hash,
block_number: block.block_number,
parent_hash: block.parent_hash,
};
}
return b;
return latestBlock;
}

async getFinalizedBlockHeight(): Promise<number> {
Expand All @@ -200,109 +199,69 @@ export class StarknetApi implements ApiWrapper {

async getBlockByHeightOrHash(
heightOrHash: number | string,
): Promise<BlockWithTxs> {
// @ts-ignore
return this.client.getBlockWithTxs(heightOrHash);
}

private async getBlockPromise(num: number, includeTx = true): Promise<any> {
const rawBlock = includeTx
? await this.client.getBlockWithTxs(num)
: await this.client.getBlockWithTxHashes(num);

if (!rawBlock) {
throw new Error(`Failed to fetch block ${num}`);
): Promise<SPEC.BLOCK_WITH_RECEIPTS> {
const block = await this.client.getBlockWithReceipts(heightOrHash);
if (!isFulfilledBlock(block)) {
jiqiang90 marked this conversation as resolved.
Show resolved Hide resolved
throw `Block ${block} is not a fulfilled block, its parent is ${block.parent_hash}`;
}

const block = formatBlock(rawBlock);

return block;
}

async getTransactionReceipt(
transactionHash: string,
): Promise<TransactionReceipt> {
const receipt = await this.client.getTransactionReceipt(transactionHash);
return formatReceipt(receipt);
}

async fetchBlock(blockNumber: number): Promise<IBlock<StarknetBlock>> {
try {
const block: StarknetBlock = await this.getBlockPromise(
blockNumber,
true,
);
const logsRaw = await this.fetchBlockLogs(blockNumber);

block.logs = logsRaw.map((l) => formatLog(l, block));
// Cast as Tx still raw here
block.transactions = (block.transactions as Record<string, any>).map(
(tx, index) => ({
// Format done
...formatTransaction(tx, block, index),
receipt: () =>
this.getTransactionReceipt(tx.transaction_hash).then((r) =>
formatReceipt(r),
),
logs: block.logs.filter(
(l) => l.transactionHash === tx.transaction_hash,
const rawBlock = await this.getBlockByHeightOrHash(blockNumber);
const formattedBlock = formatBlock(rawBlock);

const formattedTransactions = rawBlock.transactions.map((tx, index) => {
const formattedTransaction = formatTransaction(
tx,
formattedBlock,
index,
);
return {
...formattedTransaction,
logs: tx.receipt.events.map((l, logIndex) =>
formatLog(l, logIndex, formattedTransaction, formattedBlock),
),
}),
};
});

const logs: StarknetLog[] = formattedTransactions.flatMap((tx) =>
tx.logs.map((l) => ({
...l,
transaction: tx,
})),
);

const block: StarknetBlock = {
...formattedBlock,
transactions: formattedTransactions,
logs,
};

this.eventEmitter.emit('fetchBlock');
return formatBlockUtil(block);
} catch (e: any) {
throw this.handleError(e);
}
}

// This follow method from official document https://starknetjs.com/docs/guides/events
async fetchBlockLogs(blockNumber: number): Promise<StarknetLogRaw[]> {
let continuationToken: string | undefined = '0';
let chunkNum = 1;
const allEvents: StarknetLogRaw[] = [];

while (continuationToken) {
let eventsRes;
try {
eventsRes = await this.client.getEvents({
from_block: {
block_number: blockNumber,
},
to_block: {
block_number: blockNumber,
},
chunk_size: DEFAULT_EVENT_CHUNK_SIZE,
continuation_token:
continuationToken === '0' ? undefined : continuationToken,
});
} catch (e: any) {
if (!eventsRes) {
throw new Error(
`Fetch block ${blockNumber} events failed, ${e.message}`,
);
} else {
throw e;
}
}
const nbEvents = eventsRes.events.length;
continuationToken = eventsRes.continuation_token;
for (let i = 0; i < nbEvents; i++) {
const event = eventsRes.events[i];
allEvents.push(event);
}
chunkNum++;
}
return allEvents;
}

private async fetchLightBlock(
blockNumber: number,
): Promise<IBlock<LightStarknetBlock>> {
const block = await this.getBlockPromise(blockNumber, false);
const block = (await this.client.getBlockWithTxHashes(
blockNumber,
)) as SPEC.BLOCK_WITH_TX_HASHES;
const lightBlock: LightStarknetBlock = {
...block,
blockHash: block.block_hash,
blockNumber: block.block_number,
parentHash: block.parent_hash,
newRoot: block.new_root,
sequencerAddress: block.sequencer_address,
l1GasPrice: block.l1_gas_price,
starknetVersion: block.starknet_version,
logs: [],
};
return formatBlockUtil<LightStarknetBlock>(lightBlock);
}
Expand All @@ -314,7 +273,9 @@ export class StarknetApi implements ApiWrapper {
async fetchBlocksLight(
bufferBlocks: number[],
): Promise<IBlock<LightStarknetBlock>[]> {
return Promise.all(bufferBlocks.map(async (num) => this.fetchBlock(num)));
return Promise.all(
bufferBlocks.map(async (num) => this.fetchLightBlock(num)),
);
}

get api(): RpcProvider {
Expand Down
Loading
Loading