-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathinit.ts
379 lines (340 loc) · 13.3 KB
/
init.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
import { Server, createServer } from 'http';
import { Socket } from 'net';
import * as express from 'express';
import * as cors from 'cors';
import { TxRoutes } from './routes/tx';
import { DebugRoutes } from './routes/debug';
import { InfoRoutes } from './routes/info';
import { ContractRoutes } from './routes/contract';
import { CoreNodeRpcProxyRouter } from './routes/core-node-rpc-proxy';
import { BlockRoutes } from './routes/block';
import { FaucetRoutes } from './routes/faucets';
import { AddressRoutes } from './routes/address';
import { SearchRoutes } from './routes/search';
import { StxSupplyRoutes } from './routes/stx-supply';
import { createRosettaNetworkRouter } from './routes/rosetta/network';
import { createRosettaMempoolRouter } from './routes/rosetta/mempool';
import { createRosettaBlockRouter } from './routes/rosetta/block';
import { createRosettaAccountRouter } from './routes/rosetta/account';
import { createRosettaConstructionRouter } from './routes/rosetta/construction';
import { ChainID, apiDocumentationUrl } from '../helpers';
import { InvalidRequestError } from '../errors';
import { BurnchainRoutes } from './routes/burnchain';
import { BnsNamespaceRoutes } from './routes/bns/namespaces';
import { BnsPriceRoutes } from './routes/bns/pricing';
import { BnsNameRoutes } from './routes/bns/names';
import { BnsAddressRoutes } from './routes/bns/addresses';
import { MicroblockRoutes } from './routes/microblock';
import { StatusRoutes } from './routes/status';
import { TokenRoutes } from './routes/tokens';
import { FeeRateRoutes } from './routes/fee-rate';
import * as path from 'path';
import * as fs from 'fs';
import { PgStore } from '../datastore/pg-store';
import { PgWriteStore } from '../datastore/pg-write-store';
import { WebSocketTransmitter } from './routes/ws/web-socket-transmitter';
import { PoxEventRoutes, PoxRoutes } from './routes/pox';
import { logger, loggerMiddleware } from '../logger';
import {
PINO_LOGGER_CONFIG,
SERVER_VERSION,
isPgConnectionError,
isProdEnv,
parseBoolean,
waiter,
} from '@hirosystems/api-toolkit';
import { BlockRoutesV2 } from './routes/v2/blocks';
import { BurnBlockRoutesV2 } from './routes/v2/burn-blocks';
import { MempoolRoutesV2 } from './routes/v2/mempool';
import { SmartContractRoutesV2 } from './routes/v2/smart-contracts';
import { AddressRoutesV2 } from './routes/v2/addresses';
import { PoxRoutesV2 } from './routes/v2/pox';
import Fastify, { FastifyInstance, FastifyPluginAsync } from 'fastify';
import FastifyMetrics from 'fastify-metrics';
import FastifyCors from '@fastify/cors';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import * as promClient from 'prom-client';
import { BnsV2NameRoutes } from './routes/bnsV2/names';
import { BnsV2NamespaceRoutes } from './routes/bnsV2/namespaces';
import { BnsV2AddressRoutes } from './routes/bnsV2/addresses';
import { BnsV2PriceRoutes } from './routes/bnsV2/pricing';
import { BnsV2ReadRoutes } from './routes/bnsV2/reads';
export interface ApiServer {
fastifyApp: FastifyInstance;
server: Server;
ws: WebSocketTransmitter;
address: string;
datastore: PgStore;
terminate: () => Promise<void>;
forceKill: () => Promise<void>;
}
export const StacksApiRoutes: FastifyPluginAsync<
Record<never, never>,
Server,
TypeBoxTypeProvider
> = async fastify => {
await fastify.register(StatusRoutes);
await fastify.register(
async fastify => {
await fastify.register(TxRoutes, { prefix: '/tx' });
await fastify.register(StxSupplyRoutes, { prefix: '/stx_supply' });
await fastify.register(InfoRoutes, { prefix: '/info' });
await fastify.register(TokenRoutes, { prefix: '/tokens' });
await fastify.register(ContractRoutes, { prefix: '/contract' });
await fastify.register(FeeRateRoutes, { prefix: '/fee_rate' });
await fastify.register(MicroblockRoutes, { prefix: '/microblock' });
await fastify.register(BlockRoutes, { prefix: '/block' });
await fastify.register(BurnchainRoutes, { prefix: '/burnchain' });
await fastify.register(AddressRoutes, { prefix: '/address' });
await fastify.register(SearchRoutes, { prefix: '/search' });
await fastify.register(PoxRoutes, { prefix: '/:pox(pox\\d)' });
await fastify.register(PoxEventRoutes, { prefix: '/:(pox\\d_events)' });
await fastify.register(FaucetRoutes, { prefix: '/faucets' });
await fastify.register(DebugRoutes, { prefix: '/debug' });
},
{ prefix: '/extended/v1' }
);
await fastify.register(
async fastify => {
await fastify.register(BlockRoutesV2, { prefix: '/blocks' });
await fastify.register(BurnBlockRoutesV2, { prefix: '/burn-blocks' });
await fastify.register(SmartContractRoutesV2, { prefix: '/smart-contracts' });
await fastify.register(MempoolRoutesV2, { prefix: '/mempool' });
await fastify.register(PoxRoutesV2, { prefix: '/pox' });
await fastify.register(AddressRoutesV2, { prefix: '/addresses' });
},
{ prefix: '/extended/v2' }
);
// Setup legacy API v1 and v2 routes
await fastify.register(BnsNameRoutes, { prefix: '/v1/names' });
await fastify.register(BnsNamespaceRoutes, { prefix: '/v1/namespaces' });
await fastify.register(BnsAddressRoutes, { prefix: '/v1/addresses' });
await fastify.register(BnsPriceRoutes, { prefix: '/v2/prices' });
await fastify.register(BnsV2NameRoutes, { prefix: '/v2/names' });
await fastify.register(BnsV2NamespaceRoutes, { prefix: '/v2/namespaces' });
await fastify.register(BnsV2AddressRoutes, { prefix: '/v2/addresses' });
await fastify.register(BnsV2ReadRoutes, { prefix: '/v2/read' });
await fastify.register(BnsV2PriceRoutes, { prefix: '/v3/prices' });
await Promise.resolve();
};
function createRosettaServer(datastore: PgStore, chainId: ChainID) {
const app = express();
// Add API version to header
app.use((_, res, next) => {
res.setHeader(
'X-API-Version',
`${SERVER_VERSION.tag} (${SERVER_VERSION.branch}:${SERVER_VERSION.commit})`
);
res.append('Access-Control-Expose-Headers', 'X-API-Version');
next();
});
// Common logger middleware for the whole API.
app.use(loggerMiddleware);
app.set('json spaces', 2);
app.use('/doc', (req, res) => {
// if env variable for API_DOCS_URL is given
if (apiDocumentationUrl) {
return res.redirect(apiDocumentationUrl);
} else if (!isProdEnv) {
// use local documentation if serving locally
const apiDocumentationPath = path.join(__dirname + '../../../docs/.tmp/index.html');
if (fs.existsSync(apiDocumentationPath)) {
return res.sendFile(apiDocumentationPath);
}
const docNotFound = {
error: 'Local documentation not found',
desc: 'Please run the command: `npm run build:docs` and restart your server',
};
return res.send(docNotFound).status(404);
}
// for production and no API_DOCS_URL provided
const errObj = {
error: 'Documentation is not available',
desc: `You can still read documentation from https://docs.hiro.so/api`,
};
res.send(errObj).status(404);
});
app.use(
'/rosetta/v1',
(() => {
const router = express.Router();
router.use(cors());
router.use('/network', createRosettaNetworkRouter(datastore, chainId));
router.use('/mempool', createRosettaMempoolRouter(datastore, chainId));
router.use('/block', createRosettaBlockRouter(datastore, chainId));
router.use('/account', createRosettaAccountRouter(datastore, chainId));
router.use('/construction', createRosettaConstructionRouter(datastore, chainId));
return router;
})()
);
//handle invalid request gracefully
app.use((req, res) => {
res.status(404).json({ message: `${req.method} ${req.path} not found` });
});
// Setup error handler (must be added at the end of the middleware stack)
app.use(((error, req, res, next) => {
if (error && !res.headersSent) {
if (error instanceof InvalidRequestError) {
logger.warn(error, error.message);
res.status(error.status).json({ error: error.message }).end();
} else if (isPgConnectionError(error)) {
res.status(503).json({ error: `The database service is unavailable` }).end();
} else {
res.status(500);
res.json({ error: error.toString(), stack: (error as Error).stack }).end();
}
}
next(error);
}) as express.ErrorRequestHandler);
return app;
}
export async function startApiServer(opts: {
datastore: PgStore;
writeDatastore?: PgWriteStore;
chainId: ChainID;
/** If not specified, this is read from the STACKS_BLOCKCHAIN_API_HOST env var. */
serverHost?: string;
/** If not specified, this is read from the STACKS_BLOCKCHAIN_API_PORT env var. */
serverPort?: number;
}): Promise<ApiServer> {
const { datastore, writeDatastore, chainId, serverHost, serverPort } = opts;
const apiHost = serverHost ?? process.env['STACKS_BLOCKCHAIN_API_HOST'];
const apiPort = serverPort ?? parseInt(process.env['STACKS_BLOCKCHAIN_API_PORT'] ?? '');
if (!apiHost) {
throw new Error(
`STACKS_BLOCKCHAIN_API_HOST must be specified, e.g. "STACKS_BLOCKCHAIN_API_HOST=127.0.0.1"`
);
}
if (!apiPort) {
throw new Error(
`STACKS_BLOCKCHAIN_API_PORT must be specified, e.g. "STACKS_BLOCKCHAIN_API_PORT=3999"`
);
}
// Rosetta API -- https://www.rosetta-api.org
let expressApp: express.Express | undefined;
if (parseBoolean(process.env['STACKS_API_ENABLE_ROSETTA'] ?? '1')) {
expressApp = createRosettaServer(datastore, chainId);
}
const fastify = Fastify({
trustProxy: true,
logger: PINO_LOGGER_CONFIG,
ignoreTrailingSlash: true,
}).withTypeProvider<TypeBoxTypeProvider>();
fastify.decorate('db', datastore);
fastify.decorate('writeDb', writeDatastore);
fastify.decorate('chainId', chainId);
if (isProdEnv) {
await fastify.register(FastifyMetrics, {
endpoint: null,
promClient: promClient,
defaultMetrics: { enabled: false },
});
}
await fastify.register(FastifyCors, { exposedHeaders: ['X-API-Version'] });
fastify.addHook('preHandler', async (_, reply) => {
// Set API version in all responses.
void reply.header(
'X-API-Version',
`${SERVER_VERSION.tag} (${SERVER_VERSION.branch}:${SERVER_VERSION.commit})`
);
// Set caching on all routes to be disabled by default, individual routes can override.
void reply.header('Cache-Control', 'no-store');
});
fastify.setErrorHandler(async (error, _req, reply) => {
if (isPgConnectionError(error)) {
return reply.status(503).send({ error: `The database service is unavailable` });
} else {
return reply.send(error);
}
});
await fastify.register(StacksApiRoutes);
// Setup direct proxy to core-node RPC endpoints (/v2)
await fastify.register(CoreNodeRpcProxyRouter, { prefix: '/v2' });
// Wait for all routes and middleware to be ready before starting the server
await fastify.ready();
// The most straightforward way to split between Fastify and Express without
// introducing a bunch of problamatic middleware side-effects.
const rosettaPath = new RegExp('^/rosetta');
const server = createServer((req, res) => {
if (rosettaPath.test(req.url as string)) {
// handle with express
if (expressApp) {
expressApp(req, res);
} else {
res.writeHead(404).end();
}
} else {
// handle with fastify
fastify.server.emit('request', req, res);
}
});
const serverSockets = new Set<Socket>();
server.on('connection', socket => {
serverSockets.add(socket);
socket.once('close', () => {
serverSockets.delete(socket);
});
});
const ws = new WebSocketTransmitter(datastore, server);
ws.connect();
await new Promise<void>((resolve, reject) => {
try {
server.once('error', error => {
reject(error);
});
server.listen(apiPort, apiHost, () => {
resolve();
});
} catch (error) {
reject(error);
}
});
const terminate = async () => {
await new Promise<void>((resolve, reject) => {
logger.info('Closing WebSocket channels...');
ws.close(error => {
if (error) {
logger.error(error, 'Failed to gracefully close WebSocket channels');
reject(error);
} else {
logger.info('API WebSocket channels closed.');
resolve();
}
});
});
for (const socket of serverSockets) {
socket.destroy();
}
await new Promise<void>(resolve => {
logger.info('Closing API http server...');
server.close(() => {
logger.info('API http server closed.');
resolve();
});
});
};
const forceKill = async () => {
logger.info('Force closing API server...');
const [wsClosePromise, serverClosePromise] = [waiter(), waiter()];
ws.close(() => wsClosePromise.finish());
server.close(() => serverClosePromise.finish());
for (const socket of serverSockets) {
socket.destroy();
}
await Promise.allSettled([wsClosePromise, serverClosePromise]);
};
const addr = server.address();
if (addr === null) {
throw new Error('server missing address');
}
const addrStr = typeof addr === 'string' ? addr : `${addr.address}:${addr.port}`;
return {
fastifyApp: fastify,
server: server,
ws: ws,
address: addrStr,
datastore: datastore,
terminate: terminate,
forceKill: forceKill,
};
}