From b8239f6da9aa1e53af37f517a9bbc797badbaef9 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 13 Jul 2019 14:33:59 +0800 Subject: [PATCH] feat: sync system code Signed-off-by: MiaoWoo --- src/core/log.ts | 595 +++++++++++++++++++++++++++-------------- src/global.d.ts | 2 + src/system/accounts.ts | 52 ++-- src/system/dapp.ts | 16 +- src/system/file.ts | 6 +- src/system/messager.ts | 14 +- src/system/names.ts | 7 +- src/system/smart.ts | 12 +- 8 files changed, 427 insertions(+), 277 deletions(-) diff --git a/src/core/log.ts b/src/core/log.ts index af0ad3c..b9e8b56 100644 --- a/src/core/log.ts +++ b/src/core/log.ts @@ -7,236 +7,417 @@ * Twitter: https://twitter.com/terafoundation * Telegram: https://t.me/terafoundation */ -import * as fs from 'fs' -import './constant' -import './log-strict' - -var file_name_info = global.GetDataPath("info.log") +require("./constant.js"); +var fs = require('fs'); +require("./log-strict.js"); +var file_name_info = global.GetDataPath("info.log"); var file_name_infoPrev = global.GetDataPath("info-prev.log"); global.CheckSizeLogFile(file_name_info, file_name_infoPrev); -var file_name_log = global.GetDataPath("log.log") +var file_name_log = global.GetDataPath("log.log"); var file_name_logPrev = global.GetDataPath("log-prev.log"); global.CheckSizeLogFile(file_name_log, file_name_logPrev); -var file_name_log_web = global.GetDataPath("web.log") +var file_name_log_web = global.GetDataPath("web.log"); var file_name_log_webPrev = global.GetDataPath("web-prev.log"); global.CheckSizeLogFile(file_name_log_web, file_name_log_webPrev); -var StartStatTime, file_name_error = global.GetDataPath("err.log") +var file_name_error = global.GetDataPath("err.log"); var file_name_errorPrev = global.GetDataPath("err-prev.log"); - -function ToLogFile(e, t, r?) { - t instanceof Error && (t = t.message + "\n" + t.stack), global.START_SERVER || (t = global.PROCESS_NAME + ": " + t), "MAIN" !== global.PROCESS_NAME && process.send ? process.send({ - cmd: "log", - message: t - }) : (console.log(global.START_PORT_NUMBER + ": " + global.GetStrOnlyTime() + ": " + t), r || SaveToLogFileSync(e, t)); +global.CheckSizeLogFile(file_name_error, file_name_errorPrev); +var file_name_error_tx = global.GetDataPath("err-tx.log"); +var file_name_error_txPrev = global.GetDataPath("err-tx-prev.log"); +global.CheckSizeLogFile(file_name_error_tx, file_name_error_txPrev); +global.ToLog = function(Str, Level) { + if (Level === undefined) + Level = 1; + if (Level && Level > global.LOG_LEVEL) + return; + if (global.ALL_LOG_TO_CLIENT) + ToLogClient(Str, undefined, undefined); + else + ToLogFile(file_name_log, Str); }; - -function ToLogClient(e, t, r) { - let ArrLogClient = global.ArrLogClient; - e && (ToLogFile(file_name_log, e), t = t || "", ArrLogClient.push({ text: global.GetStrOnlyTime() + " " + e, key: t, final: r }), 13 < ArrLogClient.length && ArrLogClient.shift()); -}; -global.CheckSizeLogFile(file_name_error, file_name_errorPrev), global.ToLog = function(e, t) { - void 0 === t && (t = 1), t && t > global.LOG_LEVEL || (global.ALL_LOG_TO_CLIENT ? ToLogClient(e, void 0, void 0) : ToLogFile(file_name_log, - e)); -}, global.ToLogWeb = function(e) { -}, global.SmallAddr = function(e) { - return e.substr(0, 5); -}, global.ToErrorTrace = function(e) { - global.ToError(e + ":" + (new Error).stack); -}, global.ToLogTrace = function(e) { - global.ToErrorTrace(e); -}, global.ToInfo = function(e) { - ToLogFile(file_name_info, e, 1); -}, global.ToError = function(e) { - ToLogFile(file_name_error, e); -}, global.ArrLogClient = [], global.ToLogClient = ToLogClient, global.ToLogClient0 = ToLogClient; -var CONTEXT_STATS = { Total: {}, Interval: [] }, CONTEXT_ERRORS = { Total: {}, Interval: [] }, CurStatIndex = 0; -let MAX_STAT_PERIOD = global.MAX_STAT_PERIOD; -function GetCurrentStatIndex() { - var e = 2 * MAX_STAT_PERIOD + 2; - return CurStatIndex % e; -}; - -function ResizeArrMax(e) { - for (var t = [], r = Math.trunc(e.length / 2), o = 0; o < r; o++) - t[o] = Math.max(e[2 * o], e[2 * o + 1]); - return t; -}; - -function ResizeArrAvg(e) { - for (var t = [], r = Math.trunc(e.length / 2), o = 0; o < r; o++) - t[o] = (e[2 * o] + e[2 * o + 1]) / 2; - return t; -}; - -// function ResizeArr(e) { -// for (var t = [], r = Math.trunc(e.length / 2), o = 0; o < r; o++) -// t[o] = e[2 * o]; -// return t; -// }; - -function GetDiagramData(e, t) { - var r, o = 2 * MAX_STAT_PERIOD + 2; - r = "MAX:" === t.substr(0, 4); - for (var n, a = MAX_STAT_PERIOD, l = (GetCurrentStatIndex() - a + o) % o, i = (e.Total, []), T = void 0, g = l; g < l + a; g++) { - var S = g % o; - if (n = e.Interval[S]) { - var _ = n[t]; - void 0 !== _ ? r ? i.push(_) : (void 0 !== T ? i.push(_ - T) : i.push(_), T = _) : i.push(0); - } - } - return i; -}; - -function CalcInterval(e, t, r) { - for (var o, n = 2 * MAX_STAT_PERIOD + 2, a = {}, l = (t - r + n) % n, i = e.Total, T = l; T < l + r; T++) { - var g = T % n; - if (o = e.Interval[g]) - break; - } - if (o) - for (var S in i) - "MAX:" === S.substr(0, 4) ? a[S] = 0 : void 0 === o[S] ? a[S] = i[S] : a[S] = i[S] - o[S]; - return a; -}; - -function AddToStatContext(e, t, r?) { - void 0 === r && (r = 1); - var o = e.Total[t]; - o = o || 0, "MAX:" === t.substr(0, 4) ? o = Math.max(o, r) : o += r, e.Total[t] = o, StartStatTime = StartStatTime || global.GetCurrentTime(0); -}; - -function CopyStatInterval(e, t) { - var r = e.Interval[t]; - r || (r = {}, e.Interval[t] = r); - var o = e.Total; - for (var n in o) - r[n] = o[n], "MAX:" === n.substr(0, 4) && (o[n] = 0); -}; - -// function SaveToLogFileAsync(e, o) { -// fs.open(e, "a", void 0, function(e, r) { -// if (e) -// console.log("Ошибка открытия лог-файла ошибок"); -// else { -// var t = global.GetStrTime() + " : " + o + "\r\n"; -// fs.write(r, t, null, "utf8", function(e, t) { -// e ? console.log("Ошибка записи в лог-файл ошибок!") : fs.close(r, function(e) { -// e && console.log(e); -// }); -// }); -// } -// }); -// }; - -function SaveToLogFileSync(e, t) { - try { - var r = global.GetStrTime() + " : " + t + "\r\n", o = fs.openSync(e, "a"); - fs.writeSync(o, r, null, "utf8"), fs.closeSync(o); - } - catch (e) { - console.log(e.message); +global.WEB_LOG = 0; +global.ToLogWeb = function(Str) { + if (global.WEB_LOG) { + SaveToLogFileSync(file_name_log_web, Str); } }; +global.SmallAddr = function(Str) { + return Str.substr(0, 5); +}; +global.ToErrorTrace = function(Str) { + global.ToError("" + Str + ":" + new Error().stack); +}; +global.ToLogTrace = function(Str) { + global.ToErrorTrace(Str); +}; +global.ToInfo = function(Str) { + ToLogFile(file_name_info, Str, 1); +}; +global.ToError = function(Str) { + ToLogFile(file_name_error, Str); +}; +global.ToErrorTx = function(Str) { + SaveToLogFileSync(file_name_error_tx, Str); + global.ToLog(Str); +}; + +function ToLogFile(file_name, Str, bNoFile?) { + if (Str instanceof Error) { + Str = Str.message + "\n" + Str.stack; + } + if (!global.START_SERVER) + Str = global.PROCESS_NAME + ": " + Str; + if (global.PROCESS_NAME !== "MAIN" && process.send) { + process.send({ cmd: "log", message: Str }); + return; + } else { + console.log("" + global.START_PORT_NUMBER + ": " + global.GetStrOnlyTime() + ": " + Str); + } + if (bNoFile) + return; + SaveToLogFileSync(file_name, Str); +}; +global.ArrLogClient = []; + +function ToLogClient(Str, StrKey, bFinal) { + if (!Str) + return; + ToLogFile(file_name_log, Str); + if (!StrKey) + StrKey = ""; + global.ArrLogClient.push({ text: global.GetStrOnlyTime() + " " + Str, key: StrKey, final: bFinal, }); + if (global.ArrLogClient.length > 13) + global.ArrLogClient.shift(); +}; +global.ToLogClient = ToLogClient; +global.ToLogClient0 = ToLogClient; +var StartStatTime; +var CONTEXT_STATS = { Total: {}, Interval: [] }; +var CONTEXT_ERRORS = { Total: {}, Interval: [] }; +var CurStatIndex = 0; global.PrepareStatEverySecond = function() { CurStatIndex++; - var e = GetCurrentStatIndex(); - CopyStatInterval(CONTEXT_STATS, e), CopyStatInterval(CONTEXT_ERRORS, e); -} -global.TO_ERROR_LOG = function(e, t, r, o, n, a) { - r instanceof Error && (r = r.message + "\n"), "rinfo" === o ? r += " from: " + n.address + ":" + n.port : "node" === o && (r += " from: " + n.ip + ":" + n.port); - var l = e + ":" + t; - global.ToError(" ==ERROR== " + l + " " + r), AddToStatContext(CONTEXT_ERRORS, l), global.ADD_TO_STAT("ERRORS"); -} -global.HASH_RATE = 0, global.ADD_HASH_RATE = function(e) { - e /= 1e6, global.HASH_RATE += e, global.ADD_TO_STAT("HASHRATE", e); -} -global.GET_STAT = function(e) { - var t = CONTEXT_STATS.Total[e]; - return t = t || 0; -} -global.ADD_TO_STAT_TIME = function(e, t, r) { - if (global.STAT_MODE) { - if (r && 2 !== global.STAT_MODE) - return; - var o = process.hrtime(t), n = 1e3 * o[0] + o[1] / 1e6; - global.ADD_TO_STAT(e, n); + var index = GetCurrentStatIndex(); + CopyStatInterval(CONTEXT_STATS, index); + CopyStatInterval(CONTEXT_ERRORS, index); +}; +global.TO_ERROR_LOG = function(Module, ErrNum, Str, type, data1, data2) { + if (Str instanceof Error) { + Str = Str.message + "\n"; } -} -global.ADD_TO_STAT = function(e, t, r) { + if (type === "rinfo") + Str += " from: " + data1.address + ':' + data1.port; + else + if (type === "node") + Str += " from: " + data1.ip + ':' + data1.port; + var Key = Module + ":" + ErrNum; + global.ToError(" ==ERROR== " + Key + " " + Str); + AddToStatContext(CONTEXT_ERRORS, Key); + global.ADD_TO_STAT("ERRORS"); +}; + +function GetCurrentStatIndex() { + var DefMaxStatPeriod = global.MAX_STAT_PERIOD * 2 + 2; + return CurStatIndex % DefMaxStatPeriod; +}; +global.HASH_RATE = 0; +global.ADD_HASH_RATE = function(Count) { + Count = Count / 1000000; + global.HASH_RATE += Count; + global.ADD_TO_STAT("HASHRATE", Count); +}; +global.GET_STAT = function(Key) { + var Val = CONTEXT_STATS.Total[Key]; + if (!Val) + Val = 0; + return Val; +}; +global.ADD_TO_STAT_TIME = function(Name, startTime, bDetail) { if (global.STAT_MODE) { - if (r && 2 !== global.STAT_MODE) + if (bDetail && global.STAT_MODE !== 2) return; - AddToStatContext(CONTEXT_STATS, e, t); + var Time = process.hrtime(startTime); + var deltaTime = Time[0] * 1000 + Time[1] / 1e6; + global.ADD_TO_STAT(Name, deltaTime); } -} -global.GET_STATDIAGRAMS = function(e) { - global.GetCurrentTime(); - var t = GetCurrentStatIndex(); - if (!e || !e.length) +}; +global.ADD_TO_STAT = function(Key, Count, bDetail) { + if (global.STAT_MODE) { + if (bDetail && global.STAT_MODE !== 2) + return; + AddToStatContext(CONTEXT_STATS, Key, Count); + } +}; +global.GET_STATDIAGRAMS = function(Keys) { + var now = global.GetCurrentTime(); + var index = GetCurrentStatIndex(); + if (!Keys || !Keys.length) return []; - for (var r = [], o = 0; o < e.length; o++) { - var n = e[o], a = GetDiagramData(CONTEXT_STATS, n); - r.push({ name: n, maxindex: t, arr: a, starttime: StartStatTime - 0, steptime: 1 }); + var Data = []; + for (var i = 0; i < Keys.length; i++) { + var name = Keys[i]; + var Value = GetDiagramData(CONTEXT_STATS, name); + Data.push({ name: name, maxindex: index, arr: Value, starttime: (StartStatTime - 0), steptime: 1 }); } - var l = void 0; - for (o = 0; o < r.length; o++) { - 0 < (T = r[o].arr).length && (void 0 === l || T.length < l) && (l = T.length); + var MinLength = undefined; + for (var i = 0; i < Data.length; i++) { + var arr = Data[i].arr; + if (arr.length > 0 && (MinLength === undefined || arr.length < MinLength)) + MinLength = arr.length; } - for (o = 0; o < r.length; o++) { - var i = r[o], T = i.arr; - l && T.length > l && (T = T.slice(T.length - l)), l && 0 <= ",POWER_MY_WIN,POWER_BLOCKCHAIN,".indexOf("," + i.name + ",") && (T = global.SERVER.GetStatBlockchain(i.name, - l)); - for (var g = 0, S = 0; S < T.length; S++) - T[S] && (g += T[S]); - 0 < T.length && (g /= T.length); - var _ = 1; - if ("MAX:" === i.name.substr(0, 4)) - for (; 500 <= T.length;) - T = ResizeArrMax(T), _ *= 2; - else - for (; 500 <= T.length;) - T = ResizeArrAvg(T), _ *= 2; - i.AvgValue = g, i.steptime = _, i.arr = T.slice(1); + var MaxSizeArr = 500; + for (var i = 0; i < Data.length; i++) { + var ItemServer = Data[i]; + var arr = ItemServer.arr; + if (MinLength && arr.length > MinLength) { + arr = arr.slice(arr.length - MinLength); + } + if (MinLength) + if (",POWER_MY_WIN,POWER_BLOCKCHAIN,".indexOf("," + ItemServer.name + ",") >= 0) { + arr = global.SERVER.GetStatBlockchain(ItemServer.name, MinLength); + } + var AvgValue = 0; + for (var j = 0; j < arr.length; j++) { + if (arr[j]) + AvgValue += arr[j]; + } + if (arr.length > 0) + AvgValue = AvgValue / arr.length; + var StepTime = 1; + if (ItemServer.name.substr(0, 4) === "MAX:") { + while (arr.length >= MaxSizeArr) { + arr = ResizeArrMax(arr); + StepTime = StepTime * 2; + } + } else { + while (arr.length >= MaxSizeArr) { + arr = ResizeArrAvg(arr); + StepTime = StepTime * 2; + } + } + ItemServer.AvgValue = AvgValue; + ItemServer.steptime = StepTime; + ItemServer.arr = arr.slice(1); } - return r; -} -global.GET_STATS = function(e) { - var t = global.GetCurrentTime(), r = GetCurrentStatIndex(); - return { - stats: { - Counter: CONTEXT_STATS.Total, Counter10S: CalcInterval(CONTEXT_STATS, r, 10), Counter10M: CalcInterval(CONTEXT_STATS, - r, 600) - }, errors: { - Counter: CONTEXT_ERRORS.Total, Counter10S: CalcInterval(CONTEXT_ERRORS, r, 10), Counter10M: CalcInterval(CONTEXT_ERRORS, - r, 600) - }, period: (t - StartStatTime) / 1e3, Confirmation: [] + return Data; +}; +global.GET_STATS = function(Key) { + var now = global.GetCurrentTime(); + var index = GetCurrentStatIndex(); + var stats = { + Counter: CONTEXT_STATS.Total, Counter10S: CalcInterval(CONTEXT_STATS, index, 10), Counter10M: CalcInterval(CONTEXT_STATS, + index, 10 * 60), }; -} + var errors = { + Counter: CONTEXT_ERRORS.Total, Counter10S: CalcInterval(CONTEXT_ERRORS, index, 10), Counter10M: CalcInterval(CONTEXT_ERRORS, + index, 10 * 60), + }; + var Period = (now - StartStatTime) / 1000; + return { stats: stats, errors: errors, period: Period, Confirmation: [] }; +}; global.StartCommonStat = function() { - for (var _ in CONTEXT_STATS.Total) + for (var key in CONTEXT_STATS.Total) return; global.ClearCommonStat(); -} -global.ClearCommonStat = function() { - StartStatTime = void (CurStatIndex = 0), CONTEXT_STATS = { Total: {}, Interval: [] }, CONTEXT_ERRORS = { Total: {}, Interval: [] }, - global.HASH_RATE = 0, global.SERVER.ClearStat(); -} -global.ResizeArrAvg = ResizeArrAvg, global.ResizeArrMax = ResizeArrMax, global.DEBUG_MODE ? global.TO_DEBUG_LOG = function(e, t, r, o) { - global.DEBUG_MODE && ("rinfo" === t && (e += " from: " + r.address + ":" + r.port + " - " + o.length), global.ToLog(e)); -} : global.TO_DEBUG_LOG = function(e, t, r, o) { -} -global.GetStrOnlyTime = function(e) { - if (!global.GetCurrentTime) - return ":::"; - e || (e = global.GetCurrentTime()); - var t = "" + (e = e || global.GetCurrentTime()).getHours().toStringZ(2); - return t = (t = (t = t + ":" + e.getMinutes().toStringZ(2)) + ":" + e.getSeconds().toStringZ(2)) + "." + e.getMilliseconds().toStringZ(3); -} -global.GetStrTime = function(e) { - if (!global.GetCurrentTime) - return ":::"; - e || (e = global.GetCurrentTime()); - var t = "" + (e = e || global.GetCurrentTime()).getDate().toStringZ(2); - return t = (t = (t = (t = (t = (t = t + "." + (1 + e.getMonth()).toStringZ(2)) + "." + e.getFullYear()) + " " + e.getHours().toStringZ(2)) + ":" + e.getMinutes().toStringZ(2)) + ":" + e.getSeconds().toStringZ(2)) + "." + e.getMilliseconds().toStringZ(3); +}; +global.ClearCommonStat = function() { + CurStatIndex = 0; + StartStatTime = undefined; + CONTEXT_STATS = { Total: {}, Interval: [] }; + CONTEXT_ERRORS = { Total: {}, Interval: [] }; + global.HASH_RATE = 0; + global.SERVER.ClearStat(); +}; + +function ResizeArrMax(arr) { + var arr2 = []; + var Count2 = Math.trunc(arr.length / 2); + for (var i = 0; i < Count2; i++) { + arr2[i] = Math.max(arr[i * 2], arr[i * 2 + 1]); + } + return arr2; +}; + +function ResizeArrAvg(arr) { + var arr2 = []; + var Count2 = Math.trunc(arr.length / 2); + for (var i = 0; i < Count2; i++) { + arr2[i] = (arr[i * 2] + arr[i * 2 + 1]) / 2; + } + return arr2; +}; + +function ResizeArr(arr) { + var arr2 = []; + var Count2 = Math.trunc(arr.length / 2); + for (var i = 0; i < Count2; i++) { + arr2[i] = arr[i * 2]; + } + return arr2; +}; +global.ResizeArrAvg = ResizeArrAvg; +global.ResizeArrMax = ResizeArrMax; + +function GetDiagramData(Context, Key) { + var DefMaxStatPeriod = global.MAX_STAT_PERIOD * 2 + 2; + var IsMax; + if (Key.substr(0, 4) === "MAX:") + IsMax = true; + else + IsMax = false; + var delta = global.MAX_STAT_PERIOD; + var index2 = GetCurrentStatIndex(); + var index1 = (index2 - delta + DefMaxStatPeriod) % DefMaxStatPeriod; + var Total = Context.Total; + var Counter1; + var arr = []; + var PrevValue = undefined; + for (var i = index1; i < index1 + delta; i++) { + var index3 = i % DefMaxStatPeriod; + Counter1 = Context.Interval[index3]; + if (Counter1) { + var Value = Counter1[Key]; + if (Value !== undefined) { + if (!IsMax) { + if (PrevValue !== undefined) { + arr.push(Value - PrevValue); + } else { + arr.push(Value); + } + PrevValue = Value; + } else { + arr.push(Value); + } + } else { + arr.push(0); + } + } + } + return arr; +}; + +function CalcInterval(Context, index2, delta) { + var DefMaxStatPeriod = global.MAX_STAT_PERIOD * 2 + 2; + var Res = {}; + var index1 = (index2 - delta + DefMaxStatPeriod) % DefMaxStatPeriod; + var Total = Context.Total; + var Counter1; + for (var i = index1; i < index1 + delta; i++) { + var index3 = i % DefMaxStatPeriod; + Counter1 = Context.Interval[index3]; + if (Counter1) + break; + } + if (Counter1) + for (var Key in Total) { + if (Key.substr(0, 4) === "MAX:") + Res[Key] = 0; + else { + if (Counter1[Key] === undefined) + Res[Key] = Total[Key]; + else + Res[Key] = Total[Key] - Counter1[Key]; + } + } + return Res; +}; + +function AddToStatContext(Context, Key, AddValue?) { + if (AddValue === undefined) + AddValue = 1; + var Val = Context.Total[Key]; + if (!Val) + Val = 0; + if (Key.substr(0, 4) === "MAX:") + Val = Math.max(Val, AddValue); + else + Val = Val + AddValue; + Context.Total[Key] = Val; + if (!StartStatTime) + StartStatTime = global.GetCurrentTime(0); +}; + +function CopyStatInterval(Context, index) { + var Counter = Context.Interval[index]; + if (!Counter) { + Counter = {}; + Context.Interval[index] = Counter; + } + var Total = Context.Total; + for (var Key in Total) { + Counter[Key] = Total[Key]; + if (Key.substr(0, 4) === "MAX:") + Total[Key] = 0; + } +}; +if (global.DEBUG_MODE) + global.TO_DEBUG_LOG = function(Str, type, data1, data2) { + if (!global.DEBUG_MODE) + return; + if (type === "rinfo") + Str += " from: " + data1.address + ':' + data1.port + ' - ' + data2.length; + global.ToLog(Str); + }; +else + global.TO_DEBUG_LOG = function(Str, type, data1, data2) { + }; + +function SaveToLogFileAsync(fname, Str) { + fs.open(fname, "a", undefined, function(err, file_handle) { + if (!err) { + var StrLog = global.GetStrTime() + " : " + Str + "\r\n"; + fs.write(file_handle, StrLog, null, 'utf8', function(err, written) { + if (!err) { + fs.close(file_handle, function(err) { + if (err) + console.log(err); + }); + } + else { + console.log("Ошибка записи в лог-файл ошибок!"); + } + }); + } + else { + console.log("Ошибка открытия лог-файла ошибок"); + } + }); +}; + +function SaveToLogFileSync(fname, Str) { + try { + var StrLog = global.GetStrTime() + " : " + Str + "\r\n"; + var file_handle = fs.openSync(fname, "a"); + fs.writeSync(file_handle, StrLog, null, 'utf8'); + fs.closeSync(file_handle); + } + catch (err) { + console.log(err.message); + } +}; +global.GetStrOnlyTime = function(now) { + if (!global.GetCurrentTime) + return ":::"; + if (!now) + now = global.GetCurrentTime(); + var Str = "" + now.getHours().toStringZ(2); + Str = Str + ":" + now.getMinutes().toStringZ(2); + Str = Str + ":" + now.getSeconds().toStringZ(2); + Str = Str + "." + now.getMilliseconds().toStringZ(3); + return Str; +}; +global.GetStrTime = function(now) { + if (!global.GetCurrentTime) + return ":::"; + if (!now) + now = global.GetCurrentTime(); + var Str = "" + now.getDate().toStringZ(2); + Str = Str + "." + (1 + now.getMonth()).toStringZ(2); + Str = Str + "." + now.getFullYear(); + Str = Str + " " + now.getHours().toStringZ(2); + Str = Str + ":" + now.getMinutes().toStringZ(2); + Str = Str + ":" + now.getSeconds().toStringZ(2); + Str = Str + "." + now.getMilliseconds().toStringZ(3); + return Str; }; diff --git a/src/global.d.ts b/src/global.d.ts index 67c5d9b..2aec55c 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -341,6 +341,7 @@ declare global { //#region log.ts ArrLogClient: any[]; + WEB_LOG: any; ToLogWeb: Function; SmallAddr: Function; ToErrorTrace: Function; @@ -348,6 +349,7 @@ declare global { ToInfo: Function; ToError: Function; ToLogClient0: Function; + ToErrorTx: Function; PrepareStatEverySecond: Function; HASH_RATE: number; ADD_HASH_RATE: Function; diff --git a/src/system/accounts.ts b/src/system/accounts.ts index 9f7a919..ffb35fe 100644 --- a/src/system/accounts.ts +++ b/src/system/accounts.ts @@ -10,14 +10,13 @@ "use strict"; import * as fs from 'fs' -import DApp from './dapp' import DBRow from '../core/db/db-row' -import DBLib from "../core/db/db" +import '../core/rest_tables' import { secp256k1 } from '../core/library' import { TYPE_TRANSACTION } from '../constant/account' -require('../core/rest_tables'); const MAX_SUM_TER = 1e9; const MAX_SUM_CENT = 1e9; +import DBLib from "../core/db/db" global.HistoryDB = new DBLib(); const FILE_NAME_HISTORY = "history-body"; var WorkStructHistory = {}; @@ -85,7 +84,7 @@ global.FORMAT_ACCOUNT_HASH3 = "{\ SmartHash:buffer32,\ SmartCount:uint,\ }"; - +import DApp from './dapp' class MerkleDBRow extends DBRow { private MerkleTree private MerkleArr @@ -108,7 +107,7 @@ class MerkleDBRow extends DBRow { var Buf = this.Read(num, 1); if (!Buf) { if (global.WATCHDOG_DEV) - global.ToLog("CalcMerkleTree: Break account reading on num: " + num) + global.ToErrorTx("CalcMerkleTree: Break account reading on num: " + num) break; } this.MerkleArr[num] = global.shaarr(Buf) @@ -138,7 +137,7 @@ class MerkleDBRow extends DBRow { } } }; -class AccountApp extends DApp { +export default class AccountApp extends DApp { CreateTrCount FORMAT_ACCOUNT_ROW SIZE_ACCOUNT_ROW @@ -298,7 +297,7 @@ class AccountApp extends DApp { try { fs.renameSync(this.DBActPrev.FileNameFull, FileNameFull2) } catch (e) { - global.ToLog("Can-t rename for delete act-file: " + FileNameFull2 + " " + e) + global.ToErrorTx("Can-t rename for delete act-file: " + FileNameFull2 + " " + e) return; } fs.unlinkSync(FileNameFull2) @@ -306,10 +305,7 @@ class AccountApp extends DApp { try { fs.renameSync(this.DBAct.FileNameFull, this.DBActPrev.FileNameFull) } catch (e) { - global.ToLog("Can-t rename act-file!") - console.error(e) - console.log(`${this.DBAct}, ${this.DBActPrev}`) - console.log(`${this.DBAct.FileNameFull}, ${this.DBActPrev.FileNameFull}`) + global.ToErrorTx("Can-t rename act-file!" + e) return; } } @@ -328,6 +324,8 @@ class AccountApp extends DApp { case TYPE_TRANSACTION_TRANSFER: var Num = global.ReadUintFromArr(Body, 1 + 1 + 6); return Num; + case TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH: + return - 1; } } return 0; @@ -349,10 +347,9 @@ class AccountApp extends DApp { this.BeginTransaction() this.DoCoinBaseTR(Block) this.CommitTransaction(Block.BlockNum, 0xFFFF) - } - catch (e) { + } catch (e) { this.RollBackTransaction() - global.ToError("DoCoinBaseTR: " + e) + global.ToErrorTx("BlockNum:" + Block.BlockNum + " - DoCoinBaseTR: " + e) } this.CommitBlock(Block) } @@ -362,6 +359,8 @@ class AccountApp extends DApp { Result = this.OnWriteTransactionTR(Block, Body, BlockNum, TrNum, ContextFrom) } catch (e) { Result = "" + e + if (global.WATCHDOG_DEV) + global.ToErrorTx("BlockNum:" + BlockNum + ":" + e) } if (Result !== true) { this.RollBackTransaction() @@ -405,8 +404,7 @@ class AccountApp extends DApp { if (!this.TRCheckAccountHash(Body, BlockNum, TrNum)) { Result = "BAD ACCOUNT HASH" global.ToLog("2. ****FIND BAD ACCOUNT HASH IN BLOCK: " + BlockNumHash + " DO BLOCK=" + BlockNum) - } - else { + } else { Result = true } break; @@ -417,8 +415,6 @@ class AccountApp extends DApp { DoCoinBaseTR(Block) { if (Block.BlockNum < global.START_MINING) return; - if (!Block.PowHash) - throw "#121 ERROR NO Block.PowHash"; var SysData = this.ReadStateTR(0); var SysBalance = SysData.Value.SumCOIN; const REF_PERIOD_START = global.START_MINING; @@ -430,7 +426,6 @@ class AccountApp extends DApp { if (Data && Data.Currency === 0 && Data.BlockNumCreate < Block.BlockNum) { var Sum; if (Block.BlockNum >= global.NEW_FORMULA_START) { - Sum = Power * Power * SysBalance / global.TOTAL_SUPPLY_TERA / 100 if (Block.BlockNum <= global.NEW_FORMULA_TARGET1) { Sum = SysBalance * 43 * 43 / 100 / global.TOTAL_SUPPLY_TERA var KMult = (global.NEW_FORMULA_TARGET2 - Block.BlockNum) / (global.NEW_FORMULA_TARGET2 - global.NEW_FORMULA_START); @@ -513,8 +508,7 @@ class AccountApp extends DApp { var TR; try { TR = global.BufLib.GetObjectFromBuffer(Body, format, {}) - } - catch (e) { + } catch (e) { } return TR; } @@ -568,8 +562,7 @@ class AccountApp extends DApp { if (BlockNum >= 7000000 || global.LOCAL_RUN || global.TEST_NETWORK) { if (ContextFrom && ContextFrom.To.length === 1 && ContextFrom.To[0].ID === 0 && ContextFrom.To[0].SumCOIN >= global.PRICE_DAO(BlockNum).NewAccount) { CheckMinPower = 0 - } - else { + } else { if (BlockNum % BLOCK_CREATE_INTERVAL !== 0) return "The create transaction is not possible in this block: " + BlockNum; if (this.CreateTrCount > 0) @@ -629,8 +622,7 @@ class AccountApp extends DApp { return "Error length transaction"; try { var TR = global.BufLib.GetObjectFromBuffer(Body, format_money_transfer, workstructtransfer); - } - catch (e) { + } catch (e) { return "Error transaction format"; } if (!TR.Version) @@ -643,8 +635,7 @@ class AccountApp extends DApp { if (TR.Version < 3) { if (TR.OperationID !== Data.Value.OperationID) return "Error OperationID (expected: " + Data.Value.OperationID + " for ID: " + TR.FromID + ")"; - } - else { + } else { if (TR.OperationID < Data.Value.OperationID) return "Error OperationID (expected: " + Data.Value.OperationID + " for ID: " + TR.FromID + ")"; var MaxCountOperationID = 100; @@ -923,13 +914,11 @@ class AccountApp extends DApp { Filter = Filter.substring(1) try { F = global.CreateEval(Filter, "Cur,Currency,ID,Operation,Amount,Adviser,Name,PubKey,Smart,BlockNum") - } - catch (e) { + } catch (e) { F = undefined global.ToLog("" + e) } - } - else { + } else { Filter = Filter.toUpperCase() } } @@ -1400,7 +1389,6 @@ global.DAppByType[TYPE_DEPRECATED_TRANSFER2] = App; global.DAppByType[TYPE_TRANSACTION_TRANSFER] = App; global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH] = App; -export default AccountApp; // function TestStateFiles(Size, Format) { // return; // if (global.PROCESS_NAME !== "MAIN") diff --git a/src/system/dapp.ts b/src/system/dapp.ts index 8761f9e..f485972 100644 --- a/src/system/dapp.ts +++ b/src/system/dapp.ts @@ -9,7 +9,7 @@ */ "use strict"; -class DApp { +export default class DApp { constructor() { } Name() { @@ -69,19 +69,5 @@ declare global { } } } - -// function ReqDir(Path) { -// if (fs.existsSync(Path)) { -// var arr = fs.readdirSync(Path); -// for (var i = 0; i < arr.length; i++) { -// var name = arr[i]; -// global.ToLog("Reg: " + name); -// var name2 = Path + "/" + arr[i]; -// require(name2); -// } -// } -// }; global.DApps = {}; global.DAppByType = {}; - -export default DApp; \ No newline at end of file diff --git a/src/system/file.ts b/src/system/file.ts index e4baba9..91cf073 100644 --- a/src/system/file.ts +++ b/src/system/file.ts @@ -9,12 +9,12 @@ */ "use strict"; -import DApp from './dapp' global.TYPE_TRANSACTION_FILE = 5; global.FORMAT_FILE_CREATE = "{type:byte,Name:str,ContentType:str,Reserve:arr10,Data:tr}"; //@ts-ignore let WorkStructRun = {}; -class FileApp extends DApp { +import DApp from './dapp' +export default class FileApp extends DApp { constructor() { super() } @@ -37,5 +37,3 @@ class FileApp extends DApp { var App = new FileApp; global.DApps.File = App; global.DAppByType[global.TYPE_TRANSACTION_FILE] = App; - -export default FileApp diff --git a/src/system/messager.ts b/src/system/messager.ts index 45854b0..ff8e588 100644 --- a/src/system/messager.ts +++ b/src/system/messager.ts @@ -9,7 +9,6 @@ */ "use strict"; -import DApp from './dapp' require('../core/crypto-library'); const OPEN_TYPE_TRANSACTION = 11; const MESSAGE_TYPE_TRANSACTION = 12; @@ -18,8 +17,8 @@ var TempArrayTr = new Uint8Array(MAX_MSG_SIZE); const MESSAGE_START = 9; const MESSAGE_END = MAX_MSG_SIZE - 5; import './names' - -class MessagerApp extends DApp { +import DApp from './dapp' +export default class MessagerApp extends DApp { Server Channels NamesMap @@ -107,9 +106,9 @@ class MessagerApp extends DApp { }; function TestEncryptDecrypt() { - // const CMessager = module.exports; - // var Server = { KeyPair: global.GetKeyPairTest("Test"), DApp: { Names: { KeyValueMap: {} } }, }; - // var Test = new CMessager(Server); + const CMessager = module.exports; + var Server = { KeyPair: global.GetKeyPairTest("Test"), DApp: { Names: { KeyValueMap: {} } }, }; + var Test = new CMessager(Server); var KeyPair2 = global.GetKeyPairTest("Test2"); var StrTest1 = global.GetArrFromStr("Test2", 32); var StrKey = global.GetHexFromAddres(StrTest1); @@ -119,9 +118,8 @@ function TestEncryptDecrypt() { console.log("Decrypt:"); console.log(Str2); }; + var messager = new MessagerApp(); global.DApps.Messager = messager; global.DAppByType[OPEN_TYPE_TRANSACTION] = messager; global.DAppByType[MESSAGE_TYPE_TRANSACTION] = messager; - -export default MessagerApp; diff --git a/src/system/names.ts b/src/system/names.ts index bec5a70..2c62bcd 100644 --- a/src/system/names.ts +++ b/src/system/names.ts @@ -9,9 +9,9 @@ */ "use strict"; -import DApp from './dapp' const NAMES_TYPE_TRANSACTION = 10; -class NameApp extends DApp { +import DApp from './dapp' +export default class NameApp extends DApp { Server KeyValueMap; CurrentNameArr; @@ -33,8 +33,7 @@ class NameApp extends DApp { } } }; + var Name = new NameApp(); global.DApps.Names = Name; global.DAppByType[NAMES_TYPE_TRANSACTION] = Name; - -export default NameApp \ No newline at end of file diff --git a/src/system/smart.ts b/src/system/smart.ts index e13fb04..357e4f3 100644 --- a/src/system/smart.ts +++ b/src/system/smart.ts @@ -9,13 +9,12 @@ */ "use strict"; -import DApp from './dapp' -import DBRow from '../core/db/db-row' -import { secp256k1 } from '../core/library' - const LOC_ADD_NAME = "$"; require("../HTML/JS/lexer.js"); global.TickCounter = 0; +import DBRow from '../core/db/db-row' +import DApp from './dapp' +import { secp256k1 } from '../core/library' const TYPE_TRANSACTION_SMART_CREATE = 130; global.TYPE_TRANSACTION_SMART_RUN = 135; const TYPE_TRANSACTION_SMART_CHANGE = 140; @@ -63,7 +62,7 @@ global.FORMAT_SMART_CHANGE = "{\ Sign:arr64,\ }"; const WorkStructChange = {}; -class SmartApp extends DApp { +export default class SmartApp extends DApp { FORMAT_ROW ROW_SIZE DBSmart @@ -1213,10 +1212,9 @@ function StaticGetSmart(Num) { }; ChangePrototype(); InitEval(); + var smartApp = new SmartApp; global.DApps.Smart = smartApp; global.DAppByType[TYPE_TRANSACTION_SMART_CREATE] = smartApp; global.DAppByType[global.TYPE_TRANSACTION_SMART_RUN] = smartApp; global.DAppByType[TYPE_TRANSACTION_SMART_CHANGE] = smartApp; - -export default SmartApp \ No newline at end of file