-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockProcessor.ts
209 lines (170 loc) · 5.8 KB
/
BlockProcessor.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { EventEmitter } from "events";
import { BlockCache } from "./BlockCache";
import { LightBlock } from "../../../../src/models/lightstreamer";
import {
addNotesToMerkleTree,
getNotesTreeSize,
revertToNoteSize,
} from "./MerkleTree";
import { AccountsManager } from "./AccountsManager";
import { Api } from "example/src/api/Api";
const POLL_INTERVAL = 30 * 1000;
export class BlockProcessor {
private api: Api<unknown>;
private pollInterval?: NodeJS.Timer;
private isProcessingBlocks: boolean = false;
private blockCache: BlockCache;
private accountsManager: AccountsManager;
private events: EventEmitter = new EventEmitter(); // Event emitter for block events
constructor(
api: Api<unknown>,
blockCache: BlockCache,
accountsManager: AccountsManager,
) {
this.api = api;
this.blockCache = blockCache;
this.accountsManager = accountsManager;
}
public async start() {
if (this.pollInterval !== undefined) {
console.warn("Process already running");
return;
}
await this._pollForNewBlocks();
this.events.emit("blocks-processed");
this.pollInterval = setInterval(
this._pollForNewBlocks.bind(this),
POLL_INTERVAL,
);
}
public stop() {
clearInterval(this.pollInterval);
}
public waitForProcessorSync(): Promise<void> {
console.log("Processor is currently syncing. Waiting for it to finish");
return new Promise((resolve) => {
console.log("Finished block syncing processor");
this.events.once("blocks-processed", resolve);
});
}
private async _pollForNewBlocks() {
if (this.isProcessingBlocks) {
return;
}
const latestBlock = await this._getLatestBlock();
const headSequence = latestBlock.sequence;
if (!headSequence) {
throw new Error("Head sequence is undefined");
}
const cachedHeadSequence = await this.blockCache.getHeadSequence();
if (cachedHeadSequence + 1 >= headSequence) {
return;
}
this.isProcessingBlocks = true;
const batchSize = process.env["BLOCK_PROCESSING_BATCH_SIZE"]
? parseInt(process.env["BLOCK_PROCESSING_BATCH_SIZE"])
: 99;
for (let i = cachedHeadSequence + 1; i < headSequence; i += batchSize + 1) {
await this._processBlockRange(i, Math.min(i + batchSize, headSequence));
}
return;
}
private async _getLatestBlock(): Promise<{
hash: string;
sequence: number;
}> {
const response = await this.api.latestBlock.getLatestBlock();
return response.data;
}
private async _getBlockBySequence(sequence: number): Promise<LightBlock> {
const response = await this.api.block.getBlock({ sequence });
return LightBlock.decode(Buffer.from(response.data, "hex"));
}
private async _processBlockRange(startSequence: number, endSequence: number) {
console.log(`Processing blocks from ${startSequence} to ${endSequence}`);
const response = await this.api.blockRange.getBlockRange({
start: startSequence,
end: endSequence,
});
try {
const blocks = response.data;
for (const block of blocks) {
try {
await this._processBlock(
LightBlock.decode(Buffer.from(block, "hex")),
);
} catch (err) {
console.error("Error processing block:", err);
throw err;
}
}
} catch (err) {
console.error(err);
}
console.log(
`Finished processing blocks from ${startSequence} to ${endSequence}`,
);
}
private async _processBlock(block: LightBlock) {
const hasReorg = await this._checkForReorg(block);
if (hasReorg) {
return;
}
const notes: Buffer[] = [];
for (const transaction of block.transactions) {
for (const output of transaction.outputs) {
notes.push(output.note);
}
}
const prevBlockNoteSize = block.noteSize - notes.length;
const notesTreeSize = await getNotesTreeSize();
if (notesTreeSize > prevBlockNoteSize) {
await revertToNoteSize(prevBlockNoteSize);
}
await addNotesToMerkleTree(notes);
await this.blockCache.cacheBlock(block);
}
private async _checkForReorg(block: LightBlock) {
// If we're not on block 2 or greater, reorgs are impossible.
if (!(block.sequence > 1)) {
return false;
}
const prevCachedBlock = await this.blockCache.getBlockBySequence(
block.sequence - 1,
);
// If the incoming block's previous block hash matches the previous block's hash,
// there is no reorg. Note that Buffer.compare returns 0 if the two buffers are equal.
if (block.previousBlockHash == prevCachedBlock.hash) {
return false;
}
// Otherwise, we have to walk back the chain until we find the last main chain block.
let lastMainChainBlock: LightBlock | null = null;
let currentSequence = block.sequence - 1;
while (!lastMainChainBlock) {
// Get block from server
const block = await this._getBlockBySequence(currentSequence);
// Get block from cache
const cachedBlock = await this.blockCache.getBlockBySequence(
currentSequence,
);
// If the two blocks' hash matches, we've found the last valid block.
if (block.hash === cachedBlock.hash) {
lastMainChainBlock = cachedBlock;
break;
}
currentSequence -= 1;
// If we've reached the genesis block without finding the last valid block,
// something is seriously wrong.
if (currentSequence === 1) {
throw new Error("Reached genesis block without finding a valid chain");
}
}
const invalidBlocks = await this.blockCache.getBlockRange(
lastMainChainBlock.sequence + 1,
);
await this.accountsManager.handleReorg(invalidBlocks);
await this.blockCache.handleReorg(lastMainChainBlock);
await revertToNoteSize(lastMainChainBlock.noteSize);
return true;
}
}