Skip to content

Commit 7598c02

Browse files
authored
Merge pull request #436 from hack-a-chain-software/counters-of-network
feat: added counters to the network info query
2 parents 2881465 + 8e86d75 commit 7598c02

File tree

6 files changed

+115
-4
lines changed

6 files changed

+115
-4
lines changed

indexer/src/cache/init.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
import { ResolverContext } from '../kadena-server/config/apollo-server-config';
1818
import NodeCache from 'node-cache';
19-
import { HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, NETWORK_STATISTICS_KEY, NODE_INFO_KEY } from './keys';
19+
import {
20+
COUNTERS_OF_EACH_CHAIN_KEY,
21+
HASH_RATE_AND_TOTAL_DIFFICULTY_KEY,
22+
NETWORK_STATISTICS_KEY,
23+
NODE_INFO_KEY,
24+
} from './keys';
2025
import { HashRateAndTotalDifficulty } from '../kadena-server/repository/application/network-repository';
2126

2227
/**
@@ -108,6 +113,22 @@ export default async function initCache(context: ResolverContext) {
108113
}
109114
}
110115

116+
/**
117+
* Fetches and caches information about the blockchain node
118+
*
119+
* This includes node version, connectivity status, and other
120+
* node-specific information that helps monitor the node's health.
121+
*/
122+
123+
async function getCountersOfEachChain() {
124+
try {
125+
const counters = await networkRepository.getCountersOfEachChain();
126+
MEMORY_CACHE.set(COUNTERS_OF_EACH_CHAIN_KEY, counters);
127+
} catch (err) {
128+
console.error('[ERROR][CACHE][CONN_TIMEOUT] Failed to get counters of each chain', err);
129+
}
130+
}
131+
111132
// Initialize the hash rate cache with a default value
112133
// The -1 value indicates that real data hasn't been loaded yet
113134
MEMORY_CACHE.set(HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, {
@@ -124,6 +145,7 @@ export default async function initCache(context: ResolverContext) {
124145
await getNetworkStatistics();
125146
await getNodeInfo();
126147
await getHashRateAndTotalDifficulty();
148+
await getCountersOfEachChain();
127149
};
128150

129151
// Populate cache with initial data

indexer/src/cache/keys.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ export const NETWORK_STATISTICS_KEY = 'NETWORK_STATISTICS_KEY';
2424
* Used to store and retrieve information about the blockchain node
2525
*/
2626
export const NODE_INFO_KEY = 'NODE_INFO_KEY';
27+
28+
/**
29+
* Key for caching counters of each chain data
30+
* Used to store and retrieve information about the numbers of entities in each chain
31+
*/
32+
export const COUNTERS_OF_EACH_CHAIN_KEY = 'COUNTERS_OF_EACH_CHAIN_KEY';

indexer/src/kadena-server/config/graphql-types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ export type ContinuationPayload = {
131131
step?: Maybe<Scalars['Int']['output']>;
132132
};
133133

134+
export type CountersOfEachChain = {
135+
__typename?: 'CountersOfEachChain';
136+
blocksCount: Scalars['Int']['output'];
137+
chainId: Scalars['String']['output'];
138+
totalGasUsed: Scalars['String']['output'];
139+
transactionCount: Scalars['Int']['output'];
140+
};
141+
134142
/** DEX metrics including TVL, volume, and pool count */
135143
export type DexMetrics = {
136144
__typename?: 'DexMetrics';
@@ -394,6 +402,7 @@ export type NetworkInfo = {
394402
apiVersion: Scalars['String']['output'];
395403
/** The number of circulating coins. */
396404
coinsInCirculation: Scalars['Float']['output'];
405+
countersOfEachChain: Array<CountersOfEachChain>;
397406
genesisHeights: Array<GenesisHeight>;
398407
/** The network hash rate. */
399408
networkHashRate: Scalars['Float']['output'];
@@ -1551,6 +1560,7 @@ export type ResolversTypes = {
15511560
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
15521561
ChartDataPoint: ResolverTypeWrapper<ChartDataPoint>;
15531562
ContinuationPayload: ResolverTypeWrapper<ContinuationPayload>;
1563+
CountersOfEachChain: ResolverTypeWrapper<CountersOfEachChain>;
15541564
DateTime: ResolverTypeWrapper<Scalars['DateTime']['output']>;
15551565
Decimal: ResolverTypeWrapper<Scalars['Decimal']['output']>;
15561566
DexMetrics: ResolverTypeWrapper<DexMetrics>;
@@ -1846,6 +1856,7 @@ export type ResolversParentTypes = {
18461856
Boolean: Scalars['Boolean']['output'];
18471857
ChartDataPoint: ChartDataPoint;
18481858
ContinuationPayload: ContinuationPayload;
1859+
CountersOfEachChain: CountersOfEachChain;
18491860
DateTime: Scalars['DateTime']['output'];
18501861
Decimal: Scalars['Decimal']['output'];
18511862
DexMetrics: DexMetrics;
@@ -2196,6 +2207,18 @@ export type ContinuationPayloadResolvers<
21962207
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
21972208
};
21982209

2210+
export type CountersOfEachChainResolvers<
2211+
ContextType = any,
2212+
ParentType extends
2213+
ResolversParentTypes['CountersOfEachChain'] = ResolversParentTypes['CountersOfEachChain'],
2214+
> = {
2215+
blocksCount?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
2216+
chainId?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
2217+
totalGasUsed?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
2218+
transactionCount?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
2219+
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
2220+
};
2221+
21992222
export interface DateTimeScalarConfig
22002223
extends GraphQLScalarTypeConfig<ResolversTypes['DateTime'], any> {
22012224
name: 'DateTime';
@@ -2509,6 +2532,11 @@ export type NetworkInfoResolvers<
25092532
> = {
25102533
apiVersion?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
25112534
coinsInCirculation?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
2535+
countersOfEachChain?: Resolver<
2536+
Array<ResolversTypes['CountersOfEachChain']>,
2537+
ParentType,
2538+
ContextType
2539+
>;
25122540
genesisHeights?: Resolver<Array<ResolversTypes['GenesisHeight']>, ParentType, ContextType>;
25132541
networkHashRate?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
25142542
networkHost?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
@@ -3537,6 +3565,7 @@ export type Resolvers<ContextType = any> = {
35373565
BlockTransactionsConnectionEdge?: BlockTransactionsConnectionEdgeResolvers<ContextType>;
35383566
ChartDataPoint?: ChartDataPointResolvers<ContextType>;
35393567
ContinuationPayload?: ContinuationPayloadResolvers<ContextType>;
3568+
CountersOfEachChain?: CountersOfEachChainResolvers<ContextType>;
35403569
DateTime?: GraphQLScalarType;
35413570
Decimal?: GraphQLScalarType;
35423571
DexMetrics?: DexMetricsResolvers<ContextType>;

indexer/src/kadena-server/config/schema.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,14 @@ type NetworkInfo {
10501050
nodePackageVersion: String!
10511051
nodeServiceDate: DateTime
10521052
nodeLatestBehaviorHeight: Int!
1053+
countersOfEachChain: [CountersOfEachChain!]! @complexity(value: 1)
1054+
}
1055+
1056+
type CountersOfEachChain {
1057+
chainId: String!
1058+
blocksCount: Int!
1059+
transactionCount: Int!
1060+
totalGasUsed: String!
10531061
}
10541062

10551063
type GenesisHeight {

indexer/src/kadena-server/repository/application/network-repository.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ export interface GetNodeInfo {
2121
nodeLatestBehaviorHeight: number;
2222
}
2323

24-
type AllInfo = NetworkStatistics & HashRateAndTotalDifficulty & GetNodeInfo;
24+
export interface CountersOfEachChain {
25+
chainId: string;
26+
blocksCount: number;
27+
transactionCount: number;
28+
totalGasUsed: string;
29+
}
30+
31+
type AllInfo = NetworkStatistics &
32+
HashRateAndTotalDifficulty &
33+
GetNodeInfo & {
34+
countersOfEachChain: CountersOfEachChain[];
35+
};
2536

2637
export type CurrentChainHeights = Record<string, number>;
2738

@@ -31,4 +42,5 @@ export default interface NetworkRepository {
3142
getNodeInfo(): Promise<GetNodeInfo>;
3243
getAllInfo(): Promise<AllInfo>;
3344
getCurrentChainHeights(): Promise<CurrentChainHeights>;
45+
getCountersOfEachChain(): Promise<CountersOfEachChain[]>;
3446
}

indexer/src/kadena-server/repository/infra/repository/network-db-repository.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { Op } from 'sequelize';
1111
import BlockModel from '../../../../models/block';
1212
import NetworkRepository, {
13+
CountersOfEachChain,
1314
CurrentChainHeights,
1415
GetNodeInfo,
1516
HashRateAndTotalDifficulty,
@@ -25,7 +26,11 @@ import { rootPgPool } from '../../../../config/database';
2526
import { nodeInfoValidator } from '../schema-validator/node-info-validator';
2627
import { getRequiredEnvString } from '../../../../utils/helpers';
2728
import { MEMORY_CACHE } from '../../../../cache/init';
28-
import { HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, NETWORK_STATISTICS_KEY } from '../../../../cache/keys';
29+
import {
30+
COUNTERS_OF_EACH_CHAIN_KEY,
31+
HASH_RATE_AND_TOTAL_DIFFICULTY_KEY,
32+
NETWORK_STATISTICS_KEY,
33+
} from '../../../../cache/keys';
2934
import { getCirculationNumber } from '../../../../utils/coin-circulation';
3035

3136
// Configuration values from environment variables
@@ -232,6 +237,27 @@ export default class NetworkDbRepository implements NetworkRepository {
232237
return output;
233238
}
234239

240+
async getCountersOfEachChain(): Promise<CountersOfEachChain[]> {
241+
const countersQuery = `
242+
SELECT "chainId", "canonicalBlocks", "canonicalTransactions", "totalGasUsed"
243+
FROM "Counters"
244+
ORDER BY "chainId"
245+
`;
246+
247+
const { rows } = await rootPgPool.query(countersQuery);
248+
249+
const output = rows.map(row => {
250+
return {
251+
chainId: row.chainId,
252+
blocksCount: row.canonicalBlocks,
253+
transactionCount: row.canonicalTransactions,
254+
totalGasUsed: row.totalGasUsed,
255+
};
256+
});
257+
258+
return output;
259+
}
260+
235261
/**
236262
* Retrieves all network information from cache
237263
*
@@ -247,7 +273,15 @@ export default class NetworkDbRepository implements NetworkRepository {
247273
const HashRateAndTotalDifficulty = MEMORY_CACHE.get(
248274
HASH_RATE_AND_TOTAL_DIFFICULTY_KEY,
249275
) as HashRateAndTotalDifficulty;
276+
const countersOfEachChain = MEMORY_CACHE.get(
277+
COUNTERS_OF_EACH_CHAIN_KEY,
278+
) as CountersOfEachChain[];
250279

251-
return { ...nodeInfo, ...networkStatistics, ...HashRateAndTotalDifficulty };
280+
return {
281+
...nodeInfo,
282+
...networkStatistics,
283+
...HashRateAndTotalDifficulty,
284+
countersOfEachChain,
285+
};
252286
}
253287
}

0 commit comments

Comments
 (0)