@xyo-network/chain-mempool 1.19.7 → 1.19.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/node/MempoolActor.d.ts +20 -0
  2. package/dist/node/MempoolActor.d.ts.map +1 -0
  3. package/dist/node/index.d.ts +2 -1
  4. package/dist/node/index.d.ts.map +1 -1
  5. package/dist/node/index.mjs +87 -385
  6. package/dist/node/index.mjs.map +1 -1
  7. package/dist/node/runMempool.d.ts +4 -0
  8. package/dist/node/runMempool.d.ts.map +1 -0
  9. package/package.json +9 -21
  10. package/src/MempoolActor.ts +83 -0
  11. package/src/index.ts +2 -1
  12. package/src/runMempool.ts +28 -0
  13. package/dist/node/MempoolContext.d.ts +0 -9
  14. package/dist/node/MempoolContext.d.ts.map +0 -1
  15. package/dist/node/helpers/index.d.ts +0 -2
  16. package/dist/node/helpers/index.d.ts.map +0 -1
  17. package/dist/node/helpers/initChainId.d.ts +0 -4
  18. package/dist/node/helpers/initChainId.d.ts.map +0 -1
  19. package/dist/node/manifest/getLocator.d.ts +0 -14
  20. package/dist/node/manifest/getLocator.d.ts.map +0 -1
  21. package/dist/node/manifest/getNode.d.ts +0 -15
  22. package/dist/node/manifest/getNode.d.ts.map +0 -1
  23. package/dist/node/manifest/index.d.ts +0 -6
  24. package/dist/node/manifest/index.d.ts.map +0 -1
  25. package/dist/node/manifest/nodeManifest.d.ts +0 -6
  26. package/dist/node/manifest/nodeManifest.d.ts.map +0 -1
  27. package/dist/node/manifest/private/index.d.ts +0 -5
  28. package/dist/node/manifest/private/index.d.ts.map +0 -1
  29. package/dist/node/manifest/public/index.d.ts +0 -14
  30. package/dist/node/manifest/public/index.d.ts.map +0 -1
  31. package/dist/node/server/app.d.ts +0 -4
  32. package/dist/node/server/app.d.ts.map +0 -1
  33. package/dist/node/server/index.d.ts +0 -11
  34. package/dist/node/server/index.d.ts.map +0 -1
  35. package/dist/node/server/instrumentation.d.ts +0 -9
  36. package/dist/node/server/instrumentation.d.ts.map +0 -1
  37. package/dist/node/server/model.d.ts +0 -5
  38. package/dist/node/server/model.d.ts.map +0 -1
  39. package/dist/node/server/routes/addRoutes.d.ts +0 -3
  40. package/dist/node/server/routes/addRoutes.d.ts.map +0 -1
  41. package/dist/node/server/routes/address/AddressPathParams.d.ts +0 -4
  42. package/dist/node/server/routes/address/AddressPathParams.d.ts.map +0 -1
  43. package/dist/node/server/routes/address/addNodeRoutes.d.ts +0 -3
  44. package/dist/node/server/routes/address/addNodeRoutes.d.ts.map +0 -1
  45. package/dist/node/server/routes/address/get/get.d.ts +0 -4
  46. package/dist/node/server/routes/address/get/get.d.ts.map +0 -1
  47. package/dist/node/server/routes/address/get/index.d.ts +0 -2
  48. package/dist/node/server/routes/address/get/index.d.ts.map +0 -1
  49. package/dist/node/server/routes/address/index.d.ts +0 -2
  50. package/dist/node/server/routes/address/index.d.ts.map +0 -1
  51. package/dist/node/server/routes/address/post/getQueryConfig.d.ts +0 -6
  52. package/dist/node/server/routes/address/post/getQueryConfig.d.ts.map +0 -1
  53. package/dist/node/server/routes/address/post/index.d.ts +0 -2
  54. package/dist/node/server/routes/address/post/index.d.ts.map +0 -1
  55. package/dist/node/server/routes/address/post/post.d.ts +0 -8
  56. package/dist/node/server/routes/address/post/post.d.ts.map +0 -1
  57. package/dist/node/server/routes/dataLake/addDataLakeRoutes.d.ts +0 -3
  58. package/dist/node/server/routes/dataLake/addDataLakeRoutes.d.ts.map +0 -1
  59. package/dist/node/server/routes/dataLake/archivistMiddleware.d.ts +0 -10
  60. package/dist/node/server/routes/dataLake/archivistMiddleware.d.ts.map +0 -1
  61. package/dist/node/server/routes/dataLake/index.d.ts +0 -2
  62. package/dist/node/server/routes/dataLake/index.d.ts.map +0 -1
  63. package/dist/node/server/routes/healthz/addHealthRoutes.d.ts +0 -3
  64. package/dist/node/server/routes/healthz/addHealthRoutes.d.ts.map +0 -1
  65. package/dist/node/server/routes/healthz/index.d.ts +0 -2
  66. package/dist/node/server/routes/healthz/index.d.ts.map +0 -1
  67. package/dist/node/server/routes/index.d.ts +0 -4
  68. package/dist/node/server/routes/index.d.ts.map +0 -1
  69. package/dist/node/server/server.d.ts +0 -11
  70. package/dist/node/server/server.d.ts.map +0 -1
  71. package/src/MempoolContext.ts +0 -12
  72. package/src/global.d.ts +0 -13
  73. package/src/helpers/index.ts +0 -1
  74. package/src/helpers/initChainId.ts +0 -20
  75. package/src/manifest/getLocator.ts +0 -70
  76. package/src/manifest/getNode.ts +0 -32
  77. package/src/manifest/index.ts +0 -5
  78. package/src/manifest/node.json +0 -17
  79. package/src/manifest/nodeManifest.ts +0 -8
  80. package/src/manifest/private/index.ts +0 -4
  81. package/src/manifest/public/Chain.json +0 -63
  82. package/src/manifest/public/Pending.json +0 -35
  83. package/src/manifest/public/index.ts +0 -20
  84. package/src/server/app.ts +0 -31
  85. package/src/server/index.ts +0 -13
  86. package/src/server/instrumentation.ts +0 -15
  87. package/src/server/model.ts +0 -5
  88. package/src/server/routes/addRoutes.ts +0 -11
  89. package/src/server/routes/address/AddressPathParams.ts +0 -3
  90. package/src/server/routes/address/addNodeRoutes.ts +0 -21
  91. package/src/server/routes/address/get/get.ts +0 -30
  92. package/src/server/routes/address/get/index.ts +0 -1
  93. package/src/server/routes/address/index.ts +0 -1
  94. package/src/server/routes/address/post/getQueryConfig.ts +0 -23
  95. package/src/server/routes/address/post/index.ts +0 -1
  96. package/src/server/routes/address/post/post.ts +0 -77
  97. package/src/server/routes/dataLake/addDataLakeRoutes.ts +0 -9
  98. package/src/server/routes/dataLake/archivistMiddleware.ts +0 -85
  99. package/src/server/routes/dataLake/index.ts +0 -1
  100. package/src/server/routes/healthz/addHealthRoutes.ts +0 -14
  101. package/src/server/routes/healthz/index.ts +0 -1
  102. package/src/server/routes/index.ts +0 -3
  103. package/src/server/server.ts +0 -147
