From e693bab5a7359472e7ff12bf6267956e8bd1f066 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 13 Jul 2019 14:10:12 +0800 Subject: [PATCH] feat: sync process code Signed-off-by: MiaoWoo --- src/HTML/wallet.html | 2 +- src/process/api-exchange.ts | 9 +- src/process/main-process.ts | 94 +++++++++++-------- src/process/pow-process.ts | 172 ++++++++++++++++++++++------------ src/process/static-process.ts | 31 ++++-- src/process/tx-process.ts | 26 +++-- src/process/web-process.ts | 33 +++---- 7 files changed, 222 insertions(+), 145 deletions(-) diff --git a/src/HTML/wallet.html b/src/HTML/wallet.html index 8712e94..487f560 100644 --- a/src/HTML/wallet.html +++ b/src/HTML/wallet.html @@ -1420,7 +1420,6 @@

+
Block: DB delta: diff --git a/src/process/api-exchange.ts b/src/process/api-exchange.ts index 66644ce..9e09a90 100644 --- a/src/process/api-exchange.ts +++ b/src/process/api-exchange.ts @@ -128,8 +128,7 @@ WebApi2.GetTransaction = function(Params) { } } } - } - else { + } else { if (typeof Params === "object" && Params.BlockNum && Params.TrNum !== undefined) { var Block = global.SERVER.ReadBlockDB(Params.BlockNum); if (Block && Block.arrContent) { @@ -327,8 +326,7 @@ function SendTransaction(Body, TR, Wait, F) { if (text === "Not add" || text === "Bad PoW") { CreateNonceAndSend(nonce + 1, NumNext + 1); return; - } - else + } else if (text === "Bad time") { if (DELTA_FOR_TIME_TX < 6) { DELTA_FOR_TIME_TX++; @@ -339,8 +337,7 @@ function SendTransaction(Body, TR, Wait, F) { } if (Wait && TR._result) { global.GlobalRunMap[WebID] = F; - } - else { + } else { F(TR._result < 1 ? 0 : 1, text); } }); diff --git a/src/process/main-process.ts b/src/process/main-process.ts index 97f5219..d8f8d33 100644 --- a/src/process/main-process.ts +++ b/src/process/main-process.ts @@ -8,11 +8,10 @@ * Telegram: https://t.me/terafoundation */ -import '../core/constant' global.PROCESS_NAME = "MAIN"; +import '../core/constant' import * as fs from 'fs' import * as os from 'os' -// import path = require('path') import * as crypto from 'crypto'; import { secp256k1 } from '../core/library' @@ -38,8 +37,6 @@ global.glCurNumFindArr = 0; global.ArrReconnect = []; global.ArrConnect = []; var FindList = [ - { "ip": "60.12.241.181", "port": 30000 }, - { "ip": "60.12.241.181", "port": 30001 }, { "ip": "60.12.241.181", "port": 30002 }, { "ip": "60.12.241.181", "port": 30003 }, { "ip": "60.12.241.181", "port": 30004 }, @@ -53,18 +50,15 @@ var FindList = [ { "ip": "60.12.241.181", "port": 30012 }, { "ip": "60.12.241.181", "port": 30013 }, { "ip": "60.12.241.181", "port": 30014 }, - { "ip": "60.12.241.181", "port": 30015 }, { "ip": "60.12.241.181", "port": 30016 }, { "ip": "60.12.241.181", "port": 30017 }, { "ip": "60.12.241.181", "port": 30018 }, ]; if (global.LOCAL_RUN) { FindList = [{ "ip": "127.0.0.1", "port": 50001 }, { "ip": "127.0.0.1", "port": 50002 }]; +} else if (global.TEST_NETWORK) { + FindList = [{ "ip": "149.154.70.158", "port": 40000 },]; } -else - if (global.TEST_NETWORK) { - FindList = [{ "ip": "149.154.70.158", "port": 40000 },]; - } global.SERVER = undefined; global.NeedRestart = 0; process.on('uncaughtException' as any, function(err: TeraError) { @@ -104,8 +98,7 @@ if (global.HTTP_HOSTING_PORT && !global.NWMODE) { if (WebProcess.Worker && WebProcess.Worker.connected) { try { WebProcess.Worker.send({ cmd: "Stat", Name: "MAX:ALL_NODES", Value: global.CountAllNode }); - } - catch (e) { + } catch (e) { WebProcess.Worker = undefined; } } @@ -144,7 +137,11 @@ function AddTransactionFromWeb(Params) { var body = global.GetArrFromHex(Params.HexValue); if (global.TX_PROCESS && global.TX_PROCESS.Worker) { var StrHex = global.GetHexFromArr(global.sha3(body)); - global.TX_PROCESS.Worker.send({ cmd: "FindTX", TX: StrHex, Web: 1, WebID: Params.WebID }); + global.TX_PROCESS.Worker.send({ + cmd: "FindTX", + TX: StrHex, Web: 1, + WebID: Params.WebID + }); } var Res = global.SERVER.AddTransaction({ body: body }, 1); var text = global.AddTrMap[Res]; @@ -259,15 +256,18 @@ function StartChildProcess(Item) { if (typeof msg.Params === "object" && msg.Params.F) { global[msg.Name](msg.Params, function(Err, Ret) { if (msg.id && ITEM.Worker) - ITEM.Worker.send({ cmd: "retcall", id: msg.id, Err: Err, Params: Ret }); + ITEM.Worker.send({ + cmd: "retcall", + id: msg.id, + Err: Err, + Params: Ret + }); }); break; - } - else { + } else { Ret = global[msg.Name](msg.Params); } - } - catch (e) { + } catch (e) { Err = 1; Ret = "" + e; } @@ -284,7 +284,7 @@ function StartChildProcess(Item) { case "log": global.ToLog(msg.message); break; - case "global.ToLogClient": + case "ToLogClient": if (WebProcess && WebProcess.Worker) { WebProcess.Worker.send(msg); } @@ -333,14 +333,22 @@ function StartChildProcess(Item) { if (F) { GlobalRunID++; try { - ITEM.Worker.send({ cmd: "call", id: GlobalRunID, Name: Name, Params: Params }); + ITEM.Worker.send({ + cmd: "call", + id: GlobalRunID, + Name: Name, + Params: Params + }); GlobalRunMap[GlobalRunID] = F; + } catch (e) { } - catch (e) { - } - } - else { - ITEM.Worker.send({ cmd: "call", id: 0, Name: Name, Params: Params }); + } else { + ITEM.Worker.send({ + cmd: "call", + id: 0, + Name: Name, + Params: Params + }); } }; }; @@ -413,8 +421,7 @@ function RunStopPOWProcess(Mode) { global.MiningPaused = 1; if (TimeCur >= StartTime && TimeCur <= TimeEnd) { global.MiningPaused = 0; - } - else { + } else { StartTime -= 24 * 3600; TimeEnd -= 24 * 3600; if (TimeCur >= StartTime && TimeCur <= TimeEnd) { @@ -425,16 +432,13 @@ function RunStopPOWProcess(Mode) { global.ToLog("------------ MINING MUST STOP ON TIME"); ClearArrMining(); return; - } - else + } else if (!global.ArrMiningWrk.length && !global.MiningPaused) { global.ToLog("*********** MINING MUST START ON TIME"); - } - else { + } else { return; } - } - else { + } else { global.MiningPaused = 0; } if (!global.USE_MINING || Mode === "STOP") { @@ -480,12 +484,10 @@ function RunStopPOWProcess(Mode) { if (msg.cmd === "online") { Worker.bOnline = true; global.ToLog("RUNNING PROCESS:" + Worker.Num + ":" + msg.message); - } - else + } else if (msg.cmd === "POW") { global.SERVER.MiningProcess(msg); - } - else + } else if (msg.cmd === "HASHRATE") { global.ADD_HASH_RATE(msg.CountNonce); } @@ -518,9 +520,20 @@ function SetCalcPOW(Block, cmd) { if (!CurWorker.bOnline) continue; CurWorker.send({ - cmd: cmd, BlockNum: Block.BlockNum, Account: global.GENERATE_BLOCK_ACCOUNT, MinerID: global.GENERATE_BLOCK_ACCOUNT, SeqHash: Block.SeqHash, - Hash: Block.Hash, PrevHash: Block.PrevHash, Time: Date.now(), Num: CurWorker.Num, RunPeriod: global.POWRunPeriod, RunCount: global.POW_RUN_COUNT, - Percent: global.POW_MAX_PERCENT, CountMiningCPU: global.GetCountMiningCPU(), ProcessMemorySize: ProcessMemorySize, + cmd: cmd, + BlockNum: Block.BlockNum, + Account: global.GENERATE_BLOCK_ACCOUNT, + MinerID: global.GENERATE_BLOCK_ACCOUNT, + SeqHash: Block.SeqHash, + Hash: Block.Hash, + PrevHash: Block.PrevHash, + Time: Date.now(), + Num: CurWorker.Num, + RunPeriod: global.POWRunPeriod, + RunCount: global.POW_RUN_COUNT, + Percent: global.POW_MAX_PERCENT, + CountMiningCPU: global.GetCountMiningCPU(), + ProcessMemorySize: ProcessMemorySize, }); } }; @@ -567,8 +580,7 @@ function DoConnectToNodes(Arr, Mode) { if (Mode === "CONNECT") { Node.WasAddToConnect = undefined; global.SERVER.StartConnectTry(Node); - } - else + } else if (Mode === "RECONNECT") { Node.WasAddToReconnect = undefined; Node.CreateConnect(); diff --git a/src/process/pow-process.ts b/src/process/pow-process.ts index bb5704b..624b1ac 100644 --- a/src/process/pow-process.ts +++ b/src/process/pow-process.ts @@ -8,82 +8,132 @@ * Telegram: https://t.me/terafoundation */ -global.PROCESS_NAME = "POW" -global.POWPROCESS = 1 -require("../core/library") -require("../core/crypto-library") -require("../core/terahashmining"); +global.PROCESS_NAME = "POW"; +global.POWPROCESS = 1; +import "../core/library" +import "../core/crypto-library" +import "../core/terahashmining" var PROCESS = process; -process.send && !global.DEBUGPROCESS ? process.send({ cmd: "online", message: "OK" }) : PROCESS = global.DEBUGPROCESS; +if (process.send && !global.DEBUGPROCESS) { + process.send({ cmd: "online", message: "OK" }); +} else { + PROCESS = global.DEBUGPROCESS; +} var LastAlive = Date.now(); -setInterval(CheckAlive, 1e3); -var idInterval = void 0, Block: any = {}; +setInterval(CheckAlive, 1000); +var idInterval = undefined; +var Block: any = {}; +PROCESS.on('message', function(msg) { + LastAlive = Date.now(); + if (msg.cmd === "FastCalcBlock") { + var FastBlock = msg; + StartHashPump(FastBlock); + FastBlock.RunCount = 0; + try { + if (global.CreatePOWVersionX(FastBlock)) + process.send({ + cmd: "POW", + BlockNum: FastBlock.BlockNum, + SeqHash: FastBlock.SeqHash, + Hash: FastBlock.Hash, + PowHash: FastBlock.PowHash, + AddrHash: FastBlock.AddrHash, + Num: FastBlock.Num + }); + } catch (e) { + global.ToError(e); + } + } else + if (msg.cmd === "SetBlock") { + var StartNonce = 1000000 * (1 + msg.Num); + if (Block.HashCount) { + process.send({ cmd: "HASHRATE", CountNonce: Block.HashCount, Hash: Block.Hash }); + } + Block.HashCount = 0; + Block = msg; + Block.Time = Date.now(); + Block.LastNonce = StartNonce; + Block.Period = global.CONSENSUS_PERIOD_TIME * Block.Percent / 100; + if (Block.Period > 0 && Block.RunPeriod > 0) { + CalcPOWHash(); + if (idInterval !== undefined) { + clearInterval(idInterval); + } + idInterval = setInterval(CalcPOWHash, Block.RunPeriod); + } + } else + if (msg.cmd === "Alive") { + } else + if (msg.cmd === "Exit") { + PROCESS.exit(0); + } +}); function CheckAlive() { - if (!global.NOALIVE) { - var e = Date.now() - LastAlive; - Math.abs(e) > global.CHECK_STOP_CHILD_PROCESS && PROCESS.exit(0); + if (global.NOALIVE) + return; + var Delta = Date.now() - LastAlive; + if (Math.abs(Delta) > global.CHECK_STOP_CHILD_PROCESS) { + PROCESS.exit(0); + return; } }; function CalcPOWHash() { - if (Block.SeqHash) { - if ((new Date as any) - Block.Time > Block.Period) - return clearInterval(idInterval), void (idInterval = void 0); - try { - global.CreatePOWVersionX(Block) && process.send({ - cmd: "POW", BlockNum: Block.BlockNum, SeqHash: Block.SeqHash, Hash: Block.Hash, PowHash: Block.PowHash, - AddrHash: Block.AddrHash, Num: Block.Num + if (!Block.SeqHash) + return; + if ((new Date() as any) - Block.Time > Block.Period) { + clearInterval(idInterval); + idInterval = undefined; + return; + } + try { + if (global.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) { - global.ToError(e); - } + } catch (e) { + global.ToError(e); } }; -PROCESS.on("message", function(e) { - if (LastAlive = Date.now(), "FastCalcBlock" === e.cmd) { - var o = e; - StartHashPump(o), o.RunCount = 0; - try { - global.CreatePOWVersionX(o) && process.send({ - cmd: "POW", BlockNum: o.BlockNum, SeqHash: o.SeqHash, Hash: o.Hash, PowHash: o.PowHash, AddrHash: o.AddrHash, - Num: o.Num - }); - } catch (e) { - global.ToError(e); - } - } else { - if ("SetBlock" === e.cmd) { - var a = 1e6 * (1 + e.Num); - Block.HashCount && process.send({ cmd: "HASHRATE", CountNonce: Block.HashCount, Hash: Block.Hash }), Block.HashCount = 0, (Block = e).Time = Date.now(), - Block.LastNonce = a, Block.Period = global.CONSENSUS_PERIOD_TIME * Block.Percent / 100, 0 < Block.Period && 0 < Block.RunPeriod && (CalcPOWHash(), - void 0 !== idInterval && clearInterval(idInterval), idInterval = setInterval(CalcPOWHash, Block.RunPeriod)); - } else { - "Alive" === e.cmd || "Exit" === e.cmd && PROCESS.exit(0); - } +global.BlockPump = undefined; +var idIntervalPump = undefined; + +function StartHashPump(SetBlock) { + if (!global.BlockPump || global.BlockPump.BlockNum < SetBlock.BlockNum || global.BlockPump.MinerID !== SetBlock.MinerID || global.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 idIntervalPump = global.BlockPump = void 0; - -function StartHashPump(e) { - (!global.BlockPump || global.BlockPump.BlockNum < e.BlockNum || global.BlockPump.MinerID !== e.MinerID || global.BlockPump.Percent !== e.Percent) && (global.BlockPump = { - BlockNum: e.BlockNum, - RunCount: e.RunCount, MinerID: e.MinerID, Percent: e.Percent, LastNonce: 0 - }), idIntervalPump = idIntervalPump || setInterval(PumpHash, global.POWRunPeriod); }; -var StartTime = 1, EndTime = 0; +var StartTime = 1; +var EndTime = 0; function PumpHash() { - if (global.BlockPump) { - var e = Date.now(); - if (EndTime < StartTime) { - if (100 * (e - StartTime) / global.CONSENSUS_PERIOD_TIME >= global.BlockPump.Percent) - return void (EndTime = e); - global.CreatePOWVersionX(global.BlockPump, 1); - } else { - 100 * (e - EndTime) / global.CONSENSUS_PERIOD_TIME > 100 - global.BlockPump.Percent && (StartTime = e); + if (!global.BlockPump) + return; + var CurTime = Date.now(); + if (StartTime > EndTime) { + var Delta = CurTime - StartTime; + var PeriodPercent = 100 * Delta / global.CONSENSUS_PERIOD_TIME; + if (PeriodPercent >= global.BlockPump.Percent) { + EndTime = CurTime; + return; + } + global.CreatePOWVersionX(global.BlockPump, 1); + } else { + var Delta = CurTime - EndTime; + var PeriodPercent = 100 * Delta / global.CONSENSUS_PERIOD_TIME; + if (PeriodPercent > 100 - global.BlockPump.Percent) { + StartTime = CurTime; } } }; diff --git a/src/process/static-process.ts b/src/process/static-process.ts index 38c6d0b..a680618 100644 --- a/src/process/static-process.ts +++ b/src/process/static-process.ts @@ -41,8 +41,7 @@ process.on('message', function(msg) { var Ret; try { Ret = global[msg.Name](msg.Params); - } - catch (e) { + } catch (e) { Err = 1; Ret = "" + e; } @@ -243,8 +242,7 @@ function GETREST(msg) { global.ToLog("Get bad rest acc hash: " + BlockNumRest + " = " + global.GetHexFromArr(Data.AccHash) + "/" + global.GetHexFromArr(Tree.Root), 2); ArrRest = []; nResult = 0; - } - else { + } else { ArrRest = GetArrRest(BlockNumRest, StartAccount, EndAccount); ProofHash = Tree.Root; var RetProof = global.GetMerkleProof(Tree.LevelsHash, StartAccount, EndAccount); @@ -254,9 +252,22 @@ function GETREST(msg) { BufLength += ProofArrL.length * 32 + ProofArrR.length * 32 + 32; } } - var Data2 = { Result: nResult, Arr: ArrRest, Version: 1, ProofHash: ProofHash, ProofArrL: ProofArrL, ProofArrR: ProofArrR }; + var Data2 = { + Result: nResult, + Arr: ArrRest, + Version: 1, + ProofHash: ProofHash, + ProofArrL: ProofArrL, + ProofArrR: ProofArrR + }; var BufWrite = global.BufLib.GetBufferFromObject(Data2, global.FORMAT_REST_TRANSFER, BufLength, {}); - process.send({ cmd: "Send", addrStr: msg.addrStr, Method: "RETREST", Context: msg.Context, Data: BufWrite }); + process.send({ + cmd: "Send", + addrStr: msg.addrStr, + Method: "RETREST", + Context: msg.Context, + Data: BufWrite + }); }; function GETSMART(msg) { @@ -278,7 +289,13 @@ function GETSMART(msg) { } var Data2 = { Result: Arr.length ? 1 : 0, Arr: Arr }; var BufWrite = global.BufLib.GetBufferFromObject(Data2, global.FORMAT_SMART_TRANSFER, BufLength, {}); - process.send({ cmd: "Send", addrStr: msg.addrStr, Method: "RETSMART", Context: msg.Context, Data: BufWrite }); + process.send({ + cmd: "Send", + addrStr: msg.addrStr, + Method: "RETSMART", + Context: msg.Context, + Data: BufWrite + }); }; var glMapForHash = {}; diff --git a/src/process/tx-process.ts b/src/process/tx-process.ts index d78041d..aa327b6 100644 --- a/src/process/tx-process.ts +++ b/src/process/tx-process.ts @@ -26,7 +26,12 @@ if (process.send) { }, 1000); process.send({ cmd: "online", message: "OK" }); global.ToLogClient = function(Str, StrKey, bFinal) { - process.send({ cmd: "ToLogClient", Str: "" + Str, StrKey: StrKey, bFinal: bFinal }); + process.send({ + cmd: "ToLogClient", + Str: "" + Str, + StrKey: StrKey, + bFinal: bFinal + }); }; } process.on('message', function(msg) { @@ -42,13 +47,17 @@ process.on('message', function(msg) { var Ret; try { Ret = global[msg.Name](msg.Params); - } - catch (e) { + } catch (e) { Err = 1; Ret = "" + e; } if (msg.id) - process.send({ cmd: "retcall", id: msg.id, Err: Err, Params: Ret }); + process.send({ + cmd: "retcall", + id: msg.id, + Err: Err, + Params: Ret + }); break; case "FindTX": global.TreeFindTX.SaveValue(msg.TX, msg); @@ -234,7 +243,11 @@ function InitTXProcess() { } } global.ToLog("DETECT NEW VER on BlockNum=" + LastBlockNum, 2); - global.DApps.Accounts.DBStateTX.Write({ Num: 0, BlockNum: LastBlockNum, BlockNumMin: MinimalValidBlock }); + global.DApps.Accounts.DBStateTX.Write({ + Num: 0, + BlockNum: LastBlockNum, + BlockNumMin: MinimalValidBlock + }); } StateTX = global.DApps.Accounts.DBStateTX.Read(0); LastBlockNum = StateTX.BlockNum; @@ -347,8 +360,7 @@ global.EvalCode = function(Code) { try { var ret = eval(Code); Result = JSON.stringify(ret, undefined, 4); - } - catch (e) { + } catch (e) { Result = "" + e; } return Result; diff --git a/src/process/web-process.ts b/src/process/web-process.ts index f82e67a..f211acc 100644 --- a/src/process/web-process.ts +++ b/src/process/web-process.ts @@ -47,8 +47,7 @@ process.on('message', function(msg) { var Ret; try { Ret = global[msg.Name](msg.Params); - } - catch (e) { + } catch (e) { Err = 1; Ret = "" + e; } @@ -181,8 +180,7 @@ if (global.HTTPS_HOSTING_DOMAIN) { global.ToError(err); }); } -} -else { +} else { HostingServer = http.createServer(MainHTTPFunction); RunListenServer(); } @@ -216,16 +214,14 @@ function MainHTTPFunction(request, response) { if (postData && postData.length) { try { Data = JSON.parse(postData); - } - catch (e) { + } catch (e) { Response.writeHead(405, { 'Content-Type': 'text/html' }); Response.end("Error data parsing"); } } DoCommandNew(response, Type, Path, Data); }); - } - else { + } else { DoCommandNew(response, Type, Path, Params); } }; @@ -334,8 +330,7 @@ function DoCommandNew(response, Type, Path, Params) { var Ret; try { Ret = F(Params, response, ArrPath); - } - catch (e) { + } catch (e) { Ret = { result: 0, text: e.message, text2: e.stack }; } if (Ret === null) @@ -347,8 +342,7 @@ function DoCommandNew(response, Type, Path, Params) { else Str = Ret; response.end(Str); - } - catch (e) { + } catch (e) { global.ToLog("ERR PATH:" + Path); global.ToLog(e); response.end(); @@ -395,12 +389,10 @@ function DoCommandNew(response, Type, Path, Params) { Name = PrefixPath + Name; global.SendWebFile(response, Name, Path); return; - } - else + } else if (LangPathMap[Method]) { PrefixPath = "./SITE/" + Method; - } - else { + } else { var Name2 = WalletFileMap[Name]; if (!Name2) PrefixPath = "./SITE"; @@ -610,8 +602,7 @@ HostingCaller.GetAccountListByKey = function(Params, ppp, bRet) { Data.SmartState = global.BufLib.GetObjectFromBuffer(Data.Value.Data, Data.SmartObj.StateFormat, {}); if (typeof Data.SmartState === "object") Data.SmartState.Num = Item.Num; - } - catch (e) { + } catch (e) { Data.SmartState = {}; } } @@ -793,11 +784,9 @@ process.RunRPC = function(Name, Params, F) { try { process.send({ cmd: "call", id: global.GlobalRunID, Name: Name, Params: Params }); global.GlobalRunMap[global.GlobalRunID] = F; + } catch (e) { } - catch (e) { - } - } - else { + } else { process.send({ cmd: "call", id: 0, Name: Name, Params: Params }); } };