diff --git a/src/common/blockchain/interface-blockchain/blockchain/Interface-Blockchain.js b/src/common/blockchain/interface-blockchain/blockchain/Interface-Blockchain.js index 85e63176a..28b155829 100644 --- a/src/common/blockchain/interface-blockchain/blockchain/Interface-Blockchain.js +++ b/src/common/blockchain/interface-blockchain/blockchain/Interface-Blockchain.js @@ -295,9 +295,6 @@ class InterfaceBlockchain extends InterfaceBlockchainBasic{ let block = await this._loadBlock( index, undefined, revertActions); - if (index > 0 && index % 50000 === 0) - await this.db.restart(); - if (index > 0 && index % 10000 === 0) await this.blocks.savingManager.saveBlockchain(); @@ -307,8 +304,6 @@ class InterfaceBlockchain extends InterfaceBlockchainBasic{ await this.blocks.savingManager.saveBlockchain(); - await this.db.restart(); - } catch (exception){ console.error("Error loading block", index, exception); diff --git a/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js b/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js index c12b1e92c..67c025051 100644 --- a/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js +++ b/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js @@ -3,6 +3,7 @@ import MiniBlockchain from "./Mini-Blockchain"; import MiniBlockchainAccountantTree from '../state/Mini-Blockchain-Accountant-Tree' import global from "consts/global" import Log from 'common/utils/logging/Log'; +import Utils from "../../../utils/helpers/Utils"; class MiniBlockchainAdvanced extends MiniBlockchain{ @@ -87,6 +88,8 @@ class MiniBlockchainAdvanced extends MiniBlockchain{ let save = async () => { + console.info("accountant tree saveMiniBlockchain called!"); + let length = this.blocks.length; let serialization = this.accountantTree.serializeMiniAccountant(true, ); @@ -98,6 +101,8 @@ class MiniBlockchainAdvanced extends MiniBlockchain{ console.info("accountant tree", this.accountantTree.root.hash.toString("hex")); console.info("accountant tree", this.accountantTree.root.edges.length, serialization.length ); + await Utils.sleep(5000); + if (!(await this.accountantTree.saveMiniAccountant(true, "accountantTree", serialization))) throw {message: "saveMiniAccountant couldn't be saved"}; @@ -108,6 +113,8 @@ class MiniBlockchainAdvanced extends MiniBlockchain{ console.info("accountant tree saving profs"); + await Utils.sleep(5000); + if ( !(await this.prover.provesCalculated._saveProvesCalculated()) ) throw { message: "save proves calculated couldn't be saved" }; diff --git a/src/common/blockchain/mini-blockchain/state/Mini-Blockchain-Accountant-Tree.js b/src/common/blockchain/mini-blockchain/state/Mini-Blockchain-Accountant-Tree.js index 392b6f865..d1703e8ca 100644 --- a/src/common/blockchain/mini-blockchain/state/Mini-Blockchain-Accountant-Tree.js +++ b/src/common/blockchain/mini-blockchain/state/Mini-Blockchain-Accountant-Tree.js @@ -198,7 +198,7 @@ class MiniBlockchainAccountantTree extends MiniBlockchainAccountantTreeEvents { } async saveMiniAccountant(includeHashes, name, serialization, timeout) { - return await this.saveTree(name || "accountantTree", includeHashes, serialization, timeout); + return this.saveTree(name || "accountantTree", includeHashes, serialization, timeout); } async loadMiniAccountant(buffer, offset, includeHashes, name = "accountantTree", showUpdate = true) { diff --git a/src/common/blockchain/ppow-blockchain/blockchain/prover/PPoW-Blockchain-Proves-Calculated.js b/src/common/blockchain/ppow-blockchain/blockchain/prover/PPoW-Blockchain-Proves-Calculated.js index fc436be63..08a65e515 100644 --- a/src/common/blockchain/ppow-blockchain/blockchain/prover/PPoW-Blockchain-Proves-Calculated.js +++ b/src/common/blockchain/ppow-blockchain/blockchain/prover/PPoW-Blockchain-Proves-Calculated.js @@ -202,7 +202,7 @@ class PPoWBlockchainProvesCalculated{ try{ - let buffer = await this.db.get(key, 20000, 20, true); + let buffer = await this.db.get(key); if ( !buffer || !Buffer.isBuffer(buffer)) { console.error("Proof for key "+key+" was not found"); diff --git a/src/common/blockchain/utils/saving-manager/Saving-Manager.js b/src/common/blockchain/utils/saving-manager/Saving-Manager.js index d72d0f7a8..1d1de3788 100644 --- a/src/common/blockchain/utils/saving-manager/Saving-Manager.js +++ b/src/common/blockchain/utils/saving-manager/Saving-Manager.js @@ -61,9 +61,6 @@ class SavingManager{ await block.saveBlock(); - if (block.height % 5000 === 0) - await this.blockchain.db.restart(); - } catch (exception){ Log.error("Saving raised an Error", Log.LOG_TYPE.SAVING_MANAGER, exception); @@ -173,7 +170,7 @@ class SavingManager{ async saveBlockchainLength(length ){ - let answer = await this.blockchain.db.save( this.blockchain._blockchainFileName, length, 20000, 1000000) ; + let answer = await this.blockchain.db.save( this.blockchain._blockchainFileName, length, 20000 ) ; if (!answer) { Log.error("Error saving the blocks.length", Log.LOG_TYPE.SAVING_MANAGER); diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index fd5514b71..04cef6a83 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -3,10 +3,7 @@ import consts from 'consts/const_global' -const atob = require('atob'); -import MainBlockchain from 'main-blockchain/Blockchain'; import StatusEvents from "common/events/Status-Events"; -import Utils from "common/utils/helpers/Utils"; let pounchdb = (process.env.BROWSER) ? (require('pouchdb-browser').default) : (require('pouchdb-node')); class InterfaceSatoshminDB { @@ -14,111 +11,37 @@ class InterfaceSatoshminDB { constructor(databaseName = consts.DATABASE_NAMES.DEFAULT_DATABASE) { this._dbName = databaseName; - this._beingRestarted = false; this._start(); } _start(){ try { - this.db = new pounchdb(this._dbName, {revs_limit: 1}); + this.db = new pounchdb(this._dbName, {revs_limit: 1, auto_compaction: true}); } catch (exception){ console.error("InterfaceSatoshminDB exception", pounchdb); } } - async restart(){ - - if ( this._beingRestarted ) - return await Utils.sleep ( 1500); - - this._beingRestarted = true; - - this.close(); - await Utils.sleep(1500); - this._start(); - - this._beingRestarted = false; - - } - - async _createDocument(key, value) { - - try { - - let deletion = await this._deleteDocumentAttachmentIfExist(key); - - let response = await this.db.put({_id: key, value: value}); - return response && response.ok; - - } catch (err) { - if (err.status === 409) - return this._updateDocument(key, value); - else { - console.error("_createDocument raised exception", key, err); - throw err; - } - } - - } - - async _updateDocument(key, value) { - - try { - - let doc = await this.db.get(key); - - let response = await this.db.put({ - _id: key, - _rev: doc._rev, - value: value - }); - - return response && response.ok; - - } catch (exception) { - console.error("_updateDocument error" + key, exception); - throw exception; - } - - } - - async _getDocument(key) { - - try { - let response = await this.db.get(key, {attachments: true}); - - if (!response ) return null; - else if ( !response._attachments ) return response.value; - else return Buffer.from(atob(response._attachments.key.data).toString('hex'), 'hex'); //get attachment - - } catch (Exception) { - - if (Exception.status === 404) //NOT FOUND - return null; - else { - console.error("error _getDocument ", Exception); - throw Exception; - } - } - - } async _deleteDocument(key) { try { - let doc = await this.db.get(key, {attachments: true}); + let doc = await this.db.get(key, {attachments: true} ); if (!doc) return false; - let response = await this.db.remove( doc ); + let rev = doc._rev + doc = null + + let response = await this.db.remove( key, rev ); return response && response.ok; } catch (err) { if (err.status === 404) //NOT FOUND return true; - else + if (err.status === 500) StatusEvents.emit("blockchain/logs", {message: "IndexedDB Error", reason: exception.reason.toString() }); @@ -129,125 +52,85 @@ class InterfaceSatoshminDB { } - async _saveDocumentAttachment(key, value) { - - let attachment = value; - // we need blob in browser - if (process.env.BROWSER && Buffer.isBuffer(value)) - attachment = new Blob([value.toString('hex')]); - else //we are in node - attachment = Buffer.from(value.toString('hex')); + async _getDocument(key) { try { + let response = await this.db.get(key, {attachments: true, binary: true }); - let deletion = await this._deleteDocument( key ); - if (!deletion) return false; - - let result = await this.db.put({ - _id: key, - _attachments: { - key: { - content_type: 'application/octet-binary', - data: attachment - } - } - }); - - return result && result.ok; + if (!response ) return null; + if ( !response._attachments ) return response.value; + if ( response._attachments.key ) return Buffer.from( Buffer.from( response._attachments.key.data, 'base64').toString(), "hex"); //get attachment + return Buffer.from( response._attachments.myBlob.data ) //get attachment } catch (err) { + if (err.status === 404) //NOT FOUND + return null; - if (err.status === 409) { - return await this._updateDocumentAttachment(key, attachment); - } else { - if (err.status === 404) { + console.error("error _getDocument ", err); + throw err; + } - //if document not exist, create it and recall attachment - try { + } - let response = this._createDocument(key, null); - return await this._saveDocumentAttachment(key, value); + async _saveDocument(key, value ) { - } catch (exception) { + let _rev, force - console.error('_saveDocumentAttachment raised an error for key ' + key, exception); - } + try{ + let response = await this.db.get(key); + if (response){ + _rev = response._rev + force = true - } else { - console.error('_saveDocumentAttachment 222 raised an error for key ' + key, err); - throw err; - } + response = null } + }catch(err){ } - } + let result - async _updateDocumentAttachment(key, value) { + if (Buffer.isBuffer(value)){ - try { + if (process.env.BROWSER) + value = new Blob([value] ); - let doc = await this.db.get(key, {attachments: true}); - if (!doc || !doc.ok) throw "db.get didn't work"; - - let result = await this.db.put({ - _id: doc._id, + result = await this.db.put({ + _id: key, _attachments: { - key: { - content_type: 'application/octet-binary', + myBlob: { + content_type: 'text/plain', data: value } }, - _rev: doc._rev + _rev, + force, }); - return result && result.ok; - - } catch (err) { - console.error("error _updateDocumentAttachment2 " + key, err); - throw err; - } - } - - async _deleteDocumentAttachment(key) { - try { - - let doc = await this.db.get(key); - - let result; - - if (doc._attachments) result = await this.db.removeAttachment( key , this._dbName, doc._rev); - else result = await this.db.remove( key , doc._rev); + }else { - return result || result.ok; + result = await this.db.put({ + _id: key, + value, + _rev, + force, + }); - } catch (exception) { - throw exception; } - } - async _deleteDocumentAttachmentIfExist(key) { + return result && result.ok; - try { - let deletion = await this._deleteDocumentAttachment(key); - return deletion; - } catch (err) { - return false; - } } - //main methods _save(key, value) { return new Promise(async (resolve)=>{ try { - if (Buffer.isBuffer(value)) - resolve(await this._saveDocumentAttachment(key, value)); - else - resolve(await this._createDocument(key, value)); + + resolve(await this._saveDocument(key, value)); } catch (exception) { console.error("db.save error " + key, exception); @@ -265,21 +148,9 @@ class InterfaceSatoshminDB { async save( key, value, timeout, trials = 10){ - //if (!trials) trials = 1; - trials = 1000000; - - let i=0; - while ( i < trials){ - - let out = await this._save(key, value, timeout); - if (out) - return out; - - if (i > 0 && i % 5 === 0 ) - await this.restart(); - - i++; - } + let out = await this._save(key, value, timeout); + if (out) + return out; return null; } @@ -310,42 +181,19 @@ class InterfaceSatoshminDB { async get(key, timeout=7000, trials = 20) { //if ( !trials ) trials = 1; - trials = 1000000; - - let i = 0; - while (i < trials) { - - let out = await this._get(key, timeout ); - if (out) - return out.result; - if (i > 0 && i % 5 === 0 ) - await this.restart(); - - i++; - } + let out = await this._get(key, timeout ); + if (out) + return out.result; return null; - } async remove(key, trials = 10) { - trials = 1000000; - - let i=0; - while ( i < trials){ - - let out = await this._deleteDocument(key); - if (out) - return out; - - if (i > 0 && i % 5 === 0 ) - await this.restart(); - - i++; - - } + let out = await this._deleteDocument(key); + if (out) + return out; return null; } diff --git a/src/common/trees/Interface-Tree.js b/src/common/trees/Interface-Tree.js index 580cf227c..17c4f16c7 100644 --- a/src/common/trees/Interface-Tree.js +++ b/src/common/trees/Interface-Tree.js @@ -355,7 +355,7 @@ class InterfaceTree{ if ( !serialization) serialization = this._serializeTree(includeHashes); - return await this.db.save(key, serialization, timeout); + return this.db.save(key, serialization, timeout); } async loadTree(key, buffer, offset=0, includeHashes){ diff --git a/src/common/utils/Semaphore-Processing.js b/src/common/utils/Semaphore-Processing.js index bfea5faf7..43ef5950f 100644 --- a/src/common/utils/Semaphore-Processing.js +++ b/src/common/utils/Semaphore-Processing.js @@ -28,7 +28,7 @@ class SemaphoreProcessing{ processSempahoreCallback(callback){ return new Promise ((resolve) =>{ - this._list .push({callback: callback, resolver: resolve}); + this._list.push({callback: callback, resolver: resolve}); }); }