@@ -1,70 +0,0 @@
1
- import { type BaseMongoSdkPrivateConfig } from '@xylabs/mongo'
2
- import type { Logger } from '@xylabs/sdk-js'
3
- import { isDefined } from '@xylabs/sdk-js'
4
- import { MemoryArchivist } from '@xyo-network/archivist-memory'
5
- import { MongoDBArchivistV2 } from '@xyo-network/archivist-mongodb'
6
- import { ViewArchivist } from '@xyo-network/archivist-view'
7
- import { initTelemetry } from '@xyo-network/chain-telemetry'
8
- import { AbstractModule, LoggerModuleStatusReporter } from '@xyo-network/module-abstract'
9
- import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
10
- import type { MongoDBModuleParamsV2 } from '@xyo-network/module-model-mongodb'
11
- import { MemorySentinel } from '@xyo-network/sentinel-memory'
12
- import type { Config } from '@xyo-network/xl1-sdk'
13
- import { hasMongoConfig } from '@xyo-network/xl1-sdk'
14
-
15
- export interface GetLocatorContext {
16
- config: Config
17
- logger?: Logger
18
- }
19
-
20
- /**
21
- * Used for retrieving a locator with the necessary modules registered for testing
22
- * operation of the node (entirely in memory)
23
- * @returns A locator with the necessary modules registered
24
- */
25
- export const getLocator = async (context: GetLocatorContext) => {
26
- const { config, logger } = context
27
- const { otlpEndpoint } = config.telemetry?.otel ?? {}
28
- const { path: endpoint = '/metrics', port: port = 9466 } = config.telemetry?.metrics?.scrape ?? {}
29
- const { traceProvider, meterProvider } = await initTelemetry({
30
- attributes: {
31
- serviceName: 'xl1-mempool',
32
- serviceVersion: '1.0.0',
33
- },
34
- otlpEndpoint,
35
- metricsConfig: { endpoint, port },
36
- })
37
-
38
- if (isDefined(logger)) AbstractModule.defaultLogger = logger
39
- const statusReporter = logger ? new LoggerModuleStatusReporter(logger) : undefined
40
-
41
- const locator = new ModuleFactoryLocator()
42
-
43
- // If there's a MongoDB configuration
44
- const mongoConfig = config.storage?.mongo
45
- if (hasMongoConfig(mongoConfig)) {
46
- // Create the MongoDB SDK from the configuration
47
- const {
48
- connectionString: dbConnectionString, database: dbName, domain: dbDomain, password: dbPassword, username: dbUserName,
49
- } = mongoConfig
50
- const payloadSdkConfig: BaseMongoSdkPrivateConfig = {
51
- dbConnectionString, dbDomain, dbName, dbPassword, dbUserName,
52
- }
53
- const params: Partial<MongoDBModuleParamsV2> = {
54
- meterProvider, payloadSdkConfig, statusReporter, traceProvider,
55
- }
56
- // Register the MongoDB Archivist as the default
57
- locator.register(MongoDBArchivistV2.factory(params), undefined, true)
58
- }
59
-
60
- locator.register(MemoryArchivist.factory({
61
- traceProvider, meterProvider, statusReporter,
62
- }))
63
- locator.register(MemorySentinel.factory({
64
- traceProvider, meterProvider, statusReporter,
65
- }))
66
- locator.register(ViewArchivist.factory({
67
- traceProvider, meterProvider, statusReporter,
68
- }))
69
- return locator
70
- }
@@ -1,32 +0,0 @@
1
- import type { Logger } from '@xylabs/sdk-js'
2
- import { ManifestWrapper } from '@xyo-network/manifest-wrapper'
3
- import type { WalletInstance } from '@xyo-network/wallet-model'
4
- import type { Config } from '@xyo-network/xl1-sdk'
5
-
6
- import { getLocator } from './getLocator.ts'
7
- import { NodeManifest } from './nodeManifest.ts'
8
- import { PrivateChildManifests } from './private/index.ts'
9
- import { PublicChildManifests } from './public/index.ts'
10
-
11
- export interface GetNodeContext {
12
- config: Config
13
- logger?: Logger
14
- wallet: WalletInstance
15
- }
16
-
17
- /**
18
- * Creates a node with the xyo-chain modules registered
19
- * @param context The context to use for the node
20
- * @returns A node with the xyo-chain modules registered
21
- */
22
- export const getNode = async (context: GetNodeContext) => {
23
- const { wallet } = context
24
- const locator = await getLocator(context)
25
- const wrapper = new ManifestWrapper(NodeManifest, wallet, locator, PublicChildManifests, PrivateChildManifests)
26
- const [node, ...childNodes] = await wrapper.loadNodes()
27
- if (childNodes?.length > 0) {
28
- await Promise.all(childNodes.map(childNode => node.register(childNode)))
29
- await Promise.all(childNodes.map(childNode => node.attach(childNode.address, true)))
30
- }
31
- return node
32
- }
@@ -1,5 +0,0 @@
1
- export * from './getLocator.ts'
2
- export * from './getNode.ts'
3
- export * from './nodeManifest.ts'
4
- export * from './private/index.ts'
5
- export * from './public/index.ts'
@@ -1,17 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/schema.json",
3
- "nodes": [
4
- {
5
- "config": {
6
- "accountPath": "44'/60'/1",
7
- "name": "XYOChain",
8
- "schema": "network.xyo.node.config"
9
- },
10
- "modules": {
11
- "private": [],
12
- "public": []
13
- }
14
- }
15
- ],
16
- "schema": "network.xyo.manifest"
17
- }
@@ -1,8 +0,0 @@
1
- import type { PackageManifestPayload } from '@xyo-network/manifest-model'
2
-
3
- import node from './node.json' with { type: 'json' }
4
-
5
- /**
6
- * Root Node Manifest
7
- */
8
- export const NodeManifest = node as unknown as PackageManifestPayload
@@ -1,4 +0,0 @@
1
- /**
2
- * Private Child Manifests
3
- */
4
- export const PrivateChildManifests = []
@@ -1,63 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/schema.json",
3
- "nodes": [
4
- {
5
- "config": {
6
- "accountPath": "1",
7
- "name": "Chain",
8
- "schema": "network.xyo.node.config"
9
- },
10
- "modules": {
11
- "private": [
12
- {
13
- "config": {
14
- "accountPath": "1/1'/1'",
15
- "name": "Validated",
16
- "getCache": {
17
- "enabled": true,
18
- "maxEntries": 5000
19
- },
20
- "payloadSdkConfig": {
21
- "collection": "chain_validated"
22
- },
23
- "schema": "network.xyo.archivist.config"
24
- }
25
- }
26
- ],
27
- "public": [
28
- {
29
- "config": {
30
- "accountPath": "1/1/1",
31
- "name": "Submissions",
32
- "getCache": {
33
- "enabled": true,
34
- "maxEntries": 5000
35
- },
36
- "payloadSdkConfig": {
37
- "collection": "chain_submissions"
38
- },
39
- "schema": "network.xyo.archivist.config"
40
- }
41
- },
42
- {
43
- "config": {
44
- "accountPath": "1/1/2",
45
- "name": "Finalized",
46
- "allowedQueries": [
47
- "network.xyo.query.archivist.get",
48
- "network.xyo.query.archivist.next"
49
- ],
50
- "getCache": {
51
- "enabled": true,
52
- "maxEntries": 50000
53
- },
54
- "originArchivist": "Validated",
55
- "schema": "network.xyo.archivist.view.config"
56
- }
57
- }
58
- ]
59
- }
60
- }
61
- ],
62
- "schema": "network.xyo.manifest"
63
- }
@@ -1,35 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/schema.json",
3
- "nodes": [
4
- {
5
- "config": {
6
- "accountPath": "2",
7
- "name": "Pending",
8
- "schema": "network.xyo.node.config"
9
- },
10
- "modules": {
11
- "private": [],
12
- "public": [
13
- {
14
- "config": {
15
- "accountPath": "2/1/1",
16
- "name": "PendingTransactions",
17
- "getCache": {
18
- "enabled": true,
19
- "maxEntries": 5000
20
- },
21
- "labels": {
22
- "network.xyo.storage.class": "mongodb"
23
- },
24
- "payloadSdkConfig": {
25
- "collection": "pending_bundles"
26
- },
27
- "schema": "network.xyo.archivist.config"
28
- }
29
- }
30
- ]
31
- }
32
- }
33
- ],
34
- "schema": "network.xyo.manifest"
35
- }
@@ -1,20 +0,0 @@
1
- import type { ModuleManifest, PackageManifestPayload } from '@xyo-network/manifest-model'
2
-
3
- import Chain from './Chain.json' with { type: 'json' }
4
- import Pending from './Pending.json' with { type: 'json' }
5
-
6
- /**
7
- * Chain Node Manifest
8
- */
9
- export const ChainNodeManifest = Chain as unknown as PackageManifestPayload
10
- /**
11
- * Pending Node Manifest
12
- */
13
- export const PendingNodeManifest = Pending as unknown as PackageManifestPayload
14
- /**
15
- * Public Child Manifests
16
- */
17
- export const PublicChildManifests: ModuleManifest[] = [
18
- ...ChainNodeManifest.nodes,
19
- ...PendingNodeManifest.nodes,
20
- ]
package/src/server/app.ts DELETED
@@ -1,31 +0,0 @@
1
- import {
2
- customPoweredByHeader, disableCaseSensitiveRouting, disableExpressDefaultPoweredByHeader, getJsonBodyParser, getJsonBodyParserOptions, responseProfiler,
3
- standardErrors, standardResponses,
4
- } from '@xylabs/express'
5
- import compression from 'compression'
6
- import cors from 'cors'
7
- import type { Express } from 'express'
8
- import express from 'express'
9
-
10
- import type { MempoolContext } from '../MempoolContext.ts'
11
- import { addInstrumentation } from './instrumentation.ts'
12
- import { addRoutes } from './routes/index.ts'
13
-
14
- export const getApp = (context: MempoolContext): Express => {
15
- addInstrumentation()
16
- const app = express()
17
- app.set('etag', false)
18
- app.use(cors())
19
- app.use(compression())
20
- app.use(responseProfiler)
21
- app.use(getJsonBodyParser(getJsonBodyParserOptions({ limit: '1mb' })))
22
- app.use(standardResponses)
23
- disableExpressDefaultPoweredByHeader(app)
24
- app.use(customPoweredByHeader)
25
- disableCaseSensitiveRouting(app)
26
- app.context = context
27
- app.node = context.node
28
- addRoutes(app)
29
- app.use(standardErrors)
30
- return app
31
- }
@@ -1,13 +0,0 @@
1
- export * from './app.ts'
2
- export * from './server.ts'
3
-
4
- import type { NodeInstance } from '@xyo-network/node-model'
5
-
6
- declare global {
7
- // eslint-disable-next-line @typescript-eslint/no-namespace
8
- namespace Express {
9
- interface Application {
10
- node: NodeInstance
11
- }
12
- }
13
- }
@@ -1,15 +0,0 @@
1
- import { registerInstrumentations } from '@opentelemetry/instrumentation'
2
- import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
3
- import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
4
-
5
- /**
6
- * Registers OpenTelemetry instrumentations for HTTP and Express.
7
- * This function is used to set up the necessary instrumentations for monitoring
8
- * HTTP requests and Express applications. Since it monkey patches the Express
9
- * router & middleware system, it should be called before any Express applications
10
- * are defined.
11
- */
12
- export const addInstrumentation = () => {
13
- const instrumentations = [new HttpInstrumentation(), new ExpressInstrumentation()]
14
- registerInstrumentations({ instrumentations })
15
- }
@@ -1,5 +0,0 @@
1
- import type { BaseContext, Config } from '@xyo-network/xl1-sdk'
2
-
3
- export interface AppContext extends BaseContext {
4
- config: Config
5
- }
@@ -1,11 +0,0 @@
1
- import type { Express } from 'express'
2
-
3
- import { addNodeRoutes } from './address/index.ts'
4
- import { addDataLakeRoutes } from './dataLake/index.ts'
5
- import { addHealthRoutes } from './healthz/index.ts'
6
-
7
- export const addRoutes = (app: Express) => {
8
- addHealthRoutes(app)
9
- addDataLakeRoutes(app)
10
- addNodeRoutes(app)
11
- }
@@ -1,3 +0,0 @@
1
- export type AddressPathParams = {
2
- address: string
3
- }
@@ -1,21 +0,0 @@
1
- import type { Express } from 'express'
2
- import { StatusCodes } from 'http-status-codes'
3
-
4
- import { getAddress } from './get/index.ts'
5
- import { postAddress } from './post/index.ts'
6
-
7
- export const addNodeRoutes = (app: Express) => {
8
- const defaultModule = app.node
9
- const address = defaultModule.address
10
- const defaultModuleEndpoint = `/${address}`
11
- app.get('/', (_req, res) => res.redirect(StatusCodes.MOVED_TEMPORARILY, defaultModuleEndpoint))
12
- app.post('/', (_req, res) => res.redirect(StatusCodes.TEMPORARY_REDIRECT, defaultModuleEndpoint))
13
- app.get('/:address', getAddress)
14
- app.post('/:address', postAddress)
15
- app.get('/:hash', (_req, res) => {
16
- res.sendStatus(StatusCodes.NOT_FOUND)
17
- })
18
- app.post('/:hash', (_req, res) => {
19
- res.sendStatus(StatusCodes.NOT_FOUND)
20
- })
21
- }
@@ -1,30 +0,0 @@
1
- import { asyncHandler } from '@xylabs/express'
2
- import { asAddress, isDefined } from '@xylabs/sdk-js'
3
- import { isModuleName } from '@xyo-network/module-model'
4
- import type { Payload } from '@xyo-network/payload-model'
5
- import type { RequestHandler } from 'express'
6
- import { StatusCodes } from 'http-status-codes'
7
-
8
- import type { AddressPathParams } from '../AddressPathParams.ts'
9
-
10
- const handler: RequestHandler<AddressPathParams, Payload[]> = async (req, res, next) => {
11
- const { address: moduleIdentifier } = req.params
12
- const { node } = req.app
13
- const address = asAddress(moduleIdentifier)
14
- if (isDefined(address)) {
15
- let mod = node.address === address ? node : (await node.resolve(address, { direction: 'down' }))
16
- if (mod) {
17
- res.json(await mod.state())
18
- return
19
- }
20
- }
21
- if (isModuleName(moduleIdentifier)) {
22
- const mod = await node.resolve(moduleIdentifier, { direction: 'down' })
23
- if (mod) {
24
- res.redirect(StatusCodes.MOVED_TEMPORARILY, `/${mod.address}`)
25
- return
26
- }
27
- }
28
- next('route')
29
- }
30
- export const getAddress = asyncHandler(handler)
@@ -1 +0,0 @@
1
- export * from './get.ts'
@@ -1 +0,0 @@
1
- export * from './addNodeRoutes.ts'
@@ -1,23 +0,0 @@
1
- import type { BoundWitness, QueryBoundWitness } from '@xyo-network/boundwitness-model'
2
- import { BoundWitnessSchema } from '@xyo-network/boundwitness-model'
3
- import type { ModuleConfig, ModuleInstance } from '@xyo-network/module-model'
4
- import { ModuleConfigSchema } from '@xyo-network/module-model'
5
- import type { Payload } from '@xyo-network/payload-model'
6
- import type { Request } from 'express'
7
-
8
- const DEFAULT_DEPTH = 5 as const
9
-
10
- export const getQueryConfig = (mod: ModuleInstance, req: Request, bw: QueryBoundWitness, payloads?: Payload[]): ModuleConfig | undefined => {
11
- // TODO: Filter based on query addresses?
12
- // Recurse through payloads for nested BWs
13
- const nestedBwAddresses
14
- = payloads
15
- ?.flat(DEFAULT_DEPTH)
16
- .filter<BoundWitness>((payload): payload is BoundWitness => payload?.schema === BoundWitnessSchema)
17
- .map(bw => bw.addresses) ?? []
18
- // TODO: Do we want to end up with a list of addresses or a list of address lists?
19
- const addresses = [bw.addresses, ...nestedBwAddresses].filter(address => address.length > 0)
20
- const allowed = addresses.length > 0 ? Object.fromEntries(mod.queries.map(schema => [schema, addresses])) : {}
21
- const security = { allowed }
22
- return { schema: ModuleConfigSchema, security }
23
- }
@@ -1 +0,0 @@
1
- export * from './post.ts'
@@ -1,77 +0,0 @@
1
- import { asyncHandler } from '@xylabs/express'
2
- import type { JsonObject } from '@xylabs/sdk-js'
3
- import {
4
- asAddress, assertEx,
5
- isAddress,
6
- toAddress,
7
- } from '@xylabs/sdk-js'
8
- import { isQueryBoundWitness, type QueryBoundWitness } from '@xyo-network/boundwitness-model'
9
- import { ModuleErrorBuilder } from '@xyo-network/module-abstract'
10
- import type { ModuleInstance, ModuleQueryResult } from '@xyo-network/module-model'
11
- import type { ModuleError, Payload } from '@xyo-network/payload-model'
12
- import type { RequestHandler } from 'express'
13
- import { StatusCodes } from 'http-status-codes'
14
-
15
- import type { AddressPathParams } from '../AddressPathParams.ts'
16
- import { getQueryConfig } from './getQueryConfig.ts'
17
-
18
- type PostAddressRequestBody = [QueryBoundWitness, undefined | Payload[]]
19
-
20
- const handler: RequestHandler<AddressPathParams, ModuleQueryResult | ModuleError, PostAddressRequestBody> = async (req, res, next) => {
21
- const returnError = (code: number, message = 'An error occurred', details?: JsonObject) => {
22
- const error = new ModuleErrorBuilder().message(message).details(details).build()
23
- res.locals.rawResponse = false
24
- res.status(code).json(error)
25
- next()
26
- }
27
-
28
- const { address } = req.params
29
- const { node } = req.app
30
- const [bw, payloads] = Array.isArray(req.body) ? req.body : []
31
- if (!isAddress(address)) {
32
- return returnError(StatusCodes.BAD_REQUEST, 'Missing address')
33
- }
34
-
35
- if (!bw) {
36
- return returnError(StatusCodes.BAD_REQUEST, 'Missing boundwitness')
37
- }
38
-
39
- if (!isQueryBoundWitness(bw)) {
40
- return returnError(StatusCodes.BAD_REQUEST, 'Invalid query boundwitness')
41
- }
42
-
43
- let modules: ModuleInstance[] = []
44
- const normalizedAddress = toAddress(address)
45
- if (node.address === normalizedAddress) modules = [node]
46
- else {
47
- const typedAddress = asAddress(address)
48
- const byAddress = (typedAddress === undefined) ? undefined : await node.resolve(typedAddress, { maxDepth: 10 })
49
-
50
- if (byAddress) modules = [byAddress]
51
- else {
52
- const byName = await node.resolve(address, { direction: 'down' })
53
- if (byName) {
54
- const moduleAddress = assertEx(byName?.address, () => 'Error redirecting to module by address')
55
- res.redirect(StatusCodes.TEMPORARY_REDIRECT, `/${moduleAddress}`)
56
- return
57
- } else {
58
- return returnError(StatusCodes.NOT_FOUND, 'Module not found', { address })
59
- }
60
- }
61
- }
62
-
63
- if (modules.length > 0) {
64
- const mod = modules[0]
65
- const queryConfig = getQueryConfig(mod, req, bw, payloads)
66
- try {
67
- const queryResult = await mod.query(bw, payloads, queryConfig)
68
- res.json(queryResult)
69
- } catch (ex) {
70
- return returnError(StatusCodes.INTERNAL_SERVER_ERROR, 'Query Failed', { message: (ex as Error)?.message ?? 'Unknown Error' })
71
- }
72
- } else {
73
- return returnError(StatusCodes.NOT_FOUND, 'Module not found', { address })
74
- }
75
- }
76
-
77
- export const postAddress = asyncHandler(handler)
@@ -1,9 +0,0 @@
1
- import type { Express } from 'express'
2
-
3
- import { archivistMiddleware } from './archivistMiddleware.ts'
4
-
5
- export const addDataLakeRoutes = (app: Express) => {
6
- const { node } = app
7
- const archivistModuleIdentifier = 'Chain:Finalized'
8
- app.use('/chain', archivistMiddleware({ node, archivistModuleIdentifier }))
9
- }
@@ -1,85 +0,0 @@
1
- import { setRawResponseFormat } from '@xylabs/express'
2
- import { asHash, isDefined } from '@xylabs/sdk-js'
3
- import type {
4
- ArchivistInstance,
5
- ArchivistNextOptions, NextOptions,
6
- } from '@xyo-network/archivist-model'
7
- import { asArchivistInstance } from '@xyo-network/archivist-model'
8
- import type { ModuleIdentifier } from '@xyo-network/module-model'
9
- import type { NodeInstance } from '@xyo-network/node-model'
10
- import { PayloadBuilder } from '@xyo-network/payload-builder'
11
- import type { Payload } from '@xyo-network/payload-model'
12
- import { isAnyPayload, isSequence } from '@xyo-network/payload-model'
13
- import type { Router } from 'express'
14
- import express from 'express'
15
- import type { Request } from 'express-serve-static-core'
16
-
17
- const resolveArchivist = async (node: NodeInstance, archivistModuleIdentifier: ModuleIdentifier): Promise<ArchivistInstance> => {
18
- const mod = await node.resolve(archivistModuleIdentifier)
19
- return asArchivistInstance(mod, { required: true })
20
- }
21
-
22
- let archivistInstance: ArchivistInstance | undefined
23
-
24
- const getArchivist = async (node: NodeInstance, archivistModuleIdentifier: ModuleIdentifier): Promise<ArchivistInstance> => {
25
- if (isDefined(archivistInstance)) return archivistInstance
26
- archivistInstance = await resolveArchivist(node, archivistModuleIdentifier)
27
- return archivistInstance
28
- }
29
-
30
- type ArchivistMiddlewareOptions = {
31
- archivistModuleIdentifier: ModuleIdentifier
32
- node: NodeInstance
33
- }
34
-
35
- export const archivistMiddleware = (options: ArchivistMiddlewareOptions): Router => {
36
- const { node, archivistModuleIdentifier } = options
37
- const router = express.Router({ mergeParams: true })
38
-
39
- router.post('/insert', async (req, res) => {
40
- setRawResponseFormat(res)
41
- const body = Array.isArray(req.body) ? req.body : [req.body]
42
- const payloads = (await PayloadBuilder.hashPairs<Payload>(body)).map(p => p[0])
43
- const archivist = await getArchivist(node, archivistModuleIdentifier)
44
- const result = await archivist.insert(payloads)
45
- res.status(200).json(result)
46
- })
47
-
48
- router.get('/next', async (req: Request<Partial<NextOptions>>, res) => {
49
- setRawResponseFormat(res)
50
- const cursor = isSequence(req.query.cursor) ? req.query.cursor : undefined
51
- const limit = isDefined(req.query.limit) ? Number(req.query.limit) : undefined
52
- const open = isDefined(req.query.open) ? Boolean(req.query.open) : undefined
53
- const order = req.query.order === 'asc' ? 'asc' : 'desc'
54
- const options: ArchivistNextOptions = {
55
- limit, open, order, cursor,
56
- }
57
- const archivist = await getArchivist(node, archivistModuleIdentifier)
58
- const result = await archivist.next(options)
59
- res.status(200).json(result)
60
- })
61
- router.post('/next', async (req: Request<{}, {}, ArchivistNextOptions | undefined>, res) => {
62
- setRawResponseFormat(res)
63
- const options = req.body
64
- const archivist = await getArchivist(node, archivistModuleIdentifier)
65
- const result = await (isDefined(options) ? archivist.next(options) : archivist.next())
66
- res.status(200).json(result)
67
- })
68
-
69
- router.get('/get/:hash', async (req, res) => {
70
- setRawResponseFormat(res)
71
- const { hash: rawHash } = req.params
72
- const hash = asHash(rawHash)
73
- if (isDefined(hash)) {
74
- const archivist = await getArchivist(node, archivistModuleIdentifier)
75
- const [payload] = await archivist.get([hash])
76
- if (isAnyPayload(payload)) {
77
- res.json(payload)
78
- return
79
- }
80
- }
81
- res.status(400).send()
82
- })
83
-
84
- return router
85
- }
@@ -1 +0,0 @@
1
- export * from './addDataLakeRoutes.ts'
@@ -1,14 +0,0 @@
1
- import type { Express } from 'express'
2
- import { StatusCodes } from 'http-status-codes'
3
-
4
- export const addHealthRoutes = (app: Express) => {
5
- app.get('/healthz', (_req, res) => {
6
- res.status(StatusCodes.OK).send()
7
- })
8
- app.get('/livez', (_req, res) => {
9
- res.status(StatusCodes.OK).send()
10
- })
11
- app.get('/readyz', (_req, res) => {
12
- res.status(StatusCodes.OK).send()
13
- })
14
- }
@@ -1 +0,0 @@
1
- export * from './addHealthRoutes.ts'
@@ -1,3 +0,0 @@
1
- export * from './address/index.ts'
2
- export * from './addRoutes.ts'
3
- export * from './healthz/index.ts'