refactor: extracting global constants

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-07-13 18:48:51 +08:00
parent 1509b956a9
commit 207db65a07
17 changed files with 286 additions and 264 deletions

View File

@ -1,7 +1,11 @@
export enum TYPE_TRANSACTION {
TYPE_TRANSACTION_FILE = 5,
TYPE_TRANSACTION_CREATE = 100,
TYPE_DEPRECATED_TRANSFER1 = 105,
TYPE_DEPRECATED_TRANSFER2 = 110,
TYPE_TRANSACTION_TRANSFER = 111,
TYPE_TRANSACTION_ACC_HASH = 119
TYPE_TRANSACTION_ACC_HASH = 119,
TYPE_TRANSACTION_SMART_CREATE = 130,
TYPE_TRANSACTION_SMART_RUN = 135,
TYPE_TRANSACTION_SMART_CHANGE = 140
}

181
src/constant/db-format.ts Normal file
View File

@ -0,0 +1,181 @@
export enum DB_FORMAT {
//#region account.ts
FORMAT_CREATE = "{\
Type:byte,\
Currency:uint,\
PubKey:arr33,\
Name:str40,\
Adviser:uint,\
Smart:uint32,\
Reserve:arr3,\
}",
FORMAT_MONEY_TRANSFER = '{\
Type:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Sign:arr64,\
}',
FORMAT_MONEY_TRANSFER_BODY = '{\
Type:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
}',
FORMAT_MONEY_TRANSFER2 = "{\
Type:byte,\
Version:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Sign:arr64,\
}",
FORMAT_MONEY_TRANSFER_BODY2 = "{\
Type:byte,\
Version:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
}",
FORMAT_MONEY_TRANSFER3 = "{\
Type:byte,\
Version:byte,\
Reserve:uint,\
FromID:uint,\
To:[{PubKey:tr,ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Body:tr,\
Sign:arr64,\
}",
FORMAT_MONEY_TRANSFER_BODY3 = "{\
Type:byte,\
Version:byte,\
Reserve:uint,\
FromID:uint,\
To:[{PubKey:tr,ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Body:tr,\
}",
FORMAT_ACCOUNT_HASH = "{\
Type:byte,\
BlockNum:uint,\
AccHash:buffer32,\
}",
FORMAT_ACCOUNT_HASH3 = "{\
Type:byte,\
BlockNum:uint,\
AccHash:buffer32,\
AccountMax:uint,\
SmartHash:buffer32,\
SmartCount:uint,\
}",
//#endregion
//#region file.ts
FORMAT_FILE_CREATE = "{\
type:byte,\
Name:str,\
ContentType:str,\
Reserve:arr10,\
Data:tr\
}",
//#endregion
//#region smart.ts
FORMAT_SMART_CREATE = "{\
Type:byte,\
TokenGenerate:byte,\
StartValue:uint,\
OwnerPubKey:byte,\
ISIN:str,\
Zip:byte,\
AccountLength:byte,\
StateFormat:str,\
Category1:byte,\
Category2:byte,\
Category3:byte,\
Reserve:arr20,\
IconBlockNum:uint,\
IconTrNum:uint16,\
ShortName:str5,\
Name:str,\
Description:str,\
Code:str,\
HTML:str,\
}",
FORMAT_SMART_RUN = "{\
Type:byte,\
Account:uint,\
MethodName:str,\
Params:str,\
FromNum:uint,\
OperationID:uint,\
Reserve:arr10,\
Sign:arr64,\
}",
FORMAT_SMART_CHANGE = "{\
Type:byte,\
Account:uint,\
Smart:uint32,\
Reserve:arr10,\
FromNum:uint,\
OperationID:uint,\
Sign:arr64,\
}",
// global.DApps.Smart.FORMAT_ROW
FORMAT_SMART_ROW = "{\
Version:byte,\
TokenGenerate:byte,\
ISIN:str12,\
Zip:byte,\
BlockNum:uint,\
TrNum:uint16,\
IconBlockNum:uint,\
IconTrNum:uint16,\
ShortName:str5,\
Name:str40,\
Account:uint,\
AccountLength:byte,\
Category1:byte,\
Category2:byte,\
Category3:byte,\
Owner:uint,\
Reserve:arr20,\
StateFormat:str,\
Description:str,\
Code:str,\
HTML:str,\
SumHash:hash,\
}",
//#endregion
//#region block-loader-const.ts
FORMAT_BLOCK_TRANSFER = "{\
BlockNum:uint,\
TreeHash:hash,\
arrContent:[tr],\
}",
FORMAT_REST_TRANSFER = "{\
Result:uint,\
Version:uint,\
Arr:[arr200],\
ProofHash:hash,\
ProofArrL:<hash>,\
ProofArrR:<hash>,\
}",
FORMAT_SMART_TRANSFER = "{\
Result:uint,\
Arr:[tr],\
}"
//#endregion
}

View File

