1
0
forked from circlecloud/tera

feat: 1120 release c/s mode complete

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-07-17 00:13:22 +08:00
parent 305d4b88da
commit a3739d9e4d
11 changed files with 461 additions and 98 deletions

View File

@ -23,18 +23,24 @@
"start": "node dist/run-node.js" "start": "node dist/run-node.js"
}, },
"dependencies": { "dependencies": {
"@cc-server/core": "^0.6.1",
"@cc-server/ws": "^0.6.1",
"bintrees": "^1.0.2", "bintrees": "^1.0.2",
"greenlock": "^2.6.7", "greenlock": "^2.6.7",
"ntp-client": "^0.5.3", "ntp-client": "^0.5.3",
"secp256k1": "^3.6.1", "secp256k1": "^3.6.1",
"socket.io-client": "^2.2.0",
"stun": "^1.1.0", "stun": "^1.1.0",
"uglify-js": "^3.4.9", "uglify-js": "^3.4.9",
"zip": "^1.2.0" "zip": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/bintrees": "^1.0.2", "@types/bintrees": "^1.0.2",
"@types/secp256k1": "^3.5.0", "@types/express": "^4.17.0",
"@types/node": "^12.6.1", "@types/node": "^12.6.1",
"@types/secp256k1": "^3.5.0",
"@types/socket.io": "^2.1.2",
"@types/socket.io-client": "^1.4.32",
"rimraf": "^2.6.3", "rimraf": "^2.6.3",
"ts-node-dev": "^1.0.0-pre.40", "ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.5.3" "typescript": "^3.5.3"

92
public/index.html Normal file
View File

@ -0,0 +1,92 @@
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.2.0/dist/socket.io.js"> </script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.css">
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fit/fit.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/attach/attach.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.js"></script>
<style>
#terminal-container .terminal.xterm {
height: 100%;
}
#terminal-container .xterm-viewport {
height: 100% !important;
}
</style>
</head>
<body>
<div id="terminal" style="height: 100%;"></div>
<script type="text/javascript">
Terminal.applyAddon(fit);
Terminal.applyAddon(attach);
Terminal.applyAddon(fullscreen);
var term = new Terminal({
cursorBlink: true,
screenKeys: true
});
term.open(document.getElementById('terminal'));
term.toggleFullScreen();
term.fit();
var cmd = ''
term.on('data', (data) => {
if (data =='\r') {
term.writeln(data)
if(cmd.startsWith('/')){
args = cmd.split(' ');
cmd = args[0].substr(1);
switch(cmd){
case "connect":
if(args[1]){
newcon(args[1])
} else {
newcon('/main')
}
break;
case "disconnect":
socket.close();
break;
}
} else {
socket && socket.emit('webmsg', {cmd});
}
cmd = ''
} else {
cmd += data;
term.write(data)
}
});
function newcon(address){
socket = io(address,{
path: '/ws',
transports: ['websocket']
});
socket.on('connect', function(){
term.writeln('connect')
socket.emit('webclient',{})
});
socket.on('message', function(data){
for (const line of JSON.stringify(data, undefined, 4).split('\n')){
term.writeln(line);
}
});
socket.on('disconnect', function(){
term.reset();
term.writeln('disconnect')
});
}
window.onresize = function() {
term.fit();
term.scrollToBottom();
};
</script>
</body>
</html>

View File

@ -12,6 +12,7 @@
import CBlock from './block-loader' import CBlock from './block-loader'
import { RBTree } from './library' import { RBTree } from './library'
import { STreeBuffer } from './base'; import { STreeBuffer } from './base';
import { SendToWebClient } from '../rpc/server'
require('./library'); require('./library');
require('./crypto-library'); require('./crypto-library');
const TX_PROCESS_TIME = 100; const TX_PROCESS_TIME = 100;
@ -423,8 +424,7 @@ export default class CConsensus extends CBlock {
var SendData = { "Method": "RETTRANSFERTX", "Context": Info.Context, "Data": { BlockNum: Block.BlockNum, Array: Arr, } }; var SendData = { "Method": "RETTRANSFERTX", "Context": Info.Context, "Data": { BlockNum: Block.BlockNum, Array: Arr, } };
this.SendF(Node, SendData, global.MAX_BLOCK_SIZE + 1000) this.SendF(Node, SendData, global.MAX_BLOCK_SIZE + 1000)
} }
static static RETTRANSFERTX_F() {
RETTRANSFERTX_F() {
return "{BlockNum:uint,Array:[{body:tr}]}"; return "{BlockNum:uint,Array:[{body:tr}]}";
} }
RETTRANSFERTX(Info, CurTime) { RETTRANSFERTX(Info, CurTime) {
@ -639,8 +639,7 @@ export default class CConsensus extends CBlock {
var BufWrite = global.BufLib.GetBufferFromObject(Data, FORMAT_DATA_TRANSFER, global.MAX_BLOCK_SIZE + 30000, WorkStructSend); var BufWrite = global.BufLib.GetBufferFromObject(Data, FORMAT_DATA_TRANSFER, global.MAX_BLOCK_SIZE + 30000, WorkStructSend);
return BufWrite; return BufWrite;
} }
static static TRANSFER_F() {
TRANSFER_F() {
return FORMAT_DATA_TRANSFER; return FORMAT_DATA_TRANSFER;
} }
CheckingMaxPowOther(Block) { CheckingMaxPowOther(Block) {
@ -1392,6 +1391,25 @@ export default class CConsensus extends CBlock {
this.AddToMaxPOW(BlockMining, { this.AddToMaxPOW(BlockMining, {
SeqHash: BlockMining.SeqHash, AddrHash: BlockMining.AddrHash, PrevHash: BlockMining.PrevHash, TreeHash: BlockMining.TreeHash, SeqHash: BlockMining.SeqHash, AddrHash: BlockMining.AddrHash, PrevHash: BlockMining.PrevHash, TreeHash: BlockMining.TreeHash,
}) })
if (bWas == 3) {
setTimeout(() => {
let webmsg: any = { BlockNum: msg.BlockNum }
webmsg.Mining = {
Hash: BlockMining.Hash.toString(),
SeqHash: BlockMining.Hash.toString()
}
webmsg.New = {
Hash: ValueNew.Hash.toString(),
PowHash: ValueNew.PowHash.toString()
}
Object.assign(webmsg, {
HashCount,
Power,
Num: msg.Num
})
SendToWebClient(webmsg)
}, 1)
}
} }
} }
}; };

View File

@ -275,8 +275,7 @@ export default class CConnect extends CMessages {
NetConstant:{Num:uint,BlockNum:uint,MaxTrasactionLimit:uint,Reserv1:uint,Reserv2:uint,Reserv3:uint,Reserv4:uint,Reserv5:uint,Sign:arr64},\ NetConstant:{Num:uint,BlockNum:uint,MaxTrasactionLimit:uint,Reserv1:uint,Reserv2:uint,Reserv3:uint,Reserv4:uint,Reserv5:uint,Sign:arr64},\
}"; }";
} }
static static PONG_F(bSend) {
PONG_F(bSend) {
return CConnect.PING_F(bSend); return CConnect.PING_F(bSend);
} }
PING(Info, CurTime) { PING(Info, CurTime) {
@ -914,8 +913,7 @@ export default class CConnect extends CMessages {
global.SAVE_CONST(true) global.SAVE_CONST(true)
} }
} }
static static TIME_F() {
TIME_F() {
return "{Time:uint, Sign:arr64}"; return "{Time:uint, Sign:arr64}";
} }
SendTimeDev(Node) { SendTimeDev(Node) {

View File

@ -88,8 +88,7 @@ export default class CMessages extends CSmartContract {
} }
return true; return true;
} }
static static MESSAGE_F() {
MESSAGE_F() {
return "{Arr:[{addrArr:hash,body:tr,nonce:uint,time:uint}]}"; return "{Arr:[{addrArr:hash,body:tr,nonce:uint,time:uint}]}";
} }
MESSAGE(Info, CurTime) { MESSAGE(Info, CurTime) {
@ -161,8 +160,7 @@ export default class CMessages extends CSmartContract {
return - 5; return - 5;
if (Block.Active) { if (Block.Active) {
Res = - 3 Res = - 3
} } else {
else {
Res = this.AddTrToBlockQuote(Block, Tr) Res = this.AddTrToBlockQuote(Block, Tr)
if (Tr.ToAll) if (Tr.ToAll)
this.SendTransaction(Tr) this.SendTransaction(Tr)
@ -200,8 +198,7 @@ export default class CMessages extends CSmartContract {
break; break;
} }
} }
static static TRANSACTION_F() {
TRANSACTION_F() {
return "{body:tr}"; return "{body:tr}";
} }
TRANSACTION(Info, CurTime) { TRANSACTION(Info, CurTime) {

View File

@ -14,13 +14,12 @@ import * as fs from 'fs'
import * as os from 'os' import * as os from 'os'
import * as crypto from 'crypto'; import * as crypto from 'crypto';
global.START_SERVER = 1; global.START_SERVER = 1;
import { secp256k1 } from '../core/library' import { WsServer, SendToClient } from '../rpc/server'
global.DATA_PATH = global.GetNormalPathString(global.DATA_PATH); global.DATA_PATH = global.GetNormalPathString(global.DATA_PATH);
global.CODE_PATH = global.GetNormalPathString(global.CODE_PATH); global.CODE_PATH = global.GetNormalPathString(global.CODE_PATH);
console.log("DATA DIR: " + global.DATA_PATH); console.log("DATA DIR: " + global.DATA_PATH);
console.log("PROGRAM DIR: " + global.CODE_PATH); console.log("PROGRAM DIR: " + global.CODE_PATH);
import "../core/library" import { secp256k1 } from '../core/library'
global.ToLog(os.platform() + " (" + os.arch() + ") " + os.release()); global.ToLog(os.platform() + " (" + os.arch() + ") " + os.release());
var VerArr = process.versions.node.split('.'); var VerArr = process.versions.node.split('.');
global.ToLog("nodejs: " + process.versions.node); global.ToLog("nodejs: " + process.versions.node);
@ -393,6 +392,7 @@ function AllAlive() {
for (var i = 0; i < global.ArrMiningWrk.length; i++) { for (var i = 0; i < global.ArrMiningWrk.length; i++) {
global.ArrMiningWrk[i].send({ cmd: "Alive" }); global.ArrMiningWrk[i].send({ cmd: "Alive" });
} }
SendToClient({ cmd: "Alive" })
}; };
function ClearArrMining() { function ClearArrMining() {
@ -479,18 +479,20 @@ function RunStopPOWProcess(Mode) {
Worker.on('message', function(msg) { Worker.on('message', function(msg) {
if (msg.cmd === "log") { if (msg.cmd === "log") {
global.ToLog(msg.message); global.ToLog(msg.message);
} } else {
else
if (msg.cmd === "online") { if (msg.cmd === "online") {
Worker.bOnline = true; Worker.bOnline = true;
global.ToLog("RUNNING PROCESS:" + Worker.Num + ":" + msg.message); global.ToLog("RUNNING PROCESS:" + Worker.Num + ":" + msg.message);
} else } else {
if (msg.cmd === "POW") { if (msg.cmd === "POW") {
global.SERVER.MiningProcess(msg); global.SERVER.MiningProcess(msg);
} else } else {
if (msg.cmd === "HASHRATE") { if (msg.cmd === "HASHRATE") {
global.ADD_HASH_RATE(msg.CountNonce); global.ADD_HASH_RATE(msg.CountNonce);
} }
}
}
}
}); });
Worker.on('error', function(err) { Worker.on('error', function(err) {
if (!global.ArrMiningWrk.length) if (!global.ArrMiningWrk.length)
@ -510,6 +512,21 @@ function RunStopPOWProcess(Mode) {
}; };
function SetCalcPOW(Block, cmd) { function SetCalcPOW(Block, cmd) {
SendToClient({
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(),
RunPeriod: global.POWRunPeriod,
RunCount: global.POW_RUN_COUNT,
Percent: global.POW_MAX_PERCENT,
CountMiningCPU: global.GetCountMiningCPU(),
ProcessMemorySize: ProcessMemorySize,
})
if (!global.USE_MINING) if (!global.USE_MINING)
return; return;
if (global.ArrMiningWrk.length !== global.GetCountMiningCPU()) if (global.ArrMiningWrk.length !== global.GetCountMiningCPU())
@ -653,6 +670,7 @@ function RunOnce() {
require("../core/update"); require("../core/update");
global.RunOnUpdate(); global.RunOnUpdate();
StartAllProcess(1); StartAllProcess(1);
WsServer.start(8080);
// require("./dogs"); // require("./dogs");
if (global.RESTART_PERIOD_SEC) { if (global.RESTART_PERIOD_SEC) {
var Period = (global.random(600) + global.RESTART_PERIOD_SEC); var Period = (global.random(600) + global.RESTART_PERIOD_SEC);

View File

@ -16,14 +16,6 @@ import { CreatePOWVersionX } from "../core/terahashmining"
var PROCESS = process; var PROCESS = process;
if (process.send && !global.DEBUGPROCESS) { if (process.send && !global.DEBUGPROCESS) {
process.send({ cmd: "online", message: "OK" }); process.send({ cmd: "online", message: "OK" });
global.ToLogClient = function(Str, StrKey, bFinal) {
process.send({
cmd: "ToLogClient",
Str: "" + Str,
StrKey: StrKey,
bFinal: bFinal
});
};
} else { } else {
PROCESS = global.DEBUGPROCESS; PROCESS = global.DEBUGPROCESS;
} }
@ -31,51 +23,99 @@ var LastAlive = Date.now();
setInterval(CheckAlive, 1000); setInterval(CheckAlive, 1000);
var idInterval = undefined; var idInterval = undefined;
var Block: any = {}; var Block: any = {};
import { powClient } from '../rpc/client'
powClient.on('message', (msg) => {
LastAlive = Date.now();
switch (msg.cmd) {
case "FastCalcBlock":
FastCalcBlock(msg);
break;
case "Alive":
break;
case "Exit":
PROCESS.exit(0);
}
})
powClient.connect()
function FastCalcBlock(msg) {
var FastBlock = msg;
StartHashPump(FastBlock);
FastBlock.RunCount = 0;
try {
if (CreatePOWVersionX(FastBlock)) {
powClient.send({
cmd: "POW",
BlockNum: FastBlock.BlockNum,
SeqHash: FastBlock.SeqHash,
Hash: FastBlock.Hash,
PowHash: FastBlock.PowHash,
AddrHash: FastBlock.AddrHash,
Num: FastBlock.Num
});
}
} catch (e) {
powClient.emit('web', `FastCalcBlock Error ${e}...`)
global.ToError(e);
}
}
PROCESS.on('message', function(msg) { PROCESS.on('message', function(msg) {
LastAlive = Date.now(); LastAlive = Date.now();
if (msg.cmd === "FastCalcBlock") { switch (msg.cmd) {
var FastBlock = msg; case "Alive":
StartHashPump(FastBlock); break;
FastBlock.RunCount = 0; case "Exit":
try { PROCESS.exit(0);
if (CreatePOWVersionX(FastBlock)) }
process.send({ })
cmd: "POW", // PROCESS.on('message', function(msg) {
BlockNum: FastBlock.BlockNum, // LastAlive = Date.now();
SeqHash: FastBlock.SeqHash, // if (msg.cmd === "FastCalcBlock") {
Hash: FastBlock.Hash, // var FastBlock = msg;
PowHash: FastBlock.PowHash, // StartHashPump(FastBlock);
AddrHash: FastBlock.AddrHash, // FastBlock.RunCount = 0;
Num: FastBlock.Num // try {
}); // if (CreatePOWVersionX(FastBlock))
} catch (e) { // process.send({
global.ToError(e); // cmd: "POW",
} // BlockNum: FastBlock.BlockNum,
} else // SeqHash: FastBlock.SeqHash,
if (msg.cmd === "SetBlock") { // Hash: FastBlock.Hash,
var StartNonce = 1000000 * (1 + msg.Num); // PowHash: FastBlock.PowHash,
if (Block.HashCount) { // AddrHash: FastBlock.AddrHash,
process.send({ cmd: "HASHRATE", CountNonce: Block.HashCount, Hash: Block.Hash }); // Num: FastBlock.Num
} // });
Block.HashCount = 0; // } catch (e) {
Block = msg; // global.ToError(e);
Block.Time = Date.now(); // }
Block.LastNonce = StartNonce; // } else {
Block.Period = global.CONSENSUS_PERIOD_TIME * Block.Percent / 100; // if (msg.cmd === "SetBlock") {
if (Block.Period > 0 && Block.RunPeriod > 0) { // var StartNonce = 1000000 * (1 + msg.Num);
CalcPOWHash(); // if (Block.HashCount) {
if (idInterval !== undefined) { // process.send({ cmd: "HASHRATE", CountNonce: Block.HashCount, Hash: Block.Hash });
clearInterval(idInterval); // }
} // Block.HashCount = 0;
idInterval = setInterval(CalcPOWHash, Block.RunPeriod); // Block = msg;
} // Block.Time = Date.now();
} else // Block.LastNonce = StartNonce;
if (msg.cmd === "Alive") { // Block.Period = global.CONSENSUS_PERIOD_TIME * Block.Percent / 100;
} else // if (Block.Period > 0 && Block.RunPeriod > 0) {
if (msg.cmd === "Exit") { // CalcPOWHash();
PROCESS.exit(0); // 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() { function CheckAlive() {
if (global.NOALIVE) if (global.NOALIVE)
@ -87,29 +127,32 @@ function CheckAlive() {
} }
}; };
function CalcPOWHash() { // function CalcPOWHash() {
if (!Block.SeqHash) // if (!Block.SeqHash)
return; // return;
if ((new Date() as any) - Block.Time > Block.Period) { // if ((new Date() as any) - Block.Time > Block.Period) {
clearInterval(idInterval); // clearInterval(idInterval);
idInterval = undefined; // idInterval = undefined;
return; // return;
} // }
try { // try {
if (CreatePOWVersionX(Block)) // if (CreatePOWVersionX(Block)) {
process.send({ // let msg = {
cmd: "POW", // cmd: "POW",
BlockNum: Block.BlockNum, // BlockNum: Block.BlockNum,
SeqHash: Block.SeqHash, // SeqHash: Block.SeqHash,
Hash: Block.Hash, // Hash: Block.Hash,
PowHash: Block.PowHash, // PowHash: Block.PowHash,
AddrHash: Block.AddrHash, // AddrHash: Block.AddrHash,
Num: Block.Num // Num: Block.Num
}); // }
} catch (e) { // powClient.send(msg)
global.ToError(e); // //process.send(msg);
} // }
}; // } catch (e) {
// global.ToError(e);
// }
// };
global.BlockPump = undefined; global.BlockPump = undefined;
var idIntervalPump = undefined; var idIntervalPump = undefined;
@ -124,6 +167,7 @@ function StartHashPump(SetBlock) {
MinerID: SetBlock.MinerID, MinerID: SetBlock.MinerID,
Percent: SetBlock.Percent, Percent: SetBlock.Percent,
LastNonce: 0, LastNonce: 0,
Num: SetBlock.Num,
}; };
} }
if (!idIntervalPump) { if (!idIntervalPump) {
@ -144,6 +188,12 @@ function PumpHash() {
EndTime = CurTime; EndTime = CurTime;
return; return;
} }
if (global.BlockPump.RunCount) {
setTimeout(() => {
powClient.send({ cmd: "HASHRATE", CountNonce: global.BlockPump.RunCount });
}, 1)
}
global.BlockPump.LastNonce = global.BlockPump.LastNonce + global.BlockPump.Num * 100000
CreatePOWVersionX(global.BlockPump, 1); CreatePOWVersionX(global.BlockPump, 1);
} else { } else {
var Delta = CurTime - EndTime; var Delta = CurTime - EndTime;

20
src/rpc/client.ts Normal file
View File

@ -0,0 +1,20 @@
import * as io from 'socket.io-client'
let host = process.env.TERA_HOST || 'http://127.0.0.1:8080/main'
let powClient = io(host, {
path: '/ws'
})
powClient.on('connect', () => {
// console.log(`Client ${powClient.id} connect ...`)
powClient.emit('powclient', {})
powClient.emit('web', `Connect to ${host} Successful...`)
powClient.send({ cmd: "online", message: "OK" })
});
powClient.on('disconnect', (error) => {
console.log(`Client disconnect Error: ${error}...`)
});
export { powClient }

94
src/rpc/server.ts Normal file
View File

@ -0,0 +1,94 @@
import { CcServerBoot } from '@cc-server/core'
import { namespace, listener, io, TYPE, interfaces } from '@cc-server/ws'
import { Namespace } from 'socket.io';
import { Container } from 'inversify';
let pows: io.Socket[] = [];
@namespace('/main')
class TeraNamespace implements interfaces.Namespace {
nsp: Namespace;
@listener()
webclient(socket: io.Socket) {
socket.join('web');
global.ToLog(`${socket.id} WebClient Connected...`)
}
@listener()
web(socket: io.Socket, msg: any) {
socket.to('web').send('Recover From Pow: ' + JSON.stringify(msg))
}
@listener()
webmsg(socket: io.Socket, msg: any) {
global.ToLog(`Recover webmsg ${JSON.stringify(msg)}`)
switch (msg.cmd) {
case "list":
socket.send(`Client List:`)
let index = 0;
for (let id in pows) {
const pow = pows[id];
socket.send(`${index} => ${pow.id}: ${pow.conn.remoteAddress}`)
index++
}
break;
case "pow":
socket.send('Join Pow Room...')
socket.join('pow')
break;
case "leavepow":
socket.leave('pow')
socket.send('Leave Pow Room...')
break;
}
}
@listener()
powclient(socket: io.Socket) {
pows[socket.id] = socket;
// global.ToLog(`${socket.id} PowClient Connected...`)
}
@listener()
message(socket: io.Socket, msg: any) {
socket.to('pow').send(`${JSON.stringify(msg)}`)
switch (msg.cmd) {
case "POW":
global.SERVER.MiningProcess(msg);
break;
case "HASHRATE":
global.ADD_HASH_RATE(msg.CountNonce);
break;
}
}
disconnect(socket: io.Socket) {
// socket.to('web').send(`Client ${socket.id} ${socket.conn.remoteAddress} disconnect...`)
delete pows[socket.id]
}
}
let container = new Container();
let WsServer = new CcServerBoot(container).build();
function getServer(): TeraNamespace {
return container.getNamed<TeraNamespace>(TYPE.Namespace, TeraNamespace.name);
}
function getNamespace(): Namespace {
return getServer().nsp;
}
function SendToClient(msg: any) {
getNamespace().to('pow').send(JSON.stringify(msg))
let index = 0;
for (let id in pows) {
const pow = pows[id];
if (typeof msg === "object") {
msg.Num = index;
}
pow.send(msg)
index++
}
}
function SendToWebClient(msg: any) {
getNamespace().to('web').send(msg)
}
export { WsServer, SendToClient, SendToWebClient }

68
src/run-client.ts Normal file
View File

@ -0,0 +1,68 @@
let thread = parseInt(process.env.TERA_THREAD) || 1;
let Memory = thread * 4 * 1024 * 1024 * 1024
let ProcessMemorySize = Math.trunc(Memory / thread);
import { fork } from 'child_process'
import './core/constant'
import './core/library'
function Fork(Path, ArrArgs?): TeraChildProcess {
ArrArgs = ArrArgs || [];
if (global.LOCAL_RUN)
ArrArgs.push("LOCALRUN");
else
if (global.TEST_NETWORK)
ArrArgs.push("TESTRUN");
ArrArgs.push("PATH:" + global.DATA_PATH);
ArrArgs.push("HOSTING:" + global.HTTP_HOSTING_PORT);
if (!global.USE_PARAM_JS)
ArrArgs.push("NOPARAMJS");
if (global.NWMODE)
ArrArgs.push("NWMODE");
if (global.NOALIVE)
ArrArgs.push("NOALIVE");
if (global.DEV_MODE)
ArrArgs.push("DEV_MODE");
var execArgv = [];
var Worker = fork(Path, ArrArgs, { execArgv: execArgv });
return Worker as any;
};
let ArrMiningWrk = []
function RunProcess() {
for (var R = 0; R < thread; R++) {
let Worker = Fork('./process/pow-process');
ArrMiningWrk.push(Worker);
Worker.Num = ArrMiningWrk.length;
Worker.on('message', function(msg) {
switch (msg.cmd) {
case "log":
console.log(msg.message)
break;
}
});
Worker.on('error', function(err) {
if (!ArrMiningWrk.length)
return;
console.log('ERROR IN PROCESS: ' + err);
});
Worker.on('close', function(code) {
console.log(`STOP PROCESS: ${Worker.Num} pid: ${Worker.pid} code: ${code}`)
for (var i = 0; i < ArrMiningWrk.length; i++) {
if (ArrMiningWrk[i].pid === Worker.pid) {
console.log("Delete wrk from arr - pid:" + Worker.pid);
ArrMiningWrk.splice(i, 1);
}
}
});
}
}
console.log("START MINER PROCESS COUNT: " + thread + " Memory: " + ProcessMemorySize / 1024 / 1024 + " Mb for each process");
setInterval(() => {
if (!ArrMiningWrk.length) {
console.log(`ArrMiningWrk.length == ${ArrMiningWrk.length} Starting all node...`)
RunProcess();
}
}, 1000)

View File

@ -2,7 +2,9 @@
"compilerOptions": { "compilerOptions": {
"outDir": "dist", "outDir": "dist",
"allowJs": true, "allowJs": true,
"target": "es5" "target": "es5",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}, },
"include": [ "include": [
"./src/**/*", "./src/**/*",