From 1d7017829460fef7764e6b5ed0eab5d3fbb43ff6 Mon Sep 17 00:00:00 2001 From: Felix Brucker Date: Sat, 23 Sep 2023 00:46:42 +0530 Subject: [PATCH] Add support for retrieving the harvester and farmer node id --- CHANGELOG.md | 5 +++++ lib/chia-config.js | 21 +++++++++++++++++++++ lib/extensions/extensions.js | 1 + lib/extensions/string-extensions.js | 7 +++++++ lib/service/stats-collection.js | 18 ++++++++++++++++++ lib/util.js | 6 ++++++ main.js | 2 ++ package.json | 2 +- 8 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lib/extensions/extensions.js create mode 100644 lib/extensions/string-extensions.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 53992d4..f8591bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +1.19.0 / 2023-09-23 +================== + +* Adds support for retrieving the harvester and farmer node id. + 1.18.1 / 2023-09-05 ================== diff --git a/lib/chia-config.js b/lib/chia-config.js index 6344b05..eee8ca1 100644 --- a/lib/chia-config.js +++ b/lib/chia-config.js @@ -1,6 +1,7 @@ const { promises: fs } = require('fs'); const YAML = require('js-yaml'); const { join } = require('path'); +const {calculateNodeIdForCert} = require('./util') class ChiaConfig { constructor(configDirectory) { @@ -19,6 +20,26 @@ class ChiaConfig { return fs.readFile(filePath, 'utf8'); } + async getHarvesterSslCertFile() { + const filePath = join(this.configDirectory, this.config.harvester.ssl.private_crt) + + return fs.readFile(filePath, 'utf8') + } + + async getHarvesterNodeId() { + return calculateNodeIdForCert(await this.getHarvesterSslCertFile()) + } + + async getFarmerSslCertFile() { + const filePath = join(this.configDirectory, this.config.farmer.ssl.private_crt) + + return fs.readFile(filePath, 'utf8') + } + + async getFarmerNodeId() { + return calculateNodeIdForCert(await this.getFarmerSslCertFile()) + } + get daemonAddress() { return `${this.config.self_hostname}:${this.config.daemon_port}`; } diff --git a/lib/extensions/extensions.js b/lib/extensions/extensions.js new file mode 100644 index 0000000..e7f5c38 --- /dev/null +++ b/lib/extensions/extensions.js @@ -0,0 +1 @@ +require('./string-extensions') diff --git a/lib/extensions/string-extensions.js b/lib/extensions/string-extensions.js new file mode 100644 index 0000000..50f7f6f --- /dev/null +++ b/lib/extensions/string-extensions.js @@ -0,0 +1,7 @@ +String.prototype.ensureHexPrefix = function() { + return this.startsWith('0x') ? this : `0x${this}` +} + +String.prototype.stripHexPrefix = function() { + return this.startsWith('0x') ? this.slice(2) : this +} diff --git a/lib/service/stats-collection.js b/lib/service/stats-collection.js index a57c8d8..e2d4a19 100644 --- a/lib/service/stats-collection.js +++ b/lib/service/stats-collection.js @@ -289,6 +289,24 @@ class StatsCollection { await this.plotterApiClient.init(); } await this.tryUntilSucceeded(this.updateRunningServices.bind(this)); + await this.tryUntilSucceeded(async () => { + if (!this.isServiceRunning.get(harvesterService)) { + return + } + const harvesterStats = this.stats.has(harvesterService) ? this.stats.get(harvesterService) : {} + const harvesterId = await chiaConfig.getHarvesterNodeId() + harvesterStats.nodeId = harvesterId + await this.setStatsForService(harvesterService, harvesterStats, { nodeId: harvesterId }) + }) + await this.tryUntilSucceeded(async () => { + if (!this.isServiceRunning.get(farmerService)) { + return + } + const farmerStats = this.stats.has(farmerService) ? this.stats.get(farmerService) : {} + const farmerId = await chiaConfig.getFarmerNodeId() + farmerStats.nodeId = farmerId + await this.setStatsForService(farmerService, farmerStats, { nodeId: farmerId }) + }) await this.tryUntilSucceeded(this.updateStats.bind(this)); await this.tryUntilSucceeded(this.updateFullNodeStats.bind(this)); setInterval(async () => { diff --git a/lib/util.js b/lib/util.js index fcacb79..0c1a00a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,5 +1,6 @@ const moment = require('moment'); const BigNumber = require('bignumber.js') +const {X509Certificate} = require('crypto') const plottingTimestampRegex = /([0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\.[0-9]+)/ const FINISHED_LOG_LINES_128_BUCKETS = 2626 @@ -49,6 +50,11 @@ const util = { .multipliedBy((new BigNumber(2)).exponentiatedBy(kSize - 1)) .multipliedBy(getActualSpaceConstantFactor(kSize)) }, + calculateNodeIdForCert(cert) { + const x509Cert = new X509Certificate(cert) + + return x509Cert.fingerprint256.replaceAll(':', '').toLowerCase().ensureHexPrefix() + }, } module.exports = util diff --git a/main.js b/main.js index 6c25141..a8043e7 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,7 @@ #!/usr/bin/env node +require('./lib/extensions/extensions') + const config = require('./lib/service/config'); const firstRunWizard = require('./lib/service/first-run-wizard'); const statsCollection = require('./lib/service/stats-collection'); diff --git a/package.json b/package.json index c4de612..1bb2d16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chia-dashboard-satellite", - "version": "1.18.1", + "version": "1.19.0", "repository": "https://github.com/felixbrucker/chia-dashboard-satellite.git", "bugs": "https://github.com/felixbrucker/chia-dashboard-satellite/issues", "license": "GPL-3.0",