-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstatusHandler.ts
More file actions
182 lines (171 loc) · 5.47 KB
/
statusHandler.ts
File metadata and controls
182 lines (171 loc) · 5.47 KB
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
import os from 'os'
import { LOG_LEVELS_STR, GENERIC_EMOJIS } from '../../../utils/logging/Logger.js'
import {
OceanNodeStatus,
OceanNodeProvider,
OceanNodeIndexer,
StorageTypes,
OceanNodeConfig
} from '../../../@types/OceanNode.js'
import { CORE_LOGGER } from '../../../utils/logging/common.js'
import { OceanNode } from '../../../OceanNode.js'
import { typesenseSchemas } from '../../database/TypesenseSchemas.js'
import { SupportedNetwork } from '../../../@types/blockchain.js'
import { getAdminAddresses } from '../../../utils/auth.js'
import HumanHasher from 'humanhash'
import { getPackageVersion } from '../../../utils/version.js'
function getSupportedStorageTypes(config: OceanNodeConfig): StorageTypes {
return {
url: true,
arwave: !!config.arweaveGateway,
ipfs: !!config.ipfsGateway
}
}
// platform information
const platformInfo = {
cpus: os.cpus().length,
freemem: os.freemem(),
totalmem: os.totalmem(),
loadavg: os.loadavg(),
arch: os.arch(),
machine: os.machine(),
platform: os.platform(),
osType: os.type(),
node: process.version
}
function getProviderInfo(config: OceanNodeConfig): OceanNodeProvider[] {
const providers: OceanNodeProvider[] = []
for (const [key, supportedNetwork] of Object.entries(config.supportedNetworks)) {
const provider: OceanNodeProvider = {
chainId: key,
network: supportedNetwork.network
}
providers.push(provider)
}
return providers
}
async function getIndexerInfo(
oceanNode: OceanNode,
config: OceanNodeConfig
): Promise<OceanNodeIndexer[]> {
const indexerNetworks: OceanNodeIndexer[] = []
if (config.indexingNetworks) {
for (const [key, indexedNetwork] of Object.entries(config.indexingNetworks)) {
if (config.hasIndexer) {
const blockNr = await getIndexerBlockInfo(oceanNode, indexedNetwork)
const indexer: OceanNodeIndexer = {
chainId: key,
network: indexedNetwork.network,
block: blockNr
}
indexerNetworks.push(indexer)
}
}
}
return indexerNetworks
}
async function getIndexerBlockInfo(
oceanNode: OceanNode,
supportedNetwork: SupportedNetwork
): Promise<string> {
let blockNr = '0'
try {
const database = oceanNode.getDatabase()
if (!database || !database.indexer) {
CORE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_WARN,
`Indexer database is not available for network ${supportedNetwork.network}`
)
return blockNr
}
const { lastIndexedBlock } = await database.indexer.retrieve(supportedNetwork.chainId)
blockNr = lastIndexedBlock.toString()
} catch (error) {
CORE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`Error fetching last indexed block for network ${supportedNetwork.network}`
)
}
return blockNr
}
let nodeStatus: OceanNodeStatus = null
export async function status(
oceanNode: OceanNode,
nodeId?: string,
detailed: boolean = false
): Promise<OceanNodeStatus> {
CORE_LOGGER.logMessage('Command status started execution...', true)
if (!oceanNode) {
CORE_LOGGER.logMessageWithEmoji(
'Node object not found. Cannot proceed with status command.',
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return
}
const config = oceanNode.getConfig()
// no previous status?
if (!nodeStatus) {
const publicKey = oceanNode.getKeyManager().getPublicKey()
const publicKeyHex = Buffer.from(publicKey).toString('hex')
nodeStatus = {
id:
nodeId && nodeId !== undefined
? nodeId
: oceanNode.getKeyManager().getPeerId().toString(), // get current node ID
publicKey: publicKeyHex,
friendlyName: new HumanHasher().humanize(publicKeyHex),
address: oceanNode.getKeyManager().getEthAddress(),
version: getPackageVersion(),
http: config.hasHttp,
p2p: config.hasP2P,
provider: [],
indexer: [],
escrowAddress: {},
supportedStorage: getSupportedStorageTypes(config),
// uptime: process.uptime(),
platform: platformInfo,
codeHash: config.codeHash,
allowedAdmins: await getAdminAddresses()
}
}
// need to update at least block info if available
if (config.supportedNetworks) {
nodeStatus.provider = getProviderInfo(config)
nodeStatus.indexer = await getIndexerInfo(oceanNode, config)
for (const chain of Object.keys(config.supportedNetworks)) {
nodeStatus.escrowAddress[chain] = oceanNode.escrow.getEscrowContractAddressForChain(
parseInt(chain)
)
}
}
// only these 2 might change between requests
nodeStatus.platform.freemem = os.freemem()
nodeStatus.platform.loadavg = os.loadavg()
nodeStatus.uptime = process.uptime()
// depends on request
if (detailed) {
nodeStatus.c2dClusters = []
try {
const engines = await oceanNode.getC2DEngines().getAllEngines()
for (const engine of engines) {
const type = await engine.getC2DType()
nodeStatus.c2dClusters.push({
type,
hash: await engine.getC2DConfig().hash,
environments: await engine.getComputeEnvironments()
})
}
} catch (error) {
CORE_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error getting c2d clusters: ${error}`)
}
nodeStatus.supportedSchemas = typesenseSchemas.ddoSchemas
}
if (config.persistentStorage) {
nodeStatus.persistentStorage = {}
if (config.persistentStorage.accessLists)
nodeStatus.persistentStorage.accessLists = config.persistentStorage.accessLists
}
return nodeStatus
}