From 3fa0a34221e7ef8eebc3343c77cac19acc845039 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:10:13 +0300 Subject: [PATCH 01/15] no restart --- .../blockchain/Interface-Blockchain.js | 5 -- .../utils/saving-manager/Saving-Manager.js | 3 - .../satoshmindb/Interface-SatoshminDB.js | 68 +++---------------- 3 files changed, 9 insertions(+), 67 deletions(-) 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/utils/saving-manager/Saving-Manager.js b/src/common/blockchain/utils/saving-manager/Saving-Manager.js index d72d0f7a8..0df350d59 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); diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index fd5514b71..185ba96bb 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -27,21 +27,6 @@ class InterfaceSatoshminDB { } } - 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 { @@ -265,21 +250,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 +283,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; } From bf3001c4f5c4257ed43024ea7406c4d284b7bb1e Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:28:37 +0300 Subject: [PATCH 02/15] sleep to reduce memory increase --- .../mini-blockchain/blockchain/Mini-Blockchain-Advanced.js | 5 +++++ .../mini-blockchain/state/Mini-Blockchain-Accountant-Tree.js | 2 +- src/common/blockchain/utils/saving-manager/Saving-Manager.js | 2 +- src/common/trees/Interface-Tree.js | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) 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..2a6bdfae7 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{ @@ -98,6 +99,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(1000); + if (!(await this.accountantTree.saveMiniAccountant(true, "accountantTree", serialization))) throw {message: "saveMiniAccountant couldn't be saved"}; @@ -108,6 +111,8 @@ class MiniBlockchainAdvanced extends MiniBlockchain{ console.info("accountant tree saving profs"); + await Utils.sleep(1000); + 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/utils/saving-manager/Saving-Manager.js b/src/common/blockchain/utils/saving-manager/Saving-Manager.js index 0df350d59..1d1de3788 100644 --- a/src/common/blockchain/utils/saving-manager/Saving-Manager.js +++ b/src/common/blockchain/utils/saving-manager/Saving-Manager.js @@ -170,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/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){ From fc38700bad02c593ebdd92697aee797afd27f656 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:33:07 +0300 Subject: [PATCH 03/15] sleep to reduce memory increase #2 --- .../mini-blockchain/blockchain/Mini-Blockchain-Advanced.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 2a6bdfae7..014c0f025 100644 --- a/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js +++ b/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js @@ -99,7 +99,7 @@ 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(1000); + await Utils.sleep(5000); if (!(await this.accountantTree.saveMiniAccountant(true, "accountantTree", serialization))) throw {message: "saveMiniAccountant couldn't be saved"}; @@ -111,7 +111,7 @@ class MiniBlockchainAdvanced extends MiniBlockchain{ console.info("accountant tree saving profs"); - await Utils.sleep(1000); + await Utils.sleep(5000); if ( !(await this.prover.provesCalculated._saveProvesCalculated()) ) throw { message: "save proves calculated couldn't be saved" }; From d8dc715c2e8ee9406cbb22441716a79598bd1cc4 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:43:39 +0300 Subject: [PATCH 04/15] save called --- .../mini-blockchain/blockchain/Mini-Blockchain-Advanced.js | 2 ++ src/common/utils/Semaphore-Processing.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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 014c0f025..67c025051 100644 --- a/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js +++ b/src/common/blockchain/mini-blockchain/blockchain/Mini-Blockchain-Advanced.js @@ -88,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, ); 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}); }); } From 5cb66506925e286383554ce68a7593753f9c36a7 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:54:43 +0300 Subject: [PATCH 05/15] change the way data is being saved --- .../satoshmindb/Interface-SatoshminDB.js | 34 +++---------------- 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 185ba96bb..21abb044f 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -14,7 +14,6 @@ class InterfaceSatoshminDB { constructor(databaseName = consts.DATABASE_NAMES.DEFAULT_DATABASE) { this._dbName = databaseName; - this._beingRestarted = false; this._start(); } @@ -119,15 +118,13 @@ class InterfaceSatoshminDB { 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')); + attachment = new Blob([value.toString('hex')] ); + else{ //we are in node + if (!Buffer.isBuffer(attachment)) attachment = Buffer.from(value); + } try { - let deletion = await this._deleteDocument( key ); - if (!deletion) return false; - let result = await this.db.put({ _id: key, _attachments: { @@ -142,29 +139,6 @@ class InterfaceSatoshminDB { } catch (err) { - - if (err.status === 409) { - return await this._updateDocumentAttachment(key, attachment); - } else { - if (err.status === 404) { - - //if document not exist, create it and recall attachment - try { - - let response = this._createDocument(key, null); - return await this._saveDocumentAttachment(key, value); - - } catch (exception) { - - console.error('_saveDocumentAttachment raised an error for key ' + key, exception); - } - - } else { - console.error('_saveDocumentAttachment 222 raised an error for key ' + key, err); - throw err; - } - } - } } From 005796b3d2df9846c8f3af32d17938963185e827 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 19:58:26 +0300 Subject: [PATCH 06/15] force save --- .../satoshmindb/Interface-SatoshminDB.js | 29 ++----------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 21abb044f..c15751851 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -132,7 +132,8 @@ class InterfaceSatoshminDB { content_type: 'application/octet-binary', data: attachment } - } + }, + force: true, }); return result && result.ok; @@ -143,32 +144,6 @@ class InterfaceSatoshminDB { } - async _updateDocumentAttachment(key, value) { - - try { - - 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, - _attachments: { - key: { - content_type: 'application/octet-binary', - data: value - } - }, - _rev: doc._rev - }); - - return result && result.ok; - - } catch (err) { - console.error("error _updateDocumentAttachment2 " + key, err); - throw err; - } - } - async _deleteDocumentAttachment(key) { try { From 1382990173f3456d66c8f45efce7283acc605042 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 20:15:56 +0300 Subject: [PATCH 07/15] force save #2 --- src/common/satoshmindb/Interface-SatoshminDB.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index c15751851..621637165 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -125,6 +125,14 @@ class InterfaceSatoshminDB { try { + let doc = await this.db.get(key, {attachments: true}); + + let rev = {} + if (!doc || !doc.ok){ + rev._rev = doc._rev + rev.force = true + } + let result = await this.db.put({ _id: key, _attachments: { @@ -133,15 +141,14 @@ class InterfaceSatoshminDB { data: attachment } }, - force: true, + ...rev, }); return result && result.ok; } catch (err) { - + return false } - } async _deleteDocumentAttachment(key) { From 369c8a8ba5c5ae133b85256fb9c4bcebee0bb18b Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 20:22:04 +0300 Subject: [PATCH 08/15] force save #3 --- src/common/satoshmindb/Interface-SatoshminDB.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 621637165..ae63d6c09 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -127,8 +127,8 @@ class InterfaceSatoshminDB { let doc = await this.db.get(key, {attachments: true}); - let rev = {} - if (!doc || !doc.ok){ + const rev = {} + if (doc && doc.ok){ rev._rev = doc._rev rev.force = true } @@ -141,7 +141,8 @@ class InterfaceSatoshminDB { data: attachment } }, - ...rev, + _rev: rev._rev, + force: rev.force, }); return result && result.ok; From 715fd4af69f308fdb2ce1b0ae59b78333e7edd59 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 20:26:05 +0300 Subject: [PATCH 09/15] force save #4 --- .../satoshmindb/Interface-SatoshminDB.js | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index ae63d6c09..4bc1dd4c9 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -125,13 +125,8 @@ class InterfaceSatoshminDB { try { - let doc = await this.db.get(key, {attachments: true}); - - const rev = {} - if (doc && doc.ok){ - rev._rev = doc._rev - rev.force = true - } + let deletion = await this._deleteDocument( key ); + if (!deletion) return false; let result = await this.db.put({ _id: key, @@ -140,15 +135,63 @@ class InterfaceSatoshminDB { content_type: 'application/octet-binary', data: attachment } + } + }); + + return result && result.ok; + + } catch (err) { + + + if (err.status === 409) { + return await this._updateDocumentAttachment(key, attachment); + } else { + if (err.status === 404) { + + //if document not exist, create it and recall attachment + try { + + let response = this._createDocument(key, null); + return await this._saveDocumentAttachment(key, value); + + } catch (exception) { + + console.error('_saveDocumentAttachment raised an error for key ' + key, exception); + } + + } else { + console.error('_saveDocumentAttachment 222 raised an error for key ' + key, err); + throw err; + } + } + + } + + } + + async _updateDocumentAttachment(key, value) { + + try { + + 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, + _attachments: { + key: { + content_type: 'application/octet-binary', + data: value + } }, - _rev: rev._rev, - force: rev.force, + _rev: doc._rev }); return result && result.ok; } catch (err) { - return false + console.error("error _updateDocumentAttachment2 " + key, err); + throw err; } } From 9aeac8cb3419654338a151d0e4233a6d1bdd3434 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 20:36:19 +0300 Subject: [PATCH 10/15] force save #6 --- src/common/satoshmindb/Interface-SatoshminDB.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 4bc1dd4c9..2f83c9eae 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -74,7 +74,7 @@ class InterfaceSatoshminDB { 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 + else return Buffer.from( atob(response._attachments.key.data) ); //get attachment } catch (Exception) { From 205e7abfa25fff0836bff38a80d7fc0b8ac20345 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 21:49:45 +0300 Subject: [PATCH 11/15] refactor pouchdb --- .../PPoW-Blockchain-Proves-Calculated.js | 2 +- .../satoshmindb/Interface-SatoshminDB.js | 194 +++++------------- 2 files changed, 47 insertions(+), 149 deletions(-) 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/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 2f83c9eae..44366d7e8 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 { @@ -20,89 +17,31 @@ class InterfaceSatoshminDB { _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 _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) ); //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() }); @@ -113,126 +52,84 @@ 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 - if (!Buffer.isBuffer(attachment)) attachment = Buffer.from(value); - } + async _getDocument(key) { try { + let response = await this.db.get(key, {attachments: 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; + else if ( !response._attachments ) return response.value; + else return Buffer.from( response._attachments.key.data, 'base64'); //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, {attachments: true}); + 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){ } - } - - async _updateDocumentAttachment(key, value) { + let result - try { + if (Buffer.isBuffer(value)){ - let doc = await this.db.get(key, {attachments: true}); - if (!doc || !doc.ok) throw "db.get didn't work"; + if (process.env.BROWSER) + value = new Blob([value.toString('hex')] ); - let result = await this.db.put({ - _id: doc._id, + result = await this.db.put({ + _id: key, _attachments: { key: { content_type: 'application/octet-binary', 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; + }else { - if (doc._attachments) result = await this.db.removeAttachment( key , this._dbName, doc._rev); - else result = await this.db.remove( key , doc._rev); - - 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); @@ -264,6 +161,7 @@ class InterfaceSatoshminDB { this._getDocument(key).then( answer =>{ + console.log("KEY", key, answer ) resolve({result: answer } ); }).catch(exception => { From d75a5531ea44823507935b347461666957e655fc Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 22:07:57 +0300 Subject: [PATCH 12/15] fix load data --- src/common/satoshmindb/Interface-SatoshminDB.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 44366d7e8..d4a072bd5 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -58,8 +58,8 @@ class InterfaceSatoshminDB { let response = await this.db.get(key, {attachments: true}); if (!response ) return null; - else if ( !response._attachments ) return response.value; - else return Buffer.from( response._attachments.key.data, 'base64'); //get attachment + if ( !response._attachments ) return response.value; + return Buffer.from( Buffer.from( response._attachments.key.data, 'base64').toString(), "hex"); //get attachment } catch (err) { @@ -161,7 +161,6 @@ class InterfaceSatoshminDB { this._getDocument(key).then( answer =>{ - console.log("KEY", key, answer ) resolve({result: answer } ); }).catch(exception => { From eb3e8c36e81b9530786883518ce353bd2b79bc1c Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 22:23:56 +0300 Subject: [PATCH 13/15] interface satoshmin use blobs directly --- src/common/satoshmindb/Interface-SatoshminDB.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index d4a072bd5..d7fd59cfa 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -59,7 +59,8 @@ class InterfaceSatoshminDB { if (!response ) return null; if ( !response._attachments ) return response.value; - return Buffer.from( Buffer.from( response._attachments.key.data, 'base64').toString(), "hex"); //get attachment + if ( response._attachments.key ) return Buffer.from( Buffer.from( response._attachments.key.data, 'base64').toString(), "hex"); //get attachment + return Buffer.from( response._attachments.myBlob //get attachment } catch (err) { @@ -93,13 +94,13 @@ class InterfaceSatoshminDB { if (Buffer.isBuffer(value)){ if (process.env.BROWSER) - value = new Blob([value.toString('hex')] ); + value = new Blob([value] ); result = await this.db.put({ _id: key, _attachments: { - key: { - content_type: 'application/octet-binary', + myBlob: { + content_type: 'text/plain', data: value } }, From 16cc0505e3ec25749ef9c662228df870463493c6 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 22:24:39 +0300 Subject: [PATCH 14/15] fix typo --- src/common/satoshmindb/Interface-SatoshminDB.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index d7fd59cfa..6cde93a2b 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -60,7 +60,7 @@ class InterfaceSatoshminDB { 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 //get attachment + return Buffer.from( response._attachments.myBlob.data ) //get attachment } catch (err) { From 3d783d6b0c7fcaa08ede86ce9db7fc1f736c3551 Mon Sep 17 00:00:00 2001 From: Alexandru Ionut Budisteanu Date: Sat, 23 Oct 2021 22:44:33 +0300 Subject: [PATCH 15/15] more fixes for satoshmin db --- src/common/satoshmindb/Interface-SatoshminDB.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/satoshmindb/Interface-SatoshminDB.js b/src/common/satoshmindb/Interface-SatoshminDB.js index 6cde93a2b..04cef6a83 100644 --- a/src/common/satoshmindb/Interface-SatoshminDB.js +++ b/src/common/satoshmindb/Interface-SatoshminDB.js @@ -55,7 +55,7 @@ class InterfaceSatoshminDB { async _getDocument(key) { try { - let response = await this.db.get(key, {attachments: true}); + let response = await this.db.get(key, {attachments: true, binary: true }); if (!response ) return null; if ( !response._attachments ) return response.value; @@ -78,7 +78,7 @@ class InterfaceSatoshminDB { let _rev, force try{ - let response = await this.db.get(key, {attachments: true}); + let response = await this.db.get(key); if (response){ _rev = response._rev force = true