const Block = require('./block'); class Blockchain { constructor() { this.chain = [Block.genesis()]; } //adds an existing block to the blockchain, returns false if the block can't be added, true if it was added addBlock(newBlock) { if (newBlock.lastHash !== this.chain[this.chain.length - 1].hash) { console.log("Tried to add invalid block, last hash didn't match our last hash"); return false; } //how to check if new block's timestamp is believable if (newBlock.difficulty !== Block.adjustDifficulty(this.chain[this.chain.length - 1], newBlock.timestamp)) { console.log("Tried to add invalid block, difficulty is incorrect"); return false; } if (!Block.checkBlock(newBlock)) { console.log("Tried to add invalid block, block's hash doesn't match its contents"); return false; } this.chain.push(newBlock); console.log("Added new block: "); //console.log(newBlock); return true; } isValidChain(chain) { if (chain.length === 0) { return false; } if (JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis())) { return false; } for (let i=1; i