From 8d0cb723c1f52bfa8f9ba4ce76c1ce8a44dd1adb Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 22 Jul 2019 10:16:37 +0800 Subject: [PATCH] refactor: keep client online and optimiz code Signed-off-by: MiaoWoo --- src/core/buffer.ts | 24 ++++++++++++++---------- src/core/library.ts | 3 ++- src/core/rest-loader.ts | 8 +++++++- src/core/server.ts | 3 +-- src/core/update.ts | 2 +- src/global.d.ts | 3 ++- src/process/pow-process.ts | 8 ++++++-- src/process/static-process.ts | 2 +- src/rpc/client.ts | 5 +++-- src/run-client.ts | 3 +++ src/system/accounts.ts | 25 +++++++++++++------------ 11 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/core/buffer.ts b/src/core/buffer.ts index 0c965b2..2f3ff39 100644 --- a/src/core/buffer.ts +++ b/src/core/buffer.ts @@ -8,15 +8,6 @@ * Telegram: https://t.me/terafoundation */ -module.exports.GetNewBuffer = GetNewBuffer; -module.exports.GetReadBuffer = GetReadBuffer; -module.exports.alloc = GetNewBuffer; -module.exports.from = GetReadBuffer; -module.exports.Write = Write; -module.exports.Read = Read; -module.exports.GetObjectFromBuffer = GetObjectFromBuffer; -module.exports.GetBufferFromObject = GetBufferFromObject; - function Write(buf, data, StringFormat?, ParamValue?, WorkStruct?) { if (buf.len >= buf.length) { return; @@ -497,7 +488,7 @@ function GetReadBuffer(buffer) { return buf; }; -function GetObjectFromBuffer(buffer, format, WorkStruct, bDisableTime) { +function GetObjectFromBuffer(buffer, format, WorkStruct, bDisableTime?) { var buf = Buffer.from(buffer); buf.len = 0; return Read(buf, format, undefined, WorkStruct, bDisableTime); @@ -596,3 +587,16 @@ function GetAttributes(Str) { } return arr; }; + +let alloc = GetNewBuffer; +let from = GetReadBuffer; +export { + GetNewBuffer, + GetReadBuffer, + alloc, + from, + Write, + Read, + GetObjectFromBuffer, + GetBufferFromObject, +} diff --git a/src/core/library.ts b/src/core/library.ts index fb3e37c..3d793fb 100644 --- a/src/core/library.ts +++ b/src/core/library.ts @@ -68,7 +68,8 @@ if (global.USE_PARAM_JS) { } require('../HTML/JS/terahashlib.js'); require("./crypto-library"); -global.BufLib = require("../core/buffer"); +import * as BufLib from '../core/buffer' +global.BufLib = BufLib; require('../HTML/JS/sha3.js'); require('../HTML/JS/coinlib.js'); global.GetCurrentBlockNumByTime = function GetCurrentBlockNumByTime() { diff --git a/src/core/rest-loader.ts b/src/core/rest-loader.ts index 77a927c..6d32a1f 100644 --- a/src/core/rest-loader.ts +++ b/src/core/rest-loader.ts @@ -236,7 +236,13 @@ export default class CRest extends CDB { global.ToLog("Send GETREST Num:" + Task.StartNum + "-" + Task.Count + " to " + global.NodeName(Ret.Node), 2) var SELF = this; this.SendF(Ret.Node, { - "Method": "GETREST", "Data": { BlockNum: Context.BlockNumRest, AccNum: Task.StartNum, Count: Task.Count, AccHash: Context.TxProof.AccHash }, + "Method": "GETREST", + "Data": { + BlockNum: Context.BlockNumRest, + AccNum: Task.StartNum, + Count: Task.Count, + AccHash: Context.TxProof.AccHash + }, "Context": { F: function(Info) { if (Task.OK) diff --git a/src/core/server.ts b/src/core/server.ts index 6793d2a..a611896 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -644,8 +644,7 @@ export default class CTransport extends CConnect { Info.Context.ContextID = Info.ContextID } global.ContextPackets.SaveValue(Info.ContextID, Info.Context) - } - else { + } else { Info.ContextID = [] } Node.SendPacketNum++ diff --git a/src/core/update.ts b/src/core/update.ts index 061c2f9..a3c93c9 100644 --- a/src/core/update.ts +++ b/src/core/update.ts @@ -163,7 +163,7 @@ function ReWriteDBSmartWrite() { var Item = global.DApps.Smart.DBSmart.Read(num); if (!Item) break; - var Body = global.GetArrFromHex(GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, {})); + var Body = global.GetArrFromHex(global.BufLib.GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, {})); if (Body.length > 15000) global.ToLog("Smart " + Item.Num + ". " + Item.Name + " length=" + Body.length); global.DApps.Smart.DBSmartWrite(Item); diff --git a/src/global.d.ts b/src/global.d.ts index c0ff984..f06a113 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -4,6 +4,7 @@ import { ChildProcess } from 'child_process' import CSmartContract from './core/transaction-validator' import CConsensus from './core/block-exchange' import CTransport from './core/server' +import * as BufLib from './core/buffer' declare global { var nw: any; @@ -281,7 +282,7 @@ declare global { Stun: any; ZIP: any; secp256k1: any; - BufLib: any; + BufLib: typeof BufLib; GetCurrentBlockNumByTime: Function; TimeStart: number; //#endregion diff --git a/src/process/pow-process.ts b/src/process/pow-process.ts index d5d1365..7b62993 100644 --- a/src/process/pow-process.ts +++ b/src/process/pow-process.ts @@ -33,6 +33,7 @@ powClient.on('message', (msg) => { FastCalcBlock(msg); break; case "Alive": + global.DELTA_CURRENT_TIME = msg.DELTA_CURRENT_TIME; break; case "Exit": PROCESS.exit(0); @@ -68,6 +69,9 @@ PROCESS.on('message', function(msg) { LastAlive = Date.now(); switch (msg.cmd) { case "Alive": + if (!powClient.connected) { + powClient.connect() + } break; case "Exit": PROCESS.exit(0); @@ -119,8 +123,8 @@ function PumpHash() { EndTime = CurTime; return; } - global.BlockPump.LastNonce = global.BlockPump.LastNonce + global.BlockPump.Num * 100000 - global.BlockPump.LastNonce0 = global.BlockPump.LastNonce0 + global.BlockPump.Num * 100000 + global.BlockPump.LastNonce = global.BlockPump.LastNonce + global.BlockPump.Num * 200000 + global.BlockPump.LastNonce0 = global.BlockPump.LastNonce0 + global.BlockPump.Num * 200000 CreatePOWVersionX(global.BlockPump, 1); } else { var Delta = CurTime - EndTime; diff --git a/src/process/static-process.ts b/src/process/static-process.ts index fb68b47..30bc1e3 100644 --- a/src/process/static-process.ts +++ b/src/process/static-process.ts @@ -134,7 +134,7 @@ function GETBLOCK(msg) { if (msg.Context.SendCount) { return; } - var BufWrite; + var BufWrite: Buffer; var BlockDB = global.SERVER.ReadBlockDB(BlockNum); var StrSend; if (BlockDB && (global.CompareArr(BlockDB.TreeHash, TreeHash) === 0 || global.IsZeroArr(TreeHash))) { diff --git a/src/rpc/client.ts b/src/rpc/client.ts index dd0174f..c2d342f 100644 --- a/src/rpc/client.ts +++ b/src/rpc/client.ts @@ -3,7 +3,9 @@ import * as io from 'socket.io-client' let host = process.env.TERA_HOST || 'http://127.0.0.1:8080/pow' let powClient = io(host, { - path: '/ws' + path: '/ws', + autoConnect: false, + reconnection: false }) powClient.on('connect', () => { @@ -14,7 +16,6 @@ powClient.on('connect', () => { powClient.on('disconnect', (error) => { console.log(`Client disconnect Error: ${error}...`) - process.exit(0) }); function SendToWebClient(msg: any) { diff --git a/src/run-client.ts b/src/run-client.ts index 17a0b87..3161453 100644 --- a/src/run-client.ts +++ b/src/run-client.ts @@ -72,6 +72,9 @@ async function CheckAlive() { if (ArrMiningWrk.length < thread) { AddPowProcess() } + for (var i = 0; i < ArrMiningWrk.length; i++) { + ArrMiningWrk[i].send({ cmd: "Alive" }); + } setTimeout(CheckAlive, 1000) } diff --git a/src/system/accounts.ts b/src/system/accounts.ts index 45cea80..4d915c9 100644 --- a/src/system/accounts.ts +++ b/src/system/accounts.ts @@ -27,9 +27,10 @@ const WorkStructTransfer2 = {}; const WorkStructTransfer3 = {}; import DApp from './dapp' class MerkleDBRow extends DBRow { - private MerkleTree - private MerkleArr - private MerkleCalc + MerkleTree + MerkleArr + MerkleCalc + MerkleHash constructor(FileName, DataSize, Format, bReadOnly) { super(FileName, DataSize, Format, bReadOnly) this.InitMerkleTree() @@ -62,7 +63,7 @@ class MerkleDBRow extends DBRow { } Write(Data) { var RetBuf: any = {}; - var bRes = DBRow.prototype.Write.call(this, Data, RetBuf); + var bRes = super.Write(Data, RetBuf) if (bRes) { var Hash = global.shaarr(RetBuf.Buf); this.MerkleArr[Data.Num] = Hash @@ -71,7 +72,7 @@ class MerkleDBRow extends DBRow { return bRes; } Truncate(LastNum) { - DBRow.prototype.Truncate.call(this, LastNum) + super.Truncate(LastNum) if (this.MerkleArr.length !== LastNum + 1) { this.MerkleArr.length = LastNum + 1 this.MerkleCalc[LastNum] = 1 @@ -82,18 +83,18 @@ export default class AccountApp extends DApp { CreateTrCount FORMAT_ACCOUNT_ROW SIZE_ACCOUNT_ROW - DBState + DBState: MerkleDBRow FORMAT_ACCOUNT_ROW_REST SIZE_ACCOUNT_ROW_REST - DBRest - DBAct - DBActPrev + DBRest: DBRow + DBAct: DBRow + DBActPrev: DBRow FORMAT_STATE_HISTORY - DBStateHistory + DBStateHistory: DBRow HistoryFormatArr DBStateTX: DBRow - DBAccountsHash - WasCheckRestDB + DBAccountsHash: DBRow + WasCheckRestDB: number ResultTx DBChanges constructor() {