Skip to content

Instantly share code, notes, and snippets.

@JPaulMora
Created February 7, 2019 18:09
Show Gist options
  • Save JPaulMora/70e398b0fe169b5e19c4aa5b0aa405f5 to your computer and use it in GitHub Desktop.
Save JPaulMora/70e398b0fe169b5e19c4aa5b0aa405f5 to your computer and use it in GitHub Desktop.
Script to manually count snipa's pool shares data from LMDB.
"use strict";
let mysql = require("promise-mysql");
let fs = require("fs");
const range = require("range");
const debug = require("debug")("blockManager");
const async = require("async");
let config = fs.readFileSync("./config.json");
let protobuf = require('protocol-buffers');
this.lmdb = require('node-lmdb');
let comms = require('./lib/local_comms');
global.support = require("./lib/support.js")();
global.config = JSON.parse(config);
global.mysql = mysql.createPool(global.config.mysql);
global.protos = protobuf(fs.readFileSync('./lib/data.proto'));
global.database = new comms();
global.database.initEnv();
global.mysql.query("SELECT * FROM config").then(function (rows) {
rows.forEach(function (row){
if (!global.config.hasOwnProperty(row.module)){
global.config[row.module] = {};
}
if (global.config[row.module].hasOwnProperty(row.item)){
return;
}
switch(row.item_type){
case 'int':
global.config[row.module][row.item] = parseInt(row.item_value);
break;
case 'bool':
global.config[row.module][row.item] = (row.item_value === "true");
break;
case 'string':
global.config[row.module][row.item] = row.item_value;
break;
case 'float':
global.config[row.module][row.item] = parseFloat(row.item_value);
break;
}
});
})
var block = {
"block_size": 27696,
"depth": 634,
"difficulty": 51445644103,
"hash": "9d5647542acadec491fdeb66b64486a00328fca210aed2160f5e9d1e23cd0de8",
"height": 1581594,
"major_version": 7,
"minor_version": 7,
"nonce": 22500,
"num_txes": 2,
"orphan_status": false,
"prev_hash": "c52a2bf3a180d6522a0f41ee006566b15325431b1e4113abfca5e66b5c650fb9",
"reward": 4551489302035,
"timestamp": 1527388681
};
function calculatePPLNSPayments(blockHeader) {
console.log("Performing PPLNS payout on block: " + blockHeader.height + " Block Value: " + blockHeader.reward);
let rewardTotal = blockHeader.reward;
let blockCheckHeight = blockHeader.height;
let totalPaid = 0;
let paymentData = {};
paymentData['9uW9S9Q'] = {
pool_type: 'fees',
payment_address: '9uW9S9Q',
payment_id: null,
bitcoin: 0,
amount: 0
};
async.doWhilst(function (callback) {
let txn = global.database.env.beginTxn({readOnly: true});
let cursor = new global.database.lmdb.Cursor(txn, global.database.shareDB);
for (let found = (cursor.goToRange(blockCheckHeight) === blockCheckHeight); found; found = cursor.goToNextDup()) {
cursor.getCurrentBinary(function (key, data) { // jshint ignore:line
let shareData;
try {
shareData = global.protos.Share.decode(data);
} catch (e) {
console.error(e);
return;
}
let blockDiff = blockHeader.difficulty;
let rewardTotal = blockHeader.reward;
if (shareData.poolType === global.protos.POOLTYPE.PPLNS) {
let userIdentifier = shareData.paymentAddress;
if (shareData.paymentID) {
userIdentifier = userIdentifier + "." + shareData.paymentID;
}
if (!(userIdentifier in paymentData)) {
paymentData[userIdentifier] = {
pool_type: 'pplns',
payment_address: shareData.paymentAddress,
payment_id: shareData.paymentID,
bitcoin: shareData.bitcoin,
amount: 0
};
}
let amountToPay = Math.floor((shareData.shares / (blockDiff*global.config.pplns.shareMulti)) * rewardTotal);
if (totalPaid + amountToPay > rewardTotal) {
amountToPay = rewardTotal - totalPaid;
}
totalPaid += amountToPay;
let feesToPay = Math.floor(amountToPay * (global.config.payout.pplnsFee / 100));
if (shareData.bitcoin === true) {
feesToPay += Math.floor(amountToPay * (global.config.payout.btcFee / 100));
}
let donations = 0
amountToPay -= feesToPay;
paymentData[userIdentifier].amount = paymentData[userIdentifier].amount + amountToPay;
paymentData['9uW9S9Q'].amount = paymentData['9uW9S9Q'].amount + feesToPay - donations;
}
});
}
cursor.close();
txn.abort();
setImmediate(callback, null, totalPaid);
}, function (totalPayment) {
blockCheckHeight = blockCheckHeight - 1;
debug("Decrementing the block chain check height to:" + blockCheckHeight);
if (totalPayment >= rewardTotal) {
debug("Loop 1: Total Payment: " + totalPayment + " Amount Paid: " + rewardTotal + " Amount Total: " + totalPaid);
return false;
} else {
debug("Loop 2: Total Payment: " + totalPayment + " Amount Paid: " + rewardTotal + " Amount Total: " + totalPaid);
return blockCheckHeight !== 0;
}
}, function (err) {
let totalPayments = 0;
Object.keys(paymentData).forEach(function (key) {
console.log(key);
totalPayments += paymentData[key].amount;
});
console.log(totalPayments)
console.log(paymentData)
console.log("PPLNS payout cycle complete on block: " + blockHeader.height + " Block Value: " + global.support.coinToDecimal(blockHeader.reward) + " Block Payouts: " + global.support.coinToDecimal(totalPayments) + " Payout Percentage: " + (totalPayments / blockHeader.reward) * 100 + "%");
});
}
calculatePPLNSPayments(block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment