-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.ts
462 lines (399 loc) · 11.3 KB
/
service.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Imports: External */
import { BaseService } from '@eth-optimism/service-base'
import express, { Request, Response } from 'express'
import cors from 'cors'
import { BigNumber } from 'ethers'
import { JsonRpcProvider } from '@ethersproject/providers'
/* Imports: Internal */
import { TransportDB } from '../../db/transport-db'
import {
ContextResponse,
EnqueueResponse,
StateRootBatchResponse,
StateRootResponse,
SyncingResponse,
TransactionBatchResponse,
TransactionResponse,
} from '../../types'
export interface L1TransportServerOptions {
db: any
port: number
confirmations: number
l1RpcProvider: string | JsonRpcProvider
}
export class L1TransportServer extends BaseService<L1TransportServerOptions> {
protected name = 'L1 Transport Server'
protected defaultOptions = {
// TODO: Check if this port is used by any common software.
port: 7878,
}
private state: {
app: express.Express
server: any
db: TransportDB
l1RpcProvider: JsonRpcProvider
} = {} as any
protected async _init(): Promise<void> {
// TODO: I don't know if this is strictly necessary, but it's probably a good thing to do.
if (!this.options.db.isOpen()) {
await this.options.db.open()
}
this.state.db = new TransportDB(this.options.db)
this.state.l1RpcProvider =
typeof this.options.l1RpcProvider === 'string'
? new JsonRpcProvider(this.options.l1RpcProvider)
: this.options.l1RpcProvider
this._initializeApp()
}
protected async _start(): Promise<void> {
this.state.server = this.state.app.listen(this.options.port)
this.logger.info(`Server listening on port: ${this.options.port}`)
}
protected async _stop(): Promise<void> {
this.state.server.close()
}
/**
* Initializes the server application.
* Do any sort of initialization here that you want. Mostly just important that
* `_registerAllRoutes` is called at the end.
*/
private _initializeApp() {
// TODO: Maybe pass this in as a parameter instead of creating it here?
this.state.app = express()
this.state.app.use(cors())
this._registerAllRoutes()
}
/**
* Registers a route on the server.
* @param method Http method type.
* @param route Route to register.
* @param handler Handler called and is expected to return a JSON response.
*/
private _registerRoute(
method: 'get', // Just handle GET for now, but could extend this with whatever.
route: string,
handler: (req?: Request, res?: Response) => Promise<any>
): void {
// TODO: Better typing on the return value of the handler function.
// TODO: Check for route collisions.
// TODO: Add a different function to allow for removing routes.
this.state.app[method](route, async (req, res) => {
try {
this.logger.info(`${req.ip}: ${method.toUpperCase()} ${req.path}`)
return res.json(await handler(req, res))
} catch (e) {
return res.status(400).json({
error: e.toString(),
})
}
})
}
/**
* Registers all of the server routes we want to expose.
* TODO: Link to our API spec.
*/
private _registerAllRoutes(): void {
// TODO: Maybe add doc-like comments to each of these routes?
this._registerRoute(
'get',
'/eth/syncing',
async (): Promise<SyncingResponse> => {
const highestL2BlockNumber = await this.state.db.getHighestL2BlockNumber()
const currentL2Block = await this.state.db.getLatestTransaction()
if (currentL2Block === null) {
return {
syncing: true,
highestKnownBlock: highestL2BlockNumber,
currentBlock: 0,
}
}
if (highestL2BlockNumber > currentL2Block.index) {
return {
syncing: true,
highestKnownBlock: highestL2BlockNumber,
currentBlock: currentL2Block.index,
}
} else {
return {
syncing: false,
currentBlock: currentL2Block.index,
}
}
}
)
this._registerRoute(
'get',
'/eth/context/latest',
async (): Promise<ContextResponse> => {
const tip = await this.state.l1RpcProvider.getBlockNumber()
const blockNumber = Math.max(0, tip - this.options.confirmations)
const block = await this.state.l1RpcProvider.getBlock(blockNumber)
return {
blockNumber: block.number,
timestamp: block.timestamp,
blockHash: block.hash,
}
}
)
this._registerRoute(
'get',
'/enqueue/latest',
async (): Promise<EnqueueResponse> => {
const enqueue = await this.state.db.getLatestEnqueue()
if (enqueue === null) {
return null
}
const ctcIndex = await this.state.db.getTransactionIndexByQueueIndex(
enqueue.index
)
return {
...enqueue,
ctcIndex,
}
}
)
this._registerRoute(
'get',
'/enqueue/index/:index',
async (req): Promise<EnqueueResponse> => {
const enqueue = await this.state.db.getEnqueueByIndex(
BigNumber.from(req.params.index).toNumber()
)
if (enqueue === null) {
return null
}
const ctcIndex = await this.state.db.getTransactionIndexByQueueIndex(
enqueue.index
)
return {
...enqueue,
ctcIndex,
}
}
)
this._registerRoute(
'get',
'/transaction/latest',
async (): Promise<TransactionResponse> => {
const transaction = await this.state.db.getLatestFullTransaction()
if (transaction === null) {
return {
transaction: null,
batch: null,
}
}
const batch = await this.state.db.getTransactionBatchByIndex(
transaction.batchIndex
)
if (batch === null) {
return {
transaction: null,
batch: null,
}
}
return {
transaction,
batch,
}
}
)
this._registerRoute(
'get',
'/transaction/index/:index',
async (req): Promise<TransactionResponse> => {
const transaction = await this.state.db.getFullTransactionByIndex(
BigNumber.from(req.params.index).toNumber()
)
if (transaction === null) {
return {
transaction: null,
batch: null,
}
}
const batch = await this.state.db.getTransactionBatchByIndex(
transaction.batchIndex
)
if (batch === null) {
return {
transaction: null,
batch: null,
}
}
return {
transaction,
batch,
}
}
)
this._registerRoute(
'get',
'/batch/transaction/latest',
async (): Promise<TransactionBatchResponse> => {
const batch = await this.state.db.getLatestTransactionBatch()
if (batch === null) {
return {
batch: null,
transactions: [],
}
}
const transactions = await this.state.db.getFullTransactionsByIndexRange(
BigNumber.from(batch.prevTotalElements).toNumber(),
BigNumber.from(batch.prevTotalElements).toNumber() +
BigNumber.from(batch.size).toNumber()
)
if (transactions === null) {
return {
batch: null,
transactions: [],
}
}
return {
batch,
transactions,
}
}
)
this._registerRoute(
'get',
'/batch/transaction/index/:index',
async (req): Promise<TransactionBatchResponse> => {
const batch = await this.state.db.getTransactionBatchByIndex(
BigNumber.from(req.params.index).toNumber()
)
if (batch === null) {
return {
batch: null,
transactions: [],
}
}
const transactions = await this.state.db.getFullTransactionsByIndexRange(
BigNumber.from(batch.prevTotalElements).toNumber(),
BigNumber.from(batch.prevTotalElements).toNumber() +
BigNumber.from(batch.size).toNumber()
)
if (transactions === null) {
return {
batch: null,
transactions: [],
}
}
return {
batch,
transactions,
}
}
)
this._registerRoute(
'get',
'/stateroot/latest',
async (): Promise<StateRootResponse> => {
const stateRoot = await this.state.db.getLatestStateRoot()
if (stateRoot === null) {
return {
stateRoot: null,
batch: null,
}
}
const batch = await this.state.db.getStateRootBatchByIndex(
stateRoot.batchIndex
)
if (batch === null) {
return {
stateRoot: null,
batch: null,
}
}
return {
stateRoot,
batch,
}
}
)
this._registerRoute(
'get',
'/stateroot/index/:index',
async (req): Promise<StateRootResponse> => {
const stateRoot = await this.state.db.getStateRootByIndex(
BigNumber.from(req.params.index).toNumber()
)
if (stateRoot === null) {
return {
stateRoot: null,
batch: null,
}
}
const batch = await this.state.db.getStateRootBatchByIndex(
stateRoot.batchIndex
)
if (batch === null) {
return {
stateRoot: null,
batch: null,
}
}
return {
stateRoot,
batch,
}
}
)
this._registerRoute(
'get',
'/batch/stateroot/latest',
async (): Promise<StateRootBatchResponse> => {
const batch = await this.state.db.getLatestStateRootBatch()
if (batch === null) {
return {
batch: null,
stateRoots: [],
}
}
const stateRoots = await this.state.db.getStateRootsByIndexRange(
BigNumber.from(batch.prevTotalElements).toNumber(),
BigNumber.from(batch.prevTotalElements).toNumber() +
BigNumber.from(batch.size).toNumber()
)
if (stateRoots === null) {
return {
batch: null,
stateRoots: [],
}
}
return {
batch,
stateRoots,
}
}
)
this._registerRoute(
'get',
'/batch/stateroot/index/:index',
async (req): Promise<StateRootBatchResponse> => {
const batch = await this.state.db.getStateRootBatchByIndex(
BigNumber.from(req.params.index).toNumber()
)
if (batch === null) {
return {
batch: null,
stateRoots: [],
}
}
const stateRoots = await this.state.db.getStateRootsByIndexRange(
BigNumber.from(batch.prevTotalElements).toNumber(),
BigNumber.from(batch.prevTotalElements).toNumber() +
BigNumber.from(batch.size).toNumber()
)
if (stateRoots === null) {
return {
batch: null,
stateRoots: [],
}
}
return {
batch,
stateRoots,
}
}
)
}
}