Skip to content
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

fix: optimize inscription and brc-20 inserts #189

Merged
merged 13 commits into from
Aug 23, 2023
52 changes: 52 additions & 0 deletions src/admin-rpc/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Fastify, { FastifyPluginCallback } from 'fastify';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { PgStore } from '../pg/pg-store';
import { Server } from 'http';
import { Type } from '@sinclair/typebox';
import { PINO_LOGGER_CONFIG, logger } from '@hirosystems/api-toolkit';

export const AdminApi: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = (
fastify,
options,
done
) => {
fastify.post(
'/brc-20/scan',
{
schema: {
description: 'Scan for BRC-20 operations within a block range',
querystring: Type.Object({
start_block: Type.Integer(),
end_block: Type.Integer(),
}),
},
},
async (request, reply) => {
const startBlock = request.query.start_block;
const endBlock = request.query.end_block;
logger.info(
`AdminRPC scanning for BRC-20 operations from block ${startBlock} to block ${endBlock}`
);
// TODO: Provide a way to stop this scan without restarting.
fastify.db.brc20
.scanBlocks(startBlock, endBlock)
.then(() => logger.info(`AdminRPC finished scanning for BRC-20 operations`))
.catch(error => logger.error(error, `AdminRPC failed to scan for BRC-20`));
await reply.code(200).send();
}
);

done();
};

export async function buildAdminRpcServer(args: { db: PgStore }) {
const fastify = Fastify({
trustProxy: true,
logger: PINO_LOGGER_CONFIG,
}).withTypeProvider<TypeBoxTypeProvider>();

fastify.decorate('db', args.db);
await fastify.register(AdminApi, { prefix: '/ordinals/admin' });

return fastify;
}

Check warning on line 52 in src/admin-rpc/init.ts

View check run for this annotation

Codecov / codecov/patch

src/admin-rpc/init.ts#L2-L52

Added lines #L2 - L52 were not covered by tests
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const schema = Type.Object({
API_HOST: Type.String({ default: '0.0.0.0' }),
/** Port in which to serve the API */
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 */
EVENT_PORT: Type.Number({ default: 3099, minimum: 0, maximum: 65535 }),
/** Event server body limit (bytes) */
Expand Down Expand Up @@ -49,6 +51,9 @@ const schema = Type.Object({
PG_CONNECTION_POOL_MAX: Type.Number({ default: 10 }),
PG_IDLE_TIMEOUT: Type.Number({ default: 30 }),
PG_MAX_LIFETIME: Type.Number({ default: 60 }),

/** Enables BRC-20 processing in write mode APIs */
BRC20_BLOCK_SCAN_ENABLED: Type.Boolean({ default: true }),
});
type Env = Static<typeof schema>;

Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { ENV } from './env';
import { ApiMetrics } from './metrics/metrics';
import { PgStore } from './pg/pg-store';
import { buildAdminRpcServer } from './admin-rpc/init';

Check warning on line 8 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L8

Added line #L8 was not covered by tests

async function initBackgroundServices(db: PgStore) {
logger.info('Initializing background services...');
Expand All @@ -16,6 +17,16 @@
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 });

Check warning on line 29 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L20-L29

Added lines #L20 - L29 were not covered by tests
}

async function initApiService(db: PgStore) {
Expand Down
Loading
Loading