From 8c849ed49f4bf175ee6ac4329dba946e451e8b0c Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 22 Jul 2019 12:03:05 +0800 Subject: [PATCH] feat: add more type def Signed-off-by: MiaoWoo --- src/core/block-loader.ts | 11 ++++-- src/core/html-server.ts | 2 +- src/core/node.ts | 3 ++ src/core/rest-loader.ts | 70 ++++++++++++++++++++++++++++++++------ src/core/server.ts | 14 ++++---- src/core/terahashmining.ts | 1 + src/process/pow-process.ts | 32 ++++++++++------- src/run-client.ts | 4 +++ 8 files changed, 103 insertions(+), 34 deletions(-) diff --git a/src/core/block-loader.ts b/src/core/block-loader.ts index 0c59863..bf97c88 100644 --- a/src/core/block-loader.ts +++ b/src/core/block-loader.ts @@ -176,8 +176,15 @@ export default class CBlock extends CRest { this.LoadHistoryMode = true this.LoadHistoryMessage = !bSilent this.LoadHistoryContext = { - PrevBlockNum: - 1, Node: Node, BlockNum: this.BlockNumDB, MapSend: {}, Foward: 1, Pause: 0, DeltaBlockNum: 10, - StartTimeHistory: Date.now(), MaxTimeOut: 30 * 1000 + PrevBlockNum: - 1, + Node: Node, + BlockNum: this.BlockNumDB, + MapSend: {}, + Foward: 1, + Pause: 0, + DeltaBlockNum: 10, + StartTimeHistory: Date.now(), + MaxTimeOut: 30 * 1000 } if (!bSilent && !bCheckPoint && global.REST_START_COUNT) { this.CheckSyncRest() diff --git a/src/core/html-server.ts b/src/core/html-server.ts index 674c21f..8f01aaa 100644 --- a/src/core/html-server.ts +++ b/src/core/html-server.ts @@ -478,7 +478,7 @@ global.HTTPCaller.GetActList = function(Params) { return { arr: arr, result: 1 }; }; global.HTTPCaller.GetHashList = function(Params) { - var arr = global.DApps.Accounts.DBAccountsHash.GetRows(Params.StartNum, Params.CountNum, Params.Filter); + var arr = global.DApps.Accounts.DBAccountsHash.GetRows(Params.StartNum, Params.CountNum); for (var i = 0; i < arr.length; i++) { var item = arr[i]; item.BlockNum = item.Num * global.PERIOD_ACCOUNT_HASH; diff --git a/src/core/node.ts b/src/core/node.ts index 8217144..be81365 100644 --- a/src/core/node.ts +++ b/src/core/node.ts @@ -22,6 +22,7 @@ export class TeraSocket extends net.Socket { Buf: Buffer; SocketNum: number; SocketPrioritet: number; + HashRND: Buffer; } export default class CNode { addrStr @@ -102,6 +103,8 @@ export default class CNode { SocketStart WaitConnectFromServer WaitConnectIP + Prioritet + WasAddToReconnect constructor(addrStr, ip, port) { this.addrStr = addrStr this.ip = ip.trim() diff --git a/src/core/rest-loader.ts b/src/core/rest-loader.ts index 6d32a1f..81507d2 100644 --- a/src/core/rest-loader.ts +++ b/src/core/rest-loader.ts @@ -12,19 +12,50 @@ import CDB from './db/block-db' import { TYPE_TRANSACTION } from '../constant/account'; import { DB_FORMAT } from '../constant/db-format'; +import CNode from './node'; var MIN_POW_CHAINS = 2; global.COUNT_NODE_PROOF = 6; if (global.TEST_NETWORK) { MIN_POW_CHAINS = 1; global.COUNT_NODE_PROOF = 1; } +interface ContextTask { + StartNum?: number + Count?: number + Time?: number + Node?: CNode + MapSend?: {} + OK?: boolean +} +interface LoadContext { + Mode?: number, + BlockNum?: number, + BlockNumRest?: number, + WasDelta?: number, + BlockNumProof?: number, + CountProof?: number, + StartTimeHistory?: number, + MaxTimeOut?: number, + LoopSyncRest?: number, + SendGetHeaderCount?: number, + ReceiveHeaderCount?: number, + ArrProof?: any[], + MapSend?: any, + BlockProof?, + TxProof?, + AccTaskList?: ContextTask[], + AccTaskFinished?: number, + SmartTaskList?: ContextTask[], + SmartTaskFinished?: number +} + export default class CRest extends CDB { - LoadRestContext + LoadRestContext: LoadContext + LoadHistoryContext: LoadContext BlockNumDB BlockNumDBMin UseTruncateBlockDB ContextSendLoadToBegin - LoadHistoryContext constructor(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) { super(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) } @@ -43,9 +74,19 @@ export default class CRest extends CDB { return; } this.LoadRestContext = { - Mode: 0, BlockNum: BlockNumRest, BlockNumRest: BlockNumRest, WasDelta: Delta, BlockNumProof: BlockNumRest + global.DELTA_BLOCK_ACCOUNT_HASH, - CountProof: global.COUNT_BLOCKS_FOR_LOAD, StartTimeHistory: Date.now(), MaxTimeOut: 600 * 1000, LoopSyncRest: 1, SendGetHeaderCount: 0, - ReceiveHeaderCount: 0, ArrProof: [], MapSend: {} + Mode: 0, + BlockNum: BlockNumRest, + BlockNumRest: BlockNumRest, + WasDelta: Delta, + BlockNumProof: BlockNumRest + global.DELTA_BLOCK_ACCOUNT_HASH, + CountProof: global.COUNT_BLOCKS_FOR_LOAD, + StartTimeHistory: Date.now(), + MaxTimeOut: 600 * 1000, + LoopSyncRest: 1, + SendGetHeaderCount: 0, + ReceiveHeaderCount: 0, + ArrProof: [], + MapSend: {} } for (var i = 0; i < this.NodesArr.length; i++) { this.NodesArr[i].SendRestGetHeader = 0 @@ -81,7 +122,15 @@ export default class CRest extends CDB { Node.SendRestGetHeader = 1 global.ToLog("Send rest get header " + Context.BlockNumProof + " to " + global.NodeName(Node), 2) this.SendF(Node, { - "Method": "GETBLOCKHEADER", "Data": { Foward: 1, BlockNum: Context.BlockNumProof, Hash: [] }, "Context": { F: this.RETBLOCKHEADER_REST.bind(this) }, + "Method": "GETBLOCKHEADER", + "Data": { + Foward: 1, + BlockNum: Context.BlockNumProof, + Hash: [] + }, + "Context": { + F: this.RETBLOCKHEADER_REST.bind(this) + }, }) Context.SendGetHeaderCount++ break; @@ -274,7 +323,7 @@ export default class CRest extends CDB { global.ToLog("ERROR global.TX_PROCESS") return; } - Task.OK = 1 + Task.OK = true global.TX_PROCESS.RunRPC("TXWriteAccArr", { StartNum: Task.StartNum, Arr: Data.Arr }, function(Err, Params) { Context.AccTaskFinished++ }) @@ -315,7 +364,7 @@ export default class CRest extends CDB { Task.Node = Info.Node if (!global.TX_PROCESS || !global.TX_PROCESS.RunRPC) return; - Task.OK = 1 + Task.OK = true global.TX_PROCESS.RunRPC("TXWriteSmartArr", { StartNum: Task.StartNum, Arr: Data.Arr }, function(Err, Params) { Context.SmartTaskFinished++ }) @@ -340,9 +389,8 @@ export default class CRest extends CDB { if (!Task) { global.ToLog("error task number: " + t) Context.Mode = 100 - } - else { - Task.OK = 0 + } else { + Task.OK = false Context.Mode-- Context.SmartTaskFinished-- this.AddToBan(Task.Node, Str) diff --git a/src/core/server.ts b/src/core/server.ts index a611896..2018a45 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -632,7 +632,7 @@ export default class CTransport extends CConnect { break; } } - Send(Node, Info, TypeData) { + Send(Node: CNode, Info, TypeData) { if (!Node.Socket) { this.DeleteNodeFromActive(Node) return; @@ -655,10 +655,10 @@ export default class CTransport extends CConnect { Info.TimeNum = Date.now() Node.SendPacket.insert(Info) } - DoSendPacketNodeAll(Node) { + DoSendPacketNodeAll(Node: CNode) { while (this.DoSendPacketNode(Node) === 1); } - DoSendPacketNode(Node) { + DoSendPacketNode(Node: CNode) { var TimeNum = Date.now(); var Info = Node.SendPacket.max(); if (Info && TimeNum - Info.TimeNum > global.PACKET_ALIVE_PERIOD) @@ -737,7 +737,7 @@ export default class CTransport extends CConnect { global.ADD_TO_STAT("SENDDATA(KB):" + global.NodeName(Node), Value, 1) } } - CheckPOWTicketConnect(Socket, data) { + CheckPOWTicketConnect(Socket: TeraSocket, data) { try { var Info = global.BufLib.GetObjectFromBuffer(data, global.FORMAT_POW_TO_SERVER, {}); } @@ -749,7 +749,7 @@ export default class CTransport extends CConnect { this.SendCloseSocket(Socket, "DEF_NETWORK=" + Info.DEF_NETWORK + " MUST:" + global.GetNetworkName()) return; } - var Node = this.FindRunNodeContext(Info.addrArr, Info.FromIP, Info.FromPort, true); + var Node: CNode = this.FindRunNodeContext(Info.addrArr, Info.FromIP, Info.FromPort, true); if (global.CompareArr(Info.addrArr, this.addrArr) === 0) { global.AddNodeInfo(Node, "SERV: GET SELF") this.SendCloseSocket(Socket, "SELF") @@ -855,7 +855,7 @@ export default class CTransport extends CConnect { return; } let SELF = this; - this.Server = net.createServer(function(sock: any) { + this.Server = net.createServer(function(sock: TeraSocket) { if (SELF.WasBanIP({ address: sock.remoteAddress })) { sock.ConnectID = "new" global.CloseSocket(sock, "WAS BAN", true) @@ -889,7 +889,7 @@ export default class CTransport extends CConnect { var Buf = SELF.GetDataFromBuf(data); if (Buf) { SELF.CheckPOWTicketConnect(SOCKET, Buf.Data) - SOCKET.ConnectToServer = 0 + SOCKET.ConnectToServer = false return; } global.CloseSocket(SOCKET, "=SERVER ON DATA=") diff --git a/src/core/terahashmining.ts b/src/core/terahashmining.ts index 22d7c97..a08ad0d 100644 --- a/src/core/terahashmining.ts +++ b/src/core/terahashmining.ts @@ -202,5 +202,6 @@ global.GetNonceHashArr = function(BlockNum, Miner, StartNonceRnd, CountNonce) { }; export { + InitVer3, CreatePOWVersionX } diff --git a/src/process/pow-process.ts b/src/process/pow-process.ts index 7b62993..84830a0 100644 --- a/src/process/pow-process.ts +++ b/src/process/pow-process.ts @@ -12,7 +12,7 @@ global.PROCESS_NAME = "POW"; global.POWPROCESS = 1; import "../core/library" import "../core/crypto-library" -import { CreatePOWVersionX } from "../core/terahashmining" +import { InitVer3, CreatePOWVersionX } from "../core/terahashmining" var PROCESS = process; if (process.send && !global.DEBUGPROCESS) { process.send({ cmd: "online", message: "OK" }); @@ -24,6 +24,24 @@ setInterval(CheckAlive, 1000); var idInterval = undefined; var Block: any = {}; +PROCESS.on('message', function(msg) { + LastAlive = Date.now(); + switch (msg.cmd) { + case "Alive": + if (!powClient.connected) { + powClient.connect() + } + break; + case "InitVer3": + InitVer3({ + ProcessMemorySize: msg.ProcessMemorySize + }) + break + case "Exit": + PROCESS.exit(0); + } +}) + import { powClient } from '../rpc/client' powClient.on('message', (msg) => { @@ -65,18 +83,6 @@ function FastCalcBlock(msg) { global.ToError(e); } } -PROCESS.on('message', function(msg) { - LastAlive = Date.now(); - switch (msg.cmd) { - case "Alive": - if (!powClient.connected) { - powClient.connect() - } - break; - case "Exit": - PROCESS.exit(0); - } -}) function CheckAlive() { if (global.NOALIVE) diff --git a/src/run-client.ts b/src/run-client.ts index 3161453..cdebaa9 100644 --- a/src/run-client.ts +++ b/src/run-client.ts @@ -39,6 +39,10 @@ function RunProcess() { function AddPowProcess() { let Worker = Fork('./process/pow-process'); ArrMiningWrk.push(Worker); + Worker.send({ + cmd: "InitVer3", + ProcessMemorySize + }) Worker.Num = ArrMiningWrk.length; Worker.on('message', function(msg) { switch (msg.cmd) {