feat: faster rest loader

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-08-08 09:43:50 +08:00
parent 6de01c7b9e
commit f8b5c6fb72
2 changed files with 68 additions and 64 deletions

View File

@ -291,17 +291,16 @@ function CheckGlobalTime() {
global.SAVE_CONST(); global.SAVE_CONST();
}); });
setTimeout(() => { setTimeout(() => {
global.DELTA_CURRENT_TIME = global.DELTA_CURRENT_TIME * -1; if (!global.CAN_START) {
setTimeout(() => { let temp = global.DELTA_CURRENT_TIME
global.DELTA_CURRENT_TIME = global.DELTA_CURRENT_TIME * -1 global.DELTA_CURRENT_TIME = 3000;
if (!global.CAN_START) { let checkTaskId = setInterval(() => {
let temp = global.DELTA_CURRENT_TIME if (global.CAN_START) {
global.DELTA_CURRENT_TIME = 3000;
setTimeout(() => {
global.DELTA_CURRENT_TIME = temp; global.DELTA_CURRENT_TIME = temp;
}, 3000) clearInterval(checkTaskId)
} }
}, 3000) }, 500)
}
}, 5000) }, 5000)
global.SAVE_CONST(); global.SAVE_CONST();
}; };

View File

@ -21,7 +21,8 @@ if (global.TEST_NETWORK) {
MIN_POW_CHAINS = 1; MIN_POW_CHAINS = 1;
global.COUNT_NODE_PROOF = 1; global.COUNT_NODE_PROOF = 1;
} }
var RestStartTime = 0;
var CurAccCount = 0;
export default class CRest extends CDB { export default class CRest extends CDB {
LoadRestContext: LoadContext LoadRestContext: LoadContext
LoadHistoryContext: LoadContext LoadHistoryContext: LoadContext
@ -212,6 +213,7 @@ export default class CRest extends CDB {
Context.AccTaskList = [] Context.AccTaskList = []
Context.AccTaskFinished = 0 Context.AccTaskFinished = 0
var AccCount = FindTx.AccountMax + 1; var AccCount = FindTx.AccountMax + 1;
CurAccCount = AccCount;
for (var n = 0; n < AccCount; n += global.MAX_ACCOUNTS_TRANSFER) { for (var n = 0; n < AccCount; n += global.MAX_ACCOUNTS_TRANSFER) {
var Task = { StartNum: n, Count: global.MAX_ACCOUNTS_TRANSFER, Time: 0, MapSend: {} }; var Task = { StartNum: n, Count: global.MAX_ACCOUNTS_TRANSFER, Time: 0, MapSend: {} };
if (Task.StartNum + Task.Count > AccCount) if (Task.StartNum + Task.Count > AccCount)
@ -234,6 +236,7 @@ export default class CRest extends CDB {
} }
} }
Context.Mode++ Context.Mode++
RestStartTime = Date.now();
global.ToLog("Next mode: " + Context.Mode, 2) global.ToLog("Next mode: " + Context.Mode, 2)
break; break;
case 6: case 6:
@ -248,60 +251,62 @@ export default class CRest extends CDB {
for (var i = 0; i < Context.AccTaskList.length; i++) { for (var i = 0; i < Context.AccTaskList.length; i++) {
let Task = Context.AccTaskList[i]; let Task = Context.AccTaskList[i];
var Delta = CurTime - Task.Time; var Delta = CurTime - Task.Time;
if (Delta > 5 * 1000 && !Task.OK) { if (Delta > 1 * 1000 && !Task.OK) {
var Ret = this.GetNextNode(Task, "", 1); setTimeout(() => {
if (Ret.Result) { var Ret = this.GetNextNode(Task, "", 1);
global.ToLog("Send GETREST Num:" + Task.StartNum + "-" + Task.Count + " to " + global.NodeName(Ret.Node), 2) if (Ret.Result) {
var SELF = this; global.ToLog("Send GETREST Num:" + Task.StartNum + "-" + Task.Count + " to " + global.NodeName(Ret.Node), 2)
this.SendF(Ret.Node, { var SELF = this;
"Method": "GETREST", this.SendF(Ret.Node, {
"Data": { "Method": "GETREST",
BlockNum: Context.BlockNumRest, "Data": {
AccNum: Task.StartNum, BlockNum: Context.BlockNumRest,
Count: Task.Count, AccNum: Task.StartNum,
AccHash: Context.TxProof.AccHash Count: Task.Count,
}, AccHash: Context.TxProof.AccHash
"Context": { },
F: function(Info) { "Context": {
if (Task.OK) F: function(Info) {
return; if (Task.OK)
var Data = SELF.DataFromF(Info); return;
if (!Data.Result) var Data = SELF.DataFromF(Info);
return; if (!Data.Result)
if (Data.Version !== 1) { return;
global.ToLog("ERROR Version Result GETREST Num:" + Task.StartNum + " from " + global.NodeName(Info.Node), 2) if (Data.Version !== 1) {
return; 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) Task.Time = CurTime
return; }
} }, 0)
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;
}
} }
} }
break; break;