add override function for loading & saving blockchain
This commit is contained in:
parent
672d6daa8e
commit
fd987ebe4a
1 changed files with 37 additions and 15 deletions
|
@ -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) {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue