Skip to content

Commit

Permalink
Merge pull request #280 from hirosystems/beta
Browse files Browse the repository at this point in the history
release v2.0.1
  • Loading branch information
rafaelcr authored Dec 5, 2023
2 parents 0ba671b + 807c27a commit dfaa933
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 505 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## [2.0.1-beta.4](https://github.com/hirosystems/ordinals-api/compare/v2.0.1-beta.3...v2.0.1-beta.4) (2023-12-05)


### Bug Fixes

* optimize block rollback logic for re-orgs ([#279](https://github.com/hirosystems/ordinals-api/issues/279)) ([293c323](https://github.com/hirosystems/ordinals-api/commit/293c32391d5e71a3d6570526be3565a4e75572b7))

## [2.0.1-beta.3](https://github.com/hirosystems/ordinals-api/compare/v2.0.1-beta.2...v2.0.1-beta.3) (2023-11-29)


### Bug Fixes

* upgrade chainhook client to v1.4.2 ([#277](https://github.com/hirosystems/ordinals-api/issues/277)) ([67ba3d4](https://github.com/hirosystems/ordinals-api/commit/67ba3d4f48e6ce8e955c08fd75cbf009786c0160))

## [2.0.1-beta.2](https://github.com/hirosystems/ordinals-api/compare/v2.0.1-beta.1...v2.0.1-beta.2) (2023-11-23)


### Bug Fixes

* select only the first inscription id match ([106368e](https://github.com/hirosystems/ordinals-api/commit/106368e9fa415db0657988de28a541906f74d28a))

## [2.0.1-beta.1](https://github.com/hirosystems/ordinals-api/compare/v2.0.0...v2.0.1-beta.1) (2023-11-23)


### Bug Fixes

* add ENV to toggle inscription gap detection ([56ab283](https://github.com/hirosystems/ordinals-api/commit/56ab283cdfaf95efe058e5f057fb5559b9dfede0))

## [2.0.0](https://github.com/hirosystems/ordinals-api/compare/v1.2.6...v2.0.0) (2023-11-21)


Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ information.
The Ordinals API is a microservice that has hard dependencies on other systems.
Before you start, you'll need to have access to:

1. A [Chainhook node](https://github.com/hirosystems/chainhook) with a fully
indexed Ordinals `.redb` database.
1. An [Ordhook node](https://github.com/hirosystems/ordhook) with a fully
indexed Ordinals database.
1. A local writeable Postgres database for data storage

### Running the API

Clone the repo.

Create an `.env` file and specify the appropriate values to configure the local
API server, postgres DB and Chainhook node reachability. See
API server, postgres DB and Ordhook node reachability. See
[`env.ts`](https://github.com/hirosystems/ordinals-api/blob/develop/src/env.ts)
for all available configuration options.

Expand Down
38 changes: 38 additions & 0 deletions migrations/1701486147464_chain-tip-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.dropMaterializedView('chain_tip');
pgm.createTable('chain_tip', {
id: {
type: 'bool',
primaryKey: true,
default: true,
},
block_height: {
type: 'bigint',
notNull: true,
// Set block height 767430 (inscription #0 genesis) as default.
default: 767430,
},
});
pgm.addConstraint('chain_tip', 'chain_tip_one_row', 'CHECK(id)');
pgm.sql(`
INSERT INTO chain_tip (block_height) (
SELECT GREATEST(MAX(block_height), 767430) AS block_height FROM locations
)
`);
}

export function down(pgm: MigrationBuilder): void {
pgm.dropTable('chain_tip');
pgm.createMaterializedView(
'chain_tip',
{ data: true },
// Set block height 767430 (inscription #0 genesis) as default.
`SELECT GREATEST(MAX(block_height), 767430) AS block_height FROM locations`
);
pgm.createIndex('chain_tip', ['block_height'], { unique: true });
}
43 changes: 36 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@fastify/swagger": "^8.3.1",
"@fastify/type-provider-typebox": "^3.2.0",
"@hirosystems/api-toolkit": "^1.3.1",
"@hirosystems/chainhook-client": "^1.4.1",
"@hirosystems/chainhook-client": "^1.4.2",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^10.0.4",
"@semantic-release/git": "^10.0.1",
Expand Down
54 changes: 0 additions & 54 deletions src/admin-rpc/init.ts

This file was deleted.

18 changes: 10 additions & 8 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const schema = Type.Object({
/**
* Run mode for this service. Allows you to control how the API runs, typically in an auto-scaled
* environment. Available values are:
* * `default`: Runs the chainhook server and the REST API server (this is the default)
* * `writeonly`: Runs only the chainhook server
* * `default`: Runs the ordhook server and the REST API server (this is the default)
* * `writeonly`: Runs only the ordhook server
* * `readonly`: Runs only the REST API server
*/
RUN_MODE: Type.Enum(
Expand All @@ -20,24 +20,24 @@ const schema = Type.Object({
API_PORT: Type.Number({ default: 3000, minimum: 0, maximum: 65535 }),
/** Port in which to serve the Admin RPC interface */
ADMIN_RPC_PORT: Type.Number({ default: 3001, minimum: 0, maximum: 65535 }),
/** Port in which to receive chainhook events */
/** Port in which to receive ordhook events */
EVENT_PORT: Type.Number({ default: 3099, minimum: 0, maximum: 65535 }),
/** Event server body limit (bytes) */
EVENT_SERVER_BODY_LIMIT: Type.Integer({ default: 20971520 }),
/** Hostname that will be reported to the chainhook node so it can call us back with events */
/** Hostname that will be reported to the ordhook node so it can call us back with events */
EXTERNAL_HOSTNAME: Type.String({ default: '127.0.0.1' }),

/** Hostname of the chainhook node we'll use to register predicates */
/** Hostname of the ordhook node we'll use to register predicates */
CHAINHOOK_NODE_RPC_HOST: Type.String({ default: '127.0.0.1' }),
/** Control port of the chainhook node */
/** Control port of the ordhook node */
CHAINHOOK_NODE_RPC_PORT: Type.Number({ default: 20456, minimum: 0, maximum: 65535 }),
/**
* Authorization token that the chainhook node must send with every event to make sure it's
* Authorization token that the ordhook node must send with every event to make sure it's
* coming from the valid instance
*/
CHAINHOOK_NODE_AUTH_TOKEN: Type.String(),
/**
* Register chainhook predicates automatically when the API is first launched. Set this to `false`
* Register ordhook predicates automatically when the API is first launched. Set this to `false`
* if you're configuring your predicates manually for any reason.
*/
CHAINHOOK_AUTO_PREDICATE_REGISTRATION: Type.Boolean({ default: true }),
Expand All @@ -55,6 +55,8 @@ const schema = Type.Object({

/** Enables BRC-20 processing in write mode APIs */
BRC20_BLOCK_SCAN_ENABLED: Type.Boolean({ default: true }),
/** Enables inscription gap detection to prevent ingesting unordered blocks */
INSCRIPTION_GAP_DETECTION_ENABLED: Type.Boolean({ default: true }),
});
type Env = Static<typeof schema>;

Expand Down
17 changes: 3 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import { isProdEnv, logger, registerShutdownConfig } from '@hirosystems/api-toolkit';
import { buildApiServer, buildPromServer } from './api/init';
import { startChainhookServer } from './chainhook/server';
import { startOrdhookServer } from './ordhook/server';
import { ENV } from './env';
import { ApiMetrics } from './metrics/metrics';
import { PgStore } from './pg/pg-store';
import { buildAdminRpcServer } from './admin-rpc/init';

async function initBackgroundServices(db: PgStore) {
logger.info('Initializing background services...');
const server = await startChainhookServer({ db });
const server = await startOrdhookServer({ db });
registerShutdownConfig({
name: 'Chainhook Server',
name: 'Ordhook Server',
forceKillable: false,
handler: async () => {
await server.close();
},
});

const adminRpcServer = await buildAdminRpcServer({ db });
registerShutdownConfig({
name: 'Admin RPC Server',
forceKillable: false,
handler: async () => {
await adminRpcServer.close();
},
});
await adminRpcServer.listen({ host: ENV.API_HOST, port: ENV.ADMIN_RPC_PORT });
}

async function initApiService(db: PgStore) {
Expand Down
16 changes: 9 additions & 7 deletions src/chainhook/server.ts → src/ordhook/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
} from '@hirosystems/chainhook-client';
import { logger } from '@hirosystems/api-toolkit';

export const CHAINHOOK_BASE_PATH = `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`;
export const ORDHOOK_BASE_PATH = `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`;
export const PREDICATE_UUID = randomUUID();

/**
* Starts the chainhooks event server.
* Starts the Ordhook event observer.
* @param args - DB
* @returns ChainhookEventObserver instance
*/
export async function startChainhookServer(args: { db: PgStore }): Promise<ChainhookEventObserver> {
export async function startOrdhookServer(args: { db: PgStore }): Promise<ChainhookEventObserver> {
const predicates: ServerPredicate[] = [];
if (ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION) {
const blockHeight = await args.db.getChainTipBlockHeight();
Expand Down Expand Up @@ -48,12 +48,14 @@ export async function startChainhookServer(args: { db: PgStore }): Promise<Chain
wait_for_chainhook_node: ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION,
validate_chainhook_payloads: true,
body_limit: ENV.EVENT_SERVER_BODY_LIMIT,
node_type: 'ordhook',
};
const chainhookOpts: ChainhookNodeOptions = {
base_url: CHAINHOOK_BASE_PATH,
const ordhookOpts: ChainhookNodeOptions = {
base_url: ORDHOOK_BASE_PATH,
};
const server = new ChainhookEventObserver(serverOpts, chainhookOpts);
await server.start(predicates, async (_uuid: string, payload: Payload) => {
const server = new ChainhookEventObserver(serverOpts, ordhookOpts);
await server.start(predicates, async (uuid: string, payload: Payload) => {
logger.info(`OrdhookServer received payload from predicate ${uuid}`);
await args.db.updateInscriptions(payload);
});
return server;
Expand Down
Loading

0 comments on commit dfaa933

Please sign in to comment.