feat: add more type

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-08-08 14:28:30 +08:00
parent f8b5c6fb72
commit d2e29a1b6d
5 changed files with 33 additions and 29 deletions

View File

@ -118,7 +118,7 @@ export default class CDB extends CCode {
CheckBlocksOnStartReverse(StartNum: number) {
var delta = 1;
var Count = 0;
var PrevBlock;
var PrevBlock: TeraBlock;
for (var num = StartNum; num >= this.BlockNumDBMin + global.BLOCK_PROCESSING_LENGTH2; num -= delta) {
var Block = this.ReadBlockHeaderDB(num);
if (!Block || global.IsZeroArr(Block.SumHash)) {
@ -242,7 +242,7 @@ export default class CDB extends CCode {
WriteBodyResultDB(Block: TeraBlock) {
var arrTr = Block.arrContentResult;
if (Block.TrDataPos && Block.TrDataLen && Block.VersionBody && arrTr && arrTr.length) {
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, 1);
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, true);
var FD = FileItem.fd;
var Size = arrTr.length * 6;
var Position = Block.TrDataPos + Block.TrDataLen - Size;
@ -263,7 +263,7 @@ export default class CDB extends CCode {
return true;
}
WriteBodyDB(Block: TeraBlock) {
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, 1);
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, true);
var FD = FileItem.fd;
var Position = FileItem.size;
Block.TrDataPos = Position
@ -333,7 +333,7 @@ export default class CDB extends CCode {
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)
FileItem = global.BlockDB.OpenDBFile(FILE_NAME_BODY, true)
if (!Block.FilePos) {
if (!FileItem.size)
FileItem.size = 100
@ -347,7 +347,7 @@ export default class CDB extends CCode {
if (Block.FilePos >= FileItem.size) {
FileItem.size = Block.FilePos + BufWrite.length
}
FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, 1)
FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, true)
var Position = BlockNum * global.BLOCK_HEADER_SIZE2;
BufWrite = global.BufLib.GetBufferFromObject(Block, FORMAT_HEADER_VERSION2, global.BLOCK_HEADER_SIZE2, WorkStructHeader2)
written = fs.writeSync(FileItem.fd, BufWrite, 0, BufWrite.length, Position)
@ -365,7 +365,7 @@ export default class CDB extends CCode {
WriteBufHeaderToFile1(BufWrite: Buffer, BlockNum: number) {
BlockNum = Math.trunc(BlockNum)
this.TruncateBufMap(BlockNum)
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, 1);
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, true);
var Position = BlockNum * global.BLOCK_HEADER_SIZE;
var written = fs.writeSync(FileItem.fd, BufWrite, 0, BufWrite.length, Position);
if (Position >= FileItem.size) {
@ -468,7 +468,7 @@ export default class CDB extends CCode {
return undefined;
}
BlockNum = Math.trunc(BlockNum)
var Block, BufRead, FileItem, bytesRead, Position;
var Block: TeraBlock, BufRead: Buffer, FileItem, bytesRead: number, Position: number;
BufRead = global.BufLib.GetNewBuffer(global.BLOCK_HEADER_SIZE)
Position = BlockNum * global.BLOCK_HEADER_SIZE
var FileItem = global.BlockDB.OpenDBFile(FILE_NAME_HEADER);
@ -556,11 +556,11 @@ export default class CDB extends CCode {
TruncateBlockDBInner(LastBlock: TeraBlock) {
var FItem1, size;
if (global.DB_VERSION === 2) {
FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, 1)
FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, true)
size = (LastBlock.BlockNum + 1) * global.BLOCK_HEADER_SIZE2
}
else {
FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, 1)
FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, true)
size = (LastBlock.BlockNum + 1) * global.BLOCK_HEADER_SIZE
}
if (size < 0)
@ -575,13 +575,13 @@ export default class CDB extends CCode {
ClearDataBase() {
if (global.TX_PROCESS && global.TX_PROCESS.RunRPC)
global.TX_PROCESS.RunRPC("ClearDataBase", {})
var FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, 1);
var FItem1 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER, true);
FItem1.size = 0
fs.ftruncateSync(FItem1.fd, FItem1.size)
var FItem12 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, 1);
var FItem12 = global.BlockDB.OpenDBFile(FILE_NAME_HEADER2, true);
FItem12.size = 0
fs.ftruncateSync(FItem12.fd, FItem12.size)
var FItem2 = global.BlockDB.OpenDBFile(FILE_NAME_BODY, 1);
var FItem2 = global.BlockDB.OpenDBFile(FILE_NAME_BODY, true);
FItem2.size = 0
fs.ftruncateSync(FItem2.fd, FItem2.size)
this.DBHeader100.Truncate(- 1)

View File

@ -13,17 +13,16 @@ import * as fs from 'fs'
import Db from './db'
export default class DBRow extends Db {
FileName;
DataSize;
Format;
WorkStruct;
FileNameFull;
LastHash;
WasUpdate;
BufMap;
BufMapCount;
FileName: string;
DataSize: number;
Format: string;
WorkStruct: {};
FileNameFull: fs.PathLike;
LastHash: Buffer;
BufMap: { [x: string]: any; };
BufMapCount: number;
constructor(FileName, DataSize, Format, bReadOnly) {
constructor(FileName: string, DataSize: number, Format: string, bReadOnly: boolean) {
super()
this.FileName = FileName
this.DataSize = DataSize
@ -55,7 +54,7 @@ export default class DBRow extends Db {
this.DeleteMap(Data.Num)
var BufWrite = global.BufLib.GetBufferFromObject(Data, this.Format, this.DataSize, this.WorkStruct, 1);
var Position = Data.Num * this.DataSize;
var FI = this.OpenDBFile(this.FileName, 1);
var FI = this.OpenDBFile(this.FileName, true);
var written = fs.writeSync(FI.fd, BufWrite, 0, BufWrite.length, Position);
if (written !== BufWrite.length) {
global.TO_ERROR_LOG("DB-ROW", 10, "Error write to file:" + written + " <> " + BufWrite.length)
@ -116,7 +115,7 @@ export default class DBRow extends Db {
var Position = (LastNum + 1) * this.DataSize;
if (Position < 0)
Position = 0
var FI = this.OpenDBFile(this.FileName, 1);
var FI = this.OpenDBFile(this.FileName, true);
if (Position < FI.size) {
this.LastHash = undefined
this.WasUpdate = 1

View File

@ -14,8 +14,8 @@ import * as fs from 'fs'
export default class Db {
DBMap;
LastHash;
WasUpdate;
WasCheckPathDB;
WasUpdate: number;
WasCheckPathDB: boolean;
constructor() {
this.DBMap = {}
@ -24,7 +24,7 @@ export default class Db {
var Path = global.GetDataPath("DB");
global.CheckCreateDir(Path)
}
CloseDBFile(name, bdelete?) {
CloseDBFile(name: string, bdelete?: boolean) {
this.LastHash = undefined
this.WasUpdate = 1
var Item = this.DBMap[name];
@ -47,7 +47,7 @@ export default class Db {
delete this.DBMap[name]
}
}
OpenDBFile(name, bWrite?, bExist?) {
OpenDBFile(name: string, bWrite?: boolean, bExist?: boolean) {
if (bWrite && global.READ_ONLY_DB) {
global.ToLogTrace("CANNOT WRITE - DB IN READ_ONLY MODE!!!")
process.exit()

View File

@ -178,7 +178,7 @@ export default class CMessages extends CSmartContract {
if (!Tr.ToAll)
return;
var CurTime = global.GetCurrentTime(0) - 0;
var Count;
var Count: number;
if (global.GrayConnect())
Count = Math.trunc(global.MAX_GRAY_CONNECTIONS_TO_SERVER / 2)
else

View File

@ -100,6 +100,11 @@ export interface TeraJobTx {
}
export interface TeraBlock {
Hash2?: Buffer;
Hash1?: Buffer;
MinerName?: string;
Miner?: number;
Num?: number;
NodeNum?: number;
TransferNodesCount?: number;
StartMining?: boolean;