Skip to content

Commit

Permalink
feat: resume indexing from last observed block height
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Mar 6, 2023
1 parent 34721b0 commit fc4a549
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { randomUUID } from 'crypto';
import Fastify, { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
import Fastify, {
FastifyInstance,
FastifyPluginCallback,
FastifyReply,
FastifyRequest,
} from 'fastify';
import { Server } from 'http';
import { request } from 'undici';
import { ENV } from '../env';
Expand All @@ -12,7 +17,10 @@ import { processInscriptionRevealed, processInscriptionTransferred } from './hel
export const CHAINHOOK_BASE_PATH = `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`;
export const REVEAL__PREDICATE_UUID = randomUUID();

async function waitForChainhookNode() {
/**
* Ping the chainhooks node indefinitely until it's ready.
*/
async function waitForChainhookNode(this: FastifyInstance) {
logger.info(`EventServer connecting to chainhook node...`);
while (true) {
try {
Expand All @@ -29,15 +37,19 @@ async function waitForChainhookNode() {
* Register required ordinals predicates in the chainhooks node. This is executed before starting
* the events server.
*/
async function registerChainhookPredicates() {
logger.info(`EventServer registering predicates...`);
async function registerChainhookPredicates(this: FastifyInstance) {
const lastObservedBlockHeight = await this.db.getChainTipBlockHeight();
logger.info(
`EventServer registering predicates starting from block ${lastObservedBlockHeight}...`
);
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks`, {
method: 'POST',
body: JSON.stringify({
chain: 'bitcoin',
uuid: REVEAL__PREDICATE_UUID,
name: 'inscription_revealed',
version: 1,
start_block: lastObservedBlockHeight,
networks: {
mainnet: {
if_this: {
Expand All @@ -63,7 +75,7 @@ async function registerChainhookPredicates() {
/**
* Remove previously registered predicates. This is executed before closing the events server.
*/
async function removeChainhookPredicates() {
async function removeChainhookPredicates(this: FastifyInstance) {
logger.info(`EventServer closing predicates...`);
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks/bitcoin/${REVEAL__PREDICATE_UUID}`, {
method: 'DELETE',
Expand Down

0 comments on commit fc4a549

Please sign in to comment.