Skip to content

Commit

Permalink
feat: start validating with chainhook
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jul 18, 2024
1 parent e126511 commit 3832b40
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
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 @@ -55,7 +55,7 @@
"@fastify/swagger": "^7.6.1",
"@fastify/type-provider-typebox": "^3.2.0",
"@hirosystems/api-toolkit": "^1.6.2",
"@hirosystems/chainhook-client": "^1.10.0",
"@hirosystems/chainhook-client": "^1.11.0",
"@sinclair/typebox": "^0.28.17",
"@stacks/transactions": "^6.1.0",
"@types/node": "^18.11.9",
Expand Down
21 changes: 13 additions & 8 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,41 @@ import { logger } from '@hirosystems/api-toolkit';
const PREDICATE_UUID = randomUUID();

export async function startChainhookServer(args: { db: PgStore }): Promise<ChainhookEventObserver> {
const blockHeight = await args.db.getChainTipBlockHeight();
const predicates: ServerPredicate[] = [
{
const predicates: ServerPredicate[] = [];
if (ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION) {
const blockHeight = await args.db.getChainTipBlockHeight();
logger.info(`Chainhook predicate starting from block ${blockHeight}...`);
predicates.push({
uuid: PREDICATE_UUID,
name: 'block',
version: 1,
chain: 'stacks',
networks: {
mainnet: {
start_block: blockHeight,
include_contract_abi: true,
if_this: {
scope: 'block_height',
higher_than: 0,
higher_than: 1,
},
},
},
},
];
});
}

const opts: ServerOptions = {
hostname: ENV.API_HOST,
port: ENV.EVENT_PORT,
auth_token: ENV.CHAINHOOK_NODE_AUTH_TOKEN,
external_base_url: `http://${ENV.EXTERNAL_HOSTNAME}`,
validate_chainhook_payloads: true,
wait_for_chainhook_node: ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION,
validate_chainhook_payloads: false,
body_limit: ENV.EVENT_SERVER_BODY_LIMIT,
node_type: 'chainhook',
};
const chainhook: ChainhookNodeOptions = {
base_url: `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`,
};
logger.info(`ChainhookServer listening for Stacks blocks starting from block ${blockHeight}`);
const server = new ChainhookEventObserver(opts, chainhook);
await server.start(predicates, async (uuid: string, payload: Payload) => {
logger.info(
Expand All @@ -55,5 +58,7 @@ export async function startChainhookServer(args: { db: PgStore }): Promise<Chain
);
await args.db.chainhook.processPayload(payload as StacksPayload);
});
const chainTip = await args.db.getChainTipBlockHeight();
logger.info(`ChainhookServer chain tip is at ${chainTip}`);
return server;
}
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const schema = Type.Object({
* coming from the valid instance
*/
CHAINHOOK_NODE_AUTH_TOKEN: Type.String(),
/**
* Register chainhook predicates automatically when the API is first launched. Set this to `false`
* if you're configuring your predicates manually.
*/
CHAINHOOK_AUTO_PREDICATE_REGISTRATION: Type.Boolean({ default: true }),

PGHOST: Type.String(),
PGPORT: Type.Number({ default: 5432, minimum: 0, maximum: 65535 }),
Expand Down
4 changes: 3 additions & 1 deletion src/pg/chainhook/chainhook-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ export class ChainhookPgStore extends BasePgStoreModule {
);
} catch (error) {
if (error instanceof ContractNotFoundError)
logger.warn(error, `ChainhookPgStore found NFT mint for nonexisting contract`);
logger.warn(
`ChainhookPgStore found NFT mint for nonexisting contract ${mint.event.contractId}`
);
else throw error;
}
}
Expand Down

0 comments on commit 3832b40

Please sign in to comment.