forked from circlecloud/tera
refactor: keep client online and optimiz code
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
ae1242686d
commit
8d0cb723c1
@ -8,15 +8,6 @@
|
|||||||
* Telegram: https://t.me/terafoundation
|
* 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?) {
|
function Write(buf, data, StringFormat?, ParamValue?, WorkStruct?) {
|
||||||
if (buf.len >= buf.length) {
|
if (buf.len >= buf.length) {
|
||||||
return;
|
return;
|
||||||
@ -497,7 +488,7 @@ function GetReadBuffer(buffer) {
|
|||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
function GetObjectFromBuffer(buffer, format, WorkStruct, bDisableTime) {
|
function GetObjectFromBuffer(buffer, format, WorkStruct, bDisableTime?) {
|
||||||
var buf = Buffer.from(buffer);
|
var buf = Buffer.from(buffer);
|
||||||
buf.len = 0;
|
buf.len = 0;
|
||||||
return Read(buf, format, undefined, WorkStruct, bDisableTime);
|
return Read(buf, format, undefined, WorkStruct, bDisableTime);
|
||||||
@ -596,3 +587,16 @@ function GetAttributes(Str) {
|
|||||||
}
|
}
|
||||||
return arr;
|
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('../HTML/JS/terahashlib.js');
|
||||||
require("./crypto-library");
|
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/sha3.js');
|
||||||
require('../HTML/JS/coinlib.js');
|
require('../HTML/JS/coinlib.js');
|
||||||
global.GetCurrentBlockNumByTime = function GetCurrentBlockNumByTime() {
|
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)
|
global.ToLog("Send GETREST Num:" + Task.StartNum + "-" + Task.Count + " to " + global.NodeName(Ret.Node), 2)
|
||||||
var SELF = this;
|
var SELF = this;
|
||||||
this.SendF(Ret.Node, {
|
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": {
|
"Context": {
|
||||||
F: function(Info) {
|
F: function(Info) {
|
||||||
if (Task.OK)
|
if (Task.OK)
|
||||||
|
@ -644,8 +644,7 @@ export default class CTransport extends CConnect {
|
|||||||
Info.Context.ContextID = Info.ContextID
|
Info.Context.ContextID = Info.ContextID
|
||||||
}
|
}
|
||||||
global.ContextPackets.SaveValue(Info.ContextID, Info.Context)
|
global.ContextPackets.SaveValue(Info.ContextID, Info.Context)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Info.ContextID = []
|
Info.ContextID = []
|
||||||
}
|
}
|
||||||
Node.SendPacketNum++
|
Node.SendPacketNum++
|
||||||
|
@ -163,7 +163,7 @@ function ReWriteDBSmartWrite() {
|
|||||||
var Item = global.DApps.Smart.DBSmart.Read(num);
|
var Item = global.DApps.Smart.DBSmart.Read(num);
|
||||||
if (!Item)
|
if (!Item)
|
||||||
break;
|
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)
|
if (Body.length > 15000)
|
||||||
global.ToLog("Smart " + Item.Num + ". " + Item.Name + " length=" + Body.length);
|
global.ToLog("Smart " + Item.Num + ". " + Item.Name + " length=" + Body.length);
|
||||||
global.DApps.Smart.DBSmartWrite(Item);
|
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 CSmartContract from './core/transaction-validator'
|
||||||
import CConsensus from './core/block-exchange'
|
import CConsensus from './core/block-exchange'
|
||||||
import CTransport from './core/server'
|
import CTransport from './core/server'
|
||||||
|
import * as BufLib from './core/buffer'
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
var nw: any;
|
var nw: any;
|
||||||
@ -281,7 +282,7 @@ declare global {
|
|||||||
Stun: any;
|
Stun: any;
|
||||||
ZIP: any;
|
ZIP: any;
|
||||||
secp256k1: any;
|
secp256k1: any;
|
||||||
BufLib: any;
|
BufLib: typeof BufLib;
|
||||||
GetCurrentBlockNumByTime: Function;
|
GetCurrentBlockNumByTime: Function;
|
||||||
TimeStart: number;
|
TimeStart: number;
|
||||||
//#endregion
|
//#endregion
|
||||||
|
@ -33,6 +33,7 @@ powClient.on('message', (msg) => {
|
|||||||
FastCalcBlock(msg);
|
FastCalcBlock(msg);
|
||||||
break;
|
break;
|
||||||
case "Alive":
|
case "Alive":
|
||||||
|
global.DELTA_CURRENT_TIME = msg.DELTA_CURRENT_TIME;
|
||||||
break;
|
break;
|
||||||
case "Exit":
|
case "Exit":
|
||||||
PROCESS.exit(0);
|
PROCESS.exit(0);
|
||||||
@ -68,6 +69,9 @@ PROCESS.on('message', function(msg) {
|
|||||||
LastAlive = Date.now();
|
LastAlive = Date.now();
|
||||||
switch (msg.cmd) {
|
switch (msg.cmd) {
|
||||||
case "Alive":
|
case "Alive":
|
||||||
|
if (!powClient.connected) {
|
||||||
|
powClient.connect()
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "Exit":
|
case "Exit":
|
||||||
PROCESS.exit(0);
|
PROCESS.exit(0);
|
||||||
@ -119,8 +123,8 @@ function PumpHash() {
|
|||||||
EndTime = CurTime;
|
EndTime = CurTime;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
global.BlockPump.LastNonce = global.BlockPump.LastNonce + global.BlockPump.Num * 100000
|
global.BlockPump.LastNonce = global.BlockPump.LastNonce + global.BlockPump.Num * 200000
|
||||||
global.BlockPump.LastNonce0 = global.BlockPump.LastNonce0 + global.BlockPump.Num * 100000
|
global.BlockPump.LastNonce0 = global.BlockPump.LastNonce0 + global.BlockPump.Num * 200000
|
||||||
CreatePOWVersionX(global.BlockPump, 1);
|
CreatePOWVersionX(global.BlockPump, 1);
|
||||||
} else {
|
} else {
|
||||||
var Delta = CurTime - EndTime;
|
var Delta = CurTime - EndTime;
|
||||||
|
@ -134,7 +134,7 @@ function GETBLOCK(msg) {
|
|||||||
if (msg.Context.SendCount) {
|
if (msg.Context.SendCount) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var BufWrite;
|
var BufWrite: Buffer;
|
||||||
var BlockDB = global.SERVER.ReadBlockDB(BlockNum);
|
var BlockDB = global.SERVER.ReadBlockDB(BlockNum);
|
||||||
var StrSend;
|
var StrSend;
|
||||||
if (BlockDB && (global.CompareArr(BlockDB.TreeHash, TreeHash) === 0 || global.IsZeroArr(TreeHash))) {
|
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 host = process.env.TERA_HOST || 'http://127.0.0.1:8080/pow'
|
||||||
|
|
||||||
let powClient = io(host, {
|
let powClient = io(host, {
|
||||||
path: '/ws'
|
path: '/ws',
|
||||||
|
autoConnect: false,
|
||||||
|
reconnection: false
|
||||||
})
|
})
|
||||||
|
|
||||||
powClient.on('connect', () => {
|
powClient.on('connect', () => {
|
||||||
@ -14,7 +16,6 @@ powClient.on('connect', () => {
|
|||||||
|
|
||||||
powClient.on('disconnect', (error) => {
|
powClient.on('disconnect', (error) => {
|
||||||
console.log(`Client disconnect Error: ${error}...`)
|
console.log(`Client disconnect Error: ${error}...`)
|
||||||
process.exit(0)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function SendToWebClient(msg: any) {
|
function SendToWebClient(msg: any) {
|
||||||
|
@ -72,6 +72,9 @@ async function CheckAlive() {
|
|||||||
if (ArrMiningWrk.length < thread) {
|
if (ArrMiningWrk.length < thread) {
|
||||||
AddPowProcess()
|
AddPowProcess()
|
||||||
}
|
}
|
||||||
|
for (var i = 0; i < ArrMiningWrk.length; i++) {
|
||||||
|
ArrMiningWrk[i].send({ cmd: "Alive" });
|
||||||
|
}
|
||||||
setTimeout(CheckAlive, 1000)
|
setTimeout(CheckAlive, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,9 +27,10 @@ const WorkStructTransfer2 = {};
|
|||||||
const WorkStructTransfer3 = {};
|
const WorkStructTransfer3 = {};
|
||||||
import DApp from './dapp'
|
import DApp from './dapp'
|
||||||
class MerkleDBRow extends DBRow {
|
class MerkleDBRow extends DBRow {
|
||||||
private MerkleTree
|
MerkleTree
|
||||||
private MerkleArr
|
MerkleArr
|
||||||
private MerkleCalc
|
MerkleCalc
|
||||||
|
MerkleHash
|
||||||
constructor(FileName, DataSize, Format, bReadOnly) {
|
constructor(FileName, DataSize, Format, bReadOnly) {
|
||||||
super(FileName, DataSize, Format, bReadOnly)
|
super(FileName, DataSize, Format, bReadOnly)
|
||||||
this.InitMerkleTree()
|
this.InitMerkleTree()
|
||||||
@ -62,7 +63,7 @@ class MerkleDBRow extends DBRow {
|
|||||||
}
|
}
|
||||||
Write(Data) {
|
Write(Data) {
|
||||||
var RetBuf: any = {};
|
var RetBuf: any = {};
|
||||||
var bRes = DBRow.prototype.Write.call(this, Data, RetBuf);
|
var bRes = super.Write(Data, RetBuf)
|
||||||
if (bRes) {
|
if (bRes) {
|
||||||
var Hash = global.shaarr(RetBuf.Buf);
|
var Hash = global.shaarr(RetBuf.Buf);
|
||||||
this.MerkleArr[Data.Num] = Hash
|
this.MerkleArr[Data.Num] = Hash
|
||||||
@ -71,7 +72,7 @@ class MerkleDBRow extends DBRow {
|
|||||||
return bRes;
|
return bRes;
|
||||||
}
|
}
|
||||||
Truncate(LastNum) {
|
Truncate(LastNum) {
|
||||||
DBRow.prototype.Truncate.call(this, LastNum)
|
super.Truncate(LastNum)
|
||||||
if (this.MerkleArr.length !== LastNum + 1) {
|
if (this.MerkleArr.length !== LastNum + 1) {
|
||||||
this.MerkleArr.length = LastNum + 1
|
this.MerkleArr.length = LastNum + 1
|
||||||
this.MerkleCalc[LastNum] = 1
|
this.MerkleCalc[LastNum] = 1
|
||||||
@ -82,18 +83,18 @@ export default class AccountApp extends DApp {
|
|||||||
CreateTrCount
|
CreateTrCount
|
||||||
FORMAT_ACCOUNT_ROW
|
FORMAT_ACCOUNT_ROW
|
||||||
SIZE_ACCOUNT_ROW
|
SIZE_ACCOUNT_ROW
|
||||||
DBState
|
DBState: MerkleDBRow
|
||||||
FORMAT_ACCOUNT_ROW_REST
|
FORMAT_ACCOUNT_ROW_REST
|
||||||
SIZE_ACCOUNT_ROW_REST
|
SIZE_ACCOUNT_ROW_REST
|
||||||
DBRest
|
DBRest: DBRow
|
||||||
DBAct
|
DBAct: DBRow
|
||||||
DBActPrev
|
DBActPrev: DBRow
|
||||||
FORMAT_STATE_HISTORY
|
FORMAT_STATE_HISTORY
|
||||||
DBStateHistory
|
DBStateHistory: DBRow
|
||||||
HistoryFormatArr
|
HistoryFormatArr
|
||||||
DBStateTX: DBRow
|
DBStateTX: DBRow
|
||||||
DBAccountsHash
|
DBAccountsHash: DBRow
|
||||||
WasCheckRestDB
|
WasCheckRestDB: number
|
||||||
ResultTx
|
ResultTx
|
||||||
DBChanges
|
DBChanges
|
||||||
constructor() {
|
constructor() {
|
||||||
|
Loading…
Reference in New Issue
Block a user