-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.js
321 lines (286 loc) · 10.2 KB
/
lib.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { ApiPromise, WsProvider } from "@polkadot/api";
import { MongoClient } from "mongodb";
import * as dotenv from "dotenv";
dotenv.config();
const config =
process.env.DB_USE_SSL === "true"
? {
ssl: true,
sslValidate: true,
}
: {};
const dbClient = new MongoClient(process.env.DB_URL, config);
export const db = dbClient.db(process.env.DB_NAME);
export const RPC_NODE = process.env.RPC_NODE;
export const NUM_CONCURRENT_JOBS = parseInt(process.env.NUM_CONCURRENT_JOBS);
export const START_BLOCK = parseInt(process.env.START_BLOCK || 1);
export async function getLastProcessedBlockNumber() {
try {
return (
await db.collection("blocks").findOne({}, { sort: { height: -1 } })
).height;
} catch {
return START_BLOCK;
}
}
async function insertIntoCollection(collection, document) {
try {
await db.collection(collection).insertOne(document);
} catch (e) {
if (e.message.includes("E11000 duplicate key error")) {
console.log(
`Skippping dup key ${document._id} in collection ${collection}`
);
return;
}
throw e;
}
}
function mapTypes(obj) {
if (!isNaN(obj)) return Number(obj);
return obj;
}
async function parseBlock(
blockNumber,
api = null,
swallowNonExistingBlocks = false
) {
try {
if (!api) {
const wsProvider = new WsProvider(RPC_NODE);
api = await ApiPromise.create({
provider: wsProvider,
});
}
let signedBlock;
let blockHash;
try {
blockHash = await api.rpc.chain.getBlockHash(blockNumber);
signedBlock = await api.rpc.chain.getBlock(blockHash);
} catch (e) {
if (
e.message.includes(
"Unable to retrieve header and parent from supplied hash"
)
) {
console.log(
`Block ${blockNumber} is not yet avaiable, skipping.`
);
if (swallowNonExistingBlocks) return;
throw e;
}
}
const apiAt = await api.at(signedBlock.block.header.hash);
const allRecords = await apiAt.query.system.events();
const block = {
_id: blockHash.toHuman(),
height: blockNumber,
timestamp: null,
author: await getBlockAuthor(apiAt, blockNumber),
};
signedBlock.block.extrinsics.forEach(async (ex, extrinsicIndex) => {
let extrinsic = ex.toHuman();
extrinsic.success = false;
extrinsic.blockNumber = blockNumber;
extrinsic.blockHash = blockHash.toHuman();
extrinsic._id = `${blockNumber}-${extrinsicIndex}`;
//delete extrinsic.method
Object.keys(extrinsic.method.args).forEach(function (key) {
extrinsic.method.args[key] = mapTypes(
extrinsic.method.args[key]
);
});
if (["setValidationData"].includes(extrinsic.method.method)) return;
if (
extrinsic.method.section === "timestamp" &&
extrinsic.method.method === "set"
) {
block.timestamp = parseInt(
extrinsic.method.args.now.replaceAll(",", "")
);
return;
}
extrinsic.timestamp = block.timestamp;
const events = allRecords
.filter(
({ phase }) =>
phase.isApplyExtrinsic &&
phase.asApplyExtrinsic.eq(extrinsicIndex)
)
.map((e) => e.toHuman());
events.forEach(async (e, eventIndex) => {
if (Array.isArray(e)) {
e.event.data = e.event.data.map(mapTypes);
} else if (typeof e === "object") {
Object.keys(e.event.data).forEach(function (key) {
e.event.data[key] = mapTypes(e.event.data[key]);
});
}
e.event.blockNumber = blockNumber;
e.event.blockHash = blockHash.toHuman();
e.event._id = `${extrinsic._id}-${eventIndex}`;
e.event.extrinsicId = extrinsic._id;
e.event.timestamp = block.timestamp;
delete e.event.index;
});
events.forEach(async (e) => {
if (e.event.method === "ExtrinsicSuccess") {
extrinsic.success = true;
return;
}
await insertIntoCollection("events", e.event);
});
extrinsic = { ...extrinsic, ...extrinsic.method };
await insertIntoCollection("extrinsics", extrinsic);
});
await insertIntoCollection("blocks", block);
} catch (e) {
throw e;
}
}
async function catchUpWithChain(api, blockNumber, endBlockNumber) {
const numConcurrentJobs = NUM_CONCURRENT_JOBS;
for (let i = blockNumber; i <= endBlockNumber; i += numConcurrentJobs) {
let indexes = Array.from(Array(numConcurrentJobs).keys()).map(
(idx) => idx + i
);
indexes = indexes.filter((idx) => idx <= endBlockNumber);
let msg = `processing blocks ${indexes[0]} - ${
indexes[indexes.length - 1]
}`;
console.time(msg);
while (true) {
try {
await Promise.all(indexes.map((idx) => parseBlock(idx, api)));
break;
} catch (e) {
console.log(e);
await new Promise((r) => setTimeout(r, 5000));
continue;
}
}
console.timeEnd(msg);
}
}
export async function findUnprocessedBlockNumbers(blockNumber, endBlockNumber) {
const blocks = db.collection("blocks");
let processedBlockNumbers = await (
await blocks
.find({ height: { $gte: blockNumber, $lte: endBlockNumber } })
.project({ height: 1, _id: -1 })
).toArray();
processedBlockNumbers = processedBlockNumbers.map((e) => e.height);
const expectedBlockNumbers = Array(endBlockNumber - blockNumber + 1)
.fill()
.map((_, idx) => blockNumber + idx);
let unprocessedBlockNumbers = expectedBlockNumbers.filter(
(e) => !processedBlockNumbers.includes(e)
);
return unprocessedBlockNumbers;
}
export async function parseUnprocessedBlocks(api, blockNumber, endBlockNumber) {
const unprocessedBlockNumbers = await findUnprocessedBlockNumbers(
blockNumber,
endBlockNumber
);
await Promise.all(
unprocessedBlockNumbers.map((idx) => parseBlock(idx, api))
);
if (unprocessedBlockNumbers.length > 0) {
console.log(`done parsing blocks ${unprocessedBlockNumbers}`);
}
}
async function catchUpAndIndexLive(api) {
// last block number from safe base: 5506899
let lastProcessedBlockNumber = await getLastProcessedBlockNumber();
let firstRun = true;
await api.rpc.chain.subscribeFinalizedHeads(async (header) => {
const currentBlockNumber = parseInt(header.number.toString());
if (firstRun) {
console.log("catching up with chain");
catchUpWithChain(
api,
// some margin of safety, no harm if the blaock were already indexed
// and it could be that it just took them very long and were not yet processed
Math.max(
lastProcessedBlockNumber - NUM_CONCURRENT_JOBS * 5,
START_BLOCK
),
currentBlockNumber - 1
);
firstRun = false;
}
console.log(`Chain is at block: #${currentBlockNumber}`);
while (true) {
try {
await parseBlock(currentBlockNumber, api);
break;
} catch (e) {
console.log(e);
await new Promise((r) => setTimeout(r, 5000));
continue;
}
}
console.log(`Processed block ${currentBlockNumber}`);
if (currentBlockNumber % 5 === 0)
parseUnprocessedBlocks(
api,
currentBlockNumber - 20,
currentBlockNumber
);
});
}
async function getLastFinalizedBlock(api) {
return await api.rpc.chain.getBlock(await api.rpc.chain.getFinalizedHead());
}
async function getLastestFinalizedBlockNumber(api) {
return parseInt(
(await getLastFinalizedBlock(api)).block.header
.toHuman()
.number.replaceAll(",", "")
);
}
async function getLastAuthoredBlocks(api) {
try {
const lastAuthoredBlocks = await api.query.collatorSelection.lastAuthoredBlock.entries();
return lastAuthoredBlocks.map(([key, value]) => {
const collator = key.toHuman();
const blockNumber = value.toNumber();
return [collator, blockNumber];
});
}
catch (e) {
return [];
}
}
async function getBlockAuthor(api, blockNumber) {
const lastAuthoredBlocks = await getLastAuthoredBlocks(api);
const authorEntry = lastAuthoredBlocks.find(([_, authoredBlockNumber]) => authoredBlockNumber === blockNumber);
return authorEntry ? authorEntry[0][0] : null;
}
export async function main() {
const wsProvider = new WsProvider(RPC_NODE);
const api = await ApiPromise.create({
provider: wsProvider,
});
let lastProcessedBlockNumber = await getLastProcessedBlockNumber();
let currentBlockNumber = await getLastestFinalizedBlockNumber(api);
while (
currentBlockNumber - lastProcessedBlockNumber >
NUM_CONCURRENT_JOBS
) {
await catchUpWithChain(
api,
Math.max(
lastProcessedBlockNumber - 2 * NUM_CONCURRENT_JOBS,
START_BLOCK
),
currentBlockNumber
);
lastProcessedBlockNumber = await getLastProcessedBlockNumber(api);
currentBlockNumber = await getLastestFinalizedBlockNumber(api);
break;
}
console.log("Switching to live mode");
await catchUpAndIndexLive(api);
}