refactor: keep client online and optimiz code
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		@@ -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,
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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() {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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++
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								src/global.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								src/global.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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))) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user