-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathIndexerNotifier.ts
More file actions
139 lines (123 loc) · 4.3 KB
/
IndexerNotifier.ts
File metadata and controls
139 lines (123 loc) · 4.3 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
BlockTriggerBase,
Sequencer,
sequencerModule,
SequencerModule,
TaskPayload,
TaskQueue,
SequencerIdProvider,
PrivateMempool,
SettlementModule,
} from "@proto-kit/sequencer";
import { log } from "@proto-kit/common";
import { inject } from "tsyringe";
import { IndexBlockTask } from "./tasks/IndexBlockTask";
import { IndexPendingTxTask } from "./tasks/IndexPendingTxTask";
import { IndexSettlementTask } from "./tasks/IndexSettlementTask";
import { IndexBatchTask } from "./tasks/IndexBatchTask";
export type NotifierMandatorySequencerModules = {
BlockTrigger: typeof BlockTriggerBase;
Mempool: typeof PrivateMempool;
SettlementModule: typeof SettlementModule;
};
@sequencerModule()
export class IndexerNotifier extends SequencerModule<Record<never, never>> {
public constructor(
@inject("Sequencer")
public sequencer: Sequencer<NotifierMandatorySequencerModules>,
@inject("TaskQueue")
public taskQueue: TaskQueue,
public indexBlockTask: IndexBlockTask,
public indexPendingTxTask: IndexPendingTxTask,
public indexBatchTask: IndexBatchTask,
public indexSettlementTask: IndexSettlementTask,
private readonly sequencerIdProvider: SequencerIdProvider
) {
super();
}
public async propagateEventsAsTasks() {
const queue = await this.taskQueue.getQueue(this.indexBlockTask.name);
const inputSerializer = this.indexBlockTask.inputSerializer();
const txInputSerializer = this.indexPendingTxTask.inputSerializer();
const batchInputSerializer = this.indexBatchTask.inputSerializer();
const settlementInputSerializer =
this.indexSettlementTask.inputSerializer();
this.sequencer.events.on("block-metadata-produced", async (block) => {
log.debug("Notifiying the indexer about block", block.block.height);
const payload = await inputSerializer.toJSON(block);
const sequencerId = this.sequencerIdProvider.getSequencerId();
const task: TaskPayload = {
name: this.indexBlockTask.name,
payload,
flowId: "", // empty for now
sequencerId,
};
await queue.addTask(task);
});
this.sequencer.events.on("mempool-transaction-added", async (tx) => {
try {
const txQueue = await this.taskQueue.getQueue(
this.indexPendingTxTask.name
);
// This part seems weird
const payload = await txInputSerializer.toJSON(tx.toJSON());
const sequencerId = this.sequencerIdProvider.getSequencerId();
const task: TaskPayload = {
name: this.indexPendingTxTask.name,
payload,
flowId: "",
sequencerId,
};
await txQueue.addTask(task);
} catch (err) {
console.error("Failed to add pending-tx task", err);
}
});
this.sequencer.events.on("batch-produced", async (batch) => {
log.debug("Notifiying the indexer about batch", batch?.height);
try {
const batchQueue = await this.taskQueue.getQueue(
this.indexBatchTask.name
);
const payload = await batchInputSerializer.toJSON(batch);
const sequencerId = this.sequencerIdProvider.getSequencerId();
const task: TaskPayload = {
name: this.indexBatchTask.name,
payload,
flowId: "",
sequencerId,
};
await batchQueue.addTask(task);
} catch (err) {
log.error(`Failed to index batch ${batch?.height} ${err}`);
}
});
this.sequencer.events.on("settlement-submitted", async (settlement) => {
log.debug(
"Notifiying the indexer about settlement",
settlement.transactionHash
);
try {
const settlementQueue = await this.taskQueue.getQueue(
this.indexSettlementTask.name
);
const payload = await settlementInputSerializer.toJSON(settlement);
const sequencerId = this.sequencerIdProvider.getSequencerId();
const task: TaskPayload = {
name: this.indexSettlementTask.name,
payload,
flowId: "",
sequencerId,
};
await settlementQueue.addTask(task);
} catch (err) {
log.error(
`Failed to add index settlement: ${settlement.transactionHash} ${err}`
);
}
});
}
public async start(): Promise<void> {
await this.propagateEventsAsTasks();
}
}