From f8b5c6fb72da285cf42b75cbbd7c9f7e3c8e2d72 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Thu, 8 Aug 2019 09:43:50 +0800 Subject: [PATCH] feat: faster rest loader Signed-off-by: MiaoWoo --- src/core/library.ts | 19 ++++--- src/core/rest-loader.ts | 113 +++++++++++++++++++++------------------- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/src/core/library.ts b/src/core/library.ts index 3e93659..811831f 100644 --- a/src/core/library.ts +++ b/src/core/library.ts @@ -291,17 +291,16 @@ function CheckGlobalTime() { global.SAVE_CONST(); }); setTimeout(() => { - global.DELTA_CURRENT_TIME = global.DELTA_CURRENT_TIME * -1; - setTimeout(() => { - global.DELTA_CURRENT_TIME = global.DELTA_CURRENT_TIME * -1 - if (!global.CAN_START) { - let temp = global.DELTA_CURRENT_TIME - global.DELTA_CURRENT_TIME = 3000; - setTimeout(() => { + if (!global.CAN_START) { + let temp = global.DELTA_CURRENT_TIME + global.DELTA_CURRENT_TIME = 3000; + let checkTaskId = setInterval(() => { + if (global.CAN_START) { global.DELTA_CURRENT_TIME = temp; - }, 3000) - } - }, 3000) + clearInterval(checkTaskId) + } + }, 500) + } }, 5000) global.SAVE_CONST(); }; diff --git a/src/core/rest-loader.ts b/src/core/rest-loader.ts index faaec23..b2a5c9f 100644 --- a/src/core/rest-loader.ts +++ b/src/core/rest-loader.ts @@ -21,7 +21,8 @@ if (global.TEST_NETWORK) { MIN_POW_CHAINS = 1; global.COUNT_NODE_PROOF = 1; } - +var RestStartTime = 0; +var CurAccCount = 0; export default class CRest extends CDB { LoadRestContext: LoadContext LoadHistoryContext: LoadContext @@ -212,6 +213,7 @@ export default class CRest extends CDB { Context.AccTaskList = [] Context.AccTaskFinished = 0 var AccCount = FindTx.AccountMax + 1; + CurAccCount = AccCount; for (var n = 0; n < AccCount; n += global.MAX_ACCOUNTS_TRANSFER) { var Task = { StartNum: n, Count: global.MAX_ACCOUNTS_TRANSFER, Time: 0, MapSend: {} }; if (Task.StartNum + Task.Count > AccCount) @@ -234,6 +236,7 @@ export default class CRest extends CDB { } } Context.Mode++ + RestStartTime = Date.now(); global.ToLog("Next mode: " + Context.Mode, 2) break; case 6: @@ -248,60 +251,62 @@ export default class CRest extends CDB { for (var i = 0; i < Context.AccTaskList.length; i++) { let Task = Context.AccTaskList[i]; var Delta = CurTime - Task.Time; - if (Delta > 5 * 1000 && !Task.OK) { - var Ret = this.GetNextNode(Task, "", 1); - if (Ret.Result) { - 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 - }, - "Context": { - F: function(Info) { - if (Task.OK) - return; - var Data = SELF.DataFromF(Info); - if (!Data.Result) - return; - if (Data.Version !== 1) { - global.ToLog("ERROR Version Result GETREST Num:" + Task.StartNum + " from " + global.NodeName(Info.Node), 2) - return; + if (Delta > 1 * 1000 && !Task.OK) { + setTimeout(() => { + var Ret = this.GetNextNode(Task, "", 1); + if (Ret.Result) { + 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 + }, + "Context": { + F: function(Info) { + if (Task.OK) + return; + var Data = SELF.DataFromF(Info); + if (!Data.Result) + return; + if (Data.Version !== 1) { + global.ToLog("ERROR Version Result GETREST Num:" + Task.StartNum + " from " + global.NodeName(Info.Node), 2) + return; + } + if (global.CompareArrL(Data.ProofHash, Context.TxProof.AccHash) !== 0) { + global.ToLog("ERROR PROOF HASH Result GETREST Num:" + Task.StartNum + " Hash: " + global.GetHexFromArr(Data.ProofHash) + "/" + global.GetHexFromArr(Context.TxProof.AccHash) + " from " + global.NodeName(Info.Node), + 2) + return; + } + var ArrM = []; + for (var i = 0; i < Data.Arr.length; i++) { + ArrM[i] = global.shaarr(Data.Arr[i]) + } + var GetHash = global.CheckMerkleProof(Data.ProofArrL, ArrM, Data.ProofArrR); + if (global.CompareArrL(GetHash, Context.TxProof.AccHash) !== 0) { + global.ToLog("ERROR CALC PROOF HASH Result GETREST Num:" + Task.StartNum + " Hash: " + global.GetHexFromArr(GetHash) + "/" + global.GetHexFromArr(Context.TxProof.AccHash) + " from " + global.NodeName(Info.Node), + 2) + return; + } + global.ToLog("OK Result GETREST Num:" + Task.StartNum + " arr=" + Data.Arr.length + " from " + global.NodeName(Info.Node), 2) + if (!global.TX_PROCESS || !global.TX_PROCESS.RunRPC) { + global.ToLog("ERROR global.TX_PROCESS") + return; + } + Task.OK = true + global.TX_PROCESS.RunRPC("TXWriteAccArr", { StartNum: Task.StartNum, Arr: Data.Arr }, function(Err, Params) { + Context.AccTaskFinished++ + global.ToLog(`GETREST ${Context.AccTaskFinished * global.MAX_ACCOUNTS_TRANSFER}/${CurAccCount}(${(Context.AccTaskFinished / Context.AccTaskList.length * 100).toFixed(2)}%) TimeCost: ${Date.now() - RestStartTime}ms`) + }) } - if (global.CompareArrL(Data.ProofHash, Context.TxProof.AccHash) !== 0) { - global.ToLog("ERROR PROOF HASH Result GETREST Num:" + Task.StartNum + " Hash: " + global.GetHexFromArr(Data.ProofHash) + "/" + global.GetHexFromArr(Context.TxProof.AccHash) + " from " + global.NodeName(Info.Node), - 2) - return; - } - var ArrM = []; - for (var i = 0; i < Data.Arr.length; i++) { - ArrM[i] = global.shaarr(Data.Arr[i]) - } - var GetHash = global.CheckMerkleProof(Data.ProofArrL, ArrM, Data.ProofArrR); - if (global.CompareArrL(GetHash, Context.TxProof.AccHash) !== 0) { - global.ToLog("ERROR CALC PROOF HASH Result GETREST Num:" + Task.StartNum + " Hash: " + global.GetHexFromArr(GetHash) + "/" + global.GetHexFromArr(Context.TxProof.AccHash) + " from " + global.NodeName(Info.Node), - 2) - return; - } - global.ToLog("OK Result GETREST Num:" + Task.StartNum + " arr=" + Data.Arr.length + " from " + global.NodeName(Info.Node), 2) - if (!global.TX_PROCESS || !global.TX_PROCESS.RunRPC) { - global.ToLog("ERROR global.TX_PROCESS") - return; - } - Task.OK = true - global.TX_PROCESS.RunRPC("TXWriteAccArr", { StartNum: Task.StartNum, Arr: Data.Arr }, function(Err, Params) { - Context.AccTaskFinished++ - }) - } - }, - }) - Task.Time = CurTime - break; - } + }, + }) + Task.Time = CurTime + } + }, 0) } } break;