generated from hirosystems/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded-parser-worker.ts
More file actions
92 lines (82 loc) · 2.68 KB
/
threaded-parser-worker.ts
File metadata and controls
92 lines (82 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as WorkerThreads from 'node:worker_threads';
import { CoreNodeNakamotoBlockMessage, StackerDbChunk } from './core-node-message';
import {
ParsedNakamotoBlock,
ParsedStackerDbChunk,
parseNakamotoBlockMsg,
parseStackerDbChunk,
} from './msg-parsing';
export const workerFile = __filename;
export enum ThreadedParserMsgType {
NakamotoBlock = 'NakamotoBlock',
StackerDbChunk = 'StackerDbChunk',
}
interface ThreadMsg {
type: ThreadedParserMsgType;
msgId: number;
}
export interface NakamotoBlockMsgRequest extends ThreadMsg {
type: ThreadedParserMsgType.NakamotoBlock;
msgId: number;
block: CoreNodeNakamotoBlockMessage;
}
export interface NakamotoBlockMsgReply extends ThreadMsg {
type: ThreadedParserMsgType.NakamotoBlock;
msgId: number;
block: ParsedNakamotoBlock;
}
export interface StackerDbChunkMsgRequest extends ThreadMsg {
type: ThreadedParserMsgType.StackerDbChunk;
msgId: number;
chunk: StackerDbChunk;
}
export interface StackerDbChunkMsgReply extends ThreadMsg {
type: ThreadedParserMsgType.StackerDbChunk;
msgId: number;
chunk: ParsedStackerDbChunk[];
}
export type ThreadedParserMsgRequest = NakamotoBlockMsgRequest | StackerDbChunkMsgRequest;
export type ThreadedParserMsgReply = NakamotoBlockMsgReply | StackerDbChunkMsgReply;
if (!WorkerThreads.isMainThread) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const mainThreadPort = WorkerThreads.parentPort!;
mainThreadPort.on('messageerror', err => {
console.error(`Worker thread message error`, err);
});
mainThreadPort.on('message', (msg: ThreadedParserMsgRequest) => {
try {
handleWorkerMsg(msg);
} catch (err) {
console.error(`Failed to parse message: ${JSON.stringify(msg)}`);
console.error(`Error handling message from main thread`, err);
}
});
}
function handleWorkerMsg(msg: ThreadedParserMsgRequest) {
let reply: ThreadedParserMsgReply;
switch (msg.type) {
case ThreadedParserMsgType.NakamotoBlock: {
reply = {
type: ThreadedParserMsgType.NakamotoBlock,
msgId: msg.msgId,
block: parseNakamotoBlockMsg(msg.block),
} satisfies NakamotoBlockMsgReply;
break;
}
case ThreadedParserMsgType.StackerDbChunk: {
reply = {
type: ThreadedParserMsgType.StackerDbChunk,
msgId: msg.msgId,
chunk: parseStackerDbChunk(msg.chunk),
} satisfies StackerDbChunkMsgReply;
break;
}
default: {
const _exhaustiveCheck: never = msg;
throw new Error(`Unhandled message type: ${msg}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const mainThreadPort = WorkerThreads.parentPort!;
mainThreadPort.postMessage(reply);
}