sync: sync upstream code

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-07-20 22:19:06 +08:00
parent b05a00f87d
commit a981ccbcae
7 changed files with 126 additions and 42 deletions

View File

@@ -327,7 +327,7 @@ export default class CDB extends CCode {
WriteBlockHeaderToFile2(Block) {
var BufWrite, FileItem, written;
var BlockNum = Math.trunc(Block.BlockNum);
this.ClearBufMap()
this.TruncateBufMap(BlockNum)
Block.VersionDB = global.DB_VERSION
BufWrite = global.BufLib.GetBufferFromObject(Block, FORMAT_STREAM_HEADER, 200, WorkStructStreamHeader)
FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, 1)
@@ -361,7 +361,7 @@ export default class CDB extends CCode {
}
WriteBufHeaderToFile1(BufWrite, BlockNum) {
BlockNum = Math.trunc(BlockNum)
this.ClearBufMap()
this.TruncateBufMap(BlockNum)
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, 1);
var Position = BlockNum * global.BLOCK_HEADER_SIZE;
var written = fs.writeSync(FileItem.fd, BufWrite, 0, BufWrite.length, Position);
@@ -510,11 +510,23 @@ export default class CDB extends CCode {
Block.Prepared = true
return this.PrepareBlockFields(Block, BlockNum);
}
ClearBufMap() {
this.MapHeader = {}
}
TruncateBufMap(BlockNum) {
if (BlockNum % 10000 === 0)
this.ClearBufMap()
else {
delete this.MapHeader[BlockNum]
}
}
ReadBlockHeaderFromMapDB(BlockNum) {
var Block = this.MapHeader[BlockNum];
if (!Block) {
Block = this.ReadBlockHeaderDB(BlockNum)
this.MapHeader[BlockNum] = Block
} else {
Block.FromMap = 1
}
return Block;
}
@@ -585,9 +597,6 @@ export default class CDB extends CCode {
StartSyncBlockchain(Node?, bSilent?, bCheckPoint?) {
// defiend in block-loader.ts(CBlock)
}
ClearBufMap() {
this.MapHeader = {}
}
Close() {
this.ClearBufMap()
this.ReadStateTX()

View File

@@ -28,9 +28,9 @@ ContenTypeMap["wav"] = "audio/wav";
ContenTypeMap["mp3"] = "audio/mpeg";
ContenTypeMap["mp4"] = "video/mp4";
ContenTypeMap["ico"] = "image/vnd.microsoft.icon";
ContenTypeMap["jpg"] = "image/jpeg";
ContenTypeMap["png"] = "image/png";
ContenTypeMap["gif"] = "image/gif";
ContenTypeMap["jpg"] = "image/jpeg";
ContenTypeMap["html"] = "text/html";
ContenTypeMap["txt"] = "text/plain";
ContenTypeMap["csv"] = "text/csv";
@@ -41,7 +41,6 @@ ContenTypeMap["pdf"] = "application/pdf";
ContenTypeMap["exe"] = "application/octet-stream";
ContenTypeMap["msi"] = "application/octet-stream";
ContenTypeMap["woff"] = "application/font-woff";
ContenTypeMap[".js"] = "application/javascript";
ContenTypeMap["html"] = "text/html";
ContenTypeMap["psd"] = "application/octet-stream";
global.HTTPCaller = {};
@@ -1358,7 +1357,11 @@ function SendWebFile(response, name?, StrCookie?, bParsing?) {
var type = name.substr(name.length - 4, 4);
if (type.substr(0, 1) === ".")
type = type.substr(1);
var Path = "./" + name;
var Path;
if (name.substr(0, 2) !== "./")
Path = "./" + name;
else
Path = name;
if (!fs.existsSync(Path)) {
if (type === "ico") {
response.writeHead(404, { 'Content-Type': 'text/html' });
@@ -1386,7 +1389,12 @@ function SendWebFile(response, name?, StrCookie?, bParsing?) {
data = GetFileHTMLWithParsing(Path);
response.end(data);
return;
}
} else
if ("image/jpeg,image/vnd.microsoft.icon,image/svg+xml,image/png,application/javascript,text/css,text/html".indexOf(StrContentType) > - 1) {
response.end(GetFileSimpleBin(Path));
return;
}
const stream = fs.createReadStream(Path);
setTimeout(function() {
stream.close();
@@ -1477,6 +1485,16 @@ function GetFileSimple(Path) {
return data;
};
function GetFileSimpleBin(Path) {
var Key = "GetFileSimpleBin-" + Path;
var data = global.SendHTMLMap[Key];
if (!data) {
data = (fs.readFileSync(Path));
global.SendHTMLMap[Key] = data;
}
return data;
};
function SaveFileSimple(Path, Str) {
global.SendHTMLMap = {};
var Key = "GetFileSimple-" + Path;
@@ -1490,6 +1508,13 @@ global.GetFileHTMLFromMarkdown = GetFileHTMLFromMarkdown;
global.GetFileSimple = GetFileSimple;
global.SaveFileSimple = SaveFileSimple;
function ReloaSenddBufer() {
global.SendHTMLMap = {};
};
setInterval(ReloaSenddBufer, 60 * 1000);
if (global.DEV_MODE)
setInterval(ReloaSenddBufer, 1 * 1000);
function GetStrTime(now) {
if (!now)
now = global.GetCurrentTime(0);
@@ -1752,6 +1777,7 @@ function GetEventArray(SmartNum, Context) {
return ArrRet;
};
global.HTTPCaller.GetHashRate = function(ArrParams) {
var AllReadCount = 0;
var CurBlockNum = global.GetCurrentBlockNumByTime();
var ResArr = [];
for (var i = 0; i < ArrParams.length; i++) {
@@ -1774,9 +1800,7 @@ global.HTTPCaller.GetHashRate = function(ArrParams) {
var StepDelta = Math.floor(Delta / Count);
if (StepDelta < 1)
StepDelta = 1;
var CountAvg = 10;
if (i === ArrParams.length - 1)
CountAvg = 3;
var CountAvg = 3;
var StepDeltaAvg = Math.floor(StepDelta / CountAvg);
if (StepDeltaAvg < 1)
StepDeltaAvg = 1;
@@ -1791,8 +1815,10 @@ global.HTTPCaller.GetHashRate = function(ArrParams) {
var Sum = 0;
var CountSum = 0;
for (var d = 0; d < CountAvg; d++) {
var Block = global.SERVER.ReadBlockHeaderDB(Num + d);
var Block = global.SERVER.ReadBlockHeaderFromMapDB(Num + d * StepDeltaAvg);
if (Block) {
if (!Block.FromMap)
AllReadCount++;
CountSum++;
Sum += global.GetPowPower(Block.PowHash);
}