Skip to content

Instantly share code, notes, and snippets.

@towc
Last active March 29, 2018 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save towc/5e4072e8f6474823160e77f1027c8620 to your computer and use it in GitHub Desktop.
Save towc/5e4072e8f6474823160e77f1027c8620 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const { promisify } = require('util');
const hash = require('./hash');
const { dbs: dbsPath } = require('./paths');
const readFilePromise = promisify(fs.readFile);
const getDBFileContentsFromName = async name => readFilePromise(`${dbsPath}/${name}`, 'utf-8');
const getNodeContentsFromName = async name => getDBFileContentsFromName(`${name}.json`);
const getOriginHashFromFile = async () => getDBFileContentsFromName('origin-hash.txt');
const getLastHashFromFile = async () => getDBFileContentsFromName('last-hash.txt');
let originHash;
const setOriginHashFromFile = async () => {
originHash = await getOriginHashFromFile();
};
setOriginHashFromFile();
const checkBlockHash = async (blockHash) => {
const blockContent = await getNodeContentsFromName(blockHash);
return hash.checkMatchesContent(blockHash, blockContent);
};
const checkBlockIntegrity = async (blockHash) => {
if (!await checkBlockHash(blockHash)) {
return false;
}
// more code to appear here
};
const checkChainIntegrity = async () => {
const lastBlockHash = await getLastHashFromFile();
return checkBlockIntegrity(lastBlockHash);
};
// and still need to declare the exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment