1
0
forked from circlecloud/tera

fix: method refactor error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-07-10 17:28:08 +08:00
parent 87a863a228
commit 812813da8d
21 changed files with 92 additions and 111 deletions

1
.gitignore vendored
View File

@ -38,3 +38,4 @@ screenshot
.eslintcache .eslintcache
build build
DATA

View File

@ -15,8 +15,11 @@
"author": "MiaoWoo<admin@yumc.pw>", "author": "MiaoWoo<admin@yumc.pw>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"clean": "", "clean": "npx rimraf dist",
"prebuild": "yarn clean" "prebuild": "yarn clean",
"watch": "npx tsc --watch",
"build": "npx tsc",
"start": "node dist/run-node.js"
}, },
"dependencies": { "dependencies": {
"bintrees": "^1.0.2", "bintrees": "^1.0.2",
@ -29,6 +32,8 @@
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^12.6.1", "@types/node": "^12.6.1",
"rimraf": "^2.6.3",
"ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.5.3" "typescript": "^3.5.3"
} }
} }

View File

@ -271,8 +271,8 @@ class STreeBuffer {
CheckName CheckName
constructor(MaxTime, CompareFunction?, KeyType?, CheckName?) { constructor(MaxTime, CompareFunction?, KeyType?, CheckName?) {
this.KeyType = KeyType this.KeyType = KeyType
this.MetaTree1 = new RBTree(CompareFunction) this.MetaTree1 = new global.RBTree(CompareFunction)
this.MetaTree2 = new RBTree(CompareFunction) this.MetaTree2 = new global.RBTree(CompareFunction)
this.CheckName = CheckName this.CheckName = CheckName
setInterval(this.ShiftMapDirect.bind(this), MaxTime) setInterval(this.ShiftMapDirect.bind(this), MaxTime)
} }

View File

@ -51,7 +51,7 @@ module.exports = class CConsensus extends require("./block-loader")
this.CurrentBlockNum = 0 this.CurrentBlockNum = 0
this.SendBlockID = 0 this.SendBlockID = 0
this.RelayMode = false this.RelayMode = false
this.TreeSendPacket = new RBTree(global.CompareItemHash) this.TreeSendPacket = new global.RBTree(global.CompareItemHash)
if (!global.ADDRLIST_MODE && !this.VirtualMode) { if (!global.ADDRLIST_MODE && !this.VirtualMode) {
this.idBlockChainTimer = setInterval(this.StartBlockChain.bind(this), global.CONSENSUS_PERIOD_TIME - 5) this.idBlockChainTimer = setInterval(this.StartBlockChain.bind(this), global.CONSENSUS_PERIOD_TIME - 5)
setInterval(this.DoTransfer.bind(this), global.CONSENSUS_CHECK_TIME) setInterval(this.DoTransfer.bind(this), global.CONSENSUS_CHECK_TIME)
@ -85,8 +85,8 @@ module.exports = class CConsensus extends require("./block-loader")
Context.TransferFromAddr = {} Context.TransferFromAddr = {}
Context.LevelsTransfer = [] Context.LevelsTransfer = []
Context.ErrRun = "" Context.ErrRun = ""
Context.PowTxTree = new RBTree(global.CompareItemTimePow) Context.PowTxTree = new global.RBTree(global.CompareItemTimePow)
Context.PowTicketTree = new RBTree(global.CompareItemTimePow) Context.PowTicketTree = new global.RBTree(global.CompareItemTimePow)
Context.bSave = false Context.bSave = false
Context.PrevHash = undefined Context.PrevHash = undefined
Context.TreeHash = undefined Context.TreeHash = undefined
@ -692,7 +692,7 @@ module.exports = class CConsensus extends require("./block-loader")
} }
AddPOWToMaxTree(POW, item) { AddPOWToMaxTree(POW, item) {
if (!POW.MaxTree) { if (!POW.MaxTree) {
POW.MaxTree = new RBTree(function(a, b) { POW.MaxTree = new global.RBTree(function(a, b) {
return global.CompareArr(a.PowHash, b.PowHash); return global.CompareArr(a.PowHash, b.PowHash);
}) })
} }
@ -1367,8 +1367,7 @@ module.exports = class CConsensus extends require("./block-loader")
} }
} }
}; };
let STreeBuffer = global.STreeBuffer; global.TreeBlockBuf = new global.STreeBuffer(50 * 1000, global.CompareItemHashSimple, "string");
global.TreeBlockBuf = new STreeBuffer(50 * 1000, global.CompareItemHashSimple, "string");
var PrevTimeIdle = 0; var PrevTimeIdle = 0;
OnTimeIdle(); OnTimeIdle();

View File

@ -13,7 +13,7 @@ const fs = require("fs");
import * as crypto from 'crypto'; import * as crypto from 'crypto';
require('./block-loader-const'); require('./block-loader-const');
const STAT_BLOCK_LOAD_PERIOD = global.CONSENSUS_PERIOD_TIME / 5; const STAT_BLOCK_LOAD_PERIOD = global.CONSENSUS_PERIOD_TIME / 5;
module.exports = class CBlock extends require("./rest-loader.js") module.exports = class CBlock extends require("./rest-loader")
{ {
MapMapLoaded MapMapLoaded
BlockChain BlockChain
@ -1188,7 +1188,7 @@ module.exports = class CBlock extends require("./rest-loader.js")
GetHistoryTree(typedata) { GetHistoryTree(typedata) {
var Tree = global.HistoryBlockBuf.LoadValue(typedata, 1); var Tree = global.HistoryBlockBuf.LoadValue(typedata, 1);
if (!Tree) { if (!Tree) {
Tree = new RBTree(global.CompareItemHash) Tree = new global.RBTree(global.CompareItemHash)
global.HistoryBlockBuf.SaveValue(typedata, Tree) global.HistoryBlockBuf.SaveValue(typedata, Tree)
} }
return Tree; return Tree;
@ -1300,4 +1300,4 @@ global.LoadBlockFromNetwork = function(Params, F) {
F(1); F(1);
} }
}; };
global.HistoryBlockBuf = new STreeBuffer(global.HISTORY_BLOCK_COUNT * 1000, global.CompareItemHashSimple, "string"); global.HistoryBlockBuf = new global.STreeBuffer(global.HISTORY_BLOCK_COUNT * 1000, global.CompareItemHashSimple, "string");

View File

@ -352,7 +352,7 @@ module.exports = class CConnect extends require("./transfer-msg")
if (global.CREATE_ON_START) if (global.CREATE_ON_START)
return; return;
if (Data.CheckPoint.BlockNum && Data.CheckPoint.BlockNum > global.CHECK_POINT.BlockNum) { if (Data.CheckPoint.BlockNum && Data.CheckPoint.BlockNum > global.CHECK_POINT.BlockNum) {
var SignArr = global.arr2(Data.CheckPoint.Hash, global.SaveToFile(Data.CheckPoint.BlockNum)); var SignArr = global.arr2(Data.CheckPoint.Hash, global.GetArrFromValue(Data.CheckPoint.BlockNum));
if (global.CheckDevelopSign(SignArr, Data.CheckPoint.Sign)) { if (global.CheckDevelopSign(SignArr, Data.CheckPoint.Sign)) {
global.CHECK_POINT = Data.CheckPoint global.CHECK_POINT = Data.CheckPoint
this.ResetNextPingAllNode() this.ResetNextPingAllNode()
@ -451,7 +451,7 @@ module.exports = class CConnect extends require("./transfer-msg")
Level += Delta / CodeVersion.BlockPeriod Level += Delta / CodeVersion.BlockPeriod
} }
if (Level >= CodeVersion.LevelUpdate) { if (Level >= CodeVersion.LevelUpdate) {
var SignArr = global.arr2(CodeVersion.Hash, global.SaveToFile(CodeVersion.VersionNum)); var SignArr = global.arr2(CodeVersion.Hash, global.GetArrFromValue(CodeVersion.VersionNum));
if (global.CheckDevelopSign(SignArr, CodeVersion.Sign)) { if (global.CheckDevelopSign(SignArr, CodeVersion.Sign)) {
global.ToLog("Get new CodeVersion = " + CodeVersion.VersionNum + " HASH:" + global.GetHexFromArr(CodeVersion.Hash).substr(0, 20)) 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) { if (CodeVersion.VersionNum > global.CODE_VERSION.VersionNum && CodeVersion.VersionNum > global.START_LOAD_CODE.StartLoadVersionNum) {
@ -879,7 +879,7 @@ module.exports = class CConnect extends require("./transfer-msg")
} }
if (!this.SignCurrentTimeDev) { if (!this.SignCurrentTimeDev) {
var SignArr = global.GetArrFromHex(global.SERVER.addrStr); var SignArr = global.GetArrFromHex(global.SERVER.addrStr);
this.SignCurrentTimeDev = global.SaveToFile(global.SHA3BUF(SignArr), global.WALLET.KeyPair.getPrivateKey('')).signature this.SignCurrentTimeDev = global.GetArrFromValue(global.SHA3BUF(SignArr), global.WALLET.KeyPair.getPrivateKey('')).signature
} }
var Time = global.GetCurrentTime() - 0; var Time = global.GetCurrentTime() - 0;
global.ToLog("Send time: " + Time + " to " + NodeInfo(Node)) global.ToLog("Send time: " + Time + " to " + NodeInfo(Node))

View File

@ -8,7 +8,7 @@
* Telegram: https://t.me/terafoundation * Telegram: https://t.me/terafoundation
*/ */
global.UPDATE_CODE_VERSION_NUM = 1102; global.UPDATE_CODE_VERSION_NUM = 1127;
global.MIN_CODE_VERSION_NUM = 1094; global.MIN_CODE_VERSION_NUM = 1094;
global.MINING_VERSION_NUM = 0; global.MINING_VERSION_NUM = 0;
global.InitParamsArg = InitParamsArg; global.InitParamsArg = InitParamsArg;
@ -115,7 +115,7 @@ global.HTTP_HOSTING_PORT = 0;
global.HTTPS_HOSTING_DOMAIN = ""; global.HTTPS_HOSTING_DOMAIN = "";
global.HTTP_MAX_COUNT_ROWS = 20; global.HTTP_MAX_COUNT_ROWS = 20;
global.HTTP_ADMIN_PASSORD = ""; global.HTTP_ADMIN_PASSORD = "";
require("./startlib.js"); require("./startlib");
global.MIN_POWER_POW_HANDSHAKE = 12; global.MIN_POWER_POW_HANDSHAKE = 12;
global.USE_HINT = 0; global.USE_HINT = 0;
global.ALL_VIEW_ROWS = 0; global.ALL_VIEW_ROWS = 0;

View File

@ -7,7 +7,7 @@
* Twitter: https://twitter.com/terafoundation * Twitter: https://twitter.com/terafoundation
* Telegram: https://t.me/terafoundation * Telegram: https://t.me/terafoundation
*/ */
require("./library.js"); require("./library");
import * as crypto from 'crypto' import * as crypto from 'crypto'
global.MAX_SUPER_VALUE_POW = (1 << 30) * 2; global.MAX_SUPER_VALUE_POW = (1 << 30) * 2;

View File

@ -15,7 +15,7 @@ import './log'
import * as crypto from 'crypto'; import * as crypto from 'crypto';
const os = require('os'); const os = require('os');
var BlockTree = new STreeBuffer(300 * 1000, global.CompareItemHashSimple, "number"); var BlockTree = new global.STreeBuffer(300 * 1000, global.CompareItemHashSimple, "number");
const http = require('http'), net = require('net'), url = require('url'), fs = require('fs'), querystring = require('querystring'); const http = require('http'), net = require('net'), url = require('url'), fs = require('fs'), querystring = require('querystring');
var ContenTypeMap = {}; var ContenTypeMap = {};
ContenTypeMap["js"] = "application/javascript"; ContenTypeMap["js"] = "application/javascript";

View File

@ -13,17 +13,6 @@ import * as os from 'os'
import './constant' import './constant'
import './log' import './log'
let {
TO_ERROR_LOG,
TimeStart,
LOAD_CONST,
LoadParams,
CONST_NAME_ARR,
SaveParams,
SAVE_CONST,
GetDeltaCurrentTime,
} = global
Number.prototype.toStringZ = function(count) { Number.prototype.toStringZ = function(count) {
var strnum = this.toString(); var strnum = this.toString();
if (strnum.length > count) if (strnum.length > count)
@ -144,7 +133,7 @@ global.LoadParams = function(filename, DefaultValue) {
} }
} }
catch (err) { catch (err) {
TO_ERROR_LOG("MAINLIB", 100, "Error in file:" + filename + "\n" + err); global.TO_ERROR_LOG("MAINLIB", 100, "Error in file:" + filename + "\n" + err);
} }
return DefaultValue; return DefaultValue;
}; };
@ -157,7 +146,7 @@ global.StartTime = function() {
global.FinishTime = function(Str) { global.FinishTime = function(Str) {
Str = Str || ""; Str = Str || "";
var TimeFinish = global.GetCurrentTime(); var TimeFinish = global.GetCurrentTime();
var delta = TimeFinish - TimeStart; var delta = TimeFinish - global.TimeStart;
console.log(Str + " time: " + delta + " ms"); console.log(Str + " time: " + delta + " ms");
}; };
global.CompareItemBufFD = function(a, b) { global.CompareItemBufFD = function(a, b) {
@ -229,10 +218,10 @@ global.CompareItemTimePow = function(a, b) {
}; };
global.LOAD_CONST = function() { global.LOAD_CONST = function() {
var Count = 0; var Count = 0;
var constants = LoadParams(global.GetDataPath("const.lst"), {}); var constants = global.LoadParams(global.GetDataPath("const.lst"), {});
if (constants) { if (constants) {
for (var i = 0; i < CONST_NAME_ARR.length; i++) { for (var i = 0; i < global.CONST_NAME_ARR.length; i++) {
var key = CONST_NAME_ARR[i]; var key = global.CONST_NAME_ARR[i];
if (constants[key] !== undefined) { if (constants[key] !== undefined) {
Count++; Count++;
global[key] = constants[key]; global[key] = constants[key];
@ -245,12 +234,12 @@ var WasStartSaveConst = false;
function SaveConst() { function SaveConst() {
var constants = {}; var constants = {};
for (var i = 0; i < CONST_NAME_ARR.length; i++) { for (var i = 0; i < global.CONST_NAME_ARR.length; i++) {
var key = CONST_NAME_ARR[i]; var key = global.CONST_NAME_ARR[i];
if (global[key] !== undefined) if (global[key] !== undefined)
constants[key] = global[key]; constants[key] = global[key];
} }
SaveParams(global.GetDataPath("const.lst"), constants); global.SaveParams(global.GetDataPath("const.lst"), constants);
WasStartSaveConst = false; WasStartSaveConst = false;
}; };
global.SAVE_CONST = function(bForce) { global.SAVE_CONST = function(bForce) {
@ -267,7 +256,7 @@ global.SAVE_CONST = function(bForce) {
function CheckGlobalTime() { function CheckGlobalTime() {
global.ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, NetTime) { global.ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, NetTime) {
if (err) { if (err) {
TO_ERROR_LOG("MAINLIB", 110, err); global.TO_ERROR_LOG("MAINLIB", 110, err);
return; return;
} }
var curTime = new Date; var curTime = new Date;
@ -278,9 +267,9 @@ function CheckGlobalTime() {
if (Math.abs(global.DELTA_CURRENT_TIME) > 24 * 3600 * 1000) if (Math.abs(global.DELTA_CURRENT_TIME) > 24 * 3600 * 1000)
global.DELTA_CURRENT_TIME = 0; global.DELTA_CURRENT_TIME = 0;
global.ToLog("Get global time: " + NetTime); global.ToLog("Get global time: " + NetTime);
SAVE_CONST(); global.SAVE_CONST();
}); });
SAVE_CONST(); global.SAVE_CONST();
}; };
global.CheckGlobalTime = CheckGlobalTime; global.CheckGlobalTime = CheckGlobalTime;
global.GetDeltaCurrentTime = function() { global.GetDeltaCurrentTime = function() {
@ -326,7 +315,7 @@ function GetSecFromStrTime(Str) {
global.GetSecFromStrTime = GetSecFromStrTime; global.GetSecFromStrTime = GetSecFromStrTime;
global.GetCurrentTime = function(Delta_Time) { global.GetCurrentTime = function(Delta_Time) {
if (Delta_Time === undefined) if (Delta_Time === undefined)
Delta_Time = GetDeltaCurrentTime(); Delta_Time = global.GetDeltaCurrentTime();
var curTime: any = new Date; var curTime: any = new Date;
var Time = new Date(curTime - (- Delta_Time)); var Time = new Date(curTime - (- Delta_Time));
return Time; return Time;
@ -393,7 +382,7 @@ function GrayConnect() {
return 0; return 0;
}; };
global.GrayConnect = GrayConnect; global.GrayConnect = GrayConnect;
var ResConst = LOAD_CONST(); var ResConst = global.LOAD_CONST();
if (global.START_SERVER) { if (global.START_SERVER) {
if (!ResConst) { if (!ResConst) {
CheckGlobalTime(); CheckGlobalTime();

View File

@ -9,10 +9,9 @@
*/ */
"use strict"; "use strict";
require("./library.js"); require("./library");
const net = require("net"); const net = require("net");
var ConnectIDCount = 1; var ConnectIDCount = 1;
let RBTree = global.RBTree;
module.exports = class CNode { module.exports = class CNode {
addrStr addrStr
ip ip
@ -154,7 +153,7 @@ module.exports = class CNode {
this.CanHardTraffic = 0 this.CanHardTraffic = 0
this.BufWriteLength = 0 this.BufWriteLength = 0
this.BufWrite = Buffer.alloc(0) this.BufWrite = Buffer.alloc(0)
this.SendPacket = new RBTree(function(a, b) { this.SendPacket = new global.RBTree(function(a, b) {
return b.PacketNum - a.PacketNum; return b.PacketNum - a.PacketNum;
}) })
this.ConnectCount = 0 this.ConnectCount = 0
@ -343,19 +342,19 @@ module.exports = class CNode {
var Buf = global.BufLib.GetObjectFromBuffer(data, global.FORMAT_POW_TO_CLIENT, {}); var Buf = global.BufLib.GetObjectFromBuffer(data, global.FORMAT_POW_TO_CLIENT, {});
} }
catch (e) { catch (e) {
global.SERVER.Sendglobal.CloseSocket(Socket, "FORMAT_POW_TO_CLIENT") global.SERVER.SendCloseSocket(Socket, "FORMAT_POW_TO_CLIENT")
return 0; return 0;
} }
if (global.CompareArr(Buf.addrArr, global.SERVER.addrArr) === 0) { if (global.CompareArr(Buf.addrArr, global.SERVER.addrArr) === 0) {
Node.Self = true Node.Self = true
global.AddNodeInfo(Node, "END: SELF") global.AddNodeInfo(Node, "END: SELF")
global.SERVER.Sendglobal.CloseSocket(Socket, "SELF") global.SERVER.SendCloseSocket(Socket, "SELF")
return; return;
} }
var addrStr = global.GetHexFromAddres(Buf.addrArr); var addrStr = global.GetHexFromAddres(Buf.addrArr);
if (!Node.StartFindList && addrStr !== Node.addrStr) { if (!Node.StartFindList && addrStr !== Node.addrStr) {
global.AddNodeInfo(Node, "END: CHANGED ADDR: " + Node.addrStr.substr(0, 16) + "->" + addrStr.substr(0, 16)) global.AddNodeInfo(Node, "END: CHANGED ADDR: " + Node.addrStr.substr(0, 16) + "->" + addrStr.substr(0, 16))
global.SERVER.Sendglobal.CloseSocket(Socket, "ADDRESS_HAS_BEEN_CHANGED") global.SERVER.SendCloseSocket(Socket, "ADDRESS_HAS_BEEN_CHANGED")
return; return;
} }
if (Node.addrStrTemp) { if (Node.addrStrTemp) {
@ -373,7 +372,7 @@ module.exports = class CNode {
if (!Result) { if (!Result) {
global.ToLog("END: ERROR_SIGN_SERVER ADDR: " + addrStr.substr(0, 16) + " from ip: " + Socket.remoteAddress) global.ToLog("END: ERROR_SIGN_SERVER ADDR: " + addrStr.substr(0, 16) + " from ip: " + Socket.remoteAddress)
global.AddNodeInfo(Node, "END: ERROR_SIGN_SERVER ADDR: " + addrStr.substr(0, 16) + " from ip: " + Socket.remoteAddress) global.AddNodeInfo(Node, "END: ERROR_SIGN_SERVER ADDR: " + addrStr.substr(0, 16) + " from ip: " + Socket.remoteAddress)
global.SERVER.Sendglobal.CloseSocket(Socket, "ERROR_SIGN_SERVER") global.SERVER.SendCloseSocket(Socket, "ERROR_SIGN_SERVER")
return; return;
} }
if (Buf.MIN_POWER_POW_HANDSHAKE > 1 + global.MIN_POWER_POW_HANDSHAKE) { if (Buf.MIN_POWER_POW_HANDSHAKE > 1 + global.MIN_POWER_POW_HANDSHAKE) {

View File

@ -9,11 +9,11 @@
*/ */
"use strict"; "use strict";
const net = require("net"); import net = require("net");
const dgram = require("dgram"); import dgram = require("dgram");
import * as crypto from 'crypto'; import * as crypto from 'crypto';
require("./library.js"); import "./library"
require("./crypto-library"); import "./crypto-library"
const HARD_PACKET_PERIOD = 20; const HARD_PACKET_PERIOD = 20;
global.BUF_TYPE = 1; global.BUF_TYPE = 1;
global.STR_TYPE = 2; global.STR_TYPE = 2;
@ -57,9 +57,7 @@ const FORMAT_PACKET_SEND_TCP = "{\
Hash:hash,\ Hash:hash,\
Data:data,\ Data:data,\
}"; }";
let RBTree = global.RBTree; export default class CTransport extends require("./connect")
let STreeBuffer = global.STreeBuffer;
module.exports = class CTransport extends require("./connect")
{ {
UseRNDHeader UseRNDHeader
BAN_IP BAN_IP
@ -90,7 +88,7 @@ module.exports = class CTransport extends require("./connect")
this.port = RunPort this.port = RunPort
this.CanSend = 0 this.CanSend = 0
this.SendFormatMap = {} this.SendFormatMap = {}
this.ActualNodes = new RBTree(function(a, b) { this.ActualNodes = new global.RBTree(function(a, b) {
if (b.Prioritet !== a.Prioritet) if (b.Prioritet !== a.Prioritet)
return b.Prioritet - a.Prioritet; return b.Prioritet - a.Prioritet;
return global.CompareArr(a.addrArr, b.addrArr); return global.CompareArr(a.addrArr, b.addrArr);
@ -99,7 +97,7 @@ module.exports = class CTransport extends require("./connect")
this.LoadedPacketNum = 0 this.LoadedPacketNum = 0
this.LoadedSocketNum = 0 this.LoadedSocketNum = 0
setInterval(this.DoLoadBuf.bind(this), 1) setInterval(this.DoLoadBuf.bind(this), 1)
this.LoadBufSocketList = new RBTree(function(a, b) { this.LoadBufSocketList = new global.RBTree(function(a, b) {
if (b.SocketPrioritet !== a.SocketPrioritet) if (b.SocketPrioritet !== a.SocketPrioritet)
return b.SocketPrioritet - a.SocketPrioritet; return b.SocketPrioritet - a.SocketPrioritet;
return a.SocketNum - b.SocketNum; return a.SocketNum - b.SocketNum;
@ -108,7 +106,7 @@ module.exports = class CTransport extends require("./connect")
this.LastTimeHard = 0 this.LastTimeHard = 0
this.LastTimeHardOK = 0 this.LastTimeHardOK = 0
setInterval(this.DoHardPacketForSend.bind(this), HARD_PACKET_PERIOD) setInterval(this.DoHardPacketForSend.bind(this), HARD_PACKET_PERIOD)
this.HardPacketForSend = new RBTree(function(a, b) { this.HardPacketForSend = new global.RBTree(function(a, b) {
if (b.BlockProcessCount === a.BlockProcessCount) if (b.BlockProcessCount === a.BlockProcessCount)
return a.PacketNum - b.PacketNum; return a.PacketNum - b.PacketNum;
else else
@ -859,7 +857,7 @@ module.exports = class CTransport extends require("./connect")
return; return;
} }
let SELF = this; let SELF = this;
this.Server = net.createServer(function(sock) { this.Server = net.createServer(function(sock: any) {
if (SELF.WasBanIP({ address: sock.remoteAddress })) { if (SELF.WasBanIP({ address: sock.remoteAddress })) {
sock.ConnectID = "new" sock.ConnectID = "new"
global.CloseSocket(sock, "WAS BAN", true) global.CloseSocket(sock, "WAS BAN", true)
@ -1040,7 +1038,7 @@ module.exports = class CTransport extends require("./connect")
} }
} }
}; };
global.ContextPackets = new STreeBuffer(10 * 1000, global.CompareItemHash32, "object"); global.ContextPackets = new global.STreeBuffer(10 * 1000, global.CompareItemHash32, "object");
function CalcStatArr(arr, arrAvg, arrNext, Period) { function CalcStatArr(arr, arrAvg, arrNext, Period) {
var arrSum = [arr[0]]; var arrSum = [arr[0]];

View File

@ -15,8 +15,8 @@ var BLOCKNUM_ALGO2 = 6560000;
if (global.LOCAL_RUN || global.TEST_NETWORK) { if (global.LOCAL_RUN || global.TEST_NETWORK) {
BLOCKNUM_ALGO2 = 0; BLOCKNUM_ALGO2 = 0;
} }
require('./library.js'); require('./library');
require('./crypto-library.js'); require('./crypto-library');
require('../HTML/JS/terahashlib.js'); require('../HTML/JS/terahashlib.js');
var DELTA_NONCE = Math.pow(2, 40) * global.MINING_VERSION_NUM; var DELTA_NONCE = Math.pow(2, 40) * global.MINING_VERSION_NUM;
global.CreateHashMinimal = CreateHashMinimal; global.CreateHashMinimal = CreateHashMinimal;

View File

@ -22,7 +22,7 @@ module.exports = class CSmartContract extends require("./block-exchange")
BufHashTree BufHashTree
constructor(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) { constructor(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) {
super(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) super(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual)
this.BufHashTree = new RBTree(global.CompareArr) this.BufHashTree = new global.RBTree(global.CompareArr)
this.BufHashTree.LastAddNum = 0 this.BufHashTree.LastAddNum = 0
} }
AddBlockToHashTree(Block) { AddBlockToHashTree(Block) {

View File

@ -18,7 +18,7 @@ module.exports = class CMessages extends require("./transaction-validator")
super(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) super(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual)
this.MemPoolMsg = [] this.MemPoolMsg = []
for (var i = 0; i <= global.MAX_LEVEL_SPECIALIZATION; i++) for (var i = 0; i <= global.MAX_LEVEL_SPECIALIZATION; i++)
this.MemPoolMsg[i] = new RBTree(global.CompareItemTimePow) this.MemPoolMsg[i] = new global.RBTree(global.CompareItemTimePow)
} }
AddMsgToQuote(Msg) { AddMsgToQuote(Msg) {
var Tree = this.MemPoolMsg[Msg.Level]; var Tree = this.MemPoolMsg[Msg.Level];

View File

@ -10,8 +10,9 @@
import '../core/constant' import '../core/constant'
global.PROCESS_NAME = "MAIN"; global.PROCESS_NAME = "MAIN";
const fs = require('fs'); import fs = require('fs');
const os = require('os'); import os = require('os');
import path = require('path')
import * as crypto from 'crypto'; import * as crypto from 'crypto';
global.START_SERVER = 1; global.START_SERVER = 1;
@ -19,7 +20,7 @@ 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);
require("../core/library"); import "../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);
@ -27,7 +28,7 @@ if ((VerArr[0] as any as number) < 8) {
global.ToError("Error version of NodeJS=" + VerArr[0] + " Pls, download new version from www.nodejs.org and update it. The minimum version must be 8"); global.ToError("Error version of NodeJS=" + VerArr[0] + " Pls, download new version from www.nodejs.org and update it. The minimum version must be 8");
process.exit(); process.exit();
} }
var CServer = require("../core/server"); import CServer from '../core/server'
const DEF_PERIOD_SIGN_LIB = 500; const DEF_PERIOD_SIGN_LIB = 500;
setTimeout(function() { setTimeout(function() {
TestSignLib(DEF_PERIOD_SIGN_LIB); TestSignLib(DEF_PERIOD_SIGN_LIB);
@ -384,22 +385,12 @@ function ClearArrMining() {
ArrMiningWrk = []; ArrMiningWrk = [];
}; };
let {
GetCountMiningCPU,
CHECK_RUN_MINING,
GetSecFromStrTime,
SERVER,
GENERATE_BLOCK_ACCOUNT,
GetCodePath,
GrayConnect
} = global
function RunStopPOWProcess(Mode) { function RunStopPOWProcess(Mode) {
if (!GetCountMiningCPU() || GetCountMiningCPU() <= 0) if (!global.GetCountMiningCPU() || global.GetCountMiningCPU() <= 0)
return; return;
if (!StartCheckMining) { if (!StartCheckMining) {
StartCheckMining = 1; StartCheckMining = 1;
setInterval(RunStopPOWProcess, CHECK_RUN_MINING); setInterval(RunStopPOWProcess, global.CHECK_RUN_MINING);
setInterval(AllAlive, 1000); setInterval(AllAlive, 1000);
} }
if (global.NeedRestart) if (global.NeedRestart)
@ -407,8 +398,8 @@ function RunStopPOWProcess(Mode) {
if (global.USE_MINING && global.MINING_START_TIME && global.MINING_PERIOD_TIME) { if (global.USE_MINING && global.MINING_START_TIME && global.MINING_PERIOD_TIME) {
var Time = global.GetCurrentTime(); var Time = global.GetCurrentTime();
var TimeCur = Time.getUTCHours() * 3600 + Time.getUTCMinutes() * 60 + Time.getUTCSeconds(); var TimeCur = Time.getUTCHours() * 3600 + Time.getUTCMinutes() * 60 + Time.getUTCSeconds();
var StartTime = GetSecFromStrTime(global.MINING_START_TIME); var StartTime = global.GetSecFromStrTime(global.MINING_START_TIME);
var RunPeriod = GetSecFromStrTime(global.MINING_PERIOD_TIME); var RunPeriod = global.GetSecFromStrTime(global.MINING_PERIOD_TIME);
var TimeEnd = StartTime + RunPeriod; var TimeEnd = StartTime + RunPeriod;
global.MiningPaused = 1; global.MiningPaused = 1;
if (TimeCur >= StartTime && TimeCur <= TimeEnd) { if (TimeCur >= StartTime && TimeCur <= TimeEnd) {
@ -443,16 +434,16 @@ function RunStopPOWProcess(Mode) {
} }
if (global.USE_MINING && ArrMiningWrk.length) if (global.USE_MINING && ArrMiningWrk.length)
return; return;
if (SERVER.LoadHistoryMode) if (global.SERVER.LoadHistoryMode)
return; return;
if (GENERATE_BLOCK_ACCOUNT < 8) if (global.GENERATE_BLOCK_ACCOUNT < 8)
return; return;
var PathMiner = global.GetCodePath("../miner.js"); var PathMiner = global.GetCodePath("../miner.js");
if (!fs.existsSync(PathMiner)) if (!fs.existsSync(PathMiner))
PathMiner = "./process/pow-process.js"; PathMiner = "./process/pow-process.js";
if (ArrMiningWrk.length >= GetCountMiningCPU()) if (ArrMiningWrk.length >= global.GetCountMiningCPU())
return; return;
if (GrayConnect()) { if (global.GrayConnect()) {
global.ToLog("CANNOT START MINER IN NOT DIRECT IP MODE"); global.ToLog("CANNOT START MINER IN NOT DIRECT IP MODE");
return; return;
} }
@ -460,15 +451,15 @@ function RunStopPOWProcess(Mode) {
if (global.SIZE_MINING_MEMORY) if (global.SIZE_MINING_MEMORY)
Memory = global.SIZE_MINING_MEMORY; Memory = global.SIZE_MINING_MEMORY;
else { else {
Memory = os.freemem() - (512 + GetCountMiningCPU() * 100) * 1024 * 1014; Memory = os.freemem() - (512 + global.GetCountMiningCPU() * 100) * 1024 * 1014;
if (Memory < 0) { if (Memory < 0) {
global.ToLog("Not enough memory to start processes."); global.ToLog("Not enough memory to start processes.");
return; return;
} }
} }
ProcessMemorySize = Math.trunc(Memory / GetCountMiningCPU()); ProcessMemorySize = Math.trunc(Memory / global.GetCountMiningCPU());
global.ToLog("START MINER PROCESS COUNT: " + GetCountMiningCPU() + " Memory: " + ProcessMemorySize / 1024 / 1024 + " Mb for each process"); global.ToLog("START MINER PROCESS COUNT: " + global.GetCountMiningCPU() + " Memory: " + ProcessMemorySize / 1024 / 1024 + " Mb for each process");
for (var R = 0; R < GetCountMiningCPU(); R++) { for (var R = 0; R < global.GetCountMiningCPU(); R++) {
let Worker = Fork(PathMiner); let Worker = Fork(PathMiner);
ArrMiningWrk.push(Worker); ArrMiningWrk.push(Worker);
Worker.Num = ArrMiningWrk.length; Worker.Num = ArrMiningWrk.length;
@ -510,7 +501,7 @@ function RunStopPOWProcess(Mode) {
function SetCalcPOW(Block, cmd) { function SetCalcPOW(Block, cmd) {
if (!global.USE_MINING) if (!global.USE_MINING)
return; return;
if (ArrMiningWrk.length !== GetCountMiningCPU()) if (ArrMiningWrk.length !== global.GetCountMiningCPU())
return; return;
BlockMining = Block; BlockMining = Block;
for (var i = 0; i < ArrMiningWrk.length; i++) { for (var i = 0; i < ArrMiningWrk.length; i++) {
@ -518,9 +509,9 @@ function SetCalcPOW(Block, cmd) {
if (!CurWorker.bOnline) if (!CurWorker.bOnline)
continue; continue;
CurWorker.send({ CurWorker.send({
cmd: cmd, BlockNum: Block.BlockNum, Account: GENERATE_BLOCK_ACCOUNT, MinerID: GENERATE_BLOCK_ACCOUNT, SeqHash: Block.SeqHash, 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, 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: GetCountMiningCPU(), ProcessMemorySize: ProcessMemorySize, Percent: global.POW_MAX_PERCENT, CountMiningCPU: global.GetCountMiningCPU(), ProcessMemorySize: ProcessMemorySize,
}); });
} }
}; };
@ -528,11 +519,11 @@ global.SetCalcPOW = SetCalcPOW;
global.RunStopPOWProcess = RunStopPOWProcess; global.RunStopPOWProcess = RunStopPOWProcess;
function DoGetNodes() { function DoGetNodes() {
if (!SERVER) if (!global.SERVER)
return; return;
if (!GrayConnect() && global.SERVER.CanSend < 2) if (!global.GrayConnect() && global.SERVER.CanSend < 2)
return; return;
if (!SERVER.NodesArrUnSort || !SERVER.NodesArrUnSort.length) if (!global.SERVER.NodesArrUnSort || !global.SERVER.NodesArrUnSort.length)
return; return;
var Num = global.glCurNumFindArr % global.SERVER.NodesArrUnSort.length; var Num = global.glCurNumFindArr % global.SERVER.NodesArrUnSort.length;
var Node = global.SERVER.NodesArrUnSort[Num]; var Node = global.SERVER.NodesArrUnSort[Num];
@ -541,9 +532,9 @@ function DoGetNodes() {
global.glCurNumFindArr++; global.glCurNumFindArr++;
if (Node.Delete) if (Node.Delete)
return; return;
if (SERVER.NodeInBan(Node)) if (global.SERVER.NodeInBan(Node))
return; return;
if (SERVER.BusyLevel && Node.BlockProcessCount <= global.SERVER.BusyLevel) if (global.SERVER.BusyLevel && Node.BlockProcessCount <= global.SERVER.BusyLevel)
return; return;
if (GetSocketStatus(Node.Socket) === 100) { if (GetSocketStatus(Node.Socket) === 100) {
global.SERVER.StartGetNodes(Node); global.SERVER.StartGetNodes(Node);
@ -551,12 +542,12 @@ function DoGetNodes() {
}; };
function DoConnectToNodes(Arr, Mode) { function DoConnectToNodes(Arr, Mode) {
if (!SERVER) if (!global.SERVER)
return; return;
if (!GrayConnect() && global.SERVER.CanSend < 2) { if (!global.GrayConnect() && global.SERVER.CanSend < 2) {
return; return;
} }
if (GrayConnect() && global.SERVER.ActualNodes.size > global.GetGrayServerConnections()) if (global.GrayConnect() && global.SERVER.ActualNodes.size > global.GetGrayServerConnections())
return; return;
if (Arr.length) { if (Arr.length) {
var MinProcessCount = global.SERVER.BusyLevel - 1; var MinProcessCount = global.SERVER.BusyLevel - 1;

View File

@ -93,8 +93,7 @@ process.on('error' as any, function(err: TeraError) {
}); });
var CServerDB = require("../core/db/block-db"); var CServerDB = require("../core/db/block-db");
var KeyPair = crypto.createECDH('secp256k1'); var KeyPair = crypto.createECDH('secp256k1');
KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]));
77, 77, 77, 77, 77, 77, 77, 77, 77, 77]));
global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true); global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true);
global.HTTP_PORT_NUMBER = 0; global.HTTP_PORT_NUMBER = 0;
setInterval(function() { setInterval(function() {

View File

@ -10,7 +10,6 @@
global.PROCESS_NAME = "TX"; global.PROCESS_NAME = "TX";
import * as crypto from 'crypto'; import * as crypto from 'crypto';
const fs = require('fs');
require("../core/constant"); require("../core/constant");
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);
@ -96,14 +95,14 @@ var KeyPair = crypto.createECDH('secp256k1');
KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77])); 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]));
global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true); global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true);
global.TreeFindTX = new STreeBuffer(30 * 1000, global.CompareItemHashSimple, "string"); global.TreeFindTX = new global.STreeBuffer(30 * 1000, global.CompareItemHashSimple, "string");
setInterval(function() { setInterval(function() {
if (global.SERVER) { if (global.SERVER) {
global.SERVER.Close(); global.SERVER.Close();
} }
DoTXProcess(); DoTXProcess();
}, 10); }, 10);
var BlockTree = new STreeBuffer(30 * 1000, global.CompareItemHashSimple, "number"); var BlockTree = new global.STreeBuffer(30 * 1000, global.CompareItemHashSimple, "number");
global.bShowDetail = 0; global.bShowDetail = 0;
var StopTxProcess = 0; var StopTxProcess = 0;
var MinimalValidBlock = 0; var MinimalValidBlock = 0;

View File

@ -5,4 +5,4 @@ global.HTTP_PORT_NUMBER = 8080;
if (global.LOCAL_RUN === undefined) if (global.LOCAL_RUN === undefined)
global.LOCAL_RUN = 0; global.LOCAL_RUN = 0;
require('./process/main-process'); import './process/main-process'

View File

@ -11,7 +11,7 @@
"use strict"; "use strict";
import * as fs from 'fs' import * as fs from 'fs'
const DBRow = require("../core/db/db-row"); const DBRow = require("../core/db/db-row");
require('../core/rest_tables.js'); require('../core/rest_tables');
const MAX_SUM_TER = 1e9; const MAX_SUM_TER = 1e9;
const MAX_SUM_CENT = 1e9; const MAX_SUM_CENT = 1e9;
const DBLib = require("../core/db/db"); const DBLib = require("../core/db/db");

View File

@ -7,6 +7,7 @@
}, },
"include": [ "include": [
"./src/**/*", "./src/**/*",
"./src/**/**/*",
"global.d.ts" "global.d.ts"
] ]
} }