-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
368 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import { Type } from '@sinclair/typebox'; | ||
import { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'; | ||
import { Server } from 'http'; | ||
import { | ||
BlockHashResponse, | ||
BlockHeightParam, | ||
BlockHeightResponse, | ||
BlockTimestampResponse, | ||
NotFoundResponse, | ||
} from '../schemas'; | ||
|
||
const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = ( | ||
fastify, | ||
options, | ||
done | ||
) => { | ||
// todo: add blockheight cache? or re-use the inscriptions per block cache (since that would invalidate on gaps as well) | ||
// fastify.addHook('preHandler', handleInscriptionTransfersCache); | ||
|
||
fastify.get( | ||
'/blockheight', | ||
{ | ||
schema: { | ||
operationId: 'getBlockHeight', | ||
summary: 'Recursion', | ||
description: 'Retrieves the latest block height', | ||
tags: ['Recursion'], | ||
response: { | ||
200: BlockHeightResponse, | ||
404: NotFoundResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const blockHeight = (await fastify.db.getChainTipBlockHeight()) ?? 'blockheight'; | ||
// Currently, the `chain_tip` materialized view should always return a | ||
// minimum of 767430 (inscription #0 genesis), but we'll keep the fallback | ||
// to stay consistent with `ord`. | ||
|
||
await reply.send(blockHeight.toString()); | ||
} | ||
); | ||
|
||
fastify.get( | ||
'/blockhash', | ||
{ | ||
schema: { | ||
operationId: 'getBlockHash', | ||
summary: 'Recursion', | ||
description: 'Retrieves the latest block hash', | ||
tags: ['Recursion'], | ||
response: { | ||
200: BlockHashResponse, | ||
404: NotFoundResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const blockHash = (await fastify.db.getBlockHash()) ?? 'blockhash'; | ||
await reply.send(blockHash); | ||
} | ||
); | ||
|
||
fastify.get( | ||
'/blocktime', | ||
{ | ||
schema: { | ||
operationId: 'getBlockTime', | ||
summary: 'Recursion', | ||
description: 'Retrieves the latest block time', | ||
tags: ['Recursion'], | ||
response: { | ||
200: BlockTimestampResponse, | ||
404: NotFoundResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const blockTime = (await fastify.db.getBlockTimestamp()) ?? 'blocktime'; | ||
await reply.send(blockTime); | ||
} | ||
); | ||
|
||
done(); | ||
}; | ||
|
||
const ShowRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = ( | ||
fastify, | ||
options, | ||
done | ||
) => { | ||
// todo: add blockheight cache? or re-use the inscriptions per block cache (since that would invalidate on gaps as well) | ||
// fastify.addHook('preHandler', handleInscriptionCache); | ||
|
||
fastify.get( | ||
'/blockhash/:block_height', | ||
{ | ||
schema: { | ||
operationId: 'getBlockHash', | ||
summary: 'Recursion', | ||
description: 'Retrieves the block hash for a given block height', | ||
tags: ['Recursion'], | ||
params: Type.Object({ | ||
block_height: BlockHeightParam, | ||
}), | ||
response: { | ||
200: BlockHashResponse, | ||
404: NotFoundResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const blockHash = (await fastify.db.getBlockHash(request.params.block_height)) ?? 'blockhash'; | ||
await reply.send(blockHash); | ||
} | ||
); | ||
|
||
done(); | ||
}; | ||
|
||
export const RecursionRoutes: FastifyPluginAsync< | ||
Record<never, never>, | ||
Server, | ||
TypeBoxTypeProvider | ||
> = async fastify => { | ||
await fastify.register(IndexRoutes); | ||
await fastify.register(ShowRoutes); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { buildApiServer } from '../src/api/init'; | ||
import { cycleMigrations } from '../src/pg/migrations'; | ||
import { PgStore } from '../src/pg/pg-store'; | ||
import { TestFastifyServer, randomHash, testRevealApply } from './helpers'; | ||
|
||
describe('recursion routes', () => { | ||
let db: PgStore; | ||
let fastify: TestFastifyServer; | ||
|
||
beforeEach(async () => { | ||
db = await PgStore.connect({ skipMigrations: true }); | ||
fastify = await buildApiServer({ db }); | ||
await cycleMigrations(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await fastify.close(); | ||
await db.close(); | ||
}); | ||
|
||
describe('/blockheight', () => { | ||
test('returns default `blockheight` when no blocks found', async () => { | ||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockheight', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe('767430'); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
|
||
test('returns latest block height', async () => { | ||
await db.updateInscriptions(testRevealApply(778_001)); | ||
|
||
let response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockheight', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe('778001'); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
|
||
await db.updateInscriptions(testRevealApply(778_002)); | ||
|
||
response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockheight', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe('778002'); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
}); | ||
|
||
describe('/blockhash', () => { | ||
test('returns default `blockhash` when no blocks found', async () => { | ||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockhash', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe('blockhash'); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
|
||
test('returns latest block hash', async () => { | ||
let blockHash = randomHash(); | ||
await db.updateInscriptions(testRevealApply(778_001, { blockHash })); | ||
|
||
let response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockhash', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe(blockHash); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
|
||
blockHash = randomHash(); | ||
await db.updateInscriptions(testRevealApply(778_002, { blockHash })); | ||
|
||
response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blockhash', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe(blockHash); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
|
||
test('returns block hash by block height', async () => { | ||
const blockHash = randomHash(); | ||
await db.updateInscriptions(testRevealApply(778_001)); | ||
await db.updateInscriptions(testRevealApply(778_002, { blockHash })); | ||
await db.updateInscriptions(testRevealApply(778_003)); | ||
|
||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: `/ordinals/v1/blockhash/778002`, | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe(blockHash); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
}); | ||
|
||
describe('/blocktime', () => { | ||
test('returns default `blocktime` when no blocks found', async () => { | ||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blocktime', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe('blocktime'); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
|
||
test('returns latest block timestamp', async () => { | ||
let timestamp = Date.now(); | ||
await db.updateInscriptions(testRevealApply(778_001, { timestamp })); | ||
|
||
let response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blocktime', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe(timestamp.toString()); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
|
||
timestamp = Date.now(); | ||
await db.updateInscriptions(testRevealApply(778_002, { timestamp })); | ||
|
||
response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/ordinals/v1/blocktime', | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBe(timestamp.toString()); | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.