Skip to content

Commit a98225a

Browse files
authored
Merge pull request #434 from hack-a-chain-software/orphan-transactions
fix: adjusted orphaned transactions field to use the canonical flag
2 parents 123160e + f28bafe commit a98225a

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ export interface GetNodeInfo {
2323

2424
type AllInfo = NetworkStatistics & HashRateAndTotalDifficulty & GetNodeInfo;
2525

26+
export type CurrentChainHeights = Record<string, number>;
27+
2628
export default interface NetworkRepository {
2729
getNetworkStatistics(): Promise<NetworkStatistics>;
2830
getHashRateAndTotalDifficulty(chainIds: number[]): Promise<HashRateAndTotalDifficulty>;
2931
getNodeInfo(): Promise<GetNodeInfo>;
3032
getAllInfo(): Promise<AllInfo>;
33+
getCurrentChainHeights(): Promise<CurrentChainHeights>;
3134
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export interface GetTransactionsCountParams {
8484
export interface GetTransactionsByRequestKey {
8585
/** The request key to search for */
8686
requestKey: string;
87+
/** Current heights of each chain */
88+
currentChainHeights: Record<string, number>;
8789
/** Optional block hash to narrow the search */
8890
blockHash?: string | null;
8991
/** Optional minimum confirmation depth requirement */

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

Lines changed: 16 additions & 0 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+
CurrentChainHeights,
1314
GetNodeInfo,
1415
HashRateAndTotalDifficulty,
1516
NetworkStatistics,
@@ -216,6 +217,21 @@ export default class NetworkDbRepository implements NetworkRepository {
216217
return output;
217218
}
218219

220+
async getCurrentChainHeights(): Promise<CurrentChainHeights> {
221+
const heightsQuery = `
222+
SELECT "chainId", "canonicalBlocks" FROM "Counters" ORDER BY "canonicalBlocks"
223+
`;
224+
225+
const { rows } = await rootPgPool.query(heightsQuery);
226+
227+
const output = rows.reduce((acc, row) => {
228+
acc[row.chainId] = row.canonicalBlocks;
229+
return acc;
230+
}, {} as CurrentChainHeights);
231+
232+
return output;
233+
}
234+
219235
/**
220236
* Retrieves all network information from cache
221237
*

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ export default class TransactionDbRepository implements TransactionRepository {
358358
* @returns Promise resolving to matching transactions
359359
*/
360360
async getTransactionsByRequestKey(params: GetTransactionsByRequestKey) {
361-
const { requestKey, blockHash, minimumDepth } = params;
361+
const { requestKey, blockHash, minimumDepth, currentChainHeights } = params;
362362
const queryParams: (string | number)[] = [requestKey];
363363
let conditions = '';
364364

@@ -381,6 +381,7 @@ export default class TransactionDbRepository implements TransactionRepository {
381381
b.height as "height",
382382
b."hash" as "blockHash",
383383
b."chainId" as "chainId",
384+
b.canonical as canonical,
384385
t.result as "result",
385386
td.gas as "gas",
386387
td.step as step,
@@ -397,17 +398,14 @@ export default class TransactionDbRepository implements TransactionRepository {
397398

398399
const { rows } = await rootPgPool.query(query, queryParams);
399400

400-
let transactions: TransactionOutput[] = [...rows];
401+
const canonicalTxs = rows.filter(r => r.canonical === true);
402+
const orphanedTxs = rows.filter(r => r.canonical === false);
403+
let transactions: TransactionOutput[] = [...canonicalTxs, ...orphanedTxs];
401404

402405
if (minimumDepth) {
403-
const blockRepository = new BlockDbRepository();
404-
const blockHashToDepth = await blockRepository.createBlockDepthMap(
405-
rows.map(row => ({ hash: row.blockHash })),
406-
'hash',
407-
minimumDepth,
406+
const filteredTxs = rows.filter(
407+
row => currentChainHeights[row.chainId] - row.height >= minimumDepth,
408408
);
409-
410-
const filteredTxs = rows.filter(event => blockHashToDepth[event.blockHash] >= minimumDepth);
411409
transactions = [...filteredTxs];
412410
}
413411

indexer/src/kadena-server/resolvers/node-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ export const getNode = async (context: ResolverContext, id: string) => {
9797
if (type === 'Transaction') {
9898
// Resolve Transaction node - requires blockHash and requestKey
9999
const [blockHash, requestKey] = JSON.parse(params);
100+
const currentChainHeights = await context.networkRepository.getCurrentChainHeights();
100101
const output = await context.transactionRepository.getTransactionsByRequestKey({
101102
requestKey,
102103
blockHash,
104+
currentChainHeights,
103105
});
104106

105107
const outputs = output.map(t => buildTransactionOutput(t));

indexer/src/kadena-server/resolvers/query/transaction-query-resolver.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ export const transactionQueryResolver: QueryResolvers<ResolverContext>['transact
4242
throw new Error('Minimum depth must not be higher than 100.');
4343
}
4444

45+
const currentChainHeights = await context.networkRepository.getCurrentChainHeights();
4546
const transactions = await context.transactionRepository.getTransactionsByRequestKey({
4647
requestKey,
4748
blockHash,
4849
minimumDepth,
50+
currentChainHeights,
4951
});
5052

5153
if (transactions.length === 0) return null;
5254

53-
const [first, ...rest] =
54-
await context.blockRepository.getTransactionsOrderedByBlockDepth(transactions);
55+
const [canonical, ...orphanedTransactions] = transactions;
5556

5657
return {
57-
...buildTransactionOutput(first),
58-
orphanedTransactions: rest.map(r => buildTransactionOutput(r)),
58+
...buildTransactionOutput(canonical),
59+
orphanedTransactions: orphanedTransactions.map(tx => buildTransactionOutput(tx)),
5960
};
6061
};

0 commit comments

Comments
 (0)