Skip to content

Commit

Permalink
fix: optimize block rollback logic for re-orgs (#279)
Browse files Browse the repository at this point in the history
* fix: reorgs

* fix: curse type

* fix: brc20 read from memory

* chore: delete old admin rpc

* fix: move chain_tip to a table

* chore: rename stuff to ordhook

* chore: more renaming
  • Loading branch information
rafaelcr authored Dec 5, 2023
1 parent 1209056 commit 293c323
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 419 deletions.
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 });
}
54 changes: 0 additions & 54 deletions src/admin-rpc/init.ts

This file was deleted.

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
7 changes: 4 additions & 3 deletions src/chainhook/server.ts → src/ordhook/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const ORDHOOK_BASE_PATH = `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CH
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 @@ -54,7 +54,8 @@ export async function startChainhookServer(args: { db: PgStore }): Promise<Chain
base_url: ORDHOOK_BASE_PATH,
};
const server = new ChainhookEventObserver(serverOpts, ordhookOpts);
await server.start(predicates, async (_uuid: string, payload: Payload) => {
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 293c323

Please sign in to comment.