From a11f96e77520fb9fffd18b1a936e0686c7d08fc8 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sun, 14 Jul 2019 18:27:58 +0800 Subject: [PATCH] refactor: split pow cacl process Signed-off-by: MiaoWoo --- src/core/block-exchange.ts | 2 +- src/core/block-loader.ts | 3 +-- src/core/connect.ts | 9 +++------ src/core/terahashmining.ts | 7 +++++-- src/global.d.ts | 12 +++++++++--- src/process/main-process.ts | 11 ++++++----- src/process/pow-process.ts | 23 ++++++++++++++++++----- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/core/block-exchange.ts b/src/core/block-exchange.ts index ae1d193..de70d70 100644 --- a/src/core/block-exchange.ts +++ b/src/core/block-exchange.ts @@ -1325,7 +1325,7 @@ export default class CConsensus extends CBlock { return SumPow / Count; } CreatePOWNew(Block) { - CreateHashMinimal(Block, global.GENERATE_BLOCK_ACCOUNT) + global.CreateHashMinimal(Block, global.GENERATE_BLOCK_ACCOUNT) this.AddToMaxPOW(Block, { SeqHash: Block.SeqHash, AddrHash: Block.AddrHash, PrevHash: Block.PrevHash, TreeHash: Block.TreeHash, }) diff --git a/src/core/block-loader.ts b/src/core/block-loader.ts index b71a844..022fe64 100644 --- a/src/core/block-loader.ts +++ b/src/core/block-loader.ts @@ -170,8 +170,7 @@ export default class CBlock extends CRest { if (StartBlockNum <= 0) StartBlockNum = 15 global.ToLog("Current DeltaNum=" + DeltaNum + " StartBlockNum=" + StartBlockNum, 2) - } - else { + } else { StartBlockNum = this.BlockNumDB } this.LoadHistoryMode = true diff --git a/src/core/connect.ts b/src/core/connect.ts index d3eb7e3..ec817ff 100644 --- a/src/core/connect.ts +++ b/src/core/connect.ts @@ -401,8 +401,7 @@ export default class CConnect extends CMessages { var SignArr = this.GetSignCheckDeltaTime(Data.CheckDeltaTime); if (global.CheckDevelopSign(SignArr, Data.CheckDeltaTime.Sign)) { global.CHECK_DELTA_TIME = Data.CheckDeltaTime - } - else { + } else { Node.NextConnectDelta = 60 * 1000 global.ToLog("Error Sign CheckDeltaTime Num=" + Data.CheckDeltaTime.Num + " from " + global.NodeInfo(Node)) this.AddCheckErrCount(Node, 10, "Error Sign CheckDeltaTime") @@ -475,12 +474,10 @@ export default class CConnect extends CMessages { global.ToLog("Get new CodeVersion = " + CodeVersion.VersionNum + " HASH:" + global.GetHexFromArr(CodeVersion.Hash).substr(0, 20)) if (CodeVersion.VersionNum > global.CODE_VERSION.VersionNum && CodeVersion.VersionNum > global.START_LOAD_CODE.StartLoadVersionNum) { this.StartLoadCode(Node, CodeVersion) - } - else { + } else { global.CODE_VERSION = CodeVersion } - } - else { + } else { global.ToLog("Error Sign CodeVersion=" + CodeVersion.VersionNum + " from " + global.NodeInfo(Node) + " HASH:" + global.GetHexFromArr(CodeVersion.Hash).substr(0, 20)) global.ToLog(JSON.stringify(CodeVersion)) this.AddCheckErrCount(Node, 10, "Error Sign CodeVersion") diff --git a/src/core/terahashmining.ts b/src/core/terahashmining.ts index 8c43bcc..22d7c97 100644 --- a/src/core/terahashmining.ts +++ b/src/core/terahashmining.ts @@ -20,7 +20,6 @@ require('./crypto-library'); require('../HTML/JS/terahashlib.js'); var DELTA_NONCE = Math.pow(2, 40) * global.MINING_VERSION_NUM; global.CreateHashMinimal = CreateHashMinimal; -global.CreatePOWVersionX = CreatePOWVersion3; function CreateHashMinimal(Block, MinerID) { if (Block.BlockNum < BLOCKNUM_ALGO2) { @@ -73,7 +72,7 @@ function InitVer3(Block) { } }; -function CreatePOWVersion3(Block, bHashPump) { +let CreatePOWVersionX = function CreatePOWVersion3(Block, bHashPump?) { if (!bWasInitVer3) InitVer3(Block); if (!bWasInitVerOK3) @@ -201,3 +200,7 @@ global.GetNonceHashArr = function(BlockNum, Miner, StartNonceRnd, CountNonce) { } return { ArrNonce: ArrNonce, ArrHash: ArrHash }; }; + +export { + CreatePOWVersionX +} diff --git a/src/global.d.ts b/src/global.d.ts index 0a769c2..6c7f449 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,5 +1,6 @@ export { }; +import { ChildProcess } from 'child_process' import CSmartContract from './core/transaction-validator' import CConsensus from './core/block-exchange' import CTransport from './core/server' @@ -234,7 +235,7 @@ declare global { //#region main-process.ts:375 MiningPaused: number; - ArrMiningWrk: any[]; + ArrMiningWrk: TeraChildProcess[]; //#endregion //#region library.ts:118 @@ -614,7 +615,6 @@ declare global { //#region terahashmining.ts CreateHashMinimal: Function; - CreatePOWVersionX: Function; GetNonceHashArr: Function; //#endregion @@ -667,14 +667,20 @@ declare global { interface Process { RunRPC: Function; } + + } + interface TeraChildProcess extends ChildProcess { + Num: number; + bOnline: boolean } interface TeraProcess { + pid?: number; Name: string; idInterval: number | any; idInterval1: number | any; idInterval2: number | any; LastAlive: number; - Worker: Worker; + Worker: TeraChildProcess; Path: string; OnMessage: Function; PeriodAlive: number; diff --git a/src/process/main-process.ts b/src/process/main-process.ts index a04695c..7c66ae2 100644 --- a/src/process/main-process.ts +++ b/src/process/main-process.ts @@ -222,7 +222,7 @@ function StartAllProcess(bClose) { var GlobalRunID = 0; var GlobalRunMap = {}; -function StartChildProcess(Item) { +function StartChildProcess(Item: TeraProcess) { let ITEM = Item; ITEM.idInterval = setInterval(function() { var Delta0 = Date.now() - ITEM.LastAlive; @@ -670,8 +670,9 @@ function RunOnce() { }; var glPortDebug = 49800; -function Fork(Path, ArrArgs?) { - const child_process = require('child_process'); +import { fork } from 'child_process' + +function Fork(Path, ArrArgs?): TeraChildProcess { ArrArgs = ArrArgs || []; if (global.LOCAL_RUN) ArrArgs.push("LOCALRUN"); @@ -690,8 +691,8 @@ function Fork(Path, ArrArgs?) { ArrArgs.push("DEV_MODE"); glPortDebug++; var execArgv = []; - var Worker = child_process.fork(Path, ArrArgs, { execArgv: execArgv }); - return Worker; + var Worker = fork(Path, ArrArgs, { execArgv: execArgv }); + return Worker as any; }; global.SpeedSignLib = 0; global.TestSignLib = TestSignLib; diff --git a/src/process/pow-process.ts b/src/process/pow-process.ts index 624b1ac..e786b8e 100644 --- a/src/process/pow-process.ts +++ b/src/process/pow-process.ts @@ -12,10 +12,18 @@ global.PROCESS_NAME = "POW"; global.POWPROCESS = 1; import "../core/library" import "../core/crypto-library" -import "../core/terahashmining" +import { CreatePOWVersionX } from "../core/terahashmining" var PROCESS = process; if (process.send && !global.DEBUGPROCESS) { process.send({ cmd: "online", message: "OK" }); + global.ToLogClient = function(Str, StrKey, bFinal) { + process.send({ + cmd: "ToLogClient", + Str: "" + Str, + StrKey: StrKey, + bFinal: bFinal + }); + }; } else { PROCESS = global.DEBUGPROCESS; } @@ -30,7 +38,7 @@ PROCESS.on('message', function(msg) { StartHashPump(FastBlock); FastBlock.RunCount = 0; try { - if (global.CreatePOWVersionX(FastBlock)) + if (CreatePOWVersionX(FastBlock)) process.send({ cmd: "POW", BlockNum: FastBlock.BlockNum, @@ -88,9 +96,14 @@ function CalcPOWHash() { return; } try { - if (global.CreatePOWVersionX(Block)) + if (CreatePOWVersionX(Block)) process.send({ - cmd: "POW", BlockNum: Block.BlockNum, SeqHash: Block.SeqHash, Hash: Block.Hash, PowHash: Block.PowHash, AddrHash: Block.AddrHash, + cmd: "POW", + BlockNum: Block.BlockNum, + SeqHash: Block.SeqHash, + Hash: Block.Hash, + PowHash: Block.PowHash, + AddrHash: Block.AddrHash, Num: Block.Num }); } catch (e) { @@ -128,7 +141,7 @@ function PumpHash() { EndTime = CurTime; return; } - global.CreatePOWVersionX(global.BlockPump, 1); + CreatePOWVersionX(global.BlockPump, 1); } else { var Delta = CurTime - EndTime; var PeriodPercent = 100 * Delta / global.CONSENSUS_PERIOD_TIME;