From fd987ebe4a4347e97c33ba3e3ed0766de9d4a599 Mon Sep 17 00:00:00 2001 From: Daniel Yrovas Date: Tue, 12 Sep 2023 10:43:21 +1000 Subject: [PATCH] add override function for loading & saving blockchain --- blockchain/blockchain.js | 52 ++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/blockchain/blockchain.js b/blockchain/blockchain.js index d93c5e8..4fd00b1 100644 --- a/blockchain/blockchain.js +++ b/blockchain/blockchain.js @@ -5,7 +5,6 @@ const SensorRegistration = require('./sensor-registration'); const BrokerRegistration = require('./broker-registration'); const Integration = require('./integration'); const Compensation = require('./compensation'); -const fs = require('fs'); const ChainUtil = require('../util/chain-util'); const RdsStore = require('./rds-store'); const { @@ -984,21 +983,9 @@ function findBlocksDifference(oldBlocks, newBlocks) { }; } -function saveToDisk(blockchain, location) { - try { - fs.writeFileSync( - location, - blockchain.serialize()); - } catch (err) { - console.log(`Couldn't save blockchain to disk: ${err}`); - return false; - } - return true; -} - function onChange(blockchain, newBlocks, oldBlocks, difference) { if (blockchain.persisting !== null) { - saveToDisk(blockchain, blockchain.persisting); + Blockchain.saveToDisk(blockchain, blockchain.persisting); } for (const listener of blockchain.listeners) { listener(newBlocks, oldBlocks, difference); @@ -1053,6 +1040,7 @@ class Blockchain { } static loadFromDisk(location) { + const fs = require('fs'); //possible race if deleted after check, but we live with it I guess const returning = new Blockchain(); @@ -1072,6 +1060,40 @@ class Blockchain { return returning; } + static saveToDisk(blockchain, location) { + const fs = require('fs'); + try { + fs.writeFileSync( + location, + blockchain.serialize()); + } catch (err) { + console.log(`Couldn't save blockchain to disk: ${err}`); + return false; + } + return true; + } + + static loadFromDiskOverrideFS(location, saveChain, loadChain) { + Blockchain.saveToDisk = (blockchain, location) => { + saveChain(location, blockchain.serialize()); // async call, no result can be returned; must assume success. + return true; + } + Blockchain.loadFromDisk = (location) => { + const returning = new Blockchain(); + returning.persisting = location; + const rawPersistedChain = loadChain(location); // sync call, because application has already loaded file, + // this simply retrieves it from memory + // TODO: Could add check for empty string to prevent error, however, result is the same so leaving for now. + const deserialized = Blockchain.deserialize(rawPersistedChain); + const replaceResult = returning.replaceChain(deserialized); + if (!replaceResult.result) { + console.log(`Couldn't deserialize chain at '${location}', starting from genesis: ${replaceResult.reason}`); + } + return returning; + } + return Blockchain.loadFromDisk(location); + } + //adds an existing block to the blockchain, returns false if the block can't be added, true if it was added addBlock(newBlock) { @@ -1160,4 +1182,4 @@ class Blockchain { } } -module.exports = Blockchain; \ No newline at end of file +module.exports = Blockchain;