Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
simonzg committed Mar 22, 2023
1 parent 9abdfc6 commit 9bf6aef
Showing 1 changed file with 94 additions and 31 deletions.
125 changes: 94 additions & 31 deletions libs/rewardProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var meterify = require("meterify").meterify;
var Web3 = require("web3");
const { default: BigNumber } = require("bignumber.js");

const ADDR_PATTERN = RegExp("^0x[0-9a-fA-F]{40}$");

module.exports = function (logger) {
var poolConfigs = JSON.parse(process.env.pools);

Expand Down Expand Up @@ -251,14 +253,24 @@ function SetupForPool(logger, poolOptions, setupFinished) {
},
0
);

totalShares += totalSharesInRound;
logger.debug(
logSystem,
logComponent,
"Total shares in round " +
round.height +
": " +
totalSharesInRound
);

for (var workerAddress in workerShares) {
if (!(workerAddress in pendingShares)) {
pendingShares[workerAddress] = 0;
}
pendingShares[workerAddress] +=
workerShares[workerAddress];
pendingShares[workerAddress] += Number(
workerShares[workerAddress]
);
}
break;
}
Expand Down Expand Up @@ -286,9 +298,18 @@ function SetupForPool(logger, poolOptions, setupFinished) {
logComponent,
"Loading MTR balance and calculate the actual reward amount for each worker"
);
console.log("get energy for beneficiary: ", beneficiary);
web3.eth.getEnergy(beneficiary, function (err, bal) {
let pendingReward = new BigNumber(bal);
if (err) {
console.log("ERROR: ", err);
console.log("err Bal: ", bal);
return callback(err);
}
console.log("balance: ", bal);
if (Number(bal) == NaN) {
callback("could not load balance");
}
if (beneficiary in workers) {
const selfBalance = new BigNumber(
workers[beneficiary].balance || 0
Expand Down Expand Up @@ -320,30 +341,55 @@ function SetupForPool(logger, poolOptions, setupFinished) {
"Pool has pending reward of " + pendingReward.toFixed(0)
);

for (let w in workers) {
if (w in pendingShares) {
if (!workers[w].reward) {
workers[w].reward = new BigNumber(0);
} else {
console.log("pendingShares: ", pendingShares);
for (let w in pendingShares) {
const share = new BigNumber(pendingShares[w]).toNumber();
w = w.toLowerCase();
if (w == "time") {
continue;
}
if (!(w in workers)) {
workers[w] = {
reward: new BigNumber(0),
balance: new BigNumber(0),
};
} else {
console.log("reward: ", workers[w].reward);
console.log("balance: ", workers[w].balance);
if (workers[w].reward) {
workers[w].reward = new BigNumber(workers[w].reward);
}
if (!workers[w].balance) {
workers[w].balance = new BigNumber(0);
} else {
workers[w].balance = new BigNumber(workers[w].balance);
workers[w].reward = new BigNumber(0);
}

const workerReward = pendingReward
.times(share)
.div(totalShares);
if (w.match(ADDR_PATTERN)) {
workers[w].reward = workerReward.add(workers[w].reward);
workers[w].issue = true;
if (workers[w].balance) {
workers[w].balance = new BigNumber(workers[w].balance);
} else {
workers[w].balance = workerReward.add(workers[w].balance);
workers[w].issue = false;
workers[w].balance = new BigNumber(0);
}
}

console.log("PENDING REWARD: ", pendingReward.toFixed(0));
console.log("SHARE:", share);
console.log("TOTAL SHARES: ", totalShares);
const workerReward = pendingReward.times(share).div(totalShares);
console.log(
"worker: ",
w,
"reward amount: ",
workerReward.toNumber()
);
console.log(`workers[w].reward: ${workers[w].reward}`);
console.log(`workers[w].balance: ${workers[w].balance}`);
if (w.match(ADDR_PATTERN)) {
workers[w].reward = workerReward.plus(workers[w].reward);
workers[w].issue = true;
} else {
workers[w].balance = workerReward.plus(workers[w].balance);
workers[w].issue = false;
}
console.log(
`worker ${w}, balance: ${workers[w].balance}, reward: ${workers[w].reward}`
);
}

callback(null, workers, rounds);
Expand All @@ -359,8 +405,8 @@ function SetupForPool(logger, poolOptions, setupFinished) {
worker.reward = worker.reward || 0;
if (worker.issue) {
addressAmounts[w] = new BigNumber(0)
.add(worker.balance)
.add(worker.reward);
.plus(worker.balance)
.plus(worker.reward);
}
}

Expand All @@ -370,9 +416,17 @@ function SetupForPool(logger, poolOptions, setupFinished) {
}

logger.debug(logSystem, logComponent, "Prepare to send reward tx");
web3.eth.getBlockNum(function (err, blockNum) {
web3.eth.getBlockNumber(function (err, blockNum) {
if (err) {
console.log("getBlockNumber erro: ", err);
return callback(err);
}
console.log("best num:", blockNum);
web3.eth.getBlock(blockNum, function (err, best) {
if (err) {
console.log("getBlock erro: ", err);
return callback(err);
}
console.log(best);
const blockRef = best.id.substr(0, 18);
let chainTag = processingConfig.chainTag; // chainTag for testnet
Expand All @@ -381,24 +435,29 @@ function SetupForPool(logger, poolOptions, setupFinished) {
let clauses = [];
for (const addr in addressAmounts) {
const amount = addressAmounts[addr];
console.log(`pay to ${addr} with ${amount}`);
clauses.push({ to: addr, value: amount.toFixed(0) });
console.log(`pay to ${addr} with ${amount.toFixed(0)}`);
clauses.push({
to: addr,
value: amount.toFixed(0),
token: 0,
data: "0x",
});
}
const baseGas = 5000 + clauses.length * 16000; // fixed value
console.log("base gas: ", baseGas);
console.log("data gas: ", dataGas);
let txObj = {
chainTag,
blockRef, // the first 8 bytes of latest block
expiration: 48, // blockRefHeight + expiration is the height for tx expire
expiration: 64, // blockRefHeight + expiration is the height for tx expire
clauses,
gasPriceCoef: 0,
gas: baseGas + dataGas,
dependsOn: null,
nonce: getRandomInt(Number.MAX_SAFE_INTEGER), // random number
nonce: 0, // random number
};
let tx = new Transaction(txObj);
const pkBuffer = Buffer.from(pk.replace("0x", ""), "hex");
const pkBuffer = Buffer.from(poolPK.replace("0x", ""), "hex");
const signingHash = cry.blake2b256(tx.encode());
logger.debug(logSystem, logComponent, "Signed reward tx");
tx.signature = cry.secp256k1.sign(signingHash, pkBuffer);
Expand All @@ -407,6 +466,10 @@ function SetupForPool(logger, poolOptions, setupFinished) {
const rawTx = "0x" + raw.toString("hex");
logger.debug(logSystem, logComponent, "Sending out reward tx");
web3.eth.sendSignedTransaction(rawTx, function (err) {
if (err) {
console.log("sendSignedTransaction error:", err);
return callback(err);
}
callback(null, workers, rounds);
});
});
Expand Down Expand Up @@ -464,7 +527,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
trySend(0);
},
function (workers, rounds, callback) {
var totalPaid = 0;
var totalPaid = new BigNumber(0);

var balanceUpdateCommands = [];
var workerPayoutsCommand = [];
Expand All @@ -488,7 +551,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
"hset",
coin + ":balances",
w,
worker.balance.toNumber(),
worker.balance,
]);
}
}
Expand Down Expand Up @@ -549,7 +612,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
"hincrbyfloat",
coin + ":stats",
"totalPaid",
totalPaid,
totalPaid.toFixed(0),
]);
}

Expand Down

0 comments on commit 9bf6aef

Please sign in to comment.