2019-07-12 12:45:46 +00:00
|
|
|
/*
|
|
|
|
* @project: TERA
|
|
|
|
* @version: Development (beta)
|
|
|
|
* @license: MIT (not for evil)
|
|
|
|
* @copyright: Yuriy Ivanov (Vtools) 2017-2019 [progr76@gmail.com]
|
|
|
|
* Web: https://terafoundation.org
|
|
|
|
* Twitter: https://twitter.com/terafoundation
|
|
|
|
* Telegram: https://t.me/terafoundation
|
|
|
|
*/
|
|
|
|
|
2019-07-13 01:15:47 +00:00
|
|
|
global.PROCESS_NAME = "POW";
|
|
|
|
global.POWPROCESS = 1;
|
|
|
|
require("../core/library");
|
|
|
|
require("../core/crypto-library");
|
|
|
|
require("../core/terahashmining");
|
2019-07-12 12:45:46 +00:00
|
|
|
var PROCESS = process;
|
2019-07-13 01:15:47 +00:00
|
|
|
if(process.send && !global.DEBUGPROCESS)
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
process.send({cmd:"online", message:"OK"});
|
|
|
|
}
|
|
|
|
else
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
PROCESS = global.DEBUGPROCESS;
|
|
|
|
}
|
|
|
|
var LastAlive = Date.now();
|
|
|
|
setInterval(CheckAlive, 1000);
|
|
|
|
var idInterval = undefined;
|
|
|
|
var Block = {};
|
|
|
|
PROCESS.on('message', function (msg)
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
LastAlive = Date.now();
|
|
|
|
if(msg.cmd === "FastCalcBlock")
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
var FastBlock = msg;
|
|
|
|
StartHashPump(FastBlock);
|
|
|
|
FastBlock.RunCount = 0;
|
2019-07-12 12:45:46 +00:00
|
|
|
try
|
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
if(CreatePOWVersionX(FastBlock))
|
|
|
|
process.send({cmd:"POW", BlockNum:FastBlock.BlockNum, SeqHash:FastBlock.SeqHash, Hash:FastBlock.Hash, PowHash:FastBlock.PowHash,
|
|
|
|
AddrHash:FastBlock.AddrHash, Num:FastBlock.Num});
|
2019-07-12 12:45:46 +00:00
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
ToError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-07-19 04:26:00 +00:00
|
|
|
if(msg.cmd === "Alive")
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
2019-07-19 04:26:00 +00:00
|
|
|
if(msg.cmd === "Exit")
|
2019-07-13 01:15:47 +00:00
|
|
|
{
|
2019-07-19 04:26:00 +00:00
|
|
|
PROCESS.exit(0);
|
2019-07-13 01:15:47 +00:00
|
|
|
}
|
2019-07-12 12:45:46 +00:00
|
|
|
});
|
|
|
|
|
2019-07-13 01:15:47 +00:00
|
|
|
function CheckAlive()
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
if(global.NOALIVE)
|
|
|
|
return ;
|
|
|
|
var Delta = Date.now() - LastAlive;
|
|
|
|
if(Math.abs(Delta) > CHECK_STOP_CHILD_PROCESS)
|
|
|
|
{
|
|
|
|
PROCESS.exit(0);
|
|
|
|
return ;
|
|
|
|
}
|
2019-07-12 12:45:46 +00:00
|
|
|
};
|
2019-07-13 01:15:47 +00:00
|
|
|
|
|
|
|
function CalcPOWHash()
|
|
|
|
{
|
|
|
|
if(!Block.SeqHash)
|
|
|
|
return ;
|
|
|
|
if(new Date() - Block.Time > Block.Period)
|
|
|
|
{
|
|
|
|
clearInterval(idInterval);
|
|
|
|
idInterval = undefined;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(CreatePOWVersionX(Block))
|
|
|
|
process.send({cmd:"POW", BlockNum:Block.BlockNum, SeqHash:Block.SeqHash, Hash:Block.Hash, PowHash:Block.PowHash, AddrHash:Block.AddrHash,
|
|
|
|
Num:Block.Num});
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
ToError(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
global.BlockPump = undefined;
|
|
|
|
var idIntervalPump = undefined;
|
|
|
|
|
|
|
|
function StartHashPump(SetBlock)
|
|
|
|
{
|
|
|
|
if(!BlockPump || BlockPump.BlockNum < SetBlock.BlockNum || BlockPump.MinerID !== SetBlock.MinerID || BlockPump.Percent !== SetBlock.Percent)
|
|
|
|
{
|
|
|
|
global.BlockPump = {BlockNum:SetBlock.BlockNum, RunCount:SetBlock.RunCount, MinerID:SetBlock.MinerID, Percent:SetBlock.Percent,
|
|
|
|
LastNonce:0, };
|
|
|
|
}
|
|
|
|
if(!idIntervalPump)
|
|
|
|
{
|
|
|
|
idIntervalPump = setInterval(PumpHash, global.POWRunPeriod);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var StartTime = 1;
|
|
|
|
var EndTime = 0;
|
2019-07-12 12:45:46 +00:00
|
|
|
|
|
|
|
function PumpHash()
|
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
if(!BlockPump)
|
|
|
|
return ;
|
|
|
|
var CurTime = Date.now();
|
|
|
|
if(StartTime > EndTime)
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
var Delta = CurTime - StartTime;
|
|
|
|
var PeriodPercent = 100 * Delta / CONSENSUS_PERIOD_TIME;
|
|
|
|
if(PeriodPercent >= BlockPump.Percent)
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
EndTime = CurTime;
|
|
|
|
return ;
|
2019-07-12 12:45:46 +00:00
|
|
|
}
|
2019-07-13 01:15:47 +00:00
|
|
|
CreatePOWVersionX(BlockPump, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var Delta = CurTime - EndTime;
|
|
|
|
var PeriodPercent = 100 * Delta / CONSENSUS_PERIOD_TIME;
|
|
|
|
if(PeriodPercent > 100 - BlockPump.Percent)
|
2019-07-12 12:45:46 +00:00
|
|
|
{
|
2019-07-13 01:15:47 +00:00
|
|
|
StartTime = CurTime;
|
2019-07-12 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|