Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress hex data #24

Open
wants to merge 24 commits into
base: test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typings/
# dotenv environment variables file
.env

package-lock.json

Будет удалено .gitignore
Будет удалено .idea/
Expand Down
7 changes: 2 additions & 5 deletions Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ function Blockchain(config) {
const Frontend = require('./modules/frontend');
const app = express();


storj.put('app', app);
storj.put('config', config);



//Subsystems
const blockController = new (require('./modules/blockchain'))();
const NodeMetaInfo = require('./modules/NodeMetaInfo');
Expand Down Expand Up @@ -295,7 +292,7 @@ function Blockchain(config) {
}
blockHandler.changeMaxBlock(maxBlock);
transactor.changeMaxBlock(maxBlock);
blockchain.put(Number(index), JSON.stringify(block), function () {
blockchain.put(Number(index), block, function () {
if(!noHandle) {
blockHandler.handleBlock(block, cb);
} else {
Expand Down Expand Up @@ -1097,7 +1094,7 @@ function Blockchain(config) {
function getLatestBlock(callback) {
blockchain.get(maxBlock, function (err, val) {
if(!err) {
callback(JSON.parse(val));
callback(val);
} else {
callback(false);
}
Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const config = {
keyringKeysCount: 5, //Сколько генерировать ключей в связку при старте сети. Используется в Trusted консенсусе и других
checkExternalConnectionData: false, //Проверять внешние данные на соответствие
disableInternalToken: false, //выключить выпуск старых денег(false - разрешено выпускать старые деньги, true - запрет на выпуск)
compressHexData: false, //сжимать для хранения и разжимать при чтении 16ричные данные

//Tokens
precision: 10000000000, //Точность вычислений для кошелька
Expand Down
5 changes: 5 additions & 0 deletions modules/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class Block {
constructor(index, previousHash, timestamp, data, hash, startTimestamp, sign) {

if(typeof data === 'object') {
data = JSON.stringify(data);
}

this.index = index;
this.previousHash = String(previousHash).toString();
this.timestamp = timestamp;
Expand Down
13 changes: 6 additions & 7 deletions modules/blockHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class BlockHandler {
}

exBlockHandler(result, callback) {
this.handleBlock(JSON.parse(result), callback)
this.handleBlock(result, callback)
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ class BlockHandler {
try {
result = that.exBlockhainGet.sync(that, i);
if(prevBlock !== null) {
if(JSON.parse(prevBlock).hash !== JSON.parse(result).previousHash) {
if(prevBlock.hash !== result.previousHash) {
if(that.config.program.autofix) {
logger.info('Autofix: Delete chain data after ' + i + ' block');

Expand All @@ -215,13 +215,11 @@ class BlockHandler {
cb();
}


return;
break;
} else {
logger.disable = false;
console.log(JSON.parse(prevBlock));
console.log(JSON.parse(result));
console.log(prevBlock);
console.log(result);
logger.fatal('Saved chain corrupted in block ' + i + '. Remove wallets and blocks dirs for resync. Also you can use --autofix');
process.exit(1);
}
Expand Down Expand Up @@ -271,7 +269,7 @@ class BlockHandler {
}
let block;
try {
block = JSON.parse(val);
block = val;
block.data = JSON.parse(block.data);
} catch (e) {
return cb(false);
Expand Down Expand Up @@ -332,6 +330,7 @@ class BlockHandler {
logger.warning('Network without keyring');
}

that.wallet.keysPair.public = that.wallet.keysPair.public.replace(String.fromCharCode(10), "\\n");
if(that.isKeyFromKeyring(that.wallet.keysPair.public)) {
logger.warning('THRUSTED NODE. BE CAREFUL.');
}
Expand Down
49 changes: 48 additions & 1 deletion modules/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const KeyValue = require('./keyvalue');
const levelup = require('levelup');
const memdown = require('memdown');
const leveldown = require('leveldown');
const utils = require('./utils');

/**
* Blockchain manager object
Expand All @@ -33,11 +34,57 @@ class Blockchain {

get(key, callback) {
let that = this;
that.db.get(key, callback);
that.db.get(key, function (error, block) {
if (!error) {
try{
block = JSON.parse(block);

if(that.config.compressHexData){
if(typeof block === 'object'){
block = utils.decompressBlockPartsFromUnicode(block);
}
}
} catch (e) {
logger.error('Error prepare block getted from db.');
//console.log(block);
//console.log(e);
}
}
callback(error, block);
});
}

put(key, value, callback) {
let that = this;
if(that.config.compressHexData){
if(typeof value === 'object'){
let previousHash = value.previousHash || false;
let hash = value.hash || false;
let sign = value.sign || false;
if(previousHash && previousHash.length){
previousHash = utils.hexString2Unicode(previousHash);
if(false !== previousHash){
value.previousHash = previousHash;
}
}
if(hash && hash.length){
hash = utils.hexString2Unicode(hash);
if(false !== hash){
value.hash = hash;
} else {
console.log('ERROR COMPRESS HASH - '+value.hash);
process.exit();
}
}
if(sign && sign.length){
sign = utils.hexString2Unicode(sign);
if(false !== sign){
value.sign = sign;
}
}
}
}
value = JSON.stringify(value);
that.db.put(key, value, callback);
}

Expand Down
6 changes: 0 additions & 6 deletions modules/keyvalue.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class KeyValue {
that.levelup.get(key, options, callback);
break;
}


}

/**
Expand Down Expand Up @@ -117,8 +115,6 @@ class KeyValue {
that.levelup.put(key, value, callback);
break;
}


}

/**
Expand All @@ -141,8 +137,6 @@ class KeyValue {
that.levelup.del(key, options, callback);
break;
}


}


Expand Down
10 changes: 8 additions & 2 deletions modules/transactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const storj = require('./instanceStorage');
const logger = new (require('./logger'))();

const utils = require('./utils');

/**
* Реализация тёщи, которая пилит блок, пока он не будет принят в сеть
Expand Down Expand Up @@ -94,7 +94,7 @@ class Transactor {
}
return;// cb();
}
block = JSON.parse(block);

if(block.hash !== i.block.hash) {
logger.warning('Transactor: Block ' + i.block.index + ' was rejected and replaced.');
i.repeats++;
Expand Down Expand Up @@ -163,6 +163,12 @@ class Transactor {
* @var {Block} block
*/
logger.info('Transactor: Block ' + block.index + ' generated. Addes to watch list');

if(that.blockchainObject.config.compressHexData){
if(typeof block === 'object'){
block = utils.decompressBlockPartsFromUnicode(block);
}
}
that.transactions[index].block = block;
that.transactions[index].watchlist = watchlist;
that.transactions[index].index = index;
Expand Down
23 changes: 23 additions & 0 deletions modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,28 @@ module.exports = {
}

return str;
},

/**
* Decompress parts of block from utf-16 string to hex
* @param block
* @return {object}
*/
decompressBlockPartsFromUnicode: function (block) {
let previousHash = block.previousHash || false;
let hash = block.hash || false;
let sign = block.sign || false;

if (previousHash && previousHash.length) {
block.previousHash = this.unicode2HexString(previousHash);
}
if (hash && hash.length) {
block.hash = this.unicode2HexString(hash);
}
if (sign && sign.length) {
block.sign = this.unicode2HexString(sign);
}

return block;
}
};
4 changes: 0 additions & 4 deletions modules/validators/thrusted.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ function generateNextBlock(blockData, cb, cancelCondition, timestamp) {
return false;
}

if(typeof blockData === 'object') {
blockData = JSON.stringify(blockData);
}

blockchain.getLatestBlock(function (previousBlock) {
if(!previousBlock) {
return;
Expand Down