Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default interface BlockRepository {

getTotalCountOfBlockEvents(blockHash: string): Promise<number>;

getTotalCountOfBlockTransactions(blockHash: string): Promise<number>;

getLatestBlocks(params: GetLatestBlocksParams): Promise<BlockOutput[]>;

getTransactionsOrderedByBlockDepth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,27 @@ export default class BlockDbRepository implements BlockRepository {
return block?.transactionsCount || 0;
}

/**
* Counts the total number of transactions in a specific block
*
* This method performs a COUNT query to determine how many transactions
* are associated to a particular block.
*
* @param blockHash - The hash of the block to count transactions for
* @returns Promise resolving to the total count of transactions in the block
*/
async getTotalCountOfBlockTransactions(blockHash: string): Promise<number> {
const { rows } = await rootPgPool.query(
`SELECT COUNT(*) as "totalCount"
FROM "Blocks" b
JOIN "Transactions" t ON t."blockId" = b.id
WHERE b.hash = $1`,
[blockHash],
);

return rows[0].totalCount ?? 0;
}

/**
* Retrieves the most recent blocks created after a specific timestamp
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,21 @@ export default class TransactionDbRepository implements TransactionRepository {
const { rows } = await rootPgPool.query(query, queryParams);

// Transform database rows into GraphQL-compatible edges with cursors
const edges = rows.map(row => ({
cursor: row.creationTime.toString(),
node: transactionValidator.validate(row),
}));
const edges = rows
.map(row => ({
cursor: row.creationTime.toString(),
node: transactionValidator.validate(row),
}))
.sort((a, b) => {
// Primary sort is already done by DB query (creationTime DESC)
// Add secondary sort by id for consistent ordering when creationTimes are equal
const aNode = a.node as unknown as { id: string };
const bNode = b.node as unknown as { id: string };
if (a.cursor === b.cursor) {
return aNode.id > bNode.id ? 1 : -1;
}
return 0; // Maintain existing order from DB for different creationTimes
});

const pageInfo = getPageInfo({ edges, order, limit, after, before });
return pageInfo;
Expand Down
19 changes: 1 addition & 18 deletions indexer/src/kadena-server/repository/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const getPageInfo = <T>({
// that was fetched to determine if there are more pages
const limit = (limitParam ?? DEFAULT_LIMIT) - LIMIT_NEXT_PAGE_CHECK;

// Case 1: No results found - return empty page info
// Empty case: No results found - return empty page info
if (length === 0) {
return {
pageInfo: {
Expand All @@ -128,23 +128,6 @@ export const getPageInfo = <T>({
};
}

// Case 2: Only a single result - simplify the logic for this case
if (length === 1) {
return {
pageInfo: {
// For a single result, both start and end cursor are the same
startCursor: encodeCursor(edges[0].cursor),
endCursor: encodeCursor(edges[0].cursor),
// If 'before' exists, there might be more newer items (for backward pagination)
hasNextPage: !!before,
// If 'after' exists, there might be more older items (for forward pagination)
hasPreviousPage: !!after,
},
edges,
};
}

// Case 3: Multiple results - compute page info based on sort order
let hasNextPage = false;
let hasPreviousPage = false;
let startCursor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const totalCountBlockTransactionsConnectionResolver: BlockTransactionsCon
async (parent, _args, context) => {
const { blockHash } = schema.parse(parent);

const total = await context.blockRepository.getTotalCountOfBlockEvents(blockHash);
const total = await context.blockRepository.getTotalCountOfBlockTransactions(blockHash);

return total;
};
6 changes: 3 additions & 3 deletions indexer/tests/integration/fixtures/block/block.fixture.001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const blockFixture001 = {
},
edges: [
{
cursor: '257165675',
cursor: 'MjU3MTY1Njc1',
node: {
chainId: 10,
height: 5370064,
Expand Down Expand Up @@ -71,7 +71,7 @@ export const blockFixture001 = {
powHash: '00000000000000017af989466338f4c86bd3e2a9e6eb7860366fe377428b8bd7',
target: 'WcVbkSM_1xf7DgJgoL6QKFKrX4SFcvxEEAAAAAAAAAA',
transactions: {
totalCount: 0,
totalCount: 1,
pageInfo: {
endCursor: 'MTczMzU3MjgwMw==',
hasNextPage: false,
Expand All @@ -80,7 +80,7 @@ export const blockFixture001 = {
},
edges: [
{
cursor: '1733572803',
cursor: 'MTczMzU3MjgwMw==',
node: {
id: 'VHJhbnNhY3Rpb246WyJiNnBybXAzVkZIeE12cmR3VlpfREtiOGVvNW1vUEp2M1N3TkEwR0xwOEkwIiwiSWs1QmVVaG5MVGs1UlZaRk9XOXlMV0p0VlU1SFZIZHViR3d6TFRGdE5VMUVNaloyU1ZaWVprVklVbWNpIl0=',
},
Expand Down
Loading