Skip to content

Commit

Permalink
fix: offset working without cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Aug 24, 2024
1 parent 35171b1 commit e59d91e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/datastore/pg-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,15 @@ export class PgStoreV2 extends BasePgStoreModule {
)
SELECT offset_block_height as block_height
FROM ordered_blocks
WHERE block_hash = ${cursor}
WHERE block_hash = ${cursor ? cursor : sql`(SELECT block_hash FROM chain_tip LIMIT 1)`}
LIMIT 1
),
filtered_blocks AS (
SELECT *
selected_blocks AS (
SELECT ${sql(BLOCK_COLUMNS)}
FROM blocks
WHERE canonical = true
${cursor ? sql`AND block_height <= (SELECT block_height FROM cursor_block)` : sql``}
AND block_height <= (SELECT block_height FROM cursor_block)
ORDER BY block_height DESC
),
selected_blocks AS (
SELECT ${sql(BLOCK_COLUMNS)}
FROM filtered_blocks
LIMIT ${limit}
),
prev_page AS (
Expand Down Expand Up @@ -147,7 +143,7 @@ export class PgStoreV2 extends BasePgStoreModule {

const result: DbCursorPaginatedResult<DbBlock> = {
limit,
offset: 0,
offset: offset,
results: blocks,
total: total,
next_cursor: nextCursor,
Expand Down
24 changes: 21 additions & 3 deletions src/tests/block-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ describe('block tests', () => {
expect(body).toEqual(
expect.objectContaining({
limit: 3,
offset: 0,
offset: 2,
total: 14,
cursor: '0x0000000000000000000000000000000000000000000000000000000000000009',
next_cursor: '0x0000000000000000000000000000000000000000000000000000000000000012',
Expand All @@ -867,14 +867,14 @@ describe('block tests', () => {
})
);

// Test with negative offset
// Negative offset + cursor
({ body } = await supertest(api.server).get(
`/extended/v2/blocks?limit=3&cursor=0x0000000000000000000000000000000000000000000000000000000000000008&offset=-2`
));
expect(body).toEqual(
expect.objectContaining({
limit: 3,
offset: 0,
offset: -2,
total: 14,
cursor: '0x0000000000000000000000000000000000000000000000000000000000000010',
next_cursor: '0x0000000000000000000000000000000000000000000000000000000000000013',
Expand All @@ -886,6 +886,24 @@ describe('block tests', () => {
],
})
);

// Offset (no cursor) works, has original behavior
({ body } = await supertest(api.server).get(`/extended/v2/blocks?limit=3&offset=5`));
expect(body).toEqual(
expect.objectContaining({
limit: 3,
offset: 5,
total: 14,
cursor: '0x0000000000000000000000000000000000000000000000000000000000000009',
next_cursor: '0x0000000000000000000000000000000000000000000000000000000000000012',
prev_cursor: '0x0000000000000000000000000000000000000000000000000000000000000006',
results: [
expect.objectContaining({ height: 9 }),
expect.objectContaining({ height: 8 }),
expect.objectContaining({ height: 7 }),
],
})
);
});

test('blocks v2 retrieved by hash or height', async () => {
Expand Down

0 comments on commit e59d91e

Please sign in to comment.