-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add more recursion endpoints #144
Draft
janniks
wants to merge
3
commits into
develop
Choose a base branch
from
fix/add-more-recursion-endpoints
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,128 @@ | ||
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'; | ||
import { handleBlockHashCache, handleBlockHeightCache } from '../util/cache'; | ||
|
||
const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = ( | ||
fastify, | ||
options, | ||
done | ||
) => { | ||
fastify.addHook('preHandler', handleBlockHashCache); | ||
|
||
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'; | ||
janniks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
) => { | ||
fastify.addHook('preHandler', handleBlockHeightCache); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { BasePgStore, connectPostgres, logger, runMigrations } from '@hirosystems/api-toolkit'; | ||
import { BitcoinEvent, Payload } from '@hirosystems/chainhook-client'; | ||
import * as path from 'path'; | ||
import { Order, OrderBy } from '../api/schemas'; | ||
import { isProdEnv, isTestEnv, normalizedHexString, parseSatPoint } from '../api/util/helpers'; | ||
import { OrdinalSatoshi, SatoshiRarity } from '../api/util/ordinal-satoshi'; | ||
|
@@ -21,8 +23,6 @@ import { | |
DbPaginatedResult, | ||
LOCATIONS_COLUMNS, | ||
} from './types'; | ||
import { BasePgStore, connectPostgres, logger, runMigrations } from '@hirosystems/api-toolkit'; | ||
import * as path from 'path'; | ||
|
||
export const MIGRATIONS_DIR = path.join(__dirname, '../../migrations'); | ||
|
||
|
@@ -212,6 +212,36 @@ export class PgStore extends BasePgStore { | |
return parseInt(result[0].block_height); | ||
} | ||
|
||
/** | ||
* Returns the block hash of the latest block, or the block hash of the block | ||
* at the given height. | ||
* @param blockHeight - optional block height (defaults to latest block) | ||
*/ | ||
async getBlockHash(blockHeight?: string): Promise<string> { | ||
const clause = blockHeight | ||
? this.sql`WHERE block_height = ${blockHeight}` | ||
: this.sql` | ||
ORDER BY block_height DESC | ||
LIMIT 1 | ||
`; | ||
|
||
const result = await this.sql<{ block_hash: string }[]>` | ||
SELECT block_hash FROM inscriptions_per_block | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could make these queries more efficient if we modified the |
||
${clause} | ||
`; | ||
|
||
return result[0]?.block_hash; | ||
} | ||
|
||
async getBlockTimestamp(): Promise<string> { | ||
const result = await this.sql<{ timestamp: string }[]>` | ||
SELECT ROUND(EXTRACT(EPOCH FROM timestamp)) as timestamp FROM inscriptions_per_block | ||
ORDER BY block_height DESC | ||
LIMIT 1 | ||
`; | ||
return result[0]?.timestamp; | ||
} | ||
|
||
async getChainTipInscriptionCount(): Promise<number> { | ||
const result = await this.sql<{ count: number }[]>` | ||
SELECT count FROM inscription_count | ||
|
@@ -282,6 +312,25 @@ export class PgStore extends BasePgStore { | |
return `${result[0].block_hash}:${result[0].inscription_count}`; | ||
} | ||
|
||
async getBlockHashETag(): Promise<string> { | ||
const result = await this.sql<{ block_hash: string }[]>` | ||
SELECT block_hash | ||
FROM inscriptions_per_block | ||
ORDER BY block_height DESC | ||
LIMIT 1 | ||
`; | ||
return result[0].block_hash; | ||
} | ||
|
||
async getBlockHeightETag(args: { block_height: string }): Promise<string> { | ||
const result = await this.sql<{ block_hash: string }[]>` | ||
SELECT block_hash | ||
FROM inscriptions_per_block | ||
WHERE block_height = ${args.block_height} | ||
`; | ||
return result[0].block_hash; | ||
} | ||
|
||
async getInscriptionContent( | ||
args: InscriptionIdentifier | ||
): Promise<DbInscriptionContent | undefined> { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the word
blockheight
the right thing to return if we don't have a proper block height here? Not familiar with the ord docs on thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's to match the current
ord
implementation. https://sourcegraph.com/search?q=context:global+repo:ordinals/ord+and+%7C%7C+%22