@ -394,7 +394,7 @@ function TestCreateTr() {
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,
};
var Body = global.BufLib.GetBufferFromObject(TR, global.FORMAT_CREATE, 1000, {});
var Body = global.BufLib.GetBufferFromObject(TR, FORMAT_CREATE, 1000, {});
var startTime = process.hrtime();
var StartData = Date.now();
var nonce = CreateHashBodyPOWInnerMinPower(Body, 1000, 17);

View File

@ -17,11 +17,6 @@ global.PACKET_ALIVE_PERIOD = 4 * global.CONSENSUS_PERIOD_TIME;
global.PACKET_ALIVE_PERIOD_NEXT_NODE = global.PACKET_ALIVE_PERIOD / 2;
global.MAX_BLOCK_SEND = 8;
global.COUNT_TASK_FOR_NODE = 10;
global.FORMAT_BLOCK_TRANSFER = "{\
BlockNum:uint,\
TreeHash:hash,\
arrContent:[tr],\
}";
global.WRK_BLOCK_TRANSFER = {};
global.MAX_ACCOUNTS_TRANSFER = 1024;
global.MAX_SMARTS_TRANSFER = 10;
@ -29,15 +24,3 @@ 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

@ -14,6 +14,7 @@ import CRest from './rest-loader'
import { TYPE_TRANSACTION } from '../constant/account';
import { STreeBuffer } from './base';
import { RBTree } from './library';
import { DB_FORMAT } from '../constant/db-format';
//import * as crypto from 'crypto';
require('./block-loader-const');
const STAT_BLOCK_LOAD_PERIOD = global.CONSENSUS_PERIOD_TIME / 5;
@ -1009,7 +1010,7 @@ export default class CBlock extends CRest {
Info.Node.NextPing = global.MIN_PERIOD_PING
var Block = Info.Context.Block;
if (Block && !Block.TreeEq) {
var Data = global.BufLib.GetObjectFromBuffer(Info.Data, global.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
var Data = global.BufLib.GetObjectFromBuffer(Info.Data, DB_FORMAT.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
Info.Data = undefined
if (Data.BlockNum !== Block.BlockNum || global.CompareArr(Data.TreeHash, Block.TreeHash) !== 0) {
this.SetBlockNOSendToNode(Block, Info.Node, "NO")
@ -1325,7 +1326,7 @@ global.LoadBlockFromNetwork = function(Params, F) {
global.SERVER.SendF(Node, {
"Method": "GETBLOCK", "Data": { BlockNum: BlockNum, TreeHash: [] }, "Context": {
F: function(Info) {
var Block = global.BufLib.GetObjectFromBuffer(Info.Data, global.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
var Block = global.BufLib.GetObjectFromBuffer(Info.Data, DB_FORMAT.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
Info.Data = undefined;
if (!Block.BlockNum || Block.BlockNum !== Params.BlockNum) {
global.ToLog("Error get BlockNum:" + Params.BlockNum + " from " + global.NodeName(Info.Node), 2);

View File

@ -1039,7 +1039,7 @@ declare global {
namespace NodeJS {
interface Global {
//#region block-db.ts
BlockDB: any;
BlockDB: DBLib;
BLOCK_HEADER_SIZE: number;
BLOCK_HEADER_SIZE2: number;
DB_VERSION: number;

View File

@ -18,6 +18,7 @@ import * as fs from 'fs'
import * as querystring from 'querystring'
import * as crypto from 'crypto';
import { STreeBuffer } from './base';
import { TYPE_TRANSACTION } from '../constant/account';
var BlockTree = new STreeBuffer(300 * 1000, global.CompareItemHashSimple, "number");
var ContenTypeMap = {};
@ -222,7 +223,7 @@ function SendToResponceFile(response, Block, TrNum) {
var Body = Block.arrContent[TrNum];
if (Body && Body.data)
Body = Body.data;
if (Body && Body[0] === global.TYPE_TRANSACTION_FILE) {
if (Body && Body[0] === TYPE_TRANSACTION.TYPE_TRANSACTION_FILE) {
var TR = global.DApps.File.GetObjectTransaction(Body);
if (TR.ContentType.toLowerCase().indexOf("html") >= 0)
response.writeHead(200, { 'Content-Type': "text/plain" });
@ -269,7 +270,7 @@ function SendToResponceDappFile(response, Block, TrNum) {
var Body = Block.arrContent[TrNum];
if (Body) {
var Type = Body[0];
if (Type === global.TYPE_TRANSACTION_FILE) {
if (Type === TYPE_TRANSACTION.TYPE_TRANSACTION_FILE) {
var TR = global.DApps.File.GetObjectTransaction(Body);
Result = { result: 1, Type: Type, ContentType: TR.ContentType, Name: TR.Name, Body: TR.Data.toString('utf8') };
} else {

View File

@ -11,6 +11,7 @@
"use strict";
import CDB from './db/block-db'
import { TYPE_TRANSACTION } from '../constant/account';
import { DB_FORMAT } from '../constant/db-format';
export default class CRest extends CDB {
LoadRestContext
BlockNumDB
@ -150,7 +151,7 @@ export default class CRest extends CDB {
F: function(Info) {
if (Context.TxProof)
return;
var Data = global.BufLib.GetObjectFromBuffer(Info.Data, global.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
var Data = global.BufLib.GetObjectFromBuffer(Info.Data, DB_FORMAT.FORMAT_BLOCK_TRANSFER, global.WRK_BLOCK_TRANSFER);
Info.Data = undefined
if (Data.BlockNum !== BlockProof.BlockNum || global.CompareArr(Data.TreeHash, BlockProof.TreeHash) !== 0) {
global.ToLog("Error get proof block from " + global.NodeName(Item.Node), 2)
@ -167,7 +168,7 @@ export default class CRest extends CDB {
var Body = Data.arrContent[n];
if (Body[0] === TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH) {
try {
FindTx = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_ACCOUNT_HASH3, {})
FindTx = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_ACCOUNT_HASH3, {})
}
catch (e) {
global.ToLog("Error parsing Body[" + n + "] block proof: " + e, 2)
@ -393,19 +394,16 @@ export default class CRest extends CDB {
SumPower = 0
Context.ArrProof.push({ Node: Info.Node, SumPower: SumPower, arr: arr, BufRead: BufRead })
}
static
GETSMART_F() {
static GETSMART_F() {
return "{\
SmartNum:uint,\
Count:uint,\
}";
}
static
RETSMART_F() {
return global.FORMAT_SMART_TRANSFER;
static RETSMART_F() {
return DB_FORMAT.FORMAT_SMART_TRANSFER;
}
static
GETREST_F() {
static GETREST_F() {
return "{\
BlockNum:uint,\
AccNum:uint,\
@ -413,9 +411,8 @@ export default class CRest extends CDB {
AccHash:hash,\
}";
}
static
RETREST_F() {
return global.FORMAT_REST_TRANSFER;
static RETREST_F() {
return DB_FORMAT.FORMAT_REST_TRANSFER;
}
SendLoadToBegin() {
return;
@ -462,7 +459,7 @@ function CheckHashSmarts(LastSumHash) {
return Num;
var WasSumHash = Item.SumHash;
Item.SumHash = [];
var Buf = global.BufLib.GetBufferFromObject(Item, global.DApps.Smart.FORMAT_ROW, 20000, WorkStruct);
var Buf = global.BufLib.GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, WorkStruct);
var Hash = global.sha3(Buf);
var SumHash = global.sha3arr2(PrevItem.SumHash, Hash);
if (global.CompareArr(SumHash, WasSumHash) !== 0)

View File

@ -131,7 +131,7 @@ export default class CSmartContract extends CConsensus {
var ResultStr = Result;
if (Result === true || typeof Result === "number") {
ResultStr = "Add to blockchain"
if (type === global.TYPE_TRANSACTION_FILE)
if (type === TYPE_TRANSACTION.TYPE_TRANSACTION_FILE)
ResultStr += ": file/" + BlockNum + "/" + i
}
item.cmd = "RetFindTX"

View File

@ -147,6 +147,7 @@ function RecreateAccountHashDB3() {
};
import '../core/db/db-row'
import { DB_FORMAT } from '../constant/db-format';
function ReWriteDBSmartWrite() {
global.UpdateMode = 1;
global.ToLog("Start ReWriteDBSmartWrite");
@ -154,7 +155,7 @@ function ReWriteDBSmartWrite() {
var Item = global.DApps.Smart.DBSmart.Read(num);
if (!Item)
break;
var Body = global.GetArrFromHex(GetBufferFromObject(Item, global.DApps.Smart.FORMAT_ROW, 20000, {}));
var Body = global.GetArrFromHex(GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, {}));
if (Body.length > 15000)
global.ToLog("Smart " + Item.Num + ". " + Item.Name + " length=" + Body.length);
global.DApps.Smart.DBSmartWrite(Item);

38
src/global.d.ts vendored
View File

@ -413,17 +413,15 @@ declare global {
//#region accounts.ts
HistoryDB: any;
TYPE_TRANSACTION_CREATE: number;
TYPE_TRANSACTION_ACC_HASH: number;
FORMAT_CREATE: string;
FORMAT_MONEY_TRANSFER: string;
FORMAT_MONEY_TRANSFER_BODY: string;
FORMAT_MONEY_TRANSFER2: string;
FORMAT_MONEY_TRANSFER_BODY2: string;
FORMAT_MONEY_TRANSFER3: string;
FORMAT_MONEY_TRANSFER_BODY3: string;
FORMAT_ACCOUNT_HASH: string;
FORMAT_ACCOUNT_HASH3: string;
// FORMAT_CREATE: string;
// FORMAT_MONEY_TRANSFER: string;
// FORMAT_MONEY_TRANSFER_BODY: string;
// FORMAT_MONEY_TRANSFER2: string;
// FORMAT_MONEY_TRANSFER_BODY2: string;
// FORMAT_MONEY_TRANSFER3: string;
// FORMAT_MONEY_TRANSFER_BODY3: string;
// FORMAT_ACCOUNT_HASH: string;
// FORMAT_ACCOUNT_HASH3: string;
//#endregion
@ -447,8 +445,8 @@ declare global {
//#endregion
//#region file.ts
TYPE_TRANSACTION_FILE: number;
FORMAT_FILE_CREATE: string;
// TYPE_TRANSACTION_FILE: number;
// FORMAT_FILE_CREATE: string;
//#endregion
// //#region dapp.ts
@ -468,10 +466,10 @@ declare global {
//#region smart.ts
TickCounter: number;
TYPE_TRANSACTION_SMART_RUN: number;
FORMAT_SMART_CREATE: string;
FORMAT_SMART_RUN
FORMAT_SMART_CHANGE
// TYPE_TRANSACTION_SMART_RUN: number;
// FORMAT_SMART_CREATE: string;
// FORMAT_SMART_RUN
// FORMAT_SMART_CHANGE
RunSmartMethod: Function;
DebugEvent: Function;
//#endregion
@ -594,12 +592,12 @@ declare global {
PACKET_ALIVE_PERIOD_NEXT_NODE: number;
MAX_BLOCK_SEND: number;
COUNT_TASK_FOR_NODE: number;
FORMAT_BLOCK_TRANSFER: string;
// FORMAT_BLOCK_TRANSFER: string;
WRK_BLOCK_TRANSFER: any;
MAX_ACCOUNTS_TRANSFER: number;
MAX_SMARTS_TRANSFER: number;
FORMAT_REST_TRANSFER: string;
FORMAT_SMART_TRANSFER: string;
// FORMAT_REST_TRANSFER: string;
// FORMAT_SMART_TRANSFER: string;
//#endregion
//#region server.ts

View File

@ -10,6 +10,7 @@
let WebApi2: any = {};
import * as crypto from 'crypto';
import { TYPE_TRANSACTION } from '../constant/account';
import { DB_FORMAT } from '../constant/db-format';
WebApi2.GenerateKeys = function(Params) {
var KeyPair = crypto.createECDH('secp256k1');
var PrivKey = global.sha3(crypto.randomBytes(32));
@ -23,7 +24,7 @@ WebApi2.CreateAccount = function(Params, response) {
KeyPair.setPrivateKey(Buffer.from(global.GetArrFromHex(Params.PrivKey)));
var PubKey = KeyPair.getPublicKey('' as any, 'compressed');
var TR: any = { Type: TYPE_TRANSACTION.TYPE_TRANSACTION_CREATE, Currency: Params.Currency, PubKey: PubKey, Name: Params.Name, Smart: Params.Smart, };
var Body = global.BufLib.GetBufferFromObject(TR, global.FORMAT_CREATE, 1000, {}, 1);
var Body = global.BufLib.GetBufferFromObject(TR, DB_FORMAT.FORMAT_CREATE, 1000, {}, 1);
Body = Body.slice(0, Body.len + 12);
SendTransaction(Body, TR as any, Params.Wait, function(result, text) {
var Result = {
@ -87,7 +88,7 @@ WebApi2.Send = function(Params, response, A, bJsonRet) {
if (!Params.FromPrivKey)
return { result: 0, Meta: Params.Meta, text: "Params.FromPrivKey required" };
TR.Sign = global.DApps.Accounts.GetSignTransferTx(TR, global.GetArrFromHex(Params.FromPrivKey));
var Body = global.BufLib.GetBufferFromObject(TR, global.FORMAT_MONEY_TRANSFER3, global.MAX_TRANSACTION_SIZE, {}, 1);
var Body = global.BufLib.GetBufferFromObject(TR, DB_FORMAT.FORMAT_MONEY_TRANSFER3, global.MAX_TRANSACTION_SIZE, {}, 1);
Body = Body.slice(0, Body.len + 12);
SendTransaction(Body, TR, Params.Wait, function(result, text) {
var Result = {
@ -209,7 +210,7 @@ WebApi2.SendRawTransaction = function(Params, response) {
return { result: 0, Meta: Params.Meta, text: "Params.Tx.Sign required" };
var TR = Params.Tx;
TxHexToArr(TR);
var Body = global.BufLib.GetBufferFromObject(TR, global.FORMAT_MONEY_TRANSFER3, global.MAX_TRANSACTION_SIZE, {}, 1);
var Body = global.BufLib.GetBufferFromObject(TR, DB_FORMAT.FORMAT_MONEY_TRANSFER3, global.MAX_TRANSACTION_SIZE, {}, 1);
Body = Body.slice(0, Body.len + 12);
SendTransaction(Body, TR, Params.Wait, function(result, text) {
var Result = {

View File

@ -91,6 +91,7 @@ process.on('error' as any, function(err: TeraError) {
global.ToLog(err.stack);
});
import CServerDB from '../core/db/block-db'
import { DB_FORMAT } from '../constant/db-format';
var KeyPair = crypto.createECDH('secp256k1');
KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]));
global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true);
@ -182,7 +183,7 @@ function GETBLOCK(msg) {
var BlockDB = global.SERVER.ReadBlockDB(BlockNum);
var StrSend;
if (BlockDB && (global.CompareArr(BlockDB.TreeHash, TreeHash) === 0 || global.IsZeroArr(TreeHash))) {
var BufWrite = global.BufLib.GetBufferFromObject(BlockDB, global.FORMAT_BLOCK_TRANSFER, global.MAX_PACKET_LENGTH, global.WRK_BLOCK_TRANSFER);
var BufWrite = global.BufLib.GetBufferFromObject(BlockDB, DB_FORMAT.FORMAT_BLOCK_TRANSFER, global.MAX_PACKET_LENGTH, global.WRK_BLOCK_TRANSFER);
StrSend = "OK";
}
if (StrSend === "OK") {
@ -260,7 +261,7 @@ function GETREST(msg) {
ProofArrL: ProofArrL,
ProofArrR: ProofArrR
};
var BufWrite = global.BufLib.GetBufferFromObject(Data2, global.FORMAT_REST_TRANSFER, BufLength, {});
var BufWrite = global.BufLib.GetBufferFromObject(Data2, DB_FORMAT.FORMAT_REST_TRANSFER, BufLength, {});
process.send({
cmd: "Send",
addrStr: msg.addrStr,
@ -288,7 +289,7 @@ function GETSMART(msg) {
Arr.push(BufSmart);
}
var Data2 = { Result: Arr.length ? 1 : 0, Arr: Arr };
var BufWrite = global.BufLib.GetBufferFromObject(Data2, global.FORMAT_SMART_TRANSFER, BufLength, {});
var BufWrite = global.BufLib.GetBufferFromObject(Data2, DB_FORMAT.FORMAT_SMART_TRANSFER, BufLength, {});
process.send({
cmd: "Send",
addrStr: msg.addrStr,

View File

@ -101,6 +101,7 @@ process.on('error' as any, function(err: TeraError) {
global.HTTP_PORT_NUMBER = 0;
import CServerDB from '../core/transaction-validator'
import { STreeBuffer } from '../core/base';
import { DB_FORMAT } from '../constant/db-format';
var KeyPair = crypto.createECDH('secp256k1');
KeyPair.setPrivateKey(Buffer.from([77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]));
global.SERVER = new CServerDB(KeyPair, undefined, undefined, false, true);
@ -328,7 +329,7 @@ global.TXWriteAccArr = TXWriteAccArr;
function TXWriteSmartArr(Params) {
var WorkStruct = {};
var WorkFormat = global.DApps.Smart.FORMAT_ROW;
var WorkFormat = DB_FORMAT.FORMAT_SMART_ROW;
global.ToLog("Write smarts: " + Params.StartNum + "-" + Params.Arr.length, 2);
for (var i = 0; i < Params.Arr.length; i++) {
var Data = global.BufLib.GetObjectFromBuffer(Params.Arr[i], WorkFormat, WorkStruct);
@ -345,7 +346,7 @@ function TXWriteAccHash() {
var Item = global.DApps.Smart.DBSmart.Read(num);
if (!Item)
break;
var Body = global.BufLib.GetBufferFromObject(Item, global.DApps.Smart.FORMAT_ROW, 20000, {});
var Body = global.BufLib.GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, {});
global.DApps.Smart.DBSmartWrite(Item);
}
global.DApps.Accounts.CalcMerkleTree(1);

View File

@ -14,6 +14,7 @@ import DBRow from '../core/db/db-row'
import '../core/rest_tables'
import { secp256k1 } from '../core/library'
import { TYPE_TRANSACTION } from '../constant/account'
import { DB_FORMAT } from '../constant/db-format'
const MAX_SUM_TER = 1e9;
const MAX_SUM_CENT = 1e9;
import DBLib from "../core/db/db"
@ -21,69 +22,9 @@ global.HistoryDB = new DBLib();
const FILE_NAME_HISTORY = "history-body";
var WorkStructHistory = {};
const BLOCK_CREATE_INTERVAL = 10;
global.TYPE_TRANSACTION_CREATE = 100;
const TYPE_DEPRECATED_TRANSFER1 = 105;
const TYPE_DEPRECATED_TRANSFER2 = 110;
const TYPE_TRANSACTION_TRANSFER = 111;
global.TYPE_TRANSACTION_ACC_HASH = 119;
global.FORMAT_CREATE = "{\
Type:byte,\
Currency:uint,\
PubKey:arr33,\
Name:str40,\
Adviser:uint,\
Smart:uint32,\
Reserve:arr3,\
}";
global.FORMAT_MONEY_TRANSFER = '{\
Type:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Sign:arr64,\
}';
const WorkStructTransfer = {};
global.FORMAT_MONEY_TRANSFER_BODY = global.FORMAT_MONEY_TRANSFER.replace("Sign:arr64,", "");
global.FORMAT_MONEY_TRANSFER2 = "{\
Type:byte,\
Version:byte,\
Currency:uint,\
FromID:uint,\
To:[{ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Sign:arr64,\
}";
const WorkStructTransfer2 = {};
global.FORMAT_MONEY_TRANSFER_BODY2 = global.FORMAT_MONEY_TRANSFER2.replace("Sign:arr64,", "");
global.FORMAT_MONEY_TRANSFER3 = "{\
Type:byte,\
Version:byte,\
Reserve:uint,\
FromID:uint,\
To:[{PubKey:tr,ID:uint,SumCOIN:uint,SumCENT:uint32}],\
Description:str,\
OperationID:uint,\
Body:tr,\
Sign:arr64,\
}";
const WorkStructTransfer3 = {};
global.FORMAT_MONEY_TRANSFER_BODY3 = global.FORMAT_MONEY_TRANSFER3.replace("Sign:arr64,", "");
global.FORMAT_ACCOUNT_HASH = "{\
Type:byte,\
BlockNum:uint,\
AccHash:buffer32,\
}";
global.FORMAT_ACCOUNT_HASH3 = "{\
Type:byte,\
BlockNum:uint,\
AccHash:buffer32,\
AccountMax:uint,\
SmartHash:buffer32,\
SmartCount:uint,\
}";
import DApp from './dapp'
class MerkleDBRow extends DBRow {
private MerkleTree
@ -321,7 +262,7 @@ export default class AccountApp extends DApp {
var Num = BlockNum;
return Num;
}
case TYPE_TRANSACTION_TRANSFER:
case TYPE_TRANSACTION.TYPE_TRANSACTION_TRANSFER:
var Num = global.ReadUintFromArr(Body, 1 + 1 + 6);
return Num;
case TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH:
@ -376,19 +317,19 @@ export default class AccountApp extends DApp {
Result = this.TRCreateAccount(Body, BlockNum, TrNum, ContextFrom)
break;
}
case TYPE_DEPRECATED_TRANSFER1:
case TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER1:
{
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, global.FORMAT_MONEY_TRANSFER, WorkStructTransfer)
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, DB_FORMAT.FORMAT_MONEY_TRANSFER, WorkStructTransfer)
break;
}
case TYPE_DEPRECATED_TRANSFER2:
case TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER2:
{
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, global.FORMAT_MONEY_TRANSFER2, WorkStructTransfer2)
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, DB_FORMAT.FORMAT_MONEY_TRANSFER2, WorkStructTransfer2)
break;
}
case TYPE_TRANSACTION_TRANSFER:
case TYPE_TRANSACTION.TYPE_TRANSACTION_TRANSFER:
{
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, global.FORMAT_MONEY_TRANSFER3, WorkStructTransfer3)
Result = this.TRTransferMoney(Block, Body, BlockNum, TrNum, DB_FORMAT.FORMAT_MONEY_TRANSFER3, WorkStructTransfer3)
break;
}
case TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH:
@ -479,27 +420,27 @@ export default class AccountApp extends DApp {
switch (Type) {
case TYPE_TRANSACTION.TYPE_TRANSACTION_CREATE:
{
format = global.FORMAT_CREATE
format = DB_FORMAT.FORMAT_CREATE
break;
}
case TYPE_DEPRECATED_TRANSFER1:
case TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER1:
{
format = global.FORMAT_MONEY_TRANSFER
format = DB_FORMAT.FORMAT_MONEY_TRANSFER
break;
}
case TYPE_DEPRECATED_TRANSFER2:
case TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER2:
{
format = global.FORMAT_MONEY_TRANSFER2
format = DB_FORMAT.FORMAT_MONEY_TRANSFER2
break;
}
case TYPE_TRANSACTION_TRANSFER:
case TYPE_TRANSACTION.TYPE_TRANSACTION_TRANSFER:
{
format = global.FORMAT_MONEY_TRANSFER3
format = DB_FORMAT.FORMAT_MONEY_TRANSFER3
break;
}
case TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH:
{
format = global.FORMAT_ACCOUNT_HASH3
format = DB_FORMAT.FORMAT_ACCOUNT_HASH3
break;
}
default:
@ -530,7 +471,7 @@ export default class AccountApp extends DApp {
return 1;
}
try {
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_ACCOUNT_HASH3, {});
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_ACCOUNT_HASH3, {});
} catch (e) {
return 0;
}
@ -595,7 +536,7 @@ export default class AccountApp extends DApp {
return "Error min power POW for create account (update client)";
}
try {
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_CREATE, {});
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_CREATE, {});
}
catch (e) {
return "Error transaction format";
@ -1286,9 +1227,9 @@ export default class AccountApp extends DApp {
if (TR.Version === 2 || TR.Version === 3) {
var format;
if (TR.Version === 2)
format = global.FORMAT_MONEY_TRANSFER_BODY2
format = DB_FORMAT.FORMAT_MONEY_TRANSFER_BODY2
else
format = global.FORMAT_MONEY_TRANSFER_BODY3
format = DB_FORMAT.FORMAT_MONEY_TRANSFER_BODY3
Arr = []
for (var i = 0; i < TR.To.length; i++) {
var Item = TR.To[i];
@ -1304,7 +1245,7 @@ export default class AccountApp extends DApp {
Arr[Arr.length] = Body[j]
}
else {
Arr = global.BufLib.GetBufferFromObject(TR, global.FORMAT_MONEY_TRANSFER_BODY, global.MAX_TRANSACTION_SIZE, {})
Arr = global.BufLib.GetBufferFromObject(TR, DB_FORMAT.FORMAT_MONEY_TRANSFER_BODY, global.MAX_TRANSACTION_SIZE, {})
}
var sigObj = secp256k1.sign(global.SHA3BUF(Arr), Buffer.from(PrivKey));
return sigObj.signature;
@ -1384,28 +1325,7 @@ export default class AccountApp extends DApp {
var App = new AccountApp;
global.DApps["Accounts"] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_CREATE] = App;
global.DAppByType[TYPE_DEPRECATED_TRANSFER1] = App;
global.DAppByType[TYPE_DEPRECATED_TRANSFER2] = App;
global.DAppByType[TYPE_TRANSACTION_TRANSFER] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER1] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_DEPRECATED_TRANSFER2] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_TRANSFER] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_ACC_HASH] = App;
// function TestStateFiles(Size, Format) {
// return;
// if (global.PROCESS_NAME !== "MAIN")
// return;
// var DBState1 = new DBRow("state-ok", Size, Format, 0);
// var DBState2 = new DBRow("state-no", Size, Format, 0);
// for (var Num = 0; 1; Num++) {
// var Item1 = DBState1.Read(Num);
// var Item2 = DBState2.Read(Num);
// if (!Item1 && !Item2)
// break;
// var Str1 = JSON.stringify(Item1);
// var Str2 = JSON.stringify(Item2);
// if (Str1 !== Str2) {
// global.ToLog("Err item: " + Num);
// global.ToLog("1: " + Str1);
// global.ToLog("2: " + Str2);
// }
// }
// };

View File

@ -9,11 +9,10 @@
*/
"use strict";
global.TYPE_TRANSACTION_FILE = 5;
global.FORMAT_FILE_CREATE = "{type:byte,Name:str,ContentType:str,Reserve:arr10,Data:tr}";
//@ts-ignore
let WorkStructRun = {};
import DApp from './dapp'
import { TYPE_TRANSACTION } from '../constant/account';
import { DB_FORMAT } from '../constant/db-format';
export default class FileApp extends DApp {
constructor() {
super()
@ -22,7 +21,7 @@ export default class FileApp extends DApp {
return true;
}
GetObjectTransaction(Body) {
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_FILE_CREATE, WorkStructRun);
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_FILE_CREATE, WorkStructRun);
return TR;
}
GetScriptTransaction(Body) {
@ -36,4 +35,4 @@ export default class FileApp extends DApp {
};
var App = new FileApp;
global.DApps.File = App;
global.DAppByType[global.TYPE_TRANSACTION_FILE] = App;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_FILE] = App;

View File

@ -15,87 +15,21 @@ 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;
global.FORMAT_SMART_CREATE = "{\
Type:byte,\
TokenGenerate:byte,\
StartValue:uint,\
OwnerPubKey:byte,\
ISIN:str,\
Zip:byte,\
AccountLength:byte,\
StateFormat:str,\
Category1:byte,\
Category2:byte,\
Category3:byte,\
Reserve:arr20,\
IconBlockNum:uint,\
IconTrNum:uint16,\
ShortName:str5,\
Name:str,\
Description:str,\
Code:str,\
HTML:str,\
}";
import { TYPE_TRANSACTION } from '../constant/account';
import { DB_FORMAT } from '../constant/db-format';
const WorkStructCreate = {};
global.FORMAT_SMART_RUN = "{\
Type:byte,\
Account:uint,\
MethodName:str,\
Params:str,\
FromNum:uint,\
OperationID:uint,\
Reserve:arr10,\
Sign:arr64,\
}";
//@ts-ignore
const WorkStructRun = {};
global.FORMAT_SMART_CHANGE = "{\
Type:byte,\
Account:uint,\
Smart:uint32,\
Reserve:arr10,\
FromNum:uint,\
OperationID:uint,\
Sign:arr64,\
}";
const WorkStructChange = {};
export default class SmartApp extends DApp {
FORMAT_ROW
ROW_SIZE
DBSmart
RowHole
constructor() {
super()
var bReadOnly = (global.PROCESS_NAME !== "TX");
this.FORMAT_ROW = "{\
Version:byte,\
TokenGenerate:byte,\
ISIN:str12,\
Zip:byte,\
BlockNum:uint,\
TrNum:uint16,\
IconBlockNum:uint,\
IconTrNum:uint16,\
ShortName:str5,\
Name:str40,\
Account:uint,\
AccountLength:byte,\
Category1:byte,\
Category2:byte,\
Category3:byte,\
Owner:uint,\
Reserve:arr20,\
StateFormat:str,\
Description:str,\
Code:str,\
HTML:str,\
SumHash:hash,\
}"
this.ROW_SIZE = 2 * (1 << 13)
this.DBSmart = new DBRow("smart", this.ROW_SIZE, this.FORMAT_ROW, bReadOnly)
this.DBSmart = new DBRow("smart", this.ROW_SIZE, DB_FORMAT.FORMAT_SMART_ROW, bReadOnly)
this.InitHole()
if (!bReadOnly)
this.Start()
@ -118,7 +52,7 @@ export default class SmartApp extends DApp {
var Type = Body[0];
if (Type && Body.length > 90) {
switch (Type) {
case global.TYPE_TRANSACTION_SMART_RUN:
case TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_RUN:
var len = 1 + 6;
len += 2 + Body[len] + Body[len + 1] * 256
if (len + 64 > Body.length)
@ -128,7 +62,7 @@ export default class SmartApp extends DApp {
return 0;
var Num = global.ReadUintFromArr(Body, len);
return Num;
case TYPE_TRANSACTION_SMART_CHANGE:
case TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CHANGE:
var Num = global.ReadUintFromArr(Body, 1);
return Num;
}
@ -155,13 +89,13 @@ export default class SmartApp extends DApp {
var Result;
try {
switch (Type) {
case TYPE_TRANSACTION_SMART_CREATE:
case TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CREATE:
Result = this.TRCreateSmart(Block, Body, BlockNum, TrNum, ContextFrom)
break;
case global.TYPE_TRANSACTION_SMART_RUN:
case TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_RUN:
Result = this.TRRunSmart(Block, Body, BlockNum, TrNum, ContextFrom)
break;
case TYPE_TRANSACTION_SMART_CHANGE:
case TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CHANGE:
Result = this.TRChangeSmart(Block, Body, BlockNum, TrNum, ContextFrom)
break;
}
@ -174,14 +108,14 @@ export default class SmartApp extends DApp {
GetScriptTransaction(Body) {
var Type = Body[0];
var format;
if (Type === TYPE_TRANSACTION_SMART_CREATE)
format = global.FORMAT_SMART_CREATE
if (Type === TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CREATE)
format = DB_FORMAT.FORMAT_SMART_CREATE
else
if (Type === global.TYPE_TRANSACTION_SMART_RUN)
format = global.FORMAT_SMART_RUN
if (Type === TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_RUN)
format = DB_FORMAT.FORMAT_SMART_RUN
else
if (Type === TYPE_TRANSACTION_SMART_CHANGE)
format = global.FORMAT_SMART_CHANGE
if (Type === TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CHANGE)
format = DB_FORMAT.FORMAT_SMART_CHANGE
if (!format)
return "";
var TR = global.BufLib.GetObjectFromBuffer(Body, format, {});
@ -200,7 +134,7 @@ export default class SmartApp extends DApp {
return "Error length transaction (max size)";
if (BlockNum < global.SMART_BLOCKNUM_START)
return "Error block num";
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_SMART_CREATE, WorkStructCreate);
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_SMART_CREATE, WorkStructCreate);
if (!TR.Name.trim())
return "Name required";
if (TR.AccountLength > 50)
@ -297,7 +231,7 @@ export default class SmartApp extends DApp {
return "Error length transaction (min size)";
if (BlockNum < global.SMART_BLOCKNUM_START)
return "Error block num";
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_SMART_RUN, WorkStructRun);
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_SMART_RUN, WorkStructRun);
var Account = global.DApps.Accounts.ReadStateTR(TR.Account);
if (!Account)
return "RunSmart: Error account Num: " + TR.Account;
@ -321,7 +255,7 @@ export default class SmartApp extends DApp {
return "Error length transaction (min size)";
if (BlockNum < global.SMART_BLOCKNUM_START)
return "Error block num";
var TR = global.BufLib.GetObjectFromBuffer(Body, global.FORMAT_SMART_CHANGE, WorkStructChange);
var TR = global.BufLib.GetObjectFromBuffer(Body, DB_FORMAT.FORMAT_SMART_CHANGE, WorkStructChange);
if (!ContextFrom) {
var ResultCheck = this.CheckSignFrom(Body, TR, BlockNum, TrNum);
if (typeof ResultCheck === "string")
@ -421,7 +355,7 @@ export default class SmartApp extends DApp {
else
PrevNum = Item.Num - 1
Item.SumHash = []
var Buf = global.BufLib.GetBufferFromObject(Item, this.FORMAT_ROW, 20000, {});
var Buf = global.BufLib.GetBufferFromObject(Item, DB_FORMAT.FORMAT_SMART_ROW, 20000, {});
var Hash = global.sha3(Buf);
if (PrevNum < 0)
Item.SumHash = Hash
@ -1215,6 +1149,6 @@ 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;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CREATE] = smartApp;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_RUN] = smartApp;
global.DAppByType[TYPE_TRANSACTION.TYPE_TRANSACTION_SMART_CHANGE] = smartApp;