-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-worker-info.js
55 lines (48 loc) · 1.82 KB
/
get-worker-info.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
require('dotenv').config()
const { options, OnChainRegistry } = require('@phala/sdk')
const { ApiPromise, WsProvider } = require('@polkadot/api')
const argv = require('arg')({
'--ws': String,
'--clusterId': String,
'--worker': String,
'--pruntimeURL': String
})
const locale = new Intl.NumberFormat('en-US')
async function main() {
const ws = argv['--ws'] || process.env.ENDPOINT
if (!ws) {
throw new Error('No ws endpoint specified')
}
const workerId = argv['--worker']
const pruntimeURL = argv['--pruntimeURL']
if (!workerId && !pruntimeURL) {
throw new Error('No worker specified: you need specified either --worker or --pruntimeURL')
}
const connectionOptions = {}
if (workerId) {
connectionOptions.workerId = workerId
} else if (pruntimeURL) {
connectionOptions.pruntimeURL = pruntimeURL
}
if (argv['--clusterId']) {
connectionOptions.clusterId = argv['--clusterId']
}
const apiPromise = await ApiPromise.create(options({
provider: new WsProvider(ws),
noInitWarn: true
}))
// If will throws error when worker not found in the cluster.
const registry = await OnChainRegistry.create(apiPromise, connectionOptions)
console.log('Connected via', ws)
console.log('Cluster ID:', registry.clusterId)
console.log('Worker ID:', registry.remotePubkey)
console.log('Worker Endpoint:', registry.pruntimeURL)
const info = await registry.phactory.getInfo({})
console.log(info)
console.log('---')
const lastBlockHeight = await apiPromise.derive.chain.bestNumberFinalized()
console.log('Best Finalized Num:\t', locale.format(lastBlockHeight))
console.log('Pruntime block Num:\t', locale.format(info.blocknum))
console.log('Pruntime Header Num:\t', locale.format(info.headernum))
}
main().catch(console.error).finally(() => process.exit()) // eslint-disable-line no-process-exit