Skip to content

Commit

Permalink
Revert "rename finalBlocksOnly"
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere authored Feb 21, 2024
1 parent 26a4a4e commit eb4dad8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
8 changes: 3 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import { banner } from "./src/banner.js";
import { toJSON, toText } from "./src/http.js";
import { ping } from "./src/ping.js";
import { keyPair, parsePrivateKey } from "./src/auth.js";
import { Metadata, boolean } from "./src/schemas.js";

export async function action(options: WebhookRunOptions) {
const cursor = fileCursor.readCursor(options.cursorPath);
const finalBlocksOnly = boolean.parse(options.finalBlocksOnly);

// Block Emitter
const { emitter, moduleHash } = await setup({...options, cursor});
Expand All @@ -36,7 +34,7 @@ export async function action(options: WebhookRunOptions) {
emitter.on("output", async (data, cursor, clock) => {
if (!clock.timestamp) return;
const chain = new URL(options.substreamsEndpoint).hostname.split(".")[0];
const metadata: Metadata = {
const metadata = {
status: 200,
cursor,
session: {
Expand All @@ -51,7 +49,7 @@ export async function action(options: WebhookRunOptions) {
manifest: {
substreamsEndpoint: options.substreamsEndpoint,
chain,
finalBlocksOnly,
finalBlockOnly: options.finalBlocksOnly,
moduleName: options.moduleName,
type: data.getType().typeName,
moduleHash,
Expand All @@ -62,7 +60,7 @@ export async function action(options: WebhookRunOptions) {

// Queue POST
queue.add(async () => {
await postWebhook(options.webhookUrl, body, metadata, privateKey, options);
await postWebhook(options.webhookUrl, body, privateKey, options);
fs.writeFileSync(options.cursorPath, cursor);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.9.2",
"version": "0.9.1",
"name": "substreams-sink-webhook",
"description": "Substreams Sink Webhook",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions src/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function ping(url: string, privateKey?: Hex) {

// send valid signature (must respond with 200)
try {
await postWebhook(url, body, undefined, privateKey, { maximumAttempts: 0 });
await postWebhook(url, body, privateKey, { maximumAttempts: 0 });
} catch (_e) {
return false;
}
Expand All @@ -20,7 +20,7 @@ export async function ping(url: string, privateKey?: Hex) {

// send invalid signature (must NOT respond with 200)
try {
await postWebhook(url, body, undefined, invalidprivateKey, { maximumAttempts: 0 });
await postWebhook(url, body, invalidprivateKey, { maximumAttempts: 0 });
return false;
} catch (_e) {
return true;
Expand Down
10 changes: 4 additions & 6 deletions src/postWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Hex } from "@noble/curves/abstract/utils";
import { logger } from "substreams-sink";
import { createTimestamp, sign } from "./auth.js";
import logUpdate from "log-update";
import { Metadata } from "./schemas.js";

function awaitSetTimeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand All @@ -22,17 +21,16 @@ let start = now();
// let lastUpdate = now();

// TO-DO replace with Prometheus metrics
function logProgress(metadata?: Metadata) {
if ( !metadata ) return;
function logProgress() {
const delta = now() - start;
const rate = Math.round(blocks / delta);
const minutes = Math.round(delta / 60);
const seconds = delta % 60;
if ( blocks ) logUpdate(`[app] timestamp=${metadata.clock.timestamp} block_number=${metadata.clock.number} blocks=${blocks} [${rate} b/s] (${minutes}m ${seconds}s)`);
if ( blocks ) logUpdate(`[app] blocks=${blocks} [${rate} b/s] (${minutes}m ${seconds}s)`);
blocks++;
}

export async function postWebhook(url: string, body: string, metadata?: Metadata, secretKey?: Hex, options: PostWebhookOptions = {}) {
export async function postWebhook(url: string, body: string, secretKey?: Hex, options: PostWebhookOptions = {}) {
// Retry Policy
const initialInterval = 1000; // 1s
const maximumAttempts = options.maximumAttempts ?? 100 * initialInterval;
Expand Down Expand Up @@ -83,7 +81,7 @@ export async function postWebhook(url: string, body: string, metadata?: Metadata
continue;
}
// success
logProgress(metadata);
logProgress();
return { url, status };
} catch (e: any) {
const error = e.cause;
Expand Down
22 changes: 5 additions & 17 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,17 @@ export const ManifestSchema = z.object({
type: z.string(),
moduleHash: z.string(),
chain: z.string(),
finalBlocksOnly: boolean,
finalBlockOnly: boolean,
});
export type Manifest = z.infer<typeof ManifestSchema>;

export const SessionSchema = z.object({
traceId: z.string(),
resolvedStartBlock: z.number(),
});
export type Session = z.infer<typeof SessionSchema>;

export const MetadataSchema = z.object({
status: z.number(),
cursor: z.string(),
session: SessionSchema,
clock: ClockSchema,
manifest: ManifestSchema,
});
export type Metadata = z.infer<typeof MetadataSchema>;

export const makePayloadBody = <S extends z.Schema>(dataSchema: S) =>
z.object({
cursor: z.string(),
session: SessionSchema,
session: z.object({
traceId: z.string(),
resolvedStartBlock: z.number(),
}),
clock: ClockSchema,
manifest: ManifestSchema,
data: dataSchema,
Expand Down

0 comments on commit eb4dad8

Please sign in to comment.