From dac52e0b957c4445d30ec3fb7822457b4e308321 Mon Sep 17 00:00:00 2001 From: edgardo Date: Wed, 11 Nov 2020 22:59:54 -0300 Subject: [PATCH 1/4] New btc info provider --- app.js | 2 +- lib/history.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 5985acb4..d5dc7953 100644 --- a/app.js +++ b/app.js @@ -413,7 +413,7 @@ const usePrimaryBtcHashrateProvider = require('./lib/utils/config').usePrimaryBt const btcHashrateUpdater = setInterval(() => { if(usePrimaryBtcHashrateProvider) { - Nodes.updateBtcHashrate('https://api.blockchain.info/stats'); + Nodes.updateBtcHashrate('http://35.202.2.222:5000/blockchain/hashrate/');//('https://api.blockchain.info/stats'); } else { Nodes.updateBtcHashrateFromBackUp('https://chain.so/api/v2/get_info/BTC'); } diff --git a/lib/history.js b/lib/history.js index 4af95899..c045f266 100644 --- a/lib/history.js +++ b/lib/history.js @@ -1,6 +1,7 @@ var _ = require('lodash'); var d3 = require('d3'); const https = require('https'); +const http = require('http'); const logger = require('./utils/logger'); var MAX_HISTORY = 2000; @@ -691,7 +692,7 @@ function tryParseJSON (jsonString, customErrorMessage){ History.prototype.updateBtcHashrateFromExternalSource = function(source) { - https.get(source, (resp) => { + http.get(source, (resp) => { let data = ''; resp.on('data', (chunk) => { @@ -700,13 +701,16 @@ History.prototype.updateBtcHashrateFromExternalSource = function(source) resp.on('end', () => { const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); - if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ + // if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ + // return; + // } + if (!parsedData || !parsedData.data.hasOwnProperty("hashrate")){ return; } const oldBtcHashrate = this._btcHashrate; - const btcHashrateGiga = parsedData.hash_rate; - this._btcHashrate = btcHashrateGiga * 1000000000; + const btcHashrateExa = parsedData.data.hashrate; + this._btcHashrate = btcHashrateExa * 1000000000000000000; // trigger client update if(oldBtcHashrate !== this._btcHashrate) { From ce776bc439cd93863d2f62e53b034be12d1d39e3 Mon Sep 17 00:00:00 2001 From: edgardo Date: Wed, 11 Nov 2020 23:34:28 -0300 Subject: [PATCH 2/4] changed backup to blockchain.info --- app.js | 4 ++-- lib/history.js | 11 ++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index d5dc7953..3664f883 100644 --- a/app.js +++ b/app.js @@ -413,9 +413,9 @@ const usePrimaryBtcHashrateProvider = require('./lib/utils/config').usePrimaryBt const btcHashrateUpdater = setInterval(() => { if(usePrimaryBtcHashrateProvider) { - Nodes.updateBtcHashrate('http://35.202.2.222:5000/blockchain/hashrate/');//('https://api.blockchain.info/stats'); + Nodes.updateBtcHashrate('http://35.202.2.222:5000/blockchain/hashrate/'); } else { - Nodes.updateBtcHashrateFromBackUp('https://chain.so/api/v2/get_info/BTC'); + Nodes.updateBtcHashrateFromBackUp('https://api.blockchain.info/stats'); } }, 1000*10); diff --git a/lib/history.js b/lib/history.js index c045f266..f96bbdbf 100644 --- a/lib/history.js +++ b/lib/history.js @@ -704,7 +704,7 @@ History.prototype.updateBtcHashrateFromExternalSource = function(source) // if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ // return; // } - if (!parsedData || !parsedData.data.hasOwnProperty("hashrate")){ + if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ return; } @@ -734,16 +734,13 @@ History.prototype.updateBtcHashrateFromBackUpExternalSource = function(source) resp.on('end', () => { const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); - if (!parsedData || !parsedData.hasOwnProperty("status") || parsedData.status !== 'success'){ - return; - } - - if (!parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ + if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ return; } const oldBtcHashrate = this._btcHashrate; - this._btcHashrate = parsedData.data.hashrate; + const btcHashrateGiga = parsedData.hash_rate; + this._btcHashrate = btcHashrateGiga * 1000000000; // trigger client update if(oldBtcHashrate !== this._btcHashrate) { From 5a4c7b226c5ea605e1a6a051c815d48af2c1df46 Mon Sep 17 00:00:00 2001 From: edgardo Date: Wed, 11 Nov 2020 23:41:16 -0300 Subject: [PATCH 3/4] remove unnecesary comment --- lib/history.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/history.js b/lib/history.js index f96bbdbf..d420052d 100644 --- a/lib/history.js +++ b/lib/history.js @@ -701,9 +701,6 @@ History.prototype.updateBtcHashrateFromExternalSource = function(source) resp.on('end', () => { const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); - // if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ - // return; - // } if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ return; } From 6c1dabbce86599bf041f1f1a45579177f03d8553 Mon Sep 17 00:00:00 2001 From: edgardo Date: Thu, 12 Nov 2020 16:24:31 -0300 Subject: [PATCH 4/4] Changed btc provider default to sochain --- app.js | 2 +- lib/collection.js | 5 +++++ lib/history.js | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 3664f883..57e77ab3 100644 --- a/app.js +++ b/app.js @@ -413,7 +413,7 @@ const usePrimaryBtcHashrateProvider = require('./lib/utils/config').usePrimaryBt const btcHashrateUpdater = setInterval(() => { if(usePrimaryBtcHashrateProvider) { - Nodes.updateBtcHashrate('http://35.202.2.222:5000/blockchain/hashrate/'); + Nodes.updateBtcHashrate('https://sochain.com/api/v2/get_info/BTC'); } else { Nodes.updateBtcHashrateFromBackUp('https://api.blockchain.info/stats'); } diff --git a/lib/collection.js b/lib/collection.js index e153c60e..5e78e932 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -343,4 +343,9 @@ Collection.prototype.updateBtcHashrateFromBackUp = function(source) this._blockchain.updateBtcHashrateFromBackUpExternalSource(source); } +Collection.prototype.updateBtcHashrateFromAlternativeBackUp = function(source) +{ + this._blockchain.updateBtcHashrateFromAlternativeBackUpExternalSource(source); +} + module.exports = Collection; diff --git a/lib/history.js b/lib/history.js index d420052d..14fd9aee 100644 --- a/lib/history.js +++ b/lib/history.js @@ -692,7 +692,7 @@ function tryParseJSON (jsonString, customErrorMessage){ History.prototype.updateBtcHashrateFromExternalSource = function(source) { - http.get(source, (resp) => { + https.get(source, (resp) => { let data = ''; resp.on('data', (chunk) => { @@ -701,14 +701,12 @@ History.prototype.updateBtcHashrateFromExternalSource = function(source) resp.on('end', () => { const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); + if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ return; } - const oldBtcHashrate = this._btcHashrate; - const btcHashrateExa = parsedData.data.hashrate; - this._btcHashrate = btcHashrateExa * 1000000000000000000; - + this._btcHashrate = parsedData.data.hashrate // trigger client update if(oldBtcHashrate !== this._btcHashrate) { this.getCharts(); @@ -734,7 +732,7 @@ History.prototype.updateBtcHashrateFromBackUpExternalSource = function(source) if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ return; } - + const oldBtcHashrate = this._btcHashrate; const btcHashrateGiga = parsedData.hash_rate; this._btcHashrate = btcHashrateGiga * 1000000000; @@ -750,4 +748,34 @@ History.prototype.updateBtcHashrateFromBackUpExternalSource = function(source) }); } +History.prototype.updateBtcHashrateFromAlternativeExternalSource = function(source) +{ + http.get(source, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + + resp.on('end', () => { + const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); + if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ + return; + } + + const oldBtcHashrate = this._btcHashrate; + const btcHashrateExa = parsedData.data.hashrate; + this._btcHashrate = btcHashrateExa * 1000000000000000000; + + // trigger client update + if(oldBtcHashrate !== this._btcHashrate) { + this.getCharts(); + } + }); + + }).on("error", (err) => { + console.error("Error while retrieving BTC hash rate: " + err.message); + }); +} + module.exports = History;