Skip to content

Commit

Permalink
Include timestamps in headers (#315)
Browse files Browse the repository at this point in the history
* Include timestamps in headers

* Update changelog
  • Loading branch information
stwiname authored Feb 12, 2025
1 parent 1acae59 commit d7639eb
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Logs for failed transactions not being included (#313)
- Headers not including timestamps (#315)

## [4.5.0] - 2025-02-10
### Changed
Expand Down
23 changes: 2 additions & 21 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import assert from 'assert';
import { CosmWasmClient, IndexedTx } from '@cosmjs/cosmwasm-stargate';
import { toHex } from '@cosmjs/encoding';
import { Uint53 } from '@cosmjs/math';
import { GeneratedType, Registry } from '@cosmjs/proto-signing';
import { Block, defaultRegistryTypes, SearchTxQuery } from '@cosmjs/stargate';
import { CometClient, toRfc3339WithNanoseconds } from '@cosmjs/tendermint-rpc';
import { defaultRegistryTypes, SearchTxQuery } from '@cosmjs/stargate';
import { CometClient } from '@cosmjs/tendermint-rpc';
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { CosmosProjectNetConfig } from '@subql/common-cosmos';
Expand Down Expand Up @@ -253,24 +252,6 @@ export class CosmosSafeClient
this.height = height;
}

// Deprecate
async getBlock(): Promise<Block> {
const response = await this.forceGetCometClient().block(this.height);
return {
id: toHex(response.blockId.hash).toUpperCase(),
header: {
version: {
block: new Uint53(response.block.header.version.block).toString(),
app: new Uint53(response.block.header.version.app).toString(),
},
height: response.block.header.height,
chainId: response.block.header.chainId,
time: toRfc3339WithNanoseconds(response.block.header.time),
},
txs: response.block.txs,
};
}

async validators(): Promise<
Awaited<ReturnType<CometClient['validators']>>['validators']
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/indexer/dictionary/v1/dictionaryV1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function mockDictionaryService(
{
network: {
dictionary: url,
chianId: 'juno-1',
chianId: 'fetchhub-4',
},
} as any,
nodeConfig,
Expand All @@ -66,7 +66,7 @@ describe('Dictionary Queries', () => {

beforeAll(async () => {
dictionary = await mockDictionaryService(
'https://gateway.subquery.network/query/QmPjq55mgUt9S8S491Q3wEbb87fXyEkdxymT6Gwe2xe1Z1',
'https://datasource.subquery.dev/query/QmfKJuaL8iwwnSwaKt7RjTBk6Q5aUPqBzcZ1J2Q68ZvnLf',
);
});

Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export class FetchService extends BaseFetchService<
protected async getFinalizedHeader(): Promise<Header> {
// Cosmos has instant finalization
const height = await this.api.getHeight();
return cosmosBlockToHeader(height);
const blockInfo = await this.api.blockInfo(height);
return cosmosBlockToHeader(blockInfo.block.header);
}

protected async getBestHeight(): Promise<number> {
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/indexer/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: GPL-3.0

import { isMainThread } from 'node:worker_threads';
import { toRfc3339WithNanoseconds } from '@cosmjs/tendermint-rpc';
import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
Expand All @@ -17,6 +16,7 @@ import {
import { CosmosDatasource } from '@subql/types-cosmos';
import { Sequelize } from '@subql/x-sequelize';
import { SubqueryProject } from '../configure/SubqueryProject';
import { getBlockTimestamp } from '../utils/cosmos';
import { ApiService } from './api.service';
import { DsProcessorService } from './ds-processor.service';
import { DynamicDsService } from './dynamic-ds.service';
Expand Down Expand Up @@ -72,7 +72,7 @@ export class ProjectService extends BaseProjectService<

protected async getBlockTimestamp(height: number): Promise<Date> {
const response = await this.apiService.api.blockInfo(height);
return new Date(toRfc3339WithNanoseconds(response.block.header.time));
return getBlockTimestamp(response.block.header);
}

protected onProjectChange(project: SubqueryProject): void | Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/indexer/unfinalizedBlocks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class UnfinalizedBlocksService extends BaseUnfinalizedBlocksService<Block

@mainThreadOnly()
async getHeaderForHeight(height: number): Promise<Header> {
return Promise.resolve(cosmosBlockToHeader(height));
const block = await this.apiService.unsafeApi.blockInfo(height);
return cosmosBlockToHeader(block.block.header);
}
}
2 changes: 1 addition & 1 deletion packages/node/src/indexer/worker/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class WorkerService extends BaseWorkerService<
}

protected toBlockResponse(block: BlockContent): FetchBlockResponse {
return cosmosBlockToHeader(block.block.header.height);
return cosmosBlockToHeader(block.block.header);
}

protected async processFetchedBlock(
Expand Down
11 changes: 6 additions & 5 deletions packages/node/src/utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,19 @@ export function wrapEvent(
* Cosmos has instant finalization, there is also no rpc method to get a block by hash
* To get around this we use blockHeights as hashes
*/
export function cosmosBlockToHeader(blockHeight: number): Header {
export function cosmosBlockToHeader(header: CosmosHeader): Header {
return {
blockHeight: blockHeight,
blockHash: blockHeight.toString(),
parentHash: (blockHeight - 1).toString(),
blockHeight: header.height,
blockHash: header.height.toString(),
parentHash: (header.height - 1).toString(),
timestamp: getBlockTimestamp(header),
};
}

export function formatBlockUtil<B extends BlockContent>(block: B): IBlock<B> {
return {
block,
getHeader: () => cosmosBlockToHeader(block.block.header.height),
getHeader: () => cosmosBlockToHeader(block.block.header),
};
}

Expand Down

0 comments on commit d7639eb

Please sign in to comment.