Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 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 @@ -56,7 +56,7 @@
"@fastify/type-provider-typebox": "^3.2.0",
"@google-cloud/storage": "^7.12.1",
"@hirosystems/api-toolkit": "^1.7.1",
"@hirosystems/chainhook-client": "^1.12.0",
"@hirosystems/chainhook-client": "^2.0.0",
"@sinclair/typebox": "^0.28.17",
"@stacks/transactions": "^6.1.0",
"@types/node": "^20.16.1",
Expand Down
57 changes: 10 additions & 47 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@
import * as fs from 'fs';
import {
ChainhookEventObserver,
ChainhookNodeOptions,
EventObserverOptions,
EventObserverPredicate,

Check warning on line 5 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L4-L5

Added lines #L4 - L5 were not covered by tests
Payload,
ServerOptions,
ServerPredicate,
StacksPayload,
} from '@hirosystems/chainhook-client';
import { PgStore } from '../pg/pg-store';
import { ENV } from '../env';
import { logger } from '@hirosystems/api-toolkit';
import { randomUUID } from 'node:crypto';

export function getPersistedPredicateFromDisk(): ServerPredicate | undefined {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
try {
if (!fs.existsSync(predicatePath)) {
return;
}
const fileData = fs.readFileSync(predicatePath, 'utf-8');
return JSON.parse(fileData) as ServerPredicate;
} catch (error) {
logger.error(error, `ChainhookServer unable to get persisted predicate`);
}
}

export function persistPredicateToDisk(predicate: ServerPredicate) {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
try {
fs.mkdirSync(ENV.CHAINHOOK_PREDICATE_PATH, { recursive: true });
fs.writeFileSync(predicatePath, JSON.stringify(predicate, null, 2));
} catch (error) {
logger.error(error, `ChainhookServer unable to persist predicate to disk`);
}
}

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

const predicates: ServerPredicate[] = [];
const predicates: EventObserverPredicate[] = [];

Check warning on line 17 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L17

Added line #L17 was not covered by tests
if (ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION) {
const existingPredicate = getPersistedPredicateFromDisk();
if (existingPredicate) {
logger.info(
`ChainhookServer will attempt to resume existing predicate ${existingPredicate.uuid}`
);
}
const header = {
uuid: existingPredicate?.uuid ?? randomUUID(),
name: 'block',
name: 'metadata-api-blocks',

Check warning on line 20 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L20

Added line #L20 was not covered by tests
version: 1,
chain: 'stacks',
};
Expand Down Expand Up @@ -87,38 +55,33 @@
}
}

const opts: ServerOptions = {
const observer: EventObserverOptions = {

Check warning on line 58 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L58

Added line #L58 was not covered by tests
hostname: ENV.API_HOST,
port: ENV.EVENT_PORT,
auth_token: ENV.CHAINHOOK_NODE_AUTH_TOKEN,
external_base_url: `http://${ENV.EXTERNAL_HOSTNAME}`,
wait_for_chainhook_node: ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION,
validate_chainhook_payloads: false,
body_limit: ENV.EVENT_SERVER_BODY_LIMIT,
predicate_disk_file_path: ENV.CHAINHOOK_PREDICATE_PATH,
predicate_health_check_interval_ms: 300_000,

Check warning on line 67 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L66-L67

Added lines #L66 - L67 were not covered by tests
node_type: 'chainhook',
};
const chainhook: ChainhookNodeOptions = {
base_url: `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`,
};
const server = new ChainhookEventObserver(opts, chainhook);
await server.start(predicates, async (uuid: string, payload: Payload) => {
const server = new ChainhookEventObserver(observer, chainhook);
await server.start(predicates, async (payload: Payload) => {

Check warning on line 74 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L73-L74

Added lines #L73 - L74 were not covered by tests
logger.info(
`ChainhookServer received ${
payload.chainhook.is_streaming_blocks ? 'streamed' : 'replay'
} payload from predicate ${uuid}`
} payload from predicate ${payload.chainhook.uuid}`

Check warning on line 78 in src/chainhook/server.ts

View check run for this annotation

Codecov / codecov/patch

src/chainhook/server.ts#L78

Added line #L78 was not covered by tests
);
await args.db.chainhook.processPayload(payload as StacksPayload);
});
if (predicates.length) persistPredicateToDisk(predicates[0]);
return server;
}

export async function closeChainhookServer(server: ChainhookEventObserver) {
try {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
if (fs.existsSync(predicatePath)) fs.rmSync(predicatePath);
} catch (error) {
logger.error(error, `ChainhookServer unable to delete persisted predicate`);
}
await server.close();
}
99 changes: 0 additions & 99 deletions tests/chainhook/predicates.test.ts

This file was deleted.

Loading