Merge pull request #82 from Snipa22/develop

Develop
This commit is contained in:
Snipa22
2017-06-29 10:29:59 -07:00
committed by GitHub
3 changed files with 49 additions and 47 deletions
+36 -40
View File
@@ -181,10 +181,10 @@ function Database(){
this.storeBulkShares = function(shareObject) {
let cachedData = {
global_stats: global.database.getCache('global_stats'),
pplns_stats: global.database.getCache('pplns_stats'),
pps_stats: global.database.getCache('pps_stats'),
solo_stats: global.database.getCache('solo_stats')
global_stats: {totalHashes: 0, roundHashes: 0},
pplns_stats: {totalHashes: 0, roundHashes: 0},
pps_stats: {totalHashes: 0, roundHashes: 0},
solo_stats: {totalHashes: 0, roundHashes: 0}
};
let shares = {}; // Shares keyed by blockID
let shareCount = 0;
@@ -202,13 +202,6 @@ function Database(){
minerID = minerID + '.' + share.paymentID;
}
let minerIDWithIdentifier = minerID + "_" + share.identifier;
// this.incrementCacheData('global_stats', [{location: 'totalHashes', value: share.shares}, {location: 'roundHashes', value: share.shares}]);
if (!cachedData.global_stats.hasOwnProperty('totalHashes') || cachedData.global_stats.totalHashes === null) {
cachedData.global_stats.totalHashes = 0;
}
if (!cachedData.global_stats.hasOwnProperty('roundHashes') || cachedData.global_stats.roundHashes === null || share.foundBlock) {
cachedData.global_stats.roundHashes = 0;
}
cachedData.global_stats.totalHashes += share.shares;
cachedData.global_stats.roundHashes += share.shares;
let stats_type = 'pplns_stats';
@@ -223,39 +216,13 @@ function Database(){
stats_type = 'solo_stats';
break;
}
if (!cachedData[stats_type].hasOwnProperty('totalHashes') || cachedData[stats_type].totalHashes === null) {
cachedData[stats_type].totalHashes = 0;
}
if (!cachedData[stats_type].hasOwnProperty('roundHashes') || cachedData[stats_type].roundHashes === null || share.foundBlock) {
cachedData[stats_type].roundHashes = 0;
}
cachedData[stats_type].totalHashes += share.shares;
cachedData[stats_type].roundHashes += share.shares;
if (!cachedData.hasOwnProperty(minerID)) {
let minerCache = global.database.getCache(minerID);
if (minerCache === false) {
minerCache = {totalHashes: 0, goodShares: 0};
}
cachedData[minerID] = minerCache;
cachedData[minerID] = {totalHashes: 0, goodShares: 0};
}
if (!cachedData.hasOwnProperty(minerIDWithIdentifier)) {
let minerCache = global.database.getCache(minerIDWithIdentifier);
if (minerCache === false) {
minerCache = {totalHashes: 0, goodShares: 0};
}
cachedData[minerIDWithIdentifier] = minerCache;
}
if (!cachedData[minerIDWithIdentifier].hasOwnProperty('totalHashes') || cachedData[minerIDWithIdentifier].totalHashes === null) {
cachedData[minerIDWithIdentifier].totalHashes = 0;
}
if (!cachedData[minerIDWithIdentifier].hasOwnProperty('goodShares') || cachedData[minerIDWithIdentifier].goodShares === null) {
cachedData[minerIDWithIdentifier].goodShares = 0;
}
if (!cachedData[minerID].hasOwnProperty('totalHashes') || cachedData[minerID].totalHashes === null) {
cachedData[minerID].totalHashes = 0;
}
if (!cachedData[minerID].hasOwnProperty('goodShares') || cachedData[minerID].goodShares === null) {
cachedData[minerID].goodShares = 0;
cachedData[minerIDWithIdentifier] = {totalHashes: 0, goodShares: 0};
}
cachedData[minerIDWithIdentifier].totalHashes += share.shares;
cachedData[minerID].totalHashes += share.shares;
@@ -266,10 +233,35 @@ function Database(){
}
if(shareObject.length === shareCount){
// Perform insert and return
let inCache = 'notNull';
while (inCache !== null){
let txn_ro = global.database.env.beginTxn({readOnly: true});
inCache = txn_ro.getString(global.database.cacheDB, 'cacheUpdate');
txn_ro.abort();
}
let txn = global.database.env.beginTxn();
for (let key in cachedData){
if (cachedData.hasOwnProperty(key)){
txn.putString(global.database.cacheDB, key, JSON.stringify(cachedData[key]));
let cacheStore = txn.getString(global.database.cacheDB, key);
if (cacheStore === null){
txn.putString(global.database.cacheDB, key, JSON.stringify(cachedData[key]));
} else {
let json_cache = JSON.parse(cacheStore);
if (json_cache.hasOwnProperty('totalHashes')){
json_cache.totalHashes += cachedData[key].totalHashes;
json_cache.goodShares += cachedData[key].goodShares;
} else {
json_cache.totalHashes = cachedData[key].totalHashes;
json_cache.goodShares = cachedData[key].goodShares;
}
if (cachedData[key].hasOwnProperty('roundHashes') && json_cache.hasOwnProperty('roundHashes')){
json_cache.roundHashes += cachedData[key].roundHashes;
} else {
json_cache.roundHashes = cachedData[key].roundHashes;
}
txn.putString(global.database.cacheDB, key, JSON.stringify(json_cache));
}
}
}
let blocksSeen = 0;
@@ -511,11 +503,15 @@ function Database(){
this.bulkSetCache = function(cacheUpdates){
let txn = this.env.beginTxn();
txn.putString(this.cacheDB, 'cacheUpdate', 'cacheUpdate');
txn.commit();
txn = this.env.beginTxn();
for (let key in cacheUpdates){
if (cacheUpdates.hasOwnProperty(key)){
txn.putString(this.cacheDB, key, JSON.stringify(cacheUpdates[key]));
}
}
txn.del(this.cacheDB, 'cacheUpdate');
txn.commit();
};
+4
View File
@@ -292,6 +292,10 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
this.error = "Exchange addresses need payment IDs";
this.valid_miner = false;
}
if (!activeBlockTemplate){
this.error = "No active block template";
this.valid_miner = false;
}
this.id = id;
this.ipAddress = ipAddress;
+9 -7
View File
@@ -71,16 +71,18 @@ app.post('/leafApi', function (req, res) {
}
});
function storeShares(){
if (Object.keys(shareData).length > 0){
global.database.storeBulkShares(shareData);
shareData = [];
}
setTimeout(storeShares, 1000);
}
if (cluster.isMaster) {
let numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
setInterval(function(){
if (Object.keys(shareData).length > 0){
global.database.storeBulkShares(shareData);
shareData = [];
}
}, 1000);
storeShares();
for (let i = 0; i < numWorkers; i++) {
let worker = cluster.fork();