Skip to content

Commit b67dc8c

Browse files
authored
fix: upgrade to new chainhook ts client (#280)
1 parent 638e7dd commit b67dc8c

File tree

4 files changed

+15
-151
lines changed

4 files changed

+15
-151
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@fastify/type-provider-typebox": "^3.2.0",
5757
"@google-cloud/storage": "^7.12.1",
5858
"@hirosystems/api-toolkit": "^1.7.1",
59-
"@hirosystems/chainhook-client": "^1.12.0",
59+
"@hirosystems/chainhook-client": "^2.0.0",
6060
"@sinclair/typebox": "^0.28.17",
6161
"@stacks/transactions": "^6.1.0",
6262
"@types/node": "^20.16.1",

src/chainhook/server.ts

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
1-
import * as fs from 'fs';
21
import {
32
ChainhookEventObserver,
43
ChainhookNodeOptions,
4+
EventObserverOptions,
5+
EventObserverPredicate,
56
Payload,
6-
ServerOptions,
7-
ServerPredicate,
87
StacksPayload,
98
} from '@hirosystems/chainhook-client';
109
import { PgStore } from '../pg/pg-store';
1110
import { ENV } from '../env';
1211
import { logger } from '@hirosystems/api-toolkit';
13-
import { randomUUID } from 'node:crypto';
14-
15-
export function getPersistedPredicateFromDisk(): ServerPredicate | undefined {
16-
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
17-
try {
18-
if (!fs.existsSync(predicatePath)) {
19-
return;
20-
}
21-
const fileData = fs.readFileSync(predicatePath, 'utf-8');
22-
return JSON.parse(fileData) as ServerPredicate;
23-
} catch (error) {
24-
logger.error(error, `ChainhookServer unable to get persisted predicate`);
25-
}
26-
}
27-
28-
export function persistPredicateToDisk(predicate: ServerPredicate) {
29-
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
30-
try {
31-
fs.mkdirSync(ENV.CHAINHOOK_PREDICATE_PATH, { recursive: true });
32-
fs.writeFileSync(predicatePath, JSON.stringify(predicate, null, 2));
33-
} catch (error) {
34-
logger.error(error, `ChainhookServer unable to persist predicate to disk`);
35-
}
36-
}
3712

3813
export async function startChainhookServer(args: { db: PgStore }): Promise<ChainhookEventObserver> {
3914
const blockHeight = await args.db.getChainTipBlockHeight();
4015
logger.info(`ChainhookServer is at block ${blockHeight}`);
4116

42-
const predicates: ServerPredicate[] = [];
17+
const predicates: EventObserverPredicate[] = [];
4318
if (ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION) {
44-
const existingPredicate = getPersistedPredicateFromDisk();
45-
if (existingPredicate) {
46-
logger.info(
47-
`ChainhookServer will attempt to resume existing predicate ${existingPredicate.uuid}`
48-
);
49-
}
5019
const header = {
51-
uuid: existingPredicate?.uuid ?? randomUUID(),
52-
name: 'block',
20+
name: 'metadata-api-blocks',
5321
version: 1,
5422
chain: 'stacks',
5523
};
@@ -87,38 +55,33 @@ export async function startChainhookServer(args: { db: PgStore }): Promise<Chain
8755
}
8856
}
8957

90-
const opts: ServerOptions = {
58+
const observer: EventObserverOptions = {
9159
hostname: ENV.API_HOST,
9260
port: ENV.EVENT_PORT,
9361
auth_token: ENV.CHAINHOOK_NODE_AUTH_TOKEN,
9462
external_base_url: `http://${ENV.EXTERNAL_HOSTNAME}`,
9563
wait_for_chainhook_node: ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION,
9664
validate_chainhook_payloads: false,
9765
body_limit: ENV.EVENT_SERVER_BODY_LIMIT,
66+
predicate_disk_file_path: ENV.CHAINHOOK_PREDICATE_PATH,
67+
predicate_health_check_interval_ms: 300_000,
9868
node_type: 'chainhook',
9969
};
10070
const chainhook: ChainhookNodeOptions = {
10171
base_url: `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`,
10272
};
103-
const server = new ChainhookEventObserver(opts, chainhook);
104-
await server.start(predicates, async (uuid: string, payload: Payload) => {
73+
const server = new ChainhookEventObserver(observer, chainhook);
74+
await server.start(predicates, async (payload: Payload) => {
10575
logger.info(
10676
`ChainhookServer received ${
10777
payload.chainhook.is_streaming_blocks ? 'streamed' : 'replay'
108-
} payload from predicate ${uuid}`
78+
} payload from predicate ${payload.chainhook.uuid}`
10979
);
11080
await args.db.chainhook.processPayload(payload as StacksPayload);
11181
});
112-
if (predicates.length) persistPredicateToDisk(predicates[0]);
11382
return server;
11483
}
11584

11685
export async function closeChainhookServer(server: ChainhookEventObserver) {
117-
try {
118-
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
119-
if (fs.existsSync(predicatePath)) fs.rmSync(predicatePath);
120-
} catch (error) {
121-
logger.error(error, `ChainhookServer unable to delete persisted predicate`);
122-
}
12386
await server.close();
12487
}

tests/chainhook/predicates.test.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)