Skip to content

Commit

Permalink
Merge pull request rsksmart#3 from rsksmart/new_BTC_provider
Browse files Browse the repository at this point in the history
New btc info provider
  • Loading branch information
aeidelman authored Nov 13, 2020
2 parents fff0642 + 6c1dabb commit 08e9e23
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ const usePrimaryBtcHashrateProvider = require('./lib/utils/config').usePrimaryBt
const btcHashrateUpdater = setInterval(() =>
{
if(usePrimaryBtcHashrateProvider) {
Nodes.updateBtcHashrate('https://api.blockchain.info/stats');
Nodes.updateBtcHashrate('https://sochain.com/api/v2/get_info/BTC');
} else {
Nodes.updateBtcHashrateFromBackUp('https://chain.so/api/v2/get_info/BTC');
Nodes.updateBtcHashrateFromBackUp('https://api.blockchain.info/stats');
}
}, 1000*10);

Expand Down
5 changes: 5 additions & 0 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
42 changes: 34 additions & 8 deletions lib/history.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -700,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("hash_rate")){

if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){
return;
}

const oldBtcHashrate = this._btcHashrate;
const btcHashrateGiga = parsedData.hash_rate;
this._btcHashrate = btcHashrateGiga * 1000000000;

this._btcHashrate = parsedData.data.hashrate
// trigger client update
if(oldBtcHashrate !== this._btcHashrate) {
this.getCharts();
Expand All @@ -730,16 +729,43 @@ 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'){
if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){
return;
}

const oldBtcHashrate = this._btcHashrate;
const btcHashrateGiga = parsedData.hash_rate;
this._btcHashrate = btcHashrateGiga * 1000000000;

// trigger client update
if(oldBtcHashrate !== this._btcHashrate) {
this.getCharts();
}
});

if (!parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){
}).on("error", (err) => {
console.error("Error while retrieving BTC hash rate: " + err.message);
});
}

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;
this._btcHashrate = parsedData.data.hashrate;
const btcHashrateExa = parsedData.data.hashrate;
this._btcHashrate = btcHashrateExa * 1000000000000000000;

// trigger client update
if(oldBtcHashrate !== this._btcHashrate) {
Expand Down

0 comments on commit 08e9e23

Please sign in to comment.