refactor: sync block loader

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-07-13 15:51:52 +08:00
parent 12e835d5f5
commit 1509b956a9
6 changed files with 653 additions and 323 deletions

View File

@ -381,15 +381,15 @@ global.STreeBuffer = STreeBuffer;
global.TestCreateTr = TestCreateTr; global.TestCreateTr = TestCreateTr;
function TestCreateTr() { function TestCreateTr() {
// const FORMAT_CREATE = "{\ const FORMAT_CREATE = "{\
// Type:byte,\ Type:byte,\
// Currency:uint,\ Currency:uint,\
// PubKey:arr33,\ PubKey:arr33,\
// Description:str40,\ Description:str40,\
// Adviser:uint,\ Adviser:uint,\
// Reserve:arr7,\ Reserve:arr7,\
// POWCreate:arr12,\ POWCreate:arr12,\
// }"; }";
var TR = { var TR = {
Type: 100, Currency: 0, PubKey: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Type: 100, Currency: 0, PubKey: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], Description: "Description", Adviser: 10, 0, 0, 0, 0, 0], Description: "Description", Adviser: 10,

View File

@ -182,13 +182,13 @@ export default class CConsensus extends CBlock {
global.ToLog("TRANSFER BlockNum:" + Block.BlockNum + " TxArray=" + Data.TxArray.length + " from " + global.NodeName(Node)) global.ToLog("TRANSFER BlockNum:" + Block.BlockNum + " TxArray=" + Data.TxArray.length + " from " + global.NodeName(Node))
this.ToMaxPOWList(Data.MaxPOW) this.ToMaxPOWList(Data.MaxPOW)
this.ToMaxSumList(this.GetMaxSumListFromID(Node, Data.MaxSumID, Data.BlockList)) this.ToMaxSumList(this.GetMaxSumListFromID(Node, Data.MaxSumID, Data.BlockList))
//var WasNewAdd = 0; var WasNewAdd = 0;
if (Data.TxArray.length) { if (Data.TxArray.length) {
for (var i = 0; i < Data.TxArray.length; i++) { for (var i = 0; i < Data.TxArray.length; i++) {
var Tr = Data.TxArray[i]; var Tr = Data.TxArray[i];
var Res = this.AddTrToBlockQuote(Block, Tr); var Res = this.AddTrToBlockQuote(Block, Tr);
if (Res === 1) { if (Res === 1) {
//WasNewAdd = 1 WasNewAdd = 1
} }
if (global.USE_CHECK_SENDING && Res > 0) { if (global.USE_CHECK_SENDING && Res > 0) {
var Tt = Block.PowTxTree.find(Tr); var Tt = Block.PowTxTree.find(Tr);

View File

@ -8,19 +8,36 @@
* Telegram: https://t.me/terafoundation * Telegram: https://t.me/terafoundation
*/ */
global.PERIOD_GET_BLOCK = 300 global.PERIOD_GET_BLOCK = 300;
global.COUNT_HISTORY_BLOCKS_FOR_LOAD = 600 global.COUNT_HISTORY_BLOCKS_FOR_LOAD = 600;
global.COUNT_BLOCKS_FOR_CHECK_POW = 50 global.COUNT_BLOCKS_FOR_CHECK_POW = 50;
global.MAX_DELTA_COUNT_SUM_FOR_LOAD = 10 global.MAX_DELTA_COUNT_SUM_FOR_LOAD = 10;
global.MAX_COUNT_CHAIN_LOAD = 120 global.MAX_COUNT_CHAIN_LOAD = 120;
global.PACKET_ALIVE_PERIOD = 4 * global.CONSENSUS_PERIOD_TIME global.PACKET_ALIVE_PERIOD = 4 * global.CONSENSUS_PERIOD_TIME;
global.PACKET_ALIVE_PERIOD_NEXT_NODE = global.PACKET_ALIVE_PERIOD / 2 global.PACKET_ALIVE_PERIOD_NEXT_NODE = global.PACKET_ALIVE_PERIOD / 2;
global.MAX_BLOCK_SEND = 8 global.MAX_BLOCK_SEND = 8;
global.COUNT_TASK_FOR_NODE = 10 global.COUNT_TASK_FOR_NODE = 10;
global.FORMAT_BLOCK_TRANSFER = "{ BlockNum:uint, TreeHash:hash, arrContent:[tr], }" global.FORMAT_BLOCK_TRANSFER = "{\
global.WRK_BLOCK_TRANSFER = {} BlockNum:uint,\
global.MAX_ACCOUNTS_TRANSFER = 1024 TreeHash:hash,\
global.MAX_SMARTS_TRANSFER = 10 arrContent:[tr],\
global.TEST_NETWORK && (global.MAX_ACCOUNTS_TRANSFER = 128, global.MAX_SMARTS_TRANSFER = 10) }";
global.FORMAT_REST_TRANSFER = "{ Result:uint, Version:uint, Arr:[arr200], ProofHash:hash, ProofArrL:<hash>, ProofArrR:<hash>, }" global.WRK_BLOCK_TRANSFER = {};
global.FORMAT_SMART_TRANSFER = "{ Result:uint, Arr:[tr], }"; global.MAX_ACCOUNTS_TRANSFER = 1024;
global.MAX_SMARTS_TRANSFER = 10;
if (global.TEST_NETWORK) {
global.MAX_ACCOUNTS_TRANSFER = 128;
global.MAX_SMARTS_TRANSFER = 10;
}
global.FORMAT_REST_TRANSFER = "{\
Result:uint,\
Version:uint,\
Arr:[arr200],\
ProofHash:hash,\
ProofArrL:<hash>,\
ProofArrR:<hash>,\
}";
global.FORMAT_SMART_TRANSFER = "{\
Result:uint,\
Arr:[tr],\
}";

View File

@ -65,11 +65,19 @@ export default class CBlock extends CRest {
if (Num < 0) if (Num < 0)
return undefined; return undefined;
var Block: any = { var Block: any = {
BlockNum: Num, TreeHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, BlockNum: Num,
0, 0, 0], AddrHash: global.DEVELOP_PUB_KEY0, Hash: this.GetHashGenesis(Num), PowHash: this.GetHashGenesis(Num), PrevHash: [0, 0, 0, 0, TreeHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], SeqHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, AddrHash: global.DEVELOP_PUB_KEY0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], SumHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Hash: this.GetHashGenesis(Num),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], Comment1: "GENESIS", Comment2: "", TrCount: 0, TrDataPos: 0, TrDataLen: 0, PowHash: this.GetHashGenesis(Num),
PrevHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
SeqHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
SumHash: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Comment1: "GENESIS",
Comment2: "",
TrCount: 0,
TrDataPos: 0,
TrDataLen: 0,
}; };
Block.SeqHash = this.GetSeqHash(Block.BlockNum, Block.PrevHash, Block.TreeHash) Block.SeqHash = this.GetSeqHash(Block.BlockNum, Block.PrevHash, Block.TreeHash)
Block.SumPow = 0 Block.SumPow = 0
@ -138,7 +146,7 @@ export default class CBlock extends CRest {
var PrevHash = global.CalcHashFromArray(arr, true); var PrevHash = global.CalcHashFromArray(arr, true);
return PrevHash; return PrevHash;
} }
StartSyncBlockchain(Node?, bSilent?, bCheckPoint?) { StartSyncBlockchain(Node?, bSilent?, bCheckPoint?, PrevStartedBlockNum?) {
this.FREE_ALL_MEM_CHAINS() this.FREE_ALL_MEM_CHAINS()
if (global.NO_HISTORY_MODE) { if (global.NO_HISTORY_MODE) {
this.LoadHistoryMode = 0 this.LoadHistoryMode = 0
@ -152,6 +160,19 @@ export default class CBlock extends CRest {
this.RelayMode = false this.RelayMode = false
if (!bSilent) if (!bSilent)
this.RelayMode = true this.RelayMode = true
var StartBlockNum;
if (PrevStartedBlockNum) {
var DeltaNum = Math.floor(1.2 * (this.BlockNumDB - PrevStartedBlockNum));
if (DeltaNum < 1000)
DeltaNum = 1000
StartBlockNum = this.BlockNumDB - DeltaNum
if (StartBlockNum <= 0)
StartBlockNum = 15
global.ToLog("Current DeltaNum=" + DeltaNum + " StartBlockNum=" + StartBlockNum, 2)
}
else {
StartBlockNum = this.BlockNumDB
}
this.LoadHistoryMode = true this.LoadHistoryMode = true
this.LoadHistoryMessage = !bSilent this.LoadHistoryMessage = !bSilent
this.LoadHistoryContext = { this.LoadHistoryContext = {
@ -182,7 +203,7 @@ export default class CBlock extends CRest {
var DeltaTime = Date.now() - Context.StartTimeHistory; var DeltaTime = Date.now() - Context.StartTimeHistory;
if (DeltaTime > Context.MaxTimeOut) { if (DeltaTime > Context.MaxTimeOut) {
global.ToLog("DETECT TIMEOUT LOAD") global.ToLog("DETECT TIMEOUT LOAD")
this.StartSyncBlockchain() this.StartSyncBlockchain(undefined, undefined, undefined, Context.BlockNum)
return; return;
} }
} }
@ -230,9 +251,28 @@ export default class CBlock extends CRest {
return false; return false;
Tree.insert({ hash: LoadHash }) Tree.insert({ hash: LoadHash })
var chain: any = { var chain: any = {
id: 0, Count: 16, BlockNum: Num, IsSum: bIsSum, Hash: LoadHash, time: undefined, FindBlockDB: false, LoadDB: false, LoadCountDB: 0, id: 0,
LoadSumDB: 0, LoadSum: 0, ParentChain: undefined, RootChain: undefined, BlockNumStart: Num, HashStart: LoadHash, IsSumStart: bIsSum, Count: 16,
BlockHead: undefined, MapSend: {}, Comment2: "", StopSend: false, Info: "", Error: false, BlockNum: Num,
IsSum: bIsSum,
Hash: LoadHash,
time: undefined,
FindBlockDB: false,
LoadDB: false,
LoadCountDB: 0,
LoadSumDB: 0,
LoadSum: 0,
ParentChain: undefined,
RootChain: undefined,
BlockNumStart: Num,
HashStart: LoadHash,
IsSumStart: bIsSum,
BlockHead: undefined,
MapSend: {},
Comment2: "",
StopSend: false,
Info: "",
Error: false,
}; };
this.ChainBindMethods(chain) this.ChainBindMethods(chain)
chain.AddInfo(StrInfo) chain.AddInfo(StrInfo)
@ -491,6 +531,7 @@ export default class CBlock extends CRest {
} }
else { else {
if (!Context.WasLoadNum) { if (!Context.WasLoadNum) {
global.ToLog("Not found: " + Context.BlockNum + " from node:" + global.NodeName(Info.Node), 2)
Context.BlockNum = Math.floor(Context.BlockNum - Context.DeltaBlockNum) Context.BlockNum = Math.floor(Context.BlockNum - Context.DeltaBlockNum)
Context.DeltaBlockNum = Context.DeltaBlockNum * 1.2 Context.DeltaBlockNum = Context.DeltaBlockNum * 1.2
if (Context.BlockNum < global.BLOCK_PROCESSING_LENGTH2) if (Context.BlockNum < global.BLOCK_PROCESSING_LENGTH2)
@ -775,8 +816,8 @@ export default class CBlock extends CRest {
if (global.IsZeroArr(Block.TreeHash)) { if (global.IsZeroArr(Block.TreeHash)) {
Res = this.WriteBlockDB(Block) Res = this.WriteBlockDB(Block)
} else { } else {
global.ToLogTrace("global.IsZeroArr(Block.TreeHash)") global.ToLogTrace("IsZeroArr(Block.TreeHash)")
throw "global.IsZeroArr(Block.TreeHash)"; throw "IsZeroArr(Block.TreeHash)";
} }
} }
if (!Res) { if (!Res) {

View File

@ -8,319 +8,591 @@
* 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(e, r, t, n?, l?) { function Write(buf, data, StringFormat?, ParamValue?, WorkStruct?) {
if (!(e.len >= e.length)) { if (buf.len >= buf.length) {
if ("number" == typeof t) return;
throw global.ToLogTrace("ERRR StringFormat "), "ERR!!";
var a = t;
if ("buffer" === a.substr(0, 6) && 6 < a.length)
n = parseInt(a.substr(6)), a = "buffer";
else
if ("arr" === a.substr(0, 3) && 3 < a.length)
n = parseInt(a.substr(3)), a = "arr";
else
if ("str" === a.substr(0, 3) && 3 < a.length) {
var i = parseInt(a.substr(3));
return r && e.write(r, e.len, i), void (e.len += i);
} }
switch (a) { if (typeof StringFormat === "number") {
case "str": global.ToLogTrace("ERRR StringFormat ");
var f = global.toUTF8Array(r); throw "ERR!!";
65535 < (i = f.length) && (i = 0), e[e.len] = 255 & i, e[e.len + 1] = i >>> 8 & 255, e.len += 2;
for (var s = 0; s < i; s++)
e[e.len + s] = f[s];
e.len += i;
break;
case "byte":
r < 0 && (r = 0), e[e.len] = r, e.len += 1;
break;
case "double":
e.writeDoubleLE(r, e.len, 8), e.len += 8;
break;
case "uint":
r < 0 && (r = 0), 0xffffffffffff <= r && (r = 0), e.writeUIntLE(r, e.len, 6), e.len += 6;
break;
case "uint16":
r < 0 && (r = 0), e[e.len] = 255 & r, e[e.len + 1] = r >>> 8 & 255, e.len += 2;
break;
case "uint32":
r < 0 && (r = 0), e.writeUInt32LE(r, e.len, 4), e.len += 4;
break;
case "time":
var u = r.valueOf();
e.writeUIntLE(u, e.len, 6), e.len += 6;
break;
case "addres":
case "hash":
i = r ? Math.min(32, r.length) : 0;
for (s = 0; s < i; s++)
e[e.len + s] = r[s];
e.len += 32;
break;
case "buffer":
i = void 0 === n ? r.length : Math.min(n, r.length);
for (s = 0; s < i; s++)
e[e.len + s] = r[s];
e.len += n;
break;
case "arr":
i = r ? Math.min(n, r.length) : 0;
for (s = 0; s < i; s++)
e[e.len + s] = r[s];
e.len += n;
break;
case "tr":
i = r.length;
global.MAX_TRANSACTION_SIZE > global.MAX_TRANSACTION_SIZE && (i = global.MAX_TRANSACTION_SIZE), e[e.len] = 255 & i, e[e.len + 1] = i >>> 8 & 255,
e.len += 2;
for (s = 0; s < i; s++)
e[e.len + s] = r[s];
e.len += i;
break;
case "data":
i = r.length;
e.writeUInt32LE(i, e.len, 4), e.len += 4;
for (s = 0; s < i; s++)
e[e.len + s] = r[s];
e.len += i;
break;
case "hashSTR":
var o = global.GetHexFromAddres(r);
e.write(o, e.len, 64), e.len += 64;
break;
case "uintSTR":
o = r.toString();
e.write(o, e.len, 10), e.len += 10;
break;
default:
l = l || {};
var d = t.substr(0, 1);
if ("[" === d) {
r && (i = r.length);
var b = GetMiddleString(a);
Write(e, i, "uint32");
for (s = 0; s < i; s++)
Write(e, r[s], b, void 0, l);
}
else
if ("<" === d) {
r && (i = r.length);
b = GetMiddleString(a);
var h = 0, c = e.len;
e.len += 4;
for (s = 0; s < i; s++)
r[s] && (h++ , Write(e, s, "uint32"), Write(e, r[s], b, void 0, l));
e.writeUInt32LE(h, c, 4);
} }
else { else {
if ("{" !== d) var format = StringFormat;
throw "Bad write type params: " + a; if (format.substr(0, 6) === "buffer" && format.length > 6) {
var g = l[a]; ParamValue = parseInt(format.substr(6));
g || (g = GetAttributes(GetMiddleString(a)), l[a] = g); format = "buffer";
for (s = 0; s < g.length; s++) { }
var v = g[s]; else
Write(e, r[v.Key], v.Value, void 0, l); if (format.substr(0, 3) === "arr" && format.length > 3) {
ParamValue = parseInt(format.substr(3));
format = "arr";
}
else
if (format.substr(0, 3) === "str" && format.length > 3) {
var length = parseInt(format.substr(3));
if (data)
buf.write(data, buf.len, length);
buf.len += length;
return;
}
switch (format) {
case "str":
{
var arr = global.toUTF8Array(data);
let length = arr.length;
if (length > 65535)
length = 0;
buf[buf.len] = length & 255;
buf[buf.len + 1] = (length >>> 8) & 255;
buf.len += 2;
for (var i = 0; i < length; i++) {
buf[buf.len + i] = arr[i];
}
buf.len += length;
break;
}
case "byte":
{
if (data < 0)
data = 0;
buf[buf.len] = data;
buf.len += 1;
break;
}
case "double":
{
buf.writeDoubleLE(data, buf.len, 8);
buf.len += 8;
break;
}
case "uint":
{
if (data < 0)
data = 0;
if (data >= 281474976710655)
data = 0;
buf.writeUIntLE(data, buf.len, 6);
buf.len += 6;
break;
}
case "uint16":
{
if (data < 0)
data = 0;
buf[buf.len] = data & 255;
buf[buf.len + 1] = (data >>> 8) & 255;
buf.len += 2;
break;
}
case "uint32":
{
if (data < 0)
data = 0;
buf.writeUInt32LE(data, buf.len, 4);
buf.len += 4;
break;
}
case "time":
{
var Time = data.valueOf();
buf.writeUIntLE(Time, buf.len, 6);
buf.len += 6;
break;
}
case "addres":
case "hash":
{
let length;
if (data)
length = Math.min(32, data.length);
else
length = 0;
for (var i = 0; i < length; i++) {
buf[buf.len + i] = data[i];
}
buf.len += 32;
break;
}
case "buffer":
{
let length;
if (ParamValue === undefined)
length = data.length;
else
length = Math.min(ParamValue, data.length);
for (var i = 0; i < length; i++) {
buf[buf.len + i] = data[i];
}
buf.len += ParamValue;
break;
}
case "arr":
{
let length;
if (data)
length = Math.min(ParamValue, data.length);
else
length = 0;
for (var i = 0; i < length; i++) {
buf[buf.len + i] = data[i];
}
buf.len += ParamValue;
break;
}
case "tr":
{
let length = data.length;
if (global.MAX_TRANSACTION_SIZE > global.MAX_TRANSACTION_SIZE)
length = global.MAX_TRANSACTION_SIZE;
buf[buf.len] = length & 255;
buf[buf.len + 1] = (length >>> 8) & 255;
buf.len += 2;
for (var i = 0; i < length; i++) {
buf[buf.len + i] = data[i];
}
buf.len += length;
break;
}
case "data":
{
let length = data.length;
buf.writeUInt32LE(length, buf.len, 4);
buf.len += 4;
for (var i = 0; i < length; i++) {
buf[buf.len + i] = data[i];
}
buf.len += length;
break;
}
case "hashSTR":
{
var Str = global.GetHexFromAddres(data);
buf.write(Str, buf.len, 64);
buf.len += 64;
break;
}
case "uintSTR":
{
var Str = data.toString();
buf.write(Str, buf.len, 10);
buf.len += 10;
break;
}
default:
{
WorkStruct = WorkStruct || {};
var CurFormat = StringFormat.substr(0, 1);
if (CurFormat === "[") {
let length;
if (data)
length = data.length;
var formatNext = GetMiddleString(format);
Write(buf, length, "uint32");
for (var i = 0; i < length; i++) {
Write(buf, data[i], formatNext, undefined, WorkStruct);
}
}
else
if (CurFormat === "<") {
let length;
if (data)
length = data.length;
var formatNext = GetMiddleString(format);
var IndexCount = 0;
var len = buf.len;
buf.len += 4;
for (var i = 0; i < length; i++) {
if (data[i]) {
IndexCount++;
Write(buf, i, "uint32");
Write(buf, data[i], formatNext, undefined, WorkStruct);
}
}
buf.writeUInt32LE(IndexCount, len, 4);
}
else
if (CurFormat === "{") {
var attrs = WorkStruct[format];
if (!attrs) {
attrs = GetAttributes(GetMiddleString(format));
WorkStruct[format] = attrs;
}
for (var i = 0; i < attrs.length; i++) {
var type = attrs[i];
Write(buf, data[type.Key], type.Value, undefined, WorkStruct);
}
}
else {
throw "Bad write type params: " + format;
} }
} }
} }
} }
}; };
function Read(e, r, t?, n?, l?) { function Read(buf, StringFormat, ParamValue?, WorkStruct?, bDisableTime?) {
var a; var ret;
if ("number" == typeof r) if (typeof StringFormat === "number") {
throw global.ToLogTrace("ERR StringFormat"), "ERRR!"; global.ToLogTrace("ERR StringFormat");
var i = r; throw "ERRR!";
if ("buffer" === i.substr(0, 6)) }
6 < i.length ? (t = parseInt(i.substr(6)), i = "buffer") : t = 0; else {
var format = StringFormat;
if (format.substr(0, 6) === "buffer") {
if (format.length > 6) {
ParamValue = parseInt(format.substr(6));
format = "buffer";
}
else {
ParamValue = 0;
}
}
else else
if ("arr" === i.substr(0, 3)) if (format.substr(0, 3) === "arr") {
3 < i.length ? (t = parseInt(i.substr(3)), i = "arr") : t = 0; if (format.length > 3) {
ParamValue = parseInt(format.substr(3));
format = "arr";
}
else {
ParamValue = 0;
}
}
else else
if ("str" === i.substr(0, 3)) { if (format.substr(0, 3) === "str") {
if (3 < i.length) { if (format.length > 3) {
var f = parseInt(i.substr(3)); var length = parseInt(format.substr(3));
a = e.toString("utf8", e.len, e.len + f), e.len += f; ret = buf.toString('utf8', buf.len, buf.len + length);
for (var s = - 1, u = a.length - 1; 0 <= u; u--) buf.len += length;
if (0 !== a.charCodeAt(u)) { var nEnd = - 1;
s = u; for (var i = ret.length - 1; i >= 0; i--) {
if (ret.charCodeAt(i) !== 0) {
nEnd = i;
break; break;
} }
return a = 0 <= s ? a.substr(0, u + 1) : "";
} }
t = 0; if (nEnd >= 0)
ret = ret.substr(0, i + 1);
else
ret = "";
return ret;
} }
switch (i) { else {
ParamValue = 0;
}
}
switch (format) {
case "str": case "str":
f = e.len + 2 <= e.length ? e[e.len] + 256 * e[e.len + 1] : 0, e.len += 2; {
var o = e.slice(e.len, e.len + f); let length;
a = global.Utf8ArrayToStr(o), e.len += f; if (buf.len + 2 <= buf.length)
length = buf[buf.len] + buf[buf.len + 1] * 256;
else
length = 0;
buf.len += 2;
var arr = buf.slice(buf.len, buf.len + length);
ret = global.Utf8ArrayToStr(arr);
buf.len += length;
break; break;
}
case "byte": case "byte":
a = e.len + 1 <= e.length ? e[e.len] : 0, e.len += 1; {
if (buf.len + 1 <= buf.length)
ret = buf[buf.len];
else
ret = 0;
buf.len += 1;
break; break;
}
case "double": case "double":
a = e.len + 8 <= e.length ? e.readDoubleLE(e.len, 8) : 0, e.len += 8; {
if (buf.len + 8 <= buf.length)
ret = buf.readDoubleLE(buf.len, 8);
else
ret = 0;
buf.len += 8;
break; break;
}
case "uint": case "uint":
a = e.len + 6 <= e.length ? e.readUIntLE(e.len, 6) : 0, e.len += 6; {
if (buf.len + 6 <= buf.length)
ret = buf.readUIntLE(buf.len, 6);
else
ret = 0;
buf.len += 6;
break; break;
}
case "uint16": case "uint16":
a = e.len + 2 <= e.length ? e[e.len] + 256 * e[e.len + 1] : 0, e.len += 2; {
if (buf.len + 2 <= buf.length)
ret = buf[buf.len] + buf[buf.len + 1] * 256;
else
ret = 0;
buf.len += 2;
break; break;
}
case "uint32": case "uint32":
a = e.len + 4 <= e.length ? e.readUInt32LE(e.len, 4) : 0, e.len += 4; {
if (buf.len + 4 <= buf.length)
ret = buf.readUInt32LE(buf.len, 4);
else
ret = 0;
buf.len += 4;
break; break;
}
case "time": case "time":
if (l) {
if (bDisableTime)
throw "Bad read type params: time - DisableTime ON"; throw "Bad read type params: time - DisableTime ON";
a = e.len + 6 <= e.length ? e.readUIntLE(e.len, 6) : 0, a = new Date(a), e.len += 6; if (buf.len + 6 <= buf.length)
ret = buf.readUIntLE(buf.len, 6);
else
ret = 0;
ret = new Date(ret);
buf.len += 6;
break; break;
}
case "addres": case "addres":
case "hash": case "hash":
a = []; {
for (u = 0; u < 32; u++) ret = [];
e.len + u <= e.length ? a[u] = e[e.len + u] : a[u] = 0; for (var i = 0; i < 32; i++) {
e.len += 32; if (buf.len + i <= buf.length)
ret[i] = buf[buf.len + i];
else
ret[i] = 0;
}
buf.len += 32;
break; break;
}
case "buffer": case "buffer":
case "arr": case "arr":
a = e.len + t <= e.length ? e.slice(e.len, e.len + t) : Buffer.alloc(t), e.len += t; {
break; if (buf.len + ParamValue <= buf.length)
case "tr": ret = buf.slice(buf.len, buf.len + ParamValue);
if (e.len + 1 >= e.length) {
a = void 0;
break;
}
f = e[e.len] + 256 * e[e.len + 1];
e.len += 2, a = e.slice(e.len, e.len + f), e.len += f;
break;
case "data":
(f = e.len + 4 <= e.length ? e.readUInt32LE(e.len, 4) : 0) > e.length - e.len - 4 && (f = 0), e.len += 4, a = e.slice(e.len,
e.len + f), e.len += f;
break;
case "hashSTR":
var d = e.toString("utf8", e.len, e.len + 64);
a = global.GetAddresFromHex(d), e.len += 64;
break;
case "uintSTR":
d = e.toString("utf8", e.len, e.len + 10);
a = parseInt(d), e.len += 10;
break;
default:
n = n || {};
var b = i.substr(0, 1);
if ("[" === b || "<" === b) {
var h = "<" === b;
a = [];
var c = GetMiddleString(i);
for (f = Read(e, "uint32"), u = 0; u < f && e.len <= e.length; u++) {
h ? a[Read(e, "uint32")] = Read(e, c, void 0, n, l) : a[u] = Read(e, c, void 0, n, l);
}
}
else {
if ("{" !== b)
throw "Bad read type params: " + i;
var g = n[i];
g || (g = GetAttributes(GetMiddleString(i)), n[i] = g), a = {};
for (u = 0; u < g.length; u++) {
var v = g[u];
a[v.Key] = Read(e, v.Value, void 0, n, l);
}
}
}
return a;
};
function BufWriteByte(e) {
this[this.len] = e, this.len += 1;
};
function BufWrite(e, r, t) {
Write(this, e, r, t);
};
function BufRead(e, r) {
return Read(this, e, r);
};
function GetNewBuffer(e) {
var r = Buffer.alloc(e);
return r.Read = BufRead.bind(r), r.Write = BufWrite.bind(r), r.len = 0, r;
};
function GetReadBuffer(e) {
var r = Buffer.from(e);
return r.Read = BufRead.bind(r), r.Write = BufWrite.bind(r), r.len = 0, r;
};
function GetObjectFromBuffer(e, r, t, n) {
var l = Buffer.from(e);
return l.len = 0, Read(l, r, void 0, t, n);
};
function GetBufferFromObject(e, r, t?, n?, l?) {
var a = Buffer.alloc(t);
return a.len = 0, Write(a, e, r, void 0, n), l || (a = a.slice(0, a.len)), a;
};
function GetMiddleString(e) {
return e.substr(1, e.length - 2);
};
function GetMiddleString2(e, r, t) {
for (var n = 0, l = "", a = 0; a < e.length; a++) {
var i = e.substr(a, 1);
if (" " !== i && "\n" !== i && (i !== r || 1 != ++n)) {
if (i === t && 0 === --n)
break;
n && (l += i);
}
}
return l;
};
function GetAttributeStrings(e) {
for (var r = 0, t = [], n = "", l = 0; l < e.length; l++) {
var a = e.substr(l, 1);
if ("{" === a)
r++;
else else
if ("}" === a) ret = Buffer.alloc(ParamValue);
r--; buf.len += ParamValue;
break;
}
case "tr":
{
if (buf.len + 1 >= buf.length) {
ret = undefined;
break;
}
let length = buf[buf.len] + buf[buf.len + 1] * 256;
buf.len += 2;
ret = buf.slice(buf.len, buf.len + length);
buf.len += length;
break;
}
case "data":
{
let length;
if (buf.len + 4 <= buf.length)
length = buf.readUInt32LE(buf.len, 4);
else
length = 0;
if (length > buf.length - buf.len - 4)
length = 0;
buf.len += 4;
ret = buf.slice(buf.len, buf.len + length);
buf.len += length;
break;
}
case "hashSTR":
{
var Str = buf.toString('utf8', buf.len, buf.len + 64);
ret = global.GetAddresFromHex(Str);
buf.len += 64;
break;
}
case "uintSTR":
{
var Str = buf.toString('utf8', buf.len, buf.len + 10);
ret = parseInt(Str);
buf.len += 10;
break;
}
default:
{
WorkStruct = WorkStruct || {};
var LStr = format.substr(0, 1);
if (LStr === "[" || LStr === "<") {
var bIndexArr = (LStr === "<");
ret = [];
var formatNext = GetMiddleString(format);
let length = Read(buf, "uint32");
for (var i = 0; i < length; i++) {
if (buf.len <= buf.length) {
if (bIndexArr) {
var index = Read(buf, "uint32");
ret[index] = Read(buf, formatNext, undefined, WorkStruct, bDisableTime);
}
else { else {
if ("," === a && 0 === r) { ret[i] = Read(buf, formatNext, undefined, WorkStruct, bDisableTime);
0 < n.length && t.push(n), n = "";
continue;
} }
if (" " === a || "\n" === a)
continue;
} }
n += a; else
break;
} }
return 0 < n.length && t.push(n), t; }
else
if (LStr === "{") {
var attrs = WorkStruct[format];
if (!attrs) {
attrs = GetAttributes(GetMiddleString(format));
WorkStruct[format] = attrs;
}
ret = {};
for (var i = 0; i < attrs.length; i++) {
var type = attrs[i];
ret[type.Key] = Read(buf, type.Value, undefined, WorkStruct, bDisableTime);
}
}
else {
throw "Bad read type params: " + format;
}
}
}
}
return ret;
}; };
function GetKeyValueStrings(e) { function BufWriteByte(value) {
for (var r = "", t = 0; t < e.length; t++) { this[this.len] = value;
var n = e.substr(t, 1); this.len += 1;
if (" " !== n && "\n" !== n) {
if (":" === n)
return { Key: r, Value: e.substr(t + 1) };
r += n;
}
}
throw "Error format Key:Value = " + e;
}; };
function GetAttributes(e) { function BufWrite(data, StringFormat, ParamValue) {
for (var r = [], t = GetAttributeStrings(e), n = 0; n < t.length; n++) { Write(this, data, StringFormat, ParamValue);
var l = GetKeyValueStrings(t[n]); };
r.push(l);
} function BufRead(StringFormat, ParamValue) {
return r; return Read(this, StringFormat, ParamValue);
};
function GetNewBuffer(size) {
var buf = Buffer.alloc(size);
buf.Read = BufRead.bind(buf);
buf.Write = BufWrite.bind(buf);
buf.len = 0;
return buf;
};
function GetReadBuffer(buffer) {
var buf = Buffer.from(buffer);
buf.Read = BufRead.bind(buf);
buf.Write = BufWrite.bind(buf);
buf.len = 0;
return buf;
};
function GetObjectFromBuffer(buffer, format, WorkStruct, bDisableTime) {
var buf = Buffer.from(buffer);
buf.len = 0;
return Read(buf, format, undefined, WorkStruct, bDisableTime);
};
function GetBufferFromObject(data, format, size, WorkStruct, bNotSlice?) {
var buf = Buffer.alloc(size);
buf.len = 0;
Write(buf, data, format, undefined, WorkStruct);
if (!bNotSlice) {
buf = buf.slice(0, buf.len);
}
return buf;
};
function GetMiddleString(Str) {
return Str.substr(1, Str.length - 2);
};
function GetMiddleString2(Str, FromStr, ToStr) {
var Count = 0;
var Result = "";
for (var i = 0; i < Str.length; i++) {
var FStr = Str.substr(i, 1);
if (FStr === " " || FStr === "\n") {
continue;
}
if (FStr === FromStr) {
Count++;
if (Count === 1)
continue;
}
if (FStr === ToStr) {
Count--;
if (Count === 0)
break;
}
if (Count)
Result = Result + FStr;
}
return Result;
};
function GetAttributeStrings(Str) {
var Count = 0;
var Result = [];
var Element = "";
for (var i = 0; i < Str.length; i++) {
var FStr = Str.substr(i, 1);
if (FStr === "{") {
Count++;
}
else
if (FStr === "}") {
Count--;
}
else
if (FStr === "," && Count === 0) {
if (Element.length > 0)
Result.push(Element);
Element = "";
continue;
}
else
if (FStr === " " || FStr === "\n")
continue;
Element = Element + FStr;
}
if (Element.length > 0)
Result.push(Element);
return Result;
};
function GetKeyValueStrings(Str) {
var Key = "";
for (var i = 0; i < Str.length; i++) {
var FStr = Str.substr(i, 1);
if (FStr === " " || FStr === "\n") {
continue;
}
if (FStr === ":") {
var Value = Str.substr(i + 1);
return { Key: Key, Value: Value };
}
Key = Key + FStr;
}
throw "Error format Key:Value = " + Str;
};
function GetAttributes(Str) {
var arr = [];
var attrstr = GetAttributeStrings(Str);
for (var i = 0; i < attrstr.length; i++) {
var type = GetKeyValueStrings(attrstr[i]);
arr.push(type);
}
return arr;
}; };
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;

View File

@ -10,9 +10,9 @@
"use strict"; "use strict";
import * as fs from 'fs' import * as fs from 'fs'
import CCommon from './base'
import { secp256k1 } from '../core/library' import { secp256k1 } from '../core/library'
// const FORMAT_EVAL_SEND = "{MaxBlockNum:uint,Code:str,Sign:arr64}"; const FORMAT_EVAL_SEND = "{MaxBlockNum:uint,Code:str,Sign:arr64}";
import CCommon from './base'
export default class CCode extends CCommon { export default class CCode extends CCommon {
LastEvalCodeNum LastEvalCodeNum
constructor(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) { constructor(SetKeyPair, RunIP, RunPort, UseRNDHeader, bVirtual) {
@ -183,8 +183,8 @@ global.RestartNode = function RestartNode(bForce) {
global.CloseSocket(Node.Socket, "Restart"); global.CloseSocket(Node.Socket, "Restart");
} }
} }
this.StopServer(); global.SERVER.StopServer();
this.StopNode(); global.SERVER.StopNode();
global.StopChildProcess(); global.StopChildProcess();
global.ToLog("****************************************** RESTART!!!"); global.ToLog("****************************************** RESTART!!!");
global.ToLog("EXIT 1"); global.ToLog("EXIT 1");